• 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRRect.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkRegion.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypes.h"
23 #include "include/gpu/GrBackendSurface.h"
24 #include "include/gpu/GrContext.h"
25 #include "include/private/GrTypesPriv.h"
26 #include "include/private/SkColorData.h"
27 #include "src/core/SkClipOpPriv.h"
28 #include "src/core/SkClipStack.h"
29 #include "src/gpu/GrAppliedClip.h"
30 #include "src/gpu/GrCaps.h"
31 #include "src/gpu/GrClip.h"
32 #include "src/gpu/GrContextPriv.h"
33 #include "src/gpu/GrFixedClip.h"
34 #include "src/gpu/GrFragmentProcessor.h"
35 #include "src/gpu/GrPaint.h"
36 #include "src/gpu/GrReducedClip.h"
37 #include "src/gpu/GrRenderTargetContext.h"
38 #include "src/gpu/GrRenderTargetContextPriv.h"
39 #include "src/gpu/GrStencilClip.h"
40 #include "src/gpu/GrTextureProxy.h"
41 #include "src/gpu/GrUserStencilSettings.h"
42 #include "src/gpu/effects/GrTextureDomain.h"
43 #include "tools/ToolUtils.h"
44 
45 #include <utility>
46 
47 class GrRecordingContext;
48 
49 constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
50 constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
51 
52 namespace skiagm {
53 
54 ////////////////////////////////////////////////////////////////////////////////////////////////////
55 
56 class WindowRectanglesBaseGM : public GM {
57 protected:
58     virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
59 
60 private:
onISize()61     SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
62     DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
63 };
64 
onDraw(SkCanvas * canvas,SkString * errorMsg)65 DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
66     ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
67 
68     SkClipStack stack;
69     stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
70                    kDifference_SkClipOp, false);
71     stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
72                    kDifference_SkClipOp, true);
73     stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
74                     SkMatrix::I(), kDifference_SkClipOp, true);
75 
76     SkRRect nine;
77     nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
78     stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
79 
80     SkRRect complx;
81     SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
82     complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
83     stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
84 
85     return this->onCoverClipStack(stack, canvas, errorMsg);
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////////////////////////
89 
90 /**
91  * Draws a clip that will exercise window rectangles if they are supported.
92  */
93 class WindowRectanglesGM : public WindowRectanglesBaseGM {
94 private:
onShortName()95     SkString onShortName() final { return SkString("windowrectangles"); }
96     DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
97 };
98 
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas,SkString * errorMsg)99 DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
100                                                 SkString* errorMsg) {
101     SkPaint paint;
102     paint.setColor(0xff00aa80);
103 
104     // Set up the canvas's clip to match our SkClipStack.
105     SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
106     for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
107         SkClipOp op = element->getOp();
108         bool isAA = element->isAA();
109         switch (element->getDeviceSpaceType()) {
110             case SkClipStack::Element::DeviceSpaceType::kPath:
111                 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
112                 break;
113             case SkClipStack::Element::DeviceSpaceType::kRRect:
114                 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
115                 break;
116             case SkClipStack::Element::DeviceSpaceType::kRect:
117                 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
118                 break;
119             case SkClipStack::Element::DeviceSpaceType::kEmpty:
120                 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
121                 break;
122         }
123     }
124 
125     canvas->drawRect(SkRect::Make(kCoverRect), paint);
126     return DrawResult::kOk;
127 }
128 
129 DEF_GM( return new WindowRectanglesGM(); )
130 
131 ////////////////////////////////////////////////////////////////////////////////////////////////////
132 
133 constexpr static int kNumWindows = 8;
134 
135 /**
136  * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
137  * this test is to verify that window rectangles are being used during clip mask generation, and to
138  * visualize where the window rectangles are placed.
139  *
140  * We use window rectangles when generating the clip mask because there is no need to invest time
141  * defining those regions where window rectangles will be in effect during the actual draw anyway.
142  *
143  * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
144  * it, and then covering the mask with a solid color once it has been generated. The regions inside
145  * window rectangles or outside the scissor should still have the initial checkerboard intact.
146  */
147 class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
148 private:
149     constexpr static int kMaskCheckerSize = 5;
onShortName()150     SkString onShortName() final { return SkString("windowrectangles_mask"); }
151     DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
152     void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
153     void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
154     void stencilCheckerboard(GrRenderTargetContext*, bool flip);
155 };
156 
157 /**
158  * Base class for GrClips that visualize a clip mask.
159  */
160 class MaskOnlyClipBase : public GrClip {
161 private:
quickContains(const SkRect &) const162     bool quickContains(const SkRect&) const final { return false; }
isRRect(const SkRect & rtBounds,SkRRect * rr,GrAA *) const163     bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
getConservativeBounds(int width,int height,SkIRect * rect,bool * iior) const164     void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
165         rect->set(0, 0, width, height);
166         if (iior) {
167             *iior = false;
168         }
169     }
170 };
171 
172 /**
173  * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
174  */
175 class AlphaOnlyClip final : public MaskOnlyClipBase {
176 public:
AlphaOnlyClip(sk_sp<GrTextureProxy> mask,int x,int y)177     AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
178 
179 private:
apply(GrRecordingContext *,GrRenderTargetContext *,bool,bool,GrAppliedClip * out,SkRect * bounds) const180     bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
181                SkRect* bounds) const override {
182         int w = fMask->width();
183         int h = fMask->height();
184         out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
185                 fMask, SkIRect::MakeWH(w, h), {fX, fY}));
186         return true;
187     }
188     sk_sp<GrTextureProxy> fMask;
189     int fX;
190     int fY;
191 };
192 
193 /**
194  * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
195  */
make_stencil_only_clip()196 static GrStencilClip make_stencil_only_clip() {
197     return GrStencilClip(SkClipStack::kEmptyGenID);
198 };
199 
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas,SkString * errorMsg)200 DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
201                                                         SkString* errorMsg) {
202     GrContext* ctx = canvas->getGrContext();
203     GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
204     if (!ctx || !rtc) {
205         *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
206         return DrawResult::kSkip;
207     }
208     if (rtc->priv().maxWindowRectangles() < kNumWindows) {
209         *errorMsg = "Requires at least 8 window rectangles. "
210                     "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
211         return DrawResult::kSkip;
212     }
213 
214     const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
215 
216     GrPaint paint;
217     if (rtc->numSamples() <= 1) {
218         paint.setColor4f({ 0, 0.25f, 1, 1 });
219         this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
220     } else {
221         paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
222         this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
223     }
224     return DrawResult::kOk;
225 }
226 
visualizeAlphaMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)227 void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
228                                                 const GrReducedClip& reducedClip, GrPaint&& paint) {
229     const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
230     const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
231     sk_sp<GrRenderTargetContext> maskRTC(ctx->priv().makeDeferredRenderTargetContextWithFallback(
232             SkBackingFit::kExact, kCoverRect.width() + padRight, kCoverRect.height() + padBottom,
233             GrColorType::kAlpha_8, nullptr));
234     if (!maskRTC) {
235         return;
236     }
237 
238     // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
239     // the clip mask generation.
240     this->stencilCheckerboard(maskRTC.get(), true);
241     maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
242     GrPaint stencilPaint;
243     stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
244     maskRTC->priv().stencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
245                                 std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
246                                 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
247     reducedClip.drawAlphaClipMask(maskRTC.get());
248 
249     int x = kCoverRect.x() - kDeviceRect.x(),
250         y = kCoverRect.y() - kDeviceRect.y();
251 
252     // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
253     // inside window rectangles or outside the scissor should still have the initial checkerboard
254     // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
255     AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
256     rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
257                   SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
258 }
259 
visualizeStencilMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)260 void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
261                                                   const GrReducedClip& reducedClip,
262                                                   GrPaint&& paint) {
263     // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
264     // by the clip mask generation.
265     this->stencilCheckerboard(rtc, false);
266     reducedClip.drawStencilClipMask(ctx, rtc);
267 
268     // Now visualize the stencil mask by covering the entire render target. The regions inside
269     // window rectangles or outside the scissor should still have the initial checkerboard intact.
270     // (This verifies we didn't spend any time modifying those pixels in the mask.)
271     rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
272 }
273 
stencilCheckerboard(GrRenderTargetContext * rtc,bool flip)274 void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
275     constexpr static GrUserStencilSettings kSetClip(
276         GrUserStencilSettings::StaticInit<
277         0,
278         GrUserStencilTest::kAlways,
279         0,
280         GrUserStencilOp::kSetClipBit,
281         GrUserStencilOp::kKeep,
282         0>()
283     );
284 
285     rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
286 
287     for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
288         for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
289              x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
290             SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
291             GrPaint paint;
292             paint.setXPFactory(GrDisableColorXPFactory::Get());
293             rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
294                                     SkMatrix::I(), SkRect::Make(checker));
295         }
296     }
297 }
298 
299 DEF_GM( return new WindowRectanglesMaskGM(); )
300 
301 }
302