• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #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"
17 #include "include/core/SkTypes.h"
18 #include "include/gpu/GrContextOptions.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "include/private/GrTypesPriv.h"
21 #include "include/private/SkColorData.h"
22 #include "src/core/SkAutoPixmapStorage.h"
23 #include "src/gpu/GrColor.h"
24 #include "src/gpu/GrDirectContextPriv.h"
25 #include "src/gpu/GrImageInfo.h"
26 #include "src/gpu/ops/ClearOp.h"
27 #include "src/gpu/v1/SurfaceDrawContext_v1.h"
28 #include "tests/Test.h"
29 #include "tools/gpu/GrContextFactory.h"
30 
31 #include <cstdint>
32 #include <memory>
33 
34 using SurfaceDrawContext = skgpu::v1::SurfaceDrawContext;
35 using ClearOp = skgpu::v1::ClearOp;
36 
check_rect(GrDirectContext * dContext,SurfaceDrawContext * sdc,const SkIRect & rect,uint32_t expectedValue,uint32_t * actualValue,int * failX,int * failY)37 static bool check_rect(GrDirectContext* dContext,
38                        SurfaceDrawContext* sdc,
39                        const SkIRect& rect,
40                        uint32_t expectedValue,
41                        uint32_t* actualValue,
42                        int* failX,
43                        int* failY) {
44     int w = rect.width();
45     int h = rect.height();
46 
47     SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
48 
49     SkAutoPixmapStorage readback;
50     readback.alloc(dstInfo);
51 
52     readback.erase(~expectedValue);
53     if (!sdc->readPixels(dContext, readback, {rect.fLeft, rect.fTop})) {
54         return false;
55     }
56 
57     for (int y = 0; y < h; ++y) {
58         for (int x = 0; x < w; ++x) {
59             uint32_t pixel = readback.addr32()[y * w + x];
60             if (pixel != expectedValue) {
61                 *actualValue = pixel;
62                 *failX = x + rect.fLeft;
63                 *failY = y + rect.fTop;
64                 return false;
65             }
66         }
67     }
68     return true;
69 }
70 
newSDC(GrRecordingContext * rContext,int w,int h)71 std::unique_ptr<SurfaceDrawContext> newSDC(GrRecordingContext* rContext, int w, int h) {
72     return SurfaceDrawContext::Make(rContext, GrColorType::kRGBA_8888, nullptr,
73                                     SkBackingFit::kExact, {w, h}, SkSurfaceProps());
74 }
75 
clear_op_test(skiatest::Reporter * reporter,GrDirectContext * dContext)76 static void clear_op_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
77     static const int kW = 10;
78     static const int kH = 10;
79 
80     SkIRect fullRect = SkIRect::MakeWH(kW, kH);
81     std::unique_ptr<SurfaceDrawContext> sdc;
82 
83     // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
84     // it.
85     SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
86     SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
87     SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
88     SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
89     SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
90 
91     // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
92     // it.
93     SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
94     SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
95     SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
96     SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
97     SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
98 
99     uint32_t actualValue;
100     int failX, failY;
101 
102     static const GrColor kColor1 = 0xABCDEF01;
103     static const GrColor kColor2 = ~kColor1;
104     static const SkPMColor4f kColor1f = SkPMColor4f::FromBytes_RGBA(kColor1);
105     static const SkPMColor4f kColor2f = SkPMColor4f::FromBytes_RGBA(kColor2);
106 
107     sdc = newSDC(dContext, kW, kH);
108     SkASSERT(sdc);
109 
110     // Check a full clear
111     sdc->clear(fullRect, kColor1f);
112     if (!check_rect(dContext, sdc.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
113         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
114                failX, failY);
115     }
116 
117     sdc = newSDC(dContext, kW, kH);
118     SkASSERT(sdc);
119 
120     // Check two full clears, same color
121     sdc->clear(fullRect, kColor1f);
122     sdc->clear(fullRect, kColor1f);
123     if (!check_rect(dContext, sdc.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
124         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
125                failX, failY);
126     }
127 
128     sdc = newSDC(dContext, kW, kH);
129     SkASSERT(sdc);
130 
131     // Check two full clears, different colors
132     sdc->clear(fullRect, kColor1f);
133     sdc->clear(fullRect, kColor2f);
134     if (!check_rect(dContext, sdc.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
135         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
136                failX, failY);
137     }
138 
139     sdc = newSDC(dContext, kW, kH);
140     SkASSERT(sdc);
141 
142     // Test a full clear followed by a same color inset clear
143     sdc->clear(fullRect, kColor1f);
144     sdc->clear(mid1Rect, kColor1f);
145     if (!check_rect(dContext, sdc.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
146         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
147                failX, failY);
148     }
149 
150     sdc = newSDC(dContext, kW, kH);
151     SkASSERT(sdc);
152 
153     // Test a inset clear followed by same color full clear
154     sdc->clear(mid1Rect, kColor1f);
155     sdc->clear(fullRect, kColor1f);
156     if (!check_rect(dContext, sdc.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
157         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
158                failX, failY);
159     }
160 
161     sdc = newSDC(dContext, kW, kH);
162     SkASSERT(sdc);
163 
164     // Test a full clear followed by a different color inset clear
165     sdc->clear(fullRect, kColor1f);
166     sdc->clear(mid1Rect, kColor2f);
167     if (!check_rect(dContext, sdc.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
168         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
169                failX, failY);
170     }
171     if (!check_rect(dContext, sdc.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
172         !check_rect(dContext, sdc.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
173         !check_rect(dContext, sdc.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
174         !check_rect(dContext, sdc.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
175         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
176                failX, failY);
177     }
178 
179     sdc = newSDC(dContext, kW, kH);
180     SkASSERT(sdc);
181 
182     // Test a inset clear followed by a different full clear
183     sdc->clear(mid1Rect, kColor2f);
184     sdc->clear(fullRect, kColor1f);
185     if (!check_rect(dContext, sdc.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
186         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
187                failX, failY);
188     }
189 
190     sdc = newSDC(dContext, kW, kH);
191     SkASSERT(sdc);
192 
193     // Check three nested clears from largest to smallest where outermost and innermost are same
194     // color.
195     sdc->clear(fullRect, kColor1f);
196     sdc->clear(mid1Rect, kColor2f);
197     sdc->clear(mid2Rect, kColor1f);
198     if (!check_rect(dContext, sdc.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
199         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
200                failX, failY);
201     }
202     if (!check_rect(dContext, sdc.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
203         !check_rect(dContext, sdc.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
204         !check_rect(dContext, sdc.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
205         !check_rect(dContext, sdc.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
206         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
207                failX, failY);
208     }
209     if (!check_rect(dContext, sdc.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
210         !check_rect(dContext, sdc.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
211         !check_rect(dContext, sdc.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
212         !check_rect(dContext, sdc.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
213         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
214                failX, failY);
215     }
216 
217     sdc = newSDC(dContext, kW, kH);
218     SkASSERT(sdc);
219 
220     // Swap the order of the second two clears in the above test.
221     sdc->clear(fullRect, kColor1f);
222     sdc->clear(mid2Rect, kColor1f);
223     sdc->clear(mid1Rect, kColor2f);
224     if (!check_rect(dContext, sdc.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
225         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
226                failX, failY);
227     }
228     if (!check_rect(dContext, sdc.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
229         !check_rect(dContext, sdc.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
230         !check_rect(dContext, sdc.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
231         !check_rect(dContext, sdc.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
232         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
233                failX, failY);
234     }
235 
236     // Clear calls need to remain ClearOps for the following combining-tests to work as expected
237     if (!dContext->priv().caps()->performColorClearsAsDraws() &&
238         !dContext->priv().caps()->performStencilClearsAsDraws() &&
239         !dContext->priv().caps()->performPartialClearsAsDraws()) {
240         static constexpr SkIRect kScissorRect = SkIRect::MakeXYWH(1, 1, kW-1, kH-1);
241 
242         // Try combining a pure-color clear w/ a combined stencil & color clear
243         // (re skbug.com/10963)
244         {
245             sdc = newSDC(dContext, kW, kH);
246             SkASSERT(sdc);
247 
248             sdc->clearStencilClip(kScissorRect, true);
249             // This color clear can combine w/ the preceding stencil clear
250             sdc->clear(kScissorRect, SK_PMColor4fWHITE);
251 
252             // This should combine w/ the prior combined clear and overwrite the color
253             sdc->clear(kScissorRect, SK_PMColor4fBLACK);
254 
255             auto opsTask = sdc->getOpsTask();
256             REPORTER_ASSERT(reporter, opsTask->numOpChains() == 1);
257 
258             const ClearOp& clearOp = opsTask->getChain(0)->cast<ClearOp>();
259 
260             constexpr std::array<float, 4> kExpected { 0, 0, 0, 1 };
261             REPORTER_ASSERT(reporter, clearOp.color() == kExpected);
262             REPORTER_ASSERT(reporter, clearOp.stencilInsideMask());
263 
264             dContext->flushAndSubmit();
265         }
266 
267         // Try combining a pure-stencil clear w/ a combined stencil & color clear
268         // (re skbug.com/10963)
269         {
270             sdc = newSDC(dContext, kW, kH);
271             SkASSERT(sdc);
272 
273             sdc->clearStencilClip(kScissorRect, true);
274             // This color clear can combine w/ the preceding stencil clear
275             sdc->clear(kScissorRect, SK_PMColor4fWHITE);
276 
277             // This should combine w/ the prior combined clear and overwrite the 'insideStencilMask'
278             // field
279             sdc->clearStencilClip(kScissorRect, false);
280 
281             auto opsTask = sdc->getOpsTask();
282             REPORTER_ASSERT(reporter, opsTask->numOpChains() == 1);
283 
284             const ClearOp& clearOp = opsTask->getChain(0)->cast<ClearOp>();
285 
286             constexpr std::array<float, 4> kExpected { 1, 1, 1, 1 };
287             REPORTER_ASSERT(reporter, clearOp.color() == kExpected);
288             REPORTER_ASSERT(reporter, !clearOp.stencilInsideMask());
289 
290             dContext->flushAndSubmit();
291         }
292     }
293 }
294 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp,reporter,ctxInfo)295 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
296     // Regular clear
297     clear_op_test(reporter, ctxInfo.directContext());
298 
299     // Force drawing for clears
300     GrContextOptions options(ctxInfo.options());
301     options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
302     sk_gpu_test::GrContextFactory workaroundFactory(options);
303     clear_op_test(reporter, workaroundFactory.get(ctxInfo.type()));
304 }
305 
fullscreen_clear_with_layer_test(skiatest::Reporter * reporter,GrRecordingContext * rContext)306 void fullscreen_clear_with_layer_test(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
307     const SkImageInfo ii = SkImageInfo::Make(400, 77, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
308 
309     sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
310     SkCanvas* canvas = surf->getCanvas();
311 
312     SkPaint paints[2];
313     paints[0].setColor(SK_ColorGREEN);
314     paints[1].setColor(SK_ColorGRAY);
315 
316     static const int kLeftX = 158;
317     static const int kMidX = 258;
318     static const int kRightX = 383;
319     static const int kTopY = 26;
320     static const int kBotY = 51;
321 
322     const SkRect rects[2] = {
323         { kLeftX, kTopY, kMidX, kBotY },
324         { kMidX, kTopY, kRightX, kBotY },
325     };
326 
327     for (int i = 0; i < 2; ++i) {
328         // the bounds parameter is required to cause a full screen clear
329         canvas->saveLayer(&rects[i], nullptr);
330             canvas->drawRect(rects[i], paints[i]);
331         canvas->restore();
332     }
333 
334     SkBitmap bm;
335     bm.allocPixels(ii, 0);
336 
337     SkAssertResult(surf->readPixels(bm, 0, 0));
338 
339     bool isCorrect = true;
340     for (int y = kTopY; isCorrect && y < kBotY; ++y) {
341         const uint32_t* sl = bm.getAddr32(0, y);
342 
343         for (int x = kLeftX; x < kMidX; ++x) {
344             if (SK_ColorGREEN != sl[x]) {
345                 isCorrect = false;
346                 break;
347             }
348         }
349 
350         for (int x = kMidX; x < kRightX; ++x) {
351             if (SK_ColorGRAY != sl[x]) {
352                 isCorrect = false;
353                 break;
354             }
355         }
356     }
357 
358     REPORTER_ASSERT(reporter, isCorrect);
359 }
360 // From crbug.com/768134
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers,reporter,ctxInfo)361 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FullScreenClearWithLayers, reporter, ctxInfo) {
362     // Regular clear
363     fullscreen_clear_with_layer_test(reporter, ctxInfo.directContext());
364 
365     // Use draws for clears
366     GrContextOptions options(ctxInfo.options());
367     options.fUseDrawInsteadOfClear = GrContextOptions::Enable::kYes;
368     sk_gpu_test::GrContextFactory workaroundFactory(options);
369     fullscreen_clear_with_layer_test(reporter, workaroundFactory.get(ctxInfo.type()));
370 }
371