blob: 868e25949e623b3b7d29dfa3cc3331ccc94145c4 [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"
18#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/GrContext.h"
20#include "include/gpu/GrContextOptions.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040021#include "include/private/GrTypesPriv.h"
22#include "include/private/SkColorData.h"
23#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrContextPriv.h"
26#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 <cstring>
32#include <memory>
Robert Phillipsdbfecd02017-10-18 15:44:08 -040033
Brian Osman11052242016-10-27 14:47:55 -040034static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080035 uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080036 int w = rect.width();
37 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040038 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080039 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040040
41 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
42
Brian Salomon1d435302019-07-01 13:05:28 -040043 if (!rtc->readPixels(dstInfo, pixels.get(), 0, {rect.fLeft, rect.fTop})) {
Robert Phillips301431d2017-03-29 12:08:49 -040044 return false;
45 }
46
bsalomone63ffef2016-02-05 07:17:34 -080047 for (int y = 0; y < h; ++y) {
48 for (int x = 0; x < w; ++x) {
49 uint32_t pixel = pixels.get()[y * w + x];
50 if (pixel != expectedValue) {
51 *actualValue = pixel;
52 *failX = x + rect.fLeft;
53 *failY = y + rect.fTop;
54 return false;
55 }
56 }
57 }
58 return true;
59}
60
Robert Phillipse4d45bf2017-06-02 14:53:40 -040061sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
Brian Salomon27ae52c2019-07-03 11:27:44 -040062 return context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h,
Brian Salomond6287472019-06-24 15:50:07 -040063 GrColorType::kRGBA_8888, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080064}
65
Brian Salomon43f8bf02017-10-18 08:33:29 -040066static void clear_op_test(skiatest::Reporter* reporter, GrContext* context) {
bsalomone63ffef2016-02-05 07:17:34 -080067 static const int kW = 10;
68 static const int kH = 10;
69
70 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040071 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080072
73 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
74 // it.
75 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
76 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
77 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
78 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
79 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
80
81 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
82 // it.
83 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
84 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
85 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
86 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
87 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
88
89 uint32_t actualValue;
90 int failX, failY;
91
92 static const GrColor kColor1 = 0xABCDEF01;
93 static const GrColor kColor2 = ~kColor1;
Brian Osman9a9baae2018-11-05 15:06:26 -050094 static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
95 static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
bsalomone63ffef2016-02-05 07:17:34 -080096
Robert Phillipse4d45bf2017-06-02 14:53:40 -040097 rtContext = newRTC(context, kW, kH);
98 SkASSERT(rtContext);
99
bsalomone63ffef2016-02-05 07:17:34 -0800100 // Check a full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500101 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400102 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800103 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
104 failX, failY);
105 }
106
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400107 rtContext = newRTC(context, kW, kH);
108 SkASSERT(rtContext);
109
bsalomone63ffef2016-02-05 07:17:34 -0800110 // Check two full clears, same color
Brian Osman9a9baae2018-11-05 15:06:26 -0500111 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
112 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400113 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800114 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
115 failX, failY);
116 }
117
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400118 rtContext = newRTC(context, kW, kH);
119 SkASSERT(rtContext);
120
bsalomone63ffef2016-02-05 07:17:34 -0800121 // Check two full clears, different colors
Brian Osman9a9baae2018-11-05 15:06:26 -0500122 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
123 rtContext->clear(&fullRect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400124 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800125 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
126 failX, failY);
127 }
128
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400129 rtContext = newRTC(context, kW, kH);
130 SkASSERT(rtContext);
131
bsalomone63ffef2016-02-05 07:17:34 -0800132 // Test a full clear followed by a same color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500133 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
134 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400135 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800136 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
137 failX, failY);
138 }
139
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400140 rtContext = newRTC(context, kW, kH);
141 SkASSERT(rtContext);
142
bsalomone63ffef2016-02-05 07:17:34 -0800143 // Test a inset clear followed by same color full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500144 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
145 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400146 if (!check_rect(rtContext.get(), fullRect, 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 full clear followed by a different color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500155 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
156 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400157 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800158 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
159 failX, failY);
160 }
Brian Osman11052242016-10-27 14:47:55 -0400161 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
162 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
163 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
164 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800165 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
166 failX, failY);
167 }
168
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400169 rtContext = newRTC(context, kW, kH);
170 SkASSERT(rtContext);
171
bsalomone63ffef2016-02-05 07:17:34 -0800172 // Test a inset clear followed by a different full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500173 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
174 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400175 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800176 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
177 failX, failY);
178 }
179
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400180 rtContext = newRTC(context, kW, kH);
181 SkASSERT(rtContext);
182
bsalomone63ffef2016-02-05 07:17:34 -0800183 // Check three nested clears from largest to smallest where outermost and innermost are same
184 // color.
Brian Osman9a9baae2018-11-05 15:06:26 -0500185 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
186 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
187 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400188 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800189 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
190 failX, failY);
191 }
Brian Osman11052242016-10-27 14:47:55 -0400192 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
193 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
194 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
195 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800196 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
197 failX, failY);
198 }
Brian Osman11052242016-10-27 14:47:55 -0400199 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
200 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
201 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
202 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800203 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
204 failX, failY);
205 }
206
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400207 rtContext = newRTC(context, kW, kH);
208 SkASSERT(rtContext);
209
bsalomone63ffef2016-02-05 07:17:34 -0800210 // Swap the order of the second two clears in the above test.
Brian Osman9a9baae2018-11-05 15:06:26 -0500211 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
212 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
213 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400214 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800215 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
216 failX, failY);
217 }
Brian Osman11052242016-10-27 14:47:55 -0400218 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
219 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
220 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
221 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800222 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
223 failX, failY);
224 }
225}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400226
227DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500228 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400229 clear_op_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500230
231 // Force drawing for clears
232 GrContextOptions options(ctxInfo.options());
233 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
234 sk_gpu_test::GrContextFactory workaroundFactory(options);
235 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400236}
237
Brian Salomon43f8bf02017-10-18 08:33:29 -0400238void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
239 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
240
241 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
242 SkCanvas* canvas = surf->getCanvas();
243
244 SkPaint paints[2];
245 paints[0].setColor(SK_ColorGREEN);
246 paints[1].setColor(SK_ColorGRAY);
247
248 static const int kLeftX = 158;
249 static const int kMidX = 258;
250 static const int kRightX = 383;
251 static const int kTopY = 26;
252 static const int kBotY = 51;
253
254 const SkRect rects[2] = {
255 { kLeftX, kTopY, kMidX, kBotY },
256 { kMidX, kTopY, kRightX, kBotY },
257 };
258
259 for (int i = 0; i < 2; ++i) {
260 // the bounds parameter is required to cause a full screen clear
261 canvas->saveLayer(&rects[i], nullptr);
262 canvas->drawRect(rects[i], paints[i]);
263 canvas->restore();
264 }
265
266 SkBitmap bm;
267 bm.allocPixels(ii, 0);
268
269 SkAssertResult(surf->readPixels(bm, 0, 0));
270
271 bool isCorrect = true;
272 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
273 const uint32_t* sl = bm.getAddr32(0, y);
274
275 for (int x = kLeftX; x < kMidX; ++x) {
276 if (SK_ColorGREEN != sl[x]) {
277 isCorrect = false;
278 break;
279 }
280 }
281
282 for (int x = kMidX; x < kRightX; ++x) {
283 if (SK_ColorGRAY != sl[x]) {
284 isCorrect = false;
285 break;
286 }
287 }
288 }
289
290 REPORTER_ASSERT(reporter, isCorrect);
291}
292// From crbug.com/768134
293DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
Michael Ludwiga21d1962019-01-11 15:26:22 -0500294 // Regular clear
Brian Salomon43f8bf02017-10-18 08:33:29 -0400295 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
Michael Ludwiga21d1962019-01-11 15:26:22 -0500296
297 // Use draws for clears
298 GrContextOptions options(ctxInfo.options());
299 options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
300 sk_gpu_test::GrContextFactory workaroundFactory(options);
301 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400302}