| 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" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) con
st { | 34 SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) con
st { |
| 35 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), | 35 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), |
| 36 SkGetPackedG32(b), | 36 SkGetPackedG32(b), |
| 37 SkGetPackedB32(b)); | 37 SkGetPackedB32(b)); |
| 38 return SkAlphaMulQ(a, SkAlpha255To256(luma)); | 38 return SkAlphaMulQ(a, SkAlpha255To256(luma)); |
| 39 } | 39 } |
| 40 | 40 |
| 41 template <typename T> | 41 template <typename T> |
| 42 static inline const T* lumaOpA(SkXfermode::Mode mode, | 42 static inline T lumaOpA(SkXfermode::Mode mode, T src, T dst) { |
| 43 const T* src, const T* dst) { | |
| 44 return SkXfermode::kSrcIn_Mode == mode ? src : dst; | 43 return SkXfermode::kSrcIn_Mode == mode ? src : dst; |
| 45 } | 44 } |
| 46 | 45 |
| 47 template <typename T> | 46 template <typename T> |
| 48 static inline const T* lumaOpB(SkXfermode::Mode mode, | 47 static inline T lumaOpB(SkXfermode::Mode mode, T src, T dst) { |
| 49 const T* src, const T* dst) { | |
| 50 return SkXfermode::kSrcIn_Mode == mode ? dst : src; | 48 return SkXfermode::kSrcIn_Mode == mode ? dst : src; |
| 51 } | 49 } |
| 52 | 50 |
| 53 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { | 51 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { |
| 54 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { | 52 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { |
| 55 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); | 53 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); |
| 56 } | 54 } |
| 57 if (kSrcOver_Mode == mode) { | 55 if (kSrcOver_Mode == mode) { |
| 58 return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ()); | 56 return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ()); |
| 59 } | 57 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 71 , fMode((SkXfermode::Mode)buffer.readUInt()) { | 69 , fMode((SkXfermode::Mode)buffer.readUInt()) { |
| 72 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fM
ode); | 70 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fM
ode); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { | 73 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 76 INHERITED::flatten(buffer); | 74 INHERITED::flatten(buffer); |
| 77 buffer.writeUInt(fMode); | 75 buffer.writeUInt(fMode); |
| 78 } | 76 } |
| 79 | 77 |
| 80 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { | 78 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { |
| 81 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); | 79 const SkPMColor* a = lumaOpA<const SkPMColor*>(fMode, &src, &dst); |
| 82 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); | 80 const SkPMColor* b = lumaOpB<const SkPMColor*>(fMode, &src, &dst); |
| 83 return this->lumaProc(*a, *b); | 81 return this->lumaProc(*a, *b); |
| 84 } | 82 } |
| 85 | 83 |
| 86 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], | 84 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], |
| 87 int count, const SkAlpha aa[]) const { | 85 int count, const SkAlpha aa[]) const { |
| 88 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); | 86 const SkPMColor* a = lumaOpA<const SkPMColor*>(fMode, src, dst); |
| 89 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); | 87 const SkPMColor* b = lumaOpB<const SkPMColor*>(fMode, src, dst); |
| 90 | 88 |
| 91 if (aa) { | 89 if (aa) { |
| 92 for (int i = 0; i < count; ++i) { | 90 for (int i = 0; i < count; ++i) { |
| 93 unsigned cov = aa[i]; | 91 unsigned cov = aa[i]; |
| 94 if (cov) { | 92 if (cov) { |
| 95 unsigned resC = this->lumaProc(a[i], b[i]); | 93 unsigned resC = this->lumaProc(a[i], b[i]); |
| 96 if (cov < 255) { | 94 if (cov < 255) { |
| 97 resC = SkFastFourByteInterp256(resC, dst[i], | 95 resC = SkFastFourByteInterp256(resC, dst[i], |
| 98 SkAlpha255To256(cov)); | 96 SkAlpha255To256(cov)); |
| 99 } | 97 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 148 |
| 151 class GrGLLumaMaskEffect : public GrGLEffect { | 149 class GrGLLumaMaskEffect : public GrGLEffect { |
| 152 public: | 150 public: |
| 153 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 151 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 154 virtual ~GrGLLumaMaskEffect(); | 152 virtual ~GrGLLumaMaskEffect(); |
| 155 | 153 |
| 156 virtual void emitCode(GrGLShaderBuilder*, | 154 virtual void emitCode(GrGLShaderBuilder*, |
| 157 const GrDrawEffect&, | 155 const GrDrawEffect&, |
| 158 EffectKey, | 156 EffectKey, |
| 159 const char* outputColor, | 157 const char* outputColor, |
| 160 const char* inputColor, | 158 const GrGLSLExpr4& inputColor, |
| 161 const TransformedCoordsArray&, | 159 const TransformedCoordsArray&, |
| 162 const TextureSamplerArray&) SK_OVERRIDE; | 160 const TextureSamplerArray&) SK_OVERRIDE; |
| 163 | 161 |
| 164 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 162 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 165 | 163 |
| 166 private: | 164 private: |
| 167 typedef GrGLEffect INHERITED; | 165 typedef GrGLEffect INHERITED; |
| 168 }; | 166 }; |
| 169 | 167 |
| 170 class GrLumaMaskEffect : public GrEffect { | 168 class GrLumaMaskEffect : public GrEffect { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 199 : INHERITED(factory) { | 197 : INHERITED(factory) { |
| 200 } | 198 } |
| 201 | 199 |
| 202 GrGLLumaMaskEffect::~GrGLLumaMaskEffect() { | 200 GrGLLumaMaskEffect::~GrGLLumaMaskEffect() { |
| 203 } | 201 } |
| 204 | 202 |
| 205 void GrGLLumaMaskEffect::emitCode(GrGLShaderBuilder* builder, | 203 void GrGLLumaMaskEffect::emitCode(GrGLShaderBuilder* builder, |
| 206 const GrDrawEffect& effect, | 204 const GrDrawEffect& effect, |
| 207 EffectKey key, | 205 EffectKey key, |
| 208 const char* outputColor, | 206 const char* outputColor, |
| 209 const char* inputColor, | 207 const GrGLSLExpr4& inputColor, |
| 210 const TransformedCoordsArray&, | 208 const TransformedCoordsArray&, |
| 211 const TextureSamplerArray& samplers) { | 209 const TextureSamplerArray& samplers) { |
| 212 | 210 |
| 213 const GrLumaMaskEffect& lumaEffect = effect.castEffect<GrLumaMaskEffect>(); | 211 const GrLumaMaskEffect& lumaEffect = effect.castEffect<GrLumaMaskEffect>(); |
| 214 const char* dstColor = builder->dstColor(); | 212 GrGLSLExpr4 dstColor(builder->dstColor()); |
| 215 SkASSERT(NULL != dstColor); | |
| 216 if (NULL == inputColor) { | |
| 217 inputColor = "vec4(1)"; | |
| 218 } | |
| 219 | 213 |
| 220 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); | 214 const GrGLSLExpr4& opA = lumaOpA(lumaEffect.getMode(), inputColor, dstColor)
; |
| 221 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); | 215 const GrGLSLExpr4& opB = lumaOpB(lumaEffect.getMode(), inputColor, dstColor)
; |
| 222 | 216 |
| 223 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", | 217 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", |
| 224 SK_ITU_BT709_LUM_COEFF_R, | 218 SK_ITU_BT709_LUM_COEFF_R, |
| 225 SK_ITU_BT709_LUM_COEFF_G, | 219 SK_ITU_BT709_LUM_COEFF_G, |
| 226 SK_ITU_BT709_LUM_COEFF_B, | 220 SK_ITU_BT709_LUM_COEFF_B, |
| 227 opB); | 221 opB.c_str()); |
| 228 if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) { | 222 if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) { |
| 229 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); | 223 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.c_str()); |
| 230 builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputCo
lor, opA); | 224 builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputCo
lor, opA.c_str()); |
| 231 } else { | 225 } else { |
| 232 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); | 226 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA.c_str()
); |
| 233 } | 227 } |
| 234 } | 228 } |
| 235 | 229 |
| 236 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, | 230 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, |
| 237 const GrGLCaps&) { | 231 const GrGLCaps&) { |
| 238 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); | 232 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); |
| 239 return (EffectKey)effect.getMode(); | 233 return (EffectKey)effect.getMode(); |
| 240 } | 234 } |
| 241 | 235 |
| 242 ////////////////////////////////////////////////////////////////////////////// | 236 ////////////////////////////////////////////////////////////////////////////// |
| (...skipping 26 matching lines...) Expand all Loading... |
| 269 // No background texture support. | 263 // No background texture support. |
| 270 if (effect && !background) { | 264 if (effect && !background) { |
| 271 *effect = GrLumaMaskEffect::Create(fMode); | 265 *effect = GrLumaMaskEffect::Create(fMode); |
| 272 return true; | 266 return true; |
| 273 } | 267 } |
| 274 | 268 |
| 275 return false; | 269 return false; |
| 276 } | 270 } |
| 277 | 271 |
| 278 #endif // SK_SUPPORT_GPU | 272 #endif // SK_SUPPORT_GPU |
| OLD | NEW |