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

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

Issue 198003008: Implement support for expanding crop rects in image filters (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix ImageFilterTest Created 6 years, 9 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/SkColorFilterImageFilter.cpp ('k') | src/effects/SkDropShadowImageFilter.cpp » ('j') | 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 * 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 "SkReadBuffer.h" 9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h" 10 #include "SkWriteBuffer.h"
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 const SkImageFilter* displInput = getDisplacementInput(); 202 const SkImageFilter* displInput = getDisplacementInput();
203 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0, 0); 203 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0, 0);
204 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorO ffset)) || 204 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorO ffset)) ||
205 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displO ffset))) { 205 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displO ffset))) {
206 return false; 206 return false;
207 } 207 }
208 if ((displ.colorType() != kPMColor_SkColorType) || 208 if ((displ.colorType() != kPMColor_SkColorType) ||
209 (color.colorType() != kPMColor_SkColorType)) { 209 (color.colorType() != kPMColor_SkColorType)) {
210 return false; 210 return false;
211 } 211 }
212
213 SkAutoLockPixels alp_displacement(displ), alp_color(color);
214 if (!displ.getPixels() || !color.getPixels()) {
215 return false;
216 }
217 SkIRect bounds; 212 SkIRect bounds;
218 color.getBounds(&bounds); 213 // Since computeDisplacement does bounds checking on color pixel access, we don't need to pad
219 bounds.offset(colorOffset); 214 // the color bitmap to bounds here.
220 if (!this->applyCropRect(&bounds, ctx.ctm())) { 215 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) {
221 return false; 216 return false;
222 } 217 }
223 SkIRect displBounds; 218 SkIRect displBounds;
224 displ.getBounds(&displBounds); 219 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &dis pl)) {
225 displBounds.offset(displOffset);
226 if (!this->applyCropRect(&displBounds, ctx.ctm())) {
227 return false; 220 return false;
228 } 221 }
229 if (!bounds.intersect(displBounds)) { 222 if (!bounds.intersect(displBounds)) {
230 return false; 223 return false;
231 } 224 }
225 SkAutoLockPixels alp_displacement(displ), alp_color(color);
226 if (!displ.getPixels() || !color.getPixels()) {
227 return false;
228 }
232 229
233 dst->setConfig(color.config(), bounds.width(), bounds.height()); 230 dst->setConfig(color.config(), bounds.width(), bounds.height());
234 if (!dst->allocPixels()) { 231 if (!dst->allocPixels()) {
235 return false; 232 return false;
236 } 233 }
237 234
238 SkVector scale = SkVector::Make(fScale, fScale); 235 SkVector scale = SkVector::Make(fScale, fScale);
239 ctx.ctm().mapVectors(&scale, 1); 236 ctx.ctm().mapVectors(&scale, 1);
240 SkIRect colorBounds = bounds; 237 SkIRect colorBounds = bounds;
241 colorBounds.offset(-colorOffset); 238 colorBounds.offset(-colorOffset);
242 239
243 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, 240 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst,
244 &displ, colorOffset - displOffset, &color, colorBounds); 241 &displ, colorOffset - displOffset, &color, colorBounds);
245 242
246 offset->fX = bounds.left(); 243 offset->fX = bounds.left();
247 offset->fY = bounds.top(); 244 offset->fY = bounds.top();
248 return true; 245 return true;
249 } 246 }
250 247
251 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const { 248 void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const {
252 if (getColorInput()) { 249 if (getColorInput()) {
253 getColorInput()->computeFastBounds(src, dst); 250 getColorInput()->computeFastBounds(src, dst);
254 } else { 251 } else {
255 *dst = src; 252 *dst = src;
256 } 253 }
254 dst->outset(fScale * SK_ScalarHalf, fScale * SK_ScalarHalf);
257 } 255 }
258 256
259 bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, 257 bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
260 SkIRect* dst) const { 258 SkIRect* dst) const {
261 if (getColorInput()) { 259 SkIRect bounds = src;
262 return getColorInput()->filterBounds(src, ctm, dst); 260 if (getColorInput() && !getColorInput()->filterBounds(src, ctm, &bounds)) {
261 return false;
263 } 262 }
264 *dst = src; 263 bounds.outset(SkScalarCeilToInt(fScale * SK_ScalarHalf),
264 SkScalarCeilToInt(fScale * SK_ScalarHalf));
265 *dst = bounds;
265 return true; 266 return true;
266 } 267 }
267 268
268 /////////////////////////////////////////////////////////////////////////////// 269 ///////////////////////////////////////////////////////////////////////////////
269 270
270 #if SK_SUPPORT_GPU 271 #if SK_SUPPORT_GPU
271 class GrGLDisplacementMapEffect : public GrGLEffect { 272 class GrGLDisplacementMapEffect : public GrGLEffect {
272 public: 273 public:
273 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, 274 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory,
274 const GrDrawEffect& drawEffect); 275 const GrDrawEffect& drawEffect);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 }; 350 };
350 351
351 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, 352 bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
352 SkBitmap* result, SkIPoint* offset) const { 353 SkBitmap* result, SkIPoint* offset) const {
353 SkBitmap colorBM = src; 354 SkBitmap colorBM = src;
354 SkIPoint colorOffset = SkIPoint::Make(0, 0); 355 SkIPoint colorOffset = SkIPoint::Make(0, 0);
355 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM, 356 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM,
356 &colorOffset)) { 357 &colorOffset)) {
357 return false; 358 return false;
358 } 359 }
359 GrTexture* color = colorBM.getTexture();
360 SkBitmap displacementBM = src; 360 SkBitmap displacementBM = src;
361 SkIPoint displacementOffset = SkIPoint::Make(0, 0); 361 SkIPoint displacementOffset = SkIPoint::Make(0, 0);
362 if (getDisplacementInput() && 362 if (getDisplacementInput() &&
363 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacemen tBM, 363 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacemen tBM,
364 &displacementOffset)) { 364 &displacementOffset)) {
365 return false; 365 return false;
366 } 366 }
367 SkIRect bounds;
368 // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to
369 // pad the color bitmap to bounds here.
370 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) {
371 return false;
372 }
373 SkIRect displBounds;
374 if (!this->applyCropRect(ctx, proxy, displacementBM,
375 &displacementOffset, &displBounds, &displacementBM) ) {
376 return false;
377 }
378 if (!bounds.intersect(displBounds)) {
379 return false;
380 }
381 GrTexture* color = colorBM.getTexture();
367 GrTexture* displacement = displacementBM.getTexture(); 382 GrTexture* displacement = displacementBM.getTexture();
368 GrContext* context = color->getContext(); 383 GrContext* context = color->getContext();
369 384
370 GrTextureDesc desc; 385 GrTextureDesc desc;
371 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; 386 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
372 desc.fWidth = colorBM.width(); 387 desc.fWidth = colorBM.width();
373 desc.fHeight = colorBM.height(); 388 desc.fHeight = colorBM.height();
374 desc.fConfig = kSkia8888_GrPixelConfig; 389 desc.fConfig = kSkia8888_GrPixelConfig;
375 390
376 GrAutoScratchTexture ast(context, desc); 391 GrAutoScratchTexture ast(context, desc);
377 SkAutoTUnref<GrTexture> dst(ast.detach()); 392 SkAutoTUnref<GrTexture> dst(ast.detach());
378 393
379 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); 394 GrContext::AutoRenderTarget art(context, dst->asRenderTarget());
380 395
381 SkVector scale = SkVector::Make(fScale, fScale); 396 SkVector scale = SkVector::Make(fScale, fScale);
382 ctx.ctm().mapVectors(&scale, 1); 397 ctx.ctm().mapVectors(&scale, 1);
383 SkIRect bounds;
384 colorBM.getBounds(&bounds);
385 bounds.offset(colorOffset);
386 if (!this->applyCropRect(&bounds, ctx.ctm())) {
387 return false;
388 }
389 SkIRect displBounds;
390 displacementBM.getBounds(&displBounds);
391 displBounds.offset(displacementOffset);
392 if (!this->applyCropRect(&displBounds, ctx.ctm())) {
393 return false;
394 }
395 if (!bounds.intersect(displBounds)) {
396 return false;
397 }
398 398
399 GrPaint paint; 399 GrPaint paint;
400 SkMatrix offsetMatrix = GrEffect::MakeDivByTextureWHMatrix(displacement); 400 SkMatrix offsetMatrix = GrEffect::MakeDivByTextureWHMatrix(displacement);
401 offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset. fX), 401 offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset. fX),
402 SkIntToScalar(colorOffset.fY - displacementOffset. fY)); 402 SkIntToScalar(colorOffset.fY - displacementOffset. fY));
403 403
404 paint.addColorEffect( 404 paint.addColorEffect(
405 GrDisplacementMapEffect::Create(fXChannelSelector, 405 GrDisplacementMapEffect::Create(fXChannelSelector,
406 fYChannelSelector, 406 fYChannelSelector,
407 scale, 407 scale,
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 const GrGLCaps&) { 602 const GrGLCaps&) {
603 const GrDisplacementMapEffect& displacementMap = 603 const GrDisplacementMapEffect& displacementMap =
604 drawEffect.castEffect<GrDisplacementMapEffect>(); 604 drawEffect.castEffect<GrDisplacementMapEffect>();
605 605
606 EffectKey xKey = displacementMap.xChannelSelector(); 606 EffectKey xKey = displacementMap.xChannelSelector();
607 EffectKey yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBi ts; 607 EffectKey yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBi ts;
608 608
609 return xKey | yKey; 609 return xKey | yKey;
610 } 610 }
611 #endif 611 #endif
OLDNEW
« no previous file with comments | « src/effects/SkColorFilterImageFilter.cpp ('k') | src/effects/SkDropShadowImageFilter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698