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