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 SkImage& src, |
211 const Context& ctx, | 213 const Context& ctx, |
212 SkBitmap* dst, | 214 SkAutoTUnref<SkImage>& dst, |
213 SkIPoint* offset) const { | 215 SkIPoint* offset) const { |
214 SkBitmap displ = src, color = src; | 216 SkBitmap dstBitmap; |
215 const SkImageFilter* colorInput = getColorInput(); | 217 SkBitmap displacementBitmap; |
216 const SkImageFilter* displInput = getDisplacementInput(); | 218 SkBitmap colorBitmap; |
| 219 SkIRect bounds; |
217 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); | 220 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0,
0); |
218 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorO
ffset)) || | 221 { |
219 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displO
ffset))) { | 222 SkAutoTUnref<SkImage> displ(SkRef(&src)); |
| 223 SkAutoTUnref<SkImage> color(SkRef(&src)); |
| 224 const SkImageFilter* colorInput = getColorInput(); |
| 225 const SkImageFilter* displInput = getDisplacementInput(); |
| 226 |
| 227 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, color, &col
orOffset)) || |
| 228 (displInput && !displInput->filterImage(proxy, src, ctx, displ, &dis
plOffset))) { |
| 229 return false; |
| 230 } |
| 231 #if 0 // TODO COLORTYPE |
| 232 if ((displ.colorType() != kN32_SkColorType) || |
| 233 (color.colorType() != kN32_SkColorType)) { |
| 234 return false; |
| 235 } |
| 236 #endif |
| 237 // Since computeDisplacement does bounds checking on color pixel access,
we don't need to pad |
| 238 // the color bitmap to bounds here. |
| 239 if (!this->applyCropRect(ctx, *color, colorOffset, &bounds)) { |
| 240 return false; |
| 241 } |
| 242 SkIRect displBounds; |
| 243 if (!this->applyCropRect(ctx, proxy, *displ, &displOffset, &displBounds,
displ)) { |
| 244 return false; |
| 245 } |
| 246 if (!bounds.intersect(displBounds)) { |
| 247 return false; |
| 248 } |
| 249 |
| 250 |
| 251 if (!as_IB(displ)->getROPixels(&displacementBitmap)) { |
| 252 return false; |
| 253 } |
| 254 |
| 255 if (!as_IB(color)->getROPixels(&colorBitmap)) { |
| 256 return false; |
| 257 } |
| 258 } |
| 259 { |
| 260 SkAutoLockPixels alp_displacement(displacementBitmap); |
| 261 SkAutoLockPixels alp_color(colorBitmap); |
| 262 if (!displacementBitmap.getPixels() || !colorBitmap.getPixels()) { |
| 263 return false; |
| 264 } |
| 265 |
| 266 if (!dstBitmap.tryAllocPixels(colorBitmap.info().makeWH(bounds.width(),
bounds.height()))) { |
| 267 return false; |
| 268 } |
| 269 |
| 270 SkVector scale = SkVector::Make(fScale, fScale); |
| 271 ctx.ctm().mapVectors(&scale, 1); |
| 272 SkIRect colorBounds = bounds; |
| 273 colorBounds.offset(-colorOffset); |
| 274 |
| 275 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, &dstBit
map, |
| 276 &displacementBitmap, colorOffset - displOffset, |
| 277 &colorBitmap, colorBounds); |
| 278 } |
| 279 |
| 280 displacementBitmap = SkBitmap(); |
| 281 colorBitmap = SkBitmap(); |
| 282 |
| 283 SkImage* image = SkNewImageFromBitmap(dstBitmap, true, NULL); |
| 284 if (NULL == image) { |
220 return false; | 285 return false; |
221 } | 286 } |
222 if ((displ.colorType() != kN32_SkColorType) || | 287 dst.reset(image); |
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 // the color bitmap to bounds here. | |
229 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { | |
230 return false; | |
231 } | |
232 SkIRect displBounds; | |
233 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &dis
pl)) { | |
234 return false; | |
235 } | |
236 if (!bounds.intersect(displBounds)) { | |
237 return false; | |
238 } | |
239 SkAutoLockPixels alp_displacement(displ), alp_color(color); | |
240 if (!displ.getPixels() || !color.getPixels()) { | |
241 return false; | |
242 } | |
243 | |
244 if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()
))) { | |
245 return false; | |
246 } | |
247 | |
248 SkVector scale = SkVector::Make(fScale, fScale); | |
249 ctx.ctm().mapVectors(&scale, 1); | |
250 SkIRect colorBounds = bounds; | |
251 colorBounds.offset(-colorOffset); | |
252 | |
253 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, | |
254 &displ, colorOffset - displOffset, &color, colorBounds); | |
255 | |
256 offset->fX = bounds.left(); | 288 offset->fX = bounds.left(); |
257 offset->fY = bounds.top(); | 289 offset->fY = bounds.top(); |
258 return true; | 290 return true; |
259 } | 291 } |
260 | 292 |
261 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { | 293 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst)
const { |
262 if (getColorInput()) { | 294 if (getColorInput()) { |
263 getColorInput()->computeFastBounds(src, dst); | 295 getColorInput()->computeFastBounds(src, dst); |
264 } else { | 296 } else { |
265 *dst = src; | 297 *dst = src; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 GrTextureAccess fDisplacementAccess; | 408 GrTextureAccess fDisplacementAccess; |
377 GrCoordTransform fColorTransform; | 409 GrCoordTransform fColorTransform; |
378 GrTextureAccess fColorAccess; | 410 GrTextureAccess fColorAccess; |
379 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 411 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
380 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 412 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
381 SkVector fScale; | 413 SkVector fScale; |
382 | 414 |
383 typedef GrFragmentProcessor INHERITED; | 415 typedef GrFragmentProcessor INHERITED; |
384 }; | 416 }; |
385 | 417 |
386 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src,
const Context& ctx, | 418 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, SkImage& src, const C
ontext& ctx, |
387 SkBitmap* result, SkIPoint* offset)
const { | 419 SkAutoTUnref<SkImage>& result, SkIP
oint* offset) const { |
388 SkBitmap colorBM = src; | 420 SkAutoTUnref<SkImage> colorBM(SkRef(&src)); |
389 SkIPoint colorOffset = SkIPoint::Make(0, 0); | 421 SkIPoint colorOffset = SkIPoint::Make(0, 0); |
390 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
&colorBM, | 422 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx,
colorBM, |
391 &colorOffset)) { | 423 &colorOffset)) { |
392 return false; | 424 return false; |
393 } | 425 } |
394 SkBitmap displacementBM = src; | 426 SkAutoTUnref<SkImage> displacementBM(SkRef(&src)); |
395 SkIPoint displacementOffset = SkIPoint::Make(0, 0); | 427 SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
396 if (getDisplacementInput() && | 428 if (getDisplacementInput() && |
397 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacemen
tBM, | 429 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, displacement
BM, |
398 &displacementOffset)) { | 430 &displacementOffset)) { |
399 return false; | 431 return false; |
400 } | 432 } |
401 SkIRect bounds; | 433 SkIRect bounds; |
402 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to | 434 // Since GrDisplacementMapEffect does bounds checking on color pixel access,
we don't need to |
403 // pad the color bitmap to bounds here. | 435 // pad the color bitmap to bounds here. |
404 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { | 436 if (!this->applyCropRect(ctx, *colorBM, colorOffset, &bounds)) { |
405 return false; | 437 return false; |
406 } | 438 } |
407 SkIRect displBounds; | 439 SkIRect displBounds; |
408 if (!this->applyCropRect(ctx, proxy, displacementBM, | 440 if (!this->applyCropRect(ctx, proxy, *displacementBM, |
409 &displacementOffset, &displBounds, &displacementBM)
) { | 441 &displacementOffset, &displBounds, displacementBM))
{ |
410 return false; | 442 return false; |
411 } | 443 } |
412 if (!bounds.intersect(displBounds)) { | 444 if (!bounds.intersect(displBounds)) { |
413 return false; | 445 return false; |
414 } | 446 } |
415 GrTexture* color = colorBM.getTexture(); | 447 GrTexture* color = colorBM->getTexture(); |
416 GrTexture* displacement = displacementBM.getTexture(); | 448 GrTexture* displacement = displacementBM->getTexture(); |
417 GrContext* context = color->getContext(); | 449 GrContext* context = color->getContext(); |
418 | 450 |
419 GrSurfaceDesc desc; | 451 GrSurfaceDesc desc; |
420 desc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; | 452 desc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; |
421 desc.fWidth = bounds.width(); | 453 desc.fWidth = bounds.width(); |
422 desc.fHeight = bounds.height(); | 454 desc.fHeight = bounds.height(); |
423 desc.fConfig = kSkia8888_GrPixelConfig; | 455 desc.fConfig = kSkia8888_GrPixelConfig; |
424 | 456 |
425 SkAutoTUnref<GrTexture> dst( | 457 SkAutoTUnref<GrTexture> dst( |
426 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); | 458 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); |
(...skipping 18 matching lines...) Expand all Loading... |
445 scale, | 477 scale, |
446 displacement, | 478 displacement, |
447 offsetMatrix, | 479 offsetMatrix, |
448 color))->unref(); | 480 color))->unref(); |
449 SkIRect colorBounds = bounds; | 481 SkIRect colorBounds = bounds; |
450 colorBounds.offset(-colorOffset); | 482 colorBounds.offset(-colorOffset); |
451 SkMatrix matrix; | 483 SkMatrix matrix; |
452 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), | 484 matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
453 -SkIntToScalar(colorBounds.y())); | 485 -SkIntToScalar(colorBounds.y())); |
454 context->drawRect(paint, matrix, SkRect::Make(colorBounds)); | 486 context->drawRect(paint, matrix, SkRect::Make(colorBounds)); |
| 487 if (!WrapTexture(dst, bounds.width(), bounds.height(), result)) { |
| 488 return false; |
| 489 } |
455 offset->fX = bounds.left(); | 490 offset->fX = bounds.left(); |
456 offset->fY = bounds.top(); | 491 offset->fY = bounds.top(); |
457 WrapTexture(dst, bounds.width(), bounds.height(), result); | |
458 return true; | 492 return true; |
459 } | 493 } |
460 | 494 |
461 /////////////////////////////////////////////////////////////////////////////// | 495 /////////////////////////////////////////////////////////////////////////////// |
462 | 496 |
463 GrDisplacementMapEffect::GrDisplacementMapEffect( | 497 GrDisplacementMapEffect::GrDisplacementMapEffect( |
464 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, | 498 SkDisplacementMapEffect::ChannelSelectorType xChann
elSelector, |
465 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, | 499 SkDisplacementMapEffect::ChannelSelectorType yChann
elSelector, |
466 const SkVector& scale, | 500 const SkVector& scale, |
467 GrTexture* displacement, | 501 GrTexture* displacement, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 const GrGLCaps&, GrProcessorKeyBuilder* b
) { | 666 const GrGLCaps&, GrProcessorKeyBuilder* b
) { |
633 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); | 667 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMap
Effect>(); |
634 | 668 |
635 uint32_t xKey = displacementMap.xChannelSelector(); | 669 uint32_t xKey = displacementMap.xChannelSelector(); |
636 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; | 670 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBit
s; |
637 | 671 |
638 b->add32(xKey | yKey); | 672 b->add32(xKey | yKey); |
639 } | 673 } |
640 #endif | 674 #endif |
641 | 675 |
OLD | NEW |