blob: 429b9e1d5dc06465b70885472cf347466fee6f39 [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
Ben Wagner1a462bd2018-03-12 13:46:21 -04008#include "Test.h"
9
Ben Wagner501c17c2018-03-12 20:04:31 +000010#if SK_SUPPORT_GPU
11#include "GrContext.h"
12#include "GrRenderTargetContext.h"
13
14#include "SkCanvas.h"
15#include "SkSurface.h"
Robert Phillipsdbfecd02017-10-18 15:44:08 -040016
Brian Osman11052242016-10-27 14:47:55 -040017static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080018 uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080019 int w = rect.width();
20 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040021 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080022 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040023
24 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
25
26 if (!rtc->readPixels(dstInfo, pixels.get(), 0, rect.fLeft, rect.fTop)) {
27 return false;
28 }
29
bsalomone63ffef2016-02-05 07:17:34 -080030 for (int y = 0; y < h; ++y) {
31 for (int x = 0; x < w; ++x) {
32 uint32_t pixel = pixels.get()[y * w + x];
33 if (pixel != expectedValue) {
34 *actualValue = pixel;
35 *failX = x + rect.fLeft;
36 *failY = y + rect.fTop;
37 return false;
38 }
39 }
40 }
41 return true;
42}
43
Robert Phillipse4d45bf2017-06-02 14:53:40 -040044sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050045 return context->contextPriv().makeDeferredRenderTargetContext(
46 SkBackingFit::kExact, w, h,
47 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080048}
49
Brian Salomon43f8bf02017-10-18 08:33:29 -040050static void clear_op_test(skiatest::Reporter* reporter, GrContext* context) {
bsalomone63ffef2016-02-05 07:17:34 -080051 static const int kW = 10;
52 static const int kH = 10;
53
54 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040055 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080056
57 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
58 // it.
59 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
60 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
61 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
62 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
63 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
64
65 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
66 // it.
67 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
68 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
69 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
70 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
71 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
72
73 uint32_t actualValue;
74 int failX, failY;
75
76 static const GrColor kColor1 = 0xABCDEF01;
77 static const GrColor kColor2 = ~kColor1;
78
Robert Phillipse4d45bf2017-06-02 14:53:40 -040079 rtContext = newRTC(context, kW, kH);
80 SkASSERT(rtContext);
81
bsalomone63ffef2016-02-05 07:17:34 -080082 // Check a full clear
Chris Dalton344e9032017-12-11 15:42:09 -070083 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -040084 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080085 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
86 failX, failY);
87 }
88
Robert Phillipse4d45bf2017-06-02 14:53:40 -040089 rtContext = newRTC(context, kW, kH);
90 SkASSERT(rtContext);
91
bsalomone63ffef2016-02-05 07:17:34 -080092 // Check two full clears, same color
Chris Dalton344e9032017-12-11 15:42:09 -070093 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
94 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -040095 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080096 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
97 failX, failY);
98 }
99
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400100 rtContext = newRTC(context, kW, kH);
101 SkASSERT(rtContext);
102
bsalomone63ffef2016-02-05 07:17:34 -0800103 // Check two full clears, different colors
Chris Dalton344e9032017-12-11 15:42:09 -0700104 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
105 rtContext->clear(&fullRect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400106 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800107 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
108 failX, failY);
109 }
110
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400111 rtContext = newRTC(context, kW, kH);
112 SkASSERT(rtContext);
113
bsalomone63ffef2016-02-05 07:17:34 -0800114 // Test a full clear followed by a same color inset clear
Chris Dalton344e9032017-12-11 15:42:09 -0700115 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
116 rtContext->clear(&mid1Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400117 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800118 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
119 failX, failY);
120 }
121
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400122 rtContext = newRTC(context, kW, kH);
123 SkASSERT(rtContext);
124
bsalomone63ffef2016-02-05 07:17:34 -0800125 // Test a inset clear followed by same color full clear
Chris Dalton344e9032017-12-11 15:42:09 -0700126 rtContext->clear(&mid1Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
127 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400128 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
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400133 rtContext = newRTC(context, kW, kH);
134 SkASSERT(rtContext);
135
bsalomone63ffef2016-02-05 07:17:34 -0800136 // Test a full clear followed by a different color inset clear
Chris Dalton344e9032017-12-11 15:42:09 -0700137 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
138 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400139 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800140 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
141 failX, failY);
142 }
Brian Osman11052242016-10-27 14:47:55 -0400143 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
144 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
145 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
146 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800147 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
148 failX, failY);
149 }
150
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400151 rtContext = newRTC(context, kW, kH);
152 SkASSERT(rtContext);
153
bsalomone63ffef2016-02-05 07:17:34 -0800154 // Test a inset clear followed by a different full clear
Chris Dalton344e9032017-12-11 15:42:09 -0700155 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
156 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400157 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800158 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
159 failX, failY);
160 }
161
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400162 rtContext = newRTC(context, kW, kH);
163 SkASSERT(rtContext);
164
bsalomone63ffef2016-02-05 07:17:34 -0800165 // Check three nested clears from largest to smallest where outermost and innermost are same
166 // color.
Chris Dalton344e9032017-12-11 15:42:09 -0700167 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
168 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
169 rtContext->clear(&mid2Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400170 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800171 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
172 failX, failY);
173 }
Brian Osman11052242016-10-27 14:47:55 -0400174 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
175 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
176 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
177 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800178 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
179 failX, failY);
180 }
Brian Osman11052242016-10-27 14:47:55 -0400181 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
182 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
183 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
184 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800185 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
186 failX, failY);
187 }
188
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400189 rtContext = newRTC(context, kW, kH);
190 SkASSERT(rtContext);
191
bsalomone63ffef2016-02-05 07:17:34 -0800192 // Swap the order of the second two clears in the above test.
Chris Dalton344e9032017-12-11 15:42:09 -0700193 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
194 rtContext->clear(&mid2Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
195 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400196 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800197 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
198 failX, failY);
199 }
Brian Osman11052242016-10-27 14:47:55 -0400200 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
201 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
202 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
203 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800204 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
205 failX, failY);
206 }
207}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400208
209DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
210 clear_op_test(reporter, ctxInfo.grContext());
211 if (ctxInfo.backend() == kOpenGL_GrBackend) {
212 GrContextOptions options(ctxInfo.options());
213 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
214 sk_gpu_test::GrContextFactory workaroundFactory(options);
215 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
216 }
217}
218
Brian Salomon43f8bf02017-10-18 08:33:29 -0400219void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
220 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
221
222 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
223 SkCanvas* canvas = surf->getCanvas();
224
225 SkPaint paints[2];
226 paints[0].setColor(SK_ColorGREEN);
227 paints[1].setColor(SK_ColorGRAY);
228
229 static const int kLeftX = 158;
230 static const int kMidX = 258;
231 static const int kRightX = 383;
232 static const int kTopY = 26;
233 static const int kBotY = 51;
234
235 const SkRect rects[2] = {
236 { kLeftX, kTopY, kMidX, kBotY },
237 { kMidX, kTopY, kRightX, kBotY },
238 };
239
240 for (int i = 0; i < 2; ++i) {
241 // the bounds parameter is required to cause a full screen clear
242 canvas->saveLayer(&rects[i], nullptr);
243 canvas->drawRect(rects[i], paints[i]);
244 canvas->restore();
245 }
246
247 SkBitmap bm;
248 bm.allocPixels(ii, 0);
249
250 SkAssertResult(surf->readPixels(bm, 0, 0));
251
252 bool isCorrect = true;
253 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
254 const uint32_t* sl = bm.getAddr32(0, y);
255
256 for (int x = kLeftX; x < kMidX; ++x) {
257 if (SK_ColorGREEN != sl[x]) {
258 isCorrect = false;
259 break;
260 }
261 }
262
263 for (int x = kMidX; x < kRightX; ++x) {
264 if (SK_ColorGRAY != sl[x]) {
265 isCorrect = false;
266 break;
267 }
268 }
269 }
270
271 REPORTER_ASSERT(reporter, isCorrect);
272}
273// From crbug.com/768134
274DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
275 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
276 if (ctxInfo.backend() == kOpenGL_GrBackend) {
277 GrContextOptions options(ctxInfo.options());
278 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
279 sk_gpu_test::GrContextFactory workaroundFactory(options);
280 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
281 }
282}
283
284#endif