Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1191)

Unified Diff: samplecode/SampleAndroidShadows.cpp

Issue 2249973003: Add alternative ambient shadow method to Android shadow sample (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix some overlong lines Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: samplecode/SampleAndroidShadows.cpp
diff --git a/samplecode/SampleAndroidShadows.cpp b/samplecode/SampleAndroidShadows.cpp
index 9ab69a670c8f98a8d977c9eadd98fdbadaddbb07..183eae037239cb6e74ea0594ce40c850f50bcf0e 100755
--- a/samplecode/SampleAndroidShadows.cpp
+++ b/samplecode/SampleAndroidShadows.cpp
@@ -9,12 +9,15 @@
#include "SkBlurMask.h"
#include "SkBlurMaskFilter.h"
#include "SkCanvas.h"
+#include "SkGaussianEdgeShader.h"
#include "SkPath.h"
#include "SkPoint3.h"
#include "SkUtils.h"
#include "SkView.h"
#include "sk_tool_utils.h"
+////////////////////////////////////////////////////////////////////////////
+
class ShadowsView : public SampleView {
SkPath fRectPath;
SkPath fRRPath;
@@ -120,10 +123,80 @@ protected:
canvas->drawPath(path, paint);
// draw occlusion rect
+#if DRAW_OCCL_RECT
SkPaint stroke;
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setColor(SK_ColorBLUE);
canvas->drawRect(occlRect, stroke);
+#endif
+ }
+
+ void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue,
+ SkScalar ambientAlpha) {
+
+ if (ambientAlpha <= 0) {
+ return;
+ }
+
+ const SkScalar kHeightFactor = 1.f / 128.f;
+ const SkScalar kGeomFactor = 64;
+
+ SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0));
+ SkScalar radius = zValue*kHeightFactor*kGeomFactor;
+
+ // fast path
+ SkRect pathRect;
+ SkRRect pathRRect;
+ if ((path.isOval(&pathRect) && pathRect.width() == pathRect.height()) ||
+ (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) ||
+ path.isRect(&pathRect)) {
+
+ // For all of these, we outset the rect by half the radius to get our stroke shape.
+ if (path.isOval(nullptr)) {
+ pathRect.outset(0.5f*radius, 0.5f*radius);
+ pathRRect = SkRRect::MakeOval(pathRect);
+ } else if (path.isRect(nullptr)) {
+ pathRect.outset(0.5f*radius, 0.5f*radius);
+ pathRRect = SkRRect::MakeRectXY(pathRect, 0.5f*radius, 0.5f*radius);
+ } else {
+ pathRRect.outset(0.5f*radius, 0.5f*radius);
+ }
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlpha*255.999f),
+ 0, 0, 0));
+ paint.setStrokeWidth(radius);
+ paint.setStyle(SkPaint::kStroke_Style);
+
+ sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make();
robertphillips 2016/08/16 17:56:07 std::move ?
jvanverth1 2016/08/16 19:14:11 Done.
+ paint.setShader(gaussShader);
+ canvas->drawRRect(pathRRect, paint);
+ } else {
+ // occlude blur
+ SkRect occlRect;
+ GetOcclRect(path, &occlRect);
+ sk_sp<SkMaskFilter> f = SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
+ SkBlurMask::ConvertRadiusToSigma(radius),
+ occlRect,
+ SkBlurMaskFilter::kNone_BlurFlag);
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setMaskFilter(std::move(f));
+ paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlpha*255.999f),
+ 0, 0, 0));
+ canvas->drawPath(path, paint);
+
+ // draw occlusion rect
+#if DRAW_OCCL_RECT
+ SkPaint stroke;
+ stroke.setStyle(SkPaint::kStroke_Style);
+ stroke.setColor(SK_ColorBLUE);
+ canvas->drawRect(occlRect, stroke);
+#endif
+ }
+
}
void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue,
@@ -178,10 +251,12 @@ protected:
canvas->drawPath(path, paint);
// draw occlusion rect
+#if DRAW_OCCL_RECT
SkPaint stroke;
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setColor(SK_ColorRED);
- canvas->drawRect(occlRect, stroke);
+ canvas->drawRect(occlRect, stroke)
+#endif
}
void drawShadowedPath(SkCanvas* canvas, const SkPath& path, SkScalar zValue,
@@ -191,7 +266,7 @@ protected:
const SkScalar kSpotAlpha = 0.25f;
if (fShowAmbient) {
robertphillips 2016/08/16 17:56:07 Do we want to toggle between the two options ?
jvanverth1 2016/08/16 19:14:11 Done.
- this->drawAmbientShadow(canvas, path, zValue, kAmbientAlpha);
+ this->drawAmbientShadowAlt(canvas, path, zValue, kAmbientAlpha);
}
if (fShowSpot) {
this->drawSpotShadow(canvas, path, zValue, fLightPos, kLightWidth, kSpotAlpha);
@@ -218,6 +293,10 @@ protected:
paint.setColor(SK_ColorBLUE);
canvas->translate(-250, 110);
this->drawShadowedPath(canvas, fCirclePath, 5, paint);
+
+ paint.setColor(SK_ColorGREEN);
+ canvas->translate(250, 0);
+ this->drawShadowedPath(canvas, fRRPath, 5, paint);
}
protected:

Powered by Google App Engine
This is Rietveld 408576698