'<(skia_include_path)/gpu/GrUserConfig.h',
'<(skia_include_path)/gpu/GrXferProcessor.h',
+ '<(skia_include_path)/gpu/effects/GrCustomXfermode.h',
'<(skia_include_path)/gpu/effects/GrPorterDuffXferProcessor.h',
'<(skia_include_path)/gpu/gl/GrGLConfig.h',
'<(skia_src_path)/gpu/effects/GrConfigConversionEffect.h',
'<(skia_src_path)/gpu/effects/GrCoverageSetOpXP.cpp',
'<(skia_src_path)/gpu/effects/GrCoverageSetOpXP.h',
+ '<(skia_src_path)/gpu/effects/GrCustomXfermode.cpp',
+ '<(skia_src_path)/gpu/effects/GrCustomXfermodePriv.h',
'<(skia_src_path)/gpu/effects/GrBezierEffect.cpp',
'<(skia_src_path)/gpu/effects/GrBezierEffect.h',
'<(skia_src_path)/gpu/effects/GrConvolutionEffect.cpp',
--- /dev/null
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrCustomXfermode_DEFINED
+#define GrCustomXfermode_DEFINED
+
+#include "SkXfermode.h"
+
+class GrFragmentProcessor;
+class GrTexture;
+
+/**
+ * Custom Xfer modes are used for blending when the blend mode cannot be represented using blend
+ * coefficients. It is assumed that all blending is done within the processors' emit code. For each
+ * blend mode there should be a matching fragment processor (used when blending with a background
+ * texture) and xfer processor.
+ */
+namespace GrCustomXfermode {
+ bool IsSupportedMode(SkXfermode::Mode mode);
+
+ GrFragmentProcessor* CreateFP(SkXfermode::Mode mode, GrTexture* background);
+};
+
+#endif
}
}
-//////////////////////////////////////////////////////////////////////////////
-
-#if SK_SUPPORT_GPU
-
-#include "GrFragmentProcessor.h"
-#include "GrCoordTransform.h"
-#include "GrInvariantOutput.h"
-#include "GrProcessorUnitTest.h"
-#include "gl/GrGLProcessor.h"
-#include "gl/builders/GrGLProgramBuilder.h"
-
-/**
- * GrProcessor that implements the all the separable xfer modes that cannot be expressed as Coeffs.
- */
-class XferEffect : public GrFragmentProcessor {
-public:
- static bool IsSupportedMode(SkXfermode::Mode mode) {
- return mode > SkXfermode::kLastCoeffMode && mode <= SkXfermode::kLastMode;
- }
-
- static GrFragmentProcessor* Create(SkXfermode::Mode mode, GrTexture* background) {
- if (!IsSupportedMode(mode)) {
- return NULL;
- } else {
- return SkNEW_ARGS(XferEffect, (mode, background));
- }
- }
-
- virtual void getGLProcessorKey(const GrGLCaps& caps,
- GrProcessorKeyBuilder* b) const SK_OVERRIDE {
- GLProcessor::GenKey(*this, caps, b);
- }
-
- GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE {
- return SkNEW_ARGS(GLProcessor, (*this));
- }
-
- const char* name() const SK_OVERRIDE { return "XferEffect"; }
-
- SkXfermode::Mode mode() const { return fMode; }
- const GrTextureAccess& backgroundAccess() const { return fBackgroundAccess; }
-
- class GLProcessor : public GrGLFragmentProcessor {
- public:
- GLProcessor(const GrFragmentProcessor&) {}
-
- virtual void emitCode(GrGLFPBuilder* builder,
- const GrFragmentProcessor& fp,
- const char* outputColor,
- const char* inputColor,
- const TransformedCoordsArray& coords,
- const TextureSamplerArray& samplers) SK_OVERRIDE {
- SkXfermode::Mode mode = fp.cast<XferEffect>().mode();
- const GrTexture* backgroundTex =
- fp.cast<XferEffect>().backgroundAccess().getTexture();
- GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
- const char* dstColor;
- if (backgroundTex) {
- dstColor = "bgColor";
- fsBuilder->codeAppendf("\t\tvec4 %s = ", dstColor);
- fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
- fsBuilder->codeAppendf(";\n");
- } else {
- dstColor = fsBuilder->dstColor();
- }
- SkASSERT(dstColor);
-
- // We don't try to optimize for this case at all
- if (NULL == inputColor) {
- fsBuilder->codeAppendf("\t\tconst vec4 ones = vec4(1);\n");
- inputColor = "ones";
- }
- fsBuilder->codeAppendf("\t\t// SkXfermode::Mode: %s\n", SkXfermode::ModeName(mode));
-
- // These all perform src-over on the alpha channel.
- fsBuilder->codeAppendf("\t\t%s.a = %s.a + (1.0 - %s.a) * %s.a;\n",
- outputColor, inputColor, inputColor, dstColor);
-
- switch (mode) {
- case SkXfermode::kOverlay_Mode:
- // Overlay is Hard-Light with the src and dst reversed
- HardLight(fsBuilder, outputColor, dstColor, inputColor);
- break;
- case SkXfermode::kDarken_Mode:
- fsBuilder->codeAppendf("\t\t%s.rgb = min((1.0 - %s.a) * %s.rgb + %s.rgb, "
- "(1.0 - %s.a) * %s.rgb + %s.rgb);\n",
- outputColor,
- inputColor, dstColor, inputColor,
- dstColor, inputColor, dstColor);
- break;
- case SkXfermode::kLighten_Mode:
- fsBuilder->codeAppendf("\t\t%s.rgb = max((1.0 - %s.a) * %s.rgb + %s.rgb, "
- "(1.0 - %s.a) * %s.rgb + %s.rgb);\n",
- outputColor,
- inputColor, dstColor, inputColor,
- dstColor, inputColor, dstColor);
- break;
- case SkXfermode::kColorDodge_Mode:
- ColorDodgeComponent(fsBuilder, outputColor, inputColor, dstColor, 'r');
- ColorDodgeComponent(fsBuilder, outputColor, inputColor, dstColor, 'g');
- ColorDodgeComponent(fsBuilder, outputColor, inputColor, dstColor, 'b');
- break;
- case SkXfermode::kColorBurn_Mode:
- ColorBurnComponent(fsBuilder, outputColor, inputColor, dstColor, 'r');
- ColorBurnComponent(fsBuilder, outputColor, inputColor, dstColor, 'g');
- ColorBurnComponent(fsBuilder, outputColor, inputColor, dstColor, 'b');
- break;
- case SkXfermode::kHardLight_Mode:
- HardLight(fsBuilder, outputColor, inputColor, dstColor);
- break;
- case SkXfermode::kSoftLight_Mode:
- fsBuilder->codeAppendf("\t\tif (0.0 == %s.a) {\n", dstColor);
- fsBuilder->codeAppendf("\t\t\t%s.rgba = %s;\n", outputColor, inputColor);
- fsBuilder->codeAppendf("\t\t} else {\n");
- SoftLightComponentPosDstAlpha(fsBuilder, outputColor, inputColor, dstColor, 'r');
- SoftLightComponentPosDstAlpha(fsBuilder, outputColor, inputColor, dstColor, 'g');
- SoftLightComponentPosDstAlpha(fsBuilder, outputColor, inputColor, dstColor, 'b');
- fsBuilder->codeAppendf("\t\t}\n");
- break;
- case SkXfermode::kDifference_Mode:
- fsBuilder->codeAppendf("\t\t%s.rgb = %s.rgb + %s.rgb -"
- "2.0 * min(%s.rgb * %s.a, %s.rgb * %s.a);\n",
- outputColor, inputColor, dstColor, inputColor, dstColor,
- dstColor, inputColor);
- break;
- case SkXfermode::kExclusion_Mode:
- fsBuilder->codeAppendf("\t\t%s.rgb = %s.rgb + %s.rgb - "
- "2.0 * %s.rgb * %s.rgb;\n",
- outputColor, dstColor, inputColor, dstColor, inputColor);
- break;
- case SkXfermode::kMultiply_Mode:
- fsBuilder->codeAppendf("\t\t%s.rgb = (1.0 - %s.a) * %s.rgb + "
- "(1.0 - %s.a) * %s.rgb + "
- "%s.rgb * %s.rgb;\n",
- outputColor, inputColor, dstColor, dstColor, inputColor,
- inputColor, dstColor);
- break;
- case SkXfermode::kHue_Mode: {
- // SetLum(SetSat(S * Da, Sat(D * Sa)), Sa*Da, D*Sa) + (1 - Sa) * D + (1 - Da) * S
- SkString setSat, setLum;
- AddSatFunction(fsBuilder, &setSat);
- AddLumFunction(fsBuilder, &setLum);
- fsBuilder->codeAppendf("\t\tvec4 dstSrcAlpha = %s * %s.a;\n",
- dstColor, inputColor);
- fsBuilder->codeAppendf("\t\t%s.rgb = %s(%s(%s.rgb * %s.a, dstSrcAlpha.rgb), dstSrcAlpha.a, dstSrcAlpha.rgb);\n",
- outputColor, setLum.c_str(), setSat.c_str(), inputColor,
- dstColor);
- fsBuilder->codeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n",
- outputColor, inputColor, dstColor, dstColor, inputColor);
- break;
- }
- case SkXfermode::kSaturation_Mode: {
- // SetLum(SetSat(D * Sa, Sat(S * Da)), Sa*Da, D*Sa)) + (1 - Sa) * D + (1 - Da) * S
- SkString setSat, setLum;
- AddSatFunction(fsBuilder, &setSat);
- AddLumFunction(fsBuilder, &setLum);
- fsBuilder->codeAppendf("\t\tvec4 dstSrcAlpha = %s * %s.a;\n",
- dstColor, inputColor);
- fsBuilder->codeAppendf("\t\t%s.rgb = %s(%s(dstSrcAlpha.rgb, %s.rgb * %s.a), dstSrcAlpha.a, dstSrcAlpha.rgb);\n",
- outputColor, setLum.c_str(), setSat.c_str(), inputColor,
- dstColor);
- fsBuilder->codeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n",
- outputColor, inputColor, dstColor, dstColor, inputColor);
- break;
- }
- case SkXfermode::kColor_Mode: {
- // SetLum(S * Da, Sa* Da, D * Sa) + (1 - Sa) * D + (1 - Da) * S
- SkString setLum;
- AddLumFunction(fsBuilder, &setLum);
- fsBuilder->codeAppendf("\t\tvec4 srcDstAlpha = %s * %s.a;\n",
- inputColor, dstColor);
- fsBuilder->codeAppendf("\t\t%s.rgb = %s(srcDstAlpha.rgb, srcDstAlpha.a, %s.rgb * %s.a);\n",
- outputColor, setLum.c_str(), dstColor, inputColor);
- fsBuilder->codeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n",
- outputColor, inputColor, dstColor, dstColor, inputColor);
- break;
- }
- case SkXfermode::kLuminosity_Mode: {
- // SetLum(D * Sa, Sa* Da, S * Da) + (1 - Sa) * D + (1 - Da) * S
- SkString setLum;
- AddLumFunction(fsBuilder, &setLum);
- fsBuilder->codeAppendf("\t\tvec4 srcDstAlpha = %s * %s.a;\n",
- inputColor, dstColor);
- fsBuilder->codeAppendf("\t\t%s.rgb = %s(%s.rgb * %s.a, srcDstAlpha.a, srcDstAlpha.rgb);\n",
- outputColor, setLum.c_str(), dstColor, inputColor);
- fsBuilder->codeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n",
- outputColor, inputColor, dstColor, dstColor, inputColor);
- break;
- }
- default:
- SkFAIL("Unknown XferEffect mode.");
- break;
- }
- }
-
- static inline void GenKey(const GrProcessor& proc, const GrGLCaps&,
- GrProcessorKeyBuilder* b) {
- // The background may come from the dst or from a texture.
- uint32_t key = proc.numTextures();
- SkASSERT(key <= 1);
- key |= proc.cast<XferEffect>().mode() << 1;
- b->add32(key);
- }
-
- private:
- static void HardLight(GrGLFPFragmentBuilder* fsBuilder,
- const char* final,
- const char* src,
- const char* dst) {
- static const char kComponents[] = {'r', 'g', 'b'};
- for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) {
- char component = kComponents[i];
- fsBuilder->codeAppendf("\t\tif (2.0 * %s.%c <= %s.a) {\n", src, component, src);
- fsBuilder->codeAppendf("\t\t\t%s.%c = 2.0 * %s.%c * %s.%c;\n", final, component, src, component, dst, component);
- fsBuilder->codeAppend("\t\t} else {\n");
- fsBuilder->codeAppendf("\t\t\t%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s.a - %s.%c);\n",
- final, component, src, dst, dst, dst, component, src, src, component);
- fsBuilder->codeAppend("\t\t}\n");
- }
- fsBuilder->codeAppendf("\t\t%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s.a);\n",
- final, src, dst, dst, src);
- }
-
- // Does one component of color-dodge
- static void ColorDodgeComponent(GrGLFPFragmentBuilder* fsBuilder,
- const char* final,
- const char* src,
- const char* dst,
- const char component) {
- fsBuilder->codeAppendf("\t\tif (0.0 == %s.%c) {\n", dst, component);
- fsBuilder->codeAppendf("\t\t\t%s.%c = %s.%c * (1.0 - %s.a);\n",
- final, component, src, component, dst);
- fsBuilder->codeAppend("\t\t} else {\n");
- fsBuilder->codeAppendf("\t\t\tfloat d = %s.a - %s.%c;\n", src, src, component);
- fsBuilder->codeAppend("\t\t\tif (0.0 == d) {\n");
- fsBuilder->codeAppendf("\t\t\t\t%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n",
- final, component, src, dst, src, component, dst, dst, component,
- src);
- fsBuilder->codeAppend("\t\t\t} else {\n");
- fsBuilder->codeAppendf("\t\t\t\td = min(%s.a, %s.%c * %s.a / d);\n",
- dst, dst, component, src);
- fsBuilder->codeAppendf("\t\t\t\t%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n",
- final, component, src, src, component, dst, dst, component, src);
- fsBuilder->codeAppend("\t\t\t}\n");
- fsBuilder->codeAppend("\t\t}\n");
- }
-
- // Does one component of color-burn
- static void ColorBurnComponent(GrGLFPFragmentBuilder* fsBuilder,
- const char* final,
- const char* src,
- const char* dst,
- const char component) {
- fsBuilder->codeAppendf("\t\tif (%s.a == %s.%c) {\n", dst, dst, component);
- fsBuilder->codeAppendf("\t\t\t%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n",
- final, component, src, dst, src, component, dst, dst, component,
- src);
- fsBuilder->codeAppendf("\t\t} else if (0.0 == %s.%c) {\n", src, component);
- fsBuilder->codeAppendf("\t\t\t%s.%c = %s.%c * (1.0 - %s.a);\n",
- final, component, dst, component, src);
- fsBuilder->codeAppend("\t\t} else {\n");
- fsBuilder->codeAppendf("\t\t\tfloat d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / %s.%c);\n",
- dst, dst, dst, component, src, src, component);
- fsBuilder->codeAppendf("\t\t\t%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n",
- final, component, src, src, component, dst, dst, component, src);
- fsBuilder->codeAppend("\t\t}\n");
- }
-
- // Does one component of soft-light. Caller should have already checked that dst alpha > 0.
- static void SoftLightComponentPosDstAlpha(GrGLFPFragmentBuilder* fsBuilder,
- const char* final,
- const char* src,
- const char* dst,
- const char component) {
- // if (2S < Sa)
- fsBuilder->codeAppendf("\t\t\tif (2.0 * %s.%c <= %s.a) {\n", src, component, src);
- // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1)
- fsBuilder->codeAppendf("\t\t\t\t%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / %s.a + (1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.0);\n",
- final, component, dst, component, dst, component, src, src,
- component, dst, dst, src, component, dst, component, src, src,
- component);
- // else if (4D < Da)
- fsBuilder->codeAppendf("\t\t\t} else if (4.0 * %s.%c <= %s.a) {\n",
- dst, component, dst);
- fsBuilder->codeAppendf("\t\t\t\tfloat DSqd = %s.%c * %s.%c;\n",
- dst, component, dst, component);
- fsBuilder->codeAppendf("\t\t\t\tfloat DCub = DSqd * %s.%c;\n", dst, component);
- fsBuilder->codeAppendf("\t\t\t\tfloat DaSqd = %s.a * %s.a;\n", dst, dst);
- fsBuilder->codeAppendf("\t\t\t\tfloat DaCub = DaSqd * %s.a;\n", dst);
- // (Da^3 (-S)+Da^2 (S-D (3 Sa-6 S-1))+12 Da D^2 (Sa-2 S)-16 D^3 (Sa-2 S))/Da^2
- fsBuilder->codeAppendf("\t\t\t\t%s.%c = (-DaCub*%s.%c + DaSqd*(%s.%c - %s.%c * (3.0*%s.a - 6.0*%s.%c - 1.0)) + 12.0*%s.a*DSqd*(%s.a - 2.0*%s.%c) - 16.0*DCub * (%s.a - 2.0*%s.%c)) / DaSqd;\n",
- final, component, src, component, src, component, dst, component,
- src, src, component, dst, src, src, component, src, src,
- component);
- fsBuilder->codeAppendf("\t\t\t} else {\n");
- // -sqrt(Da * D) (Sa-2 S)-Da S+D (Sa-2 S+1)+S
- fsBuilder->codeAppendf("\t\t\t\t%s.%c = -sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c + %s.%c*(%s.a - 2.0*%s.%c + 1.0) + %s.%c;\n",
- final, component, dst, dst, component, src, src, component, dst,
- src, component, dst, component, src, src, component, src,
- component);
- fsBuilder->codeAppendf("\t\t\t}\n");
- }
-
- // Adds a function that takes two colors and an alpha as input. It produces a color with the
- // hue and saturation of the first color, the luminosity of the second color, and the input
- // alpha. It has this signature:
- // vec3 set_luminance(vec3 hueSatColor, float alpha, vec3 lumColor).
- static void AddLumFunction(GrGLFPFragmentBuilder* fsBuilder, SkString* setLumFunction) {
- // Emit a helper that gets the luminance of a color.
- SkString getFunction;
- GrGLShaderVar getLumArgs[] = {
- GrGLShaderVar("color", kVec3f_GrSLType),
- };
- SkString getLumBody("\treturn dot(vec3(0.3, 0.59, 0.11), color);\n");
- fsBuilder->emitFunction(kFloat_GrSLType,
- "luminance",
- SK_ARRAY_COUNT(getLumArgs), getLumArgs,
- getLumBody.c_str(),
- &getFunction);
-
- // Emit the set luminance function.
- GrGLShaderVar setLumArgs[] = {
- GrGLShaderVar("hueSat", kVec3f_GrSLType),
- GrGLShaderVar("alpha", kFloat_GrSLType),
- GrGLShaderVar("lumColor", kVec3f_GrSLType),
- };
- SkString setLumBody;
- setLumBody.printf("\tfloat diff = %s(lumColor - hueSat);\n", getFunction.c_str());
- setLumBody.append("\tvec3 outColor = hueSat + diff;\n");
- setLumBody.appendf("\tfloat outLum = %s(outColor);\n", getFunction.c_str());
- setLumBody.append("\tfloat minComp = min(min(outColor.r, outColor.g), outColor.b);\n"
- "\tfloat maxComp = max(max(outColor.r, outColor.g), outColor.b);\n"
- "\tif (minComp < 0.0 && outLum != minComp) {\n"
- "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * outLum) / (outLum - minComp);\n"
- "\t}\n"
- "\tif (maxComp > alpha && maxComp != outLum) {\n"
- "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * (alpha - outLum)) / (maxComp - outLum);\n"
- "\t}\n"
- "\treturn outColor;\n");
- fsBuilder->emitFunction(kVec3f_GrSLType,
- "set_luminance",
- SK_ARRAY_COUNT(setLumArgs), setLumArgs,
- setLumBody.c_str(),
- setLumFunction);
- }
-
- // Adds a function that creates a color with the hue and luminosity of one input color and
- // the saturation of another color. It will have this signature:
- // float set_saturation(vec3 hueLumColor, vec3 satColor)
- static void AddSatFunction(GrGLFPFragmentBuilder* fsBuilder, SkString* setSatFunction) {
- // Emit a helper that gets the saturation of a color
- SkString getFunction;
- GrGLShaderVar getSatArgs[] = { GrGLShaderVar("color", kVec3f_GrSLType) };
- SkString getSatBody;
- getSatBody.printf("\treturn max(max(color.r, color.g), color.b) - "
- "min(min(color.r, color.g), color.b);\n");
- fsBuilder->emitFunction(kFloat_GrSLType,
- "saturation",
- SK_ARRAY_COUNT(getSatArgs), getSatArgs,
- getSatBody.c_str(),
- &getFunction);
-
- // Emit a helper that sets the saturation given sorted input channels. This used
- // to use inout params for min, mid, and max components but that seems to cause
- // problems on PowerVR drivers. So instead it returns a vec3 where r, g ,b are the
- // adjusted min, mid, and max inputs, respectively.
- SkString helperFunction;
- GrGLShaderVar helperArgs[] = {
- GrGLShaderVar("minComp", kFloat_GrSLType),
- GrGLShaderVar("midComp", kFloat_GrSLType),
- GrGLShaderVar("maxComp", kFloat_GrSLType),
- GrGLShaderVar("sat", kFloat_GrSLType),
- };
- static const char kHelperBody[] = "\tif (minComp < maxComp) {\n"
- "\t\tvec3 result;\n"
- "\t\tresult.r = 0.0;\n"
- "\t\tresult.g = sat * (midComp - minComp) / (maxComp - minComp);\n"
- "\t\tresult.b = sat;\n"
- "\t\treturn result;\n"
- "\t} else {\n"
- "\t\treturn vec3(0, 0, 0);\n"
- "\t}\n";
- fsBuilder->emitFunction(kVec3f_GrSLType,
- "set_saturation_helper",
- SK_ARRAY_COUNT(helperArgs), helperArgs,
- kHelperBody,
- &helperFunction);
-
- GrGLShaderVar setSatArgs[] = {
- GrGLShaderVar("hueLumColor", kVec3f_GrSLType),
- GrGLShaderVar("satColor", kVec3f_GrSLType),
- };
- const char* helpFunc = helperFunction.c_str();
- SkString setSatBody;
- setSatBody.appendf("\tfloat sat = %s(satColor);\n"
- "\tif (hueLumColor.r <= hueLumColor.g) {\n"
- "\t\tif (hueLumColor.g <= hueLumColor.b) {\n"
- "\t\t\thueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLumColor.b, sat);\n"
- "\t\t} else if (hueLumColor.r <= hueLumColor.b) {\n"
- "\t\t\thueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLumColor.g, sat);\n"
- "\t\t} else {\n"
- "\t\t\thueLumColor.brg = %s(hueLumColor.b, hueLumColor.r, hueLumColor.g, sat);\n"
- "\t\t}\n"
- "\t} else if (hueLumColor.r <= hueLumColor.b) {\n"
- "\t\thueLumColor.grb = %s(hueLumColor.g, hueLumColor.r, hueLumColor.b, sat);\n"
- "\t} else if (hueLumColor.g <= hueLumColor.b) {\n"
- "\t\thueLumColor.gbr = %s(hueLumColor.g, hueLumColor.b, hueLumColor.r, sat);\n"
- "\t} else {\n"
- "\t\thueLumColor.bgr = %s(hueLumColor.b, hueLumColor.g, hueLumColor.r, sat);\n"
- "\t}\n"
- "\treturn hueLumColor;\n",
- getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFunc,
- helpFunc, helpFunc);
- fsBuilder->emitFunction(kVec3f_GrSLType,
- "set_saturation",
- SK_ARRAY_COUNT(setSatArgs), setSatArgs,
- setSatBody.c_str(),
- setSatFunction);
-
- }
-
- typedef GrGLFragmentProcessor INHERITED;
- };
-
- GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
-
-private:
- XferEffect(SkXfermode::Mode mode, GrTexture* background)
- : fMode(mode) {
- this->initClassID<XferEffect>();
- if (background) {
- fBackgroundTransform.reset(kLocal_GrCoordSet, background,
- GrTextureParams::kNone_FilterMode);
- this->addCoordTransform(&fBackgroundTransform);
- fBackgroundAccess.reset(background);
- this->addTextureAccess(&fBackgroundAccess);
- } else {
- this->setWillReadDstColor();
- }
- }
- bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE {
- const XferEffect& s = other.cast<XferEffect>();
- return fMode == s.fMode;
- }
-
- void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
- inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
- }
-
- SkXfermode::Mode fMode;
- GrCoordTransform fBackgroundTransform;
- GrTextureAccess fBackgroundAccess;
-
- typedef GrFragmentProcessor INHERITED;
-};
-
-GR_DEFINE_FRAGMENT_PROCESSOR_TEST(XferEffect);
-GrFragmentProcessor* XferEffect::TestCreate(SkRandom* rand,
- GrContext*,
- const GrDrawTargetCaps&,
- GrTexture*[]) {
- int mode = rand->nextRangeU(SkXfermode::kLastCoeffMode + 1, SkXfermode::kLastSeparableMode);
-
- return SkNEW_ARGS(XferEffect, (static_cast<SkXfermode::Mode>(mode), NULL));
-}
-
-#endif
-
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
}
#if SK_SUPPORT_GPU
+#include "effects/GrCustomXfermode.h"
+
bool SkProcCoeffXfermode::asFragmentProcessor(GrFragmentProcessor** fp,
GrTexture* background) const {
- if (XferEffect::IsSupportedMode(fMode)) {
+ if (GrCustomXfermode::IsSupportedMode(fMode)) {
if (fp) {
- *fp = XferEffect::Create(fMode, background);
+ *fp = GrCustomXfermode::CreateFP(fMode, background);
SkASSERT(*fp);
}
return true;
--- /dev/null
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "effects/GrCustomXfermode.h"
+#include "effects/GrCustomXfermodePriv.h"
+
+#include "GrCoordTransform.h"
+#include "GrContext.h"
+#include "GrFragmentProcessor.h"
+#include "GrInvariantOutput.h"
+#include "GrProcessor.h"
+#include "GrTexture.h"
+#include "GrTextureAccess.h"
+#include "SkXfermode.h"
+#include "gl/GrGLCaps.h"
+#include "gl/GrGLProcessor.h"
+#include "gl/GrGLProgramDataManager.h"
+#include "gl/builders/GrGLProgramBuilder.h"
+
+bool GrCustomXfermode::IsSupportedMode(SkXfermode::Mode mode) {
+ return mode > SkXfermode::kLastCoeffMode && mode <= SkXfermode::kLastMode;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Static helpers
+///////////////////////////////////////////////////////////////////////////////
+
+static void hard_light(GrGLFPFragmentBuilder* fsBuilder,
+ const char* final,
+ const char* src,
+ const char* dst) {
+ static const char kComponents[] = {'r', 'g', 'b'};
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) {
+ char component = kComponents[i];
+ fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src);
+ fsBuilder->codeAppendf("%s.%c = 2.0 * %s.%c * %s.%c;",
+ final, component, src, component, dst, component);
+ fsBuilder->codeAppend("} else {");
+ fsBuilder->codeAppendf("%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s.a - %s.%c);",
+ final, component, src, dst, dst, dst, component, src, src,
+ component);
+ fsBuilder->codeAppend("}");
+ }
+ fsBuilder->codeAppendf("%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s.a);",
+ final, src, dst, dst, src);
+}
+
+// Does one component of color-dodge
+static void color_dodge_component(GrGLFPFragmentBuilder* fsBuilder,
+ const char* final,
+ const char* src,
+ const char* dst,
+ const char component) {
+ fsBuilder->codeAppendf("if (0.0 == %s.%c) {", dst, component);
+ fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);",
+ final, component, src, component, dst);
+ fsBuilder->codeAppend("} else {");
+ fsBuilder->codeAppendf("float d = %s.a - %s.%c;", src, src, component);
+ fsBuilder->codeAppend("if (0.0 == d) {");
+ fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);",
+ final, component, src, dst, src, component, dst, dst, component,
+ src);
+ fsBuilder->codeAppend("} else {");
+ fsBuilder->codeAppendf("d = min(%s.a, %s.%c * %s.a / d);",
+ dst, dst, component, src);
+ fsBuilder->codeAppendf("%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);",
+ final, component, src, src, component, dst, dst, component, src);
+ fsBuilder->codeAppend("}");
+ fsBuilder->codeAppend("}");
+}
+
+// Does one component of color-burn
+static void color_burn_component(GrGLFPFragmentBuilder* fsBuilder,
+ const char* final,
+ const char* src,
+ const char* dst,
+ const char component) {
+ fsBuilder->codeAppendf("if (%s.a == %s.%c) {", dst, dst, component);
+ fsBuilder->codeAppendf("%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);",
+ final, component, src, dst, src, component, dst, dst, component,
+ src);
+ fsBuilder->codeAppendf("} else if (0.0 == %s.%c) {", src, component);
+ fsBuilder->codeAppendf("%s.%c = %s.%c * (1.0 - %s.a);",
+ final, component, dst, component, src);
+ fsBuilder->codeAppend("} else {");
+ fsBuilder->codeAppendf("float d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / %s.%c);",
+ dst, dst, dst, component, src, src, component);
+ fsBuilder->codeAppendf("%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);",
+ final, component, src, src, component, dst, dst, component, src);
+ fsBuilder->codeAppend("}");
+}
+
+// Does one component of soft-light. Caller should have already checked that dst alpha > 0.
+static void soft_light_component_pos_dst_alpha(GrGLFPFragmentBuilder* fsBuilder,
+ const char* final,
+ const char* src,
+ const char* dst,
+ const char component) {
+ // if (2S < Sa)
+ fsBuilder->codeAppendf("if (2.0 * %s.%c <= %s.a) {", src, component, src);
+ // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1)
+ fsBuilder->codeAppendf("%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / %s.a +"
+ "(1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.0);",
+ final, component, dst, component, dst, component, src, src,
+ component, dst, dst, src, component, dst, component, src, src,
+ component);
+ // else if (4D < Da)
+ fsBuilder->codeAppendf("} else if (4.0 * %s.%c <= %s.a) {",
+ dst, component, dst);
+ fsBuilder->codeAppendf("float DSqd = %s.%c * %s.%c;",
+ dst, component, dst, component);
+ fsBuilder->codeAppendf("float DCub = DSqd * %s.%c;", dst, component);
+ fsBuilder->codeAppendf("float DaSqd = %s.a * %s.a;", dst, dst);
+ fsBuilder->codeAppendf("float DaCub = DaSqd * %s.a;", dst);
+ // (Da^3 (-S)+Da^2 (S-D (3 Sa-6 S-1))+12 Da D^2 (Sa-2 S)-16 D^3 (Sa-2 S))/Da^2
+ fsBuilder->codeAppendf("%s.%c ="
+ "(-DaCub*%s.%c + DaSqd*(%s.%c - %s.%c * (3.0*%s.a - 6.0*%s.%c - 1.0)) +"
+ " 12.0*%s.a*DSqd*(%s.a - 2.0*%s.%c) - 16.0*DCub * (%s.a - 2.0*%s.%c)) /"
+ "DaSqd;",
+ final, component, src, component, src, component, dst, component,
+ src, src, component, dst, src, src, component, src, src,
+ component);
+ fsBuilder->codeAppendf("} else {");
+ // -sqrt(Da * D) (Sa-2 S)-Da S+D (Sa-2 S+1)+S
+ fsBuilder->codeAppendf("%s.%c = -sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c +"
+ "%s.%c*(%s.a - 2.0*%s.%c + 1.0) + %s.%c;",
+ final, component, dst, dst, component, src, src, component, dst,
+ src, component, dst, component, src, src, component, src,
+ component);
+ fsBuilder->codeAppendf("}");
+}
+
+// Adds a function that takes two colors and an alpha as input. It produces a color with the
+// hue and saturation of the first color, the luminosity of the second color, and the input
+// alpha. It has this signature:
+// vec3 set_luminance(vec3 hueSatColor, float alpha, vec3 lumColor).
+static void add_lum_function(GrGLFPFragmentBuilder* fsBuilder, SkString* setLumFunction) {
+ // Emit a helper that gets the luminance of a color.
+ SkString getFunction;
+ GrGLShaderVar getLumArgs[] = {
+ GrGLShaderVar("color", kVec3f_GrSLType),
+ };
+ SkString getLumBody("return dot(vec3(0.3, 0.59, 0.11), color);");
+ fsBuilder->emitFunction(kFloat_GrSLType,
+ "luminance",
+ SK_ARRAY_COUNT(getLumArgs), getLumArgs,
+ getLumBody.c_str(),
+ &getFunction);
+
+ // Emit the set luminance function.
+ GrGLShaderVar setLumArgs[] = {
+ GrGLShaderVar("hueSat", kVec3f_GrSLType),
+ GrGLShaderVar("alpha", kFloat_GrSLType),
+ GrGLShaderVar("lumColor", kVec3f_GrSLType),
+ };
+ SkString setLumBody;
+ setLumBody.printf("float diff = %s(lumColor - hueSat);", getFunction.c_str());
+ setLumBody.append("vec3 outColor = hueSat + diff;");
+ setLumBody.appendf("float outLum = %s(outColor);", getFunction.c_str());
+ setLumBody.append("float minComp = min(min(outColor.r, outColor.g), outColor.b);"
+ "float maxComp = max(max(outColor.r, outColor.g), outColor.b);"
+ "if (minComp < 0.0 && outLum != minComp) {"
+ "outColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * outLum) /"
+ "(outLum - minComp);"
+ "}"
+ "if (maxComp > alpha && maxComp != outLum) {"
+ "outColor = outLum +"
+ "((outColor - vec3(outLum, outLum, outLum)) * (alpha - outLum)) /"
+ "(maxComp - outLum);"
+ "}"
+ "return outColor;");
+ fsBuilder->emitFunction(kVec3f_GrSLType,
+ "set_luminance",
+ SK_ARRAY_COUNT(setLumArgs), setLumArgs,
+ setLumBody.c_str(),
+ setLumFunction);
+}
+
+// Adds a function that creates a color with the hue and luminosity of one input color and
+// the saturation of another color. It will have this signature:
+// float set_saturation(vec3 hueLumColor, vec3 satColor)
+static void add_sat_function(GrGLFPFragmentBuilder* fsBuilder, SkString* setSatFunction) {
+ // Emit a helper that gets the saturation of a color
+ SkString getFunction;
+ GrGLShaderVar getSatArgs[] = { GrGLShaderVar("color", kVec3f_GrSLType) };
+ SkString getSatBody;
+ getSatBody.printf("return max(max(color.r, color.g), color.b) - "
+ "min(min(color.r, color.g), color.b);");
+ fsBuilder->emitFunction(kFloat_GrSLType,
+ "saturation",
+ SK_ARRAY_COUNT(getSatArgs), getSatArgs,
+ getSatBody.c_str(),
+ &getFunction);
+
+ // Emit a helper that sets the saturation given sorted input channels. This used
+ // to use inout params for min, mid, and max components but that seems to cause
+ // problems on PowerVR drivers. So instead it returns a vec3 where r, g ,b are the
+ // adjusted min, mid, and max inputs, respectively.
+ SkString helperFunction;
+ GrGLShaderVar helperArgs[] = {
+ GrGLShaderVar("minComp", kFloat_GrSLType),
+ GrGLShaderVar("midComp", kFloat_GrSLType),
+ GrGLShaderVar("maxComp", kFloat_GrSLType),
+ GrGLShaderVar("sat", kFloat_GrSLType),
+ };
+ static const char kHelperBody[] = "if (minComp < maxComp) {"
+ "vec3 result;"
+ "result.r = 0.0;"
+ "result.g = sat * (midComp - minComp) / (maxComp - minComp);"
+ "result.b = sat;"
+ "return result;"
+ "} else {"
+ "return vec3(0, 0, 0);"
+ "}";
+ fsBuilder->emitFunction(kVec3f_GrSLType,
+ "set_saturation_helper",
+ SK_ARRAY_COUNT(helperArgs), helperArgs,
+ kHelperBody,
+ &helperFunction);
+
+ GrGLShaderVar setSatArgs[] = {
+ GrGLShaderVar("hueLumColor", kVec3f_GrSLType),
+ GrGLShaderVar("satColor", kVec3f_GrSLType),
+ };
+ const char* helpFunc = helperFunction.c_str();
+ SkString setSatBody;
+ setSatBody.appendf("float sat = %s(satColor);"
+ "if (hueLumColor.r <= hueLumColor.g) {"
+ "if (hueLumColor.g <= hueLumColor.b) {"
+ "hueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLumColor.b, sat);"
+ "} else if (hueLumColor.r <= hueLumColor.b) {"
+ "hueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLumColor.g, sat);"
+ "} else {"
+ "hueLumColor.brg = %s(hueLumColor.b, hueLumColor.r, hueLumColor.g, sat);"
+ "}"
+ "} else if (hueLumColor.r <= hueLumColor.b) {"
+ "hueLumColor.grb = %s(hueLumColor.g, hueLumColor.r, hueLumColor.b, sat);"
+ "} else if (hueLumColor.g <= hueLumColor.b) {"
+ "hueLumColor.gbr = %s(hueLumColor.g, hueLumColor.b, hueLumColor.r, sat);"
+ "} else {"
+ "hueLumColor.bgr = %s(hueLumColor.b, hueLumColor.g, hueLumColor.r, sat);"
+ "}"
+ "return hueLumColor;",
+ getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFunc,
+ helpFunc, helpFunc);
+ fsBuilder->emitFunction(kVec3f_GrSLType,
+ "set_saturation",
+ SK_ARRAY_COUNT(setSatArgs), setSatArgs,
+ setSatBody.c_str(),
+ setSatFunction);
+
+}
+
+static void emit_custom_xfermode_code(SkXfermode::Mode mode,
+ GrGLFPFragmentBuilder* fsBuilder,
+ const char* outputColor,
+ const char* inputColor,
+ const char* dstColor) {
+ // We don't try to optimize for this case at all
+ if (NULL == inputColor) {
+ fsBuilder->codeAppendf("const vec4 ones = vec4(1);");
+ inputColor = "ones";
+ }
+ fsBuilder->codeAppendf("// SkXfermode::Mode: %s\n", SkXfermode::ModeName(mode));
+
+ // These all perform src-over on the alpha channel.
+ fsBuilder->codeAppendf("%s.a = %s.a + (1.0 - %s.a) * %s.a;",
+ outputColor, inputColor, inputColor, dstColor);
+
+ switch (mode) {
+ case SkXfermode::kOverlay_Mode:
+ // Overlay is Hard-Light with the src and dst reversed
+ hard_light(fsBuilder, outputColor, dstColor, inputColor);
+ break;
+ case SkXfermode::kDarken_Mode:
+ fsBuilder->codeAppendf("%s.rgb = min((1.0 - %s.a) * %s.rgb + %s.rgb, "
+ "(1.0 - %s.a) * %s.rgb + %s.rgb);",
+ outputColor,
+ inputColor, dstColor, inputColor,
+ dstColor, inputColor, dstColor);
+ break;
+ case SkXfermode::kLighten_Mode:
+ fsBuilder->codeAppendf("%s.rgb = max((1.0 - %s.a) * %s.rgb + %s.rgb, "
+ "(1.0 - %s.a) * %s.rgb + %s.rgb);",
+ outputColor,
+ inputColor, dstColor, inputColor,
+ dstColor, inputColor, dstColor);
+ break;
+ case SkXfermode::kColorDodge_Mode:
+ color_dodge_component(fsBuilder, outputColor, inputColor, dstColor, 'r');
+ color_dodge_component(fsBuilder, outputColor, inputColor, dstColor, 'g');
+ color_dodge_component(fsBuilder, outputColor, inputColor, dstColor, 'b');
+ break;
+ case SkXfermode::kColorBurn_Mode:
+ color_burn_component(fsBuilder, outputColor, inputColor, dstColor, 'r');
+ color_burn_component(fsBuilder, outputColor, inputColor, dstColor, 'g');
+ color_burn_component(fsBuilder, outputColor, inputColor, dstColor, 'b');
+ break;
+ case SkXfermode::kHardLight_Mode:
+ hard_light(fsBuilder, outputColor, inputColor, dstColor);
+ break;
+ case SkXfermode::kSoftLight_Mode:
+ fsBuilder->codeAppendf("if (0.0 == %s.a) {", dstColor);
+ fsBuilder->codeAppendf("%s.rgba = %s;", outputColor, inputColor);
+ fsBuilder->codeAppendf("} else {");
+ soft_light_component_pos_dst_alpha(fsBuilder, outputColor, inputColor, dstColor, 'r');
+ soft_light_component_pos_dst_alpha(fsBuilder, outputColor, inputColor, dstColor, 'g');
+ soft_light_component_pos_dst_alpha(fsBuilder, outputColor, inputColor, dstColor, 'b');
+ fsBuilder->codeAppendf("}");
+ break;
+ case SkXfermode::kDifference_Mode:
+ fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb -"
+ "2.0 * min(%s.rgb * %s.a, %s.rgb * %s.a);",
+ outputColor, inputColor, dstColor, inputColor, dstColor,
+ dstColor, inputColor);
+ break;
+ case SkXfermode::kExclusion_Mode:
+ fsBuilder->codeAppendf("%s.rgb = %s.rgb + %s.rgb - "
+ "2.0 * %s.rgb * %s.rgb;",
+ outputColor, dstColor, inputColor, dstColor, inputColor);
+ break;
+ case SkXfermode::kMultiply_Mode:
+ fsBuilder->codeAppendf("%s.rgb = (1.0 - %s.a) * %s.rgb + "
+ "(1.0 - %s.a) * %s.rgb + "
+ "%s.rgb * %s.rgb;",
+ outputColor, inputColor, dstColor, dstColor, inputColor,
+ inputColor, dstColor);
+ break;
+ case SkXfermode::kHue_Mode: {
+ // SetLum(SetSat(S * Da, Sat(D * Sa)), Sa*Da, D*Sa) + (1 - Sa) * D + (1 - Da) * S
+ SkString setSat, setLum;
+ add_sat_function(fsBuilder, &setSat);
+ add_lum_function(fsBuilder, &setLum);
+ fsBuilder->codeAppendf("vec4 dstSrcAlpha = %s * %s.a;",
+ dstColor, inputColor);
+ fsBuilder->codeAppendf("%s.rgb = %s(%s(%s.rgb * %s.a, dstSrcAlpha.rgb),"
+ "dstSrcAlpha.a, dstSrcAlpha.rgb);",
+ outputColor, setLum.c_str(), setSat.c_str(), inputColor,
+ dstColor);
+ fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;",
+ outputColor, inputColor, dstColor, dstColor, inputColor);
+ break;
+ }
+ case SkXfermode::kSaturation_Mode: {
+ // SetLum(SetSat(D * Sa, Sat(S * Da)), Sa*Da, D*Sa)) + (1 - Sa) * D + (1 - Da) * S
+ SkString setSat, setLum;
+ add_sat_function(fsBuilder, &setSat);
+ add_lum_function(fsBuilder, &setLum);
+ fsBuilder->codeAppendf("vec4 dstSrcAlpha = %s * %s.a;",
+ dstColor, inputColor);
+ fsBuilder->codeAppendf("%s.rgb = %s(%s(dstSrcAlpha.rgb, %s.rgb * %s.a),"
+ "dstSrcAlpha.a, dstSrcAlpha.rgb);",
+ outputColor, setLum.c_str(), setSat.c_str(), inputColor,
+ dstColor);
+ fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;",
+ outputColor, inputColor, dstColor, dstColor, inputColor);
+ break;
+ }
+ case SkXfermode::kColor_Mode: {
+ // SetLum(S * Da, Sa* Da, D * Sa) + (1 - Sa) * D + (1 - Da) * S
+ SkString setLum;
+ add_lum_function(fsBuilder, &setLum);
+ fsBuilder->codeAppendf("vec4 srcDstAlpha = %s * %s.a;",
+ inputColor, dstColor);
+ fsBuilder->codeAppendf("%s.rgb = %s(srcDstAlpha.rgb, srcDstAlpha.a, %s.rgb * %s.a);",
+ outputColor, setLum.c_str(), dstColor, inputColor);
+ fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;",
+ outputColor, inputColor, dstColor, dstColor, inputColor);
+ break;
+ }
+ case SkXfermode::kLuminosity_Mode: {
+ // SetLum(D * Sa, Sa* Da, S * Da) + (1 - Sa) * D + (1 - Da) * S
+ SkString setLum;
+ add_lum_function(fsBuilder, &setLum);
+ fsBuilder->codeAppendf("vec4 srcDstAlpha = %s * %s.a;",
+ inputColor, dstColor);
+ fsBuilder->codeAppendf("%s.rgb = %s(%s.rgb * %s.a, srcDstAlpha.a, srcDstAlpha.rgb);",
+ outputColor, setLum.c_str(), dstColor, inputColor);
+ fsBuilder->codeAppendf("%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;",
+ outputColor, inputColor, dstColor, dstColor, inputColor);
+ break;
+ }
+ default:
+ SkFAIL("Unknown Custom Xfer mode.");
+ break;
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Fragment Processor
+///////////////////////////////////////////////////////////////////////////////
+
+GrFragmentProcessor* GrCustomXfermode::CreateFP(SkXfermode::Mode mode, GrTexture* background) {
+ if (!GrCustomXfermode::IsSupportedMode(mode)) {
+ return NULL;
+ } else {
+ return SkNEW_ARGS(GrCustomXferFP, (mode, background));
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+class GLCustomXferFP : public GrGLFragmentProcessor {
+public:
+ GLCustomXferFP(const GrFragmentProcessor&) {}
+ ~GLCustomXferFP() SK_OVERRIDE {};
+
+ void emitCode(GrGLFPBuilder* builder,
+ const GrFragmentProcessor& fp,
+ const char* outputColor,
+ const char* inputColor,
+ const TransformedCoordsArray& coords,
+ const TextureSamplerArray& samplers) SK_OVERRIDE {
+ SkXfermode::Mode mode = fp.cast<GrCustomXferFP>().mode();
+ const GrTexture* backgroundTex =
+ fp.cast<GrCustomXferFP>().backgroundAccess().getTexture();
+ GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
+ const char* dstColor;
+ if (backgroundTex) {
+ dstColor = "bgColor";
+ fsBuilder->codeAppendf("vec4 %s = ", dstColor);
+ fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
+ fsBuilder->codeAppendf(";");
+ } else {
+ dstColor = fsBuilder->dstColor();
+ }
+ SkASSERT(dstColor);
+
+ emit_custom_xfermode_code(mode, fsBuilder, outputColor, inputColor, dstColor);
+ }
+
+ void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE {}
+
+ static void GenKey(const GrFragmentProcessor& proc, const GrGLCaps&, GrProcessorKeyBuilder* b) {
+ // The background may come from the dst or from a texture.
+ uint32_t key = proc.numTextures();
+ SkASSERT(key <= 1);
+ key |= proc.cast<GrCustomXferFP>().mode() << 1;
+ b->add32(key);
+ }
+
+private:
+ typedef GrGLFragmentProcessor INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+GrCustomXferFP::GrCustomXferFP(SkXfermode::Mode mode, GrTexture* background)
+ : fMode(mode) {
+ this->initClassID<GrCustomXferFP>();
+ if (background) {
+ fBackgroundTransform.reset(kLocal_GrCoordSet, background,
+ GrTextureParams::kNone_FilterMode);
+ this->addCoordTransform(&fBackgroundTransform);
+ fBackgroundAccess.reset(background);
+ this->addTextureAccess(&fBackgroundAccess);
+ } else {
+ this->setWillReadDstColor();
+ }
+}
+
+void GrCustomXferFP::getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const {
+ GLCustomXferFP::GenKey(*this, caps, b);
+}
+
+GrGLFragmentProcessor* GrCustomXferFP::createGLInstance() const {
+ return SkNEW_ARGS(GLCustomXferFP, (*this));
+}
+
+bool GrCustomXferFP::onIsEqual(const GrFragmentProcessor& other) const {
+ const GrCustomXferFP& s = other.cast<GrCustomXferFP>();
+ return fMode == s.fMode;
+}
+
+void GrCustomXferFP::onComputeInvariantOutput(GrInvariantOutput* inout) const {
+ inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
+}
+
+GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCustomXferFP);
+GrFragmentProcessor* GrCustomXferFP::TestCreate(SkRandom* rand,
+ GrContext*,
+ const GrDrawTargetCaps&,
+ GrTexture*[]) {
+ int mode = rand->nextRangeU(SkXfermode::kLastCoeffMode + 1, SkXfermode::kLastSeparableMode);
+
+ return SkNEW_ARGS(GrCustomXferFP, (static_cast<SkXfermode::Mode>(mode), NULL));
+}
+
--- /dev/null
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrCustomXfermodePriv_DEFINED
+#define GrCustomXfermodePriv_DEFINED
+
+#include "GrCoordTransform.h"
+#include "GrFragmentProcessor.h"
+#include "GrTextureAccess.h"
+#include "SkXfermode.h"
+
+class GrGLCaps;
+class GrGLFragmentProcessor;
+class GrInvariantOutput;
+class GrProcessorKeyBuilder;
+class GrTexture;
+
+///////////////////////////////////////////////////////////////////////////////
+// Fragment Processor
+///////////////////////////////////////////////////////////////////////////////
+
+class GrCustomXferFP : public GrFragmentProcessor {
+public:
+ GrCustomXferFP(SkXfermode::Mode mode, GrTexture* background);
+
+ void getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const SK_OVERRIDE;
+
+ GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
+
+ const char* name() const SK_OVERRIDE { return "Custom Xfermode"; }
+
+ SkXfermode::Mode mode() const { return fMode; }
+ const GrTextureAccess& backgroundAccess() const { return fBackgroundAccess; }
+
+private:
+ bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
+
+ void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
+
+ GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
+
+ SkXfermode::Mode fMode;
+ GrCoordTransform fBackgroundTransform;
+ GrTextureAccess fBackgroundAccess;
+
+ typedef GrFragmentProcessor INHERITED;
+};
+
+#endif
+