2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "SkAnimTimer.h"
11 #include "SkBlurMaskFilter.h"
12 #include "SkGaussianEdgeShader.h"
13 #include "SkRRectsGaussianEdgeMaskFilter.h"
15 #include "SkPathOps.h"
19 constexpr int kNumCols = 2;
20 constexpr int kNumRows = 5;
21 constexpr int kCellSize = 128;
22 constexpr SkScalar kPad = 8.0f;
23 constexpr SkScalar kInitialBlurRadius = 8.0f;
24 constexpr SkScalar kPeriod = 8.0f;
25 constexpr int kClipOffset = 32;
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
32 // When it returns true, this call will have placed a device-space _circle, rect or
33 // simple circular_ RRect in "rr"
34 virtual bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const = 0;
35 virtual SkPath asPath(SkScalar inset) const = 0;
36 virtual void draw(SkCanvas* canvas, const SkPaint& paint) const = 0;
37 virtual void clip(SkCanvas* canvas) const = 0;
38 virtual bool contains(const SkRect& r) const = 0;
39 virtual const SkRect& bounds() const = 0;
42 typedef Object* (*PFMakeMthd)(const SkRect& r);
44 class RRect : public Object {
46 RRect(const SkRect& r) {
47 fRRect = SkRRect::MakeRectXY(r, 4*kPad, 4*kPad);
50 bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
51 if (!ctm.isSimilarity()) { // the corners have to remain circular
56 if (!ctm.getMinMaxScales(scales)) {
60 SkASSERT(SkScalarNearlyEqual(scales[0], scales[1]));
63 ctm.mapRect(&devRect, fRRect.rect());
65 SkScalar scaledRad = scales[0] * fRRect.getSimpleRadii().fX;
67 *rr = SkRRect::MakeRectXY(devRect, scaledRad, scaledRad);
71 SkPath asPath(SkScalar inset) const override {
73 tmp.inset(inset, inset);
79 void draw(SkCanvas* canvas, const SkPaint& paint) const override {
80 canvas->drawRRect(fRRect, paint);
83 void clip(SkCanvas* canvas) const override {
84 canvas->clipRRect(fRRect);
87 bool contains(const SkRect& r) const override {
88 return fRRect.contains(r);
91 const SkRect& bounds() const override {
92 return fRRect.getBounds();
95 static Object* Make(const SkRect& r) {
103 class StrokedRRect : public Object {
105 StrokedRRect(const SkRect& r) {
106 fRRect = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad);
107 fStrokedBounds = r.makeOutset(kPad, kPad);
110 bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
114 SkPath asPath(SkScalar inset) const override {
115 SkRRect tmp = fRRect;
116 tmp.inset(inset, inset);
118 // In this case we want the outline of the stroked rrect
120 paint.setAntiAlias(true);
121 paint.setStyle(SkPaint::kStroke_Style);
122 paint.setStrokeWidth(kPad);
126 SkStroke stroke(paint);
127 stroke.strokePath(p, &stroked);
131 void draw(SkCanvas* canvas, const SkPaint& paint) const override {
132 SkPaint stroke(paint);
133 stroke.setStyle(SkPaint::kStroke_Style);
134 stroke.setStrokeWidth(kPad);
136 canvas->drawRRect(fRRect, stroke);
139 void clip(SkCanvas* canvas) const override {
140 canvas->clipPath(this->asPath(0.0f));
143 bool contains(const SkRect& r) const override {
147 const SkRect& bounds() const override {
148 return fStrokedBounds;
151 static Object* Make(const SkRect& r) {
152 return new StrokedRRect(r);
157 SkRect fStrokedBounds;
160 class Oval : public Object {
162 Oval(const SkRect& r) {
163 fRRect = SkRRect::MakeOval(r);
166 bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
167 if (!ctm.isSimilarity()) { // circles have to remain circles
172 ctm.mapRect(&devRect, fRRect.rect());
173 *rr = SkRRect::MakeOval(devRect);
177 SkPath asPath(SkScalar inset) const override {
178 SkRRect tmp = fRRect;
179 tmp.inset(inset, inset);
186 void draw(SkCanvas* canvas, const SkPaint& paint) const override {
187 canvas->drawRRect(fRRect, paint);
190 void clip(SkCanvas* canvas) const override {
191 canvas->clipRRect(fRRect);
194 bool contains(const SkRect& r) const override {
195 return fRRect.contains(r);
198 const SkRect& bounds() const override {
199 return fRRect.getBounds();
202 static Object* Make(const SkRect& r) {
210 class Rect : public Object {
212 Rect(const SkRect& r) : fRect(r) { }
214 bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
215 if (!ctm.rectStaysRect()) {
220 ctm.mapRect(&devRect, fRect);
221 *rr = SkRRect::MakeRect(devRect);
225 SkPath asPath(SkScalar inset) const override {
227 tmp.inset(inset, inset);
234 void draw(SkCanvas* canvas, const SkPaint& paint) const override {
235 canvas->drawRect(fRect, paint);
238 void clip(SkCanvas* canvas) const override {
239 canvas->clipRect(fRect);
242 bool contains(const SkRect& r) const override {
243 return fRect.contains(r);
246 const SkRect& bounds() const override {
250 static Object* Make(const SkRect& r) {
258 class Pentagon : public Object {
260 Pentagon(const SkRect& r) {
261 SkPoint points[5] = {
262 { 0.000000f, -1.000000f },
263 { -0.951056f, -0.309017f },
264 { -0.587785f, 0.809017f },
265 { 0.587785f, 0.809017f },
266 { 0.951057f, -0.309017f },
269 SkScalar height = r.height()/2.0f;
270 SkScalar width = r.width()/2.0f;
272 fPath.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height);
273 fPath.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height);
274 fPath.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height);
275 fPath.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height);
276 fPath.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height);
280 bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
284 SkPath asPath(SkScalar inset) const override { return fPath; }
286 void draw(SkCanvas* canvas, const SkPaint& paint) const override {
287 canvas->drawPath(fPath, paint);
290 void clip(SkCanvas* canvas) const override {
291 canvas->clipPath(this->asPath(0.0f));
294 bool contains(const SkRect& r) const override {
298 const SkRect& bounds() const override {
299 return fPath.getBounds();
302 static Object* Make(const SkRect& r) {
303 return new Pentagon(r);
310 ///////////////////////////////////////////////////////////////////////////////////////////////////
313 // This GM attempts to mimic Android's reveal animation
314 class RevealGM : public GM {
319 kRRectsGaussianEdge_Mode,
321 kLast_Mode = kRRectsGaussianEdge_Mode
323 static const int kModeCount = kLast_Mode + 1;
328 kDRRect_CoverageGeom,
331 kLast_CoverageGeom = kPath_CoverageGeom
333 static const int kCoverageGeomCount = kLast_CoverageGeom + 1;
337 , fMode(kRRectsGaussianEdge_Mode)
339 , fBlurRadius(kInitialBlurRadius)
340 , fCoverageGeom(kRect_CoverageGeom) {
341 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
345 bool runAsBench() const override { return true; }
347 SkString onShortName() override {
348 return SkString("reveal");
351 SkISize onISize() override {
352 return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize);
355 void onDraw(SkCanvas* canvas) override {
356 PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make };
357 PFMakeMthd drawMakes[kNumRows] = {
358 RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make
362 strokePaint.setStyle(SkPaint::kStroke_Style);
363 strokePaint.setStrokeWidth(0.0f);
365 for (int y = 0; y < kNumRows; ++y) {
366 for (int x = 0; x < kNumCols; ++x) {
367 SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize),
368 SkIntToScalar(y*kCellSize),
369 SkIntToScalar(kCellSize),
370 SkIntToScalar(kCellSize));
373 canvas->clipRect(cell);
375 cell.inset(kPad, kPad);
376 SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset,
377 cell.centerY() + kClipOffset);
378 SkScalar curSize = kCellSize * fFraction;
379 const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize,
380 clipCenter.fY - curSize,
381 clipCenter.fX + curSize,
382 clipCenter.fY + curSize);
384 std::unique_ptr<Object> clipObj((*clipMakes[x])(clipRect));
385 std::unique_ptr<Object> drawObj((*drawMakes[y])(cell));
387 // The goal is to replace this clipped draw (which clips the
388 // shadow) with a draw using the geometric clip
389 if (kGaussianEdge_Mode == fMode) {
391 clipObj->clip(canvas);
393 // Draw with GaussianEdgeShader
395 paint.setAntiAlias(true);
396 // G channel is an F6.2 radius
397 int iBlurRad = (int)(4.0f * fBlurRadius);
398 paint.setColor(SkColorSetARGB(255, iBlurRad >> 8, iBlurRad & 0xFF, 0));
399 paint.setShader(SkGaussianEdgeShader::Make());
400 drawObj->draw(canvas, paint);
402 } else if (kBlurMask_Mode == fMode) {
405 SkScalar sigma = fBlurRadius / 4.0f;
407 if (clipObj->contains(drawObj->bounds())) {
408 clippedPath = drawObj->asPath(2.0f*sigma);
410 SkPath drawnPath = drawObj->asPath(2.0f*sigma);
411 SkPath clipPath = clipObj->asPath(2.0f*sigma);
413 SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath));
417 blurPaint.setAntiAlias(true);
418 blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma));
419 canvas->drawPath(clippedPath, blurPaint);
421 SkASSERT(kRRectsGaussianEdge_Mode == fMode);
423 SkRect cover = drawObj->bounds();
424 SkAssertResult(cover.intersect(clipObj->bounds()));
428 SkRRect devSpaceClipRR, devSpaceDrawnRR;
430 if (clipObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceClipRR) &&
431 drawObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceDrawnRR)) {
432 paint.setMaskFilter(SkRRectsGaussianEdgeMaskFilter::Make(devSpaceClipRR,
437 strokePaint.setColor(SK_ColorBLUE);
439 switch (fCoverageGeom) {
440 case kRect_CoverageGeom:
441 canvas->drawRect(cover, paint);
442 canvas->drawRect(cover, strokePaint);
444 case kRRect_CoverageGeom: {
445 const SkRRect rrect = SkRRect::MakeRectXY(
446 cover.makeOutset(10.0f, 10.0f),
448 canvas->drawRRect(rrect, paint);
449 canvas->drawRRect(rrect, strokePaint);
452 case kDRRect_CoverageGeom: {
453 const SkRRect inner = SkRRect::MakeRectXY(cover.makeInset(10.0f, 10.0f),
455 const SkRRect outer = SkRRect::MakeRectXY(
456 cover.makeOutset(10.0f, 10.0f),
458 canvas->drawDRRect(outer, inner, paint);
459 canvas->drawDRRect(outer, inner, strokePaint);
462 case kPath_CoverageGeom: {
464 path.moveTo(cover.fLeft, cover.fTop);
465 path.lineTo(cover.centerX(), cover.centerY());
466 path.lineTo(cover.fRight, cover.fTop);
467 path.lineTo(cover.fRight, cover.fBottom);
468 path.lineTo(cover.centerX(), cover.centerY());
469 path.lineTo(cover.fLeft, cover.fBottom);
471 canvas->drawPath(path, paint);
472 canvas->drawPath(path, strokePaint);
478 // Draw the clip and draw objects for reference
479 strokePaint.setColor(SK_ColorRED);
480 canvas->drawPath(drawObj->asPath(0.0f), strokePaint);
481 strokePaint.setColor(SK_ColorGREEN);
482 canvas->drawPath(clipObj->asPath(0.0f), strokePaint);
489 bool onHandleKey(SkUnichar uni) override {
492 fMode = (Mode)((fMode + 1) % kModeCount);
498 fBlurRadius = SkTMax(1.0f, fBlurRadius - 1.0f);
504 fCoverageGeom = (CoverageGeom) ((fCoverageGeom+1) % kCoverageGeomCount);
511 bool onAnimate(const SkAnimTimer& timer) override {
513 fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f);
523 CoverageGeom fCoverageGeom;
525 typedef GM INHERITED;
528 //////////////////////////////////////////////////////////////////////////////
530 DEF_GM(return new RevealGM;)