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