2016-02-05 15:17:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
2016-10-27 18:47:55 +00:00
|
|
|
#include "GrRenderTargetContext.h"
|
2016-02-05 15:17:34 +00:00
|
|
|
#include "GrGpu.h"
|
|
|
|
#include "GrRenderTarget.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "GrTextureProvider.h"
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
|
2016-02-05 15:17:34 +00:00
|
|
|
uint32_t* actualValue, int* failX, int* failY) {
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTarget* rt = rtc->accessRenderTarget();
|
2016-02-05 15:17:34 +00:00
|
|
|
int w = rect.width();
|
|
|
|
int h = rect.height();
|
2016-11-02 21:07:33 +00:00
|
|
|
std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
|
2016-02-05 15:17:34 +00:00
|
|
|
memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
|
|
|
|
rt->readPixels(rect.fLeft, rect.fTop, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
uint32_t pixel = pixels.get()[y * w + x];
|
|
|
|
if (pixel != expectedValue) {
|
|
|
|
*actualValue = pixel;
|
|
|
|
*failX = x + rect.fLeft;
|
|
|
|
*failY = y + rect.fTop;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:38:40 +00:00
|
|
|
// TODO: this test does this thorough purging of the rendertargets b.c. right now
|
|
|
|
// the clear optimizations rely on the rendertarget's uniqueID. It can be
|
|
|
|
// relaxed when we switch that over to using rendertargetcontext ids (although
|
|
|
|
// we probably will want to have more clear values then too)
|
2016-10-27 18:47:55 +00:00
|
|
|
static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) {
|
2016-11-11 17:38:40 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID();
|
|
|
|
#endif
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (*rtc) {
|
|
|
|
SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();)
|
|
|
|
rtc->reset(nullptr);
|
2016-02-05 15:17:34 +00:00
|
|
|
}
|
|
|
|
context->freeGpuResources();
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
*rtc = context->makeRenderTargetContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig,
|
|
|
|
nullptr);
|
2016-04-28 16:55:15 +00:00
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID);
|
2016-02-05 15:17:34 +00:00
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
return *rtc != nullptr;
|
2016-02-05 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:59:58 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2016-02-05 15:17:34 +00:00
|
|
|
static const int kW = 10;
|
|
|
|
static const int kH = 10;
|
|
|
|
|
|
|
|
SkIRect fullRect = SkIRect::MakeWH(kW, kH);
|
2016-10-27 18:47:55 +00:00
|
|
|
sk_sp<GrRenderTargetContext> rtContext;
|
2016-02-05 15:17:34 +00:00
|
|
|
|
|
|
|
// A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
|
|
|
|
// it.
|
|
|
|
SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
|
|
|
|
SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
|
|
|
|
SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
|
|
|
|
SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
|
|
|
|
SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
|
|
|
|
|
|
|
|
// A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
|
|
|
|
// it.
|
|
|
|
SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
|
|
|
|
SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
|
|
|
|
SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
|
|
|
|
SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
|
|
|
|
SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
|
|
|
|
|
|
|
|
uint32_t actualValue;
|
|
|
|
int failX, failY;
|
|
|
|
|
|
|
|
static const GrColor kColor1 = 0xABCDEF01;
|
|
|
|
static const GrColor kColor2 = ~kColor1;
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check a full clear
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check two full clears, same color
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check two full clears, different colors
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&fullRect, kColor2, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Test a full clear followed by a same color inset clear
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&mid1Rect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Test a inset clear followed by same color full clear
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&mid1Rect, kColor1, false);
|
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Test a full clear followed by a different color inset clear
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&mid1Rect, kColor2, false);
|
|
|
|
if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Test a inset clear followed by a different full clear
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&mid1Rect, kColor2, false);
|
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check three nested clears from largest to smallest where outermost and innermost are same
|
|
|
|
// color.
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&mid1Rect, kColor2, false);
|
|
|
|
rtContext->clear(&mid2Rect, kColor1, false);
|
|
|
|
if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!reset_rtc(&rtContext, context, kW, kH)) {
|
|
|
|
ERRORF(reporter, "Could not create render target context.");
|
2016-02-05 15:17:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Swap the order of the second two clears in the above test.
|
2016-10-27 18:47:55 +00:00
|
|
|
rtContext->clear(&fullRect, kColor1, false);
|
|
|
|
rtContext->clear(&mid2Rect, kColor1, false);
|
|
|
|
rtContext->clear(&mid1Rect, kColor2, false);
|
|
|
|
if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
|
|
|
|
!check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
|
2016-02-05 15:17:34 +00:00
|
|
|
ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
|
|
|
|
failX, failY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|