| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkLumaXfermode.h" | 8 #include "SkLumaXfermode.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
| 11 #include "SkString.h" | 11 #include "SkString.h" |
| 12 | 12 |
| 13 #if SK_SUPPORT_GPU | 13 #if SK_SUPPORT_GPU |
| 14 #include "gl/GrGLEffect.h" | 14 #include "gl/GrGLEffect.h" |
| 15 #include "gl/GrGLEffectMatrix.h" | 15 #include "gl/GrGLEffectMatrix.h" |
| 16 #include "GrContext.h" | 16 #include "GrContext.h" |
| 17 #include "GrTBackendEffectFactory.h" | 17 #include "GrTBackendEffectFactory.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 static inline SkPMColor luma_proc(const SkPMColor a, const SkPMColor b) { | 20 class SkLumaMaskXfermodeSrcOver : public SkLumaMaskXfermode { |
| 21 public: |
| 22 SkLumaMaskXfermodeSrcOver(); |
| 23 |
| 24 private: |
| 25 virtual SkPMColor lumaProc(const SkPMColor a, const SkPMColor b) const; |
| 26 }; |
| 27 |
| 28 SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) con
st { |
| 21 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), | 29 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), |
| 22 SkGetPackedG32(b), | 30 SkGetPackedG32(b), |
| 23 SkGetPackedB32(b)); | 31 SkGetPackedB32(b)); |
| 24 return SkAlphaMulQ(a, SkAlpha255To256(luma)); | 32 return SkAlphaMulQ(a, SkAlpha255To256(luma)); |
| 25 } | 33 } |
| 26 | 34 |
| 27 template <typename T> | 35 template <typename T> |
| 28 static inline const T* lumaOpA(SkXfermode::Mode mode, | 36 static inline const T* lumaOpA(SkXfermode::Mode mode, |
| 29 const T* src, const T* dst) { | 37 const T* src, const T* dst) { |
| 30 return SkXfermode::kSrcIn_Mode == mode ? src : dst; | 38 return SkXfermode::kSrcIn_Mode == mode ? src : dst; |
| 31 } | 39 } |
| 32 | 40 |
| 33 template <typename T> | 41 template <typename T> |
| 34 static inline const T* lumaOpB(SkXfermode::Mode mode, | 42 static inline const T* lumaOpB(SkXfermode::Mode mode, |
| 35 const T* src, const T* dst) { | 43 const T* src, const T* dst) { |
| 36 return SkXfermode::kSrcIn_Mode == mode ? dst : src; | 44 return SkXfermode::kSrcIn_Mode == mode ? dst : src; |
| 37 } | 45 } |
| 38 | 46 |
| 39 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { | 47 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { |
| 40 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { | 48 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { |
| 41 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); | 49 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); |
| 42 } | 50 } |
| 51 if (kSrcOver_Mode == mode) { |
| 52 return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ()); |
| 53 } |
| 54 |
| 43 return NULL; | 55 return NULL; |
| 44 } | 56 } |
| 45 | 57 |
| 46 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode) | 58 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode) |
| 47 : fMode(mode) { | 59 : fMode(mode) { |
| 48 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode); | 60 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode || kSrcOver_Mode == mode
); |
| 49 } | 61 } |
| 50 | 62 |
| 51 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer) | 63 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer) |
| 52 : INHERITED(buffer) | 64 : INHERITED(buffer) |
| 53 , fMode((SkXfermode::Mode)buffer.readUInt()) { | 65 , fMode((SkXfermode::Mode)buffer.readUInt()) { |
| 54 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode); | 66 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fM
ode); |
| 55 } | 67 } |
| 56 | 68 |
| 57 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { | 69 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 58 INHERITED::flatten(buffer); | 70 INHERITED::flatten(buffer); |
| 59 buffer.writeUInt(fMode); | 71 buffer.writeUInt(fMode); |
| 60 } | 72 } |
| 61 | 73 |
| 62 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { | 74 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { |
| 63 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); | 75 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); |
| 64 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); | 76 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); |
| 65 return luma_proc(*a, *b); | 77 return this->lumaProc(*a, *b); |
| 66 } | 78 } |
| 67 | 79 |
| 68 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], | 80 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], |
| 69 int count, const SkAlpha aa[]) const { | 81 int count, const SkAlpha aa[]) const { |
| 70 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); | 82 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); |
| 71 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); | 83 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); |
| 72 | 84 |
| 73 if (aa) { | 85 if (aa) { |
| 74 for (int i = 0; i < count; ++i) { | 86 for (int i = 0; i < count; ++i) { |
| 75 unsigned cov = aa[i]; | 87 unsigned cov = aa[i]; |
| 76 if (cov) { | 88 if (cov) { |
| 77 unsigned resC = luma_proc(a[i], b[i]); | 89 unsigned resC = this->lumaProc(a[i], b[i]); |
| 78 if (cov < 255) { | 90 if (cov < 255) { |
| 79 resC = SkFastFourByteInterp256(resC, dst[i], | 91 resC = SkFastFourByteInterp256(resC, dst[i], |
| 80 SkAlpha255To256(cov)); | 92 SkAlpha255To256(cov)); |
| 81 } | 93 } |
| 82 dst[i] = resC; | 94 dst[i] = resC; |
| 83 } | 95 } |
| 84 } | 96 } |
| 85 } else { | 97 } else { |
| 86 for (int i = 0; i < count; ++i) { | 98 for (int i = 0; i < count; ++i) { |
| 87 dst[i] = luma_proc(a[i], b[i]); | 99 dst[i] = this->lumaProc(a[i], b[i]); |
| 88 } | 100 } |
| 89 } | 101 } |
| 90 } | 102 } |
| 91 | 103 |
| 92 #ifdef SK_DEVELOPER | 104 #ifdef SK_DEVELOPER |
| 93 void SkLumaMaskXfermode::toString(SkString* str) const { | 105 void SkLumaMaskXfermode::toString(SkString* str) const { |
| 94 str->printf("SkLumaMaskXfermode: mode: %s", | 106 str->printf("SkLumaMaskXfermode: mode: %s", |
| 95 fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN"); | 107 fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN"); |
| 96 } | 108 } |
| 97 #endif | 109 #endif |
| 98 | 110 |
| 111 SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver() : SkLumaMaskXfermode(kSrc
Over_Mode) {} |
| 112 |
| 113 SkPMColor SkLumaMaskXfermodeSrcOver::lumaProc(const SkPMColor a, const SkPMColor
b) const { |
| 114 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), |
| 115 SkGetPackedG32(b), |
| 116 SkGetPackedB32(b)); |
| 117 |
| 118 unsigned oldAlpha = SkGetPackedA32(b); |
| 119 unsigned newR = 0, newG = 0, newB = 0; |
| 120 |
| 121 if (oldAlpha > 0) { |
| 122 newR = SkGetPackedR32(b) * 255 / oldAlpha; |
| 123 newG = SkGetPackedG32(b) * 255 / oldAlpha; |
| 124 newB = SkGetPackedB32(b) * 255 / oldAlpha; |
| 125 } |
| 126 |
| 127 SkPMColor colorB = SkPremultiplyARGBInline(luma, newR, newG, newB); |
| 128 |
| 129 return SkPMSrcOver(colorB, a); |
| 130 } |
| 131 |
| 99 #if SK_SUPPORT_GPU | 132 #if SK_SUPPORT_GPU |
| 100 ////////////////////////////////////////////////////////////////////////////// | 133 ////////////////////////////////////////////////////////////////////////////// |
| 101 | 134 |
| 102 class GrGLLumaMaskEffect : public GrGLEffect { | 135 class GrGLLumaMaskEffect : public GrGLEffect { |
| 103 public: | 136 public: |
| 104 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 137 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 105 virtual ~GrGLLumaMaskEffect(); | 138 virtual ~GrGLLumaMaskEffect(); |
| 106 | 139 |
| 107 virtual void emitCode(GrGLShaderBuilder*, | 140 virtual void emitCode(GrGLShaderBuilder*, |
| 108 const GrDrawEffect&, | 141 const GrDrawEffect&, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 200 } |
| 168 | 201 |
| 169 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); | 202 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); |
| 170 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); | 203 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); |
| 171 | 204 |
| 172 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", | 205 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", |
| 173 SK_ITU_BT709_LUM_COEFF_R, | 206 SK_ITU_BT709_LUM_COEFF_R, |
| 174 SK_ITU_BT709_LUM_COEFF_G, | 207 SK_ITU_BT709_LUM_COEFF_G, |
| 175 SK_ITU_BT709_LUM_COEFF_B, | 208 SK_ITU_BT709_LUM_COEFF_B, |
| 176 opB); | 209 opB); |
| 177 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); | 210 if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) { |
| 211 builder->fsCodeAppendf("\t\tvec4 newB = %s;\n\t\tif (newB.a > 0.0) { new
B *= luma / newB.a; }\n\t\tnewB.a = luma;\n", opB); |
| 212 builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputCo
lor, opA); |
| 213 } else { |
| 214 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); |
| 215 } |
| 178 } | 216 } |
| 179 | 217 |
| 180 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, | 218 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, |
| 181 const GrGLCaps&) { | 219 const GrGLCaps&) { |
| 182 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); | 220 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); |
| 183 return (EffectKey)effect.getMode(); | 221 return (EffectKey)effect.getMode(); |
| 184 } | 222 } |
| 185 | 223 |
| 186 ////////////////////////////////////////////////////////////////////////////// | 224 ////////////////////////////////////////////////////////////////////////////// |
| 187 | 225 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 213 // No background texture support. | 251 // No background texture support. |
| 214 if (effect && !background) { | 252 if (effect && !background) { |
| 215 *effect = GrLumaMaskEffect::Create(fMode); | 253 *effect = GrLumaMaskEffect::Create(fMode); |
| 216 return true; | 254 return true; |
| 217 } | 255 } |
| 218 | 256 |
| 219 return false; | 257 return false; |
| 220 } | 258 } |
| 221 | 259 |
| 222 #endif // SK_SUPPORT_GPU | 260 #endif // SK_SUPPORT_GPU |
| OLD | NEW |