Chromium Code Reviews| Index: samplecode/SampleAndroidShadows.cpp |
| diff --git a/samplecode/SampleAndroidShadows.cpp b/samplecode/SampleAndroidShadows.cpp |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..6d502f1a517c4063fd1cc3479eec1e5fda61ec7e |
| --- /dev/null |
| +++ b/samplecode/SampleAndroidShadows.cpp |
| @@ -0,0 +1,242 @@ |
| + |
| +/* |
|
robertphillips
2016/08/12 15:55:01
time warp
jvanverth1
2016/08/12 16:43:43
Done.
|
| + * Copyright 2011 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| +#include "SampleCode.h" |
| +#include "SkBlurMask.h" |
| +#include "SkBlurMaskFilter.h" |
| +#include "SkCanvas.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; |
| + SkPath fCirclePath; |
|
robertphillips
2016/08/12 15:55:01
Do we use these last 3 ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + SkPath fOvalPath; |
| + SkPath fConvexPath; |
| + SkPath fConcavePath; |
| + SkPoint3 fLightPos; |
| + |
| + bool fShowAmbient; |
| + bool fShowSpot; |
| + bool fShowObject; |
| + |
| +public: |
| + ShadowsView() {} |
| + |
| +protected: |
| + void onOnceBeforeDraw() override { |
| + fCirclePath.addCircle(0, 0, 50); |
| + fRectPath.addRect(SkRect::MakeXYWH(-100, -50, 200, 100)); |
| + fRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-100, -50, 200, 100), 4, 4)); |
| + fLightPos = SkPoint3::Make(-220, -330, 256); |
|
robertphillips
2016/08/12 15:55:01
I feel we should init the bools in the ctor
jvanverth1
2016/08/12 16:43:43
Done.
|
| + fShowAmbient = fShowSpot = fShowObject = true; |
| + } |
| + |
| + // overrides from SkEventSink |
| + bool onQuery(SkEvent* evt) override { |
| + if (SampleCode::TitleQ(*evt)) { |
| + SampleCode::TitleR(evt, "AndroidShadows"); |
| + return true; |
| + } |
| + |
| + SkUnichar uni; |
| + if (SampleCode::CharQ(*evt, &uni)) { |
| + switch (uni) { |
| + case 'B': |
| + fShowAmbient = !fShowAmbient; |
| + break; |
| + case 'S': |
| + fShowSpot = !fShowSpot; |
| + break; |
| + case 'O': |
| + fShowObject = !fShowObject; |
| + break; |
| + default: |
| + break; |
| + } |
| + this->inval(nullptr); |
| + } |
| + return this->INHERITED::onQuery(evt); |
| + } |
| + |
| + void drawBG(SkCanvas* canvas) { |
| + canvas->drawColor(0xFFDDDDDD); |
| + } |
| + |
|
robertphillips
2016/08/12 15:55:01
move out of class or make GetOcclRect ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + static void get_occl_rect(const SkPath& path, SkRect* occlRect) { |
| + SkRect pathRect; |
| + SkRRect pathRRect; |
| + if (path.isOval(&pathRect)) { |
| + *occlRect = sk_tool_utils::compute_central_occluder(SkRRect::MakeOval(pathRect)); |
| + } else if (path.isRRect(&pathRRect)) { |
| + *occlRect = sk_tool_utils::compute_central_occluder(pathRRect); |
| + } else if (!path.isRect(occlRect)) { |
| + *occlRect = SkRect::MakeEmpty(); |
| + } |
| + } |
| + |
| + void drawAmbientShadow(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; |
| + |
| + // occlude blur |
| + SkRect occlRect; |
| + get_occl_rect(path, &occlRect); |
| + sk_sp<SkMaskFilter> mf = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| + SkBlurMask::ConvertRadiusToSigma(radius), |
| + occlRect, |
| + SkBlurMaskFilter::kNone_BlurFlag); |
| + |
| + SkPaint paint; |
| + paint.setAntiAlias(true); |
|
robertphillips
2016/08/12 15:55:01
std::move(mf) ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + paint.setMaskFilter(mf); |
| + paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlpha*255.999f), 0, 0, 0)); |
| + canvas->drawPath(path, paint); |
| + |
| + // draw occlusion rect |
| + SkPaint stroke; |
| + stroke.setStyle(SkPaint::kStroke_Style); |
| + stroke.setColor(SK_ColorBLUE); |
| + canvas->drawRect(occlRect, stroke); |
| + } |
| + |
| + void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| + SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlpha) { |
| + if (spotAlpha <= 0) { |
| + return; |
| + } |
| + |
| + SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
| + if (zRatio < 0.0f) { |
| + zRatio = 0.0f; |
| + } else if (zRatio > 0.95f) { |
| + zRatio = 0.95f; |
| + } |
| + SkScalar radius = lightWidth*zRatio; |
| + |
| + // compute the transformation params |
| + SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
| + canvas->getTotalMatrix().mapPoints(¢er, 1); |
| + SkPoint offset = SkPoint::Make(-zRatio*(lightPos.fX - center.fX), |
| + -zRatio*(lightPos.fY - center.fY)); |
| + SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); |
| + if (scale < 1.0f) { |
| + scale = 1.0f; |
| + } else if (scale > 1024.f) { |
| + scale = 1024.f; |
| + } |
| + |
| + SkAutoCanvasRestore acr(canvas, true); |
| + |
| + SkRect occlRect; |
| + get_occl_rect(path, &occlRect); |
|
robertphillips
2016/08/12 15:55:01
Is this explicit xform necessary? occlRect should
jvanverth1
2016/08/12 16:43:43
Yes, it is (though the xform needs to be scale, th
|
| + // apply inverse transform |
| + occlRect.offset(-offset); |
| + occlRect.fLeft /= scale; |
| + occlRect.fRight /= scale; |
| + occlRect.fTop /= scale; |
| + occlRect.fBottom /= scale; |
| + sk_sp<SkMaskFilter> mf = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| + SkBlurMask::ConvertRadiusToSigma(radius), |
| + occlRect, |
| + SkBlurMaskFilter::kNone_BlurFlag); |
| + |
| + SkPaint paint; |
| + paint.setAntiAlias(true); |
|
robertphillips
2016/08/12 15:55:01
here too ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + paint.setMaskFilter(mf); |
| + paint.setColor(SkColorSetARGB((unsigned char)(spotAlpha*255.999f), 0, 0, 0)); |
| + |
| + // apply transformation to shadow |
| + canvas->scale(scale, scale); |
| + canvas->translate(offset.fX, offset.fY); |
| + canvas->drawPath(path, paint); |
| + |
| + // draw occlusion rect |
| + SkPaint stroke; |
| + stroke.setStyle(SkPaint::kStroke_Style); |
| + stroke.setColor(SK_ColorRED); |
| + canvas->drawRect(occlRect, stroke); |
| + } |
| + |
| + void drawShadowedPath(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| + const SkPaint& paint) { |
| + const SkScalar kLightWidth = 3; |
| + const SkScalar kAmbientAlpha = 0.25f; |
| + const SkScalar kSpotAlpha = 0.25f; |
| + |
| + if (fShowAmbient) { |
|
robertphillips
2016/08/12 15:55:01
this-> ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + drawAmbientShadow(canvas, path, zValue, kAmbientAlpha); |
| + } |
| + if (fShowSpot) { |
|
robertphillips
2016/08/12 15:55:01
Here too ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + drawSpotShadow(canvas, path, zValue, fLightPos, kLightWidth, kSpotAlpha); |
| + } |
| + if (fShowObject) { |
| + canvas->drawPath(path, paint); |
| + } |
| + } |
| + |
| + void onDrawContent(SkCanvas* canvas) override { |
|
robertphillips
2016/08/12 15:55:01
this-> ?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + drawBG(canvas); |
| + |
| + SkPaint paint; |
| + paint.setAntiAlias(true); |
| + |
| + paint.setColor(SK_ColorBLUE); |
| + canvas->translate(200, 200); |
|
robertphillips
2016/08/12 15:55:01
?
jvanverth1
2016/08/12 16:43:43
Done.
|
| + drawShadowedPath(canvas, fCirclePath, 5, paint); |
| + |
| + paint.setColor(SK_ColorWHITE); |
| + canvas->translate(0, -110); |
| + drawShadowedPath(canvas, fRectPath, 5, paint); |
| + |
| + paint.setColor(SK_ColorRED); |
| + canvas->translate(250, 0); |
| + drawShadowedPath(canvas, fRRPath, 5, paint); |
| + } |
| + |
| +protected: |
| + SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
| + return new SkView::Click(this); |
| + } |
| + |
| + bool onClick(Click *click) override { |
| + SkScalar x = click->fCurr.fX; |
| + SkScalar y = click->fCurr.fY; |
| + |
| + SkScalar dx = x - click->fPrev.fX; |
| + SkScalar dy = y - click->fPrev.fY; |
| + |
| + if (dx != 0 || dy != 0) { |
| + fLightPos.fX += dx; |
| + fLightPos.fY += dy; |
| + this->inval(nullptr); |
| + } |
| + |
| + return true; |
| + } |
| + |
| +private: |
| + typedef SkView INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +static SkView* MyFactory() { return new ShadowsView; } |
| +static SkViewRegister reg(MyFactory); |