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