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 #include "SkTextUtils.h"
13
14 #include "GrAppliedClip.h"
15 #include "GrCaps.h"
16 #include "GrContextPriv.h"
17 #include "GrReducedClip.h"
18 #include "GrRenderTargetContext.h"
19 #include "GrRenderTargetContextPriv.h"
20 #include "GrResourceProvider.h"
21 #include "GrStencilClip.h"
22 #include "effects/GrTextureDomain.h"
23
24 constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
25 constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
26
27 namespace skiagm {
28
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
30
31 class WindowRectanglesBaseGM : public GM {
32 protected:
33 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
34
35 private:
onISize()36 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
37 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
38 };
39
onDraw(SkCanvas * canvas,SkString * errorMsg)40 DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
41 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
42
43 SkClipStack stack;
44 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
45 kDifference_SkClipOp, false);
46 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
47 kDifference_SkClipOp, true);
48 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
49 SkMatrix::I(), kDifference_SkClipOp, true);
50
51 SkRRect nine;
52 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
53 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
54
55 SkRRect complx;
56 SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
57 complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
58 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
59
60 return this->onCoverClipStack(stack, canvas, errorMsg);
61 }
62
63 ////////////////////////////////////////////////////////////////////////////////////////////////////
64
65 /**
66 * Draws a clip that will exercise window rectangles if they are supported.
67 */
68 class WindowRectanglesGM : public WindowRectanglesBaseGM {
69 private:
onShortName()70 SkString onShortName() final { return SkString("windowrectangles"); }
71 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
72 };
73
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas,SkString * errorMsg)74 DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
75 SkString* errorMsg) {
76 SkPaint paint;
77 paint.setColor(0xff00aa80);
78
79 // Set up the canvas's clip to match our SkClipStack.
80 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
81 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
82 SkClipOp op = element->getOp();
83 bool isAA = element->isAA();
84 switch (element->getDeviceSpaceType()) {
85 case SkClipStack::Element::DeviceSpaceType::kPath:
86 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
87 break;
88 case SkClipStack::Element::DeviceSpaceType::kRRect:
89 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
90 break;
91 case SkClipStack::Element::DeviceSpaceType::kRect:
92 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
93 break;
94 case SkClipStack::Element::DeviceSpaceType::kEmpty:
95 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
96 break;
97 }
98 }
99
100 canvas->drawRect(SkRect::Make(kCoverRect), paint);
101 return DrawResult::kOk;
102 }
103
104 DEF_GM( return new WindowRectanglesGM(); )
105
106 ////////////////////////////////////////////////////////////////////////////////////////////////////
107
108 constexpr static int kNumWindows = 8;
109
110 /**
111 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
112 * this test is to verify that window rectangles are being used during clip mask generation, and to
113 * visualize where the window rectangles are placed.
114 *
115 * We use window rectangles when generating the clip mask because there is no need to invest time
116 * defining those regions where window rectangles will be in effect during the actual draw anyway.
117 *
118 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
119 * it, and then covering the mask with a solid color once it has been generated. The regions inside
120 * window rectangles or outside the scissor should still have the initial checkerboard intact.
121 */
122 class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
123 private:
124 constexpr static int kMaskCheckerSize = 5;
onShortName()125 SkString onShortName() final { return SkString("windowrectangles_mask"); }
126 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
127 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
128 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
129 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
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(GrRecordingContext *,GrRenderTargetContext *,bool,bool,GrAppliedClip * out,SkRect * bounds) const155 bool apply(GrRecordingContext*, 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,SkString * errorMsg)175 DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
176 SkString* errorMsg) {
177 GrContext* ctx = canvas->getGrContext();
178 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
179 if (!ctx || !rtc) {
180 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
181 return DrawResult::kSkip;
182 }
183 if (rtc->priv().maxWindowRectangles() < kNumWindows) {
184 *errorMsg = "Requires at least 8 window rectangles. "
185 "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
186 return DrawResult::kSkip;
187 }
188
189 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
190
191 GrPaint paint;
192 if (GrFSAAType::kNone == rtc->fsaaType()) {
193 paint.setColor4f({ 0, 0.25f, 1, 1 });
194 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
195 } else {
196 paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
197 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
198 }
199 return DrawResult::kOk;
200 }
201
visualizeAlphaMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)202 void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
203 const GrReducedClip& reducedClip, GrPaint&& paint) {
204 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
205 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
206 const GrBackendFormat format =
207 ctx->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
208 sk_sp<GrRenderTargetContext> maskRTC(
209 ctx->priv().makeDeferredRenderTargetContextWithFallback(
210 format, SkBackingFit::kExact,
211 kCoverRect.width() + padRight,
212 kCoverRect.height() + padBottom,
213 kAlpha_8_GrPixelConfig, nullptr));
214 if (!maskRTC) {
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(maskRTC.get(), true);
221 maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
222 maskRTC->priv().drawAndStencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
223 SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
224 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
225 reducedClip.drawAlphaClipMask(maskRTC.get());
226
227 int x = kCoverRect.x() - kDeviceRect.x(),
228 y = kCoverRect.y() - kDeviceRect.y();
229
230 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
231 // inside window rectangles or outside the scissor should still have the initial checkerboard
232 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
233 AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
234 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
235 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
236 }
237
visualizeStencilMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)238 void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
239 const GrReducedClip& reducedClip,
240 GrPaint&& paint) {
241 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
242 // by the clip mask generation.
243 this->stencilCheckerboard(rtc, false);
244 reducedClip.drawStencilClipMask(ctx, rtc);
245
246 // Now visualize the stencil mask by covering the entire render target. The regions inside
247 // window rectangles or outside the scissor should still have the initial checkerboard intact.
248 // (This verifies we didn't spend any time modifying those pixels in the mask.)
249 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
250 }
251
stencilCheckerboard(GrRenderTargetContext * rtc,bool flip)252 void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
253 constexpr static GrUserStencilSettings kSetClip(
254 GrUserStencilSettings::StaticInit<
255 0,
256 GrUserStencilTest::kAlways,
257 0,
258 GrUserStencilOp::kSetClipBit,
259 GrUserStencilOp::kKeep,
260 0>()
261 );
262
263 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
264
265 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
266 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
267 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
268 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
269 rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(),
270 SkRect::Make(checker));
271 }
272 }
273 }
274
275 DEF_GM( return new WindowRectanglesMaskGM(); )
276
277 }
278