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

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 | « include/core/SkScalar.h ('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
70 const int coordY = y + SkScalarRoundToInt(displY); 69 const int srcX = x + SkScalarTruncToInt(displX);
71 *dstPtr = ((coordX < 0) || (coordX >= srcW) || (coordY < 0) || (coor dY >= srcH)) ? 70 const int srcY = y + SkScalarTruncToInt(displY);
72 0 : *(src->getAddr32(coordX, coordY)); 71 *dstPtr = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= sr cH)) ?
72 0 : *(src->getAddr32(srcX, srcY));
73 } 73 }
74 } 74 }
75 } 75 }
76 76
77 template<SkDisplacementMapEffect::ChannelSelectorType typeX> 77 template<SkDisplacementMapEffect::ChannelSelectorType typeX>
78 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe lector, 78 void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSe lector,
79 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma p* src) 79 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitma p* src)
80 { 80 {
81 switch (yChannelSelector) { 81 switch (yChannelSelector) {
82 case SkDisplacementMapEffect::kR_ChannelSelectorType: 82 case SkDisplacementMapEffect::kR_ChannelSelectorType:
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 fColorAccess.getTexture() == s.fColorAccess.getTexture() && 338 fColorAccess.getTexture() == s.fColorAccess.getTexture() &&
339 fXChannelSelector == s.fXChannelSelector && 339 fXChannelSelector == s.fXChannelSelector &&
340 fYChannelSelector == s.fYChannelSelector && 340 fYChannelSelector == s.fYChannelSelector &&
341 fScale == s.fScale; 341 fScale == s.fScale;
342 } 342 }
343 343
344 const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const { 344 const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const {
345 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance(); 345 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance();
346 } 346 }
347 347
348 void GrDisplacementMapEffect::getConstantColorComponents(GrColor* color, 348 void GrDisplacementMapEffect::getConstantColorComponents(GrColor*,
349 uint32_t* validFlags) c onst { 349 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), 350 // 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 351 // 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 352 // 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 353 // 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. 354 // not of constant color when a displacement effect is applied.
355 *validFlags = 0; 355 *validFlags = 0;
356 } 356 }
357 357
358 /////////////////////////////////////////////////////////////////////////////// 358 ///////////////////////////////////////////////////////////////////////////////
359 359
360 GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect); 360 GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect);
361 361
362 GrEffectRef* GrDisplacementMapEffect::TestCreate(SkMWCRandom* random, 362 GrEffectRef* GrDisplacementMapEffect::TestCreate(SkMWCRandom* random,
363 GrContext* context, 363 GrContext*,
364 GrTexture* textures[]) { 364 GrTexture* textures[]) {
365 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 365 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
366 GrEffectUnitTest::kAlphaTextureIdx; 366 GrEffectUnitTest::kAlphaTextureIdx;
367 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 367 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
368 GrEffectUnitTest::kAlphaTextureIdx; 368 GrEffectUnitTest::kAlphaTextureIdx;
369 static const int kMaxComponent = 4; 369 static const int kMaxComponent = 4;
370 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = 370 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
371 static_cast<SkDisplacementMapEffect::ChannelSelectorType>( 371 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
372 random->nextRangeU(1, kMaxComponent)); 372 random->nextRangeU(1, kMaxComponent));
373 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = 373 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
374 static_cast<SkDisplacementMapEffect::ChannelSelectorType>( 374 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
375 random->nextRangeU(1, kMaxComponent)); 375 random->nextRangeU(1, kMaxComponent));
376 SkScalar scale = random->nextUScalar1(); 376 SkScalar scale = random->nextRangeScalar(0, SkFloatToScalar(100.0f));
377 377
378 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s cale, 378 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s cale,
379 textures[texIdxDispl], textures[texId xColor]); 379 textures[texIdxDispl], textures[texId xColor]);
380 } 380 }
381 381
382 /////////////////////////////////////////////////////////////////////////////// 382 ///////////////////////////////////////////////////////////////////////////////
383 383
384 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor y& factory, 384 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor y& factory,
385 const GrEffectRef& effect) 385 const GrEffectRef& effect)
386 : INHERITED(factory) 386 : INHERITED(factory)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 GrTexture* colorTex = displacementMap.texture(1); 484 GrTexture* colorTex = displacementMap.texture(1);
485 fDisplacementEffectMatrix.setData(uman, 485 fDisplacementEffectMatrix.setData(uman,
486 GrEffect::MakeDivByTextureWHMatrix(displTex ), 486 GrEffect::MakeDivByTextureWHMatrix(displTex ),
487 stage.getCoordChangeMatrix(), 487 stage.getCoordChangeMatrix(),
488 displTex); 488 displTex);
489 fColorEffectMatrix.setData(uman, 489 fColorEffectMatrix.setData(uman,
490 GrEffect::MakeDivByTextureWHMatrix(colorTex), 490 GrEffect::MakeDivByTextureWHMatrix(colorTex),
491 stage.getCoordChangeMatrix(), 491 stage.getCoordChangeMatrix(),
492 colorTex); 492 colorTex);
493 493
494 uman.set2f(fScaleUni, SkScalarToFloat(displacementMap.scale()), 494 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe x->width()));
495 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? 495 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe x->height()));
496 SkScalarToFloat(displacementMap.scale()) : 496 uman.set2f(fScaleUni, SkScalarToFloat(scaleX),
497 SkScalarToFloat(-displacementMap.scale())); 497 colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
498 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
498 } 499 }
499 500
500 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& sta ge, 501 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& sta ge,
501 const GrGLCaps&) { 502 const GrGLCaps&) {
502 const GrDisplacementMapEffect& displacementMap = 503 const GrDisplacementMapEffect& displacementMap =
503 GetEffectFromStage<GrDisplacementMapEffect>(stage); 504 GetEffectFromStage<GrDisplacementMapEffect>(stage);
504 505
505 GrTexture* displTex = displacementMap.texture(0); 506 GrTexture* displTex = displacementMap.texture(0);
506 GrTexture* colorTex = displacementMap.texture(1); 507 GrTexture* colorTex = displacementMap.texture(1);
507 508
508 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(displTex), 509 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(displTex),
509 stage.getCoordChangeMatrix(), 510 stage.getCoordChangeMatrix(),
510 displTex); 511 displTex);
511 512
512 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(colorTex), 513 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa trix(colorTex),
513 stage.getCoordChangeMatrix(), 514 stage.getCoordChangeMatrix(),
514 colorTex); 515 colorTex);
515 516
516 colorKey <<= GrGLEffectMatrix::kKeyBits; 517 colorKey <<= GrGLEffectMatrix::kKeyBits;
517 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits); 518 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits);
518 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits + 519 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix ::kKeyBits +
519 SkDisplacementMapEff ect::kKeyBits); 520 SkDisplacementMapEff ect::kKeyBits);
520 521
521 return xKey | yKey | displKey | colorKey; 522 return xKey | yKey | displKey | colorKey;
522 } 523 }
523 #endif 524 #endif
OLDNEW
« no previous file with comments | « include/core/SkScalar.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698