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(), |
| 46 | SkCanvas::kDifference_Op, false); |
| 47 | stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(), |
| 48 | SkCanvas::kDifference_Op, true); |
| 49 | stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45), |
| 50 | SkMatrix::I(), SkCanvas::kDifference_Op, 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); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 54 | stack.clipRRect(nine, SkMatrix::I(), SkCanvas::kDifference_Op, 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); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 59 | stack.clipRRect(complx, SkMatrix::I(), SkCanvas::kDifference_Op, 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 | |
| 77 | /** |
| 78 | * This is a simple helper class for resetting a canvas's clip to our test’s SkClipStack. |
| 79 | */ |
| 80 | class ReplayClipStackVisitor final : public SkCanvasClipVisitor { |
| 81 | public: |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 82 | typedef SkCanvas::ClipOp Op; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 83 | ReplayClipStackVisitor(SkCanvas* canvas) : fCanvas(canvas) {} |
| 84 | void clipRect(const SkRect& r, Op op, bool aa) override { fCanvas->clipRect(r, op, aa); } |
| 85 | void clipRRect(const SkRRect& rr, Op op, bool aa) override { fCanvas->clipRRect(rr, op, aa); } |
| 86 | void clipPath(const SkPath&, Op, bool) override { SkFAIL("Not implemented"); } |
| 87 | private: |
| 88 | SkCanvas* const fCanvas; |
| 89 | }; |
| 90 | |
| 91 | void WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) { |
| 92 | SkPaint paint; |
| 93 | paint.setColor(0xff00aa80); |
| 94 | |
| 95 | // Set up the canvas's clip to match our SkClipStack. |
| 96 | ReplayClipStackVisitor visitor(canvas); |
| 97 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 98 | for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) { |
| 99 | element->replay(&visitor); |
| 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 Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 130 | void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, |
| 131 | const GrPaint&); |
| 132 | void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, |
| 133 | const GrPaint&); |
| 134 | void stencilCheckerboard(GrRenderTargetContext*, bool flip); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 135 | void fail(SkCanvas*); |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * Base class for GrClips that visualize a clip mask. |
| 140 | */ |
| 141 | class MaskOnlyClipBase : public GrClip { |
| 142 | private: |
| 143 | bool quickContains(const SkRect&) const final { return false; } |
| 144 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const final { return false; } |
| 145 | void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final { |
| 146 | rect->set(0, 0, width, height); |
| 147 | if (iior) { |
| 148 | *iior = false; |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask. |
| 155 | */ |
| 156 | class AlphaOnlyClip final : public MaskOnlyClipBase { |
| 157 | public: |
| 158 | AlphaOnlyClip(GrTexture* mask, int x, int y) { |
| 159 | int w = mask->width(), h = mask->height(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 160 | fFP = GrDeviceSpaceTextureDecalFragmentProcessor::Make(mask, SkIRect::MakeWH(w, h), {x, y}); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 161 | } |
| 162 | private: |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 163 | bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 164 | out->addCoverageFP(fFP); |
| 165 | return true; |
| 166 | } |
| 167 | sk_sp<GrFragmentProcessor> fFP; |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * This class clips a cover by the stencil clip bit. We use it to visualize the stencil mask. |
| 172 | */ |
| 173 | class StencilOnlyClip final : public MaskOnlyClipBase { |
| 174 | private: |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 175 | bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 176 | out->addStencilClip(); |
| 177 | return true; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) { |
| 182 | GrContext* ctx = canvas->getGrContext(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 183 | GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext(); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 184 | |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 185 | if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 186 | this->fail(canvas); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), kNumWindows); |
| 191 | |
| 192 | GrPaint paint; |
| 193 | paint.setAntiAlias(true); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 194 | if (!rtc->isStencilBufferMultisampled()) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 195 | paint.setColor4f(GrColor4f(0, 0.25f, 1, 1)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 196 | this->visualizeAlphaMask(ctx, rtc, reducedClip, paint); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 197 | } else { |
| 198 | paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 199 | this->visualizeStencilMask(ctx, rtc, reducedClip, paint); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 203 | void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc, |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 204 | const GrReducedClip& reducedClip, |
| 205 | const GrPaint& paint) { |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 206 | sk_sp<GrRenderTargetContext> maskRTC( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 207 | ctx->makeRenderTargetContextWithFallback(SkBackingFit::kExact, kLayerRect.width(), |
| 208 | kLayerRect.height(), kAlpha_8_GrPixelConfig, |
| 209 | nullptr)); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 210 | if (!maskRTC || |
| 211 | !ctx->resourceProvider()->attachStencilAttachment(maskRTC->accessRenderTarget())) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by |
| 216 | // the clip mask generation. |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 217 | this->stencilCheckerboard(maskRTC.get(), true); |
| 218 | maskRTC->clear(nullptr, GrColorPackA4(0xff), true); |
| 219 | maskRTC->priv().drawAndStencilRect(StencilOnlyClip(), &GrUserStencilSettings::kUnused, |
| 220 | SkRegion::kDifference_Op, false, false, SkMatrix::I(), |
| 221 | SkRect::MakeIWH(maskRTC->width(), maskRTC->height())); |
| 222 | reducedClip.drawAlphaClipMask(maskRTC.get()); |
| 223 | sk_sp<GrTexture> mask(maskRTC->asTexture()); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 224 | |
| 225 | int x = kCoverRect.x() - kLayerRect.x(), |
| 226 | y = kCoverRect.y() - kLayerRect.y(); |
| 227 | |
| 228 | // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions |
| 229 | // inside window rectangles or outside the scissor should still have the initial checkerboard |
| 230 | // intact. (This verifies we didn't spend any time modifying those pixels in the mask.) |
| 231 | AlphaOnlyClip clip(mask.get(), x, y); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 232 | rtc->drawRect(clip, paint, SkMatrix::I(), |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 233 | SkRect::Make(SkIRect::MakeXYWH(x, y, mask->width(), mask->height()))); |
| 234 | } |
| 235 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 236 | void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc, |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 237 | const GrReducedClip& reducedClip, |
| 238 | const GrPaint& paint) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 239 | if (!ctx->resourceProvider()->attachStencilAttachment(rtc->accessRenderTarget())) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched |
| 244 | // by the clip mask generation. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 245 | this->stencilCheckerboard(rtc, false); |
| 246 | reducedClip.drawStencilClipMask(ctx, rtc, {kLayerRect.x(), kLayerRect.y()}); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 247 | |
| 248 | // Now visualize the stencil mask by covering the entire render target. The regions inside |
| 249 | // window rectangless or outside the scissor should still have the initial checkerboard intact. |
| 250 | // (This verifies we didn't spend any time modifying those pixels in the mask.) |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 251 | rtc->drawPaint(StencilOnlyClip(), paint, SkMatrix::I()); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 254 | void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 255 | constexpr static GrUserStencilSettings kSetClip( |
| 256 | GrUserStencilSettings::StaticInit< |
| 257 | 0, |
| 258 | GrUserStencilTest::kAlways, |
| 259 | 0, |
| 260 | GrUserStencilOp::kSetClipBit, |
| 261 | GrUserStencilOp::kKeep, |
| 262 | 0>() |
| 263 | ); |
| 264 | |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 265 | rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 266 | |
| 267 | for (int y = 0; y < kLayerRect.height(); y += kMaskCheckerSize) { |
| 268 | for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize; |
| 269 | x < kLayerRect.width(); x += 2 * kMaskCheckerSize) { |
| 270 | SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 271 | rtc->priv().stencilRect(GrNoClip(), &kSetClip, false, SkMatrix::I(), |
| 272 | SkRect::Make(checker)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void WindowRectanglesMaskGM::fail(SkCanvas* canvas) { |
| 278 | SkPaint paint; |
| 279 | paint.setAntiAlias(true); |
| 280 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 281 | paint.setTextSize(20); |
| 282 | sk_tool_utils::set_portable_typeface(&paint); |
| 283 | |
| 284 | SkString errorMsg; |
| 285 | errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows); |
| 286 | |
| 287 | canvas->clipRect(SkRect::Make(kCoverRect)); |
| 288 | canvas->clear(SK_ColorWHITE); |
| 289 | canvas->drawText(errorMsg.c_str(), errorMsg.size(), SkIntToScalar(kCoverRect.centerX()), |
| 290 | SkIntToScalar(kCoverRect.centerY() - 10), paint); |
| 291 | } |
| 292 | |
| 293 | DEF_GM( return new WindowRectanglesMaskGM(); ) |
| 294 | |
| 295 | #endif |
| 296 | |
| 297 | } |