blob: 703792d1a21866a537cf84b5d7e34a1ebcbe5f0b [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
Ben Wagnerb607a8f2018-03-12 13:46:21 -04008#include "SkTypes.h"
Ben Wagner1a462bd2018-03-12 13:46:21 -04009
Ben Wagnerb607a8f2018-03-12 13:46:21 -040010#include "GrColor.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000011#include "GrContext.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040012#include "GrContextFactory.h"
13#include "GrContextOptions.h"
14#include "GrContextPriv.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000015#include "GrRenderTargetContext.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040016#include "GrTypes.h"
17#include "SkBitmap.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000018#include "SkCanvas.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040019#include "SkColor.h"
20#include "SkColorSpace.h"
21#include "SkImageInfo.h"
22#include "SkPaint.h"
23#include "SkRect.h"
24#include "SkRefCnt.h"
Ben Wagner501c17c2018-03-12 20:04:31 +000025#include "SkSurface.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040026#include "Test.h"
27
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) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050059 return context->contextPriv().makeDeferredRenderTargetContext(
60 SkBackingFit::kExact, w, h,
61 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080062}
63
Brian Salomon43f8bf02017-10-18 08:33:29 -040064static void clear_op_test(skiatest::Reporter* reporter, GrContext* context) {
bsalomone63ffef2016-02-05 07:17:34 -080065 static const int kW = 10;
66 static const int kH = 10;
67
68 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040069 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080070
71 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
72 // it.
73 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
74 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
75 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
76 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
77 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
78
79 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
80 // it.
81 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
82 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
83 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
84 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
85 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
86
87 uint32_t actualValue;
88 int failX, failY;
89
90 static const GrColor kColor1 = 0xABCDEF01;
91 static const GrColor kColor2 = ~kColor1;
92
Robert Phillipse4d45bf2017-06-02 14:53:40 -040093 rtContext = newRTC(context, kW, kH);
94 SkASSERT(rtContext);
95
bsalomone63ffef2016-02-05 07:17:34 -080096 // Check a full clear
Chris Dalton344e9032017-12-11 15:42:09 -070097 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -040098 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080099 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
100 failX, failY);
101 }
102
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400103 rtContext = newRTC(context, kW, kH);
104 SkASSERT(rtContext);
105
bsalomone63ffef2016-02-05 07:17:34 -0800106 // Check two full clears, same color
Chris Dalton344e9032017-12-11 15:42:09 -0700107 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
108 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400109 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800110 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
111 failX, failY);
112 }
113
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400114 rtContext = newRTC(context, kW, kH);
115 SkASSERT(rtContext);
116
bsalomone63ffef2016-02-05 07:17:34 -0800117 // Check two full clears, different colors
Chris Dalton344e9032017-12-11 15:42:09 -0700118 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
119 rtContext->clear(&fullRect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400120 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800121 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
122 failX, failY);
123 }
124
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400125 rtContext = newRTC(context, kW, kH);
126 SkASSERT(rtContext);
127
bsalomone63ffef2016-02-05 07:17:34 -0800128 // Test a full clear followed by a same color inset clear
Chris Dalton344e9032017-12-11 15:42:09 -0700129 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
130 rtContext->clear(&mid1Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400131 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800132 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
133 failX, failY);
134 }
135
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400136 rtContext = newRTC(context, kW, kH);
137 SkASSERT(rtContext);
138
bsalomone63ffef2016-02-05 07:17:34 -0800139 // Test a inset clear followed by same color full clear
Chris Dalton344e9032017-12-11 15:42:09 -0700140 rtContext->clear(&mid1Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
141 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400142 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800143 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
144 failX, failY);
145 }
146
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400147 rtContext = newRTC(context, kW, kH);
148 SkASSERT(rtContext);
149
bsalomone63ffef2016-02-05 07:17:34 -0800150 // Test a full clear followed by a different color inset clear
Chris Dalton344e9032017-12-11 15:42:09 -0700151 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
152 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400153 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800154 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
155 failX, failY);
156 }
Brian Osman11052242016-10-27 14:47:55 -0400157 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
158 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
159 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
160 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800161 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
162 failX, failY);
163 }
164
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400165 rtContext = newRTC(context, kW, kH);
166 SkASSERT(rtContext);
167
bsalomone63ffef2016-02-05 07:17:34 -0800168 // Test a inset clear followed by a different full clear
Chris Dalton344e9032017-12-11 15:42:09 -0700169 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
170 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400171 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800172 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
173 failX, failY);
174 }
175
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400176 rtContext = newRTC(context, kW, kH);
177 SkASSERT(rtContext);
178
bsalomone63ffef2016-02-05 07:17:34 -0800179 // Check three nested clears from largest to smallest where outermost and innermost are same
180 // color.
Chris Dalton344e9032017-12-11 15:42:09 -0700181 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
182 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
183 rtContext->clear(&mid2Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400184 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800185 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
186 failX, failY);
187 }
Brian Osman11052242016-10-27 14:47:55 -0400188 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
189 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
190 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
191 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800192 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
193 failX, failY);
194 }
Brian Osman11052242016-10-27 14:47:55 -0400195 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
196 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
197 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
198 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800199 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
200 failX, failY);
201 }
202
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400203 rtContext = newRTC(context, kW, kH);
204 SkASSERT(rtContext);
205
bsalomone63ffef2016-02-05 07:17:34 -0800206 // Swap the order of the second two clears in the above test.
Chris Dalton344e9032017-12-11 15:42:09 -0700207 rtContext->clear(&fullRect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
208 rtContext->clear(&mid2Rect, kColor1, GrRenderTargetContext::CanClearFullscreen::kNo);
209 rtContext->clear(&mid1Rect, kColor2, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400210 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800211 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
212 failX, failY);
213 }
Brian Osman11052242016-10-27 14:47:55 -0400214 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
215 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
216 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
217 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800218 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
219 failX, failY);
220 }
221}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400222
223DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
224 clear_op_test(reporter, ctxInfo.grContext());
225 if (ctxInfo.backend() == kOpenGL_GrBackend) {
226 GrContextOptions options(ctxInfo.options());
227 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
228 sk_gpu_test::GrContextFactory workaroundFactory(options);
229 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
230 }
231}
232
Brian Salomon43f8bf02017-10-18 08:33:29 -0400233void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
234 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
235
236 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
237 SkCanvas* canvas = surf->getCanvas();
238
239 SkPaint paints[2];
240 paints[0].setColor(SK_ColorGREEN);
241 paints[1].setColor(SK_ColorGRAY);
242
243 static const int kLeftX = 158;
244 static const int kMidX = 258;
245 static const int kRightX = 383;
246 static const int kTopY = 26;
247 static const int kBotY = 51;
248
249 const SkRect rects[2] = {
250 { kLeftX, kTopY, kMidX, kBotY },
251 { kMidX, kTopY, kRightX, kBotY },
252 };
253
254 for (int i = 0; i < 2; ++i) {
255 // the bounds parameter is required to cause a full screen clear
256 canvas->saveLayer(&rects[i], nullptr);
257 canvas->drawRect(rects[i], paints[i]);
258 canvas->restore();
259 }
260
261 SkBitmap bm;
262 bm.allocPixels(ii, 0);
263
264 SkAssertResult(surf->readPixels(bm, 0, 0));
265
266 bool isCorrect = true;
267 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
268 const uint32_t* sl = bm.getAddr32(0, y);
269
270 for (int x = kLeftX; x < kMidX; ++x) {
271 if (SK_ColorGREEN != sl[x]) {
272 isCorrect = false;
273 break;
274 }
275 }
276
277 for (int x = kMidX; x < kRightX; ++x) {
278 if (SK_ColorGRAY != sl[x]) {
279 isCorrect = false;
280 break;
281 }
282 }
283 }
284
285 REPORTER_ASSERT(reporter, isCorrect);
286}
287// From crbug.com/768134
288DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
289 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
290 if (ctxInfo.backend() == kOpenGL_GrBackend) {
291 GrContextOptions options(ctxInfo.options());
292 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
293 sk_gpu_test::GrContextFactory workaroundFactory(options);
294 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
295 }
296}