jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2016 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "SampleCode.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
| 11 | #include "SkBlurMaskFilter.h" |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 13 | #include "SkCamera.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 15 | #include "SkGaussianEdgeShader.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 16 | #include "SkPath.h" |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 17 | #include "SkPathOps.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 18 | #include "SkPoint3.h" |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 19 | #include "SkShadowUtils.h" |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 20 | #include "SkUtils.h" |
| 21 | #include "SkView.h" |
| 22 | #include "sk_tool_utils.h" |
| 23 | |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 24 | #define USE_SHADOW_UTILS |
| 25 | |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 26 | //////////////////////////////////////////////////////////////////////////// |
| 27 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 28 | class ShadowsView : public SampleView { |
| 29 | SkPath fRectPath; |
| 30 | SkPath fRRPath; |
| 31 | SkPath fCirclePath; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 32 | SkPath fFunkyRRPath; |
| 33 | SkPath fCubicPath; |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 34 | SkPath fSquareRRectPath; |
| 35 | SkPath fWideRectPath; |
| 36 | SkPath fWideOvalPath; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 37 | SkPoint3 fLightPos; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 38 | SkScalar fZDelta; |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 39 | SkScalar fAnimTranslate; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 40 | |
| 41 | bool fShowAmbient; |
| 42 | bool fShowSpot; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 43 | bool fUseAlt; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 44 | bool fShowObject; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 45 | bool fIgnoreShadowAlpha; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 46 | |
| 47 | public: |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 48 | ShadowsView() |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 49 | : fZDelta(0) |
| 50 | , fAnimTranslate(0) |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 51 | , fShowAmbient(true) |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 52 | , fShowSpot(true) |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 53 | , fUseAlt(true) |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 54 | , fShowObject(true) |
| 55 | , fIgnoreShadowAlpha(false) {} |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | void onOnceBeforeDraw() override { |
| 59 | fCirclePath.addCircle(0, 0, 50); |
| 60 | fRectPath.addRect(SkRect::MakeXYWH(-100, -50, 200, 100)); |
| 61 | fRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-100, -50, 200, 100), 4, 4)); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 62 | fFunkyRRPath.addRoundRect(SkRect::MakeXYWH(-50, -50, SK_Scalar1 * 100, SK_Scalar1 * 100), |
| 63 | 40 * SK_Scalar1, 20 * SK_Scalar1, |
| 64 | SkPath::kCW_Direction); |
| 65 | fCubicPath.cubicTo(100 * SK_Scalar1, 50 * SK_Scalar1, |
| 66 | 20 * SK_Scalar1, 100 * SK_Scalar1, |
| 67 | 0 * SK_Scalar1, 0 * SK_Scalar1); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 68 | fSquareRRectPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-50, -50, 100, 100), |
| 69 | 10, 10)); |
| 70 | fWideRectPath.addRect(SkRect::MakeXYWH(0, 0, 630, 70)); |
| 71 | fWideOvalPath.addOval(SkRect::MakeXYWH(0, 0, 630, 70)); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 72 | |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 73 | fLightPos = SkPoint3::Make(-700, -700, 2800); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // overrides from SkEventSink |
| 77 | bool onQuery(SkEvent* evt) override { |
| 78 | if (SampleCode::TitleQ(*evt)) { |
| 79 | SampleCode::TitleR(evt, "AndroidShadows"); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | SkUnichar uni; |
| 84 | if (SampleCode::CharQ(*evt, &uni)) { |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 85 | bool handled = false; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 86 | switch (uni) { |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 87 | case 'W': |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 88 | fShowAmbient = !fShowAmbient; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 89 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 90 | break; |
| 91 | case 'S': |
| 92 | fShowSpot = !fShowSpot; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 93 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 94 | break; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 95 | case 'T': |
| 96 | fUseAlt = !fUseAlt; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 97 | handled = true; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 98 | break; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 99 | case 'O': |
| 100 | fShowObject = !fShowObject; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 101 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 102 | break; |
| 103 | case '>': |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 104 | fZDelta += 0.5f; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 105 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 106 | break; |
| 107 | case '<': |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 108 | fZDelta -= 0.5f; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 109 | handled = true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 110 | break; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 111 | case '?': |
| 112 | fIgnoreShadowAlpha = !fIgnoreShadowAlpha; |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 113 | handled = true; |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 114 | break; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 115 | default: |
| 116 | break; |
| 117 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 118 | if (handled) { |
| 119 | this->inval(nullptr); |
| 120 | return true; |
| 121 | } |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 122 | } |
| 123 | return this->INHERITED::onQuery(evt); |
| 124 | } |
| 125 | |
| 126 | void drawBG(SkCanvas* canvas) { |
| 127 | canvas->drawColor(0xFFDDDDDD); |
| 128 | } |
| 129 | |
| 130 | static void GetOcclRect(const SkPath& path, SkRect* occlRect) { |
| 131 | SkRect pathRect; |
| 132 | SkRRect pathRRect; |
| 133 | if (path.isOval(&pathRect)) { |
| 134 | *occlRect = sk_tool_utils::compute_central_occluder(SkRRect::MakeOval(pathRect)); |
| 135 | } else if (path.isRRect(&pathRRect)) { |
| 136 | *occlRect = sk_tool_utils::compute_central_occluder(pathRRect); |
| 137 | } else if (path.isRect(occlRect)) { |
| 138 | // the inverse transform for the spot shadow occluder doesn't always get us |
| 139 | // back to exactly the same position, so deducting a little slop |
| 140 | occlRect->inset(1, 1); |
| 141 | } else { |
| 142 | *occlRect = SkRect::MakeEmpty(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void drawAmbientShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 147 | SkScalar ambientAlpha) { |
| 148 | |
| 149 | if (ambientAlpha <= 0) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | const SkScalar kHeightFactor = 1.f / 128.f; |
| 154 | const SkScalar kGeomFactor = 64; |
| 155 | |
| 156 | SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); |
| 157 | SkScalar radius = zValue*kHeightFactor*kGeomFactor; |
| 158 | |
| 159 | // occlude blur |
| 160 | SkRect occlRect; |
| 161 | GetOcclRect(path, &occlRect); |
| 162 | sk_sp<SkMaskFilter> mf = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 163 | SkBlurMask::ConvertRadiusToSigma(radius), |
| 164 | occlRect, |
| 165 | SkBlurMaskFilter::kNone_BlurFlag); |
| 166 | |
| 167 | SkPaint paint; |
| 168 | paint.setAntiAlias(true); |
| 169 | paint.setMaskFilter(std::move(mf)); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 170 | paint.setColor(SkColorSetARGB(fIgnoreShadowAlpha |
| 171 | ? 255 |
| 172 | : (unsigned char)(ambientAlpha*umbraAlpha*255.999f), 0, 0, 0)); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 173 | canvas->drawPath(path, paint); |
| 174 | |
| 175 | // draw occlusion rect |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 176 | #if DRAW_OCCL_RECT |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 177 | SkPaint stroke; |
| 178 | stroke.setStyle(SkPaint::kStroke_Style); |
| 179 | stroke.setColor(SK_ColorBLUE); |
| 180 | canvas->drawRect(occlRect, stroke); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 181 | #endif |
| 182 | } |
| 183 | |
| 184 | void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 185 | SkScalar ambientAlpha) { |
| 186 | |
| 187 | if (ambientAlpha <= 0) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | const SkScalar kHeightFactor = 1.f / 128.f; |
| 192 | const SkScalar kGeomFactor = 64; |
| 193 | |
| 194 | SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); |
| 195 | SkScalar radius = zValue*kHeightFactor*kGeomFactor; |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 196 | // distance to outer of edge of geometry from original shape edge |
| 197 | SkScalar offset = radius*umbraAlpha; |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 198 | |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 199 | SkRect pathRect; |
| 200 | SkRRect pathRRect; |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 201 | SkScalar scaleFactors[2]; |
| 202 | if (!canvas->getTotalMatrix().getMinMaxScales(scaleFactors)) { |
| 203 | return; |
| 204 | } |
| 205 | if (scaleFactors[0] != scaleFactors[1] || radius*scaleFactors[0] >= 64 || |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 206 | !((path.isOval(&pathRect) && pathRect.width() == pathRect.height()) || |
| 207 | (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) || |
| 208 | path.isRect(&pathRect))) { |
| 209 | this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); |
| 210 | return; |
| 211 | } |
| 212 | |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 213 | // For all of these, we inset the offset rect by half the radius to get our stroke shape. |
| 214 | SkScalar strokeOutset = offset - SK_ScalarHalf*radius; |
| 215 | // Make sure we'll have a radius of at least 0.5 after xform |
| 216 | if (strokeOutset*scaleFactors[0] < 0.5f) { |
| 217 | strokeOutset = 0.5f / scaleFactors[0]; |
| 218 | } |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 219 | if (path.isOval(nullptr)) { |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 220 | pathRect.outset(strokeOutset, strokeOutset); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 221 | pathRRect = SkRRect::MakeOval(pathRect); |
| 222 | } else if (path.isRect(nullptr)) { |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 223 | pathRect.outset(strokeOutset, strokeOutset); |
| 224 | pathRRect = SkRRect::MakeRectXY(pathRect, strokeOutset, strokeOutset); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 225 | } else { |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 226 | pathRRect.outset(strokeOutset, strokeOutset); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 227 | } |
| 228 | |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 229 | SkPaint paint; |
| 230 | paint.setAntiAlias(true); |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 231 | paint.setStyle(SkPaint::kStroke_Style); |
| 232 | // we outset the stroke a little to cover up AA on the interior edge |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 233 | SkScalar pad = 0.5f; |
| 234 | paint.setStrokeWidth(radius + 2*pad); |
| 235 | // handle scale of radius and pad due to CTM |
| 236 | radius *= scaleFactors[0]; |
| 237 | pad *= scaleFactors[0]; |
| 238 | SkASSERT(radius < 16384); |
| 239 | SkASSERT(pad < 64); |
| 240 | // Convert radius to 14.2 fixed point and place in the R & G components. |
| 241 | // Convert pad to 6.2 fixed point and place in the B component. |
| 242 | uint16_t iRadius = (uint16_t)(radius*4.0f); |
| 243 | unsigned char alpha = (unsigned char)(ambientAlpha*255.999f); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 244 | paint.setColor(SkColorSetARGB(fIgnoreShadowAlpha ? 255 : alpha, |
| 245 | iRadius >> 8, iRadius & 0xff, |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 246 | (unsigned char)(4.0f*pad))); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 247 | |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 248 | paint.setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorBLACK, SkBlendMode::kModulate)); |
jvanverth | c20c0c0 | 2016-09-14 07:04:49 -0700 | [diff] [blame] | 249 | paint.setShader(SkGaussianEdgeShader::Make()); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 250 | canvas->drawRRect(pathRRect, paint); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 254 | SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlpha) { |
| 255 | if (spotAlpha <= 0) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
| 260 | if (zRatio < 0.0f) { |
| 261 | zRatio = 0.0f; |
| 262 | } else if (zRatio > 0.95f) { |
| 263 | zRatio = 0.95f; |
| 264 | } |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 265 | SkScalar blurRadius = lightWidth*zRatio; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 266 | |
| 267 | // compute the transformation params |
| 268 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 269 | SkMatrix ctmInverse; |
| 270 | if (!canvas->getTotalMatrix().invert(&ctmInverse)) { |
| 271 | return; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 272 | } |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 273 | SkPoint lightPos2D = SkPoint::Make(lightPos.fX, lightPos.fY); |
| 274 | ctmInverse.mapPoints(&lightPos2D, 1); |
| 275 | SkPoint offset = SkPoint::Make(zRatio*(center.fX - lightPos2D.fX), |
| 276 | zRatio*(center.fY - lightPos2D.fY)); |
| 277 | SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 278 | |
| 279 | SkAutoCanvasRestore acr(canvas, true); |
| 280 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 281 | sk_sp<SkMaskFilter> mf = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 282 | SkBlurMask::ConvertRadiusToSigma(blurRadius), |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 283 | SkBlurMaskFilter::kNone_BlurFlag); |
| 284 | |
| 285 | SkPaint paint; |
| 286 | paint.setAntiAlias(true); |
| 287 | paint.setMaskFilter(std::move(mf)); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 288 | paint.setColor(SkColorSetARGB(fIgnoreShadowAlpha |
| 289 | ? 255 |
| 290 | : (unsigned char)(spotAlpha*255.999f), 0, 0, 0)); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 291 | |
| 292 | // apply transformation to shadow |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 293 | canvas->scale(scale, scale); |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 294 | canvas->translate(offset.fX, offset.fY); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 295 | canvas->drawPath(path, paint); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 296 | } |
| 297 | |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 298 | void drawSpotShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 299 | SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlpha) { |
| 300 | if (spotAlpha <= 0) { |
| 301 | return; |
| 302 | } |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 303 | |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 304 | SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
| 305 | if (zRatio < 0.0f) { |
| 306 | zRatio = 0.0f; |
| 307 | } else if (zRatio > 0.95f) { |
| 308 | zRatio = 0.95f; |
| 309 | } |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 310 | SkScalar radius = 2.0f*lightWidth*zRatio; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 311 | |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 312 | SkRect pathRect; |
| 313 | SkRRect pathRRect; |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 314 | SkScalar scaleFactors[2]; |
| 315 | if (!canvas->getTotalMatrix().getMinMaxScales(scaleFactors)) { |
| 316 | return; |
| 317 | } |
| 318 | if (scaleFactors[0] != scaleFactors[1] || radius*scaleFactors[0] >= 16384 || |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 319 | !((path.isOval(&pathRect) && pathRect.width() == pathRect.height()) || |
| 320 | (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) || |
| 321 | path.isRect(&pathRect))) { |
| 322 | this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spotAlpha); |
| 323 | return; |
| 324 | } |
| 325 | |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 326 | // For all of these, we need to ensure we have a rrect with radius >= 0.5f in device space |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 327 | const SkScalar minRadius = SK_ScalarHalf/scaleFactors[0]; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 328 | if (path.isOval(nullptr)) { |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 329 | pathRRect = SkRRect::MakeOval(pathRect); |
| 330 | } else if (path.isRect(nullptr)) { |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 331 | pathRRect = SkRRect::MakeRectXY(pathRect, minRadius, minRadius); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 332 | } else { |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 333 | if (pathRRect.getSimpleRadii().fX < minRadius) { |
| 334 | pathRRect.setRectXY(pathRRect.rect(), minRadius, minRadius); |
| 335 | } |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 336 | } |
| 337 | |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 338 | // compute the scale and translation for the shadow |
| 339 | SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); |
| 340 | SkRRect shadowRRect; |
| 341 | pathRRect.transform(SkMatrix::MakeScale(scale, scale), &shadowRRect); |
| 342 | SkPoint center = SkPoint::Make(shadowRRect.rect().centerX(), shadowRRect.rect().centerY()); |
| 343 | SkMatrix ctmInverse; |
| 344 | if (!canvas->getTotalMatrix().invert(&ctmInverse)) { |
| 345 | return; |
| 346 | } |
| 347 | SkPoint lightPos2D = SkPoint::Make(lightPos.fX, lightPos.fY); |
| 348 | ctmInverse.mapPoints(&lightPos2D, 1); |
| 349 | SkPoint offset = SkPoint::Make(zRatio*(center.fX - lightPos2D.fX), |
| 350 | zRatio*(center.fY - lightPos2D.fY)); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 351 | SkAutoCanvasRestore acr(canvas, true); |
| 352 | |
| 353 | SkPaint paint; |
| 354 | paint.setAntiAlias(true); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 355 | // We want to extend the stroked area in so that it meets up with the caster |
| 356 | // geometry. The stroked geometry will, by definition already be inset half the |
| 357 | // stroke width but we also have to account for the scaling. |
| 358 | // We also add 1/2 to cover up AA on the interior edge. |
| 359 | SkScalar scaleOffset = (scale - 1.0f) * SkTMax(SkTMax(SkTAbs(pathRect.fLeft), |
| 360 | SkTAbs(pathRect.fRight)), |
| 361 | SkTMax(SkTAbs(pathRect.fTop), |
| 362 | SkTAbs(pathRect.fBottom))); |
| 363 | SkScalar insetAmount = offset.length() - (0.5f * radius) + scaleOffset + 0.5f; |
| 364 | |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 365 | // compute area |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 366 | SkScalar strokeWidth = radius + insetAmount; |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 367 | SkScalar strokedArea = 2.0f*strokeWidth*(shadowRRect.width() + shadowRRect.height()); |
| 368 | SkScalar filledArea = (shadowRRect.height() + radius)*(shadowRRect.width() + radius); |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 369 | // If the area of the stroked geometry is larger than the fill geometry, or |
| 370 | // if our pad is too big to convert to 6.2 fixed point, just fill it. |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 371 | if (strokedArea > filledArea) { |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 372 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 373 | paint.setStrokeWidth(radius); |
| 374 | } else { |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 375 | // Since we can't have unequal strokes, inset the shadow rect so the inner |
| 376 | // and outer edges of the stroke will land where we want. |
| 377 | SkRect insetRect = shadowRRect.rect().makeInset(insetAmount/2.0f, insetAmount/2.0f); |
| 378 | SkScalar insetRad = SkTMax(shadowRRect.getSimpleRadii().fX - insetAmount/2.0f, |
| 379 | minRadius); |
| 380 | |
| 381 | shadowRRect = SkRRect::MakeRectXY(insetRect, insetRad, insetRad); |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 382 | paint.setStyle(SkPaint::kStroke_Style); |
| 383 | paint.setStrokeWidth(strokeWidth); |
| 384 | } |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 385 | paint.setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorBLACK, SkBlendMode::kModulate)); |
jvanverth | c20c0c0 | 2016-09-14 07:04:49 -0700 | [diff] [blame] | 386 | paint.setShader(SkGaussianEdgeShader::Make()); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 387 | // handle scale of radius due to CTM |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 388 | radius *= scaleFactors[0]; |
| 389 | // don't need to scale pad as it was computed from the transformed offset |
| 390 | SkASSERT(radius < 16384); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 391 | SkScalar pad = 0; |
jvanverth | a4f1af8 | 2016-08-29 07:17:47 -0700 | [diff] [blame] | 392 | SkASSERT(pad < 64); |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 393 | // Convert radius to 14.2 fixed point and place in the R & G components. |
| 394 | // Convert pad to 6.2 fixed point and place in the B component. |
| 395 | uint16_t iRadius = (uint16_t)(radius*4.0f); |
| 396 | unsigned char alpha = (unsigned char)(spotAlpha*255.999f); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 397 | paint.setColor(SkColorSetARGB(fIgnoreShadowAlpha ? 255 : alpha, |
| 398 | iRadius >> 8, iRadius & 0xff, |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 399 | (unsigned char)(4.0f*pad))); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 400 | |
| 401 | // apply transformation to shadow |
| 402 | canvas->translate(offset.fX, offset.fY); |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 403 | canvas->drawRRect(shadowRRect, paint); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void drawShadowedPath(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 407 | const SkPaint& paint, SkScalar ambientAlpha, |
| 408 | const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha) { |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 409 | #ifdef USE_SHADOW_UTILS |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 410 | if (fUseAlt) { |
| 411 | if (fShowAmbient) { |
| 412 | this->drawAmbientShadowAlt(canvas, path, zValue, ambientAlpha); |
| 413 | } |
| 414 | if (fShowSpot) { |
| 415 | this->drawSpotShadowAlt(canvas, path, zValue, lightPos, lightWidth, spotAlpha); |
| 416 | } |
| 417 | } else { |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 418 | if (!fShowAmbient) { |
| 419 | ambientAlpha = 0; |
| 420 | } |
| 421 | if (!fShowSpot) { |
| 422 | spotAlpha = 0; |
| 423 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 424 | |
| 425 | SkShadowUtils::DrawShadow(canvas, path, |
| 426 | zValue, |
| 427 | lightPos, lightWidth, |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 428 | ambientAlpha, spotAlpha, SK_ColorBLACK); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 429 | //SkShadowUtils::DrawUncachedShadow(canvas, path, |
| 430 | // [zValue](SkScalar, SkScalar) { return zValue; }, |
| 431 | // lightPos, lightWidth, |
| 432 | // ambientAlpha, spotAlpha, SK_ColorBLACK); |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 433 | } |
| 434 | #else |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 435 | if (fShowAmbient) { |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 436 | if (fUseAlt) { |
| 437 | this->drawAmbientShadowAlt(canvas, path, zValue, ambientAlpha); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 438 | } else { |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 439 | this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 440 | } |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 441 | } |
| 442 | if (fShowSpot) { |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 443 | if (fUseAlt) { |
| 444 | this->drawSpotShadowAlt(canvas, path, zValue, lightPos, lightWidth, spotAlpha); |
| 445 | } else { |
| 446 | this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spotAlpha); |
| 447 | } |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 448 | } |
Jim Van Verth | 3c1b7db | 2016-10-31 16:06:51 -0400 | [diff] [blame] | 449 | #endif |
| 450 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 451 | if (fShowObject) { |
| 452 | canvas->drawPath(path, paint); |
Robert Phillips | 95304e3 | 2016-10-07 14:44:07 -0400 | [diff] [blame] | 453 | } else { |
| 454 | SkPaint strokePaint; |
| 455 | |
| 456 | strokePaint.setColor(paint.getColor()); |
| 457 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 458 | |
| 459 | canvas->drawPath(path, strokePaint); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | void onDrawContent(SkCanvas* canvas) override { |
| 464 | this->drawBG(canvas); |
jvanverth | d99858a | 2016-09-12 07:51:04 -0700 | [diff] [blame] | 465 | const SkScalar kLightWidth = 2800; |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 466 | const SkScalar kAmbientAlpha = 0.25f; |
| 467 | const SkScalar kSpotAlpha = 0.25f; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 468 | |
| 469 | SkPaint paint; |
| 470 | paint.setAntiAlias(true); |
| 471 | |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 472 | SkPoint3 lightPos = fLightPos; |
| 473 | |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 474 | paint.setColor(SK_ColorWHITE); |
| 475 | canvas->translate(200, 90); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 476 | lightPos.fX += 200; |
| 477 | lightPos.fY += 90; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 478 | this->drawShadowedPath(canvas, fRRPath, SkTMax(1.0f, 2+fZDelta), paint, kAmbientAlpha, |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 479 | lightPos, kLightWidth, kSpotAlpha); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 480 | |
| 481 | paint.setColor(SK_ColorRED); |
| 482 | canvas->translate(250, 0); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 483 | lightPos.fX += 250; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 484 | this->drawShadowedPath(canvas, fRectPath, SkTMax(1.0f, 4+fZDelta), paint, kAmbientAlpha, |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 485 | lightPos, kLightWidth, kSpotAlpha); |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 486 | |
| 487 | paint.setColor(SK_ColorBLUE); |
| 488 | canvas->translate(-250, 110); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 489 | lightPos.fX -= 250; |
| 490 | lightPos.fY += 110; |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 491 | this->drawShadowedPath(canvas, fCirclePath, SkTMax(1.0f, 8+fZDelta), paint, kAmbientAlpha, |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 492 | lightPos, kLightWidth, 0.5f); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 493 | |
| 494 | paint.setColor(SK_ColorGREEN); |
| 495 | canvas->translate(250, 0); |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 496 | lightPos.fX += 250; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 497 | this->drawShadowedPath(canvas, fRRPath, SkTMax(1.0f, 64+fZDelta), paint, kAmbientAlpha, |
jvanverth | d7315f91 | 2016-08-17 10:06:18 -0700 | [diff] [blame] | 498 | lightPos, kLightWidth, kSpotAlpha); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 499 | |
| 500 | paint.setColor(SK_ColorYELLOW); |
| 501 | canvas->translate(-250, 110); |
| 502 | lightPos.fX -= 250; |
| 503 | lightPos.fY += 110; |
Jim Van Verth | 2aa535e | 2017-02-06 14:36:01 -0500 | [diff] [blame] | 504 | this->drawShadowedPath(canvas, fFunkyRRPath, SkTMax(1.0f, 8+fZDelta), paint, kAmbientAlpha, |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 505 | lightPos, kLightWidth, kSpotAlpha); |
| 506 | |
| 507 | paint.setColor(SK_ColorCYAN); |
| 508 | canvas->translate(250, 0); |
| 509 | lightPos.fX += 250; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 510 | this->drawShadowedPath(canvas, fCubicPath, SkTMax(1.0f, 16 + fZDelta), paint, |
| 511 | kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 512 | |
| 513 | // circular reveal |
| 514 | SkPath tmpPath; |
| 515 | SkPath tmpClipPath; |
| 516 | tmpClipPath.addCircle(fAnimTranslate, 0, 60); |
| 517 | Op(fSquareRRectPath, tmpClipPath, kIntersect_SkPathOp, &tmpPath); |
| 518 | |
| 519 | paint.setColor(SK_ColorMAGENTA); |
| 520 | canvas->translate(-125, 60); |
| 521 | lightPos.fX -= 125; |
| 522 | lightPos.fY += 60; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 523 | this->drawShadowedPath(canvas, tmpPath, SkTMax(1.0f, 32 + fZDelta), paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 524 | lightPos, kLightWidth, .5f); |
| 525 | |
| 526 | // perspective paths |
| 527 | SkPoint pivot = SkPoint::Make(fWideRectPath.getBounds().width()/2, |
| 528 | fWideRectPath.getBounds().height()/2); |
| 529 | SkPoint translate = SkPoint::Make(50, 450); |
| 530 | paint.setColor(SK_ColorWHITE); |
| 531 | Sk3DView view; |
| 532 | view.save(); |
| 533 | view.rotateX(10); |
| 534 | SkMatrix persp; |
| 535 | view.getMatrix(&persp); |
| 536 | persp.preTranslate(-pivot.fX, -pivot.fY); |
| 537 | persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY); |
| 538 | canvas->setMatrix(persp); |
| 539 | lightPos = fLightPos; |
| 540 | lightPos.fX += pivot.fX + translate.fX; |
| 541 | lightPos.fY += pivot.fY + translate.fY; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 542 | this->drawShadowedPath(canvas, fWideRectPath, SkTMax(1.0f, 16 + fZDelta), paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 543 | lightPos, kLightWidth, .5f); |
| 544 | |
| 545 | pivot = SkPoint::Make(fWideOvalPath.getBounds().width() / 2, |
| 546 | fWideOvalPath.getBounds().height() / 2); |
| 547 | translate = SkPoint::Make(50, 600); |
| 548 | view.restore(); |
| 549 | view.rotateY(10); |
| 550 | view.getMatrix(&persp); |
| 551 | persp.preTranslate(-pivot.fX, -pivot.fY); |
| 552 | persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY); |
| 553 | canvas->setMatrix(persp); |
| 554 | lightPos = fLightPos; |
| 555 | lightPos.fX += pivot.fX + translate.fX; |
| 556 | lightPos.fY += pivot.fY + translate.fY; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 557 | this->drawShadowedPath(canvas, fWideOvalPath, SkTMax(1.0f, 32 + fZDelta), paint, .1f, |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 558 | lightPos, kLightWidth, .5f); |
| 559 | } |
| 560 | |
| 561 | bool onAnimate(const SkAnimTimer& timer) override { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 562 | fAnimTranslate = timer.pingPong(30, 0, 200, -200); |
Jim Van Verth | 2c1cd6d | 2017-03-10 14:34:51 -0500 | [diff] [blame] | 563 | |
| 564 | return true; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | protected: |
| 568 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
| 569 | return new SkView::Click(this); |
| 570 | } |
| 571 | |
| 572 | bool onClick(Click *click) override { |
| 573 | SkScalar x = click->fCurr.fX; |
| 574 | SkScalar y = click->fCurr.fY; |
| 575 | |
| 576 | SkScalar dx = x - click->fPrev.fX; |
| 577 | SkScalar dy = y - click->fPrev.fY; |
| 578 | |
| 579 | if (dx != 0 || dy != 0) { |
| 580 | fLightPos.fX += dx; |
| 581 | fLightPos.fY += dy; |
| 582 | this->inval(nullptr); |
| 583 | } |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | private: |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 589 | typedef SampleView INHERITED; |
jvanverth | e1a3bc6 | 2016-08-12 10:40:38 -0700 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | ////////////////////////////////////////////////////////////////////////////// |
| 593 | |
| 594 | static SkView* MyFactory() { return new ShadowsView; } |
| 595 | static SkViewRegister reg(MyFactory); |