blob: 9b171fd96d952542feec8a6dca31eb001bed6d8e [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
Brian Osman11052242016-10-27 14:47:55 -040032static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080033 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);
43 if (!rtc->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(),
44 {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
Robert Phillipsfe4b4812020-07-17 14:15:51 -040067static void clear_op_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
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
Robert Phillipsfe4b4812020-07-17 14:15:51 -040098 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400103 if (!check_rect(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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400108 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400114 if (!check_rect(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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400119 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400125 if (!check_rect(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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400130 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400136 if (!check_rect(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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400141 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400147 if (!check_rect(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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400152 rtContext = newRTC(rContext, 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);
Brian Osman11052242016-10-27 14:47:55 -0400158 if (!check_rect(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 }
Brian Osman11052242016-10-27 14:47:55 -0400162 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
163 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
164 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
165 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800166 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
167 failX, failY);
168 }
169
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400170 rtContext = newRTC(rContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400171 SkASSERT(rtContext);
172
bsalomone63ffef2016-02-05 07:17:34 -0800173 // Test a inset clear followed by a different full clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400174 rtContext->clear(mid1Rect, kColor2f);
175 rtContext->clear(fullRect, kColor1f);
Brian Osman11052242016-10-27 14:47:55 -0400176 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800177 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
178 failX, failY);
179 }
180
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400181 rtContext = newRTC(rContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400182 SkASSERT(rtContext);
183
bsalomone63ffef2016-02-05 07:17:34 -0800184 // Check three nested clears from largest to smallest where outermost and innermost are same
185 // color.
Michael Ludwig81d41722020-05-26 16:57:38 -0400186 rtContext->clear(fullRect, kColor1f);
187 rtContext->clear(mid1Rect, kColor2f);
188 rtContext->clear(mid2Rect, kColor1f);
Brian Osman11052242016-10-27 14:47:55 -0400189 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800190 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
191 failX, failY);
192 }
Brian Osman11052242016-10-27 14:47:55 -0400193 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
194 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
195 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
196 !check_rect(rtContext.get(), innerBottomEdge, 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
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400208 rtContext = newRTC(rContext, kW, kH);
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400209 SkASSERT(rtContext);
210
bsalomone63ffef2016-02-05 07:17:34 -0800211 // Swap the order of the second two clears in the above test.
Michael Ludwig81d41722020-05-26 16:57:38 -0400212 rtContext->clear(fullRect, kColor1f);
213 rtContext->clear(mid2Rect, kColor1f);
214 rtContext->clear(mid1Rect, kColor2f);
Brian Osman11052242016-10-27 14:47:55 -0400215 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800216 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
217 failX, failY);
218 }
Brian Osman11052242016-10-27 14:47:55 -0400219 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
220 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
221 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
222 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800223 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
224 failX, failY);
225 }
226}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400227
228DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500229 // Regular clear
Robert Phillips6d344c32020-07-06 10:56:46 -0400230 clear_op_test(reporter, ctxInfo.directContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500231
232 // Force drawing for clears
233 GrContextOptions options(ctxInfo.options());
234 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
235 sk_gpu_test::GrContextFactory workaroundFactory(options);
236 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400237}
238
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400239void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400240 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
241
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400242 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400243 SkCanvas* canvas = surf->getCanvas();
244
245 SkPaint paints[2];
246 paints[0].setColor(SK_ColorGREEN);
247 paints[1].setColor(SK_ColorGRAY);
248
249 static const int kLeftX = 158;
250 static const int kMidX = 258;
251 static const int kRightX = 383;
252 static const int kTopY = 26;
253 static const int kBotY = 51;
254
255 const SkRect rects[2] = {
256 { kLeftX, kTopY, kMidX, kBotY },
257 { kMidX, kTopY, kRightX, kBotY },
258 };
259
260 for (int i = 0; i < 2; ++i) {
261 // the bounds parameter is required to cause a full screen clear
262 canvas->saveLayer(&rects[i], nullptr);
263 canvas->drawRect(rects[i], paints[i]);
264 canvas->restore();
265 }
266
267 SkBitmap bm;
268 bm.allocPixels(ii, 0);
269
270 SkAssertResult(surf->readPixels(bm, 0, 0));
271
272 bool isCorrect = true;
273 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
274 const uint32_t* sl = bm.getAddr32(0, y);
275
276 for (int x = kLeftX; x < kMidX; ++x) {
277 if (SK_ColorGREEN != sl[x]) {
278 isCorrect = false;
279 break;
280 }
281 }
282
283 for (int x = kMidX; x < kRightX; ++x) {
284 if (SK_ColorGRAY != sl[x]) {
285 isCorrect = false;
286 break;
287 }
288 }
289 }
290
291 REPORTER_ASSERT(reporter, isCorrect);
292}
293// From crbug.com/768134
294DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500295 // Regular clear
Robert Phillips6d344c32020-07-06 10:56:46 -0400296 fullscreen_clear_with_layer_test(reporter, ctxInfo.directContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500297
298 // Use draws for clears
299 GrContextOptions options(ctxInfo.options());
300 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
301 sk_gpu_test::GrContextFactory workaroundFactory(options);
302 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400303}