2016-09-08 16:02:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2016-09-08 16:02:43 +00:00
|
|
|
#include "SkAnimTimer.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkGaussianEdgeShader.h"
|
2016-10-20 13:40:55 +00:00
|
|
|
#include "SkRRectsGaussianEdgeMaskFilter.h"
|
2016-09-08 16:02:43 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPathOps.h"
|
|
|
|
#include "SkRRect.h"
|
|
|
|
#include "SkStroke.h"
|
|
|
|
|
|
|
|
constexpr int kNumCols = 2;
|
|
|
|
constexpr int kNumRows = 5;
|
|
|
|
constexpr int kCellSize = 128;
|
|
|
|
constexpr SkScalar kPad = 8.0f;
|
2016-10-18 16:58:29 +00:00
|
|
|
constexpr SkScalar kInitialBlurRadius = 8.0f;
|
2016-09-08 16:02:43 +00:00
|
|
|
constexpr SkScalar kPeriod = 8.0f;
|
|
|
|
constexpr int kClipOffset = 32;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
class Object {
|
|
|
|
public:
|
|
|
|
virtual ~Object() {}
|
2016-10-20 15:58:40 +00:00
|
|
|
// When it returns true, this call will have placed a device-space _circle, rect or
|
|
|
|
// simple circular_ RRect in "rr"
|
|
|
|
virtual bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const = 0;
|
2016-09-13 00:25:50 +00:00
|
|
|
virtual SkPath asPath(SkScalar inset) const = 0;
|
2016-09-12 19:02:16 +00:00
|
|
|
virtual void draw(SkCanvas* canvas, const SkPaint& paint) const = 0;
|
|
|
|
virtual void clip(SkCanvas* canvas) const = 0;
|
|
|
|
virtual bool contains(const SkRect& r) const = 0;
|
|
|
|
virtual const SkRect& bounds() const = 0;
|
|
|
|
};
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
typedef Object* (*PFMakeMthd)(const SkRect& r);
|
|
|
|
|
|
|
|
class RRect : public Object {
|
|
|
|
public:
|
|
|
|
RRect(const SkRect& r) {
|
|
|
|
fRRect = SkRRect::MakeRectXY(r, 4*kPad, 4*kPad);
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
|
|
|
|
if (!ctm.isSimilarity()) { // the corners have to remain circular
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar scales[2];
|
|
|
|
if (!ctm.getMinMaxScales(scales)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(SkScalarNearlyEqual(scales[0], scales[1]));
|
|
|
|
|
|
|
|
SkRect devRect;
|
|
|
|
ctm.mapRect(&devRect, fRRect.rect());
|
|
|
|
|
|
|
|
SkScalar scaledRad = scales[0] * fRRect.getSimpleRadii().fX;
|
|
|
|
|
|
|
|
*rr = SkRRect::MakeRectXY(devRect, scaledRad, scaledRad);
|
2016-09-12 19:02:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath asPath(SkScalar inset) const override {
|
|
|
|
SkRRect tmp = fRRect;
|
|
|
|
tmp.inset(inset, inset);
|
2016-09-12 19:02:16 +00:00
|
|
|
SkPath p;
|
2016-09-13 00:25:50 +00:00
|
|
|
p.addRRect(tmp);
|
2016-09-12 19:02:16 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
|
|
|
|
canvas->drawRRect(fRRect, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clip(SkCanvas* canvas) const override {
|
|
|
|
canvas->clipRRect(fRRect);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
bool contains(const SkRect& r) const override {
|
|
|
|
return fRRect.contains(r);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
const SkRect& bounds() const override {
|
|
|
|
return fRRect.getBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object* Make(const SkRect& r) {
|
|
|
|
return new RRect(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkRRect fRRect;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StrokedRRect : public Object {
|
|
|
|
public:
|
|
|
|
StrokedRRect(const SkRect& r) {
|
|
|
|
fRRect = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad);
|
|
|
|
fStrokedBounds = r.makeOutset(kPad, kPad);
|
|
|
|
}
|
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
|
2016-09-12 19:02:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath asPath(SkScalar inset) const override {
|
|
|
|
SkRRect tmp = fRRect;
|
|
|
|
tmp.inset(inset, inset);
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
// In this case we want the outline of the stroked rrect
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(kPad);
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
SkPath p, stroked;
|
2016-09-13 00:25:50 +00:00
|
|
|
p.addRRect(tmp);
|
2016-09-08 16:02:43 +00:00
|
|
|
SkStroke stroke(paint);
|
|
|
|
stroke.strokePath(p, &stroked);
|
2016-09-12 19:02:16 +00:00
|
|
|
return stroked;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
|
|
|
|
SkPaint stroke(paint);
|
|
|
|
stroke.setStyle(SkPaint::kStroke_Style);
|
|
|
|
stroke.setStrokeWidth(kPad);
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
canvas->drawRRect(fRRect, stroke);
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void clip(SkCanvas* canvas) const override {
|
2016-09-13 00:25:50 +00:00
|
|
|
canvas->clipPath(this->asPath(0.0f));
|
2016-09-12 19:02:16 +00:00
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
bool contains(const SkRect& r) const override {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
const SkRect& bounds() const override {
|
|
|
|
return fStrokedBounds;
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
static Object* Make(const SkRect& r) {
|
|
|
|
return new StrokedRRect(r);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
private:
|
|
|
|
SkRRect fRRect;
|
|
|
|
SkRect fStrokedBounds;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Oval : public Object {
|
|
|
|
public:
|
|
|
|
Oval(const SkRect& r) {
|
|
|
|
fRRect = SkRRect::MakeOval(r);
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
|
|
|
|
if (!ctm.isSimilarity()) { // circles have to remain circles
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect devRect;
|
|
|
|
ctm.mapRect(&devRect, fRRect.rect());
|
|
|
|
*rr = SkRRect::MakeOval(devRect);
|
2016-09-12 19:02:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath asPath(SkScalar inset) const override {
|
|
|
|
SkRRect tmp = fRRect;
|
|
|
|
tmp.inset(inset, inset);
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
SkPath p;
|
2016-09-13 00:25:50 +00:00
|
|
|
p.addRRect(tmp);
|
2016-09-12 19:02:16 +00:00
|
|
|
return p;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
|
|
|
|
canvas->drawRRect(fRRect, paint);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void clip(SkCanvas* canvas) const override {
|
|
|
|
canvas->clipRRect(fRRect);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
bool contains(const SkRect& r) const override {
|
|
|
|
return fRRect.contains(r);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
const SkRect& bounds() const override {
|
|
|
|
return fRRect.getBounds();
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
static Object* Make(const SkRect& r) {
|
|
|
|
return new Oval(r);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
private:
|
|
|
|
SkRRect fRRect;
|
|
|
|
};
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
class Rect : public Object {
|
|
|
|
public:
|
|
|
|
Rect(const SkRect& r) : fRect(r) { }
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
|
|
|
|
if (!ctm.rectStaysRect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect devRect;
|
|
|
|
ctm.mapRect(&devRect, fRect);
|
|
|
|
*rr = SkRRect::MakeRect(devRect);
|
2016-09-12 19:02:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath asPath(SkScalar inset) const override {
|
|
|
|
SkRect tmp = fRect;
|
|
|
|
tmp.inset(inset, inset);
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
SkPath p;
|
2016-09-13 00:25:50 +00:00
|
|
|
p.addRect(tmp);
|
2016-09-12 19:02:16 +00:00
|
|
|
return p;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
|
|
|
|
canvas->drawRect(fRect, paint);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void clip(SkCanvas* canvas) const override {
|
|
|
|
canvas->clipRect(fRect);
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
bool contains(const SkRect& r) const override {
|
|
|
|
return fRect.contains(r);
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
const SkRect& bounds() const override {
|
|
|
|
return fRect;
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
static Object* Make(const SkRect& r) {
|
|
|
|
return new Rect(r);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
private:
|
|
|
|
SkRect fRect;
|
|
|
|
};
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
class Pentagon : public Object {
|
|
|
|
public:
|
|
|
|
Pentagon(const SkRect& r) {
|
|
|
|
SkPoint points[5] = {
|
|
|
|
{ 0.000000f, -1.000000f },
|
|
|
|
{ -0.951056f, -0.309017f },
|
|
|
|
{ -0.587785f, 0.809017f },
|
|
|
|
{ 0.587785f, 0.809017f },
|
|
|
|
{ 0.951057f, -0.309017f },
|
|
|
|
};
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
SkScalar height = r.height()/2.0f;
|
|
|
|
SkScalar width = r.width()/2.0f;
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
fPath.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height);
|
|
|
|
fPath.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height);
|
|
|
|
fPath.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height);
|
|
|
|
fPath.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height);
|
|
|
|
fPath.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height);
|
|
|
|
fPath.close();
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
bool asDevSpaceRRect(const SkMatrix& ctm, SkRRect* rr) const override {
|
2016-09-12 19:02:16 +00:00
|
|
|
return false;
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath asPath(SkScalar inset) const override { return fPath; }
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
void clip(SkCanvas* canvas) const override {
|
2016-09-13 00:25:50 +00:00
|
|
|
canvas->clipPath(this->asPath(0.0f));
|
2016-09-12 19:02:16 +00:00
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
bool contains(const SkRect& r) const override {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkRect& bounds() const override {
|
|
|
|
return fPath.getBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object* Make(const SkRect& r) {
|
|
|
|
return new Pentagon(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkPath fPath;
|
|
|
|
};
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// This GM attempts to mimic Android's reveal animation
|
|
|
|
class RevealGM : public GM {
|
|
|
|
public:
|
2016-09-12 19:02:16 +00:00
|
|
|
enum Mode {
|
|
|
|
kGaussianEdge_Mode,
|
|
|
|
kBlurMask_Mode,
|
|
|
|
kRRectsGaussianEdge_Mode,
|
|
|
|
|
|
|
|
kLast_Mode = kRRectsGaussianEdge_Mode
|
|
|
|
};
|
|
|
|
static const int kModeCount = kLast_Mode + 1;
|
|
|
|
|
2016-10-20 13:40:55 +00:00
|
|
|
enum CoverageGeom {
|
|
|
|
kRect_CoverageGeom,
|
|
|
|
kRRect_CoverageGeom,
|
|
|
|
kDRRect_CoverageGeom,
|
|
|
|
kPath_CoverageGeom,
|
|
|
|
|
|
|
|
kLast_CoverageGeom = kPath_CoverageGeom
|
|
|
|
};
|
|
|
|
static const int kCoverageGeomCount = kLast_CoverageGeom + 1;
|
|
|
|
|
2016-10-18 16:58:29 +00:00
|
|
|
RevealGM()
|
|
|
|
: fFraction(0.5f)
|
|
|
|
, fMode(kRRectsGaussianEdge_Mode)
|
|
|
|
, fPause(false)
|
2016-10-20 13:40:55 +00:00
|
|
|
, fBlurRadius(kInitialBlurRadius)
|
|
|
|
, fCoverageGeom(kRect_CoverageGeom) {
|
2016-09-08 16:02:43 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-10-20 15:58:40 +00:00
|
|
|
bool runAsBench() const override { return true; }
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("reveal");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-09-12 19:02:16 +00:00
|
|
|
PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make };
|
|
|
|
PFMakeMthd drawMakes[kNumRows] = {
|
|
|
|
RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make
|
2016-09-08 16:02:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint strokePaint;
|
|
|
|
strokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
strokePaint.setStrokeWidth(0.0f);
|
|
|
|
|
|
|
|
for (int y = 0; y < kNumRows; ++y) {
|
|
|
|
for (int x = 0; x < kNumCols; ++x) {
|
|
|
|
SkRect cell = SkRect::MakeXYWH(SkIntToScalar(x*kCellSize),
|
|
|
|
SkIntToScalar(y*kCellSize),
|
|
|
|
SkIntToScalar(kCellSize),
|
|
|
|
SkIntToScalar(kCellSize));
|
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(cell);
|
|
|
|
|
2016-09-08 16:02:43 +00:00
|
|
|
cell.inset(kPad, kPad);
|
|
|
|
SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset,
|
|
|
|
cell.centerY() + kClipOffset);
|
|
|
|
SkScalar curSize = kCellSize * fFraction;
|
2016-09-12 19:02:16 +00:00
|
|
|
const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize,
|
|
|
|
clipCenter.fY - curSize,
|
|
|
|
clipCenter.fX + curSize,
|
|
|
|
clipCenter.fY + curSize);
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<Object> clipObj((*clipMakes[x])(clipRect));
|
|
|
|
std::unique_ptr<Object> drawObj((*drawMakes[y])(cell));
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
// The goal is to replace this clipped draw (which clips the
|
|
|
|
// shadow) with a draw using the geometric clip
|
2016-09-12 19:02:16 +00:00
|
|
|
if (kGaussianEdge_Mode == fMode) {
|
2016-09-08 16:02:43 +00:00
|
|
|
canvas->save();
|
2016-09-12 19:02:16 +00:00
|
|
|
clipObj->clip(canvas);
|
|
|
|
|
|
|
|
// Draw with GaussianEdgeShader
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
// G channel is an F6.2 radius
|
2016-10-18 16:58:29 +00:00
|
|
|
int iBlurRad = (int)(4.0f * fBlurRadius);
|
|
|
|
paint.setColor(SkColorSetARGB(255, iBlurRad >> 8, iBlurRad & 0xFF, 0));
|
2016-09-12 19:02:16 +00:00
|
|
|
paint.setShader(SkGaussianEdgeShader::Make());
|
|
|
|
drawObj->draw(canvas, paint);
|
2016-09-08 16:02:43 +00:00
|
|
|
canvas->restore();
|
2016-09-12 19:02:16 +00:00
|
|
|
} else if (kBlurMask_Mode == fMode) {
|
|
|
|
SkPath clippedPath;
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-10-18 16:58:29 +00:00
|
|
|
SkScalar sigma = fBlurRadius / 4.0f;
|
2016-09-13 00:25:50 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
if (clipObj->contains(drawObj->bounds())) {
|
2016-09-13 00:25:50 +00:00
|
|
|
clippedPath = drawObj->asPath(2.0f*sigma);
|
2016-09-12 19:02:16 +00:00
|
|
|
} else {
|
2016-09-13 00:25:50 +00:00
|
|
|
SkPath drawnPath = drawObj->asPath(2.0f*sigma);
|
|
|
|
SkPath clipPath = clipObj->asPath(2.0f*sigma);
|
2016-09-08 16:02:43 +00:00
|
|
|
|
2016-09-12 19:02:16 +00:00
|
|
|
SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath));
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
SkPaint blurPaint;
|
|
|
|
blurPaint.setAntiAlias(true);
|
2016-09-13 00:25:50 +00:00
|
|
|
blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma));
|
2016-09-08 16:02:43 +00:00
|
|
|
canvas->drawPath(clippedPath, blurPaint);
|
2016-09-12 19:02:16 +00:00
|
|
|
} else {
|
|
|
|
SkASSERT(kRRectsGaussianEdge_Mode == fMode);
|
|
|
|
|
|
|
|
SkRect cover = drawObj->bounds();
|
|
|
|
SkAssertResult(cover.intersect(clipObj->bounds()));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
SkRRect devSpaceClipRR, devSpaceDrawnRR;
|
2016-09-12 19:02:16 +00:00
|
|
|
|
2016-10-20 15:58:40 +00:00
|
|
|
if (clipObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceClipRR) &&
|
|
|
|
drawObj->asDevSpaceRRect(canvas->getTotalMatrix(), &devSpaceDrawnRR)) {
|
|
|
|
paint.setMaskFilter(SkRRectsGaussianEdgeMaskFilter::Make(devSpaceClipRR,
|
|
|
|
devSpaceDrawnRR,
|
2016-10-20 13:40:55 +00:00
|
|
|
fBlurRadius));
|
2016-09-12 19:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 13:40:55 +00:00
|
|
|
strokePaint.setColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
switch (fCoverageGeom) {
|
|
|
|
case kRect_CoverageGeom:
|
|
|
|
canvas->drawRect(cover, paint);
|
|
|
|
canvas->drawRect(cover, strokePaint);
|
|
|
|
break;
|
|
|
|
case kRRect_CoverageGeom: {
|
|
|
|
const SkRRect rrect = SkRRect::MakeRectXY(
|
|
|
|
cover.makeOutset(10.0f, 10.0f),
|
|
|
|
10.0f, 10.0f);
|
|
|
|
canvas->drawRRect(rrect, paint);
|
|
|
|
canvas->drawRRect(rrect, strokePaint);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kDRRect_CoverageGeom: {
|
|
|
|
const SkRRect inner = SkRRect::MakeRectXY(cover.makeInset(10.0f, 10.0f),
|
|
|
|
10.0f, 10.0f);
|
|
|
|
const SkRRect outer = SkRRect::MakeRectXY(
|
|
|
|
cover.makeOutset(10.0f, 10.0f),
|
|
|
|
10.0f, 10.0f);
|
|
|
|
canvas->drawDRRect(outer, inner, paint);
|
|
|
|
canvas->drawDRRect(outer, inner, strokePaint);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kPath_CoverageGeom: {
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(cover.fLeft, cover.fTop);
|
|
|
|
path.lineTo(cover.centerX(), cover.centerY());
|
|
|
|
path.lineTo(cover.fRight, cover.fTop);
|
|
|
|
path.lineTo(cover.fRight, cover.fBottom);
|
|
|
|
path.lineTo(cover.centerX(), cover.centerY());
|
|
|
|
path.lineTo(cover.fLeft, cover.fBottom);
|
|
|
|
path.close();
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
canvas->drawPath(path, strokePaint);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
2016-09-12 19:02:16 +00:00
|
|
|
|
|
|
|
// Draw the clip and draw objects for reference
|
|
|
|
strokePaint.setColor(SK_ColorRED);
|
2016-09-13 00:25:50 +00:00
|
|
|
canvas->drawPath(drawObj->asPath(0.0f), strokePaint);
|
2016-09-12 19:02:16 +00:00
|
|
|
strokePaint.setColor(SK_ColorGREEN);
|
2016-09-13 00:25:50 +00:00
|
|
|
canvas->drawPath(clipObj->asPath(0.0f), strokePaint);
|
2016-09-12 19:02:16 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onHandleKey(SkUnichar uni) override {
|
|
|
|
switch (uni) {
|
|
|
|
case 'C':
|
2016-09-12 19:02:16 +00:00
|
|
|
fMode = (Mode)((fMode + 1) % kModeCount);
|
2016-09-08 16:02:43 +00:00
|
|
|
return true;
|
2016-10-18 16:58:29 +00:00
|
|
|
case '+':
|
|
|
|
fBlurRadius += 1.0f;
|
|
|
|
return true;
|
|
|
|
case '-':
|
|
|
|
fBlurRadius = SkTMax(1.0f, fBlurRadius - 1.0f);
|
|
|
|
return true;
|
2016-09-13 00:25:50 +00:00
|
|
|
case 'p':
|
|
|
|
fPause = !fPause;
|
|
|
|
return true;
|
2016-10-20 13:40:55 +00:00
|
|
|
case 'G':
|
|
|
|
fCoverageGeom = (CoverageGeom) ((fCoverageGeom+1) % kCoverageGeomCount);
|
|
|
|
return true;
|
2016-09-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
2016-09-13 00:25:50 +00:00
|
|
|
if (!fPause) {
|
|
|
|
fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
2016-09-08 16:02:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-20 13:40:55 +00:00
|
|
|
SkScalar fFraction;
|
|
|
|
Mode fMode;
|
|
|
|
bool fPause;
|
|
|
|
float fBlurRadius;
|
|
|
|
CoverageGeom fCoverageGeom;
|
2016-09-08 16:02:43 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new RevealGM;)
|
|
|
|
}
|