blob: 13a696f91ff824f7ab96390ef5a3e821d910dd2b [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/GrContext.h"
19#include "include/gpu/GrContextOptions.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040025#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrRenderTargetContext.h"
27#include "tests/Test.h"
28#include "tools/gpu/GrContextFactory.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040029
Ben Wagner9707a7e2019-05-06 17:17:19 -040030#include <cstdint>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040031#include <memory>
Robert Phillipsdbfecd02017-10-18 15:44:08 -040032
Brian Osman11052242016-10-27 14:47:55 -040033static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080034 uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080035 int w = rect.width();
36 int h = rect.height();
Robert Phillips301431d2017-03-29 12:08:49 -040037
38 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
39
Robert Phillipsd3442842019-08-02 12:26:22 -040040 SkAutoPixmapStorage readback;
41 readback.alloc(dstInfo);
42
43 readback.erase(~expectedValue);
44 if (!rtc->readPixels(readback.info(), readback.writable_addr(), readback.rowBytes(),
45 {rect.fLeft, rect.fTop})) {
Robert Phillips301431d2017-03-29 12:08:49 -040046 return false;
47 }
48
bsalomone63ffef2016-02-05 07:17:34 -080049 for (int y = 0; y < h; ++y) {
50 for (int x = 0; x < w; ++x) {
Robert Phillipsd3442842019-08-02 12:26:22 -040051 uint32_t pixel = readback.addr32()[y * w + x];
bsalomone63ffef2016-02-05 07:17:34 -080052 if (pixel != expectedValue) {
53 *actualValue = pixel;
54 *failX = x + rect.fLeft;
55 *failY = y + rect.fTop;
56 return false;
57 }
58 }
59 }
60 return true;
61}
62
Brian Salomonbf6b9792019-08-21 09:38:10 -040063std::unique_ptr<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
Greg Daniele20fcad2020-01-08 11:52:34 -050064 return GrRenderTargetContext::Make(
65 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {w, h});
bsalomone63ffef2016-02-05 07:17:34 -080066}
67
Brian Salomon43f8bf02017-10-18 08:33:29 -040068static void clear_op_test(skiatest::Reporter* reporter, GrContext* context) {
bsalomone63ffef2016-02-05 07:17:34 -080069 static const int kW = 10;
70 static const int kH = 10;
71
72 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Salomonbf6b9792019-08-21 09:38:10 -040073 std::unique_ptr<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080074
75 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
76 // it.
77 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
78 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
79 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
80 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
81 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
82
83 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
84 // it.
85 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
86 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
87 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
88 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
89 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
90
91 uint32_t actualValue;
92 int failX, failY;
93
94 static const GrColor kColor1 = 0xABCDEF01;
95 static const GrColor kColor2 = ~kColor1;
Brian Osman9a9baae2018-11-05 15:06:26 -050096 static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
97 static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
bsalomone63ffef2016-02-05 07:17:34 -080098
Robert Phillipse4d45bf2017-06-02 14:53:40 -040099 rtContext = newRTC(context, kW, kH);
100 SkASSERT(rtContext);
101
bsalomone63ffef2016-02-05 07:17:34 -0800102 // Check a full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500103 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400104 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
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400109 rtContext = newRTC(context, kW, kH);
110 SkASSERT(rtContext);
111
bsalomone63ffef2016-02-05 07:17:34 -0800112 // Check two full clears, same color
Brian Osman9a9baae2018-11-05 15:06:26 -0500113 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
114 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400115 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800116 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
117 failX, failY);
118 }
119
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400120 rtContext = newRTC(context, kW, kH);
121 SkASSERT(rtContext);
122
bsalomone63ffef2016-02-05 07:17:34 -0800123 // Check two full clears, different colors
Brian Osman9a9baae2018-11-05 15:06:26 -0500124 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
125 rtContext->clear(&fullRect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400126 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800127 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
128 failX, failY);
129 }
130
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400131 rtContext = newRTC(context, kW, kH);
132 SkASSERT(rtContext);
133
bsalomone63ffef2016-02-05 07:17:34 -0800134 // Test a full clear followed by a same color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500135 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
136 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400137 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800138 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
139 failX, failY);
140 }
141
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400142 rtContext = newRTC(context, kW, kH);
143 SkASSERT(rtContext);
144
bsalomone63ffef2016-02-05 07:17:34 -0800145 // Test a inset clear followed by same color full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500146 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
147 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400148 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800149 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
150 failX, failY);
151 }
152
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400153 rtContext = newRTC(context, kW, kH);
154 SkASSERT(rtContext);
155
bsalomone63ffef2016-02-05 07:17:34 -0800156 // Test a full clear followed by a different color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500157 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
158 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400159 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800160 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
161 failX, failY);
162 }
Brian Osman11052242016-10-27 14:47:55 -0400163 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
164 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
165 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
166 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800167 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
168 failX, failY);
169 }
170
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400171 rtContext = newRTC(context, kW, kH);
172 SkASSERT(rtContext);
173
bsalomone63ffef2016-02-05 07:17:34 -0800174 // Test a inset clear followed by a different full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500175 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
176 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400177 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800178 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
179 failX, failY);
180 }
181
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400182 rtContext = newRTC(context, kW, kH);
183 SkASSERT(rtContext);
184
bsalomone63ffef2016-02-05 07:17:34 -0800185 // Check three nested clears from largest to smallest where outermost and innermost are same
186 // color.
Brian Osman9a9baae2018-11-05 15:06:26 -0500187 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
188 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
189 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400190 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800191 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
192 failX, failY);
193 }
Brian Osman11052242016-10-27 14:47:55 -0400194 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
195 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
196 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
197 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800198 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
199 failX, failY);
200 }
Brian Osman11052242016-10-27 14:47:55 -0400201 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
202 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
203 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
204 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800205 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
206 failX, failY);
207 }
208
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400209 rtContext = newRTC(context, kW, kH);
210 SkASSERT(rtContext);
211
bsalomone63ffef2016-02-05 07:17:34 -0800212 // Swap the order of the second two clears in the above test.
Brian Osman9a9baae2018-11-05 15:06:26 -0500213 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
214 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
215 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400216 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800217 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
218 failX, failY);
219 }
Brian Osman11052242016-10-27 14:47:55 -0400220 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
221 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
222 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
223 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800224 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
225 failX, failY);
226 }
227}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400228
229DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500230 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400231 clear_op_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500232
233 // Force drawing for clears
234 GrContextOptions options(ctxInfo.options());
235 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
236 sk_gpu_test::GrContextFactory workaroundFactory(options);
237 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400238}
239
Brian Salomon43f8bf02017-10-18 08:33:29 -0400240void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
241 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
242
243 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
244 SkCanvas* canvas = surf->getCanvas();
245
246 SkPaint paints[2];
247 paints[0].setColor(SK_ColorGREEN);
248 paints[1].setColor(SK_ColorGRAY);
249
250 static const int kLeftX = 158;
251 static const int kMidX = 258;
252 static const int kRightX = 383;
253 static const int kTopY = 26;
254 static const int kBotY = 51;
255
256 const SkRect rects[2] = {
257 { kLeftX, kTopY, kMidX, kBotY },
258 { kMidX, kTopY, kRightX, kBotY },
259 };
260
261 for (int i = 0; i < 2; ++i) {
262 // the bounds parameter is required to cause a full screen clear
263 canvas->saveLayer(&rects[i], nullptr);
264 canvas->drawRect(rects[i], paints[i]);
265 canvas->restore();
266 }
267
268 SkBitmap bm;
269 bm.allocPixels(ii, 0);
270
271 SkAssertResult(surf->readPixels(bm, 0, 0));
272
273 bool isCorrect = true;
274 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
275 const uint32_t* sl = bm.getAddr32(0, y);
276
277 for (int x = kLeftX; x < kMidX; ++x) {
278 if (SK_ColorGREEN != sl[x]) {
279 isCorrect = false;
280 break;
281 }
282 }
283
284 for (int x = kMidX; x < kRightX; ++x) {
285 if (SK_ColorGRAY != sl[x]) {
286 isCorrect = false;
287 break;
288 }
289 }
290 }
291
292 REPORTER_ASSERT(reporter, isCorrect);
293}
294// From crbug.com/768134
295DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500296 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400297 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500298
299 // Use draws for clears
300 GrContextOptions options(ctxInfo.options());
301 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
302 sk_gpu_test::GrContextFactory workaroundFactory(options);
303 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400304}