| 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 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLumaMaskXfermodeSrcOve
r) |
| 25 |
| 26 protected: |
| 27 SkLumaMaskXfermodeSrcOver(SkFlattenableReadBuffer&); |
| 28 |
| 29 private: |
| 30 typedef SkLumaMaskXfermode INHERITED; |
| 31 |
| 32 virtual SkPMColor lumaProc(const SkPMColor a, const SkPMColor b) const; |
| 33 }; |
| 34 |
| 35 SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) con
st { |
| 21 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), | 36 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), |
| 22 SkGetPackedG32(b), | 37 SkGetPackedG32(b), |
| 23 SkGetPackedB32(b)); | 38 SkGetPackedB32(b)); |
| 24 return SkAlphaMulQ(a, SkAlpha255To256(luma)); | 39 return SkAlphaMulQ(a, SkAlpha255To256(luma)); |
| 25 } | 40 } |
| 26 | 41 |
| 27 template <typename T> | 42 template <typename T> |
| 28 static inline const T* lumaOpA(SkXfermode::Mode mode, | 43 static inline const T* lumaOpA(SkXfermode::Mode mode, |
| 29 const T* src, const T* dst) { | 44 const T* src, const T* dst) { |
| 30 return SkXfermode::kSrcIn_Mode == mode ? src : dst; | 45 return SkXfermode::kSrcIn_Mode == mode ? src : dst; |
| 31 } | 46 } |
| 32 | 47 |
| 33 template <typename T> | 48 template <typename T> |
| 34 static inline const T* lumaOpB(SkXfermode::Mode mode, | 49 static inline const T* lumaOpB(SkXfermode::Mode mode, |
| 35 const T* src, const T* dst) { | 50 const T* src, const T* dst) { |
| 36 return SkXfermode::kSrcIn_Mode == mode ? dst : src; | 51 return SkXfermode::kSrcIn_Mode == mode ? dst : src; |
| 37 } | 52 } |
| 38 | 53 |
| 39 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { | 54 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { |
| 40 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { | 55 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { |
| 41 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); | 56 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); |
| 42 } | 57 } |
| 58 if (kSrcOver_Mode == mode) { |
| 59 return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ()); |
| 60 } |
| 61 |
| 43 return NULL; | 62 return NULL; |
| 44 } | 63 } |
| 45 | 64 |
| 46 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode) | 65 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode) |
| 47 : fMode(mode) { | 66 : fMode(mode) { |
| 48 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode); | 67 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode || kSrcOver_Mode == mode
); |
| 49 } | 68 } |
| 50 | 69 |
| 51 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer) | 70 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer) |
| 52 : INHERITED(buffer) | 71 : INHERITED(buffer) |
| 53 , fMode((SkXfermode::Mode)buffer.readUInt()) { | 72 , fMode((SkXfermode::Mode)buffer.readUInt()) { |
| 54 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode); | 73 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fM
ode); |
| 55 } | 74 } |
| 56 | 75 |
| 57 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { | 76 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 58 INHERITED::flatten(buffer); | 77 INHERITED::flatten(buffer); |
| 59 buffer.writeUInt(fMode); | 78 buffer.writeUInt(fMode); |
| 60 } | 79 } |
| 61 | 80 |
| 62 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { | 81 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { |
| 63 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); | 82 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); |
| 64 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); | 83 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); |
| 65 return luma_proc(*a, *b); | 84 return this->lumaProc(*a, *b); |
| 66 } | 85 } |
| 67 | 86 |
| 68 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], | 87 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], |
| 69 int count, const SkAlpha aa[]) const { | 88 int count, const SkAlpha aa[]) const { |
| 70 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); | 89 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); |
| 71 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); | 90 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); |
| 72 | 91 |
| 73 if (aa) { | 92 if (aa) { |
| 74 for (int i = 0; i < count; ++i) { | 93 for (int i = 0; i < count; ++i) { |
| 75 unsigned cov = aa[i]; | 94 unsigned cov = aa[i]; |
| 76 if (cov) { | 95 if (cov) { |
| 77 unsigned resC = luma_proc(a[i], b[i]); | 96 unsigned resC = this->lumaProc(a[i], b[i]); |
| 78 if (cov < 255) { | 97 if (cov < 255) { |
| 79 resC = SkFastFourByteInterp256(resC, dst[i], | 98 resC = SkFastFourByteInterp256(resC, dst[i], |
| 80 SkAlpha255To256(cov)); | 99 SkAlpha255To256(cov)); |
| 81 } | 100 } |
| 82 dst[i] = resC; | 101 dst[i] = resC; |
| 83 } | 102 } |
| 84 } | 103 } |
| 85 } else { | 104 } else { |
| 86 for (int i = 0; i < count; ++i) { | 105 for (int i = 0; i < count; ++i) { |
| 87 dst[i] = luma_proc(a[i], b[i]); | 106 dst[i] = this->lumaProc(a[i], b[i]); |
| 88 } | 107 } |
| 89 } | 108 } |
| 90 } | 109 } |
| 91 | 110 |
| 92 #ifdef SK_DEVELOPER | 111 #ifdef SK_DEVELOPER |
| 93 void SkLumaMaskXfermode::toString(SkString* str) const { | 112 void SkLumaMaskXfermode::toString(SkString* str) const { |
| 94 str->printf("SkLumaMaskXfermode: mode: %s", | 113 str->printf("SkLumaMaskXfermode: mode: %s", |
| 95 fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN"); | 114 fMode == kSrcIn_Mode ? "SRC_IN" : |
| 115 fMode == kDstIn_Mode ? "DST_IN" : "SRC_OVER"); |
| 96 } | 116 } |
| 97 #endif | 117 #endif |
| 98 | 118 |
| 119 SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver() : SkLumaMaskXfermode(kSrc
Over_Mode) {} |
| 120 |
| 121 SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver(SkFlattenableReadBuffer& bu
ffer) |
| 122 : INHERITED(buffer) { |
| 123 } |
| 124 |
| 125 SkPMColor SkLumaMaskXfermodeSrcOver::lumaProc(const SkPMColor a, const SkPMColor
b) const { |
| 126 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), |
| 127 SkGetPackedG32(b), |
| 128 SkGetPackedB32(b)); |
| 129 |
| 130 unsigned oldAlpha = SkGetPackedA32(b); |
| 131 unsigned newR = 0, newG = 0, newB = 0; |
| 132 |
| 133 if (oldAlpha > 0) { |
| 134 newR = SkGetPackedR32(b) * 255 / oldAlpha; |
| 135 newG = SkGetPackedG32(b) * 255 / oldAlpha; |
| 136 newB = SkGetPackedB32(b) * 255 / oldAlpha; |
| 137 } |
| 138 |
| 139 SkPMColor colorB = SkPremultiplyARGBInline(luma, newR, newG, newB); |
| 140 |
| 141 return SkPMSrcOver(colorB, a); |
| 142 } |
| 143 |
| 144 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLumaMaskXfermode) |
| 145 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaMaskXfermode) |
| 146 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaMaskXfermodeSrcOver) |
| 147 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 148 |
| 99 #if SK_SUPPORT_GPU | 149 #if SK_SUPPORT_GPU |
| 100 ////////////////////////////////////////////////////////////////////////////// | 150 ////////////////////////////////////////////////////////////////////////////// |
| 101 | 151 |
| 102 class GrGLLumaMaskEffect : public GrGLEffect { | 152 class GrGLLumaMaskEffect : public GrGLEffect { |
| 103 public: | 153 public: |
| 104 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 154 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 105 virtual ~GrGLLumaMaskEffect(); | 155 virtual ~GrGLLumaMaskEffect(); |
| 106 | 156 |
| 107 virtual void emitCode(GrGLShaderBuilder*, | 157 virtual void emitCode(GrGLShaderBuilder*, |
| 108 const GrDrawEffect&, | 158 const GrDrawEffect&, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 217 } |
| 168 | 218 |
| 169 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); | 219 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); |
| 170 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); | 220 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); |
| 171 | 221 |
| 172 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", | 222 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", |
| 173 SK_ITU_BT709_LUM_COEFF_R, | 223 SK_ITU_BT709_LUM_COEFF_R, |
| 174 SK_ITU_BT709_LUM_COEFF_G, | 224 SK_ITU_BT709_LUM_COEFF_G, |
| 175 SK_ITU_BT709_LUM_COEFF_B, | 225 SK_ITU_BT709_LUM_COEFF_B, |
| 176 opB); | 226 opB); |
| 177 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); | 227 if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) { |
| 228 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); |
| 229 builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputCo
lor, opA); |
| 230 } else { |
| 231 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); |
| 232 } |
| 178 } | 233 } |
| 179 | 234 |
| 180 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, | 235 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, |
| 181 const GrGLCaps&) { | 236 const GrGLCaps&) { |
| 182 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); | 237 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); |
| 183 return (EffectKey)effect.getMode(); | 238 return (EffectKey)effect.getMode(); |
| 184 } | 239 } |
| 185 | 240 |
| 186 ////////////////////////////////////////////////////////////////////////////// | 241 ////////////////////////////////////////////////////////////////////////////// |
| 187 | 242 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 213 // No background texture support. | 268 // No background texture support. |
| 214 if (effect && !background) { | 269 if (effect && !background) { |
| 215 *effect = GrLumaMaskEffect::Create(fMode); | 270 *effect = GrLumaMaskEffect::Create(fMode); |
| 216 return true; | 271 return true; |
| 217 } | 272 } |
| 218 | 273 |
| 219 return false; | 274 return false; |
| 220 } | 275 } |
| 221 | 276 |
| 222 #endif // SK_SUPPORT_GPU | 277 #endif // SK_SUPPORT_GPU |
| OLD | NEW |