Added GrGLBlend.h|cpp with helper function AppendPorterDuffBlend() in preparation for SkComposeShader gpu backend

BUG=skia:

Review URL: https://codereview.chromium.org/1254833003
This commit is contained in:
wangyix 2015-07-24 13:48:26 -07:00 committed by Commit bot
parent 20dac88565
commit cef14bf9ce
4 changed files with 97 additions and 56 deletions

View File

@ -269,6 +269,8 @@
'<(skia_src_path)/gpu/gl/GrGLAssembleInterface.cpp',
'<(skia_src_path)/gpu/gl/GrGLAssembleInterface.h',
'<(skia_src_path)/gpu/gl/GrGLBlend.cpp',
'<(skia_src_path)/gpu/gl/GrGLBlend.h',
'<(skia_src_path)/gpu/gl/GrGLBufferImpl.cpp',
'<(skia_src_path)/gpu/gl/GrGLBufferImpl.h',
'<(skia_src_path)/gpu/gl/GrGLCaps.cpp',

View File

@ -13,6 +13,7 @@
#include "GrProcOptInfo.h"
#include "GrTypes.h"
#include "GrXferProcessor.h"
#include "gl/GrGLBlend.h"
#include "gl/GrGLXferProcessor.h"
#include "gl/builders/GrGLFragmentShaderBuilder.h"
#include "gl/builders/GrGLProgramBuilder.h"
@ -488,50 +489,6 @@ private:
///////////////////////////////////////////////////////////////////////////////
static bool append_porterduff_term(GrGLXPFragmentBuilder* fsBuilder, SkXfermode::Coeff coeff,
const char* colorName, const char* srcColorName,
const char* dstColorName, bool hasPrevious) {
if (SkXfermode::kZero_Coeff == coeff) {
return hasPrevious;
} else {
if (hasPrevious) {
fsBuilder->codeAppend(" + ");
}
fsBuilder->codeAppendf("%s", colorName);
switch (coeff) {
case SkXfermode::kOne_Coeff:
break;
case SkXfermode::kSC_Coeff:
fsBuilder->codeAppendf(" * %s", srcColorName);
break;
case SkXfermode::kISC_Coeff:
fsBuilder->codeAppendf(" * (vec4(1.0) - %s)", srcColorName);
break;
case SkXfermode::kDC_Coeff:
fsBuilder->codeAppendf(" * %s", dstColorName);
break;
case SkXfermode::kIDC_Coeff:
fsBuilder->codeAppendf(" * (vec4(1.0) - %s)", dstColorName);
break;
case SkXfermode::kSA_Coeff:
fsBuilder->codeAppendf(" * %s.a", srcColorName);
break;
case SkXfermode::kISA_Coeff:
fsBuilder->codeAppendf(" * (1.0 - %s.a)", srcColorName);
break;
case SkXfermode::kDA_Coeff:
fsBuilder->codeAppendf(" * %s.a", dstColorName);
break;
case SkXfermode::kIDA_Coeff:
fsBuilder->codeAppendf(" * (1.0 - %s.a)", dstColorName);
break;
default:
SkFAIL("Unsupported Blend Coeff");
}
return true;
}
}
class GLShaderPDXferProcessor : public GrGLXferProcessor {
public:
static void GenKey(const GrProcessor& processor, GrProcessorKeyBuilder* b) {
@ -545,17 +502,7 @@ private:
const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>();
GrGLXPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
SkXfermode::Coeff srcCoeff, dstCoeff;
SkXfermode::ModeAsCoeff(xp.getXfermode(), &srcCoeff, &dstCoeff);
fsBuilder->codeAppendf("%s =", outColor);
// append src blend
bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor,
false);
// append dst blend
SkAssertResult(append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor,
didAppend));
fsBuilder->codeAppend(";");
GrGLBlend::AppendPorterDuffBlend(fsBuilder, srcColor, dstColor, outColor, xp.getXfermode());
}
void onSetData(const GrGLProgramDataManager&, const GrXferProcessor&) override {}
@ -824,4 +771,3 @@ void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp,
*outPrimary = blendFormula.fPrimaryOutputType;
*outSecondary = blendFormula.fSecondaryOutputType;
}

69
src/gpu/gl/GrGLBlend.cpp Normal file
View File

@ -0,0 +1,69 @@
/*
* 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 "GrGLBlend.h"
#include "gl/builders/GrGLFragmentShaderBuilder.h"
static bool append_porterduff_term(GrGLFragmentBuilder* fsBuilder, SkXfermode::Coeff coeff,
const char* colorName, const char* srcColorName,
const char* dstColorName, bool hasPrevious) {
if (SkXfermode::kZero_Coeff == coeff) {
return hasPrevious;
} else {
if (hasPrevious) {
fsBuilder->codeAppend(" + ");
}
fsBuilder->codeAppendf("%s", colorName);
switch (coeff) {
case SkXfermode::kOne_Coeff:
break;
case SkXfermode::kSC_Coeff:
fsBuilder->codeAppendf(" * %s", srcColorName);
break;
case SkXfermode::kISC_Coeff:
fsBuilder->codeAppendf(" * (vec4(1.0) - %s)", srcColorName);
break;
case SkXfermode::kDC_Coeff:
fsBuilder->codeAppendf(" * %s", dstColorName);
break;
case SkXfermode::kIDC_Coeff:
fsBuilder->codeAppendf(" * (vec4(1.0) - %s)", dstColorName);
break;
case SkXfermode::kSA_Coeff:
fsBuilder->codeAppendf(" * %s.a", srcColorName);
break;
case SkXfermode::kISA_Coeff:
fsBuilder->codeAppendf(" * (1.0 - %s.a)", srcColorName);
break;
case SkXfermode::kDA_Coeff:
fsBuilder->codeAppendf(" * %s.a", dstColorName);
break;
case SkXfermode::kIDA_Coeff:
fsBuilder->codeAppendf(" * (1.0 - %s.a)", dstColorName);
break;
default:
SkFAIL("Unsupported Blend Coeff");
}
return true;
}
}
void GrGLBlend::AppendPorterDuffBlend(GrGLFragmentBuilder* fsBuilder, const char* srcColor,
const char* dstColor, const char* outColor,
SkXfermode::Mode mode) {
SkXfermode::Coeff srcCoeff, dstCoeff;
SkXfermode::ModeAsCoeff(mode, &srcCoeff, &dstCoeff);
fsBuilder->codeAppendf("%s =", outColor);
// append src blend
bool didAppend = append_porterduff_term(fsBuilder, srcCoeff, srcColor, srcColor, dstColor,
false);
// append dst blend
SkAssertResult(append_porterduff_term(fsBuilder, dstCoeff, dstColor, srcColor, dstColor,
didAppend));
fsBuilder->codeAppend(";");
}

24
src/gpu/gl/GrGLBlend.h Normal file
View File

@ -0,0 +1,24 @@
/*
* 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 GrGLBlend_DEFINED
#define GrGLBlend_DEFINED
#include "SkXfermode.h"
class GrGLFragmentBuilder;
namespace GrGLBlend {
/*
* Appends GLSL code to fsBuilder that assigns a specified blend of the srcColor and dstColor
* variables to the outColor variable.
*/
void AppendPorterDuffBlend(GrGLFragmentBuilder* fsBuilder, const char* srcColor,
const char* dstColor, const char* outColor, SkXfermode::Mode mode);
};
#endif