blob: 5720efed3a9a67625aac9ba0bd850951b473ae07 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/private/GrTypesPriv.h"
26#include "include/private/SkColorData.h"
27#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkClipStack.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrAppliedClip.h"
30#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrFixedClip.h"
34#include "src/gpu/GrFragmentProcessor.h"
35#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrReducedClip.h"
37#include "src/gpu/GrRenderTargetContext.h"
38#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrStencilClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040040#include "src/gpu/GrTextureProxy.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "src/gpu/GrUserStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/effects/GrTextureDomain.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040043#include "tools/ToolUtils.h"
44
45#include <utility>
46
47class GrRecordingContext;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070048
49constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070050constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070051
52namespace skiagm {
53
54////////////////////////////////////////////////////////////////////////////////////////////////////
55
56class WindowRectanglesBaseGM : public GM {
57protected:
Chris Dalton50e24d72019-02-07 16:20:09 -070058 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070059
60private:
61 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
Chris Dalton50e24d72019-02-07 16:20:09 -070062 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070063};
64
Chris Dalton50e24d72019-02-07 16:20:09 -070065DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinea3f0142019-03-20 11:12:10 -050066 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070067
68 SkClipStack stack;
Brian Salomona3b45d42016-10-03 11:36:16 -040069 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050070 kDifference_SkClipOp, false);
Brian Salomona3b45d42016-10-03 11:36:16 -040071 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050072 kDifference_SkClipOp, true);
Brian Salomona3b45d42016-10-03 11:36:16 -040073 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
Mike Reedc1f77742016-12-09 09:00:50 -050074 SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070075
76 SkRRect nine;
77 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
Mike Reedc1f77742016-12-09 09:00:50 -050078 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070079
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);
Mike Reedc1f77742016-12-09 09:00:50 -050083 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070084
Chris Dalton50e24d72019-02-07 16:20:09 -070085 return this->onCoverClipStack(stack, canvas, errorMsg);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070086}
87
88////////////////////////////////////////////////////////////////////////////////////////////////////
89
90/**
91 * Draws a clip that will exercise window rectangles if they are supported.
92 */
93class WindowRectanglesGM : public WindowRectanglesBaseGM {
94private:
95 SkString onShortName() final { return SkString("windowrectangles"); }
Chris Dalton50e24d72019-02-07 16:20:09 -070096 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070097};
98
Chris Dalton50e24d72019-02-07 16:20:09 -070099DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
100 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700101 SkPaint paint;
102 paint.setColor(0xff00aa80);
103
104 // Set up the canvas's clip to match our SkClipStack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700105 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
106 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
Mike Reed598524d2017-03-08 13:13:44 -0500107 SkClipOp op = element->getOp();
108 bool isAA = element->isAA();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400109 switch (element->getDeviceSpaceType()) {
110 case SkClipStack::Element::DeviceSpaceType::kPath:
111 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500112 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400113 case SkClipStack::Element::DeviceSpaceType::kRRect:
114 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500115 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400116 case SkClipStack::Element::DeviceSpaceType::kRect:
117 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500118 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400119 case SkClipStack::Element::DeviceSpaceType::kEmpty:
Mike Reed598524d2017-03-08 13:13:44 -0500120 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
121 break;
122 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700123 }
124
125 canvas->drawRect(SkRect::Make(kCoverRect), paint);
Chris Dalton50e24d72019-02-07 16:20:09 -0700126 return DrawResult::kOk;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700127}
128
129DEF_GM( return new WindowRectanglesGM(); )
130
131////////////////////////////////////////////////////////////////////////////////////////////////////
132
mtklein0a441072016-09-06 11:45:31 -0700133constexpr static int kNumWindows = 8;
134
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700135/**
Chris Daltonc5348082018-03-30 15:59:38 +0000136 * 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
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700138 * 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 */
147class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
148private:
149 constexpr static int kMaskCheckerSize = 5;
150 SkString onShortName() final { return SkString("windowrectangles_mask"); }
Chris Dalton50e24d72019-02-07 16:20:09 -0700151 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
Chris Daltonc5348082018-03-30 15:59:38 +0000152 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Salomon82f44312017-01-11 13:42:54 -0500153 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400154 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700155};
156
157/**
158 * Base class for GrClips that visualize a clip mask.
159 */
160class MaskOnlyClipBase : public GrClip {
161private:
162 bool quickContains(const SkRect&) const final { return false; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500163 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700164 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
Mike Reed92b33352019-08-24 19:39:13 -0400165 rect->setWH(width, height);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700166 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 */
175class AlphaOnlyClip final : public MaskOnlyClipBase {
176public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400177 AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
178
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700179private:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500180 bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
Brian Salomon97180af2017-03-14 13:42:58 -0400181 SkRect* bounds) const override {
Brian Salomonaff329b2017-08-11 09:40:37 -0400182 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400183 fMask, SkIRect::MakeSize(fMask->dimensions()), {fX, fY}));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700184 return true;
185 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400186 sk_sp<GrTextureProxy> fMask;
187 int fX;
188 int fY;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700189};
190
191/**
Chris Daltonbbfd5162017-11-07 13:35:22 -0700192 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700193 */
Chris Daltonbbfd5162017-11-07 13:35:22 -0700194static GrStencilClip make_stencil_only_clip() {
195 return GrStencilClip(SkClipStack::kEmptyGenID);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700196};
197
Chris Dalton50e24d72019-02-07 16:20:09 -0700198DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
199 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700200 GrContext* ctx = canvas->getGrContext();
Brian Osman11052242016-10-27 14:47:55 -0400201 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
Chris Dalton50e24d72019-02-07 16:20:09 -0700202 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;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700210 }
211
Ethan Nicholaseace9352018-10-15 20:09:54 +0000212 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700213
Chris Daltonc5348082018-03-30 15:59:38 +0000214 GrPaint paint;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600215 if (rtc->numSamples() <= 1) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400216 paint.setColor4f({ 0, 0.25f, 1, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000217 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700218 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400219 paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000220 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700221 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700222 return DrawResult::kOk;
Chris Daltonc5348082018-03-30 15:59:38 +0000223}
224
225void 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;
Greg Daniele20fcad2020-01-08 11:52:34 -0500229 auto maskRTC = GrRenderTargetContext::MakeWithFallback(
230 ctx, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact,
231 {kCoverRect.width() + padRight, kCoverRect.height() + padBottom});
Chris Daltonc5348082018-03-30 15:59:38 +0000232 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);
Brian Osman9a9baae2018-11-05 15:06:26 -0500239 maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400240 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()));
Chris Daltonc5348082018-03-30 15:59:38 +0000245 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->asTextureProxyRef(), x, y);
254 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
255 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700256}
257
Brian Osman11052242016-10-27 14:47:55 -0400258void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700259 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500260 GrPaint&& paint) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700261 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
262 // by the clip mask generation.
Brian Osman11052242016-10-27 14:47:55 -0400263 this->stencilCheckerboard(rtc, false);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000264 reducedClip.drawStencilClipMask(ctx, rtc);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700265
266 // Now visualize the stencil mask by covering the entire render target. The regions inside
Robert Phillips806be2d2017-06-28 15:23:59 -0400267 // window rectangles or outside the scissor should still have the initial checkerboard intact.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700268 // (This verifies we didn't spend any time modifying those pixels in the mask.)
Chris Daltonbbfd5162017-11-07 13:35:22 -0700269 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700270}
271
Brian Osman11052242016-10-27 14:47:55 -0400272void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700273 constexpr static GrUserStencilSettings kSetClip(
274 GrUserStencilSettings::StaticInit<
275 0,
276 GrUserStencilTest::kAlways,
277 0,
278 GrUserStencilOp::kSetClipBit,
279 GrUserStencilOp::kKeep,
280 0>()
281 );
282
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000283 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700284
Brian Salomon9a767722017-03-13 17:57:28 -0400285 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700286 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
Brian Salomon9a767722017-03-13 17:57:28 -0400287 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700288 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400289 GrPaint paint;
290 paint.setXPFactory(GrDisableColorXPFactory::Get());
291 rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
292 SkMatrix::I(), SkRect::Make(checker));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700293 }
294 }
295}
296
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700297DEF_GM( return new WindowRectanglesMaskGM(); )
298
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700299}