robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 12 | #include "SkLayerDrawLooper.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 13 | #include "SkMaskFilter.h" |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 14 | |
| 15 | // This GM tests 3 different ways of drawing four shadows around a square: |
| 16 | // just using 4 blurred rects |
| 17 | // using 4 1-level draw loopers |
| 18 | // using 1 4-level draw looper |
| 19 | // They all produce exactly the same pixels |
| 20 | class MegaLooperGM : public skiagm::GM { |
| 21 | public: |
| 22 | // The types define "<# of loopers> x <# of stages per looper>" |
| 23 | enum Type { |
| 24 | k0x0_Type, // draw without loopers at all |
| 25 | k4x1_Type, // a looper for each shadow |
| 26 | k1x4_Type, // all four shadows in one looper |
| 27 | }; |
| 28 | |
| 29 | MegaLooperGM(Type type) : fType(type) {} |
| 30 | |
| 31 | protected: |
| 32 | virtual SkString onShortName() { |
| 33 | switch (fType) { |
| 34 | case k0x0_Type: |
| 35 | return SkString("megalooper_0x0"); |
| 36 | break; |
| 37 | case k4x1_Type: |
| 38 | return SkString("megalooper_4x1"); |
| 39 | break; |
| 40 | case k1x4_Type: // fall through |
| 41 | default: |
| 42 | return SkString("megalooper_1x4"); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | virtual SkISize onISize() { |
| 48 | return SkISize::Make(kWidth, kHeight); |
| 49 | } |
| 50 | |
| 51 | virtual void onDraw(SkCanvas* canvas) { |
| 52 | for (int y = 100; y < kHeight; y += 200) { |
| 53 | for (int x = 100; x < kWidth; x += 200) { |
| 54 | switch (fType) { |
| 55 | case k0x0_Type: |
| 56 | draw0x0(canvas, SkIntToScalar(x), SkIntToScalar(y)); |
| 57 | break; |
| 58 | case k4x1_Type: |
| 59 | draw4x1(canvas, SkIntToScalar(x), SkIntToScalar(y)); |
| 60 | break; |
| 61 | case k1x4_Type: // fall through |
| 62 | default: |
| 63 | draw1x4(canvas, SkIntToScalar(x), SkIntToScalar(y)); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 71 | static constexpr int kWidth = 800; |
| 72 | static constexpr int kHeight = 800; |
| 73 | static constexpr int kHalfOuterClipSize = 100; |
| 74 | static constexpr int kHalfSquareSize = 50; |
| 75 | static constexpr int kOffsetToOutsideClip = kHalfSquareSize + kHalfOuterClipSize + 1; |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 76 | |
| 77 | static const SkPoint gBlurOffsets[4]; |
| 78 | static const SkColor gColors[4]; |
| 79 | Type fType; |
| 80 | |
| 81 | // Just draw a blurred rect at each of the four corners of a square (centered at x,y). |
| 82 | // Use two clips to define a rectori where we want pixels to appear. |
| 83 | void draw0x0(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 84 | SkRect innerClip = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize }; |
| 85 | innerClip.offset(x, y); |
| 86 | |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 87 | SkRect outerClip = { |
| 88 | -kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize, |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 89 | kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize |
| 90 | }; |
| 91 | outerClip.offset(x, y); |
| 92 | |
| 93 | canvas->save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 94 | canvas->clipRect(outerClip, kIntersect_SkClipOp); |
| 95 | canvas->clipRect(innerClip, kDifference_SkClipOp); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 96 | |
| 97 | SkPaint paint; |
| 98 | paint.setAntiAlias(true); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 99 | paint.setMaskFilter(MakeBlur()); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 100 | |
| 101 | for (int i = 0; i < 4; ++i) { |
| 102 | paint.setColor(gColors[i]); |
| 103 | |
| 104 | SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize }; |
| 105 | rect.offset(gBlurOffsets[i]); |
| 106 | rect.offset(x, y); |
| 107 | canvas->drawRect(rect, paint); |
| 108 | } |
| 109 | |
| 110 | canvas->restore(); |
| 111 | } |
| 112 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 113 | static sk_sp<SkMaskFilter> MakeBlur() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 114 | const SkScalar kBlurSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(25)); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 115 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 116 | return SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kBlurSigma); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // This draws 4 blurred shadows around a single square (centered at x, y). |
| 120 | // Each blur is offset +/- half the square's side in x & y from the original |
| 121 | // (so each blurred rect is centered at one of the corners of the original). |
| 122 | // For each blur a large outer clip is centered around the blurred rect |
| 123 | // while a difference clip stays at the location of the original rect. |
| 124 | // Each blurred rect is drawn with a draw looper where the original (non- |
| 125 | // blurred rect) is offset to reside outside of the large outer clip (so |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 126 | // it never appears) but the offset in the draw looper is used to translate |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 127 | // the blurred version back into the clip. |
| 128 | void draw4x1(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 129 | |
| 130 | for (int i = 0; i < 4; ++i) { |
| 131 | SkPaint loopPaint; |
| 132 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 133 | loopPaint.setLooper(create1Looper(-kOffsetToOutsideClip, 0, gColors[i])); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 134 | loopPaint.setAntiAlias(true); |
| 135 | |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 136 | SkRect outerClip = { |
| 137 | -kHalfOuterClipSize, -kHalfOuterClipSize, |
| 138 | kHalfOuterClipSize, kHalfOuterClipSize |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 139 | }; |
| 140 | outerClip.offset(x, y); |
| 141 | // center it on the blurred rect |
| 142 | outerClip.offset(gBlurOffsets[i]); |
| 143 | |
| 144 | SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize }; |
| 145 | rect.offset(x, y); |
| 146 | |
| 147 | canvas->save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 148 | canvas->clipRect(outerClip, kIntersect_SkClipOp); |
| 149 | canvas->clipRect(rect, kDifference_SkClipOp); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 150 | |
| 151 | // move the rect to where we want the blur to appear |
| 152 | rect.offset(gBlurOffsets[i]); |
| 153 | // then move it outside the clip (the blur stage of the draw |
| 154 | // looper will undo this translation) |
| 155 | rect.offset(SkIntToScalar(kOffsetToOutsideClip), 0); |
| 156 | |
| 157 | canvas->drawRect(rect, loopPaint); |
| 158 | canvas->restore(); |
| 159 | } |
| 160 | } |
| 161 | |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 162 | // Create a 1-tier drawlooper |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 163 | sk_sp<SkDrawLooper> create1Looper(SkScalar xOff, SkScalar yOff, SkColor color) { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 164 | SkLayerDrawLooper::Builder looperBuilder; |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 165 | SkLayerDrawLooper::LayerInfo info; |
| 166 | |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 167 | info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit | |
| 168 | SkLayerDrawLooper::kMaskFilter_Bit; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 169 | info.fColorMode = SkBlendMode::kSrc; |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 170 | info.fOffset.set(xOff, yOff); |
| 171 | info.fPostTranslate = false; |
| 172 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 173 | SkPaint* paint = looperBuilder.addLayer(info); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 174 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 175 | paint->setMaskFilter(MakeBlur()); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 176 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 177 | paint->setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kSrcIn)); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 178 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 179 | return looperBuilder.detach(); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void draw1x4(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 183 | SkRect rect = { -kHalfSquareSize, -kHalfSquareSize, kHalfSquareSize, kHalfSquareSize }; |
| 184 | rect.offset(x, y); |
| 185 | |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 186 | SkRect outerClip = { |
| 187 | -kHalfOuterClipSize-kHalfSquareSize, -kHalfOuterClipSize-kHalfSquareSize, |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 188 | kHalfOuterClipSize+kHalfSquareSize, kHalfOuterClipSize+kHalfSquareSize |
| 189 | }; |
| 190 | outerClip.offset(x, y); |
| 191 | |
| 192 | SkPaint paint; |
| 193 | paint.setAntiAlias(true); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 194 | paint.setLooper(create4Looper(-kOffsetToOutsideClip-kHalfSquareSize, 0)); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 195 | |
| 196 | canvas->save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 197 | canvas->clipRect(outerClip, kIntersect_SkClipOp); |
| 198 | canvas->clipRect(rect, kDifference_SkClipOp); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 199 | |
| 200 | rect.offset(SkIntToScalar(kOffsetToOutsideClip+kHalfSquareSize), 0); |
| 201 | canvas->drawRect(rect, paint); |
| 202 | canvas->restore(); |
| 203 | } |
| 204 | |
| 205 | // Create a 4-tier draw looper |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 206 | sk_sp<SkDrawLooper> create4Looper(SkScalar xOff, SkScalar yOff) { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 207 | SkLayerDrawLooper::Builder looperBuilder; |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 208 | SkLayerDrawLooper::LayerInfo info; |
| 209 | |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 210 | info.fPaintBits = SkLayerDrawLooper::kColorFilter_Bit | |
| 211 | SkLayerDrawLooper::kMaskFilter_Bit; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 212 | info.fColorMode = SkBlendMode::kSrc; |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 213 | info.fPostTranslate = false; |
| 214 | |
| 215 | SkPaint* paint; |
| 216 | |
| 217 | for (int i = 3; i >= 0; --i) { |
| 218 | info.fOffset.set(xOff+gBlurOffsets[i].fX, yOff+gBlurOffsets[i].fY); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 219 | paint = looperBuilder.addLayer(info); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 220 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 221 | paint->setMaskFilter(MakeBlur()); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 222 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 223 | paint->setColorFilter(SkColorFilter::MakeModeFilter(gColors[i], SkBlendMode::kSrcIn)); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 226 | return looperBuilder.detach(); |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | typedef GM INHERITED; |
| 230 | }; |
| 231 | |
| 232 | const SkPoint MegaLooperGM::gBlurOffsets[4] = { |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 233 | { kHalfSquareSize, kHalfSquareSize }, |
| 234 | { -kHalfSquareSize, kHalfSquareSize }, |
| 235 | { kHalfSquareSize, -kHalfSquareSize }, |
robertphillips@google.com | 4157085 | 2013-06-03 17:13:25 +0000 | [diff] [blame] | 236 | { -kHalfSquareSize, -kHalfSquareSize } |
| 237 | }; |
| 238 | |
| 239 | const SkColor MegaLooperGM::gColors[4] = { |
| 240 | SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLUE, SK_ColorRED |
| 241 | }; |
| 242 | |
| 243 | DEF_GM( return new MegaLooperGM(MegaLooperGM::k0x0_Type); ) |
| 244 | DEF_GM( return new MegaLooperGM(MegaLooperGM::k4x1_Type); ) |
| 245 | DEF_GM( return new MegaLooperGM(MegaLooperGM::k1x4_Type); ) |