blob: 931a154df4be8282cd85472532ba51892eeec116 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070010#include "SkClipStack.h"
11#include "SkRRect.h"
12
13#if SK_SUPPORT_GPU
14# include "GrAppliedClip.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070015# include "GrStencilClip.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070016# include "GrReducedClip.h"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040017# include "GrRenderTargetContext.h"
18# include "GrRenderTargetContextPriv.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070019# include "GrResourceProvider.h"
20# include "effects/GrTextureDomain.h"
21#endif
22
23constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070024constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070025
26namespace skiagm {
27
28////////////////////////////////////////////////////////////////////////////////////////////////////
29
30class WindowRectanglesBaseGM : public GM {
31protected:
32 virtual void onCoverClipStack(const SkClipStack&, SkCanvas*) = 0;
33
34private:
35 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
36 void onDraw(SkCanvas*) final;
37};
38
39void WindowRectanglesBaseGM::onDraw(SkCanvas* canvas) {
40 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070041
42 SkClipStack stack;
Brian Salomona3b45d42016-10-03 11:36:16 -040043 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050044 kDifference_SkClipOp, false);
Brian Salomona3b45d42016-10-03 11:36:16 -040045 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050046 kDifference_SkClipOp, true);
Brian Salomona3b45d42016-10-03 11:36:16 -040047 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
Mike Reedc1f77742016-12-09 09:00:50 -050048 SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070049
50 SkRRect nine;
51 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
Mike Reedc1f77742016-12-09 09:00:50 -050052 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070053
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);
Mike Reedc1f77742016-12-09 09:00:50 -050057 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070058
59 this->onCoverClipStack(stack, canvas);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070060}
61
62////////////////////////////////////////////////////////////////////////////////////////////////////
63
64/**
65 * Draws a clip that will exercise window rectangles if they are supported.
66 */
67class WindowRectanglesGM : public WindowRectanglesBaseGM {
68private:
69 SkString onShortName() final { return SkString("windowrectangles"); }
70 void onCoverClipStack(const SkClipStack&, SkCanvas*) final;
71};
72
csmartdaltonbf4a8f92016-09-06 10:01:06 -070073void 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.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070078 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
79 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
Mike Reed598524d2017-03-08 13:13:44 -050080 SkClipOp op = element->getOp();
81 bool isAA = element->isAA();
Brian Salomonf3b46e52017-08-30 11:37:57 -040082 switch (element->getDeviceSpaceType()) {
83 case SkClipStack::Element::DeviceSpaceType::kPath:
84 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -050085 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -040086 case SkClipStack::Element::DeviceSpaceType::kRRect:
87 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -050088 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -040089 case SkClipStack::Element::DeviceSpaceType::kRect:
90 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -050091 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -040092 case SkClipStack::Element::DeviceSpaceType::kEmpty:
Mike Reed598524d2017-03-08 13:13:44 -050093 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
94 break;
95 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070096 }
97
98 canvas->drawRect(SkRect::Make(kCoverRect), paint);
99}
100
101DEF_GM( return new WindowRectanglesGM(); )
102
103////////////////////////////////////////////////////////////////////////////////////////////////////
104
105#if SK_SUPPORT_GPU
106
mtklein0a441072016-09-06 11:45:31 -0700107constexpr static int kNumWindows = 8;
108
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700109/**
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 */
121class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
122private:
123 constexpr static int kMaskCheckerSize = 5;
124 SkString onShortName() final { return SkString("windowrectangles_mask"); }
125 void onCoverClipStack(const SkClipStack&, SkCanvas*) final;
Brian Salomon82f44312017-01-11 13:42:54 -0500126 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
127 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400128 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700129 void fail(SkCanvas*);
130};
131
132/**
133 * Base class for GrClips that visualize a clip mask.
134 */
135class MaskOnlyClipBase : public GrClip {
136private:
137 bool quickContains(const SkRect&) const final { return false; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500138 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700139 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 */
150class AlphaOnlyClip final : public MaskOnlyClipBase {
151public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400152 AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
153
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700154private:
Brian Salomon97180af2017-03-14 13:42:58 -0400155 bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
156 SkRect* bounds) const override {
Brian Salomonaff329b2017-08-11 09:40:37 -0400157 int w = fMask->width();
158 int h = fMask->height();
159 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
160 fMask, SkIRect::MakeWH(w, h), {fX, fY}));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700161 return true;
162 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400163 sk_sp<GrTextureProxy> fMask;
164 int fX;
165 int fY;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700166};
167
168/**
Chris Daltonbbfd5162017-11-07 13:35:22 -0700169 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700170 */
Chris Daltonbbfd5162017-11-07 13:35:22 -0700171static GrStencilClip make_stencil_only_clip() {
172 return GrStencilClip(SkClipStack::kEmptyGenID);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700173};
174
175void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
176 GrContext* ctx = canvas->getGrContext();
Brian Osman11052242016-10-27 14:47:55 -0400177 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700178
Robert Phillipsec2249f2016-11-09 08:54:35 -0500179 if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700180 this->fail(canvas);
181 return;
182 }
183
Brian Salomon14471772017-12-05 10:35:15 -0500184 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps()->shaderCaps(),
185 kNumWindows);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700186
187 GrPaint paint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400188 if (GrFSAAType::kNone == rtc->fsaaType()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700189 paint.setColor4f(GrColor4f(0, 0.25f, 1, 1));
Brian Salomon82f44312017-01-11 13:42:54 -0500190 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700191 } else {
192 paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1));
Brian Salomon82f44312017-01-11 13:42:54 -0500193 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700194 }
195}
196
Brian Osman11052242016-10-27 14:47:55 -0400197void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500198 const GrReducedClip& reducedClip, GrPaint&& paint) {
Brian Salomon9a767722017-03-13 17:57:28 -0400199 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
200 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
Brian Osman693a5402016-10-27 15:13:22 -0400201 sk_sp<GrRenderTargetContext> maskRTC(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500202 ctx->contextPriv().makeDeferredRenderTargetContextWithFallback(
203 SkBackingFit::kExact,
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400204 kCoverRect.width() + padRight,
205 kCoverRect.height() + padBottom,
206 kAlpha_8_GrPixelConfig, nullptr));
Robert Phillipsc0192e32017-09-21 12:00:26 -0400207 if (!maskRTC) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700208 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.
Brian Osman693a5402016-10-27 15:13:22 -0400213 this->stencilCheckerboard(maskRTC.get(), true);
Chris Dalton344e9032017-12-11 15:42:09 -0700214 maskRTC->clear(nullptr, GrColorPackA4(0xff), GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Daltonbbfd5162017-11-07 13:35:22 -0700215 maskRTC->priv().drawAndStencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500216 SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
Brian Osman693a5402016-10-27 15:13:22 -0400217 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
218 reducedClip.drawAlphaClipMask(maskRTC.get());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700219
Brian Salomon9a767722017-03-13 17:57:28 -0400220 int x = kCoverRect.x() - kDeviceRect.x(),
221 y = kCoverRect.y() - kDeviceRect.y();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700222
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.)
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400226 AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
Brian Salomon82f44312017-01-11 13:42:54 -0500227 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
Robert Phillips40fd7c92017-01-30 08:06:27 -0500228 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700229}
230
Brian Osman11052242016-10-27 14:47:55 -0400231void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700232 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500233 GrPaint&& paint) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700234 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
235 // by the clip mask generation.
Brian Osman11052242016-10-27 14:47:55 -0400236 this->stencilCheckerboard(rtc, false);
Brian Salomon9a767722017-03-13 17:57:28 -0400237 reducedClip.drawStencilClipMask(ctx, rtc);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700238
239 // Now visualize the stencil mask by covering the entire render target. The regions inside
Robert Phillips806be2d2017-06-28 15:23:59 -0400240 // window rectangles or outside the scissor should still have the initial checkerboard intact.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700241 // (This verifies we didn't spend any time modifying those pixels in the mask.)
Chris Daltonbbfd5162017-11-07 13:35:22 -0700242 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700243}
244
Brian Osman11052242016-10-27 14:47:55 -0400245void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700246 constexpr static GrUserStencilSettings kSetClip(
247 GrUserStencilSettings::StaticInit<
248 0,
249 GrUserStencilTest::kAlways,
250 0,
251 GrUserStencilOp::kSetClipBit,
252 GrUserStencilOp::kKeep,
253 0>()
254 );
255
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000256 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700257
Brian Salomon9a767722017-03-13 17:57:28 -0400258 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700259 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
Brian Salomon9a767722017-03-13 17:57:28 -0400260 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700261 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500262 rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(),
Brian Osman693a5402016-10-27 15:13:22 -0400263 SkRect::Make(checker));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700264 }
265 }
266}
267
268void WindowRectanglesMaskGM::fail(SkCanvas* canvas) {
269 SkPaint paint;
270 paint.setAntiAlias(true);
271 paint.setTextAlign(SkPaint::kCenter_Align);
272 paint.setTextSize(20);
273 sk_tool_utils::set_portable_typeface(&paint);
274
275 SkString errorMsg;
276 errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows);
277
278 canvas->clipRect(SkRect::Make(kCoverRect));
279 canvas->clear(SK_ColorWHITE);
Cary Clark2a475ea2017-04-28 15:35:12 -0400280 canvas->drawString(errorMsg, SkIntToScalar(kCoverRect.centerX()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700281 SkIntToScalar(kCoverRect.centerY() - 10), paint);
282}
283
284DEF_GM( return new WindowRectanglesMaskGM(); )
285
286#endif
287
288}