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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 201 } |
200 | 202 |
201 void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { | 203 void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { |
202 this->INHERITED::flatten(buffer); | 204 this->INHERITED::flatten(buffer); |
203 buffer.writeInt((int) fXChannelSelector); | 205 buffer.writeInt((int) fXChannelSelector); |
204 buffer.writeInt((int) fYChannelSelector); | 206 buffer.writeInt((int) fYChannelSelector); |
205 buffer.writeScalar(fScale); | 207 buffer.writeScalar(fScale); |
206 } | 208 } |
207 | 209 |
208 bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy, | 210 bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy, |
209 const SkBitmap& src, | 211 const SkImage* src, |
210 const Context& ctx, | 212 const Context& ctx, |
211 SkBitmap* dst, | 213 SkAutoTUnref<const SkImage>& dst, |
212 SkIPoint* offset) const { | 214 SkIPoint* offset) const { |
213 SkBitmap displ = src, color = src; | 215 SkIRect bounds; |
| 216 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); |
| 217 |
| 218 SkAutoTUnref<const SkImage> displ(SkRef(src)); |
| 219 SkAutoTUnref<const SkImage> color(SkRef(src)); |
214 const SkImageFilter* colorInput = getColorInput(); | 220 const SkImageFilter* colorInput = getColorInput(); |
215 const SkImageFilter* displInput = getDisplacementInput(); | 221 const SkImageFilter* displInput = getDisplacementInput(); |
216 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); | 222 |
217 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorO
ffset)) || | 223 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, color, &colorOf
fset)) || |
218 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displO
ffset))) { | 224 (displInput && !displInput->filterImage(proxy, src, ctx, displ, &displOf
fset))) { |
219 return false; | 225 return false; |
220 } | 226 } |
221 if ((displ.colorType() != kN32_SkColorType) || | |
222 (color.colorType() != kN32_SkColorType)) { | |
223 return false; | |
224 } | |
225 SkIRect bounds; | |
226 // Since computeDisplacement does bounds checking on color pixel access, we
don't need to pad | 227 // Since computeDisplacement does bounds checking on color pixel access, we
don't need to pad |
227 // the color bitmap to bounds here. | 228 // the color bitmap to bounds here. |
228 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { | 229 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { |
229 return false; | 230 return false; |
230 } | 231 } |
231 SkIRect displBounds; | 232 SkIRect displBounds; |
232 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &dis
pl)) { | 233 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, disp
l)) { |
233 return false; | 234 return false; |
234 } | 235 } |
235 if (!bounds.intersect(displBounds)) { | 236 if (!bounds.intersect(displBounds)) { |
236 return false; | 237 return false; |
237 } | 238 } |
238 SkAutoLockPixels alp_displacement(displ), alp_color(color); | 239 |
239 if (!displ.getPixels() || !color.getPixels()) { | 240 SkBitmap displacementBitmap; |
| 241 SkAutoAdoptImageAsN32Bitmap aai_displacement(displ, &displacementBitmap); |
| 242 if (NULL == displacementBitmap.getPixels()) { |
240 return false; | 243 return false; |
241 } | 244 } |
242 | 245 |
243 if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()
))) { | 246 SkBitmap colorBitmap; |
| 247 SkAutoAdoptImageAsN32Bitmap aai_color(color, &colorBitmap); |
| 248 if (NULL == colorBitmap.getPixels()) { |
244 return false; | 249 return false; |
245 } | 250 } |
246 | 251 |
| 252 SkBitmap dstBitmap; |
| 253 if (!dstBitmap.tryAllocPixels(colorBitmap.info().makeWH(bounds.width(), boun
ds.height()))) { |
| 254 return false; |
| 255 } |
| 256 |
247 SkVector scale = SkVector::Make(fScale, fScale); | 257 SkVector scale = SkVector::Make(fScale, fScale); |
248 ctx.ctm().mapVectors(&scale, 1); | 258 ctx.ctm().mapVectors(&scale, 1); |
249 SkIRect colorBounds = bounds; | 259 SkIRect colorBounds = bounds; |
250 colorBounds.offset(-colorOffset); | 260 colorBounds.offset(-colorOffset); |
251 | 261 |
252 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, | 262 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, &dstBitmap, |
253 &displ, colorOffset - displOffset, &color, colorBounds); | 263 &displacementBitmap, colorOffset - displOffset, |
| 264 &colorBitmap, colorBounds); |
254 | 265 |
| 266 |
| 267 displacementBitmap = SkBitmap(); |
| 268 colorBitmap = SkBitmap(); |
| 269 |
| 270 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 271 if (NULL == image) { |
| 272 return false; |
| 273 } |
| 274 dst.reset(image); |
255 offset->fX = bounds.left(); | 275 offset->fX = bounds.left(); |
256 offset->fY = bounds.top(); | 276 offset->fY = bounds.top(); |
257 return true; | 277 return true; |
258 } | 278 } |
259 | 279 |
260 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { | 280 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { |
261 if (getColorInput()) { | 281 if (getColorInput()) { |
262 getColorInput()->computeFastBounds(src, dst); | 282 getColorInput()->computeFastBounds(src, dst); |
263 } else { | 283 } else { |
264 *dst = src; | 284 *dst = src; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 GrTextureAccess fDisplacementAccess; | 395 GrTextureAccess fDisplacementAccess; |
376 GrCoordTransform fColorTransform; | 396 GrCoordTransform fColorTransform; |
377 GrTextureAccess fColorAccess; | 397 GrTextureAccess fColorAccess; |
378 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 398 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
379 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 399 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
380 SkVector fScale; | 400 SkVector fScale; |
381 | 401 |
382 typedef GrFragmentProcessor INHERITED; | 402 typedef GrFragmentProcessor INHERITED; |
383 }; | 403 }; |
384 | 404 |
385 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src,
const Context& ctx, | 405 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkImage* src, c
onst Context& ctx, |
386 SkBitmap* result, SkIPoint* offset)
const { | 406 SkAutoTUnref<const SkImage>& result
, |
387 SkBitmap colorBM = src; | 407 SkIPoint* offset) const { |
| 408 SkAutoTUnref<const SkImage> colorBM(SkRef(src)); |
388 SkIPoint colorOffset = SkIPoint::Make(0, 0); | 409 SkIPoint colorOffset = SkIPoint::Make(0, 0); |
389 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
&colorBM, | 410 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
colorBM, |
390 &colorOffset)) { | 411 &colorOffset)) { |
391 return false; | 412 return false; |
392 } | 413 } |
393 SkBitmap displacementBM = src; | 414 SkAutoTUnref<const SkImage> displacementBM(SkRef(src)); |
394 SkIPoint displacementOffset = SkIPoint::Make(0, 0); | 415 SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
395 if (getDisplacementInput() && | 416 if (getDisplacementInput() && |
396 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacemen
tBM, | 417 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, displacement
BM, |
397 &displacementOffset)) { | 418 &displacementOffset)) { |
398 return false; | 419 return false; |
399 } | 420 } |
400 SkIRect bounds; | 421 SkIRect bounds; |
401 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to | 422 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to |
402 // pad the color bitmap to bounds here. | 423 // pad the color bitmap to bounds here. |
403 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { | 424 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { |
404 return false; | 425 return false; |
405 } | 426 } |
406 SkIRect displBounds; | 427 SkIRect displBounds; |
407 if (!this->applyCropRect(ctx, proxy, displacementBM, | 428 if (!this->applyCropRect(ctx, proxy, displacementBM, |
408 &displacementOffset, &displBounds, &displacementBM)
) { | 429 &displacementOffset, &displBounds, displacementBM))
{ |
409 return false; | 430 return false; |
410 } | 431 } |
411 if (!bounds.intersect(displBounds)) { | 432 if (!bounds.intersect(displBounds)) { |
412 return false; | 433 return false; |
413 } | 434 } |
414 GrTexture* color = colorBM.getTexture(); | 435 GrTexture* color = colorBM->getTexture(); |
415 GrTexture* displacement = displacementBM.getTexture(); | 436 GrTexture* displacement = displacementBM->getTexture(); |
416 GrContext* context = color->getContext(); | 437 GrContext* context = color->getContext(); |
417 | 438 |
418 GrSurfaceDesc desc; | 439 GrSurfaceDesc desc; |
419 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 440 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
420 desc.fWidth = bounds.width(); | 441 desc.fWidth = bounds.width(); |
421 desc.fHeight = bounds.height(); | 442 desc.fHeight = bounds.height(); |
422 desc.fConfig = kSkia8888_GrPixelConfig; | 443 desc.fConfig = kSkia8888_GrPixelConfig; |
423 | 444 |
424 SkAutoTUnref<GrTexture> dst( | 445 SkAutoTUnref<GrTexture> dst( |
425 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); | 446 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); |
(...skipping 17 matching lines...) Expand all Loading... |
443 displacement, | 464 displacement, |
444 offsetMatrix, | 465 offsetMatrix, |
445 color))->unref(); | 466 color))->unref(); |
446 SkIRect colorBounds = bounds; | 467 SkIRect colorBounds = bounds; |
447 colorBounds.offset(-colorOffset); | 468 colorBounds.offset(-colorOffset); |
448 SkMatrix matrix; | 469 SkMatrix matrix; |
449 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), | 470 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
450 -SkIntToScalar(colorBounds.y())); | 471 -SkIntToScalar(colorBounds.y())); |
451 context->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, matrix, | 472 context->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, matrix, |
452 SkRect::Make(colorBounds)); | 473 SkRect::Make(colorBounds)); |
| 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 |