diff options
-rw-r--r-- | src/effects/SkDisplacementMapEffect.cpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/effects/SkDisplacementMapEffect.cpp b/src/effects/SkDisplacementMapEffect.cpp index 661dacd53b..d9c1079ba0 100644 --- a/src/effects/SkDisplacementMapEffect.cpp +++ b/src/effects/SkDisplacementMapEffect.cpp @@ -23,6 +23,7 @@ #include "SkGr.h" #include "SkGrPriv.h" #include "effects/GrTextureDomain.h" +#include "glsl/GrGLSLColorSpaceXformHelper.h" #include "glsl/GrGLSLFragmentProcessor.h" #include "glsl/GrGLSLFragmentShaderBuilder.h" #include "glsl/GrGLSLProgramDataManager.h" @@ -221,10 +222,11 @@ public: SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, SkVector scale, GrTexture* displacement, const SkMatrix& offsetMatrix, GrTexture* color, - const SkISize& colorDimensions) { + sk_sp<GrColorSpaceXform> colorSpaceXform, const SkISize& colorDimensions) { return sk_sp<GrFragmentProcessor>( new GrDisplacementMapEffect(xChannelSelector, yChannelSelector, scale, displacement, - offsetMatrix, color, colorDimensions)); + offsetMatrix, color, std::move(colorSpaceXform), + colorDimensions)); } virtual ~GrDisplacementMapEffect(); @@ -239,6 +241,7 @@ public: const char* name() const override { return "DisplacementMap"; } const GrTextureDomain& domain() const { return fDomain; } + GrColorSpaceXform* colorSpaceXform() const { return fColorSpaceXform.get(); } private: GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; @@ -253,7 +256,7 @@ private: SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, const SkVector& scale, GrTexture* displacement, const SkMatrix& offsetMatrix, - GrTexture* color, + GrTexture* color, sk_sp<GrColorSpaceXform> colorSpaceXform, const SkISize& colorDimensions); GR_DECLARE_FRAGMENT_PROCESSOR_TEST; @@ -263,6 +266,7 @@ private: GrCoordTransform fColorTransform; GrTextureDomain fDomain; TextureSampler fColorSampler; + sk_sp<GrColorSpaceXform> fColorSpaceXform; SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; SkVector fScale; @@ -332,11 +336,13 @@ sk_sp<SkSpecialImage> SkDisplacementMapEffect::onFilterImage(SkSpecialImage* sou return nullptr; } - GrPaint paint; SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displTexture.get()); offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displOffset.fX), SkIntToScalar(colorOffset.fY - displOffset.fY)); - + SkColorSpace* colorSpace = ctx.outputProperties().colorSpace(); + sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(color->getColorSpace(), + colorSpace); + GrPaint paint; paint.addColorFragmentProcessor( GrDisplacementMapEffect::Make(fXChannelSelector, fYChannelSelector, @@ -344,12 +350,12 @@ sk_sp<SkSpecialImage> SkDisplacementMapEffect::onFilterImage(SkSpecialImage* sou displTexture.get(), offsetMatrix, colorTexture.get(), + std::move(colorSpaceXform), SkISize::Make(color->width(), color->height()))); paint.setPorterDuffXPFactory(SkBlendMode::kSrc); SkMatrix matrix; matrix.setTranslate(-SkIntToScalar(colorBounds.x()), -SkIntToScalar(colorBounds.y())); - SkColorSpace* colorSpace = ctx.outputProperties().colorSpace(); sk_sp<GrRenderTargetContext> renderTargetContext( context->makeDeferredRenderTargetContext(SkBackingFit::kApprox, bounds.width(), bounds.height(), @@ -461,7 +467,10 @@ protected: void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; private: - GrGLSLProgramDataManager::UniformHandle fScaleUni; + typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; + + UniformHandle fScaleUni; + UniformHandle fColorSpaceXformUni; GrTextureDomain::GLDomain fGLDomain; typedef GrGLSLFragmentProcessor INHERITED; @@ -485,6 +494,7 @@ GrDisplacementMapEffect::GrDisplacementMapEffect( GrTexture* displacement, const SkMatrix& offsetMatrix, GrTexture* color, + sk_sp<GrColorSpaceXform> colorSpaceXform, const SkISize& colorDimensions) : fDisplacementTransform(offsetMatrix, displacement, GrSamplerParams::kNone_FilterMode) , fDisplacementSampler(displacement) @@ -492,6 +502,7 @@ GrDisplacementMapEffect::GrDisplacementMapEffect( , fDomain(GrTextureDomain::MakeTexelDomain(color, SkIRect::MakeSize(colorDimensions)), GrTextureDomain::kDecal_Mode) , fColorSampler(color) + , fColorSpaceXform(std::move(colorSpaceXform)) , fXChannelSelector(xChannelSelector) , fYChannelSelector(yChannelSelector) , fScale(scale) { @@ -542,9 +553,11 @@ sk_sp<GrFragmentProcessor> GrDisplacementMapEffect::TestCreate(GrProcessorTestDa SkISize colorDimensions; colorDimensions.fWidth = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->width()); colorDimensions.fHeight = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->height()); + auto colorSpaceXform = GrTest::TestColorXform(d->fRandom); return GrDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale, d->fTextures[texIdxDispl], SkMatrix::I(), - d->fTextures[texIdxColor], colorDimensions); + d->fTextures[texIdxColor], colorSpaceXform, + colorDimensions); } /////////////////////////////////////////////////////////////////////////////// @@ -562,6 +575,10 @@ void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { // a number smaller than that to approximate 0, but // leave room for 32-bit float GPU rounding errors. + GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler, + displacementMap.colorSpaceXform(), + &fColorSpaceXformUni); + GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; fragBuilder->codeAppendf("\t\tvec4 %s = ", dColor); fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fTransformedCoords[0].c_str(), @@ -619,7 +636,9 @@ void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { domain, args.fOutputColor, SkString(cCoords), - args.fTexSamplers[1]); + args.fTexSamplers[1], + nullptr, + &colorSpaceHelper); fragBuilder->codeAppend(";\n"); } @@ -633,6 +652,9 @@ void GrGLDisplacementMapEffect::onSetData(const GrGLSLProgramDataManager& pdman, colorTex->origin() == kTopLeft_GrSurfaceOrigin ? SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin()); + if (SkToBool(displacementMap.colorSpaceXform())) { + pdman.setSkMatrix44(fColorSpaceXformUni, displacementMap.colorSpaceXform()->srcToDst()); + } } void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, @@ -643,5 +665,6 @@ void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits; b->add32(xKey | yKey); + b->add32(GrColorSpaceXform::XformKey(displacementMap.colorSpaceXform())); } #endif |