blob: 4cfd4f3b067f13aeb3c2041bc0f346cd65514fca [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/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkColorSpace.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkSurface.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040017#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/gpu/GrContextOptions.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040019#include "include/gpu/GrDirectContext.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040020#include "include/private/GrTypesPriv.h"
21#include "include/private/SkColorData.h"
Robert Phillipsd3442842019-08-02 12:26:22 -040022#include "src/core/SkAutoPixmapStorage.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrColor.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040024#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRenderTargetContext.h"
26#include "tests/Test.h"
27#include "tools/gpu/GrContextFactory.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040028
Ben Wagner9707a7e2019-05-06 17:17:19 -040029#include <cstdint>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040030#include <memory>
Robert Phillipsdbfecd02017-10-18 15:44:08 -040031
Adlai Hollerc95b5892020-08-11 12:02:22 -040032static bool check_rect(GrDirectContext* dContext, GrRenderTargetContext* rtc, const SkIRect& rect,
33 uint32_t expectedValue, uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080034 int w = rect.width();
35 int h = rect.height();
Robert Phillips301431d2017-03-29 12:08:49 -040036
37 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
38
Robert Phillipsd3442842019-08-02 12:26:22 -040039 SkAutoPixmapStorage readback;
40 readback.alloc(dstInfo);
41
42 readback.erase(~expectedValue);
Adlai Hollerc95b5892020-08-11 12:02:22 -040043 if (!rtc->readPixels(dContext, readback.info(), readback.writable_addr(), readback.rowBytes(),
Robert Phillipsd3442842019-08-02 12:26:22 -040044 {rect.fLeft, rect.fTop})) {
Robert Phillips301431d2017-03-29 12:08:49 -040045 return false;
46 }
47
bsalomone63ffef2016-02-05 07:17:34 -080048 for (int y = 0; y < h; ++y) {
49 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -040050 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -080051 if (pixel != expectedValue) {
52 *actualValue = pixel;
53 *failX = x + rect.fLeft;
54 *failY = y + rect.fTop;
55 return false;
56 }
57 }
58 }
59 return true;
60}
61
Robert Phillipsfe4b4812020-07-17 14:15:51 -040062std::unique_ptr<GrRenderTargetContext> newRTC(GrRecordingContext* rContext, int w, int h) {
Greg Daniele20fcad2020-01-08 11:52:34 -050063 return GrRenderTargetContext::Make(
Robert Phillipsfe4b4812020-07-17 14:15:51 -040064 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {w, h});
bsalomone63ffef2016-02-05 07:17:34 -080065}
66
Adlai Hollerc95b5892020-08-11 12:02:22 -040067static void clear_op_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
bsalomone63ffef2016-02-05 07:17:34 -080068 static const int kW = 10;
69 static const int kH = 10;
70
71 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Salomonbf6b9792019-08-21 09:38:10 -040072 std::unique_ptr<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080073
74 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
75 // it.
76 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
77 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
78 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
79 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
80 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
81
82 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
83 // it.
84 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
85 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
86 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
87 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
88 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
89
90 uint32_t actualValue;
91 int failX, failY;
92
93 static const GrColor kColor1 = 0xABCDEF01;
94 static const GrColor kColor2 = ~kColor1;
Brian Osman9a9baae2018-11-05 15:06:26 -050095 static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
96 static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
bsalomone63ffef2016-02-05 07:17:34 -080097
Adlai Hollerc95b5892020-08-11 12:02:22 -040098 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -040099 SkASSERT(rtContext);
100
bsalomone63ffef2016-02-05 07:17:34 -0800101 // Check a full clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400102 rtContext->clear(fullRect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400103 if (!check_rect(dContext, rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800104 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
105 failX, failY);
106 }
107
Adlai Hollerc95b5892020-08-11 12:02:22 -0400108 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400109 SkASSERT(rtContext);
110
bsalomone63ffef2016-02-05 07:17:34 -0800111 // Check two full clears, same color
Michael Ludwig81d41722020-05-26 16:57:38 -0400112 rtContext->clear(fullRect, kColor1f);
113 rtContext->clear(fullRect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400114 if (!check_rect(dContext, rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800115 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
116 failX, failY);
117 }
118
Adlai Hollerc95b5892020-08-11 12:02:22 -0400119 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400120 SkASSERT(rtContext);
121
bsalomone63ffef2016-02-05 07:17:34 -0800122 // Check two full clears, different colors
Michael Ludwig81d41722020-05-26 16:57:38 -0400123 rtContext->clear(fullRect, kColor1f);
124 rtContext->clear(fullRect, kColor2f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400125 if (!check_rect(dContext, rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800126 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
127 failX, failY);
128 }
129
Adlai Hollerc95b5892020-08-11 12:02:22 -0400130 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400131 SkASSERT(rtContext);
132
bsalomone63ffef2016-02-05 07:17:34 -0800133 // Test a full clear followed by a same color inset clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400134 rtContext->clear(fullRect, kColor1f);
135 rtContext->clear(mid1Rect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400136 if (!check_rect(dContext, rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800137 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
138 failX, failY);
139 }
140
Adlai Hollerc95b5892020-08-11 12:02:22 -0400141 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400142 SkASSERT(rtContext);
143
bsalomone63ffef2016-02-05 07:17:34 -0800144 // Test a inset clear followed by same color full clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400145 rtContext->clear(mid1Rect, kColor1f);
146 rtContext->clear(fullRect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400147 if (!check_rect(dContext, rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800148 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
149 failX, failY);
150 }
151
Adlai Hollerc95b5892020-08-11 12:02:22 -0400152 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400153 SkASSERT(rtContext);
154
bsalomone63ffef2016-02-05 07:17:34 -0800155 // Test a full clear followed by a different color inset clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400156 rtContext->clear(fullRect, kColor1f);
157 rtContext->clear(mid1Rect, kColor2f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400158 if (!check_rect(dContext, rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800159 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
160 failX, failY);
161 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400162 if (!check_rect(
163 dContext, rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
164 !check_rect(
165 dContext, rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
166 !check_rect(
167 dContext, rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
168 !check_rect(
169 dContext, rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800170 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
171 failX, failY);
172 }
173
Adlai Hollerc95b5892020-08-11 12:02:22 -0400174 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400175 SkASSERT(rtContext);
176
bsalomone63ffef2016-02-05 07:17:34 -0800177 // Test a inset clear followed by a different full clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400178 rtContext->clear(mid1Rect, kColor2f);
179 rtContext->clear(fullRect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400180 if (!check_rect(dContext, rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800181 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
182 failX, failY);
183 }
184
Adlai Hollerc95b5892020-08-11 12:02:22 -0400185 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400186 SkASSERT(rtContext);
187
bsalomone63ffef2016-02-05 07:17:34 -0800188 // Check three nested clears from largest to smallest where outermost and innermost are same
189 // color.
Michael Ludwig81d41722020-05-26 16:57:38 -0400190 rtContext->clear(fullRect, kColor1f);
191 rtContext->clear(mid1Rect, kColor2f);
192 rtContext->clear(mid2Rect, kColor1f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400193 if (!check_rect(dContext, rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800194 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
195 failX, failY);
196 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400197 if (!check_rect(
198 dContext, rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
199 !check_rect(
200 dContext, rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
201 !check_rect(
202 dContext, rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
203 !check_rect(
204 dContext, rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800205 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
206 failX, failY);
207 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400208 if (!check_rect(
209 dContext, rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
210 !check_rect(
211 dContext, rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
212 !check_rect(
213 dContext, rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
214 !check_rect(
215 dContext, rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800216 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
217 failX, failY);
218 }
219
Adlai Hollerc95b5892020-08-11 12:02:22 -0400220 rtContext = newRTC(dContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400221 SkASSERT(rtContext);
222
bsalomone63ffef2016-02-05 07:17:34 -0800223 // Swap the order of the second two clears in the above test.
Michael Ludwig81d41722020-05-26 16:57:38 -0400224 rtContext->clear(fullRect, kColor1f);
225 rtContext->clear(mid2Rect, kColor1f);
226 rtContext->clear(mid1Rect, kColor2f);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400227 if (!check_rect(dContext, rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800228 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
229 failX, failY);
230 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400231 if (!check_rect(
232 dContext, rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
233 !check_rect(
234 dContext, rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
235 !check_rect(
236 dContext, rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
237 !check_rect(
238 dContext, rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800239 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
240 failX, failY);
241 }
242}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400243
244DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500245 // Regular clear
Robert Phillips6d344c32020-07-06 10:56:46 -0400246 clear_op_test(reporter, ctxInfo.directContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500247
248 // Force drawing for clears
249 GrContextOptions options(ctxInfo.options());
250 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
251 sk_gpu_test::GrContextFactory workaroundFactory(options);
252 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400253}
254
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400255void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400256 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
257
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400258 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400259 SkCanvas* canvas = surf->getCanvas();
260
261 SkPaint paints[2];
262 paints[0].setColor(SK_ColorGREEN);
263 paints[1].setColor(SK_ColorGRAY);
264
265 static const int kLeftX = 158;
266 static const int kMidX = 258;
267 static const int kRightX = 383;
268 static const int kTopY = 26;
269 static const int kBotY = 51;
270
271 const SkRect rects[2] = {
272 { kLeftX, kTopY, kMidX, kBotY },
273 { kMidX, kTopY, kRightX, kBotY },
274 };
275
276 for (int i = 0; i < 2; ++i) {
277 // the bounds parameter is required to cause a full screen clear
278 canvas->saveLayer(&rects[i], nullptr);
279 canvas->drawRect(rects[i], paints[i]);
280 canvas->restore();
281 }
282
283 SkBitmap bm;
284 bm.allocPixels(ii, 0);
285
286 SkAssertResult(surf->readPixels(bm, 0, 0));
287
288 bool isCorrect = true;
289 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
290 const uint32_t* sl = bm.getAddr32(0, y);
291
292 for (int x = kLeftX; x < kMidX; ++x) {
293 if (SK_ColorGREEN != sl[x]) {
294 isCorrect = false;
295 break;
296 }
297 }
298
299 for (int x = kMidX; x < kRightX; ++x) {
300 if (SK_ColorGRAY != sl[x]) {
301 isCorrect = false;
302 break;
303 }
304 }
305 }
306
307 REPORTER_ASSERT(reporter, isCorrect);
308}
309// From crbug.com/768134
310DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500311 // Regular clear
Robert Phillips6d344c32020-07-06 10:56:46 -0400312 fullscreen_clear_with_layer_test(reporter, ctxInfo.directContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500313
314 // Use draws for clears
315 GrContextOptions options(ctxInfo.options());
316 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
317 sk_gpu_test::GrContextFactory workaroundFactory(options);
318 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400319}