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" |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 12 | #include "SkRRectsGaussianEdgeShader.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; |
robertphillips | ec89534 | 2016-09-15 13:18:15 -0700 | [diff] [blame] | 22 | constexpr SkScalar kBlurRadius = 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() {} |
| 31 | virtual bool asRRect(SkRRect* rr) const = 0; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 32 | virtual SkPath asPath(SkScalar inset) const = 0; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 33 | virtual void draw(SkCanvas* canvas, const SkPaint& paint) const = 0; |
| 34 | virtual void clip(SkCanvas* canvas) const = 0; |
| 35 | virtual bool contains(const SkRect& r) const = 0; |
| 36 | virtual const SkRect& bounds() const = 0; |
| 37 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 38 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 39 | typedef Object* (*PFMakeMthd)(const SkRect& r); |
| 40 | |
| 41 | class RRect : public Object { |
| 42 | public: |
| 43 | RRect(const SkRect& r) { |
| 44 | fRRect = SkRRect::MakeRectXY(r, 4*kPad, 4*kPad); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 45 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 46 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 47 | bool asRRect(SkRRect* rr) const override { |
| 48 | *rr = fRRect; |
| 49 | return true; |
| 50 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 51 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 52 | SkPath asPath(SkScalar inset) const override { |
| 53 | SkRRect tmp = fRRect; |
| 54 | tmp.inset(inset, inset); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 55 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 56 | p.addRRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 57 | return p; |
| 58 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 59 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 60 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 61 | canvas->drawRRect(fRRect, paint); |
| 62 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 63 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 64 | void clip(SkCanvas* canvas) const override { |
| 65 | canvas->clipRRect(fRRect); |
| 66 | } |
| 67 | |
| 68 | bool contains(const SkRect& r) const override { |
| 69 | return fRRect.contains(r); |
| 70 | } |
| 71 | |
| 72 | const SkRect& bounds() const override { |
| 73 | return fRRect.getBounds(); |
| 74 | } |
| 75 | |
| 76 | static Object* Make(const SkRect& r) { |
| 77 | return new RRect(r); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | SkRRect fRRect; |
| 82 | }; |
| 83 | |
| 84 | class StrokedRRect : public Object { |
| 85 | public: |
| 86 | StrokedRRect(const SkRect& r) { |
| 87 | fRRect = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad); |
| 88 | fStrokedBounds = r.makeOutset(kPad, kPad); |
| 89 | } |
| 90 | |
| 91 | bool asRRect(SkRRect* rr) const override { |
| 92 | return false; |
| 93 | } |
| 94 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 95 | SkPath asPath(SkScalar inset) const override { |
| 96 | SkRRect tmp = fRRect; |
| 97 | tmp.inset(inset, inset); |
| 98 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 99 | // In this case we want the outline of the stroked rrect |
| 100 | SkPaint paint; |
| 101 | paint.setAntiAlias(true); |
| 102 | paint.setStyle(SkPaint::kStroke_Style); |
| 103 | paint.setStrokeWidth(kPad); |
| 104 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 105 | SkPath p, stroked; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 106 | p.addRRect(tmp); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 107 | SkStroke stroke(paint); |
| 108 | stroke.strokePath(p, &stroked); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 109 | return stroked; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 110 | } |
| 111 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 112 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 113 | SkPaint stroke(paint); |
| 114 | stroke.setStyle(SkPaint::kStroke_Style); |
| 115 | stroke.setStrokeWidth(kPad); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 116 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 117 | canvas->drawRRect(fRRect, stroke); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 120 | void clip(SkCanvas* canvas) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 121 | canvas->clipPath(this->asPath(0.0f)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 122 | } |
| 123 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 124 | bool contains(const SkRect& r) const override { |
| 125 | return false; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 128 | const SkRect& bounds() const override { |
| 129 | return fStrokedBounds; |
| 130 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 131 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 132 | static Object* Make(const SkRect& r) { |
| 133 | return new StrokedRRect(r); |
| 134 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 135 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 136 | private: |
| 137 | SkRRect fRRect; |
| 138 | SkRect fStrokedBounds; |
| 139 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 140 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 141 | class Oval : public Object { |
| 142 | public: |
| 143 | Oval(const SkRect& r) { |
| 144 | fRRect = SkRRect::MakeOval(r); |
| 145 | } |
| 146 | |
| 147 | bool asRRect(SkRRect* rr) const override { |
| 148 | *rr = fRRect; |
| 149 | return true; |
| 150 | } |
| 151 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 152 | SkPath asPath(SkScalar inset) const override { |
| 153 | SkRRect tmp = fRRect; |
| 154 | tmp.inset(inset, inset); |
| 155 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 156 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 157 | p.addRRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 158 | return p; |
| 159 | } |
| 160 | |
| 161 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 162 | canvas->drawRRect(fRRect, paint); |
| 163 | } |
| 164 | |
| 165 | void clip(SkCanvas* canvas) const override { |
| 166 | canvas->clipRRect(fRRect); |
| 167 | } |
| 168 | |
| 169 | bool contains(const SkRect& r) const override { |
| 170 | return fRRect.contains(r); |
| 171 | } |
| 172 | |
| 173 | const SkRect& bounds() const override { |
| 174 | return fRRect.getBounds(); |
| 175 | } |
| 176 | |
| 177 | static Object* Make(const SkRect& r) { |
| 178 | return new Oval(r); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | SkRRect fRRect; |
| 183 | }; |
| 184 | |
| 185 | class Rect : public Object { |
| 186 | public: |
| 187 | Rect(const SkRect& r) : fRect(r) { } |
| 188 | |
| 189 | bool asRRect(SkRRect* rr) const override { |
| 190 | *rr = SkRRect::MakeRect(fRect); |
| 191 | return true; |
| 192 | } |
| 193 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 194 | SkPath asPath(SkScalar inset) const override { |
| 195 | SkRect tmp = fRect; |
| 196 | tmp.inset(inset, inset); |
| 197 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 198 | SkPath p; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 199 | p.addRect(tmp); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 200 | return p; |
| 201 | } |
| 202 | |
| 203 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 204 | canvas->drawRect(fRect, paint); |
| 205 | } |
| 206 | |
| 207 | void clip(SkCanvas* canvas) const override { |
| 208 | canvas->clipRect(fRect); |
| 209 | } |
| 210 | |
| 211 | bool contains(const SkRect& r) const override { |
| 212 | return fRect.contains(r); |
| 213 | } |
| 214 | |
| 215 | const SkRect& bounds() const override { |
| 216 | return fRect; |
| 217 | } |
| 218 | |
| 219 | static Object* Make(const SkRect& r) { |
| 220 | return new Rect(r); |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | SkRect fRect; |
| 225 | }; |
| 226 | |
| 227 | class Pentagon : public Object { |
| 228 | public: |
| 229 | Pentagon(const SkRect& r) { |
| 230 | SkPoint points[5] = { |
| 231 | { 0.000000f, -1.000000f }, |
| 232 | { -0.951056f, -0.309017f }, |
| 233 | { -0.587785f, 0.809017f }, |
| 234 | { 0.587785f, 0.809017f }, |
| 235 | { 0.951057f, -0.309017f }, |
| 236 | }; |
| 237 | |
| 238 | SkScalar height = r.height()/2.0f; |
| 239 | SkScalar width = r.width()/2.0f; |
| 240 | |
| 241 | fPath.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height); |
| 242 | fPath.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height); |
| 243 | fPath.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height); |
| 244 | fPath.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height); |
| 245 | fPath.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height); |
| 246 | fPath.close(); |
| 247 | } |
| 248 | |
| 249 | bool asRRect(SkRRect* rr) const override { |
| 250 | return false; |
| 251 | } |
| 252 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 253 | SkPath asPath(SkScalar inset) const override { return fPath; } |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 254 | |
| 255 | void draw(SkCanvas* canvas, const SkPaint& paint) const override { |
| 256 | canvas->drawPath(fPath, paint); |
| 257 | } |
| 258 | |
| 259 | void clip(SkCanvas* canvas) const override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 260 | canvas->clipPath(this->asPath(0.0f)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | bool contains(const SkRect& r) const override { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | const SkRect& bounds() const override { |
| 268 | return fPath.getBounds(); |
| 269 | } |
| 270 | |
| 271 | static Object* Make(const SkRect& r) { |
| 272 | return new Pentagon(r); |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | SkPath fPath; |
| 277 | }; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 280 | namespace skiagm { |
| 281 | |
| 282 | // This GM attempts to mimic Android's reveal animation |
| 283 | class RevealGM : public GM { |
| 284 | public: |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 285 | enum Mode { |
| 286 | kGaussianEdge_Mode, |
| 287 | kBlurMask_Mode, |
| 288 | kRRectsGaussianEdge_Mode, |
| 289 | |
| 290 | kLast_Mode = kRRectsGaussianEdge_Mode |
| 291 | }; |
| 292 | |
| 293 | static const int kModeCount = kLast_Mode + 1; |
| 294 | |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 295 | RevealGM() : fFraction(0.5f), fMode(kRRectsGaussianEdge_Mode), fPause(false) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 296 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 297 | } |
| 298 | |
| 299 | protected: |
| 300 | |
| 301 | SkString onShortName() override { |
| 302 | return SkString("reveal"); |
| 303 | } |
| 304 | |
| 305 | SkISize onISize() override { |
| 306 | return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize); |
| 307 | } |
| 308 | |
| 309 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 310 | PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make }; |
| 311 | PFMakeMthd drawMakes[kNumRows] = { |
| 312 | RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | SkPaint strokePaint; |
| 316 | strokePaint.setColor(SK_ColorGREEN); |
| 317 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 318 | strokePaint.setStrokeWidth(0.0f); |
| 319 | |
| 320 | for (int y = 0; y < kNumRows; ++y) { |
| 321 | for (int x = 0; x < kNumCols; ++x) { |
| 322 | SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize), |
| 323 | SkIntToScalar(y*kCellSize), |
| 324 | SkIntToScalar(kCellSize), |
| 325 | SkIntToScalar(kCellSize)); |
| 326 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 327 | canvas->save(); |
| 328 | canvas->clipRect(cell); |
| 329 | |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 330 | cell.inset(kPad, kPad); |
| 331 | SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset, |
| 332 | cell.centerY() + kClipOffset); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 333 | SkScalar curSize = kCellSize * fFraction; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 334 | const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize, |
| 335 | clipCenter.fY - curSize, |
| 336 | clipCenter.fX + curSize, |
| 337 | clipCenter.fY + curSize); |
| 338 | |
| 339 | SkAutoTDelete<Object> clipObj((*clipMakes[x])(clipRect)); |
| 340 | SkAutoTDelete<Object> drawObj((*drawMakes[y])(cell)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 341 | |
| 342 | // The goal is to replace this clipped draw (which clips the |
| 343 | // shadow) with a draw using the geometric clip |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 344 | if (kGaussianEdge_Mode == fMode) { |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 345 | canvas->save(); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 346 | clipObj->clip(canvas); |
| 347 | |
| 348 | // Draw with GaussianEdgeShader |
| 349 | SkPaint paint; |
| 350 | paint.setAntiAlias(true); |
| 351 | // G channel is an F6.2 radius |
robertphillips | ec89534 | 2016-09-15 13:18:15 -0700 | [diff] [blame] | 352 | paint.setColor(SkColorSetARGB(255, 0, (unsigned char)(4*kBlurRadius), 0)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 353 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 354 | drawObj->draw(canvas, paint); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 355 | canvas->restore(); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 356 | } else if (kBlurMask_Mode == fMode) { |
| 357 | SkPath clippedPath; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 358 | |
robertphillips | ec89534 | 2016-09-15 13:18:15 -0700 | [diff] [blame] | 359 | SkScalar sigma = kBlurRadius / 4.0f; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 360 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 361 | if (clipObj->contains(drawObj->bounds())) { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 362 | clippedPath = drawObj->asPath(2.0f*sigma); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 363 | } else { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 364 | SkPath drawnPath = drawObj->asPath(2.0f*sigma); |
| 365 | SkPath clipPath = clipObj->asPath(2.0f*sigma); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 366 | |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 367 | SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath)); |
| 368 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 369 | |
| 370 | SkPaint blurPaint; |
| 371 | blurPaint.setAntiAlias(true); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 372 | blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma)); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 373 | canvas->drawPath(clippedPath, blurPaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 374 | } else { |
| 375 | SkASSERT(kRRectsGaussianEdge_Mode == fMode); |
| 376 | |
| 377 | SkRect cover = drawObj->bounds(); |
| 378 | SkAssertResult(cover.intersect(clipObj->bounds())); |
| 379 | |
| 380 | SkPaint paint; |
| 381 | |
| 382 | SkRRect clipRR, drawnRR; |
| 383 | |
| 384 | if (clipObj->asRRect(&clipRR) && drawObj->asRRect(&drawnRR)) { |
| 385 | paint.setShader(SkRRectsGaussianEdgeShader::Make(clipRR, drawnRR, |
robertphillips | ec89534 | 2016-09-15 13:18:15 -0700 | [diff] [blame] | 386 | kBlurRadius)); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | canvas->drawRect(cover, paint); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 390 | } |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 391 | |
| 392 | // Draw the clip and draw objects for reference |
| 393 | SkPaint strokePaint; |
| 394 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 395 | strokePaint.setStrokeWidth(0); |
| 396 | strokePaint.setColor(SK_ColorRED); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 397 | canvas->drawPath(drawObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 398 | strokePaint.setColor(SK_ColorGREEN); |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 399 | canvas->drawPath(clipObj->asPath(0.0f), strokePaint); |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 400 | |
| 401 | canvas->restore(); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | bool onHandleKey(SkUnichar uni) override { |
| 407 | switch (uni) { |
| 408 | case 'C': |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 409 | fMode = (Mode)((fMode + 1) % kModeCount); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 410 | return true; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 411 | case 'p': |
| 412 | fPause = !fPause; |
| 413 | return true; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool onAnimate(const SkAnimTimer& timer) override { |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 420 | if (!fPause) { |
| 421 | fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f); |
| 422 | } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | |
| 426 | private: |
| 427 | SkScalar fFraction; |
robertphillips | 2af83ac | 2016-09-12 12:02:16 -0700 | [diff] [blame] | 428 | Mode fMode; |
robertphillips | f619a7a | 2016-09-12 17:25:50 -0700 | [diff] [blame] | 429 | bool fPause; |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 430 | |
| 431 | typedef GM INHERITED; |
| 432 | }; |
| 433 | |
| 434 | ////////////////////////////////////////////////////////////////////////////// |
| 435 | |
| 436 | DEF_GM(return new RevealGM;) |
| 437 | } |