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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkClipStack.h" |
| 10 | #include "SkRRect.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | # include "GrAppliedClip.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | # include "GrRenderTargetContext.h" |
| 15 | # include "GrRenderTargetContextPriv.h" |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 16 | # include "GrFixedClip.h" |
| 17 | # include "GrReducedClip.h" |
| 18 | # include "GrRenderTargetPriv.h" |
| 19 | # include "GrResourceProvider.h" |
| 20 | # include "effects/GrTextureDomain.h" |
| 21 | #endif |
| 22 | |
| 23 | constexpr static SkIRect kDeviceRect = {0, 0, 600, 600}; |
| 24 | constexpr static SkIRect kLayerRect = {25, 25, 575, 575}; |
| 25 | constexpr static SkIRect kCoverRect = {50, 50, 550, 550}; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 26 | |
| 27 | namespace skiagm { |
| 28 | |
| 29 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | class WindowRectanglesBaseGM : public GM { |
| 32 | protected: |
| 33 | virtual void onCoverClipStack(const SkClipStack&, SkCanvas*) = 0; |
| 34 | |
| 35 | private: |
| 36 | SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); } |
| 37 | void onDraw(SkCanvas*) final; |
| 38 | }; |
| 39 | |
| 40 | void WindowRectanglesBaseGM::onDraw(SkCanvas* canvas) { |
| 41 | sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25); |
| 42 | canvas->saveLayer(SkRect::Make(kLayerRect), nullptr); |
| 43 | |
| 44 | SkClipStack stack; |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 45 | stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 46 | kDifference_SkClipOp, false); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 47 | stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 48 | kDifference_SkClipOp, true); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 49 | stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 50 | SkMatrix::I(), kDifference_SkClipOp, true); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 51 | |
| 52 | SkRRect nine; |
| 53 | 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] | 54 | stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 55 | |
| 56 | SkRRect complx; |
| 57 | SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}}; |
| 58 | complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 59 | stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 60 | |
| 61 | this->onCoverClipStack(stack, canvas); |
| 62 | |
| 63 | canvas->restore(); |
| 64 | } |
| 65 | |
| 66 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | /** |
| 69 | * Draws a clip that will exercise window rectangles if they are supported. |
| 70 | */ |
| 71 | class WindowRectanglesGM : public WindowRectanglesBaseGM { |
| 72 | private: |
| 73 | SkString onShortName() final { return SkString("windowrectangles"); } |
| 74 | void onCoverClipStack(const SkClipStack&, SkCanvas*) final; |
| 75 | }; |
| 76 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 77 | void WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) { |
| 78 | SkPaint paint; |
| 79 | paint.setColor(0xff00aa80); |
| 80 | |
| 81 | // Set up the canvas's clip to match our SkClipStack. |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 82 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 83 | for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) { |
Mike Reed | 598524d | 2017-03-08 13:13:44 -0500 | [diff] [blame^] | 84 | SkClipOp op = element->getOp(); |
| 85 | bool isAA = element->isAA(); |
| 86 | switch (element->getType()) { |
| 87 | case SkClipStack::Element::kPath_Type: |
| 88 | canvas->clipPath(element->getPath(), op, isAA); |
| 89 | break; |
| 90 | case SkClipStack::Element::kRRect_Type: |
| 91 | canvas->clipRRect(element->getRRect(), op, isAA); |
| 92 | break; |
| 93 | case SkClipStack::Element::kRect_Type: |
| 94 | canvas->clipRect(element->getRect(), op, isAA); |
| 95 | break; |
| 96 | case SkClipStack::Element::kEmpty_Type: |
| 97 | canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false); |
| 98 | break; |
| 99 | } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | canvas->drawRect(SkRect::Make(kCoverRect), paint); |
| 103 | } |
| 104 | |
| 105 | DEF_GM( return new WindowRectanglesGM(); ) |
| 106 | |
| 107 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | #if SK_SUPPORT_GPU |
| 110 | |
mtklein | 0a44107 | 2016-09-06 11:45:31 -0700 | [diff] [blame] | 111 | constexpr static int kNumWindows = 8; |
| 112 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of |
| 115 | * this test is to verify that window rectangles are being used during clip mask generation, and to |
| 116 | * visualize where the window rectangles are placed. |
| 117 | * |
| 118 | * We use window rectangles when generating the clip mask because there is no need to invest time |
| 119 | * defining those regions where window rectangles will be in effect during the actual draw anyway. |
| 120 | * |
| 121 | * This test works by filling the entire clip mask with a small checkerboard pattern before drawing |
| 122 | * it, and then covering the mask with a solid color once it has been generated. The regions inside |
| 123 | * window rectangles or outside the scissor should still have the initial checkerboard intact. |
| 124 | */ |
| 125 | class WindowRectanglesMaskGM : public WindowRectanglesBaseGM { |
| 126 | private: |
| 127 | constexpr static int kMaskCheckerSize = 5; |
| 128 | SkString onShortName() final { return SkString("windowrectangles_mask"); } |
| 129 | void onCoverClipStack(const SkClipStack&, SkCanvas*) final; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 130 | void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&); |
| 131 | void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 132 | void stencilCheckerboard(GrRenderTargetContext*, bool flip); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 133 | void fail(SkCanvas*); |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * Base class for GrClips that visualize a clip mask. |
| 138 | */ |
| 139 | class MaskOnlyClipBase : public GrClip { |
| 140 | private: |
| 141 | bool quickContains(const SkRect&) const final { return false; } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 142 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 143 | void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final { |
| 144 | rect->set(0, 0, width, height); |
| 145 | if (iior) { |
| 146 | *iior = false; |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | /** |
| 152 | * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask. |
| 153 | */ |
| 154 | class AlphaOnlyClip final : public MaskOnlyClipBase { |
| 155 | public: |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 156 | AlphaOnlyClip(GrContext* context, sk_sp<GrTextureProxy> mask, int x, int y) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 157 | int w = mask->width(), h = mask->height(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 158 | fFP = GrDeviceSpaceTextureDecalFragmentProcessor::Make(context, std::move(mask), |
| 159 | SkIRect::MakeWH(w, h), {x, y}); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 160 | } |
| 161 | private: |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 162 | bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 163 | out->addCoverageFP(fFP); |
| 164 | return true; |
| 165 | } |
| 166 | sk_sp<GrFragmentProcessor> fFP; |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * This class clips a cover by the stencil clip bit. We use it to visualize the stencil mask. |
| 171 | */ |
| 172 | class StencilOnlyClip final : public MaskOnlyClipBase { |
| 173 | private: |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 174 | bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 175 | out->addStencilClip(); |
| 176 | return true; |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) { |
| 181 | GrContext* ctx = canvas->getGrContext(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 182 | GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext(); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 183 | |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 184 | if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 185 | this->fail(canvas); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), kNumWindows); |
| 190 | |
| 191 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 192 | if (!rtc->isStencilBufferMultisampled()) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 193 | paint.setColor4f(GrColor4f(0, 0.25f, 1, 1)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 194 | this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 195 | } else { |
| 196 | paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 197 | this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 201 | void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 202 | const GrReducedClip& reducedClip, GrPaint&& paint) { |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 203 | sk_sp<GrRenderTargetContext> maskRTC( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 204 | ctx->makeRenderTargetContextWithFallback(SkBackingFit::kExact, kLayerRect.width(), |
| 205 | kLayerRect.height(), kAlpha_8_GrPixelConfig, |
| 206 | nullptr)); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 207 | if (!maskRTC || |
| 208 | !ctx->resourceProvider()->attachStencilAttachment(maskRTC->accessRenderTarget())) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by |
| 213 | // the clip mask generation. |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 214 | this->stencilCheckerboard(maskRTC.get(), true); |
| 215 | maskRTC->clear(nullptr, GrColorPackA4(0xff), true); |
| 216 | maskRTC->priv().drawAndStencilRect(StencilOnlyClip(), &GrUserStencilSettings::kUnused, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 217 | SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(), |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 218 | SkRect::MakeIWH(maskRTC->width(), maskRTC->height())); |
| 219 | reducedClip.drawAlphaClipMask(maskRTC.get()); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 220 | |
| 221 | int x = kCoverRect.x() - kLayerRect.x(), |
| 222 | y = kCoverRect.y() - kLayerRect.y(); |
| 223 | |
| 224 | // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions |
| 225 | // inside window rectangles or outside the scissor should still have the initial checkerboard |
| 226 | // intact. (This verifies we didn't spend any time modifying those pixels in the mask.) |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 227 | AlphaOnlyClip clip(ctx, maskRTC->asTextureProxyRef(), x, y); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 228 | rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(), |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 229 | SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height()))); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 232 | void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc, |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 233 | const GrReducedClip& reducedClip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 234 | GrPaint&& paint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 235 | if (!ctx->resourceProvider()->attachStencilAttachment(rtc->accessRenderTarget())) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched |
| 240 | // by the clip mask generation. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 241 | this->stencilCheckerboard(rtc, false); |
| 242 | reducedClip.drawStencilClipMask(ctx, rtc, {kLayerRect.x(), kLayerRect.y()}); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 243 | |
| 244 | // Now visualize the stencil mask by covering the entire render target. The regions inside |
| 245 | // window rectangless or outside the scissor should still have the initial checkerboard intact. |
| 246 | // (This verifies we didn't spend any time modifying those pixels in the mask.) |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 247 | rtc->drawPaint(StencilOnlyClip(), std::move(paint), SkMatrix::I()); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 250 | void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 251 | constexpr static GrUserStencilSettings kSetClip( |
| 252 | GrUserStencilSettings::StaticInit< |
| 253 | 0, |
| 254 | GrUserStencilTest::kAlways, |
| 255 | 0, |
| 256 | GrUserStencilOp::kSetClipBit, |
| 257 | GrUserStencilOp::kKeep, |
| 258 | 0>() |
| 259 | ); |
| 260 | |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 261 | rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 262 | |
| 263 | for (int y = 0; y < kLayerRect.height(); y += kMaskCheckerSize) { |
| 264 | for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize; |
| 265 | x < kLayerRect.width(); x += 2 * kMaskCheckerSize) { |
| 266 | SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 267 | rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(), |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 268 | SkRect::Make(checker)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void WindowRectanglesMaskGM::fail(SkCanvas* canvas) { |
| 274 | SkPaint paint; |
| 275 | paint.setAntiAlias(true); |
| 276 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 277 | paint.setTextSize(20); |
| 278 | sk_tool_utils::set_portable_typeface(&paint); |
| 279 | |
| 280 | SkString errorMsg; |
| 281 | errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows); |
| 282 | |
| 283 | canvas->clipRect(SkRect::Make(kCoverRect)); |
| 284 | canvas->clear(SK_ColorWHITE); |
| 285 | canvas->drawText(errorMsg.c_str(), errorMsg.size(), SkIntToScalar(kCoverRect.centerX()), |
| 286 | SkIntToScalar(kCoverRect.centerY() - 10), paint); |
| 287 | } |
| 288 | |
| 289 | DEF_GM( return new WindowRectanglesMaskGM(); ) |
| 290 | |
| 291 | #endif |
| 292 | |
| 293 | } |