blob: 14d10e251bc7941fb1bcd6d5c886b2268adb0d52 [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
Brian Salomonc7fe0f72018-05-11 10:14:21 -040013#include "GrAppliedClip.h"
14#include "GrCaps.h"
15#include "GrContextPriv.h"
16#include "GrReducedClip.h"
17#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "GrStencilClip.h"
21#include "effects/GrTextureDomain.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070022
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
mtklein0a441072016-09-06 11:45:31 -0700105constexpr static int kNumWindows = 8;
106
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700107/**
Chris Daltonc5348082018-03-30 15:59:38 +0000108 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
109 * this test is to verify that window rectangles are being used during clip mask generation, and to
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700110 * visualize where the window rectangles are placed.
111 *
112 * We use window rectangles when generating the clip mask because there is no need to invest time
113 * defining those regions where window rectangles will be in effect during the actual draw anyway.
114 *
115 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
116 * it, and then covering the mask with a solid color once it has been generated. The regions inside
117 * window rectangles or outside the scissor should still have the initial checkerboard intact.
118 */
119class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
120private:
121 constexpr static int kMaskCheckerSize = 5;
122 SkString onShortName() final { return SkString("windowrectangles_mask"); }
123 void onCoverClipStack(const SkClipStack&, SkCanvas*) final;
Chris Daltonc5348082018-03-30 15:59:38 +0000124 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Salomon82f44312017-01-11 13:42:54 -0500125 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400126 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700127 void fail(SkCanvas*);
128};
129
130/**
131 * Base class for GrClips that visualize a clip mask.
132 */
133class MaskOnlyClipBase : public GrClip {
134private:
135 bool quickContains(const SkRect&) const final { return false; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500136 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700137 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
138 rect->set(0, 0, width, height);
139 if (iior) {
140 *iior = false;
141 }
142 }
143};
144
145/**
146 * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
147 */
148class AlphaOnlyClip final : public MaskOnlyClipBase {
149public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400150 AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
151
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700152private:
Brian Salomon97180af2017-03-14 13:42:58 -0400153 bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
154 SkRect* bounds) const override {
Brian Salomonaff329b2017-08-11 09:40:37 -0400155 int w = fMask->width();
156 int h = fMask->height();
157 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
158 fMask, SkIRect::MakeWH(w, h), {fX, fY}));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700159 return true;
160 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400161 sk_sp<GrTextureProxy> fMask;
162 int fX;
163 int fY;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700164};
165
166/**
Chris Daltonbbfd5162017-11-07 13:35:22 -0700167 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700168 */
Chris Daltonbbfd5162017-11-07 13:35:22 -0700169static GrStencilClip make_stencil_only_clip() {
170 return GrStencilClip(SkClipStack::kEmptyGenID);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700171};
172
173void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
174 GrContext* ctx = canvas->getGrContext();
Brian Osman11052242016-10-27 14:47:55 -0400175 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700176
Robert Phillipsec2249f2016-11-09 08:54:35 -0500177 if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700178 this->fail(canvas);
179 return;
180 }
181
Chris Dalton4c458b12018-06-16 17:22:59 -0600182 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700183
Chris Daltonc5348082018-03-30 15:59:38 +0000184 GrPaint paint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400185 if (GrFSAAType::kNone == rtc->fsaaType()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700186 paint.setColor4f(GrColor4f(0, 0.25f, 1, 1));
Chris Daltonc5348082018-03-30 15:59:38 +0000187 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700188 } else {
189 paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1));
Chris Daltonc5348082018-03-30 15:59:38 +0000190 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700191 }
Chris Daltonc5348082018-03-30 15:59:38 +0000192}
193
194void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
195 const GrReducedClip& reducedClip, GrPaint&& paint) {
196 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
197 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
198 sk_sp<GrRenderTargetContext> maskRTC(
199 ctx->contextPriv().makeDeferredRenderTargetContextWithFallback(
200 SkBackingFit::kExact,
201 kCoverRect.width() + padRight,
202 kCoverRect.height() + padBottom,
203 kAlpha_8_GrPixelConfig, nullptr));
204 if (!maskRTC) {
205 return;
206 }
207
208 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
209 // the clip mask generation.
210 this->stencilCheckerboard(maskRTC.get(), true);
211 maskRTC->clear(nullptr, GrColorPackA4(0xff), GrRenderTargetContext::CanClearFullscreen::kYes);
212 maskRTC->priv().drawAndStencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
213 SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
214 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
215 reducedClip.drawAlphaClipMask(maskRTC.get());
216
217 int x = kCoverRect.x() - kDeviceRect.x(),
218 y = kCoverRect.y() - kDeviceRect.y();
219
220 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
221 // inside window rectangles or outside the scissor should still have the initial checkerboard
222 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
223 AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
224 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
225 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700226}
227
Brian Osman11052242016-10-27 14:47:55 -0400228void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700229 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500230 GrPaint&& paint) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700231 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
232 // by the clip mask generation.
Brian Osman11052242016-10-27 14:47:55 -0400233 this->stencilCheckerboard(rtc, false);
Brian Salomon9a767722017-03-13 17:57:28 -0400234 reducedClip.drawStencilClipMask(ctx, rtc);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700235
236 // Now visualize the stencil mask by covering the entire render target. The regions inside
Robert Phillips806be2d2017-06-28 15:23:59 -0400237 // window rectangles or outside the scissor should still have the initial checkerboard intact.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700238 // (This verifies we didn't spend any time modifying those pixels in the mask.)
Chris Daltonbbfd5162017-11-07 13:35:22 -0700239 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700240}
241
Brian Osman11052242016-10-27 14:47:55 -0400242void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700243 constexpr static GrUserStencilSettings kSetClip(
244 GrUserStencilSettings::StaticInit<
245 0,
246 GrUserStencilTest::kAlways,
247 0,
248 GrUserStencilOp::kSetClipBit,
249 GrUserStencilOp::kKeep,
250 0>()
251 );
252
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000253 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700254
Brian Salomon9a767722017-03-13 17:57:28 -0400255 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700256 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
Brian Salomon9a767722017-03-13 17:57:28 -0400257 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700258 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500259 rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(),
Brian Osman693a5402016-10-27 15:13:22 -0400260 SkRect::Make(checker));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700261 }
262 }
263}
264
265void WindowRectanglesMaskGM::fail(SkCanvas* canvas) {
266 SkPaint paint;
267 paint.setAntiAlias(true);
268 paint.setTextAlign(SkPaint::kCenter_Align);
269 paint.setTextSize(20);
270 sk_tool_utils::set_portable_typeface(&paint);
271
272 SkString errorMsg;
273 errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows);
274
275 canvas->clipRect(SkRect::Make(kCoverRect));
276 canvas->clear(SK_ColorWHITE);
Mike Reed1f275852018-04-11 14:30:17 -0400277
278 canvas->drawString(errorMsg, SkIntToScalar((kCoverRect.left() + kCoverRect.right())/2),
279 SkIntToScalar((kCoverRect.top() + kCoverRect.bottom())/2 - 10), paint);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700280}
281
282DEF_GM( return new WindowRectanglesMaskGM(); )
283
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700284}