Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: src/effects/SkEmbossMaskFilter.cpp

Issue 21835004: Blur refactoring (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Removed unneeded #includes Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/effects/SkBlurMaskFilter.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 static inline int pin2byte(int n) { 17 static inline int pin2byte(int n) {
18 if (n < 0) { 18 if (n < 0) {
19 n = 0; 19 n = 0;
20 } else if (n > 0xFF) { 20 } else if (n > 0xFF) {
21 n = 0xFF; 21 n = 0xFF;
22 } 22 }
23 return n; 23 return n;
24 } 24 }
25 25
26 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], 26 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3],
27 SkScalar ambient, SkScalar specular , 27 SkScalar ambient, SkScalar specular ,
28 SkScalar blurRadius) { 28 SkScalar blurRadius) {
29 return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurR adius),
30 direction, ambient, specular);
31 }
32
33 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar direction[3],
34 SkScalar ambient, SkScalar specular ) {
29 if (direction == NULL) { 35 if (direction == NULL) {
30 return NULL; 36 return NULL;
31 } 37 }
32 38
33 // ambient should be 0...1 as a scalar 39 // ambient should be 0...1 as a scalar
34 int am = pin2byte(SkScalarToFixed(ambient) >> 8); 40 int am = pin2byte(SkScalarToFixed(ambient) >> 8);
35 41
36 // specular should be 0..15.99 as a scalar 42 // specular should be 0..15.99 as a scalar
37 int sp = pin2byte(SkScalarToFixed(specular) >> 12); 43 int sp = pin2byte(SkScalarToFixed(specular) >> 12);
38 44
39 SkEmbossMaskFilter::Light light; 45 SkEmbossMaskFilter::Light light;
40 46
41 memcpy(light.fDirection, direction, sizeof(light.fDirection)); 47 memcpy(light.fDirection, direction, sizeof(light.fDirection));
42 light.fAmbient = SkToU8(am); 48 light.fAmbient = SkToU8(am);
43 light.fSpecular = SkToU8(sp); 49 light.fSpecular = SkToU8(sp);
44 50
45 return SkNEW_ARGS(SkEmbossMaskFilter, (light, blurRadius)); 51 return SkNEW_ARGS(SkEmbossMaskFilter, (blurSigma, light));
46 } 52 }
47 53
48 /////////////////////////////////////////////////////////////////////////////// 54 ///////////////////////////////////////////////////////////////////////////////
49 55
50 static void normalize(SkScalar v[3]) { 56 static void normalize(SkScalar v[3]) {
51 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare( v[2]); 57 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare( v[2]);
52 mag = SkScalarSqrt(mag); 58 mag = SkScalarSqrt(mag);
53 59
54 for (int i = 0; i < 3; i++) { 60 for (int i = 0; i < 3; i++) {
55 v[i] = SkScalarDiv(v[i], mag); 61 v[i] = SkScalarDiv(v[i], mag);
56 } 62 }
57 } 63 }
58 64
59 SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius) 65 SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
60 : fLight(light), fBlurRadius(blurRadius) { 66 : fBlurSigma(blurSigma), fLight(light) {
61 normalize(fLight.fDirection); 67 normalize(fLight.fDirection);
62 } 68 }
63 69
70 SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius)
71 : fLight(light) {
72 normalize(fLight.fDirection);
73
74 fBlurSigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
75 }
76
64 SkMask::Format SkEmbossMaskFilter::getFormat() const { 77 SkMask::Format SkEmbossMaskFilter::getFormat() const {
65 return SkMask::k3D_Format; 78 return SkMask::k3D_Format;
66 } 79 }
67 80
68 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, 81 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
69 const SkMatrix& matrix, SkIPoint* margin) const { 82 const SkMatrix& matrix, SkIPoint* margin) co nst {
70 SkScalar radius = matrix.mapRadius(fBlurRadius); 83 SkScalar sigma = matrix.mapRadius(fBlurSigma);
71 84
72 if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style, 85 if (!SkBlurMask::BoxBlur(dst, src, sigma, SkBlurMask::kInner_Style,
73 SkBlurMask::kLow_Quality)) { 86 SkBlurMask::kLow_Quality)) {
74 return false; 87 return false;
75 } 88 }
76 89
77 dst->fFormat = SkMask::k3D_Format; 90 dst->fFormat = SkMask::k3D_Format;
78 if (margin) { 91 if (margin) {
79 margin->set(SkScalarCeil(radius), SkScalarCeil(radius)); 92 margin->set(SkScalarCeil(3*sigma), SkScalarCeil(3*sigma));
80 } 93 }
81 94
82 if (src.fImage == NULL) { 95 if (src.fImage == NULL) {
83 return true; 96 return true;
84 } 97 }
85 98
86 // create a larger buffer for the other two channels (should force fBlur to do this for us) 99 // create a larger buffer for the other two channels (should force fBlur to do this for us)
87 100
88 { 101 {
89 uint8_t* alphaPlane = dst->fImage; 102 uint8_t* alphaPlane = dst->fImage;
(...skipping 24 matching lines...) Expand all
114 memcpy(dst->fImage, src.fImage, src.computeImageSize()); 127 memcpy(dst->fImage, src.fImage, src.computeImageSize());
115 128
116 return true; 129 return true;
117 } 130 }
118 131
119 SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) 132 SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer)
120 : SkMaskFilter(buffer) { 133 : SkMaskFilter(buffer) {
121 SkASSERT(buffer.getArrayCount() == sizeof(Light)); 134 SkASSERT(buffer.getArrayCount() == sizeof(Light));
122 buffer.readByteArray(&fLight); 135 buffer.readByteArray(&fLight);
123 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean 136 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean
124 fBlurRadius = buffer.readScalar(); 137 fBlurSigma = buffer.readScalar();
138 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V13_AND_ALL_OTHER_INSTANCES_TO O
139 // Fixing this must be done in two stages. When the skps are recaptured in V 13,
140 // remove the ConvertRadiusToSigma but retain the absolute value.
141 // At the same time, switch the code in flatten to write a positive value.
142 // When the skps are captured in V14 the absolute value can be removed.
143 if (fBlurSigma > 0) {
144 fBlurSigma = SkBlurMask::ConvertRadiusToSigma(fBlurSigma);
145 } else {
146 fBlurSigma = -fBlurSigma;
147 }
148 #endif
125 } 149 }
126 150
127 void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const { 151 void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
128 this->INHERITED::flatten(buffer); 152 this->INHERITED::flatten(buffer);
129 153
130 Light tmpLight = fLight; 154 Light tmpLight = fLight;
131 tmpLight.fPad = 0; // for the font-cache lookup to be clean 155 tmpLight.fPad = 0; // for the font-cache lookup to be clean
132 buffer.writeByteArray(&tmpLight, sizeof(tmpLight)); 156 buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
133 buffer.writeScalar(fBlurRadius); 157 buffer.writeScalar(-fBlurSigma);
134 } 158 }
135 159
136 #ifdef SK_DEVELOPER 160 #ifdef SK_DEVELOPER
137 void SkEmbossMaskFilter::toString(SkString* str) const { 161 void SkEmbossMaskFilter::toString(SkString* str) const {
138 str->append("SkEmbossMaskFilter: ("); 162 str->append("SkEmbossMaskFilter: (");
139 163
140 str->append("direction: ("); 164 str->append("direction: (");
141 str->appendScalar(fLight.fDirection[0]); 165 str->appendScalar(fLight.fDirection[0]);
142 str->append(", "); 166 str->append(", ");
143 str->appendScalar(fLight.fDirection[1]); 167 str->appendScalar(fLight.fDirection[1]);
144 str->append(", "); 168 str->append(", ");
145 str->appendScalar(fLight.fDirection[2]); 169 str->appendScalar(fLight.fDirection[2]);
146 str->append(") "); 170 str->append(") ");
147 171
148 str->appendf("ambient: %d specular: %d ", 172 str->appendf("ambient: %d specular: %d ",
149 fLight.fAmbient, fLight.fSpecular); 173 fLight.fAmbient, fLight.fSpecular);
150 174
151 str->append("blurRadius: "); 175 str->append("blurSigma: ");
152 str->appendScalar(fBlurRadius); 176 str->appendScalar(fBlurSigma);
153 str->append(")"); 177 str->append(")");
154 } 178 }
155 #endif 179 #endif
OLDNEW
« no previous file with comments | « src/effects/SkBlurMaskFilter.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698