Add shader-based GaussianEdgeShader for reveal case

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2321713004

Review-Url: https://codereview.chromium.org/2321713004
This commit is contained in:
robertphillips 2016-09-12 12:02:16 -07:00 committed by Commit bot
parent cc6aeee675
commit 2af83ac4f6
5 changed files with 767 additions and 206 deletions

View File

@ -9,6 +9,7 @@
#include "SkAnimTimer.h"
#include "SkBlurMaskFilter.h"
#include "SkGaussianEdgeShader.h"
#include "SkRRectsGaussianEdgeShader.h"
#include "SkPath.h"
#include "SkPathOps.h"
#include "SkRRect.h"
@ -22,211 +23,246 @@ constexpr SkScalar kPeriod = 8.0f;
constexpr int kClipOffset = 32;
///////////////////////////////////////////////////////////////////////////////////////////////////
typedef SkPath (*PFDrawMthd)(SkCanvas*, const SkRect&, bool);
static SkPath draw_rrect(SkCanvas* canvas, const SkRect& r, bool stroked) {
SkRRect rr = SkRRect::MakeRectXY(r, 2*kPad, 2*kPad);
class Object {
public:
virtual ~Object() {}
virtual bool asRRect(SkRRect* rr) const = 0;
virtual SkPath asPath() const = 0;
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;
};
SkPaint paint;
paint.setAntiAlias(true);
if (stroked) {
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorRED);
} else {
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
paint.setShader(SkGaussianEdgeShader::Make());
typedef Object* (*PFMakeMthd)(const SkRect& r);
class RRect : public Object {
public:
RRect(const SkRect& r) {
fRRect = SkRRect::MakeRectXY(r, 4*kPad, 4*kPad);
}
canvas->drawRRect(rr, paint);
SkPath p;
p.addRoundRect(r, 2*kPad, 2*kPad);
return p;
}
bool asRRect(SkRRect* rr) const override {
*rr = fRRect;
return true;
}
static SkPath draw_stroked_rrect(SkCanvas* canvas, const SkRect& r, bool stroked) {
SkRect insetRect = r;
insetRect.inset(kPad, kPad);
SkRRect rr = SkRRect::MakeRectXY(insetRect, 2*kPad, 2*kPad);
SkPath asPath() const override {
SkPath p;
p.addRRect(fRRect);
return p;
}
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(kPad);
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
canvas->drawRRect(fRRect, paint);
}
void clip(SkCanvas* canvas) const override {
canvas->clipRRect(fRRect);
}
bool contains(const SkRect& r) const override {
return fRRect.contains(r);
}
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);
}
bool asRRect(SkRRect* rr) const override {
return false;
}
SkPath asPath() const override {
// In this case we want the outline of the stroked rrect
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(kPad);
if (stroked) {
// In this case we want to draw a stroked representation of the stroked rrect
SkPath p, stroked;
p.addRRect(rr);
p.addRRect(fRRect);
SkStroke stroke(paint);
stroke.strokePath(p, &stroked);
paint.setStrokeWidth(0);
paint.setColor(SK_ColorRED);
canvas->drawPath(stroked, paint);
} else {
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
paint.setShader(SkGaussianEdgeShader::Make());
canvas->drawRRect(rr, paint);
return stroked;
}
SkPath p;
insetRect.outset(kPad/2.0f, kPad/2.0f);
p.addRoundRect(insetRect, 2*kPad, 2*kPad);
return p;
}
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
SkPaint stroke(paint);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(kPad);
static SkPath draw_oval(SkCanvas* canvas, const SkRect& r, bool stroked) {
SkRRect rr = SkRRect::MakeOval(r);
SkPaint paint;
paint.setAntiAlias(true);
if (stroked) {
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorRED);
} else {
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
paint.setShader(SkGaussianEdgeShader::Make());
}
canvas->drawRRect(rr, paint);
SkPath p;
p.addOval(r);
return p;
}
static SkPath draw_square(SkCanvas* canvas, const SkRect& r, bool stroked) {
SkPaint paint;
paint.setAntiAlias(true);
if (stroked) {
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorRED);
} else {
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
paint.setShader(SkGaussianEdgeShader::Make());
}
canvas->drawRect(r, paint);
SkPath p;
p.addRect(r);
return p;
}
static SkPath draw_pentagon(SkCanvas* canvas, const SkRect& r, bool stroked) {
SkPath p;
SkPoint points[5] = {
{ 0.000000f, -1.000000f },
{ -0.951056f, -0.309017f },
{ -0.587785f, 0.809017f },
{ 0.587785f, 0.809017f },
{ 0.951057f, -0.309017f },
};
SkScalar height = r.height()/2.0f;
SkScalar width = r.width()/2.0f;
p.moveTo(r.centerX() + points[0].fX * width, r.centerY() + points[0].fY * height);
p.lineTo(r.centerX() + points[1].fX * width, r.centerY() + points[1].fY * height);
p.lineTo(r.centerX() + points[2].fX * width, r.centerY() + points[2].fY * height);
p.lineTo(r.centerX() + points[3].fX * width, r.centerY() + points[3].fY * height);
p.lineTo(r.centerX() + points[4].fX * width, r.centerY() + points[4].fY * height);
p.close();
SkPaint paint;
paint.setAntiAlias(true);
if (stroked) {
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorRED);
} else {
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
// This currently goes through the GrAAConvexPathRenderer and produces a
// AAConvexPathBatch (i.e., it doesn't have a analytic distance)
// paint.setShader(SkGaussianEdgeShader::Make());
}
canvas->drawPath(p, paint);
return p;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
typedef void (*PFClipMthd)(SkCanvas* canvas, const SkPoint&, SkScalar);
static void circle_clip(SkCanvas* canvas, const SkPoint& center, SkScalar rad) {
SkRect r = SkRect::MakeLTRB(center.fX - rad, center.fY - rad, center.fX + rad, center.fY + rad);
SkRRect rr = SkRRect::MakeOval(r);
canvas->clipRRect(rr);
}
static void square_clip(SkCanvas* canvas, const SkPoint& center, SkScalar size) {
SkScalar newSize = SK_ScalarRoot2Over2 * size;
SkRect r = SkRect::MakeLTRB(center.fX - newSize, center.fY - newSize,
center.fX + newSize, center.fY + newSize);
canvas->clipRect(r);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// These are stand alone methods (rather than just, say, returning the SkPath for the clip
// object) so that we can catch the clip-contains-victim case.
typedef SkPath (*PFGeometricClipMthd)(const SkPoint&, SkScalar, const SkPath&);
static SkPath circle_geometric_clip(const SkPoint& center, SkScalar rad, const SkPath& victim) {
const SkRect bound = victim.getBounds();
SkPoint pts[4];
bound.toQuad(pts);
bool clipContainsVictim = true;
for (int i = 0; i < 4; ++i) {
SkScalar distSq = (pts[i].fX - center.fX) * (pts[i].fX - center.fX) +
(pts[i].fY - center.fY) * (pts[i].fY - center.fY);
if (distSq >= rad*rad) {
clipContainsVictim = false;
}
canvas->drawRRect(fRRect, stroke);
}
if (clipContainsVictim) {
return victim;
void clip(SkCanvas* canvas) const override {
canvas->clipPath(this->asPath());
}
// Add victim contains clip test?
SkPath clipPath;
clipPath.addCircle(center.fX, center.fY, rad);
SkPath result;
SkAssertResult(Op(clipPath, victim, kIntersect_SkPathOp, &result));
return result;
}
static SkPath square_geometric_clip(const SkPoint& center, SkScalar size, const SkPath& victim) {
SkScalar newSize = SK_ScalarRoot2Over2 * size;
SkRect r = SkRect::MakeLTRB(center.fX - newSize, center.fY - newSize,
center.fX + newSize, center.fY + newSize);
const SkRect bound = victim.getBounds();
if (r.contains(bound)) {
return victim;
bool contains(const SkRect& r) const override {
return false;
}
// Add victim contains clip test?
const SkRect& bounds() const override {
return fStrokedBounds;
}
SkPath clipPath;
clipPath.addRect(r);
static Object* Make(const SkRect& r) {
return new StrokedRRect(r);
}
SkPath result;
SkAssertResult(Op(clipPath, victim, kIntersect_SkPathOp, &result));
private:
SkRRect fRRect;
SkRect fStrokedBounds;
};
return result;
}
class Oval : public Object {
public:
Oval(const SkRect& r) {
fRRect = SkRRect::MakeOval(r);
}
bool asRRect(SkRRect* rr) const override {
*rr = fRRect;
return true;
}
SkPath asPath() const override {
SkPath p;
p.addRRect(fRRect);
return p;
}
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
canvas->drawRRect(fRRect, paint);
}
void clip(SkCanvas* canvas) const override {
canvas->clipRRect(fRRect);
}
bool contains(const SkRect& r) const override {
return fRRect.contains(r);
}
const SkRect& bounds() const override {
return fRRect.getBounds();
}
static Object* Make(const SkRect& r) {
return new Oval(r);
}
private:
SkRRect fRRect;
};
class Rect : public Object {
public:
Rect(const SkRect& r) : fRect(r) { }
bool asRRect(SkRRect* rr) const override {
*rr = SkRRect::MakeRect(fRect);
return true;
}
SkPath asPath() const override {
SkPath p;
p.addRect(fRect);
return p;
}
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
canvas->drawRect(fRect, paint);
}
void clip(SkCanvas* canvas) const override {
canvas->clipRect(fRect);
}
bool contains(const SkRect& r) const override {
return fRect.contains(r);
}
const SkRect& bounds() const override {
return fRect;
}
static Object* Make(const SkRect& r) {
return new Rect(r);
}
private:
SkRect fRect;
};
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 },
};
SkScalar height = r.height()/2.0f;
SkScalar width = r.width()/2.0f;
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();
}
bool asRRect(SkRRect* rr) const override {
return false;
}
SkPath asPath() const override { return fPath; }
void draw(SkCanvas* canvas, const SkPaint& paint) const override {
canvas->drawPath(fPath, paint);
}
void clip(SkCanvas* canvas) const override {
canvas->clipPath(this->asPath());
}
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;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace skiagm {
@ -234,7 +270,17 @@ namespace skiagm {
// This GM attempts to mimic Android's reveal animation
class RevealGM : public GM {
public:
RevealGM() : fFraction(0.5f), fDrawWithGaussianEdge(true) {
enum Mode {
kGaussianEdge_Mode,
kBlurMask_Mode,
kRRectsGaussianEdge_Mode,
kLast_Mode = kRRectsGaussianEdge_Mode
};
static const int kModeCount = kLast_Mode + 1;
RevealGM() : fFraction(0.5f), fMode(kRRectsGaussianEdge_Mode) {
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
}
@ -249,17 +295,9 @@ protected:
}
void onDraw(SkCanvas* canvas) override {
PFClipMthd clips[kNumCols] = { circle_clip, square_clip };
PFGeometricClipMthd geometricClips[kNumCols] = {
circle_geometric_clip,
square_geometric_clip
};
PFDrawMthd draws[kNumRows] = {
draw_rrect,
draw_stroked_rrect,
draw_oval,
draw_square,
draw_pentagon
PFMakeMthd clipMakes[kNumCols] = { Oval::Make, Rect::Make };
PFMakeMthd drawMakes[kNumRows] = {
RRect::Make, StrokedRRect::Make, Oval::Make, Rect::Make, Pentagon::Make
};
SkPaint strokePaint;
@ -274,32 +312,79 @@ protected:
SkIntToScalar(kCellSize),
SkIntToScalar(kCellSize));
canvas->save();
canvas->clipRect(cell);
cell.inset(kPad, kPad);
SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset,
cell.centerY() + kClipOffset);
SkScalar curSize = kCellSize * fFraction;
const SkRect clipRect = SkRect::MakeLTRB(clipCenter.fX - curSize,
clipCenter.fY - curSize,
clipCenter.fX + curSize,
clipCenter.fY + curSize);
SkAutoTDelete<Object> clipObj((*clipMakes[x])(clipRect));
SkAutoTDelete<Object> drawObj((*drawMakes[y])(cell));
// The goal is to replace this clipped draw (which clips the
// shadow) with a draw using the geometric clip
if (fDrawWithGaussianEdge) {
if (kGaussianEdge_Mode == fMode) {
canvas->save();
(*clips[x])(canvas, clipCenter, curSize);
(*draws[y])(canvas, cell, false);
clipObj->clip(canvas);
// Draw with GaussianEdgeShader
SkPaint paint;
paint.setAntiAlias(true);
// G channel is an F6.2 radius
paint.setColor(SkColorSetARGB(255, 255, (unsigned char)(4*kPad), 0));
paint.setShader(SkGaussianEdgeShader::Make());
drawObj->draw(canvas, paint);
canvas->restore();
}
} else if (kBlurMask_Mode == fMode) {
SkPath clippedPath;
SkPath drawnPath = (*draws[y])(canvas, cell, true);
if (clipObj->contains(drawObj->bounds())) {
clippedPath = drawObj->asPath();
} else {
SkPath drawnPath = drawObj->asPath();
SkPath clipPath = clipObj->asPath();
if (!fDrawWithGaussianEdge) {
SkPath clippedPath = (*geometricClips[x])(clipCenter, curSize, drawnPath);
SkASSERT(clippedPath.isConvex());
SkAssertResult(Op(clipPath, drawnPath, kIntersect_SkPathOp, &clippedPath));
}
SkPaint blurPaint;
blurPaint.setAntiAlias(true);
blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 3.0f));
canvas->drawPath(clippedPath, blurPaint);
} else {
SkASSERT(kRRectsGaussianEdge_Mode == fMode);
SkRect cover = drawObj->bounds();
SkAssertResult(cover.intersect(clipObj->bounds()));
SkPaint paint;
SkRRect clipRR, drawnRR;
if (clipObj->asRRect(&clipRR) && drawObj->asRRect(&drawnRR)) {
paint.setShader(SkRRectsGaussianEdgeShader::Make(clipRR, drawnRR,
kPad, 0.0f));
}
canvas->drawRect(cover, paint);
}
// Draw the clip and draw objects for reference
SkPaint strokePaint;
strokePaint.setStyle(SkPaint::kStroke_Style);
strokePaint.setStrokeWidth(0);
strokePaint.setColor(SK_ColorRED);
canvas->drawPath(drawObj->asPath(), strokePaint);
strokePaint.setColor(SK_ColorGREEN);
canvas->drawPath(clipObj->asPath(), strokePaint);
canvas->restore();
}
}
}
@ -307,7 +392,7 @@ protected:
bool onHandleKey(SkUnichar uni) override {
switch (uni) {
case 'C':
fDrawWithGaussianEdge = !fDrawWithGaussianEdge;
fMode = (Mode)((fMode + 1) % kModeCount);
return true;
}
@ -321,7 +406,7 @@ protected:
private:
SkScalar fFraction;
bool fDrawWithGaussianEdge;
Mode fMode;
typedef GM INHERITED;
};

View File

@ -59,6 +59,7 @@
'<(skia_src_path)/effects/SkPaintImageFilter.cpp',
'<(skia_src_path)/effects/SkPerlinNoiseShader.cpp',
'<(skia_src_path)/effects/SkPictureImageFilter.cpp',
'<(skia_src_path)/effects/SkRRectsGaussianEdgeShader.cpp',
'<(skia_src_path)/effects/SkTableColorFilter.cpp',
'<(skia_src_path)/effects/SkTableMaskFilter.cpp',
'<(skia_src_path)/effects/SkTileImageFilter.cpp',
@ -117,6 +118,7 @@
'<(skia_include_path)/effects/SkPaintFlagsDrawFilter.h',
'<(skia_include_path)/effects/SkPaintImageFilter.h',
'<(skia_include_path)/effects/SkPerlinNoiseShader.h',
'<(skia_include_path)/effects/SkRRectsGaussianEdgeShader.h',
'<(skia_include_path)/effects/SkTableColorFilter.h',
'<(skia_include_path)/effects/SkTableMaskFilter.h',
'<(skia_include_path)/effects/SkTileImageFilter.h',

View File

@ -0,0 +1,37 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkRRectsGaussianEdgeShader_DEFINED
#define SkRRectsGaussianEdgeShader_DEFINED
#include "SkShader.h"
class SkRRect;
class SK_API SkRRectsGaussianEdgeShader {
public:
/** Returns a shader that applies a Gaussian blur depending on distance to the edge
* of the intersection of two round rects.
* Currently this is only useable with round rects that have the same radii at
* all the corners and for which the x & y radii are equal.
* Raster will draw nothing.
*
* The coverage geometry that should be drawn should be no larger than the intersection
* of the bounding boxes of the two round rects. Ambitious users can omit the center
* area of the coverage geometry if it is known to be occluded.
*/
static sk_sp<SkShader> Make(const SkRRect& first,
const SkRRect& second,
SkScalar radius, SkScalar pad);
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
private:
SkRRectsGaussianEdgeShader(); // can't be instantiated
};
#endif

View File

@ -0,0 +1,435 @@
/*
* 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 "SkRRectsGaussianEdgeShader.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
/** \class SkRRectsGaussianEdgeShaderImpl
* This shader applies a gaussian edge to the intersection of two round rects.
* The round rects must have the same radii at each corner and the x&y radii
* must also be equal.
*/
class SkRRectsGaussianEdgeShaderImpl : public SkShader {
public:
SkRRectsGaussianEdgeShaderImpl(const SkRRect& first, const SkRRect& second,
SkScalar radius, SkScalar pad)
: fFirst(first)
, fSecond(second)
, fRadius(radius)
, fPad(pad) {
}
bool isOpaque() const override { return false; }
#if SK_SUPPORT_GPU
sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
#endif
class GaussianEdgeShaderContext : public SkShader::Context {
public:
GaussianEdgeShaderContext(const SkRRectsGaussianEdgeShaderImpl&, const ContextRec&);
~GaussianEdgeShaderContext() override { }
void shadeSpan(int x, int y, SkPMColor[], int count) override;
uint32_t getFlags() const override { return 0; }
private:
SkColor fPaintColor;
typedef SkShader::Context INHERITED;
};
SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkRRectsGaussianEdgeShaderImpl)
protected:
void flatten(SkWriteBuffer&) const override;
size_t onContextSize(const ContextRec&) const override;
Context* onCreateContext(const ContextRec&, void*) const override;
private:
SkRRect fFirst;
SkRRect fSecond;
SkScalar fRadius;
SkScalar fPad;
friend class SkRRectsGaussianEdgeShader; // for serialization registration system
typedef SkShader INHERITED;
};
////////////////////////////////////////////////////////////////////////////
#if SK_SUPPORT_GPU
#include "GrCoordTransform.h"
#include "GrFragmentProcessor.h"
#include "GrInvariantOutput.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUniformHandler.h"
#include "SkGr.h"
#include "SkGrPriv.h"
class RRectsGaussianEdgeFP : public GrFragmentProcessor {
public:
enum Mode {
kCircle_Mode,
kRect_Mode,
kSimpleCircular_Mode,
};
RRectsGaussianEdgeFP(const SkRRect& first, const SkRRect& second,
SkScalar radius, SkScalar pad)
: fFirst(first)
, fSecond(second)
, fRadius(radius)
, fPad(pad) {
this->initClassID<RRectsGaussianEdgeFP>();
this->setWillReadFragmentPosition();
fFirstMode = ComputeMode(fFirst);
fSecondMode = ComputeMode(fSecond);
}
class GLSLRRectsGaussianEdgeFP : public GrGLSLFragmentProcessor {
public:
GLSLRRectsGaussianEdgeFP() { }
void emitModeCode(Mode mode,
GrGLSLFPFragmentBuilder* fragBuilder,
const char* posName,
const char* sizesName,
const char* radiiName,
const char* outputName,
const char indices[2]) { // how to access the params for the 2 rrects
// positive distance is towards the center of the circle
fragBuilder->codeAppendf("vec2 delta = %s.xy - %s.%s;",
fragBuilder->fragmentPosition(),
posName, indices);
switch (mode) {
case kCircle_Mode:
fragBuilder->codeAppendf("%s = %s.%c - length(delta);",
outputName,
sizesName, indices[0]);
break;
case kRect_Mode:
fragBuilder->codeAppendf("float xDist = %s.%c - abs(delta.x);",
sizesName, indices[0]);
fragBuilder->codeAppendf("float yDist = %s.%c - abs(delta.y);",
sizesName, indices[1]);
fragBuilder->codeAppendf("%s = min(xDist, yDist);", outputName);
break;
case kSimpleCircular_Mode:
// For the circular round rect we first compute the distance
// to the rect. Then we compute a multiplier that is 1 if the
// point is in one of the circular corners. We then compute the
// distance from the corner and then use the multiplier to mask
// between the two distances.
fragBuilder->codeAppendf("float xDist = %s.%c - abs(delta.x);",
sizesName, indices[0]);
fragBuilder->codeAppendf("float yDist = %s.%c - abs(delta.y);",
sizesName, indices[1]);
fragBuilder->codeAppend("float rectDist = min(xDist, yDist);");
fragBuilder->codeAppendf("vec2 cornerCenter = %s.%s - %s.%s;",
sizesName, indices,
radiiName, indices);
fragBuilder->codeAppend("delta = vec2(abs(delta.x) - cornerCenter.x,"
"abs(delta.y) - cornerCenter.y);");
fragBuilder->codeAppendf("xDist = %s.%c - abs(delta.x);",
radiiName, indices[0]);
fragBuilder->codeAppendf("yDist = %s.%c - abs(delta.y);",
radiiName, indices[1]);
fragBuilder->codeAppend("float cornerDist = min(xDist, yDist);");
fragBuilder->codeAppend("float multiplier = step(0.0, cornerDist);");
fragBuilder->codeAppendf("delta += %s.%s;", radiiName, indices);
fragBuilder->codeAppendf("cornerDist = 2.0 * %s.%c - length(delta);",
radiiName, indices[0]);
fragBuilder->codeAppendf("%s = (multiplier * cornerDist) +"
"((1.0-multiplier) * rectDist);",
outputName);
break;
}
}
void emitCode(EmitArgs& args) override {
const RRectsGaussianEdgeFP& fp = args.fFp.cast<RRectsGaussianEdgeFP>();
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
const char* positionsUniName = nullptr;
fPositionsUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"Positions", &positionsUniName);
const char* sizesUniName = nullptr;
fSizesUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"Sizes", &sizesUniName);
const char* radiiUniName = nullptr;
fRadiiUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"Radii", &radiiUniName);
const char* padRadUniName = nullptr;
fPadRadUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec2f_GrSLType, kDefault_GrSLPrecision,
"PadRad", &padRadUniName);
fragBuilder->codeAppend("float firstDist;");
fragBuilder->codeAppend("{");
this->emitModeCode(fp.firstMode(), fragBuilder,
positionsUniName, sizesUniName, radiiUniName, "firstDist", "xy");
fragBuilder->codeAppend("}");
fragBuilder->codeAppend("float secondDist;");
fragBuilder->codeAppend("{");
this->emitModeCode(fp.secondMode(), fragBuilder,
positionsUniName, sizesUniName, radiiUniName, "secondDist", "zw");
fragBuilder->codeAppend("}");
// Here use the sign of the distance to the two round rects to mask off the different
// cases.
fragBuilder->codeAppend("float in1 = step(0.0f, firstDist);");
fragBuilder->codeAppend("float in2 = step(0.0f, secondDist);");
fragBuilder->codeAppend("float dist = "
"in1*in2 * min(firstDist, secondDist);"
"in1*(1.0-in2) * firstDist +"
"(1.0-in1)*in2 * secondDist;");
// Finally use the distance to apply the Gaussian edge
fragBuilder->codeAppendf("float factor = 1.0 - clamp((dist - %s.x)/%s.y, 0.0, 1.0);",
padRadUniName, padRadUniName);
fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
fragBuilder->codeAppendf("%s = vec4(%s.rgb, factor);",
args.fOutputColor, args.fInputColor);
}
static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
GrProcessorKeyBuilder* b) {
const RRectsGaussianEdgeFP& fp = proc.cast<RRectsGaussianEdgeFP>();
b->add32(fp.firstMode() | (fp.secondMode() << 4));
}
protected:
void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
const SkRRect& first = edgeFP.first();
const SkRRect& second = edgeFP.second();
pdman.set4f(fPositionsUni,
first.getBounds().centerX(),
first.getBounds().centerY(),
second.getBounds().centerX(),
second.getBounds().centerY());
pdman.set4f(fSizesUni,
0.5f * first.rect().width(),
0.5f * first.rect().height(),
0.5f * second.rect().width(),
0.5f * second.rect().height());
// This is a bit of overkill since fX should equal fY for both round rects but it
// makes the shader code simpler.
pdman.set4f(fRadiiUni,
0.5f * first.getSimpleRadii().fX,
0.5f * first.getSimpleRadii().fY,
0.5f * second.getSimpleRadii().fX,
0.5f * second.getSimpleRadii().fY);
pdman.set2f(fPadRadUni, edgeFP.pad(), edgeFP.radius());
}
private:
// The centers of the two round rects (x1, y1, x2, y2)
GrGLSLProgramDataManager::UniformHandle fPositionsUni;
// The half widths and half heights of the two round rects (w1/2, h1/2, w2/2, h2/2)
// For circles we still upload both width & height to simplify things
GrGLSLProgramDataManager::UniformHandle fSizesUni;
// The half corner radii of the two round rects (rx1/2, ry1/2, rx2/2, ry2/2)
// We upload both the x&y radii (although they are currently always the same) to make
// the indexing in the shader code simpler. In some future world we could also support
// non-circular corner round rects & ellipses.
GrGLSLProgramDataManager::UniformHandle fRadiiUni;
// The pad and radius parameters (padding, radius)
GrGLSLProgramDataManager::UniformHandle fPadRadUni;
typedef GrGLSLFragmentProcessor INHERITED;
};
void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
GLSLRRectsGaussianEdgeFP::GenKey(*this, caps, b);
}
const char* name() const override { return "RRectsGaussianEdgeFP"; }
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
}
const SkRRect& first() const { return fFirst; }
Mode firstMode() const { return fFirstMode; }
const SkRRect& second() const { return fSecond; }
Mode secondMode() const { return fSecondMode; }
SkScalar radius() const { return fRadius; }
SkScalar pad() const { return fPad; }
private:
static Mode ComputeMode(const SkRRect& rr) {
if (rr.isCircle()) {
return kCircle_Mode;
} else if (rr.isRect()) {
return kRect_Mode;
} else {
SkASSERT(rr.isSimpleCircular());
return kSimpleCircular_Mode;
}
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
return new GLSLRRectsGaussianEdgeFP;
}
bool onIsEqual(const GrFragmentProcessor& proc) const override {
const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
return fFirst == edgeFP.fFirst && fSecond == edgeFP.fSecond &&
fRadius == edgeFP.fRadius && fPad == edgeFP.fPad;
}
SkRRect fFirst;
Mode fFirstMode;
SkRRect fSecond;
Mode fSecondMode;
SkScalar fRadius;
SkScalar fPad;
typedef GrFragmentProcessor INHERITED;
};
////////////////////////////////////////////////////////////////////////////
sk_sp<GrFragmentProcessor> SkRRectsGaussianEdgeShaderImpl::asFragmentProcessor(
const AsFPArgs& args) const {
return sk_make_sp<RRectsGaussianEdgeFP>(fFirst, fSecond, fRadius, fPad);
}
#endif
////////////////////////////////////////////////////////////////////////////
SkRRectsGaussianEdgeShaderImpl::GaussianEdgeShaderContext::GaussianEdgeShaderContext(
const SkRRectsGaussianEdgeShaderImpl& shader,
const ContextRec& rec)
: INHERITED(shader, rec) {
fPaintColor = rec.fPaint->getColor();
}
void SkRRectsGaussianEdgeShaderImpl::GaussianEdgeShaderContext::shadeSpan(int x, int y,
SkPMColor result[],
int count) {
// TODO: implement
for (int i = 0; i < count; ++i) {
result[i] = fPaintColor;
}
}
////////////////////////////////////////////////////////////////////////////
#ifndef SK_IGNORE_TO_STRING
void SkRRectsGaussianEdgeShaderImpl::toString(SkString* str) const {
str->appendf("RRectsGaussianEdgeShader: ()");
}
#endif
sk_sp<SkFlattenable> SkRRectsGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) {
// Discarding SkShader flattenable params
bool hasLocalMatrix = buf.readBool();
SkAssertResult(!hasLocalMatrix);
SkRect rect1, rect2;
buf.readRect(&rect1);
SkScalar xRad1 = buf.readScalar();
SkScalar yRad1 = buf.readScalar();
buf.readRect(&rect2);
SkScalar xRad2 = buf.readScalar();
SkScalar yRad2 = buf.readScalar();
SkScalar radius = buf.readScalar();
SkScalar pad = buf.readScalar();
return sk_make_sp<SkRRectsGaussianEdgeShaderImpl>(SkRRect::MakeRectXY(rect1, xRad1, yRad1),
SkRRect::MakeRectXY(rect2, xRad2, yRad2),
radius, pad);
}
void SkRRectsGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const {
INHERITED::flatten(buf);
SkASSERT(fFirst.isRect() || fFirst.isCircle() || fFirst.isSimpleCircular());
buf.writeRect(fFirst.rect());
const SkVector& radii1 = fFirst.getSimpleRadii();
buf.writeScalar(radii1.fX);
buf.writeScalar(radii1.fY);
SkASSERT(fSecond.isRect() || fSecond.isCircle() || fSecond.isSimpleCircular());
buf.writeRect(fSecond.rect());
const SkVector& radii2 = fSecond.getSimpleRadii();
buf.writeScalar(radii2.fX);
buf.writeScalar(radii2.fY);
buf.writeScalar(fRadius);
buf.writeScalar(fPad);
}
size_t SkRRectsGaussianEdgeShaderImpl::onContextSize(const ContextRec& rec) const {
return sizeof(GaussianEdgeShaderContext);
}
SkShader::Context* SkRRectsGaussianEdgeShaderImpl::onCreateContext(const ContextRec& rec,
void* storage) const {
return new (storage) GaussianEdgeShaderContext(*this, rec);
}
///////////////////////////////////////////////////////////////////////////////
sk_sp<SkShader> SkRRectsGaussianEdgeShader::Make(const SkRRect& first,
const SkRRect& second,
SkScalar radius,
SkScalar pad) {
if ((!first.isRect() && !first.isCircle() && !first.isSimpleCircular()) ||
(!second.isRect() && !second.isCircle() && !second.isSimpleCircular())) {
// we only deal with the shapes where the x & y radii are equal
// and the same for all four corners
return nullptr;
}
return sk_make_sp<SkRRectsGaussianEdgeShaderImpl>(first, second, radius, pad);
}
///////////////////////////////////////////////////////////////////////////////
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkRRectsGaussianEdgeShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRRectsGaussianEdgeShaderImpl)
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
///////////////////////////////////////////////////////////////////////////////

View File

@ -25,6 +25,7 @@
#include "SkDropShadowImageFilter.h"
#include "SkEmbossMaskFilter.h"
#include "SkGaussianEdgeShader.h"
#include "SkRRectsGaussianEdgeShader.h"
#include "SkGradientShader.h"
#include "SkImageSource.h"
#include "SkLayerDrawLooper.h"
@ -91,6 +92,7 @@ void SkFlattenable::PrivateInitializer::InitEffects() {
SkLightingShader::InitializeFlattenables();
SkNormalSource::InitializeFlattenables();
SkGaussianEdgeShader::InitializeFlattenables();
SkRRectsGaussianEdgeShader::InitializeFlattenables();
// PathEffect
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArcToPathEffect)