blob: 4a6e26c86a47b7a5af6fe5bdee3b86c4fa29c627 [file] [log] [blame]
bsalomone63ffef2016-02-05 07:17:34 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9
10#if SK_SUPPORT_GPU
11#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
bsalomone63ffef2016-02-05 07:17:34 -080013#include "GrGpu.h"
14#include "GrRenderTarget.h"
15#include "GrTexture.h"
16#include "GrTextureProvider.h"
17
Brian Osman11052242016-10-27 14:47:55 -040018static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080019 uint32_t* actualValue, int* failX, int* failY) {
Brian Osman11052242016-10-27 14:47:55 -040020 GrRenderTarget* rt = rtc->accessRenderTarget();
bsalomone63ffef2016-02-05 07:17:34 -080021 int w = rect.width();
22 int h = rect.height();
23 SkAutoTDeleteArray<uint32_t> pixels(new uint32_t[w * h]);
24 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
25 rt->readPixels(rect.fLeft, rect.fTop, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
26 for (int y = 0; y < h; ++y) {
27 for (int x = 0; x < w; ++x) {
28 uint32_t pixel = pixels.get()[y * w + x];
29 if (pixel != expectedValue) {
30 *actualValue = pixel;
31 *failX = x + rect.fLeft;
32 *failY = y + rect.fTop;
33 return false;
34 }
35 }
36 }
37 return true;
38}
39
Brian Osman11052242016-10-27 14:47:55 -040040static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) {
bsalomone63ffef2016-02-05 07:17:34 -080041 SkDEBUGCODE(uint32_t oldID = 0;)
Brian Osman11052242016-10-27 14:47:55 -040042 if (*rtc) {
43 SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();)
44 rtc->reset(nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080045 }
46 context->freeGpuResources();
47
Brian Osman11052242016-10-27 14:47:55 -040048 *rtc = context->makeRenderTargetContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig,
49 nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080050
Brian Osman11052242016-10-27 14:47:55 -040051 SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID);
robertphillipsd4c741e2016-04-28 09:55:15 -070052
Brian Osman11052242016-10-27 14:47:55 -040053 return *rtc != nullptr;
bsalomone63ffef2016-02-05 07:17:34 -080054}
55
bsalomon68d91342016-04-12 09:59:58 -070056DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070057 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -080058 static const int kW = 10;
59 static const int kH = 10;
60
61 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040062 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080063
64 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
65 // it.
66 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
67 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
68 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
69 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
70 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
71
72 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
73 // it.
74 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
75 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
76 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
77 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
78 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
79
80 uint32_t actualValue;
81 int failX, failY;
82
83 static const GrColor kColor1 = 0xABCDEF01;
84 static const GrColor kColor2 = ~kColor1;
85
Brian Osman11052242016-10-27 14:47:55 -040086 if (!reset_rtc(&rtContext, context, kW, kH)) {
87 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -080088 return;
89 }
90 // Check a full clear
Brian Osman11052242016-10-27 14:47:55 -040091 rtContext->clear(&fullRect, kColor1, false);
92 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080093 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
94 failX, failY);
95 }
96
Brian Osman11052242016-10-27 14:47:55 -040097 if (!reset_rtc(&rtContext, context, kW, kH)) {
98 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -080099 return;
100 }
101 // Check two full clears, same color
Brian Osman11052242016-10-27 14:47:55 -0400102 rtContext->clear(&fullRect, kColor1, false);
103 rtContext->clear(&fullRect, kColor1, false);
104 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800105 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
106 failX, failY);
107 }
108
Brian Osman11052242016-10-27 14:47:55 -0400109 if (!reset_rtc(&rtContext, context, kW, kH)) {
110 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800111 return;
112 }
113 // Check two full clears, different colors
Brian Osman11052242016-10-27 14:47:55 -0400114 rtContext->clear(&fullRect, kColor1, false);
115 rtContext->clear(&fullRect, kColor2, false);
116 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800117 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
118 failX, failY);
119 }
120
Brian Osman11052242016-10-27 14:47:55 -0400121 if (!reset_rtc(&rtContext, context, kW, kH)) {
122 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800123 return;
124 }
125 // Test a full clear followed by a same color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400126 rtContext->clear(&fullRect, kColor1, false);
127 rtContext->clear(&mid1Rect, kColor1, false);
128 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800129 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
130 failX, failY);
131 }
132
Brian Osman11052242016-10-27 14:47:55 -0400133 if (!reset_rtc(&rtContext, context, kW, kH)) {
134 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800135 return;
136 }
137 // Test a inset clear followed by same color full clear
Brian Osman11052242016-10-27 14:47:55 -0400138 rtContext->clear(&mid1Rect, kColor1, false);
139 rtContext->clear(&fullRect, kColor1, false);
140 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800141 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
142 failX, failY);
143 }
144
Brian Osman11052242016-10-27 14:47:55 -0400145 if (!reset_rtc(&rtContext, context, kW, kH)) {
146 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800147 return;
148 }
149 // Test a full clear followed by a different color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400150 rtContext->clear(&fullRect, kColor1, false);
151 rtContext->clear(&mid1Rect, kColor2, false);
152 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800153 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
154 failX, failY);
155 }
Brian Osman11052242016-10-27 14:47:55 -0400156 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
157 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
158 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
159 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800160 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
161 failX, failY);
162 }
163
Brian Osman11052242016-10-27 14:47:55 -0400164 if (!reset_rtc(&rtContext, context, kW, kH)) {
165 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800166 return;
167 }
168 // Test a inset clear followed by a different full clear
Brian Osman11052242016-10-27 14:47:55 -0400169 rtContext->clear(&mid1Rect, kColor2, false);
170 rtContext->clear(&fullRect, kColor1, false);
171 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800172 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
173 failX, failY);
174 }
175
Brian Osman11052242016-10-27 14:47:55 -0400176 if (!reset_rtc(&rtContext, context, kW, kH)) {
177 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800178 return;
179 }
180 // Check three nested clears from largest to smallest where outermost and innermost are same
181 // color.
Brian Osman11052242016-10-27 14:47:55 -0400182 rtContext->clear(&fullRect, kColor1, false);
183 rtContext->clear(&mid1Rect, kColor2, false);
184 rtContext->clear(&mid2Rect, kColor1, false);
185 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800186 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
187 failX, failY);
188 }
Brian Osman11052242016-10-27 14:47:55 -0400189 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
190 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
191 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
192 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800193 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
194 failX, failY);
195 }
Brian Osman11052242016-10-27 14:47:55 -0400196 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
197 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
198 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
199 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800200 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
201 failX, failY);
202 }
203
Brian Osman11052242016-10-27 14:47:55 -0400204 if (!reset_rtc(&rtContext, context, kW, kH)) {
205 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800206 return;
207 }
208 // Swap the order of the second two clears in the above test.
Brian Osman11052242016-10-27 14:47:55 -0400209 rtContext->clear(&fullRect, kColor1, false);
210 rtContext->clear(&mid2Rect, kColor1, false);
211 rtContext->clear(&mid1Rect, kColor2, false);
212 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800213 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
214 failX, failY);
215 }
Brian Osman11052242016-10-27 14:47:55 -0400216 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
217 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
218 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
219 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800220 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
221 failX, failY);
222 }
223}
224#endif