| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 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 | 8 |
| 9 | 9 |
| 10 #include "SkEmbossMaskFilter.h" | 10 #include "SkEmbossMaskFilter.h" |
| 11 #include "SkBlurMaskFilter.h" | 11 #include "SkBlurMaskFilter.h" |
| 12 #include "SkBlurMask.h" | 12 #include "SkBlurMask.h" |
| 13 #include "SkEmbossMask.h" | 13 #include "SkEmbossMask.h" |
| 14 #include "SkFlattenableBuffers.h" | 14 #include "SkFlattenableBuffers.h" |
| 15 #include "SkString.h" | 15 #include "SkString.h" |
| 16 | 16 |
| 17 #ifndef CLEAN_UP_WHEN_SKPS_ARE_CAPTURED_IN_V13 |
| 18 #include "SkPicture.h" |
| 19 #endif |
| 20 |
| 17 static inline int pin2byte(int n) { | 21 static inline int pin2byte(int n) { |
| 18 if (n < 0) { | 22 if (n < 0) { |
| 19 n = 0; | 23 n = 0; |
| 20 } else if (n > 0xFF) { | 24 } else if (n > 0xFF) { |
| 21 n = 0xFF; | 25 n = 0xFF; |
| 22 } | 26 } |
| 23 return n; | 27 return n; |
| 24 } | 28 } |
| 25 | 29 |
| 26 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], | 30 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], |
| 27 SkScalar ambient, SkScalar specular
, | 31 SkScalar ambient, SkScalar specular
, |
| 28 SkScalar blurRadius) { | 32 SkScalar blurRadius) { |
| 33 return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurR
adius), |
| 34 direction, ambient, specular); |
| 35 } |
| 36 |
| 37 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar
direction[3], |
| 38 SkScalar ambient, SkScalar specular
) { |
| 29 if (direction == NULL) { | 39 if (direction == NULL) { |
| 30 return NULL; | 40 return NULL; |
| 31 } | 41 } |
| 32 | 42 |
| 33 // ambient should be 0...1 as a scalar | 43 // ambient should be 0...1 as a scalar |
| 34 int am = pin2byte(SkScalarToFixed(ambient) >> 8); | 44 int am = pin2byte(SkScalarToFixed(ambient) >> 8); |
| 35 | 45 |
| 36 // specular should be 0..15.99 as a scalar | 46 // specular should be 0..15.99 as a scalar |
| 37 int sp = pin2byte(SkScalarToFixed(specular) >> 12); | 47 int sp = pin2byte(SkScalarToFixed(specular) >> 12); |
| 38 | 48 |
| 39 SkEmbossMaskFilter::Light light; | 49 SkEmbossMaskFilter::Light light; |
| 40 | 50 |
| 41 memcpy(light.fDirection, direction, sizeof(light.fDirection)); | 51 memcpy(light.fDirection, direction, sizeof(light.fDirection)); |
| 42 light.fAmbient = SkToU8(am); | 52 light.fAmbient = SkToU8(am); |
| 43 light.fSpecular = SkToU8(sp); | 53 light.fSpecular = SkToU8(sp); |
| 44 | 54 |
| 45 return SkNEW_ARGS(SkEmbossMaskFilter, (light, blurRadius)); | 55 return SkNEW_ARGS(SkEmbossMaskFilter, (blurSigma, light)); |
| 46 } | 56 } |
| 47 | 57 |
| 48 /////////////////////////////////////////////////////////////////////////////// | 58 /////////////////////////////////////////////////////////////////////////////// |
| 49 | 59 |
| 50 static void normalize(SkScalar v[3]) { | 60 static void normalize(SkScalar v[3]) { |
| 51 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(
v[2]); | 61 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(
v[2]); |
| 52 mag = SkScalarSqrt(mag); | 62 mag = SkScalarSqrt(mag); |
| 53 | 63 |
| 54 for (int i = 0; i < 3; i++) { | 64 for (int i = 0; i < 3; i++) { |
| 55 v[i] = SkScalarDiv(v[i], mag); | 65 v[i] = SkScalarDiv(v[i], mag); |
| 56 } | 66 } |
| 57 } | 67 } |
| 58 | 68 |
| 59 SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius) | 69 SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light) |
| 60 : fLight(light), fBlurRadius(blurRadius) { | 70 : fBlurSigma(blurSigma), fLight(light) { |
| 61 normalize(fLight.fDirection); | 71 normalize(fLight.fDirection); |
| 62 } | 72 } |
| 63 | 73 |
| 74 SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius) |
| 75 : fLight(light) { |
| 76 normalize(fLight.fDirection); |
| 77 |
| 78 fBlurSigma = SkBlurMask::ConvertRadiusToSigma(blurRadius); |
| 79 } |
| 80 |
| 64 SkMask::Format SkEmbossMaskFilter::getFormat() const { | 81 SkMask::Format SkEmbossMaskFilter::getFormat() const { |
| 65 return SkMask::k3D_Format; | 82 return SkMask::k3D_Format; |
| 66 } | 83 } |
| 67 | 84 |
| 68 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, | 85 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, |
| 69 const SkMatrix& matrix, SkIPoint* margin) const { | 86 const SkMatrix& matrix, SkIPoint* margin) co
nst { |
| 70 SkScalar radius = matrix.mapRadius(fBlurRadius); | 87 SkScalar sigma = matrix.mapRadius(fBlurSigma); |
| 71 | 88 |
| 72 if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style, | 89 if (!SkBlurMask::BoxBlur(dst, src, sigma, SkBlurMask::kInner_Style, |
| 73 SkBlurMask::kLow_Quality)) { | 90 SkBlurMask::kLow_Quality)) { |
| 74 return false; | 91 return false; |
| 75 } | 92 } |
| 76 | 93 |
| 77 dst->fFormat = SkMask::k3D_Format; | 94 dst->fFormat = SkMask::k3D_Format; |
| 78 if (margin) { | 95 if (margin) { |
| 79 margin->set(SkScalarCeil(radius), SkScalarCeil(radius)); | 96 margin->set(SkScalarCeil(3*sigma), SkScalarCeil(3*sigma)); |
| 80 } | 97 } |
| 81 | 98 |
| 82 if (src.fImage == NULL) { | 99 if (src.fImage == NULL) { |
| 83 return true; | 100 return true; |
| 84 } | 101 } |
| 85 | 102 |
| 86 // create a larger buffer for the other two channels (should force fBlur to
do this for us) | 103 // create a larger buffer for the other two channels (should force fBlur to
do this for us) |
| 87 | 104 |
| 88 { | 105 { |
| 89 uint8_t* alphaPlane = dst->fImage; | 106 uint8_t* alphaPlane = dst->fImage; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 114 memcpy(dst->fImage, src.fImage, src.computeImageSize()); | 131 memcpy(dst->fImage, src.fImage, src.computeImageSize()); |
| 115 | 132 |
| 116 return true; | 133 return true; |
| 117 } | 134 } |
| 118 | 135 |
| 119 SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) | 136 SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) |
| 120 : SkMaskFilter(buffer) { | 137 : SkMaskFilter(buffer) { |
| 121 SkASSERT(buffer.getArrayCount() == sizeof(Light)); | 138 SkASSERT(buffer.getArrayCount() == sizeof(Light)); |
| 122 buffer.readByteArray(&fLight); | 139 buffer.readByteArray(&fLight); |
| 123 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean | 140 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean |
| 124 fBlurRadius = buffer.readScalar(); | 141 #ifndef CLEAN_UP_WHEN_SKPS_ARE_CAPTURED_IN_V13 |
| 142 if (SkPicture::PICTURE_VERSION < 13) { |
| 143 fBlurSigma = SkBlurMask::ConvertRadiusToSigma(buffer.readScalar()); |
| 144 } else { |
| 145 #endif |
| 146 fBlurSigma = buffer.readScalar(); |
| 147 #ifndef CLEAN_UP_WHEN_SKPS_ARE_CAPTURED_IN_V13 |
| 148 } |
| 149 #endif |
| 125 } | 150 } |
| 126 | 151 |
| 127 void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | 152 void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 128 this->INHERITED::flatten(buffer); | 153 this->INHERITED::flatten(buffer); |
| 129 | 154 |
| 130 Light tmpLight = fLight; | 155 Light tmpLight = fLight; |
| 131 tmpLight.fPad = 0; // for the font-cache lookup to be clean | 156 tmpLight.fPad = 0; // for the font-cache lookup to be clean |
| 132 buffer.writeByteArray(&tmpLight, sizeof(tmpLight)); | 157 buffer.writeByteArray(&tmpLight, sizeof(tmpLight)); |
| 133 buffer.writeScalar(fBlurRadius); | 158 buffer.writeScalar(fBlurSigma); |
| 134 } | 159 } |
| 135 | 160 |
| 136 #ifdef SK_DEVELOPER | 161 #ifdef SK_DEVELOPER |
| 137 void SkEmbossMaskFilter::toString(SkString* str) const { | 162 void SkEmbossMaskFilter::toString(SkString* str) const { |
| 138 str->append("SkEmbossMaskFilter: ("); | 163 str->append("SkEmbossMaskFilter: ("); |
| 139 | 164 |
| 140 str->append("direction: ("); | 165 str->append("direction: ("); |
| 141 str->appendScalar(fLight.fDirection[0]); | 166 str->appendScalar(fLight.fDirection[0]); |
| 142 str->append(", "); | 167 str->append(", "); |
| 143 str->appendScalar(fLight.fDirection[1]); | 168 str->appendScalar(fLight.fDirection[1]); |
| 144 str->append(", "); | 169 str->append(", "); |
| 145 str->appendScalar(fLight.fDirection[2]); | 170 str->appendScalar(fLight.fDirection[2]); |
| 146 str->append(") "); | 171 str->append(") "); |
| 147 | 172 |
| 148 str->appendf("ambient: %d specular: %d ", | 173 str->appendf("ambient: %d specular: %d ", |
| 149 fLight.fAmbient, fLight.fSpecular); | 174 fLight.fAmbient, fLight.fSpecular); |
| 150 | 175 |
| 151 str->append("blurRadius: "); | 176 str->append("blurSigma: "); |
| 152 str->appendScalar(fBlurRadius); | 177 str->appendScalar(fBlurSigma); |
| 153 str->append(")"); | 178 str->append(")"); |
| 154 } | 179 } |
| 155 #endif | 180 #endif |
| OLD | NEW |