| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2016 Google Inc. | 3 * Copyright 2016 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkBlurMask.h" | 9 #include "SkBlurMask.h" |
| 10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 #endif | 136 #endif |
| 137 } | 137 } |
| 138 | 138 |
| 139 void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zVa
lue, | 139 void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zVa
lue, |
| 140 SkScalar ambientAlpha) { | 140 SkScalar ambientAlpha) { |
| 141 | 141 |
| 142 if (ambientAlpha <= 0) { | 142 if (ambientAlpha <= 0) { |
| 143 return; | 143 return; |
| 144 } | 144 } |
| 145 | 145 |
| 146 SkRect pathRect; | |
| 147 SkRRect pathRRect; | |
| 148 if ((!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) &
& | |
| 149 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
| 150 !path.isRect(&pathRect)) { | |
| 151 this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 const SkScalar kHeightFactor = 1.f / 128.f; | 146 const SkScalar kHeightFactor = 1.f / 128.f; |
| 156 const SkScalar kGeomFactor = 64; | 147 const SkScalar kGeomFactor = 64; |
| 157 | 148 |
| 158 SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); | 149 SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); |
| 159 SkScalar radius = zValue*kHeightFactor*kGeomFactor; | 150 SkScalar radius = zValue*kHeightFactor*kGeomFactor; |
| 160 | 151 |
| 161 // For all of these, we outset the rect by the radius to get our coverag
e shape. | 152 SkRect pathRect; |
| 153 SkRRect pathRRect; |
| 154 if (radius >= 64 || |
| 155 !((path.isOval(&pathRect) && pathRect.width() == pathRect.height())
|| |
| 156 (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) || |
| 157 path.isRect(&pathRect))) { |
| 158 this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); |
| 159 return; |
| 160 } |
| 161 |
| 162 // For all of these, we outset the rect by half the radius to get our st
roke shape. |
| 163 SkScalar halfRadius = SK_ScalarHalf*radius; |
| 162 if (path.isOval(nullptr)) { | 164 if (path.isOval(nullptr)) { |
| 163 pathRect.outset(radius, radius); | 165 pathRect.outset(halfRadius, halfRadius); |
| 164 pathRRect = SkRRect::MakeOval(pathRect); | 166 pathRRect = SkRRect::MakeOval(pathRect); |
| 165 } else if (path.isRect(nullptr)) { | 167 } else if (path.isRect(nullptr)) { |
| 166 pathRect.outset(radius, radius); | 168 pathRect.outset(halfRadius, halfRadius); |
| 167 pathRRect = SkRRect::MakeRectXY(pathRect, radius, radius); | 169 pathRRect = SkRRect::MakeRectXY(pathRect, halfRadius, halfRadius); |
| 168 } else { | 170 } else { |
| 169 pathRRect.outset(radius, radius); | 171 pathRRect.outset(halfRadius, halfRadius); |
| 170 } | 172 } |
| 171 | 173 |
| 172 SkPaint paint; | 174 SkPaint paint; |
| 173 paint.setAntiAlias(true); | 175 paint.setAntiAlias(true); |
| 176 paint.setStyle(SkPaint::kStroke_Style); |
| 177 // we outset the stroke a little to cover up AA on the interior edge |
| 178 paint.setStrokeWidth(radius + 1); |
| 174 // handle scale of radius due to CTM | 179 // handle scale of radius due to CTM |
| 175 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); | 180 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); |
| 176 radius *= maxScale; | 181 radius *= maxScale; |
| 177 unsigned char gray = (unsigned char)(ambientAlpha*umbraAlpha*255.999f); | 182 unsigned char gray = (unsigned char)(ambientAlpha*umbraAlpha*255.999f); |
| 178 SkASSERT(radius < 256); | 183 SkASSERT(radius < 64); |
| 179 // convert the radius to fixed point 8.8 and | 184 // Convert radius to 6.2 fixed point and place in the G component. |
| 180 // place it in the G,B components of the color | 185 paint.setColor(SkColorSetARGB(1, gray, (unsigned char)(4.0f*radius), 0))
; |
| 181 unsigned char intPart = (unsigned char)radius; | |
| 182 SkScalar fracPart = radius - intPart; | |
| 183 paint.setColor(SkColorSetARGB(1, gray, intPart, (unsigned char)(fracPart
*256.f))); | |
| 184 | 186 |
| 185 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); | 187 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); |
| 186 paint.setShader(gaussShader); | 188 paint.setShader(gaussShader); |
| 187 canvas->drawRRect(pathRRect, paint); | 189 canvas->drawRRect(pathRRect, paint); |
| 188 } | 190 } |
| 189 | 191 |
| 190 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, | 192 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 191 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp
ha) { | 193 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp
ha) { |
| 192 if (spotAlpha <= 0) { | 194 if (spotAlpha <= 0) { |
| 193 return; | 195 return; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 canvas->drawRect(occlRect, stroke) | 260 canvas->drawRect(occlRect, stroke) |
| 259 #endif | 261 #endif |
| 260 } | 262 } |
| 261 | 263 |
| 262 void drawSpotShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue
, | 264 void drawSpotShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue
, |
| 263 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp
ha) { | 265 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp
ha) { |
| 264 if (spotAlpha <= 0) { | 266 if (spotAlpha <= 0) { |
| 265 return; | 267 return; |
| 266 } | 268 } |
| 267 | 269 |
| 268 SkRect pathRect; | |
| 269 SkRRect pathRRect; | |
| 270 if ((!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) &
& | |
| 271 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
| 272 !path.isRect(&pathRect)) { | |
| 273 this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spo
tAlpha); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 SkScalar zRatio = zValue / (lightPos.fZ - zValue); | 270 SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
| 278 if (zRatio < 0.0f) { | 271 if (zRatio < 0.0f) { |
| 279 zRatio = 0.0f; | 272 zRatio = 0.0f; |
| 280 } else if (zRatio > 0.95f) { | 273 } else if (zRatio > 0.95f) { |
| 281 zRatio = 0.95f; | 274 zRatio = 0.95f; |
| 282 } | 275 } |
| 283 SkScalar radius = lightWidth*zRatio; | 276 SkScalar radius = lightWidth*zRatio; |
| 284 | 277 |
| 285 // For all of these, we outset the rect by the radius to get our coverag
e shape. | 278 SkRect pathRect; |
| 279 SkRRect pathRRect; |
| 280 if (radius >= 64 || |
| 281 !((path.isOval(&pathRect) && pathRect.width() == pathRect.height())
|| |
| 282 (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) || |
| 283 path.isRect(&pathRect))) { |
| 284 this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spo
tAlpha); |
| 285 return; |
| 286 } |
| 287 |
| 288 // For all of these, we outset the rect by half the radius to get our st
roke shape. |
| 289 SkScalar halfRadius = SK_ScalarHalf*radius; |
| 286 if (path.isOval(nullptr)) { | 290 if (path.isOval(nullptr)) { |
| 287 pathRect.outset(radius, radius); | 291 pathRect.outset(halfRadius, halfRadius); |
| 288 pathRRect = SkRRect::MakeOval(pathRect); | 292 pathRRect = SkRRect::MakeOval(pathRect); |
| 289 } else if (path.isRect(nullptr)) { | 293 } else if (path.isRect(nullptr)) { |
| 290 pathRect.outset(radius, radius); | 294 pathRect.outset(halfRadius, halfRadius); |
| 291 pathRRect = SkRRect::MakeRectXY(pathRect, radius, radius); | 295 pathRRect = SkRRect::MakeRectXY(pathRect, halfRadius, halfRadius); |
| 292 } else { | 296 } else { |
| 293 pathRRect.outset(radius, radius); | 297 pathRRect.outset(halfRadius, halfRadius); |
| 294 } | 298 } |
| 295 | 299 |
| 296 // compute the transformation params | 300 // compute the transformation params |
| 297 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBound
s().centerY()); | 301 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBound
s().centerY()); |
| 298 canvas->getTotalMatrix().mapPoints(¢er, 1); | 302 canvas->getTotalMatrix().mapPoints(¢er, 1); |
| 299 SkPoint offset = SkPoint::Make(-zRatio*(lightPos.fX - center.fX), | 303 SkPoint offset = SkPoint::Make(-zRatio*(lightPos.fX - center.fX), |
| 300 -zRatio*(lightPos.fY - center.fY)); | 304 -zRatio*(lightPos.fY - center.fY)); |
| 301 SkAutoCanvasRestore acr(canvas, true); | 305 SkAutoCanvasRestore acr(canvas, true); |
| 302 | 306 |
| 303 SkPaint paint; | 307 SkPaint paint; |
| 304 paint.setAntiAlias(true); | 308 paint.setAntiAlias(true); |
| 309 // We outset the stroke by the length of the translation so the shadow e
xtends to |
| 310 // the edge of the shape. We also add 1/2 to cover up AA on the interior
edge. |
| 311 SkScalar pad = offset.length() + 0.5f; |
| 312 // compute area |
| 313 SkScalar strokeWidth = radius + 2.0f*pad; |
| 314 SkScalar strokedArea = 2.0f*strokeWidth*(pathRRect.width() + pathRRect.h
eight()); |
| 315 SkScalar filledArea = (pathRRect.height() + radius)*(pathRRect.width() +
radius); |
| 316 // If the area of the stroked geometry is larger than the fill geometry,
or |
| 317 // if our pad is too big to convert to 6.2 fixed point, just fill it. |
| 318 if (strokedArea > filledArea || pad >= 64) { |
| 319 pad = 0; |
| 320 paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 321 paint.setStrokeWidth(radius); |
| 322 } else { |
| 323 paint.setStyle(SkPaint::kStroke_Style); |
| 324 paint.setStrokeWidth(strokeWidth); |
| 325 } |
| 305 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); | 326 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); |
| 306 paint.setShader(gaussShader); | 327 paint.setShader(gaussShader); |
| 307 // handle scale of radius due to CTM | 328 // handle scale of radius due to CTM |
| 308 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); | 329 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); |
| 309 radius *= maxScale; | 330 radius *= maxScale; |
| 310 unsigned char gray = (unsigned char)(spotAlpha*255.999f); | 331 unsigned char gray = (unsigned char)(spotAlpha*255.999f); |
| 311 SkASSERT(radius < 256); | 332 SkASSERT(radius < 64); |
| 312 // convert the radius to fixed point 8.8 and | 333 SkASSERT(pad < 64); |
| 313 // place it in the G,B components of the color | 334 // Convert radius and pad to 6.2 fixed point and place in the G & B comp
onents. |
| 314 unsigned char intPart = (unsigned char)radius; | 335 paint.setColor(SkColorSetARGB(1, gray, (unsigned char)(radius*4.0f), |
| 315 SkScalar fracPart = radius - intPart; | 336 (unsigned char)(pad*4.0f))); |
| 316 paint.setColor(SkColorSetARGB(1, gray, intPart, (unsigned char)(fracPart
*256.f))); | |
| 317 | 337 |
| 318 // apply transformation to shadow | 338 // apply transformation to shadow |
| 319 canvas->translate(offset.fX, offset.fY); | 339 canvas->translate(offset.fX, offset.fY); |
| 320 #if 0 | 340 #if 0 |
| 321 // It looks like the scale may be invalid | 341 // It looks like the scale may be invalid |
| 322 SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); | 342 SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); |
| 323 if (scale < 1.0f) { | 343 if (scale < 1.0f) { |
| 324 scale = 1.0f; | 344 scale = 1.0f; |
| 325 } else if (scale > 1024.f) { | 345 } else if (scale > 1024.f) { |
| 326 scale = 1024.f; | 346 scale = 1024.f; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 } | 432 } |
| 413 | 433 |
| 414 private: | 434 private: |
| 415 typedef SkView INHERITED; | 435 typedef SkView INHERITED; |
| 416 }; | 436 }; |
| 417 | 437 |
| 418 ////////////////////////////////////////////////////////////////////////////// | 438 ////////////////////////////////////////////////////////////////////////////// |
| 419 | 439 |
| 420 static SkView* MyFactory() { return new ShadowsView; } | 440 static SkView* MyFactory() { return new ShadowsView; } |
| 421 static SkViewRegister reg(MyFactory); | 441 static SkViewRegister reg(MyFactory); |
| OLD | NEW |