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