Chromium Code Reviews| 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 fShadowType.fShadowRadius = 0.0f; | |
| 16 fShadowType.fBlurAlgorithm = SkShadowType::kNoBlur_BlurAlgorithm; | |
| 17 fShadowType.fBiasingConstant = 0.0f; | |
| 18 fShadowType.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 (fShadowType.fBlurAlgorithm == SkShadowType::kVariance_BlurAlgorithm) { | |
| 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::setShadowType(SkShadowType sType) { | |
|
jvanverth1
2016/08/15 16:37:17
const &
vjiaoblack
2016/08/15 17:43:31
Done.
| |
| 62 fShadowType = sType; | |
| 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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 } | 231 } |
| 213 | 232 |
| 214 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, | 233 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, |
| 215 const SkPaint &paint) { | 234 const SkPaint &paint) { |
| 216 this->updateMatrix(); | 235 this->updateMatrix(); |
| 217 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | 236 this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| 218 this->restore(); | 237 this->restore(); |
| 219 } | 238 } |
| 220 | 239 |
| 221 #endif | 240 #endif |
| OLD | NEW |