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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRRect.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkRegion.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypes.h"
23 #include "include/gpu/GrBackendSurface.h"
24 #include "include/gpu/GrContext.h"
25 #include "include/private/GrTypesPriv.h"
26 #include "include/private/SkColorData.h"
27 #include "src/core/SkClipOpPriv.h"
28 #include "src/core/SkClipStack.h"
29 #include "src/gpu/GrAppliedClip.h"
30 #include "src/gpu/GrCaps.h"
31 #include "src/gpu/GrClip.h"
32 #include "src/gpu/GrContextPriv.h"
33 #include "src/gpu/GrFixedClip.h"
34 #include "src/gpu/GrFragmentProcessor.h"
35 #include "src/gpu/GrPaint.h"
36 #include "src/gpu/GrReducedClip.h"
37 #include "src/gpu/GrRenderTargetContext.h"
38 #include "src/gpu/GrRenderTargetContextPriv.h"
39 #include "src/gpu/GrStencilClip.h"
40 #include "src/gpu/GrTextureProxy.h"
41 #include "src/gpu/GrUserStencilSettings.h"
42 #include "src/gpu/effects/GrTextureDomain.h"
43 #include "tools/ToolUtils.h"
44
45 #include <utility>
46
47 class GrRecordingContext;
48
49 constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
50 constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
51
52 namespace skiagm {
53
54 ////////////////////////////////////////////////////////////////////////////////////////////////////
55
56 class WindowRectanglesBaseGM : public GM {
57 protected:
58 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
59
60 private:
onISize()61 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
62 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
63 };
64
onDraw(SkCanvas * canvas,SkString * errorMsg)65 DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
66 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
67
68 SkClipStack stack;
69 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
70 kDifference_SkClipOp, false);
71 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
72 kDifference_SkClipOp, true);
73 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
74 SkMatrix::I(), kDifference_SkClipOp, true);
75
76 SkRRect nine;
77 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
78 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
79
80 SkRRect complx;
81 SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
82 complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
83 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
84
85 return this->onCoverClipStack(stack, canvas, errorMsg);
86 }
87
88 ////////////////////////////////////////////////////////////////////////////////////////////////////
89
90 /**
91 * Draws a clip that will exercise window rectangles if they are supported.
92 */
93 class WindowRectanglesGM : public WindowRectanglesBaseGM {
94 private:
onShortName()95 SkString onShortName() final { return SkString("windowrectangles"); }
96 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
97 };
98
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas,SkString * errorMsg)99 DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
100 SkString* errorMsg) {
101 SkPaint paint;
102 paint.setColor(0xff00aa80);
103
104 // Set up the canvas's clip to match our SkClipStack.
105 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
106 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
107 SkClipOp op = element->getOp();
108 bool isAA = element->isAA();
109 switch (element->getDeviceSpaceType()) {
110 case SkClipStack::Element::DeviceSpaceType::kPath:
111 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
112 break;
113 case SkClipStack::Element::DeviceSpaceType::kRRect:
114 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
115 break;
116 case SkClipStack::Element::DeviceSpaceType::kRect:
117 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
118 break;
119 case SkClipStack::Element::DeviceSpaceType::kEmpty:
120 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
121 break;
122 }
123 }
124
125 canvas->drawRect(SkRect::Make(kCoverRect), paint);
126 return DrawResult::kOk;
127 }
128
129 DEF_GM( return new WindowRectanglesGM(); )
130
131 ////////////////////////////////////////////////////////////////////////////////////////////////////
132
133 constexpr static int kNumWindows = 8;
134
135 /**
136 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
137 * this test is to verify that window rectangles are being used during clip mask generation, and to
138 * visualize where the window rectangles are placed.
139 *
140 * We use window rectangles when generating the clip mask because there is no need to invest time
141 * defining those regions where window rectangles will be in effect during the actual draw anyway.
142 *
143 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
144 * it, and then covering the mask with a solid color once it has been generated. The regions inside
145 * window rectangles or outside the scissor should still have the initial checkerboard intact.
146 */
147 class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
148 private:
149 constexpr static int kMaskCheckerSize = 5;
onShortName()150 SkString onShortName() final { return SkString("windowrectangles_mask"); }
151 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
152 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
153 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
154 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
155 };
156
157 /**
158 * Base class for GrClips that visualize a clip mask.
159 */
160 class MaskOnlyClipBase : public GrClip {
161 private:
quickContains(const SkRect &) const162 bool quickContains(const SkRect&) const final { return false; }
isRRect(const SkRect & rtBounds,SkRRect * rr,GrAA *) const163 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
getConservativeBounds(int width,int height,SkIRect * rect,bool * iior) const164 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
165 rect->setWH(width, height);
166 if (iior) {
167 *iior = false;
168 }
169 }
170 };
171
172 /**
173 * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
174 */
175 class AlphaOnlyClip final : public MaskOnlyClipBase {
176 public:
AlphaOnlyClip(GrSurfaceProxyView mask,int x,int y)177 AlphaOnlyClip(GrSurfaceProxyView mask, int x, int y) : fMask(std::move(mask)), fX(x), fY(y) {}
178
179 private:
apply(GrRecordingContext *,GrRenderTargetContext *,bool,bool,GrAppliedClip * out,SkRect * bounds) const180 bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
181 SkRect* bounds) const override {
182 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
183 fMask, SkIRect::MakeSize(fMask.proxy()->dimensions()), {fX, fY}));
184 return true;
185 }
186 GrSurfaceProxyView fMask;
187 int fX;
188 int fY;
189 };
190
191 /**
192 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
193 */
make_stencil_only_clip()194 static GrStencilClip make_stencil_only_clip() {
195 return GrStencilClip(SkClipStack::kEmptyGenID);
196 };
197
onCoverClipStack(const SkClipStack & stack,SkCanvas * canvas,SkString * errorMsg)198 DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
199 SkString* errorMsg) {
200 GrContext* ctx = canvas->getGrContext();
201 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
202 if (!ctx || !rtc) {
203 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
204 return DrawResult::kSkip;
205 }
206 if (rtc->priv().maxWindowRectangles() < kNumWindows) {
207 *errorMsg = "Requires at least 8 window rectangles. "
208 "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
209 return DrawResult::kSkip;
210 }
211
212 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
213
214 GrPaint paint;
215 if (rtc->numSamples() <= 1) {
216 paint.setColor4f({ 0, 0.25f, 1, 1 });
217 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
218 } else {
219 paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
220 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
221 }
222 return DrawResult::kOk;
223 }
224
visualizeAlphaMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)225 void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
226 const GrReducedClip& reducedClip, GrPaint&& paint) {
227 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
228 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
229 auto maskRTC = GrRenderTargetContext::MakeWithFallback(
230 ctx, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact,
231 {kCoverRect.width() + padRight, kCoverRect.height() + padBottom});
232 if (!maskRTC) {
233 return;
234 }
235
236 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
237 // the clip mask generation.
238 this->stencilCheckerboard(maskRTC.get(), true);
239 maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
240 GrPaint stencilPaint;
241 stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
242 maskRTC->priv().stencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
243 std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
244 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
245 reducedClip.drawAlphaClipMask(maskRTC.get());
246
247 int x = kCoverRect.x() - kDeviceRect.x(),
248 y = kCoverRect.y() - kDeviceRect.y();
249
250 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
251 // inside window rectangles or outside the scissor should still have the initial checkerboard
252 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
253 AlphaOnlyClip clip(maskRTC->readSurfaceView(), x, y);
254 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
255 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
256 }
257
visualizeStencilMask(GrContext * ctx,GrRenderTargetContext * rtc,const GrReducedClip & reducedClip,GrPaint && paint)258 void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
259 const GrReducedClip& reducedClip,
260 GrPaint&& paint) {
261 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
262 // by the clip mask generation.
263 this->stencilCheckerboard(rtc, false);
264 reducedClip.drawStencilClipMask(ctx, rtc);
265
266 // Now visualize the stencil mask by covering the entire render target. The regions inside
267 // window rectangles or outside the scissor should still have the initial checkerboard intact.
268 // (This verifies we didn't spend any time modifying those pixels in the mask.)
269 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
270 }
271
stencilCheckerboard(GrRenderTargetContext * rtc,bool flip)272 void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
273 constexpr static GrUserStencilSettings kSetClip(
274 GrUserStencilSettings::StaticInit<
275 0,
276 GrUserStencilTest::kAlways,
277 0,
278 GrUserStencilOp::kSetClipBit,
279 GrUserStencilOp::kKeep,
280 0>()
281 );
282
283 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
284
285 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
286 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
287 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
288 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
289 GrPaint paint;
290 paint.setXPFactory(GrDisableColorXPFactory::Get());
291 rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
292 SkMatrix::I(), SkRect::Make(checker));
293 }
294 }
295 }
296
297 DEF_GM( return new WindowRectanglesMaskGM(); )
298
299 }
300