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