| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkPathEffect.h" | 8 #include "SkPathEffect.h" |
| 9 #include "SkShadowPaintFilterCanvas.h" | 9 #include "SkShadowPaintFilterCanvas.h" |
| 10 | 10 |
| 11 #ifdef SK_EXPERIMENTAL_SHADOWING | 11 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 12 | 12 |
| 13 SkShadowPaintFilterCanvas::SkShadowPaintFilterCanvas(SkCanvas *canvas) | 13 SkShadowPaintFilterCanvas::SkShadowPaintFilterCanvas(SkCanvas *canvas) |
| 14 : SkPaintFilterCanvas(canvas) { } | 14 : SkPaintFilterCanvas(canvas) { |
| 15 fShadowParams.fShadowRadius = 0.0f; |
| 16 fShadowParams.fType = SkShadowParams::kNoBlur_ShadowType; |
| 17 fShadowParams.fBiasingConstant = 0.0f; |
| 18 fShadowParams.fMinVariance = 0.0f; |
| 19 } |
| 15 | 20 |
| 16 // TODO use a shader instead | 21 // TODO use a shader instead |
| 17 bool SkShadowPaintFilterCanvas::onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Ty
pe type) const { | 22 bool SkShadowPaintFilterCanvas::onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Ty
pe type) const { |
| 18 if (*paint) { | 23 if (*paint) { |
| 19 int z = this->getZ(); | 24 int z = this->getZ(); |
| 20 SkASSERT(z <= 0xFF && z >= 0x00); | 25 SkASSERT(z <= 0xFF && z >= 0x00); |
| 21 | 26 |
| 22 SkPaint newPaint; | 27 SkPaint newPaint; |
| 23 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffect()
)); | 28 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffect()
)); |
| 24 | 29 |
| 25 SkColor color = 0xFF000000; // init color to opaque black | 30 SkColor color = 0xFF000000; // init color to opaque black |
| 26 color |= z; // Put the index into the blue component | 31 color |= z; // Put the index into the blue component |
| 32 |
| 33 if (fShadowParams.fType == SkShadowParams::kVariance_ShadowType) { |
| 34 int z2 = z * z; |
| 35 if (z2 > 255 * 256) { |
| 36 color |= 0xff00; |
| 37 } else { |
| 38 // Let's only store the more significant bits of z2 to save spac
e. |
| 39 // In practice, this should barely impact shadow blur quality. |
| 40 color |= z2 & 0x0000ff00; |
| 41 } |
| 42 } |
| 27 newPaint.setColor(color); | 43 newPaint.setColor(color); |
| 28 | 44 |
| 29 *paint->writable() = newPaint; | 45 *paint->writable() = newPaint; |
| 30 } | 46 } |
| 31 | 47 |
| 32 return true; | 48 return true; |
| 33 } | 49 } |
| 34 | 50 |
| 35 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li
ght, int maxDepth, | 51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li
ght, int maxDepth, |
| 36 int width, int height) { | 52 int width, int height) { |
| 37 SkASSERT(light.type() != SkLights::Light::kAmbient_LightType); | 53 SkASSERT(light.type() != SkLights::Light::kAmbient_LightType); |
| 38 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width, | 54 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width, |
| 39 width * 2); | 55 width * 2); |
| 40 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height, | 56 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height, |
| 41 height * 2); | 57 height * 2); |
| 42 return SkISize::Make(dMapWidth, dMapHeight); | 58 return SkISize::Make(dMapWidth, dMapHeight); |
| 43 } | 59 } |
| 44 | 60 |
| 61 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams ¶ms) { |
| 62 fShadowParams = params; |
| 63 } |
| 45 | 64 |
| 46 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk
Matrix *matrix, | 65 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk
Matrix *matrix, |
| 47 const SkPaint *paint) { | 66 const SkPaint *paint) { |
| 48 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); | 67 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); |
| 49 if (this->onFilter(&filteredPaint, kPicture_Type)) { | 68 if (this->onFilter(&filteredPaint, kPicture_Type)) { |
| 50 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); | 69 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); |
| 51 } | 70 } |
| 52 } | 71 } |
| 53 | 72 |
| 54 void SkShadowPaintFilterCanvas::updateMatrix() { | 73 void SkShadowPaintFilterCanvas::updateMatrix() { |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 239 } |
| 221 | 240 |
| 222 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar
x, SkScalar y, | 241 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar
x, SkScalar y, |
| 223 const SkPaint &paint) { | 242 const SkPaint &paint) { |
| 224 this->updateMatrix(); | 243 this->updateMatrix(); |
| 225 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | 244 this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| 226 this->restore(); | 245 this->restore(); |
| 227 } | 246 } |
| 228 | 247 |
| 229 #endif | 248 #endif |
| OLD | NEW |