robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -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 "SkAnimTimer.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkGaussianEdgeShader.h" |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 12 | #include "SkRRectsGaussianEdgeMaskFilter.h" |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 13 | #include "SkPath.h" |
| 14 | #include "SkPathOps.h" |
| 15 | #include "SkRRect.h" |
| 16 | #include "SkStroke.h" |
| 17 | |
| 18 | constexpr int kNumCols = 2; |
| 19 | constexpr int kNumRows = 5; |
| 20 | constexpr int kCellSize = 128; |
| 21 | constexpr SkScalar kPad = 8.0f; |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 22 | constexpr SkScalar kInitialBlurRadius = 8.0f; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 23 | constexpr SkScalar kPeriod = 8.0f; |
| 24 | constexpr int kClipOffset = 32; |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 27 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 28 | class Object { |
| 29 | public: |
| 30 | virtual ~Object() {} |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 31 | // When it returns true, this call will have placed a device-space _circle, rect or |
| 32 | // simple circular_ RRect in "rr" |
| 33 | virtual bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const = 0; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 34 | virtual SkPath asPath(SkScalar inset) const = 0; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 35 | virtual void draw(SkCanvas* canvas, const SkPaint& paint) const = 0; |
| 36 | virtual void clip(SkCanvas* canvas) const = 0; |
| 37 | virtual bool contains(const SkRect& r) const = 0; |
| 38 | virtual const SkRect& bounds() const = 0; |
| 39 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 40 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 41 | typedef Object* (*PFMakeMthd)(const SkRect& r); |
| 42 | |
| 43 | class RRect : public Object { |
| 44 | public: |
| 45 | RRect(const SkRect& r) { |
| 46 | fRRect = SkRRect::MakeRectXY(r, 4*kPad, 4*kPad); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 47 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 48 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 49 | bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override { |
| 50 | if (!ctm.isSimilarity()) { // the corners have to remain circular |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | SkScalar scales[2]; |
| 55 | if (!ctm.getMinMaxScales(scales)) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | SkASSERT(SkScalarNearlyEqual(scales[0], scales[1])); |
| 60 | |
| 61 | SkRect devRect; |
| 62 | ctm.mapRect(&devRect, fRRect.rect()); |
| 63 | |
| 64 | SkScalar scaledRad = scales[0] * fRRect.getSimpleRadii().fX; |
| 65 | |
| 66 | *rr = SkRRect::MakeRectXY(devRect, scaledRad, scaledRad); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 69 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 70 | SkPath asPath(SkScalar inset) const override { |
| 71 | SkRRect tmp = fRRect; |
| 72 | tmp.inset(inset, inset); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 73 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 74 | p.addRRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 75 | return p; |
| 76 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 77 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 78 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 79 | canvas->drawRRect(fRRect, paint); |
| 80 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 81 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 82 | void clip(SkCanvas* canvas) const override { |
| 83 | canvas->clipRRect(fRRect); |
| 84 | } |
| 85 | |
| 86 | bool contains(const SkRect& r) const override { |
| 87 | return fRRect.contains(r); |
| 88 | } |
| 89 | |
| 90 | const SkRect& bounds() const override { |
| 91 | return fRRect.getBounds(); |
| 92 | } |
| 93 | |
| 94 | static Object* Make(const SkRect& r) { |
| 95 | return new RRect(r); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | SkRRect fRRect; |
| 100 | }; |
| 101 | |
| 102 | class StrokedRRect : public Object { |
| 103 | public: |
| 104 | StrokedRRect(const SkRect& r) { |
| 105 | fRRect = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad); |
| 106 | fStrokedBounds = r.makeOutset(kPad, kPad); |
| 107 | } |
| 108 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 109 | bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 113 | SkPath asPath(SkScalar inset) const override { |
| 114 | SkRRect tmp = fRRect; |
| 115 | tmp.inset(inset, inset); |
| 116 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 117 | // In this case we want the outline of the stroked rrect |
| 118 | SkPaint paint; |
| 119 | paint.setAntiAlias(true); |
| 120 | paint.setStyle(SkPaint::kStroke_Style); |
| 121 | paint.setStrokeWidth(kPad); |
| 122 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 123 | SkPath p, stroked; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 124 | p.addRRect(tmp); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 125 | SkStroke stroke(paint); |
| 126 | stroke.strokePath(p, &stroked); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 127 | return stroked; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 128 | } |
| 129 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 130 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 131 | SkPaint stroke(paint); |
| 132 | stroke.setStyle(SkPaint::kStroke_Style); |
| 133 | stroke.setStrokeWidth(kPad); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 134 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 135 | canvas->drawRRect(fRRect, stroke); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 136 | } |
| 137 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 138 | void clip(SkCanvas* canvas) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 139 | canvas->clipPath(this->asPath(0.0f)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 140 | } |
| 141 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 142 | bool contains(const SkRect& r) const override { |
| 143 | return false; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 144 | } |
| 145 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 146 | const SkRect& bounds() const override { |
| 147 | return fStrokedBounds; |
| 148 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 149 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 150 | static Object* Make(const SkRect& r) { |
| 151 | return new StrokedRRect(r); |
| 152 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 153 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 154 | private: |
| 155 | SkRRect fRRect; |
| 156 | SkRect fStrokedBounds; |
| 157 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 158 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 159 | class Oval : public Object { |
| 160 | public: |
| 161 | Oval(const SkRect& r) { |
| 162 | fRRect = SkRRect::MakeOval(r); |
| 163 | } |
| 164 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 165 | bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override { |
| 166 | if (!ctm.isSimilarity()) { // circles have to remain circles |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | SkRect devRect; |
| 171 | ctm.mapRect(&devRect, fRRect.rect()); |
| 172 | *rr = SkRRect::MakeOval(devRect); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 176 | SkPath asPath(SkScalar inset) const override { |
| 177 | SkRRect tmp = fRRect; |
| 178 | tmp.inset(inset, inset); |
| 179 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 180 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 181 | p.addRRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 182 | return p; |
| 183 | } |
| 184 | |
| 185 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 186 | canvas->drawRRect(fRRect, paint); |
| 187 | } |
| 188 | |
| 189 | void clip(SkCanvas* canvas) const override { |
| 190 | canvas->clipRRect(fRRect); |
| 191 | } |
| 192 | |
| 193 | bool contains(const SkRect& r) const override { |
| 194 | return fRRect.contains(r); |
| 195 | } |
| 196 | |
| 197 | const SkRect& bounds() const override { |
| 198 | return fRRect.getBounds(); |
| 199 | } |
| 200 | |
| 201 | static Object* Make(const SkRect& r) { |
| 202 | return new Oval(r); |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | SkRRect fRRect; |
| 207 | }; |
| 208 | |
| 209 | class Rect : public Object { |
| 210 | public: |
| 211 | Rect(const SkRect& r) : fRect(r) { } |
| 212 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 213 | bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override { |
| 214 | if (!ctm.rectStaysRect()) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | SkRect devRect; |
| 219 | ctm.mapRect(&devRect, fRect); |
| 220 | *rr = SkRRect::MakeRect(devRect); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 224 | SkPath asPath(SkScalar inset) const override { |
| 225 | SkRect tmp = fRect; |
| 226 | tmp.inset(inset, inset); |
| 227 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 228 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 229 | p.addRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 230 | return p; |
| 231 | } |
| 232 | |
| 233 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 234 | canvas->drawRect(fRect, paint); |
| 235 | } |
| 236 | |
| 237 | void clip(SkCanvas* canvas) const override { |
| 238 | canvas->clipRect(fRect); |
| 239 | } |
| 240 | |
| 241 | bool contains(const SkRect& r) const override { |
| 242 | return fRect.contains(r); |
| 243 | } |
| 244 | |
| 245 | const SkRect& bounds() const override { |
| 246 | return fRect; |
| 247 | } |
| 248 | |
| 249 | static Object* Make(const SkRect& r) { |
| 250 | return new Rect(r); |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | SkRect fRect; |
| 255 | }; |
| 256 | |
| 257 | class Pentagon : public Object { |
| 258 | public: |
| 259 | Pentagon(const SkRect& r) { |
| 260 | SkPoint points[5] = { |
| 261 | { 0.000000f, -1.000000f }, |
| 262 | { -0.951056f, -0.309017f }, |
| 263 | { -0.587785f, 0.809017f }, |
| 264 | { 0.587785f, 0.809017f }, |
| 265 | { 0.951057f, -0.309017f }, |
| 266 | }; |
| 267 | |
| 268 | SkScalar height = r.height()/2.0f; |
| 269 | SkScalar width = r.width()/2.0f; |
| 270 | |
| 271 | fPath.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height); |
| 272 | fPath.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height); |
| 273 | fPath.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height); |
| 274 | fPath.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height); |
| 275 | fPath.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height); |
| 276 | fPath.close(); |
| 277 | } |
| 278 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 279 | bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 283 | SkPath asPath(SkScalar inset) const override { return fPath; } |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 284 | |
| 285 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 286 | canvas->drawPath(fPath, paint); |
| 287 | } |
| 288 | |
| 289 | void clip(SkCanvas* canvas) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 290 | canvas->clipPath(this->asPath(0.0f)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool contains(const SkRect& r) const override { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | const SkRect& bounds() const override { |
| 298 | return fPath.getBounds(); |
| 299 | } |
| 300 | |
| 301 | static Object* Make(const SkRect& r) { |
| 302 | return new Pentagon(r); |
| 303 | } |
| 304 | |
| 305 | private: |
| 306 | SkPath fPath; |
| 307 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 308 | |
| 309 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 310 | namespace skiagm { |
| 311 | |
| 312 | // This GM attempts to mimic Android's reveal animation |
| 313 | class RevealGM : public GM { |
| 314 | public: |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 315 | enum Mode { |
| 316 | kGaussianEdge_Mode, |
| 317 | kBlurMask_Mode, |
| 318 | kRRectsGaussianEdge_Mode, |
| 319 | |
| 320 | kLast_Mode = kRRectsGaussianEdge_Mode |
| 321 | }; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 322 | static const int kModeCount = kLast_Mode + 1; |
| 323 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 324 | enum CoverageGeom { |
| 325 | kRect_CoverageGeom, |
| 326 | kRRect_CoverageGeom, |
| 327 | kDRRect_CoverageGeom, |
| 328 | kPath_CoverageGeom, |
| 329 | |
| 330 | kLast_CoverageGeom = kPath_CoverageGeom |
| 331 | }; |
| 332 | static const int kCoverageGeomCount = kLast_CoverageGeom + 1; |
| 333 | |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 334 | RevealGM() |
| 335 | : fFraction(0.5f) |
| 336 | , fMode(kRRectsGaussianEdge_Mode) |
| 337 | , fPause(false) |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 338 | , fBlurRadius(kInitialBlurRadius) |
| 339 | , fCoverageGeom(kRect_CoverageGeom) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 340 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 341 | } |
| 342 | |
| 343 | protected: |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 344 | bool runAsBench() const override { return true; } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 345 | |
| 346 | SkString onShortName() override { |
| 347 | return SkString("reveal"); |
| 348 | } |
| 349 | |
| 350 | SkISize onISize() override { |
| 351 | return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize); |
| 352 | } |
| 353 | |
| 354 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 355 | PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make }; |
| 356 | PFMakeMthd drawMakes[kNumRows] = { |
| 357 | RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | SkPaint strokePaint; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 361 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 362 | strokePaint.setStrokeWidth(0.0f); |
| 363 | |
| 364 | for (int y = 0; y < kNumRows; ++y) { |
| 365 | for (int x = 0; x < kNumCols; ++x) { |
| 366 | SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize), |
| 367 | SkIntToScalar(y*kCellSize), |
| 368 | SkIntToScalar(kCellSize), |
| 369 | SkIntToScalar(kCellSize)); |
| 370 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 371 | canvas->save(); |
| 372 | canvas->clipRect(cell); |
| 373 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 374 | cell.inset(kPad, kPad); |
| 375 | SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset, |
| 376 | cell.centerY() + kClipOffset); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 377 | SkScalar curSize = kCellSize * fFraction; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 378 | const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize, |
| 379 | clipCenter.fY - curSize, |
| 380 | clipCenter.fX + curSize, |
| 381 | clipCenter.fY + curSize); |
| 382 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 383 | std::unique_ptr<Object> clipObj((*clipMakes[x])(clipRect)); |
| 384 | std::unique_ptr<Object> drawObj((*drawMakes[y])(cell)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 385 | |
| 386 | // The goal is to replace this clipped draw (which clips the |
| 387 | // shadow) with a draw using the geometric clip |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 388 | if (kGaussianEdge_Mode == fMode) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 389 | canvas->save(); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 390 | clipObj->clip(canvas); |
| 391 | |
| 392 | // Draw with GaussianEdgeShader |
| 393 | SkPaint paint; |
| 394 | paint.setAntiAlias(true); |
| 395 | // G channel is an F6.2 radius |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 396 | int iBlurRad = (int)(4.0f * fBlurRadius); |
| 397 | paint.setColor(SkColorSetARGB(255, iBlurRad >> 8, iBlurRad & 0xFF, 0)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 398 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 399 | drawObj->draw(canvas, paint); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 400 | canvas->restore(); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 401 | } else if (kBlurMask_Mode == fMode) { |
| 402 | SkPath clippedPath; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 403 | |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 404 | SkScalar sigma = fBlurRadius / 4.0f; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 405 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 406 | if (clipObj->contains(drawObj->bounds())) { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 407 | clippedPath = drawObj->asPath(2.0f*sigma); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 408 | } else { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 409 | SkPath drawnPath = drawObj->asPath(2.0f*sigma); |
| 410 | SkPath clipPath = clipObj->asPath(2.0f*sigma); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 411 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 412 | SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath)); |
| 413 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 414 | |
| 415 | SkPaint blurPaint; |
| 416 | blurPaint.setAntiAlias(true); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 417 | blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 418 | canvas->drawPath(clippedPath, blurPaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 419 | } else { |
| 420 | SkASSERT(kRRectsGaussianEdge_Mode == fMode); |
| 421 | |
| 422 | SkRect cover = drawObj->bounds(); |
| 423 | SkAssertResult(cover.intersect(clipObj->bounds())); |
| 424 | |
| 425 | SkPaint paint; |
| 426 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 427 | SkRRect devSpaceClipRR, devSpaceDrawnRR; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 428 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 429 | if (clipObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceClipRR) && |
| 430 | drawObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceDrawnRR)) { |
| 431 | paint.setMaskFilter(SkRRectsGaussianEdgeMaskFilter::Make(devSpaceClipRR, |
| 432 | devSpaceDrawnRR, |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 433 | fBlurRadius)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 436 | strokePaint.setColor(SK_ColorBLUE); |
| 437 | |
| 438 | switch (fCoverageGeom) { |
| 439 | case kRect_CoverageGeom: |
| 440 | canvas->drawRect(cover, paint); |
| 441 | canvas->drawRect(cover, strokePaint); |
| 442 | break; |
| 443 | case kRRect_CoverageGeom: { |
| 444 | const SkRRect rrect = SkRRect::MakeRectXY( |
| 445 | cover.makeOutset(10.0f, 10.0f), |
| 446 | 10.0f, 10.0f); |
| 447 | canvas->drawRRect(rrect, paint); |
| 448 | canvas->drawRRect(rrect, strokePaint); |
| 449 | break; |
| 450 | } |
| 451 | case kDRRect_CoverageGeom: { |
| 452 | const SkRRect inner = SkRRect::MakeRectXY(cover.makeInset(10.0f, 10.0f), |
| 453 | 10.0f, 10.0f); |
| 454 | const SkRRect outer = SkRRect::MakeRectXY( |
| 455 | cover.makeOutset(10.0f, 10.0f), |
| 456 | 10.0f, 10.0f); |
| 457 | canvas->drawDRRect(outer, inner, paint); |
| 458 | canvas->drawDRRect(outer, inner, strokePaint); |
| 459 | break; |
| 460 | } |
| 461 | case kPath_CoverageGeom: { |
| 462 | SkPath path; |
| 463 | path.moveTo(cover.fLeft, cover.fTop); |
| 464 | path.lineTo(cover.centerX(), cover.centerY()); |
| 465 | path.lineTo(cover.fRight, cover.fTop); |
| 466 | path.lineTo(cover.fRight, cover.fBottom); |
| 467 | path.lineTo(cover.centerX(), cover.centerY()); |
| 468 | path.lineTo(cover.fLeft, cover.fBottom); |
| 469 | path.close(); |
| 470 | canvas->drawPath(path, paint); |
| 471 | canvas->drawPath(path, strokePaint); |
| 472 | break; |
| 473 | } |
| 474 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 475 | } |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 476 | |
| 477 | // Draw the clip and draw objects for reference |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 478 | strokePaint.setColor(SK_ColorRED); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 479 | canvas->drawPath(drawObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 480 | strokePaint.setColor(SK_ColorGREEN); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 481 | canvas->drawPath(clipObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 482 | |
| 483 | canvas->restore(); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | bool onHandleKey(SkUnichar uni) override { |
| 489 | switch (uni) { |
| 490 | case 'C': |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 491 | fMode = (Mode)((fMode + 1) % kModeCount); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 492 | return true; |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 493 | case '+': |
| 494 | fBlurRadius += 1.0f; |
| 495 | return true; |
| 496 | case '-': |
| 497 | fBlurRadius = SkTMax(1.0f, fBlurRadius - 1.0f); |
| 498 | return true; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 499 | case 'p': |
| 500 | fPause = !fPause; |
| 501 | return true; |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 502 | case 'G': |
| 503 | fCoverageGeom = (CoverageGeom) ((fCoverageGeom+1) % kCoverageGeomCount); |
| 504 | return true; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | bool onAnimate(const SkAnimTimer& timer) override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 511 | if (!fPause) { |
| 512 | fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f); |
| 513 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 514 | return true; |
| 515 | } |
| 516 | |
| 517 | private: |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 518 | SkScalar fFraction; |
| 519 | Mode fMode; |
| 520 | bool fPause; |
| 521 | float fBlurRadius; |
| 522 | CoverageGeom fCoverageGeom; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 523 | |
| 524 | typedef GM INHERITED; |
| 525 | }; |
| 526 | |
| 527 | ////////////////////////////////////////////////////////////////////////////// |
| 528 | |
| 529 | DEF_GM(return new RevealGM;) |
| 530 | } |