Chromium Code Reviews| Index: src/core/SkComposeShader.cpp |
| diff --git a/src/core/SkComposeShader.cpp b/src/core/SkComposeShader.cpp |
| index c16d7ffc578f352f948d432934c68c41851c6623..15c556c76ce80c96f0254ef8ae0751b1bd5a7e08 100644 |
| --- a/src/core/SkComposeShader.cpp |
| +++ b/src/core/SkComposeShader.cpp |
| @@ -194,6 +194,160 @@ void SkComposeShader::ComposeShaderContext::shadeSpan(int x, int y, SkPMColor re |
| } |
| } |
| +#if SK_SUPPORT_GPU |
| + |
| +#include "SkGr.h" |
| +#include "GrProcessor.h" |
| +#include "gl/GrGLBlend.h" |
| +#include "gl/builders/GrGLProgramBuilder.h" |
| + |
| +///////////////////////////////////////////////////////////////////// |
| + |
| +class GrComposeEffect : public GrFragmentProcessor { |
| +public: |
| + |
| + static GrFragmentProcessor* Create(GrFragmentProcessor* fpA, GrFragmentProcessor* fpB, |
| + SkXfermode::Mode mode) { |
| + return SkNEW_ARGS(GrComposeEffect, (fpA, fpB, mode)); |
| + } |
| + const char* name() const override {return fName.c_str(); } |
| + void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override; |
| + |
| + SkXfermode::Mode getMode() const { return fMode; } |
| + int getShaderAChildIndex() const { return fShaderAChildIndex; } |
| + int getShaderBChildIndex() const { return fShaderBChildIndex; } |
| + |
| +protected: |
| + bool onIsEqual(const GrFragmentProcessor&) const override; |
| + void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
| + |
| +private: |
| + GrComposeEffect(GrFragmentProcessor* fpA, GrFragmentProcessor* fpB, SkXfermode::Mode mode) |
| + : fMode(mode) { |
| + this->initClassID<GrComposeEffect>(); |
| + fShaderAChildIndex = this->registerChildProcessor(fpA); |
| + fShaderBChildIndex = this->registerChildProcessor(fpB); |
| + fName.printf("Compose Shader <%s, %s> (Mode: %s)", fpA->name(), fpB->name(), |
| + SkXfermode::ModeName(fMode)); |
| + } |
| + |
| + GrGLFragmentProcessor* onCreateGLInstance() const override; |
| + |
| + SkString fName; |
| + int fShaderAChildIndex; |
| + int fShaderBChildIndex; |
| + SkXfermode::Mode fMode; |
| + |
| + typedef GrFragmentProcessor INHERITED; |
| +}; |
| + |
| +///////////////////////////////////////////////////////////////////// |
| + |
| +class GrGLComposeEffect : public GrGLFragmentProcessor { |
| +public: |
| + GrGLComposeEffect(const GrProcessor& processor) { |
| + const GrComposeEffect& cs = processor.cast<GrComposeEffect>(); |
| + fShaderAChildIndex = cs.getShaderAChildIndex(); |
| + fShaderBChildIndex = cs.getShaderBChildIndex(); |
| + fMode = cs.getMode(); |
| + } |
| + |
| + void emitCode(EmitArgs&) override; |
| + |
| +private: |
| + int fShaderAChildIndex; |
| + int fShaderBChildIndex; |
| + SkXfermode::Mode fMode; |
| + |
| + typedef GrGLFragmentProcessor INHERITED; |
| +}; |
| + |
| +bool GrComposeEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| + const GrComposeEffect& cs = other.cast<GrComposeEffect>(); |
| + return fMode == cs.fMode; |
| +} |
| + |
| +void GrComposeEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
| + inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| +} |
| + |
| +void GrComposeEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
| + b->add32(fMode); |
| +} |
| + |
| +GrGLFragmentProcessor* GrComposeEffect::onCreateGLInstance() const{ |
| + return SkNEW_ARGS(GrGLComposeEffect, (*this)); |
| +} |
| + |
| +///////////////////////////////////////////////////////////////////// |
| + |
| +void GrGLComposeEffect::emitCode(EmitArgs& args) { |
| + |
| + GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
| + |
| + // emit the code of the two child shaders |
| + SkString mangledOutputColorA; |
| + this->emitChild(fShaderAChildIndex, args.fInputColor, &mangledOutputColorA, args); |
| + SkString mangledOutputColorB; |
| + this->emitChild(fShaderBChildIndex, args.fInputColor, &mangledOutputColorB, args); |
| + |
| + // emit blend code |
| + fsBuilder->codeAppend("\t{\n"); |
| + fsBuilder->codeAppendf("\t\t// Compose Xfer Mode: %s\n", SkXfermode::ModeName(fMode)); |
| + GrGLBlend::AppendPorterDuffBlend(fsBuilder, mangledOutputColorB.c_str(), |
| + mangledOutputColorA.c_str(), args.fOutputColor, fMode); |
| + fsBuilder->codeAppend("\t}\n"); |
| +} |
| + |
| +///////////////////////////////////////////////////////////////////// |
| + |
| +bool SkComposeShader::asFragmentProcessor(GrContext* context, const SkPaint& paint, |
| + const SkMatrix& viewM, const SkMatrix* localMatrix, |
| + GrColor* paintColor, |
| + GrProcessorDataManager* procDataManager, |
| + GrFragmentProcessor** fp) const { |
| + // NULL fMode implies src-over |
| + SkXfermode::Mode mode = SkXfermode::Mode::kSrcOver_Mode; |
| + if (fMode) { |
| + // Fragment processor will only support coefficient modes that aren't kClear_Mode. |
| + // This is because GrGLBlend::AppendPorterDuffBlend(), which emits the blend code in the |
| + // shader, only supports those xfer modes. |
| + SkXfermode::Coeff srcCoeff, dstCoeff; |
| + if (!fMode->asMode(&mode) || !SkXfermode::ModeAsCoeff(mode, &srcCoeff, &dstCoeff) |
| + || mode == SkXfermode::Mode::kClear_Mode) { |
|
egdaniel
2015/08/21 16:21:39
nit: we put the variable on the right side of ==
|
| + return false; |
| + } |
| + } |
| + GrFragmentProcessor* fpA = NULL; |
| + if (!fShaderA->asFragmentProcessor(context, paint, viewM, localMatrix, paintColor, |
| + procDataManager, &fpA) || !fpA) { |
|
egdaniel
2015/08/21 16:21:39
Can you line this up so that procDataMangeger, &fp
|
| + return false; |
| + } |
| + GrFragmentProcessor* fpB = NULL; |
| + if (!fShaderB->asFragmentProcessor(context, paint, viewM, localMatrix, paintColor, |
|
egdaniel
2015/08/21 16:21:39
same as above
|
| + procDataManager, &fpB) || !fpB) { |
| + fpA->unref(); |
| + return false; |
| + } |
| + |
| + *fp = GrComposeEffect::Create(fpA, fpB, mode); |
|
egdaniel
2015/08/21 16:21:39
Is there any chance that this Create can fail and
|
| + fpA->unref(); |
| + fpB->unref(); |
| + |
| + return true; |
| +} |
| + |
| + |
| + |
| +#else |
| +bool SkComposeShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&, |
| + const SkMatrix*, GrColor*, GrProcessorDataManager*, |
| + GrFragmentProcessor**) const { |
| + SkDEBUGFAIL("Should not call in GPU-less build"); |
| + return false; |
| +} |
| +#endif |
| + |
| #ifndef SK_IGNORE_TO_STRING |
| void SkComposeShader::toString(SkString* str) const { |
| str->append("SkComposeShader: ("); |