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