aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar robertphillips <robertphillips@google.com>2016-09-08 09:02:43 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-09-08 09:02:44 -0700
commit05a4cf59591024d838b204bb0f6fac42598ead28 (patch)
tree9744866d58622ba2e16ec391cc8859972fbd928f
parent51761d12d00fb1ebe7c63efce91b0e84b4afac5c (diff)
Add GM/slide to simulate Android-style reveal clip
Hopefully, this will let us play w/ geometric and shader-based solutions. GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2316593003 Committed: https://skia.googlesource.com/skia/+/ffac5c4aae18fc706e4077763c190a89c8507fb0 Review-Url: https://codereview.chromium.org/2316593003
-rw-r--r--gm/gm.h4
-rw-r--r--gm/reveal.cpp332
-rw-r--r--include/effects/SkGaussianEdgeShader.h3
-rw-r--r--samplecode/GMSampleView.cpp9
-rw-r--r--src/effects/SkGaussianEdgeShader.cpp27
-rw-r--r--src/gpu/glsl/GrGLSLProgramBuilder.cpp3
-rw-r--r--src/ports/SkGlobalInitialization_default.cpp3
7 files changed, 370 insertions, 11 deletions
diff --git a/gm/gm.h b/gm/gm.h
index e00a650f33..ff033a88aa 100644
--- a/gm/gm.h
+++ b/gm/gm.h
@@ -100,6 +100,9 @@ namespace skiagm {
}
bool animate(const SkAnimTimer&);
+ bool handleKey(SkUnichar uni) {
+ return this->onHandleKey(uni);
+ }
virtual void modifyGrContextOptions(GrContextOptions* options) {}
@@ -114,6 +117,7 @@ namespace skiagm {
virtual SkString onShortName() = 0;
virtual bool onAnimate(const SkAnimTimer&) { return false; }
+ virtual bool onHandleKey(SkUnichar uni) { return false; }
virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
private:
diff --git a/gm/reveal.cpp b/gm/reveal.cpp
new file mode 100644
index 0000000000..de36c0e8d4
--- /dev/null
+++ b/gm/reveal.cpp
@@ -0,0 +1,332 @@
+/*
+ * 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"
+#include "SkAnimTimer.h"
+#include "SkBlurMaskFilter.h"
+#include "SkGaussianEdgeShader.h"
+#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;
+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);
+
+ 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.addRoundRect(r, 2*kPad, 2*kPad);
+ return p;
+}
+
+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);
+
+ 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);
+ 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);
+ }
+
+ SkPath p;
+ insetRect.outset(kPad/2.0f, kPad/2.0f);
+ p.addRoundRect(insetRect, 2*kPad, 2*kPad);
+ return p;
+}
+
+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;
+ }
+ }
+
+ if (clipContainsVictim) {
+ return victim;
+ }
+
+ // 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;
+ }
+
+ // Add victim contains clip test?
+
+ SkPath clipPath;
+ clipPath.addRect(r);
+
+ SkPath result;
+ SkAssertResult(Op(clipPath, victim, kIntersect_SkPathOp, &result));
+
+ return result;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+namespace skiagm {
+
+// This GM attempts to mimic Android's reveal animation
+class RevealGM : public GM {
+public:
+ RevealGM() : fFraction(0.5f), fDrawWithGaussianEdge(true) {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+protected:
+
+ SkString onShortName() override {
+ return SkString("reveal");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize);
+ }
+
+ 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
+ };
+
+ SkPaint strokePaint;
+ strokePaint.setColor(SK_ColorGREEN);
+ 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));
+
+ cell.inset(kPad, kPad);
+ SkPoint clipCenter = SkPoint::Make(cell.centerX() - kClipOffset,
+ cell.centerY() + kClipOffset);
+
+ SkScalar curSize = kCellSize * fFraction;
+
+ // The goal is to replace this clipped draw (which clips the
+ // shadow) with a draw using the geometric clip
+ if (fDrawWithGaussianEdge) {
+ canvas->save();
+ (*clips[x])(canvas, clipCenter, curSize);
+ (*draws[y])(canvas, cell, false);
+ canvas->restore();
+ }
+
+ SkPath drawnPath = (*draws[y])(canvas, cell, true);
+
+ if (!fDrawWithGaussianEdge) {
+ SkPath clippedPath = (*geometricClips[x])(clipCenter, curSize, drawnPath);
+ SkASSERT(clippedPath.isConvex());
+
+ SkPaint blurPaint;
+ blurPaint.setAntiAlias(true);
+ blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 3.0f));
+ canvas->drawPath(clippedPath, blurPaint);
+ }
+ }
+ }
+ }
+
+ bool onHandleKey(SkUnichar uni) override {
+ switch (uni) {
+ case 'C':
+ fDrawWithGaussianEdge = !fDrawWithGaussianEdge;
+ return true;
+ }
+
+ return false;
+ }
+
+ bool onAnimate(const SkAnimTimer& timer) override {
+ fFraction = timer.pingPong(kPeriod, 0.0f, 0.0f, 1.0f);
+ return true;
+ }
+
+private:
+ SkScalar fFraction;
+ bool fDrawWithGaussianEdge;
+
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new RevealGM;)
+}
diff --git a/include/effects/SkGaussianEdgeShader.h b/include/effects/SkGaussianEdgeShader.h
index 59a2889e94..ef54ece56e 100644
--- a/include/effects/SkGaussianEdgeShader.h
+++ b/include/effects/SkGaussianEdgeShader.h
@@ -19,6 +19,9 @@ public:
static sk_sp<SkShader> Make();
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
+
+private:
+ SkGaussianEdgeShader(); // can't be instantiated
};
#endif
diff --git a/samplecode/GMSampleView.cpp b/samplecode/GMSampleView.cpp
index 80a6803148..5e48391787 100644
--- a/samplecode/GMSampleView.cpp
+++ b/samplecode/GMSampleView.cpp
@@ -26,6 +26,15 @@ bool GMSampleView::onQuery(SkEvent* evt) {
SampleCode::TitleR(evt, name.c_str());
return true;
}
+
+ SkUnichar uni;
+ if (SampleCode::CharQ(*evt, &uni)) {
+ if (fGM->handleKey(uni)) {
+ this->inval(nullptr);
+ return true;
+ }
+ }
+
return this->INHERITED::onQuery(evt);
}
diff --git a/src/effects/SkGaussianEdgeShader.cpp b/src/effects/SkGaussianEdgeShader.cpp
index d73bfadbd7..01b0606c01 100644
--- a/src/effects/SkGaussianEdgeShader.cpp
+++ b/src/effects/SkGaussianEdgeShader.cpp
@@ -67,17 +67,25 @@ public:
GLSLGaussianEdgeFP() {}
void emitCode(EmitArgs& args) override {
-
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
- fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
- fragBuilder->codeAppend("float radius = color.g*64.0;");
- fragBuilder->codeAppend("float pad = color.b*64.0;");
-
- fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad)/radius, 0.0, 1.0);",
- fragBuilder->distanceVectorName());
- fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
- fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color.r);", args.fOutputColor);
+ if (!args.fGpImplementsDistanceVector) {
+ fragBuilder->codeAppendf("// GP does not implement fsDistanceVector - "
+ " returning grey in GLSLGaussianEdgeFP\n");
+ fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
+ fragBuilder->codeAppendf("%s = vec4(0, 0, 0, color.r);", args.fOutputColor);
+ } else {
+ fragBuilder->codeAppendf("vec4 color = %s;", args.fInputColor);
+ fragBuilder->codeAppend("float radius = color.g*64.0;");
+ fragBuilder->codeAppend("float pad = color.b*64.0;");
+
+ fragBuilder->codeAppendf("float factor = 1.0 - clamp((%s.z - pad)/radius,"
+ "0.0, 1.0);",
+ fragBuilder->distanceVectorName());
+ fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
+ fragBuilder->codeAppendf("%s = factor*vec4(0.0, 0.0, 0.0, color.r);",
+ args.fOutputColor);
+ }
}
static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
@@ -133,7 +141,6 @@ sk_sp<SkFlattenable> SkGaussianEdgeShaderImpl::CreateProc(SkReadBuffer& buf) {
}
void SkGaussianEdgeShaderImpl::flatten(SkWriteBuffer& buf) const {
- this->INHERITED::flatten(buf);
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/glsl/GrGLSLProgramBuilder.cpp b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
index a6bff8a85a..3383221ebf 100644
--- a/src/gpu/glsl/GrGLSLProgramBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
@@ -89,6 +89,9 @@ void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& pr
const char* distanceVectorName = nullptr;
if (this->fPipeline.usesDistanceVectorField() && proc.implementsDistanceVector()) {
+ // Each individual user (FP) of the distance vector must be able to handle having this
+ // variable be undeclared. There is no single default value that will yield a reasonable
+ // result for all users.
distanceVectorName = fFS.distanceVectorName();
fFS.codeAppend( "// Normalized vector to the closest geometric edge (in device space)\n");
fFS.codeAppend( "// Distance to the edge encoded in the z-component\n");
diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp
index fdafd9609a..1f1973649c 100644
--- a/src/ports/SkGlobalInitialization_default.cpp
+++ b/src/ports/SkGlobalInitialization_default.cpp
@@ -24,6 +24,7 @@
#include "SkDisplacementMapEffect.h"
#include "SkDropShadowImageFilter.h"
#include "SkEmbossMaskFilter.h"
+#include "SkGaussianEdgeShader.h"
#include "SkGradientShader.h"
#include "SkImageSource.h"
#include "SkLayerDrawLooper.h"
@@ -89,7 +90,7 @@ void SkFlattenable::PrivateInitializer::InitEffects() {
SkGradientShader::InitializeFlattenables();
SkLightingShader::InitializeFlattenables();
SkNormalSource::InitializeFlattenables();
-
+ SkGaussianEdgeShader::InitializeFlattenables();
// PathEffect
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArcToPathEffect)