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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 10 | #include "SkAnimTimer.h" |
| 11 | #include "SkBlurMaskFilter.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() {} |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 31 | // When it returns true, this call will have placed a device-space _circle, rect or |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 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 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 176 | SkPath asPath(SkScalar inset) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 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 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 224 | SkPath asPath(SkScalar inset) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 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 { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 316 | kBlurMask_Mode, |
| 317 | kRRectsGaussianEdge_Mode, |
| 318 | |
| 319 | kLast_Mode = kRRectsGaussianEdge_Mode |
| 320 | }; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 321 | static const int kModeCount = kLast_Mode + 1; |
| 322 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 323 | enum CoverageGeom { |
| 324 | kRect_CoverageGeom, |
| 325 | kRRect_CoverageGeom, |
| 326 | kDRRect_CoverageGeom, |
| 327 | kPath_CoverageGeom, |
| 328 | |
| 329 | kLast_CoverageGeom = kPath_CoverageGeom |
| 330 | }; |
| 331 | static const int kCoverageGeomCount = kLast_CoverageGeom + 1; |
| 332 | |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 333 | RevealGM() |
| 334 | : fFraction(0.5f) |
| 335 | , fMode(kRRectsGaussianEdge_Mode) |
| 336 | , fPause(false) |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 337 | , fBlurRadius(kInitialBlurRadius) |
| 338 | , fCoverageGeom(kRect_CoverageGeom) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 339 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 340 | } |
| 341 | |
| 342 | protected: |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 343 | bool runAsBench() const override { return true; } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 344 | |
| 345 | SkString onShortName() override { |
| 346 | return SkString("reveal"); |
| 347 | } |
| 348 | |
| 349 | SkISize onISize() override { |
| 350 | return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize); |
| 351 | } |
| 352 | |
| 353 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 354 | PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make }; |
| 355 | PFMakeMthd drawMakes[kNumRows] = { |
| 356 | RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | SkPaint strokePaint; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 360 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 361 | strokePaint.setStrokeWidth(0.0f); |
| 362 | |
| 363 | for (int y = 0; y < kNumRows; ++y) { |
| 364 | for (int x = 0; x < kNumCols; ++x) { |
| 365 | SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize), |
| 366 | SkIntToScalar(y*kCellSize), |
| 367 | SkIntToScalar(kCellSize), |
| 368 | SkIntToScalar(kCellSize)); |
| 369 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 370 | canvas->save(); |
| 371 | canvas->clipRect(cell); |
| 372 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 373 | cell.inset(kPad, kPad); |
| 374 | SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset, |
| 375 | cell.centerY() + kClipOffset); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 376 | SkScalar curSize = kCellSize * fFraction; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 377 | const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize, |
| 378 | clipCenter.fY - curSize, |
| 379 | clipCenter.fX + curSize, |
| 380 | clipCenter.fY + curSize); |
| 381 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 382 | std::unique_ptr<Object> clipObj((*clipMakes[x])(clipRect)); |
| 383 | std::unique_ptr<Object> drawObj((*drawMakes[y])(cell)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 384 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 385 | // The goal is to replace this clipped draw (which clips the |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 386 | // shadow) with a draw using the geometric clip |
Florin Malita | bb3f562 | 2017-05-31 14:20:12 +0000 | [diff] [blame] | 387 | if (kBlurMask_Mode == fMode) { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 388 | SkPath clippedPath; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 389 | |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 390 | SkScalar sigma = fBlurRadius / 4.0f; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 391 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 392 | if (clipObj->contains(drawObj->bounds())) { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 393 | clippedPath = drawObj->asPath(2.0f*sigma); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 394 | } else { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 395 | SkPath drawnPath = drawObj->asPath(2.0f*sigma); |
| 396 | SkPath clipPath = clipObj->asPath(2.0f*sigma); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 397 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 398 | SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath)); |
| 399 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 400 | |
| 401 | SkPaint blurPaint; |
| 402 | blurPaint.setAntiAlias(true); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 403 | blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 404 | canvas->drawPath(clippedPath, blurPaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 405 | } else { |
| 406 | SkASSERT(kRRectsGaussianEdge_Mode == fMode); |
| 407 | |
| 408 | SkRect cover = drawObj->bounds(); |
| 409 | SkAssertResult(cover.intersect(clipObj->bounds())); |
| 410 | |
| 411 | SkPaint paint; |
| 412 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 413 | SkRRect devSpaceClipRR, devSpaceDrawnRR; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 414 | |
Robert Phillips | d2fe3bc | 2016-10-20 11:58:40 -0400 | [diff] [blame] | 415 | if (clipObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceClipRR) && |
| 416 | drawObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceDrawnRR)) { |
| 417 | paint.setMaskFilter(SkRRectsGaussianEdgeMaskFilter::Make(devSpaceClipRR, |
| 418 | devSpaceDrawnRR, |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 419 | fBlurRadius)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 422 | strokePaint.setColor(SK_ColorBLUE); |
| 423 | |
| 424 | switch (fCoverageGeom) { |
| 425 | case kRect_CoverageGeom: |
| 426 | canvas->drawRect(cover, paint); |
| 427 | canvas->drawRect(cover, strokePaint); |
| 428 | break; |
| 429 | case kRRect_CoverageGeom: { |
| 430 | const SkRRect rrect = SkRRect::MakeRectXY( |
| 431 | cover.makeOutset(10.0f, 10.0f), |
| 432 | 10.0f, 10.0f); |
| 433 | canvas->drawRRect(rrect, paint); |
| 434 | canvas->drawRRect(rrect, strokePaint); |
| 435 | break; |
| 436 | } |
| 437 | case kDRRect_CoverageGeom: { |
| 438 | const SkRRect inner = SkRRect::MakeRectXY(cover.makeInset(10.0f, 10.0f), |
| 439 | 10.0f, 10.0f); |
| 440 | const SkRRect outer = SkRRect::MakeRectXY( |
| 441 | cover.makeOutset(10.0f, 10.0f), |
| 442 | 10.0f, 10.0f); |
| 443 | canvas->drawDRRect(outer, inner, paint); |
| 444 | canvas->drawDRRect(outer, inner, strokePaint); |
| 445 | break; |
| 446 | } |
| 447 | case kPath_CoverageGeom: { |
| 448 | SkPath path; |
| 449 | path.moveTo(cover.fLeft, cover.fTop); |
| 450 | path.lineTo(cover.centerX(), cover.centerY()); |
| 451 | path.lineTo(cover.fRight, cover.fTop); |
| 452 | path.lineTo(cover.fRight, cover.fBottom); |
| 453 | path.lineTo(cover.centerX(), cover.centerY()); |
| 454 | path.lineTo(cover.fLeft, cover.fBottom); |
| 455 | path.close(); |
| 456 | canvas->drawPath(path, paint); |
| 457 | canvas->drawPath(path, strokePaint); |
| 458 | break; |
| 459 | } |
| 460 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 461 | } |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 462 | |
| 463 | // Draw the clip and draw objects for reference |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 464 | strokePaint.setColor(SK_ColorRED); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 465 | canvas->drawPath(drawObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 466 | strokePaint.setColor(SK_ColorGREEN); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 467 | canvas->drawPath(clipObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 468 | |
| 469 | canvas->restore(); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | bool onHandleKey(SkUnichar uni) override { |
| 475 | switch (uni) { |
| 476 | case 'C': |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 477 | fMode = (Mode)((fMode + 1) % kModeCount); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 478 | return true; |
Robert Phillips | 40085e6 | 2016-10-18 12:58:29 -0400 | [diff] [blame] | 479 | case '+': |
| 480 | fBlurRadius += 1.0f; |
| 481 | return true; |
| 482 | case '-': |
| 483 | fBlurRadius = SkTMax(1.0f, fBlurRadius - 1.0f); |
| 484 | return true; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 485 | case 'p': |
| 486 | fPause = !fPause; |
| 487 | return true; |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 488 | case 'G': |
| 489 | fCoverageGeom = (CoverageGeom) ((fCoverageGeom+1) % kCoverageGeomCount); |
| 490 | return true; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 491 | } |
| 492 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
| 496 | bool onAnimate(const SkAnimTimer& timer) override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 497 | if (!fPause) { |
| 498 | fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f); |
| 499 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 500 | return true; |
| 501 | } |
| 502 | |
| 503 | private: |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 504 | SkScalar fFraction; |
| 505 | Mode fMode; |
| 506 | bool fPause; |
| 507 | float fBlurRadius; |
| 508 | CoverageGeom fCoverageGeom; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 509 | |
| 510 | typedef GM INHERITED; |
| 511 | }; |
| 512 | |
| 513 | ////////////////////////////////////////////////////////////////////////////// |
| 514 | |
| 515 | DEF_GM(return new RevealGM;) |
| 516 | } |