LumaXfermodeGM() {
fSrcInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode));
fDstInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode));
+ fSrcOverXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcOver_Mode));
SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
}
virtual SkISize onISize() SK_OVERRIDE {
- return SkISize::Make(600, 420);
+ return SkISize::Make(800, 420);
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
- SkXfermode* modes[] = { NULL, fSrcInXfer, fDstInXfer };
+ SkXfermode* modes[] = { NULL, fSrcInXfer, fDstInXfer, fSrcOverXfer };
struct {
SkShader* fShader1;
SkShader* fShader2;
draw_label(canvas, "SrcOver", SkPoint::Make(gridStep, 20));
draw_label(canvas, "SrcInLuma", SkPoint::Make(gridStep * 3, 20));
draw_label(canvas, "DstInLuma", SkPoint::Make(gridStep * 5, 20));
+ draw_label(canvas, "SrcOverLuma", SkPoint::Make(gridStep * 7, 20));
for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
canvas->save();
canvas->translate(kInset, gridStep * i + 30);
private:
SkAutoTUnref<SkShader> fGr1, fGr2;
- SkAutoTUnref<SkXfermode> fSrcInXfer, fDstInXfer;
+ SkAutoTUnref<SkXfermode> fSrcInXfer, fDstInXfer, fSrcOverXfer;
typedef skiagm::GM INHERITED;
};
* http://www.w3.org/TR/css-masking/#MaskValues
*
* The luminance-to-alpha function is applied before performing a standard
- * SrcIn/DstIn xfer:
+ * SrcIn/DstIn/SrcOver xfer:
*
* luma(C) = (0.2125 * C.r + 0.7154 * C.g + 0.0721 * C.b) * C.a
*
public:
/** Return an SkLumaMaskXfermode object for the specified submode.
*
- * Only kSrcIn_Mode and kDstIn_Mode are supported - for everything else,
+ * Only kSrcIn_Mode, kDstIn_Mode kSrcOver_Mode are supported - for everything else,
* the factory returns NULL.
*/
static SkXfermode* Create(SkXfermode::Mode);
SK_DEVELOPER_TO_STRING()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLumaMaskXfermode)
+ SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
#if SK_SUPPORT_GPU
virtual bool asNewEffectOrCoeff(GrContext*, GrEffectRef**, Coeff*, Coeff*,
SkLumaMaskXfermode(SkFlattenableReadBuffer&);
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
-private:
SkLumaMaskXfermode(SkXfermode::Mode);
+private:
const SkXfermode::Mode fMode;
typedef SkXfermode INHERITED;
+
+ virtual SkPMColor lumaProc(const SkPMColor a, const SkPMColor b) const;
};
#endif
#include "GrTBackendEffectFactory.h"
#endif
-static inline SkPMColor luma_proc(const SkPMColor a, const SkPMColor b) {
+class SkLumaMaskXfermodeSrcOver : public SkLumaMaskXfermode {
+public:
+ SkLumaMaskXfermodeSrcOver();
+
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLumaMaskXfermodeSrcOver)
+
+protected:
+ SkLumaMaskXfermodeSrcOver(SkFlattenableReadBuffer&);
+
+private:
+ typedef SkLumaMaskXfermode INHERITED;
+
+ virtual SkPMColor lumaProc(const SkPMColor a, const SkPMColor b) const;
+};
+
+SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) const {
unsigned luma = SkComputeLuminance(SkGetPackedR32(b),
SkGetPackedG32(b),
SkGetPackedB32(b));
if (kSrcIn_Mode == mode || kDstIn_Mode == mode) {
return SkNEW_ARGS(SkLumaMaskXfermode, (mode));
}
+ if (kSrcOver_Mode == mode) {
+ return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ());
+ }
+
return NULL;
}
SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode)
: fMode(mode) {
- SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode);
+ SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode || kSrcOver_Mode == mode);
}
SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer)
, fMode((SkXfermode::Mode)buffer.readUInt()) {
- SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode);
+ SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fMode);
}
void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const {
SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const {
const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst);
const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst);
- return luma_proc(*a, *b);
+ return this->lumaProc(*a, *b);
}
void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[],
for (int i = 0; i < count; ++i) {
unsigned cov = aa[i];
if (cov) {
- unsigned resC = luma_proc(a[i], b[i]);
+ unsigned resC = this->lumaProc(a[i], b[i]);
if (cov < 255) {
resC = SkFastFourByteInterp256(resC, dst[i],
SkAlpha255To256(cov));
}
} else {
for (int i = 0; i < count; ++i) {
- dst[i] = luma_proc(a[i], b[i]);
+ dst[i] = this->lumaProc(a[i], b[i]);
}
}
}
#ifdef SK_DEVELOPER
void SkLumaMaskXfermode::toString(SkString* str) const {
str->printf("SkLumaMaskXfermode: mode: %s",
- fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN");
+ fMode == kSrcIn_Mode ? "SRC_IN" :
+ fMode == kDstIn_Mode ? "DST_IN" : "SRC_OVER");
}
#endif
+SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver() : SkLumaMaskXfermode(kSrcOver_Mode) {}
+
+SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver(SkFlattenableReadBuffer& buffer)
+ : INHERITED(buffer) {
+}
+
+SkPMColor SkLumaMaskXfermodeSrcOver::lumaProc(const SkPMColor a, const SkPMColor b) const {
+ unsigned luma = SkComputeLuminance(SkGetPackedR32(b),
+ SkGetPackedG32(b),
+ SkGetPackedB32(b));
+
+ unsigned oldAlpha = SkGetPackedA32(b);
+ unsigned newR = 0, newG = 0, newB = 0;
+
+ if (oldAlpha > 0) {
+ newR = SkGetPackedR32(b) * 255 / oldAlpha;
+ newG = SkGetPackedG32(b) * 255 / oldAlpha;
+ newB = SkGetPackedB32(b) * 255 / oldAlpha;
+ }
+
+ SkPMColor colorB = SkPremultiplyARGBInline(luma, newR, newG, newB);
+
+ return SkPMSrcOver(colorB, a);
+}
+
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLumaMaskXfermode)
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaMaskXfermode)
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaMaskXfermodeSrcOver)
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
+
#if SK_SUPPORT_GPU
//////////////////////////////////////////////////////////////////////////////
SK_ITU_BT709_LUM_COEFF_G,
SK_ITU_BT709_LUM_COEFF_B,
opB);
- builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA);
+ if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) {
+ builder->fsCodeAppendf("\t\tvec4 newB = %s;\n\t\tif (newB.a > 0.0) { newB *= luma / newB.a; }\n\t\tnewB.a = luma;\n", opB);
+ builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputColor, opA);
+ } else {
+ builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA);
+ }
}
GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect,