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