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