blob: d9271fc0c9ea6f66f11acb1faa181873dfa83d45 [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;
Brian Osman9a9baae2018-11-05 15:06:26 -050092 static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
93 static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
bsalomone63ffef2016-02-05 07:17:34 -080094
Robert Phillipse4d45bf2017-06-02 14:53:40 -040095 rtContext = newRTC(context, kW, kH);
96 SkASSERT(rtContext);
97
bsalomone63ffef2016-02-05 07:17:34 -080098 // Check a full clear
Brian Osman9a9baae2018-11-05 15:06:26 -050099 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400100 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800101 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
102 failX, failY);
103 }
104
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400105 rtContext = newRTC(context, kW, kH);
106 SkASSERT(rtContext);
107
bsalomone63ffef2016-02-05 07:17:34 -0800108 // Check two full clears, same color
Brian Osman9a9baae2018-11-05 15:06:26 -0500109 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
110 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400111 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800112 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
113 failX, failY);
114 }
115
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400116 rtContext = newRTC(context, kW, kH);
117 SkASSERT(rtContext);
118
bsalomone63ffef2016-02-05 07:17:34 -0800119 // Check two full clears, different colors
Brian Osman9a9baae2018-11-05 15:06:26 -0500120 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
121 rtContext->clear(&fullRect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400122 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800123 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
124 failX, failY);
125 }
126
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400127 rtContext = newRTC(context, kW, kH);
128 SkASSERT(rtContext);
129
bsalomone63ffef2016-02-05 07:17:34 -0800130 // Test a full clear followed by a same color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500131 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
132 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400133 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800134 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
135 failX, failY);
136 }
137
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400138 rtContext = newRTC(context, kW, kH);
139 SkASSERT(rtContext);
140
bsalomone63ffef2016-02-05 07:17:34 -0800141 // Test a inset clear followed by same color full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500142 rtContext->clear(&mid1Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
143 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400144 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800145 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
146 failX, failY);
147 }
148
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400149 rtContext = newRTC(context, kW, kH);
150 SkASSERT(rtContext);
151
bsalomone63ffef2016-02-05 07:17:34 -0800152 // Test a full clear followed by a different color inset clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500153 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
154 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400155 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800156 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
157 failX, failY);
158 }
Brian Osman11052242016-10-27 14:47:55 -0400159 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
160 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
161 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
162 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800163 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
164 failX, failY);
165 }
166
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400167 rtContext = newRTC(context, kW, kH);
168 SkASSERT(rtContext);
169
bsalomone63ffef2016-02-05 07:17:34 -0800170 // Test a inset clear followed by a different full clear
Brian Osman9a9baae2018-11-05 15:06:26 -0500171 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
172 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400173 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800174 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
175 failX, failY);
176 }
177
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400178 rtContext = newRTC(context, kW, kH);
179 SkASSERT(rtContext);
180
bsalomone63ffef2016-02-05 07:17:34 -0800181 // Check three nested clears from largest to smallest where outermost and innermost are same
182 // color.
Brian Osman9a9baae2018-11-05 15:06:26 -0500183 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
184 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
185 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400186 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800187 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
188 failX, failY);
189 }
Brian Osman11052242016-10-27 14:47:55 -0400190 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
191 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
192 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
193 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800194 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
195 failX, failY);
196 }
Brian Osman11052242016-10-27 14:47:55 -0400197 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
198 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
199 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
200 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800201 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
202 failX, failY);
203 }
204
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400205 rtContext = newRTC(context, kW, kH);
206 SkASSERT(rtContext);
207
bsalomone63ffef2016-02-05 07:17:34 -0800208 // Swap the order of the second two clears in the above test.
Brian Osman9a9baae2018-11-05 15:06:26 -0500209 rtContext->clear(&fullRect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
210 rtContext->clear(&mid2Rect, kColor1f, GrRenderTargetContext::CanClearFullscreen::kNo);
211 rtContext->clear(&mid1Rect, kColor2f, GrRenderTargetContext::CanClearFullscreen::kNo);
Brian Osman11052242016-10-27 14:47:55 -0400212 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800213 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
214 failX, failY);
215 }
Brian Osman11052242016-10-27 14:47:55 -0400216 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
217 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
218 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
219 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800220 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
221 failX, failY);
222 }
223}
Brian Salomon43f8bf02017-10-18 08:33:29 -0400224
225DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
226 clear_op_test(reporter, ctxInfo.grContext());
Greg Danielbdf12ad2018-10-12 09:31:11 -0400227 if (ctxInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400228 GrContextOptions options(ctxInfo.options());
229 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
230 sk_gpu_test::GrContextFactory workaroundFactory(options);
231 clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
232 }
233}
234
Brian Salomon43f8bf02017-10-18 08:33:29 -0400235void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrContext* context) {
236 const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
237
238 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
239 SkCanvas* canvas = surf->getCanvas();
240
241 SkPaint paints[2];
242 paints[0].setColor(SK_ColorGREEN);
243 paints[1].setColor(SK_ColorGRAY);
244
245 static const int kLeftX = 158;
246 static const int kMidX = 258;
247 static const int kRightX = 383;
248 static const int kTopY = 26;
249 static const int kBotY = 51;
250
251 const SkRect rects[2] = {
252 { kLeftX, kTopY, kMidX, kBotY },
253 { kMidX, kTopY, kRightX, kBotY },
254 };
255
256 for (int i = 0; i < 2; ++i) {
257 // the bounds parameter is required to cause a full screen clear
258 canvas->saveLayer(&rects[i], nullptr);
259 canvas->drawRect(rects[i], paints[i]);
260 canvas->restore();
261 }
262
263 SkBitmap bm;
264 bm.allocPixels(ii, 0);
265
266 SkAssertResult(surf->readPixels(bm, 0, 0));
267
268 bool isCorrect = true;
269 for (int y = kTopY; isCorrect && y < kBotY; ++y) {
270 const uint32_t* sl = bm.getAddr32(0, y);
271
272 for (int x = kLeftX; x < kMidX; ++x) {
273 if (SK_ColorGREEN != sl[x]) {
274 isCorrect = false;
275 break;
276 }
277 }
278
279 for (int x = kMidX; x < kRightX; ++x) {
280 if (SK_ColorGRAY != sl[x]) {
281 isCorrect = false;
282 break;
283 }
284 }
285 }
286
287 REPORTER_ASSERT(reporter, isCorrect);
288}
289// From crbug.com/768134
290DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
291 fullscreen_clear_with_layer_test(reporter, ctxInfo.grContext());
Greg Danielbdf12ad2018-10-12 09:31:11 -0400292 if (ctxInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400293 GrContextOptions options(ctxInfo.options());
294 options.fUseDrawInsteadOfGLClear = GrContextOptions::Enable::kYes;
295 sk_gpu_test::GrContextFactory workaroundFactory(options);
296 fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
297 }
298}