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

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

Issue 12787007: Fix for displacement mapping (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 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 | « gm/displacement.cpp ('k') | no next file » | 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 "SkFlattenableBuffers.h" 9 #include "SkFlattenableBuffers.h"
10 #include "SkUnPreMultiply.h" 10 #include "SkUnPreMultiply.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
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 static const SkScalar Half8bit = SkFloatToScalar(255.0f * 0.5f);
54 const int dstW = displ->width(); 53 const int dstW = displ->width();
55 const int dstH = displ->height(); 54 const int dstH = displ->height();
56 const int srcW = src->width(); 55 const int srcW = src->width();
57 const int srcH = src->height(); 56 const int srcH = src->height();
58 const SkScalar scaleX = SkScalarMul(SkScalarMul(scale, SkIntToScalar(dstW)), Inv8bit); 57 const SkScalar scaleForColor = SkScalarMul(scale, Inv8bit);
59 const SkScalar scaleY = SkScalarMul(SkScalarMul(scale, SkIntToScalar(dstH)), Inv8bit); 58 const SkScalar scaleAdj = SK_ScalarHalf - SkScalarMul(scale, SK_ScalarHalf);
60 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); 59 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
61 for (int y = 0; y < dstH; ++y) { 60 for (int y = 0; y < dstH; ++y) {
62 const SkPMColor* displPtr = displ->getAddr32(0, y); 61 const SkPMColor* displPtr = displ->getAddr32(0, y);
63 SkPMColor* dstPtr = dst->getAddr32(0, y); 62 SkPMColor* dstPtr = dst->getAddr32(0, y);
64 for (int x = 0; x < dstW; ++x, ++displPtr, ++dstPtr) { 63 for (int x = 0; x < dstW; ++x, ++displPtr, ++dstPtr) {
65 const SkScalar displX = 64 const SkScalar displX = SkScalarMul(scaleForColor,
66 SkScalarMul(scaleX, SkIntToScalar(getValue<typeX>(*displPtr, tab le))-Half8bit); 65 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj;
67 const SkScalar displY = 66 const SkScalar displY = SkScalarMul(scaleForColor,
68 SkScalarMul(scaleY, SkIntToScalar(getValue<typeY>(*displPtr, tab le))-Half8bit); 67 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj;
69 const int coordX = x + SkScalarRoundToInt(displX); 68 // Truncate the displacement values
sugoi 2013/03/15 17:47:06 I didn't see any SkScalarTruncToInt or similar fun
bsalomon 2013/03/15 19:09:53 Doesn't cast do what you want? If we care about fi
70 const int coordY = y + SkScalarRoundToInt(displY); 69 const int srcX = x + (displX < 0 ? SkScalarCeilToInt(displX) :
71 *dstPtr = ((coordX < 0) || (coordX >= srcW) || (coordY < 0) || (coor dY >= srcH)) ? 70 SkScalarFloorToInt(displX));
72 0 : *(src->getAddr32(coordX, coordY)); 71 const int srcY = y + (displY < 0 ? SkScalarCeilToInt(displY) :
72 SkScalarFloorToInt(displY));
73 *dstPtr = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= sr cH)) ?
74 0 : *(src->getAddr32(srcX, srcY));
73 } 75 }
74 } 76 }
75 } 77 }
76 78
77 template<SkDisplacementMapEffect::ChannelSelectorType typeX> 79 template<SkDisplacementMapEffect::ChannelSelectorType typeX>
78 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe lector, 80 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe lector,
79 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma p* src) 81 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma p* src)
80 { 82 {
81 switch (yChannelSelector) { 83 switch (yChannelSelector) {
82 case SkDisplacementMapEffect::kR_ChannelSelectorType: 84 case SkDisplacementMapEffect::kR_ChannelSelectorType:
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 fColorAccess.getTexture() == s.fColorAccess.getTexture() && 340 fColorAccess.getTexture() == s.fColorAccess.getTexture() &&
339 fXChannelSelector == s.fXChannelSelector && 341 fXChannelSelector == s.fXChannelSelector &&
340 fYChannelSelector == s.fYChannelSelector && 342 fYChannelSelector == s.fYChannelSelector &&
341 fScale == s.fScale; 343 fScale == s.fScale;
342 } 344 }
343 345
344 const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const { 346 const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const {
345 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance(); 347 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance();
346 } 348 }
347 349
348 void GrDisplacementMapEffect::getConstantColorComponents(GrColor* color, 350 void GrDisplacementMapEffect::getConstantColorComponents(GrColor*,
349 uint32_t* validFlags) c onst { 351 uint32_t* validFlags) c onst {
350 // Any displacement offset bringing a pixel out of bounds will output a colo r of (0,0,0,0), 352 // Any displacement offset bringing a pixel out of bounds will output a colo r of (0,0,0,0),
351 // so the only way we'd get a constant alpha is if the input color image has a constant alpha 353 // so the only way we'd get a constant alpha is if the input color image has a constant alpha
352 // and no displacement offset push any texture coordinates out of bounds OR if the constant 354 // and no displacement offset push any texture coordinates out of bounds OR if the constant
353 // alpha is 0. Since this isn't trivial to compute at this point, let's assu me the output is 355 // alpha is 0. Since this isn't trivial to compute at this point, let's assu me the output is
354 // not of constant color when a displacement effect is applied. 356 // not of constant color when a displacement effect is applied.
355 *validFlags = 0; 357 *validFlags = 0;
356 } 358 }
357 359
358 /////////////////////////////////////////////////////////////////////////////// 360 ///////////////////////////////////////////////////////////////////////////////
359 361
360 GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect); 362 GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect);
361 363
362 GrEffectRef* GrDisplacementMapEffect::TestCreate(SkMWCRandom* random, 364 GrEffectRef* GrDisplacementMapEffect::TestCreate(SkMWCRandom* random,
363 GrContext* context, 365 GrContext*,
364 GrTexture* textures[]) { 366 GrTexture* textures[]) {
365 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 367 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
366 GrEffectUnitTest::kAlphaTextureIdx; 368 GrEffectUnitTest::kAlphaTextureIdx;
367 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 369 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
368 GrEffectUnitTest::kAlphaTextureIdx; 370 GrEffectUnitTest::kAlphaTextureIdx;
369 static const int kMaxComponent = 4; 371 static const int kMaxComponent = 4;
370 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = 372 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
371 static_cast<SkDisplacementMapEffect::ChannelSelectorType>( 373 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
372 random->nextRangeU(1, kMaxComponent)); 374 random->nextRangeU(1, kMaxComponent));
373 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = 375 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
374 static_cast<SkDisplacementMapEffect::ChannelSelectorType>( 376 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
375 random->nextRangeU(1, kMaxComponent)); 377 random->nextRangeU(1, kMaxComponent));
376 SkScalar scale = random->nextUScalar1(); 378 SkScalar scale = random->nextRangeScalar(0, SkFloatToScalar(100.0f));
sugoi 2013/03/15 17:47:06 Changed the test range so that it's more appropria
377 379
378 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s cale, 380 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s cale,
379 textures[texIdxDispl], textures[texId xColor]); 381 textures[texIdxDispl], textures[texId xColor]);
380 } 382 }
381 383
382 /////////////////////////////////////////////////////////////////////////////// 384 ///////////////////////////////////////////////////////////////////////////////
383 385
384 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor y& factory, 386 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor y& factory,
385 const GrEffectRef& effect) 387 const GrEffectRef& effect)
386 : INHERITED(factory) 388 : INHERITED(factory)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 GrTexture* colorTex = displacementMap.texture(1); 486 GrTexture* colorTex = displacementMap.texture(1);
485 fDisplacementEffectMatrix.setData(uman, 487 fDisplacementEffectMatrix.setData(uman,
486 GrEffect::MakeDivByTextureWHMatrix(displTex ), 488 GrEffect::MakeDivByTextureWHMatrix(displTex ),
487 stage.getCoordChangeMatrix(), 489 stage.getCoordChangeMatrix(),
488 displTex); 490 displTex);
489 fColorEffectMatrix.setData(uman, 491 fColorEffectMatrix.setData(uman,
490 GrEffect::MakeDivByTextureWHMatrix(colorTex), 492 GrEffect::MakeDivByTextureWHMatrix(colorTex),
491 stage.getCoordChangeMatrix(), 493 stage.getCoordChangeMatrix(),
492 colorTex); 494 colorTex);
493 495
494 uman.set2f(fScaleUni, SkScalarToFloat(displacementMap.scale()), 496 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe x->width()));
sugoi 2013/03/15 17:47:06 The scale now has to be divided by the size in gpu
495 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? 497 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe x->height()));
496 SkScalarToFloat(displacementMap.scale()) : 498 uman.set2f(fScaleUni, SkScalarToFloat(scaleX),
497 SkScalarToFloat(-displacementMap.scale())); 499 colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
500 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
498 } 501 }
499 502
500 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& sta ge, 503 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& sta ge,
501 const GrGLCaps&) { 504 const GrGLCaps&) {
502 const GrDisplacementMapEffect& displacementMap = 505 const GrDisplacementMapEffect& displacementMap =
503 GetEffectFromStage<GrDisplacementMapEffect>(stage); 506 GetEffectFromStage<GrDisplacementMapEffect>(stage);
504 507
505 GrTexture* displTex = displacementMap.texture(0); 508 GrTexture* displTex = displacementMap.texture(0);
506 GrTexture* colorTex = displacementMap.texture(1); 509 GrTexture* colorTex = displacementMap.texture(1);
507 510
508 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(displTex), 511 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(displTex),
509 stage.getCoordChangeMatrix(), 512 stage.getCoordChangeMatrix(),
510 displTex); 513 displTex);
511 514
512 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(colorTex), 515 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(colorTex),
513 stage.getCoordChangeMatrix(), 516 stage.getCoordChangeMatrix(),
514 colorTex); 517 colorTex);
515 518
516 colorKey <<= GrGLEffectMatrix::kKeyBits; 519 colorKey <<= GrGLEffectMatrix::kKeyBits;
517 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits); 520 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits);
518 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits + 521 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits +
519 SkDisplacementMapEff ect::kKeyBits); 522 SkDisplacementMapEff ect::kKeyBits);
520 523
521 return xKey | yKey | displKey | colorKey; 524 return xKey | yKey | displKey | colorKey;
522 } 525 }
523 #endif 526 #endif
OLDNEW
« no previous file with comments | « gm/displacement.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698