Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1269)

Side by Side Diff: src/effects/SkLumaXfermode.cpp

Issue 24078006: In order to use CSS luminance masking, we need to be able to create an instance of SkLumaXfermode w… (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 SkPMColor SkLumaMaskXfermode::lumaProc(const SkPMColor a, const SkPMColor b) con st {
21 unsigned luma = SkComputeLuminance(SkGetPackedR32(b), 21 unsigned luma = SkComputeLuminance(SkGetPackedR32(b),
22 SkGetPackedG32(b), 22 SkGetPackedG32(b),
23 SkGetPackedB32(b)); 23 SkGetPackedB32(b));
24 return SkAlphaMulQ(a, SkAlpha255To256(luma)); 24 return SkAlphaMulQ(a, SkAlpha255To256(luma));
25 } 25 }
26 26
27 template <typename T> 27 template <typename T>
28 static inline const T* lumaOpA(SkXfermode::Mode mode, 28 static inline const T* lumaOpA(SkXfermode::Mode mode,
29 const T* src, const T* dst) { 29 const T* src, const T* dst) {
30 return SkXfermode::kSrcIn_Mode == mode ? src : dst; 30 return SkXfermode::kSrcIn_Mode == mode ? src : dst;
31 } 31 }
32 32
33 template <typename T> 33 template <typename T>
34 static inline const T* lumaOpB(SkXfermode::Mode mode, 34 static inline const T* lumaOpB(SkXfermode::Mode mode,
35 const T* src, const T* dst) { 35 const T* src, const T* dst) {
36 return SkXfermode::kSrcIn_Mode == mode ? dst : src; 36 return SkXfermode::kSrcIn_Mode == mode ? dst : src;
37 } 37 }
38 38
39 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) { 39 SkXfermode* SkLumaMaskXfermode::Create(SkXfermode::Mode mode) {
40 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) { 40 if (kSrcIn_Mode == mode || kDstIn_Mode == mode) {
41 return SkNEW_ARGS(SkLumaMaskXfermode, (mode)); 41 return SkNEW_ARGS(SkLumaMaskXfermode, (mode));
42 } 42 }
43 if (kSrcOver_Mode == mode) {
44 return SkNEW_ARGS(SkLumaMaskXfermodeSrcOver, ());
45 }
46
43 return NULL; 47 return NULL;
44 } 48 }
45 49
46 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode) 50 SkLumaMaskXfermode::SkLumaMaskXfermode(SkXfermode::Mode mode)
47 : fMode(mode) { 51 : fMode(mode) {
48 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode); 52 SkASSERT(kSrcIn_Mode == mode || kDstIn_Mode == mode || kSrcOver_Mode == mode );
49 } 53 }
50 54
51 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer) 55 SkLumaMaskXfermode::SkLumaMaskXfermode(SkFlattenableReadBuffer& buffer)
52 : INHERITED(buffer) 56 : INHERITED(buffer)
53 , fMode((SkXfermode::Mode)buffer.readUInt()) { 57 , fMode((SkXfermode::Mode)buffer.readUInt()) {
54 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode); 58 SkASSERT(kSrcIn_Mode == fMode || kDstIn_Mode == fMode || kSrcOver_Mode == fM ode);
55 } 59 }
56 60
57 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const { 61 void SkLumaMaskXfermode::flatten(SkFlattenableWriteBuffer& buffer) const {
58 INHERITED::flatten(buffer); 62 INHERITED::flatten(buffer);
59 buffer.writeUInt(fMode); 63 buffer.writeUInt(fMode);
60 } 64 }
61 65
62 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const { 66 SkPMColor SkLumaMaskXfermode::xferColor(SkPMColor src, SkPMColor dst) const {
63 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst); 67 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, &src, &dst);
64 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst); 68 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, &src, &dst);
65 return luma_proc(*a, *b); 69 return lumaProc(*a, *b);
66 } 70 }
67 71
68 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], 72 void SkLumaMaskXfermode::xfer32(SkPMColor dst[], const SkPMColor src[],
69 int count, const SkAlpha aa[]) const { 73 int count, const SkAlpha aa[]) const {
70 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst); 74 const SkPMColor* a = lumaOpA<SkPMColor>(fMode, src, dst);
71 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst); 75 const SkPMColor* b = lumaOpB<SkPMColor>(fMode, src, dst);
72 76
73 if (aa) { 77 if (aa) {
74 for (int i = 0; i < count; ++i) { 78 for (int i = 0; i < count; ++i) {
75 unsigned cov = aa[i]; 79 unsigned cov = aa[i];
76 if (cov) { 80 if (cov) {
77 unsigned resC = luma_proc(a[i], b[i]); 81 unsigned resC = lumaProc(a[i], b[i]);
78 if (cov < 255) { 82 if (cov < 255) {
79 resC = SkFastFourByteInterp256(resC, dst[i], 83 resC = SkFastFourByteInterp256(resC, dst[i],
80 SkAlpha255To256(cov)); 84 SkAlpha255To256(cov));
81 } 85 }
82 dst[i] = resC; 86 dst[i] = resC;
83 } 87 }
84 } 88 }
85 } else { 89 } else {
86 for (int i = 0; i < count; ++i) { 90 for (int i = 0; i < count; ++i) {
87 dst[i] = luma_proc(a[i], b[i]); 91 dst[i] = lumaProc(a[i], b[i]);
88 } 92 }
89 } 93 }
90 } 94 }
91 95
92 #ifdef SK_DEVELOPER 96 #ifdef SK_DEVELOPER
93 void SkLumaMaskXfermode::toString(SkString* str) const { 97 void SkLumaMaskXfermode::toString(SkString* str) const {
94 str->printf("SkLumaMaskXfermode: mode: %s", 98 str->printf("SkLumaMaskXfermode: mode: %s",
95 fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN"); 99 fMode == kSrcIn_Mode ? "SRC_IN" : "DST_IN");
96 } 100 }
97 #endif 101 #endif
98 102
103 SkLumaMaskXfermodeSrcOver::SkLumaMaskXfermodeSrcOver() : SkLumaMaskXfermode(kSrc Over_Mode) {}
104
105 SkPMColor SkLumaMaskXfermodeSrcOver::lumaProc(const SkPMColor a, const SkPMColor b) const {
106 unsigned luma = SkComputeLuminance(SkGetPackedR32(b),
107 SkGetPackedG32(b),
108 SkGetPackedB32(b));
109
110 unsigned oldAlpha = SkGetPackedA32(b);
111 unsigned oldR = 0, oldG = 0, oldB = 0;
112
113 if (oldAlpha > 0) {
114 oldR = SkGetPackedR32(b) * 255 / oldAlpha;
115 oldG = SkGetPackedG32(b) * 255 / oldAlpha;
116 oldB = SkGetPackedB32(b) * 255 / oldAlpha;
117 }
118
119 SkPMColor colorB = SkPremultiplyARGBInline(luma, oldR, oldG, oldB);
120
121 return SkPMSrcOver(colorB, a);
122 }
123
99 #if SK_SUPPORT_GPU 124 #if SK_SUPPORT_GPU
100 ////////////////////////////////////////////////////////////////////////////// 125 //////////////////////////////////////////////////////////////////////////////
101 126
102 class GrGLLumaMaskEffect : public GrGLEffect { 127 class GrGLLumaMaskEffect : public GrGLEffect {
103 public: 128 public:
104 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 129 GrGLLumaMaskEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
105 virtual ~GrGLLumaMaskEffect(); 130 virtual ~GrGLLumaMaskEffect();
106 131
107 virtual void emitCode(GrGLShaderBuilder*, 132 virtual void emitCode(GrGLShaderBuilder*,
108 const GrDrawEffect&, 133 const GrDrawEffect&,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 } 192 }
168 193
169 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor); 194 const char *opA = lumaOpA<char>(lumaEffect.getMode(), inputColor, dstColor);
170 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor); 195 const char *opB = lumaOpB<char>(lumaEffect.getMode(), inputColor, dstColor);
171 196
172 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n", 197 builder->fsCodeAppendf("\t\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb); \n",
173 SK_ITU_BT709_LUM_COEFF_R, 198 SK_ITU_BT709_LUM_COEFF_R,
174 SK_ITU_BT709_LUM_COEFF_G, 199 SK_ITU_BT709_LUM_COEFF_G,
175 SK_ITU_BT709_LUM_COEFF_B, 200 SK_ITU_BT709_LUM_COEFF_B,
176 opB); 201 opB);
177 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA); 202 if (SkXfermode::kSrcOver_Mode == lumaEffect.getMode()) {
203 builder->fsCodeAppendf("\t\tvec4 newB = %s;\t\tif (newB.a > 0.0) { newB *= luma / newB.a; }\n\t\tnewB.a = luma;\n", opB);
204 builder->fsCodeAppendf("\t\t%s = newB + %s * (1.0 - luma); \n", outputCo lor, opA);
205 } else {
206 builder->fsCodeAppendf("\t\t%s = %s * luma;\n", outputColor, opA);
207 }
178 } 208 }
179 209
180 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect, 210 GrGLEffect::EffectKey GrGLLumaMaskEffect::GenKey(const GrDrawEffect& drawEffect,
181 const GrGLCaps&) { 211 const GrGLCaps&) {
182 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>(); 212 const GrLumaMaskEffect& effect = drawEffect.castEffect<GrLumaMaskEffect>();
183 return (EffectKey)effect.getMode(); 213 return (EffectKey)effect.getMode();
184 } 214 }
185 215
186 ////////////////////////////////////////////////////////////////////////////// 216 //////////////////////////////////////////////////////////////////////////////
187 217
(...skipping 25 matching lines...) Expand all
213 // No background texture support. 243 // No background texture support.
214 if (effect && !background) { 244 if (effect && !background) {
215 *effect = GrLumaMaskEffect::Create(fMode); 245 *effect = GrLumaMaskEffect::Create(fMode);
216 return true; 246 return true;
217 } 247 }
218 248
219 return false; 249 return false;
220 } 250 }
221 251
222 #endif // SK_SUPPORT_GPU 252 #endif // SK_SUPPORT_GPU
OLDNEW
« include/effects/SkLumaXfermode.h ('K') | « include/effects/SkLumaXfermode.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698