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