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 "GrStencilClip.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->getDeviceSpaceType()) {
83 case SkClipStack::Element::DeviceSpaceType::kPath:
84 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
85 break;
86 case SkClipStack::Element::DeviceSpaceType::kRRect:
87 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
88 break;
89 case SkClipStack::Element::DeviceSpaceType::kRect:
90 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
91 break;
92 case SkClipStack::Element::DeviceSpaceType::kEmpty:
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) : fMask(mask), fX(x), fY(y) {}
153
154 private:
apply(GrContext *,GrRenderTargetContext *,bool,bool,GrAppliedClip * out,SkRect * bounds) const155 bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
156 SkRect* bounds) const override {
157 int w = fMask->width();
158 int h = fMask->height();
159 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
160 fMask, SkIRect::MakeWH(w, h), {fX, fY}));
161 return true;
162 }
163 sk_sp<GrTextureProxy> fMask;
164 int fX;
165 int fY;
166 };
167
168 /**
169 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
170 */
make_stencil_only_clip()171 static GrStencilClip make_stencil_only_clip() {
172 return GrStencilClip(SkClipStack::kEmptyGenID);
173 };
174
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas)175 void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
176 GrContext* ctx = canvas->getGrContext();
177 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
178
179 if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) {
180 this->fail(canvas);
181 return;
182 }
183
184 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps()->shaderCaps(),
185 kNumWindows);
186
187 GrPaint paint;
188 if (GrFSAAType::kNone == rtc->fsaaType()) {
189 paint.setColor4f(GrColor4f(0, 0.25f, 1, 1));
190 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
191 } else {
192 paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1));
193 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
194 }
195 }
196
visualizeAlphaMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)197 void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
198 const GrReducedClip& reducedClip, GrPaint&& paint) {
199 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
200 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
201 sk_sp<GrRenderTargetContext> maskRTC(
202 ctx->makeDeferredRenderTargetContextWithFallback(SkBackingFit::kExact,
203 kCoverRect.width() + padRight,
204 kCoverRect.height() + padBottom,
205 kAlpha_8_GrPixelConfig, nullptr));
206 if (!maskRTC) {
207 return;
208 }
209
210 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
211 // the clip mask generation.
212 this->stencilCheckerboard(maskRTC.get(), true);
213 maskRTC->clear(nullptr, GrColorPackA4(0xff), GrRenderTargetContext::CanClearFullscreen::kYes);
214 maskRTC->priv().drawAndStencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
215 SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
216 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
217 reducedClip.drawAlphaClipMask(maskRTC.get());
218
219 int x = kCoverRect.x() - kDeviceRect.x(),
220 y = kCoverRect.y() - kDeviceRect.y();
221
222 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
223 // inside window rectangles or outside the scissor should still have the initial checkerboard
224 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
225 AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
226 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
227 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
228 }
229
visualizeStencilMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)230 void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
231 const GrReducedClip& reducedClip,
232 GrPaint&& paint) {
233 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
234 // by the clip mask generation.
235 this->stencilCheckerboard(rtc, false);
236 reducedClip.drawStencilClipMask(ctx, rtc);
237
238 // Now visualize the stencil mask by covering the entire render target. The regions inside
239 // window rectangles or outside the scissor should still have the initial checkerboard intact.
240 // (This verifies we didn't spend any time modifying those pixels in the mask.)
241 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
242 }
243
stencilCheckerboard(GrRenderTargetContext * rtc,bool flip)244 void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
245 constexpr static GrUserStencilSettings kSetClip(
246 GrUserStencilSettings::StaticInit<
247 0,
248 GrUserStencilTest::kAlways,
249 0,
250 GrUserStencilOp::kSetClipBit,
251 GrUserStencilOp::kKeep,
252 0>()
253 );
254
255 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
256
257 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
258 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
259 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
260 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
261 rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(),
262 SkRect::Make(checker));
263 }
264 }
265 }
266
fail(SkCanvas * canvas)267 void WindowRectanglesMaskGM::fail(SkCanvas* canvas) {
268 SkPaint paint;
269 paint.setAntiAlias(true);
270 paint.setTextAlign(SkPaint::kCenter_Align);
271 paint.setTextSize(20);
272 sk_tool_utils::set_portable_typeface(&paint);
273
274 SkString errorMsg;
275 errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows);
276
277 canvas->clipRect(SkRect::Make(kCoverRect));
278 canvas->clear(SK_ColorWHITE);
279 canvas->drawString(errorMsg, SkIntToScalar(kCoverRect.centerX()),
280 SkIntToScalar(kCoverRect.centerY() - 10), paint);
281 }
282
283 DEF_GM( return new WindowRectanglesMaskGM(); )
284
285 #endif
286
287 }
288