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" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkPathOps.h" |
| 14 | #include "SkRRect.h" |
| 15 | #include "SkStroke.h" |
| 16 | |
| 17 | constexpr int kNumCols = 2; |
| 18 | constexpr int kNumRows = 5; |
| 19 | constexpr int kCellSize = 128; |
| 20 | constexpr SkScalar kPad = 8.0f; |
| 21 | constexpr SkScalar kPeriod = 8.0f; |
| 22 | constexpr int kClipOffset = 32; |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 25 | typedef SkPath (*PFDrawMthd)(SkCanvas*, const SkRect&, bool); |
| 26 | |
| 27 | static SkPath draw_rrect(SkCanvas* canvas, const SkRect& r, bool stroked) { |
| 28 | SkRRect rr = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad); |
| 29 | |
| 30 | SkPaint paint; |
| 31 | paint.setAntiAlias(true); |
| 32 | if (stroked) { |
| 33 | paint.setStyle(SkPaint::kStroke_Style); |
| 34 | paint.setColor(SK_ColorRED); |
| 35 | } else { |
| 36 | // G channel is an F6.2 radius |
| 37 | paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0)); |
| 38 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 39 | } |
| 40 | canvas->drawRRect(rr, paint); |
| 41 | |
| 42 | SkPath p; |
| 43 | p.addRoundRect(r, 2*kPad, 2*kPad); |
| 44 | return p; |
| 45 | } |
| 46 | |
| 47 | static SkPath draw_stroked_rrect(SkCanvas* canvas, const SkRect& r, bool stroked) { |
| 48 | SkRect insetRect = r; |
| 49 | insetRect.inset(kPad, kPad); |
| 50 | SkRRect rr = SkRRect::MakeRectXY(insetRect, 2*kPad, 2*kPad); |
| 51 | |
| 52 | SkPaint paint; |
| 53 | paint.setAntiAlias(true); |
| 54 | paint.setStyle(SkPaint::kStroke_Style); |
| 55 | paint.setStrokeWidth(kPad); |
| 56 | |
| 57 | if (stroked) { |
| 58 | // In this case we want to draw a stroked representation of the stroked rrect |
| 59 | SkPath p, stroked; |
| 60 | p.addRRect(rr); |
| 61 | SkStroke stroke(paint); |
| 62 | stroke.strokePath(p, &stroked); |
| 63 | |
| 64 | paint.setStrokeWidth(0); |
| 65 | paint.setColor(SK_ColorRED); |
| 66 | canvas->drawPath(stroked, paint); |
| 67 | } else { |
| 68 | // G channel is an F6.2 radius |
| 69 | paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0)); |
| 70 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 71 | |
| 72 | canvas->drawRRect(rr, paint); |
| 73 | } |
| 74 | |
| 75 | SkPath p; |
| 76 | insetRect.outset(kPad/2.0f, kPad/2.0f); |
| 77 | p.addRoundRect(insetRect, 2*kPad, 2*kPad); |
| 78 | return p; |
| 79 | } |
| 80 | |
| 81 | static SkPath draw_oval(SkCanvas* canvas, const SkRect& r, bool stroked) { |
| 82 | SkRRect rr = SkRRect::MakeOval(r); |
| 83 | |
| 84 | SkPaint paint; |
| 85 | paint.setAntiAlias(true); |
| 86 | if (stroked) { |
| 87 | paint.setStyle(SkPaint::kStroke_Style); |
| 88 | paint.setColor(SK_ColorRED); |
| 89 | } else { |
| 90 | // G channel is an F6.2 radius |
| 91 | paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0)); |
| 92 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 93 | } |
| 94 | canvas->drawRRect(rr, paint); |
| 95 | |
| 96 | SkPath p; |
| 97 | p.addOval(r); |
| 98 | return p; |
| 99 | } |
| 100 | |
| 101 | static SkPath draw_square(SkCanvas* canvas, const SkRect& r, bool stroked) { |
| 102 | SkPaint paint; |
| 103 | paint.setAntiAlias(true); |
| 104 | if (stroked) { |
| 105 | paint.setStyle(SkPaint::kStroke_Style); |
| 106 | paint.setColor(SK_ColorRED); |
| 107 | } else { |
| 108 | // G channel is an F6.2 radius |
| 109 | paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0)); |
| 110 | paint.setShader(SkGaussianEdgeShader::Make()); |
| 111 | } |
| 112 | canvas->drawRect(r, paint); |
| 113 | |
| 114 | SkPath p; |
| 115 | p.addRect(r); |
| 116 | return p; |
| 117 | } |
| 118 | |
| 119 | static SkPath draw_pentagon(SkCanvas* canvas, const SkRect& r, bool stroked) { |
| 120 | SkPath p; |
| 121 | |
| 122 | SkPoint points[5] = { |
| 123 | { 0.000000f, -1.000000f }, |
| 124 | { -0.951056f, -0.309017f }, |
| 125 | { -0.587785f, 0.809017f }, |
| 126 | { 0.587785f, 0.809017f }, |
| 127 | { 0.951057f, -0.309017f }, |
| 128 | }; |
| 129 | |
| 130 | SkScalar height = r.height()/2.0f; |
| 131 | SkScalar width = r.width()/2.0f; |
| 132 | |
| 133 | p.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height); |
| 134 | p.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height); |
| 135 | p.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height); |
| 136 | p.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height); |
| 137 | p.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height); |
| 138 | p.close(); |
| 139 | |
| 140 | SkPaint paint; |
| 141 | paint.setAntiAlias(true); |
| 142 | if (stroked) { |
| 143 | paint.setStyle(SkPaint::kStroke_Style); |
| 144 | paint.setColor(SK_ColorRED); |
| 145 | } else { |
| 146 | // G channel is an F6.2 radius |
| 147 | paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0)); |
| 148 | // This currently goes through the GrAAConvexPathRenderer and produces a |
| 149 | // AAConvexPathBatch (i.e., it doesn't have a analytic distance) |
| 150 | // paint.setShader(SkGaussianEdgeShader::Make()); |
| 151 | } |
| 152 | canvas->drawPath(p, paint); |
| 153 | |
| 154 | return p; |
| 155 | } |
| 156 | |
| 157 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 158 | typedef void (*PFClipMthd)(SkCanvas* canvas, const SkPoint&, SkScalar); |
| 159 | |
| 160 | static void circle_clip(SkCanvas* canvas, const SkPoint& center, SkScalar rad) { |
| 161 | SkRect r = SkRect::MakeLTRB(center.fX - rad, center.fY - rad, center.fX + rad, center.fY + rad); |
| 162 | SkRRect rr = SkRRect::MakeOval(r); |
| 163 | |
| 164 | canvas->clipRRect(rr); |
| 165 | } |
| 166 | |
| 167 | static void square_clip(SkCanvas* canvas, const SkPoint& center, SkScalar size) { |
| 168 | SkScalar newSize = SK_ScalarRoot2Over2 * size; |
| 169 | SkRect r = SkRect::MakeLTRB(center.fX - newSize, center.fY - newSize, |
| 170 | center.fX + newSize, center.fY + newSize); |
| 171 | |
| 172 | canvas->clipRect(r); |
| 173 | } |
| 174 | |
| 175 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 176 | // These are stand alone methods (rather than just, say, returning the SkPath for the clip |
| 177 | // object) so that we can catch the clip-contains-victim case. |
| 178 | typedef SkPath (*PFGeometricClipMthd)(const SkPoint&, SkScalar, const SkPath&); |
| 179 | |
| 180 | static SkPath circle_geometric_clip(const SkPoint& center, SkScalar rad, const SkPath& victim) { |
| 181 | const SkRect bound = victim.getBounds(); |
| 182 | SkPoint pts[4]; |
| 183 | bound.toQuad(pts); |
| 184 | |
| 185 | bool clipContainsVictim = true; |
| 186 | for (int i = 0; i < 4; ++i) { |
| 187 | SkScalar distSq = (pts[i].fX - center.fX) * (pts[i].fX - center.fX) + |
| 188 | (pts[i].fY - center.fY) * (pts[i].fY - center.fY); |
| 189 | if (distSq >= rad*rad) { |
| 190 | clipContainsVictim = false; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (clipContainsVictim) { |
| 195 | return victim; |
| 196 | } |
| 197 | |
| 198 | // Add victim contains clip test? |
| 199 | |
| 200 | SkPath clipPath; |
| 201 | clipPath.addCircle(center.fX, center.fY, rad); |
| 202 | |
| 203 | SkPath result; |
| 204 | SkAssertResult(Op(clipPath, victim, kIntersect_SkPathOp, &result)); |
| 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | static SkPath square_geometric_clip(const SkPoint& center, SkScalar size, const SkPath& victim) { |
| 210 | SkScalar newSize = SK_ScalarRoot2Over2 * size; |
| 211 | SkRect r = SkRect::MakeLTRB(center.fX - newSize, center.fY - newSize, |
| 212 | center.fX + newSize, center.fY + newSize); |
| 213 | |
| 214 | const SkRect bound = victim.getBounds(); |
| 215 | |
| 216 | if (r.contains(bound)) { |
| 217 | return victim; |
| 218 | } |
| 219 | |
| 220 | // Add victim contains clip test? |
| 221 | |
| 222 | SkPath clipPath; |
| 223 | clipPath.addRect(r); |
| 224 | |
| 225 | SkPath result; |
| 226 | SkAssertResult(Op(clipPath, victim, kIntersect_SkPathOp, &result)); |
| 227 | |
| 228 | return result; |
| 229 | } |
| 230 | |
| 231 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 232 | namespace skiagm { |
| 233 | |
| 234 | // This GM attempts to mimic Android's reveal animation |
| 235 | class RevealGM : public GM { |
| 236 | public: |
| 237 | RevealGM() : fFraction(0.5f), fDrawWithGaussianEdge(true) { |
| 238 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 239 | } |
| 240 | |
| 241 | protected: |
| 242 | |
| 243 | SkString onShortName() override { |
| 244 | return SkString("reveal"); |
| 245 | } |
| 246 | |
| 247 | SkISize onISize() override { |
| 248 | return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize); |
| 249 | } |
| 250 | |
| 251 | void onDraw(SkCanvas* canvas) override { |
| 252 | PFClipMthd clips[kNumCols] = { circle_clip, square_clip }; |
| 253 | PFGeometricClipMthd geometricClips[kNumCols] = { |
| 254 | circle_geometric_clip, |
| 255 | square_geometric_clip |
| 256 | }; |
| 257 | PFDrawMthd draws[kNumRows] = { |
| 258 | draw_rrect, |
| 259 | draw_stroked_rrect, |
| 260 | draw_oval, |
| 261 | draw_square, |
| 262 | draw_pentagon |
| 263 | }; |
| 264 | |
| 265 | SkPaint strokePaint; |
| 266 | strokePaint.setColor(SK_ColorGREEN); |
| 267 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 268 | strokePaint.setStrokeWidth(0.0f); |
| 269 | |
| 270 | for (int y = 0; y < kNumRows; ++y) { |
| 271 | for (int x = 0; x < kNumCols; ++x) { |
| 272 | SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize), |
| 273 | SkIntToScalar(y*kCellSize), |
| 274 | SkIntToScalar(kCellSize), |
| 275 | SkIntToScalar(kCellSize)); |
| 276 | |
| 277 | cell.inset(kPad, kPad); |
| 278 | SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset, |
| 279 | cell.centerY() + kClipOffset); |
| 280 | |
| 281 | SkScalar curSize = kCellSize * fFraction; |
| 282 | |
| 283 | // The goal is to replace this clipped draw (which clips the |
| 284 | // shadow) with a draw using the geometric clip |
| 285 | if (fDrawWithGaussianEdge) { |
| 286 | canvas->save(); |
| 287 | (*clips[x])(canvas, clipCenter, curSize); |
| 288 | (*draws[y])(canvas, cell, false); |
| 289 | canvas->restore(); |
| 290 | } |
| 291 | |
| 292 | SkPath drawnPath = (*draws[y])(canvas, cell, true); |
| 293 | |
| 294 | if (!fDrawWithGaussianEdge) { |
| 295 | SkPath clippedPath = (*geometricClips[x])(clipCenter, curSize, drawnPath); |
| 296 | SkASSERT(clippedPath.isConvex()); |
| 297 | |
| 298 | SkPaint blurPaint; |
| 299 | blurPaint.setAntiAlias(true); |
| 300 | blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 3.0f)); |
| 301 | canvas->drawPath(clippedPath, blurPaint); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | bool onHandleKey(SkUnichar uni) override { |
| 308 | switch (uni) { |
| 309 | case 'C': |
| 310 | fDrawWithGaussianEdge = !fDrawWithGaussianEdge; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | bool onAnimate(const SkAnimTimer& timer) override { |
| 318 | fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f); |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | private: |
| 323 | SkScalar fFraction; |
| 324 | bool fDrawWithGaussianEdge; |
| 325 | |
| 326 | typedef GM INHERITED; |
| 327 | }; |
| 328 | |
| 329 | ////////////////////////////////////////////////////////////////////////////// |
| 330 | |
| 331 | DEF_GM(return new RevealGM;) |
| 332 | } |