OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkDisplacementMapEffect.h" | 8 #include "SkDisplacementMapEffect.h" |
9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
10 #include "SkUnPreMultiply.h" | 10 #include "SkUnPreMultiply.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(
l)); | 40 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(
l)); |
41 } | 41 } |
42 | 42 |
43 template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>( | 43 template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>( |
44 SkColor l, const SkUnPreMultiply::Scale*) { | 44 SkColor l, const SkUnPreMultiply::Scale*) { |
45 return SkGetPackedA32(l); | 45 return SkGetPackedA32(l); |
46 } | 46 } |
47 | 47 |
48 template<SkDisplacementMapEffect::ChannelSelectorType typeX, | 48 template<SkDisplacementMapEffect::ChannelSelectorType typeX, |
49 SkDisplacementMapEffect::ChannelSelectorType typeY> | 49 SkDisplacementMapEffect::ChannelSelectorType typeY> |
50 void computeDisplacement(SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src) | 50 void computeDisplacement(SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src, const SkIRect& bounds) |
51 { | 51 { |
52 static const SkScalar Inv8bit = SkScalarDiv(SK_Scalar1, SkFloatToScalar(255.
0f)); | 52 static const SkScalar Inv8bit = SkScalarDiv(SK_Scalar1, SkFloatToScalar(255.
0f)); |
53 const int dstW = displ->width(); | |
54 const int dstH = displ->height(); | |
55 const int srcW = src->width(); | 53 const int srcW = src->width(); |
56 const int srcH = src->height(); | 54 const int srcH = src->height(); |
57 const SkScalar scaleForColor = SkScalarMul(scale, Inv8bit); | 55 const SkScalar scaleForColor = SkScalarMul(scale, Inv8bit); |
58 const SkScalar scaleAdj = SK_ScalarHalf - SkScalarMul(scale, SK_ScalarHalf); | 56 const SkScalar scaleAdj = SK_ScalarHalf - SkScalarMul(scale, SK_ScalarHalf); |
59 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); | 57 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
60 for (int y = 0; y < dstH; ++y) { | 58 SkPMColor* dstPtr = dst->getAddr32(0, 0); |
61 const SkPMColor* displPtr = displ->getAddr32(0, y); | 59 for (int y = bounds.top(); y < bounds.bottom(); ++y) { |
62 SkPMColor* dstPtr = dst->getAddr32(0, y); | 60 const SkPMColor* displPtr = displ->getAddr32(bounds.left(), y); |
63 for (int x = 0; x < dstW; ++x, ++displPtr, ++dstPtr) { | 61 for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) { |
64 const SkScalar displX = SkScalarMul(scaleForColor, | 62 const SkScalar displX = SkScalarMul(scaleForColor, |
65 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj; | 63 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj; |
66 const SkScalar displY = SkScalarMul(scaleForColor, | 64 const SkScalar displY = SkScalarMul(scaleForColor, |
67 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj; | 65 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj; |
68 // Truncate the displacement values | 66 // Truncate the displacement values |
69 const int srcX = x + SkScalarTruncToInt(displX); | 67 const int srcX = x + SkScalarTruncToInt(displX); |
70 const int srcY = y + SkScalarTruncToInt(displY); | 68 const int srcY = y + SkScalarTruncToInt(displY); |
71 *dstPtr = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= sr
cH)) ? | 69 *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >=
srcH)) ? |
72 0 : *(src->getAddr32(srcX, srcY)); | 70 0 : *(src->getAddr32(srcX, srcY)); |
73 } | 71 } |
74 } | 72 } |
75 } | 73 } |
76 | 74 |
77 template<SkDisplacementMapEffect::ChannelSelectorType typeX> | 75 template<SkDisplacementMapEffect::ChannelSelectorType typeX> |
78 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe
lector, | 76 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe
lector, |
79 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src) | 77 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src, const SkIRect& bounds) |
80 { | 78 { |
81 switch (yChannelSelector) { | 79 switch (yChannelSelector) { |
82 case SkDisplacementMapEffect::kR_ChannelSelectorType: | 80 case SkDisplacementMapEffect::kR_ChannelSelectorType: |
83 computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorTy
pe>( | 81 computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorTy
pe>( |
84 scale, dst, displ, src); | 82 scale, dst, displ, src, bounds); |
85 break; | 83 break; |
86 case SkDisplacementMapEffect::kG_ChannelSelectorType: | 84 case SkDisplacementMapEffect::kG_ChannelSelectorType: |
87 computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorTy
pe>( | 85 computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorTy
pe>( |
88 scale, dst, displ, src); | 86 scale, dst, displ, src, bounds); |
89 break; | 87 break; |
90 case SkDisplacementMapEffect::kB_ChannelSelectorType: | 88 case SkDisplacementMapEffect::kB_ChannelSelectorType: |
91 computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorTy
pe>( | 89 computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorTy
pe>( |
92 scale, dst, displ, src); | 90 scale, dst, displ, src, bounds); |
93 break; | 91 break; |
94 case SkDisplacementMapEffect::kA_ChannelSelectorType: | 92 case SkDisplacementMapEffect::kA_ChannelSelectorType: |
95 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorTy
pe>( | 93 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorTy
pe>( |
96 scale, dst, displ, src); | 94 scale, dst, displ, src, bounds); |
97 break; | 95 break; |
98 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: | 96 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
99 default: | 97 default: |
100 SkDEBUGFAIL("Unknown Y channel selector"); | 98 SkDEBUGFAIL("Unknown Y channel selector"); |
101 } | 99 } |
102 } | 100 } |
103 | 101 |
104 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSe
lector, | 102 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSe
lector, |
105 SkDisplacementMapEffect::ChannelSelectorType yChannelSe
lector, | 103 SkDisplacementMapEffect::ChannelSelectorType yChannelSe
lector, |
106 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src) | 104 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma
p* src, const SkIRect& bounds) |
107 { | 105 { |
108 switch (xChannelSelector) { | 106 switch (xChannelSelector) { |
109 case SkDisplacementMapEffect::kR_ChannelSelectorType: | 107 case SkDisplacementMapEffect::kR_ChannelSelectorType: |
110 computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>( | 108 computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>( |
111 yChannelSelector, scale, dst, displ, src); | 109 yChannelSelector, scale, dst, displ, src, bounds); |
112 break; | 110 break; |
113 case SkDisplacementMapEffect::kG_ChannelSelectorType: | 111 case SkDisplacementMapEffect::kG_ChannelSelectorType: |
114 computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>( | 112 computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>( |
115 yChannelSelector, scale, dst, displ, src); | 113 yChannelSelector, scale, dst, displ, src, bounds); |
116 break; | 114 break; |
117 case SkDisplacementMapEffect::kB_ChannelSelectorType: | 115 case SkDisplacementMapEffect::kB_ChannelSelectorType: |
118 computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>( | 116 computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>( |
119 yChannelSelector, scale, dst, displ, src); | 117 yChannelSelector, scale, dst, displ, src, bounds); |
120 break; | 118 break; |
121 case SkDisplacementMapEffect::kA_ChannelSelectorType: | 119 case SkDisplacementMapEffect::kA_ChannelSelectorType: |
122 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>( | 120 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>( |
123 yChannelSelector, scale, dst, displ, src); | 121 yChannelSelector, scale, dst, displ, src, bounds); |
124 break; | 122 break; |
125 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: | 123 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
126 default: | 124 default: |
127 SkDEBUGFAIL("Unknown X channel selector"); | 125 SkDEBUGFAIL("Unknown X channel selector"); |
128 } | 126 } |
129 } | 127 } |
130 | 128 |
131 } // end namespace | 129 } // end namespace |
132 | 130 |
133 /////////////////////////////////////////////////////////////////////////////// | 131 /////////////////////////////////////////////////////////////////////////////// |
134 | 132 |
135 SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSel
ector, | 133 SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSel
ector, |
136 ChannelSelectorType yChannelSel
ector, | 134 ChannelSelectorType yChannelSel
ector, |
137 SkScalar scale, | 135 SkScalar scale, |
138 SkImageFilter* displacement, | 136 SkImageFilter* displacement, |
139 SkImageFilter* color) | 137 SkImageFilter* color, |
140 : INHERITED(displacement, color) | 138 const CropRect* cropRect) |
| 139 : INHERITED(displacement, color, cropRect) |
141 , fXChannelSelector(xChannelSelector) | 140 , fXChannelSelector(xChannelSelector) |
142 , fYChannelSelector(yChannelSelector) | 141 , fYChannelSelector(yChannelSelector) |
143 , fScale(scale) | 142 , fScale(scale) |
144 { | 143 { |
145 } | 144 } |
146 | 145 |
147 SkDisplacementMapEffect::~SkDisplacementMapEffect() { | 146 SkDisplacementMapEffect::~SkDisplacementMapEffect() { |
148 } | 147 } |
149 | 148 |
150 SkDisplacementMapEffect::SkDisplacementMapEffect(SkFlattenableReadBuffer& buffer
) | 149 SkDisplacementMapEffect::SkDisplacementMapEffect(SkFlattenableReadBuffer& buffer
) |
(...skipping 26 matching lines...) Expand all Loading... |
177 } | 176 } |
178 if ((displ.config() != SkBitmap::kARGB_8888_Config) || | 177 if ((displ.config() != SkBitmap::kARGB_8888_Config) || |
179 (color.config() != SkBitmap::kARGB_8888_Config)) { | 178 (color.config() != SkBitmap::kARGB_8888_Config)) { |
180 return false; | 179 return false; |
181 } | 180 } |
182 | 181 |
183 SkAutoLockPixels alp_displacement(displ), alp_color(color); | 182 SkAutoLockPixels alp_displacement(displ), alp_color(color); |
184 if (!displ.getPixels() || !color.getPixels()) { | 183 if (!displ.getPixels() || !color.getPixels()) { |
185 return false; | 184 return false; |
186 } | 185 } |
187 dst->setConfig(displ.config(), displ.width(), displ.height()); | 186 SkIRect bounds; |
| 187 color.getBounds(&bounds); |
| 188 if (!this->applyCropRect(&bounds, ctm)) { |
| 189 return false; |
| 190 } |
| 191 |
| 192 dst->setConfig(color.config(), bounds.width(), bounds.height()); |
188 dst->allocPixels(); | 193 dst->allocPixels(); |
189 if (!dst->getPixels()) { | 194 if (!dst->getPixels()) { |
190 return false; | 195 return false; |
191 } | 196 } |
192 | 197 |
193 computeDisplacement(fXChannelSelector, fYChannelSelector, fScale, dst, &disp
l, &color); | 198 computeDisplacement(fXChannelSelector, fYChannelSelector, fScale, dst, &disp
l, &color, bounds); |
194 | 199 |
| 200 offset->fX += bounds.left(); |
| 201 offset->fY += bounds.top(); |
195 return true; | 202 return true; |
196 } | 203 } |
197 | 204 |
198 /////////////////////////////////////////////////////////////////////////////// | 205 /////////////////////////////////////////////////////////////////////////////// |
199 | 206 |
200 #if SK_SUPPORT_GPU | 207 #if SK_SUPPORT_GPU |
201 class GrGLDisplacementMapEffect : public GrGLEffect { | 208 class GrGLDisplacementMapEffect : public GrGLEffect { |
202 public: | 209 public: |
203 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, | 210 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
204 const GrDrawEffect& drawEffect); | 211 const GrDrawEffect& drawEffect); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 309 |
303 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); | 310 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); |
304 | 311 |
305 GrPaint paint; | 312 GrPaint paint; |
306 paint.addColorEffect( | 313 paint.addColorEffect( |
307 GrDisplacementMapEffect::Create(fXChannelSelector, | 314 GrDisplacementMapEffect::Create(fXChannelSelector, |
308 fYChannelSelector, | 315 fYChannelSelector, |
309 fScale, | 316 fScale, |
310 displacement, | 317 displacement, |
311 color))->unref(); | 318 color))->unref(); |
312 SkRect srcRect; | 319 SkIRect bounds; |
313 src.getBounds(&srcRect); | 320 src.getBounds(&bounds); |
314 SkRect dstRect = srcRect; | 321 if (!this->applyCropRect(&bounds, ctm)) { |
315 dstRect.offset(SkIntToScalar(colorOffset.fX), SkIntToScalar(colorOffset.fY))
; | 322 return false; |
316 context->drawRectToRect(paint, srcRect, dstRect); | 323 } |
317 return SkImageFilterUtils::WrapTexture(dst, src.width(), src.height(), resul
t); | 324 SkRect srcRect = SkRect::Make(bounds); |
| 325 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 326 context->drawRectToRect(paint, dstRect, srcRect); |
| 327 offset->fX += bounds.left(); |
| 328 offset->fY += bounds.top(); |
| 329 return SkImageFilterUtils::WrapTexture(dst, bounds.width(), bounds.height(),
result); |
318 } | 330 } |
319 | 331 |
320 /////////////////////////////////////////////////////////////////////////////// | 332 /////////////////////////////////////////////////////////////////////////////// |
321 | 333 |
322 GrDisplacementMapEffect::GrDisplacementMapEffect( | 334 GrDisplacementMapEffect::GrDisplacementMapEffect( |
323 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, | 335 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, |
324 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, | 336 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, |
325 SkScalar scale, | 337 SkScalar scale, |
326 GrTexture* displacement, | 338 GrTexture* displacement, |
327 GrTexture* color) | 339 GrTexture* color) |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 const GrGLCaps&) { | 506 const GrGLCaps&) { |
495 const GrDisplacementMapEffect& displacementMap = | 507 const GrDisplacementMapEffect& displacementMap = |
496 drawEffect.castEffect<GrDisplacementMapEffect>(); | 508 drawEffect.castEffect<GrDisplacementMapEffect>(); |
497 | 509 |
498 EffectKey xKey = displacementMap.xChannelSelector(); | 510 EffectKey xKey = displacementMap.xChannelSelector(); |
499 EffectKey yKey = displacementMap.yChannelSelector() << SkDisplacementMapEffe
ct::kKeyBits; | 511 EffectKey yKey = displacementMap.yChannelSelector() << SkDisplacementMapEffe
ct::kKeyBits; |
500 | 512 |
501 return xKey | yKey; | 513 return xKey | yKey; |
502 } | 514 } |
503 #endif | 515 #endif |
OLD | NEW |