blob: 32096091f9236d079e07deffa80eb39fdf75843c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Ben Wagner1a462bd2018-03-12 13:46:21 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkColorSpace.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkSurface.h"
19#include "include/gpu/GrContext.h"
20#include "include/gpu/GrContextOptions.h"
21#include "include/gpu/GrTypes.h"
22#include "include/private/GrColor.h"
23#include "src/gpu/GrContextPriv.h"
24#include "src/gpu/GrRenderTargetContext.h"
25#include "tests/Test.h"
26#include "tools/gpu/GrContextFactory.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040027
28#include <cstring>
29#include <memory>
Robert Phillipsdbfecd02017-10-18 15:44:08 -040030
Brian Osman11052242016-10-27 14:47:55 -040031static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080032 uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080033 int w = rect.width();
34 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040035 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080036 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040037
38 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
39
40 if (!rtc->readPixels(dstInfo, pixels.get(), 0, rect.fLeft, rect.fTop)) {
41 return false;
42 }
43
bsalomone63ffef2016-02-05 07:17:34 -080044 for (int y = 0; y < h; ++y) {
45 for (int x = 0; x < w; ++x) {
46 uint32_t pixel = pixels.get()[y * w + x];
47 if (pixel != expectedValue) {
48 *actualValue = pixel;
49 *failX = x + rect.fLeft;
50 *failY = y + rect.fTop;
51 return false;
52 }
53 }
54 }
55 return true;
56}
57
Robert Phillipse4d45bf2017-06-02 14:53:40 -040058sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
Greg Daniel4065d452018-11-16 15:43:41 -050059 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050060 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
61 return context->priv().makeDeferredRenderTargetContext(format, SkBackingFit::kExact, w, h,
62 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080063}
64
Brian Salomon43f8bf02017-10-18 08:33:29 -040065static void clear_op_test(skiatest::Reporter* reporter, GrContext* context) {
bsalomone63ffef2016-02-05 07:17:34 -080066 static const int kW = 10;
67 static const int kH = 10;
68
69 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040070 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080071
72 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
73 // it.
74 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
75 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
76 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
77 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
78 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
79
80 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
81 // it.
82 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
83 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
84 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
85 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
86 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
87
88 uint32_t actualValue;
89 int failX, failY;
90
91 static const GrColor kColor1 = 0xABCDEF01;
92 static const GrColor kColor2 = ~kColor1;
Brian Osman9a9baae2018-11-05 15:06:26 -050093 static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
94 static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
bsalomone63ffef2016-02-05 07:17:34 -080095
Robert Phillipse4d45bf2017-06-02 14:53:40 -040096 rtContext = newRTC(context, kW, kH);
97 SkASSERT(rtContext);
98
bsalomone63ffef2016-02-05 07:17:34 -080099 // Check a full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500100 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400101 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800102 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
103 failX, failY);
104 }
105
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400106 rtContext = newRTC(context, kW, kH);
107 SkASSERT(rtContext);
108
bsalomone63ffef2016-02-05 07:17:34 -0800109 // Check two full clears, same color
Brian Osman9a9baae2018-11-05 15:06:26 -0500110 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
111 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400112 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800113 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
114 failX, failY);
115 }
116
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400117 rtContext = newRTC(context, kW, kH);
118 SkASSERT(rtContext);
119
bsalomone63ffef2016-02-05 07:17:34 -0800120 // Check two full clears, different colors
Brian Osman9a9baae2018-11-05 15:06:26 -0500121 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
122 rtContext->clear(&fullRect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400123 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800124 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
125 failX, failY);
126 }
127
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400128 rtContext = newRTC(context, kW, kH);
129 SkASSERT(rtContext);
130
bsalomone63ffef2016-02-05 07:17:34 -0800131 // Test a full clear followed by a same color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500132 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
133 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400134 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800135 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
136 failX, failY);
137 }
138
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400139 rtContext = newRTC(context, kW, kH);
140 SkASSERT(rtContext);
141
bsalomone63ffef2016-02-05 07:17:34 -0800142 // Test a inset clear followed by same color full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500143 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
144 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400145 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800146 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
147 failX, failY);
148 }
149
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400150 rtContext = newRTC(context, kW, kH);
151 SkASSERT(rtContext);
152
bsalomone63ffef2016-02-05 07:17:34 -0800153 // Test a full clear followed by a different color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500154 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
155 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400156 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800157 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
158 failX, failY);
159 }
Brian Osman11052242016-10-27 14:47:55 -0400160 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
161 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
162 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
163 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800164 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
165 failX, failY);
166 }
167
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400168 rtContext = newRTC(context, kW, kH);
169 SkASSERT(rtContext);
170
bsalomone63ffef2016-02-05 07:17:34 -0800171 // Test a inset clear followed by a different full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500172 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
173 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400174 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800175 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
176 failX, failY);
177 }
178
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400179 rtContext = newRTC(context, kW, kH);
180 SkASSERT(rtContext);
181
bsalomone63ffef2016-02-05 07:17:34 -0800182 // Check three nested clears from largest to smallest where outermost and innermost are same
183 // color.
Brian Osman9a9baae2018-11-05 15:06:26 -0500184 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
185 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
186 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400187 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800188 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
189 failX, failY);
190 }
Brian Osman11052242016-10-27 14:47:55 -0400191 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
192 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
193 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
194 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800195 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
196 failX, failY);
197 }
Brian Osman11052242016-10-27 14:47:55 -0400198 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
199 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
200 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
201 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800202 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
203 failX, failY);
204 }
205
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400206 rtContext = newRTC(context, kW, kH);
207 SkASSERT(rtContext);
208
bsalomone63ffef2016-02-05 07:17:34 -0800209 // Swap the order of the second two clears in the above test.
Brian Osman9a9baae2018-11-05 15:06:26 -0500210 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
211 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
212 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400213 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800214 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
215 failX, failY);
216 }
Brian Osman11052242016-10-27 14:47:55 -0400217 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
218 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
219 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
220 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800221 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
222 failX, failY);
223 }
224}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400225
226DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500227 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400228 clear_op_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500229
230 // Force drawing for clears
231 GrContextOptions options(ctxInfo.options());
232 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
233 sk_gpu_test::GrContextFactory workaroundFactory(options);
234 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400235}
236
Brian Salomon43f8bf02017-10-18 08:33:29 -0400237void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
238 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
239
240 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
241 SkCanvas* canvas = surf->getCanvas();
242
243 SkPaint paints[2];
244 paints[0].setColor(SK_ColorGREEN);
245 paints[1].setColor(SK_ColorGRAY);
246
247 static const int kLeftX = 158;
248 static const int kMidX = 258;
249 static const int kRightX = 383;
250 static const int kTopY = 26;
251 static const int kBotY = 51;
252
253 const SkRect rects[2] = {
254 { kLeftX, kTopY, kMidX, kBotY },
255 { kMidX, kTopY, kRightX, kBotY },
256 };
257
258 for (int i = 0; i < 2; ++i) {
259 // the bounds parameter is required to cause a full screen clear
260 canvas->saveLayer(&rects[i], nullptr);
261 canvas->drawRect(rects[i], paints[i]);
262 canvas->restore();
263 }
264
265 SkBitmap bm;
266 bm.allocPixels(ii, 0);
267
268 SkAssertResult(surf->readPixels(bm, 0, 0));
269
270 bool isCorrect = true;
271 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
272 const uint32_t* sl = bm.getAddr32(0, y);
273
274 for (int x = kLeftX; x < kMidX; ++x) {
275 if (SK_ColorGREEN != sl[x]) {
276 isCorrect = false;
277 break;
278 }
279 }
280
281 for (int x = kMidX; x < kRightX; ++x) {
282 if (SK_ColorGRAY != sl[x]) {
283 isCorrect = false;
284 break;
285 }
286 }
287 }
288
289 REPORTER_ASSERT(reporter, isCorrect);
290}
291// From crbug.com/768134
292DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500293 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400294 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500295
296 // Use draws for clears
297 GrContextOptions options(ctxInfo.options());
298 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
299 sk_gpu_test::GrContextFactory workaroundFactory(options);
300 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400301}