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 "SkColorPriv.h" |
| 10 #include "SkImagePriv.h" |
| 11 #include "SkImage_Base.h" |
9 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkUnPreMultiply.h" |
10 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
11 #include "SkUnPreMultiply.h" | |
12 #include "SkColorPriv.h" | |
13 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
14 #include "GrContext.h" | 16 #include "GrContext.h" |
15 #include "GrCoordTransform.h" | 17 #include "GrCoordTransform.h" |
16 #include "GrInvariantOutput.h" | 18 #include "GrInvariantOutput.h" |
17 #include "gl/GrGLProcessor.h" | 19 #include "gl/GrGLProcessor.h" |
18 #include "gl/builders/GrGLProgramBuilder.h" | 20 #include "gl/builders/GrGLProgramBuilder.h" |
19 #endif | 21 #endif |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 202 } |
201 | 203 |
202 void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { | 204 void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { |
203 this->INHERITED::flatten(buffer); | 205 this->INHERITED::flatten(buffer); |
204 buffer.writeInt((int) fXChannelSelector); | 206 buffer.writeInt((int) fXChannelSelector); |
205 buffer.writeInt((int) fYChannelSelector); | 207 buffer.writeInt((int) fYChannelSelector); |
206 buffer.writeScalar(fScale); | 208 buffer.writeScalar(fScale); |
207 } | 209 } |
208 | 210 |
209 bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy, | 211 bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy, |
210 const SkBitmap& src, | 212 const SkImage* src, |
211 const Context& ctx, | 213 const Context& ctx, |
212 SkBitmap* dst, | 214 SkAutoTUnref<const SkImage>& dst, |
213 SkIPoint* offset) const { | 215 SkIPoint* offset) const { |
214 SkBitmap displ = src, color = src; | 216 SkIRect bounds; |
| 217 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); |
| 218 |
| 219 SkAutoTUnref<const SkImage> displ(SkRef(src)); |
| 220 SkAutoTUnref<const SkImage> color(SkRef(src)); |
215 const SkImageFilter* colorInput = getColorInput(); | 221 const SkImageFilter* colorInput = getColorInput(); |
216 const SkImageFilter* displInput = getDisplacementInput(); | 222 const SkImageFilter* displInput = getDisplacementInput(); |
217 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); | 223 |
218 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorO
ffset)) || | 224 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, color, &colorOf
fset)) || |
219 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displO
ffset))) { | 225 (displInput && !displInput->filterImage(proxy, src, ctx, displ, &displOf
fset))) { |
220 return false; | 226 return false; |
221 } | 227 } |
222 if ((displ.colorType() != kN32_SkColorType) || | |
223 (color.colorType() != kN32_SkColorType)) { | |
224 return false; | |
225 } | |
226 SkIRect bounds; | |
227 // Since computeDisplacement does bounds checking on color pixel access, we
don't need to pad | 228 // Since computeDisplacement does bounds checking on color pixel access, we
don't need to pad |
228 // the color bitmap to bounds here. | 229 // the color bitmap to bounds here. |
229 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { | 230 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { |
230 return false; | 231 return false; |
231 } | 232 } |
232 SkIRect displBounds; | 233 SkIRect displBounds; |
233 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &dis
pl)) { | 234 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, disp
l)) { |
234 return false; | 235 return false; |
235 } | 236 } |
236 if (!bounds.intersect(displBounds)) { | 237 if (!bounds.intersect(displBounds)) { |
237 return false; | 238 return false; |
238 } | 239 } |
239 SkAutoLockPixels alp_displacement(displ), alp_color(color); | 240 |
240 if (!displ.getPixels() || !color.getPixels()) { | 241 SkBitmap displacementBitmap; |
| 242 SkAutoAdoptImageAsN32Bitmap aai_displacement(displ, &displacementBitmap); |
| 243 if (NULL == displacementBitmap.getPixels()) { |
241 return false; | 244 return false; |
242 } | 245 } |
243 | 246 |
244 if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()
))) { | 247 SkBitmap colorBitmap; |
| 248 SkAutoAdoptImageAsN32Bitmap aai_color(color, &colorBitmap); |
| 249 if (NULL == colorBitmap.getPixels()) { |
245 return false; | 250 return false; |
246 } | 251 } |
247 | 252 |
| 253 SkBitmap dstBitmap; |
| 254 if (!dstBitmap.tryAllocPixels(colorBitmap.info().makeWH(bounds.width(), boun
ds.height()))) { |
| 255 return false; |
| 256 } |
| 257 |
248 SkVector scale = SkVector::Make(fScale, fScale); | 258 SkVector scale = SkVector::Make(fScale, fScale); |
249 ctx.ctm().mapVectors(&scale, 1); | 259 ctx.ctm().mapVectors(&scale, 1); |
250 SkIRect colorBounds = bounds; | 260 SkIRect colorBounds = bounds; |
251 colorBounds.offset(-colorOffset); | 261 colorBounds.offset(-colorOffset); |
252 | 262 |
253 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, | 263 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, &dstBitmap, |
254 &displ, colorOffset - displOffset, &color, colorBounds); | 264 &displacementBitmap, colorOffset - displOffset, |
| 265 &colorBitmap, colorBounds); |
255 | 266 |
| 267 |
| 268 displacementBitmap = SkBitmap(); |
| 269 colorBitmap = SkBitmap(); |
| 270 |
| 271 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 272 if (NULL == image) { |
| 273 return false; |
| 274 } |
| 275 dst.reset(image); |
256 offset->fX = bounds.left(); | 276 offset->fX = bounds.left(); |
257 offset->fY = bounds.top(); | 277 offset->fY = bounds.top(); |
258 return true; | 278 return true; |
259 } | 279 } |
260 | 280 |
261 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { | 281 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { |
262 if (getColorInput()) { | 282 if (getColorInput()) { |
263 getColorInput()->computeFastBounds(src, dst); | 283 getColorInput()->computeFastBounds(src, dst); |
264 } else { | 284 } else { |
265 *dst = src; | 285 *dst = src; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 GrTextureAccess fDisplacementAccess; | 396 GrTextureAccess fDisplacementAccess; |
377 GrCoordTransform fColorTransform; | 397 GrCoordTransform fColorTransform; |
378 GrTextureAccess fColorAccess; | 398 GrTextureAccess fColorAccess; |
379 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 399 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
380 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 400 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
381 SkVector fScale; | 401 SkVector fScale; |
382 | 402 |
383 typedef GrFragmentProcessor INHERITED; | 403 typedef GrFragmentProcessor INHERITED; |
384 }; | 404 }; |
385 | 405 |
386 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src,
const Context& ctx, | 406 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkImage* src, c
onst Context& ctx, |
387 SkBitmap* result, SkIPoint* offset)
const { | 407 SkAutoTUnref<const SkImage>& result
, |
388 SkBitmap colorBM = src; | 408 SkIPoint* offset) const { |
| 409 SkAutoTUnref<const SkImage> colorBM(SkRef(src)); |
389 SkIPoint colorOffset = SkIPoint::Make(0, 0); | 410 SkIPoint colorOffset = SkIPoint::Make(0, 0); |
390 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
&colorBM, | 411 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
colorBM, |
391 &colorOffset)) { | 412 &colorOffset)) { |
392 return false; | 413 return false; |
393 } | 414 } |
394 SkBitmap displacementBM = src; | 415 SkAutoTUnref<const SkImage> displacementBM(SkRef(src)); |
395 SkIPoint displacementOffset = SkIPoint::Make(0, 0); | 416 SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
396 if (getDisplacementInput() && | 417 if (getDisplacementInput() && |
397 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacemen
tBM, | 418 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, displacement
BM, |
398 &displacementOffset)) { | 419 &displacementOffset)) { |
399 return false; | 420 return false; |
400 } | 421 } |
401 SkIRect bounds; | 422 SkIRect bounds; |
402 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to | 423 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to |
403 // pad the color bitmap to bounds here. | 424 // pad the color bitmap to bounds here. |
404 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { | 425 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { |
405 return false; | 426 return false; |
406 } | 427 } |
407 SkIRect displBounds; | 428 SkIRect displBounds; |
408 if (!this->applyCropRect(ctx, proxy, displacementBM, | 429 if (!this->applyCropRect(ctx, proxy, displacementBM, |
409 &displacementOffset, &displBounds, &displacementBM)
) { | 430 &displacementOffset, &displBounds, displacementBM))
{ |
410 return false; | 431 return false; |
411 } | 432 } |
412 if (!bounds.intersect(displBounds)) { | 433 if (!bounds.intersect(displBounds)) { |
413 return false; | 434 return false; |
414 } | 435 } |
415 GrTexture* color = colorBM.getTexture(); | 436 GrTexture* color = colorBM->getTexture(); |
416 GrTexture* displacement = displacementBM.getTexture(); | 437 GrTexture* displacement = displacementBM->getTexture(); |
417 GrContext* context = color->getContext(); | 438 GrContext* context = color->getContext(); |
418 | 439 |
419 GrSurfaceDesc desc; | 440 GrSurfaceDesc desc; |
420 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 441 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
421 desc.fWidth = bounds.width(); | 442 desc.fWidth = bounds.width(); |
422 desc.fHeight = bounds.height(); | 443 desc.fHeight = bounds.height(); |
423 desc.fConfig = kSkia8888_GrPixelConfig; | 444 desc.fConfig = kSkia8888_GrPixelConfig; |
424 | 445 |
425 SkAutoTUnref<GrTexture> dst( | 446 SkAutoTUnref<GrTexture> dst( |
426 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); | 447 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); |
(...skipping 16 matching lines...) Expand all Loading... |
443 scale, | 464 scale, |
444 displacement, | 465 displacement, |
445 offsetMatrix, | 466 offsetMatrix, |
446 color))->unref(); | 467 color))->unref(); |
447 SkIRect colorBounds = bounds; | 468 SkIRect colorBounds = bounds; |
448 colorBounds.offset(-colorOffset); | 469 colorBounds.offset(-colorOffset); |
449 SkMatrix matrix; | 470 SkMatrix matrix; |
450 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), | 471 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
451 -SkIntToScalar(colorBounds.y())); | 472 -SkIntToScalar(colorBounds.y())); |
452 context->drawRect(dst->asRenderTarget(), paint, matrix, SkRect::Make(colorBo
unds)); | 473 context->drawRect(dst->asRenderTarget(), paint, matrix, SkRect::Make(colorBo
unds)); |
| 474 if (!WrapTexture(dst, bounds.width(), bounds.height(), result)) { |
| 475 return false; |
| 476 } |
453 offset->fX = bounds.left(); | 477 offset->fX = bounds.left(); |
454 offset->fY = bounds.top(); | 478 offset->fY = bounds.top(); |
455 WrapTexture(dst, bounds.width(), bounds.height(), result); | |
456 return true; | 479 return true; |
457 } | 480 } |
458 | 481 |
459 /////////////////////////////////////////////////////////////////////////////// | 482 /////////////////////////////////////////////////////////////////////////////// |
460 | 483 |
461 GrDisplacementMapEffect::GrDisplacementMapEffect( | 484 GrDisplacementMapEffect::GrDisplacementMapEffect( |
462 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, | 485 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, |
463 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, | 486 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, |
464 const SkVector& scale, | 487 const SkVector& scale, |
465 GrTexture* displacement, | 488 GrTexture* displacement, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 const GrGLCaps&, GrProcessorKeyBuilder* b
) { | 653 const GrGLCaps&, GrProcessorKeyBuilder* b
) { |
631 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); | 654 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); |
632 | 655 |
633 uint32_t xKey = displacementMap.xChannelSelector(); | 656 uint32_t xKey = displacementMap.xChannelSelector(); |
634 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; | 657 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; |
635 | 658 |
636 b->add32(xKey | yKey); | 659 b->add32(xKey | yKey); |
637 } | 660 } |
638 #endif | 661 #endif |
639 | 662 |
OLD | NEW |