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 "SkArithmeticMode.h" | 8 #include "SkArithmeticMode.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 int count, const SkAlpha aaCoverage[]) const { | 88 int count, const SkAlpha aaCoverage[]) const { |
89 SkScalar k1 = fK[0] / 255; | 89 SkScalar k1 = fK[0] / 255; |
90 SkScalar k2 = fK[1]; | 90 SkScalar k2 = fK[1]; |
91 SkScalar k3 = fK[2]; | 91 SkScalar k3 = fK[2]; |
92 SkScalar k4 = fK[3] * 255; | 92 SkScalar k4 = fK[3] * 255; |
93 | 93 |
94 for (int i = 0; i < count; ++i) { | 94 for (int i = 0; i < count; ++i) { |
95 if ((NULL == aaCoverage) || aaCoverage[i]) { | 95 if ((NULL == aaCoverage) || aaCoverage[i]) { |
96 SkPMColor sc = src[i]; | 96 SkPMColor sc = src[i]; |
97 SkPMColor dc = dst[i]; | 97 SkPMColor dc = dst[i]; |
98 int sa = SkGetPackedA32(sc); | |
99 int da = SkGetPackedA32(dc); | |
100 | |
101 int srcNeedsUnpremul = needsUnpremul(sa); | |
102 int dstNeedsUnpremul = needsUnpremul(da); | |
103 | 98 |
104 int a, r, g, b; | 99 int a, r, g, b; |
105 | 100 |
106 if (!srcNeedsUnpremul && !dstNeedsUnpremul) { | 101 if (false) { |
107 a = arith(k1, k2, k3, k4, sa, da); | 102 int sa = SkGetPackedA32(sc); |
| 103 int da = SkGetPackedA32(dc); |
| 104 |
| 105 int srcNeedsUnpremul = needsUnpremul(sa); |
| 106 int dstNeedsUnpremul = needsUnpremul(da); |
| 107 |
| 108 if (!srcNeedsUnpremul && !dstNeedsUnpremul) { |
| 109 a = arith(k1, k2, k3, k4, sa, da); |
| 110 r = arith(k1, k2, k3, k4, SkGetPackedR32(sc), SkGetPackedR32
(dc)); |
| 111 g = arith(k1, k2, k3, k4, SkGetPackedG32(sc), SkGetPackedG32
(dc)); |
| 112 b = arith(k1, k2, k3, k4, SkGetPackedB32(sc), SkGetPackedB32
(dc)); |
| 113 } else { |
| 114 int sr = SkGetPackedR32(sc); |
| 115 int sg = SkGetPackedG32(sc); |
| 116 int sb = SkGetPackedB32(sc); |
| 117 if (srcNeedsUnpremul) { |
| 118 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale
(sa); |
| 119 sr = SkUnPreMultiply::ApplyScale(scale, sr); |
| 120 sg = SkUnPreMultiply::ApplyScale(scale, sg); |
| 121 sb = SkUnPreMultiply::ApplyScale(scale, sb); |
| 122 } |
| 123 |
| 124 int dr = SkGetPackedR32(dc); |
| 125 int dg = SkGetPackedG32(dc); |
| 126 int db = SkGetPackedB32(dc); |
| 127 if (dstNeedsUnpremul) { |
| 128 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale
(da); |
| 129 dr = SkUnPreMultiply::ApplyScale(scale, dr); |
| 130 dg = SkUnPreMultiply::ApplyScale(scale, dg); |
| 131 db = SkUnPreMultiply::ApplyScale(scale, db); |
| 132 } |
| 133 |
| 134 a = arith(k1, k2, k3, k4, sa, da); |
| 135 r = arith(k1, k2, k3, k4, sr, dr); |
| 136 g = arith(k1, k2, k3, k4, sg, dg); |
| 137 b = arith(k1, k2, k3, k4, sb, db); |
| 138 } |
| 139 } else { |
| 140 a = arith(k1, k2, k3, k4, SkGetPackedA32(sc), SkGetPackedA32(dc)
); |
108 r = arith(k1, k2, k3, k4, SkGetPackedR32(sc), SkGetPackedR32(dc)
); | 141 r = arith(k1, k2, k3, k4, SkGetPackedR32(sc), SkGetPackedR32(dc)
); |
| 142 r = SkMin32(r, a); |
109 g = arith(k1, k2, k3, k4, SkGetPackedG32(sc), SkGetPackedG32(dc)
); | 143 g = arith(k1, k2, k3, k4, SkGetPackedG32(sc), SkGetPackedG32(dc)
); |
| 144 g = SkMin32(g, a); |
110 b = arith(k1, k2, k3, k4, SkGetPackedB32(sc), SkGetPackedB32(dc)
); | 145 b = arith(k1, k2, k3, k4, SkGetPackedB32(sc), SkGetPackedB32(dc)
); |
111 } else { | 146 b = SkMin32(b, a); |
112 int sr = SkGetPackedR32(sc); | |
113 int sg = SkGetPackedG32(sc); | |
114 int sb = SkGetPackedB32(sc); | |
115 if (srcNeedsUnpremul) { | |
116 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(sa)
; | |
117 sr = SkUnPreMultiply::ApplyScale(scale, sr); | |
118 sg = SkUnPreMultiply::ApplyScale(scale, sg); | |
119 sb = SkUnPreMultiply::ApplyScale(scale, sb); | |
120 } | |
121 | |
122 int dr = SkGetPackedR32(dc); | |
123 int dg = SkGetPackedG32(dc); | |
124 int db = SkGetPackedB32(dc); | |
125 if (dstNeedsUnpremul) { | |
126 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(da)
; | |
127 dr = SkUnPreMultiply::ApplyScale(scale, dr); | |
128 dg = SkUnPreMultiply::ApplyScale(scale, dg); | |
129 db = SkUnPreMultiply::ApplyScale(scale, db); | |
130 } | |
131 | |
132 a = arith(k1, k2, k3, k4, sa, da); | |
133 r = arith(k1, k2, k3, k4, sr, dr); | |
134 g = arith(k1, k2, k3, k4, sg, dg); | |
135 b = arith(k1, k2, k3, k4, sb, db); | |
136 } | 147 } |
137 | 148 |
138 // apply antialias coverage if necessary | 149 // apply antialias coverage if necessary |
139 if (aaCoverage && 0xFF != aaCoverage[i]) { | 150 if (aaCoverage && 0xFF != aaCoverage[i]) { |
140 int scale = aaCoverage[i] + (aaCoverage[i] >> 7); | 151 int scale = aaCoverage[i] + (aaCoverage[i] >> 7); |
141 a = blend(a, SkGetPackedA32(sc), scale); | 152 a = blend(a, SkGetPackedA32(sc), scale); |
142 r = blend(r, SkGetPackedR32(sc), scale); | 153 r = blend(r, SkGetPackedR32(sc), scale); |
143 g = blend(g, SkGetPackedG32(sc), scale); | 154 g = blend(g, SkGetPackedG32(sc), scale); |
144 b = blend(b, SkGetPackedB32(sc), scale); | 155 b = blend(b, SkGetPackedB32(sc), scale); |
145 } | 156 } |
146 | 157 |
147 // turn the result back into premul | 158 // turn the result back into premul |
148 if (0xFF != a) { | 159 if (false /* 0xFF != a */) { |
149 int scale = a + (a >> 7); | 160 int scale = a + (a >> 7); |
150 r = SkAlphaMul(r, scale); | 161 r = SkAlphaMul(r, scale); |
151 g = SkAlphaMul(g, scale); | 162 g = SkAlphaMul(g, scale); |
152 b = SkAlphaMul(b, scale); | 163 b = SkAlphaMul(b, scale); |
153 } | 164 } |
154 dst[i] = SkPackARGB32(a, r, g, b); | 165 dst[i] = SkPackARGB32(a, r, g, b); |
155 } | 166 } |
156 } | 167 } |
157 } | 168 } |
158 | 169 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 GrAssert(NULL != dstColor); | 365 GrAssert(NULL != dstColor); |
355 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 366 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
356 kVec4f_GrSLType, "k"); | 367 kVec4f_GrSLType, "k"); |
357 const char* kUni = builder->getUniformCStr(fKUni); | 368 const char* kUni = builder->getUniformCStr(fKUni); |
358 | 369 |
359 // We don't try to optimize for this case at all | 370 // We don't try to optimize for this case at all |
360 if (NULL == inputColor) { | 371 if (NULL == inputColor) { |
361 builder->fsCodeAppendf("\t\tconst vec4 src = %s;\n", GrGLSLOnesVecf(4)); | 372 builder->fsCodeAppendf("\t\tconst vec4 src = %s;\n", GrGLSLOnesVecf(4)); |
362 } else { | 373 } else { |
363 builder->fsCodeAppendf("\t\tvec4 src = %s;\n", inputColor); | 374 builder->fsCodeAppendf("\t\tvec4 src = %s;\n", inputColor); |
364 builder->fsCodeAppendf("\t\tsrc.rgb = clamp(src.rgb / src.a, 0.0, 1.0);\
n"); | 375 if (false) { |
| 376 builder->fsCodeAppendf("\t\tsrc.rgb = clamp(src.rgb / src.a, 0.0, 1.
0);\n"); |
| 377 } |
365 } | 378 } |
366 | 379 |
367 builder->fsCodeAppendf("\t\tvec4 dst = %s;\n", dstColor); | 380 builder->fsCodeAppendf("\t\tvec4 dst = %s;\n", dstColor); |
368 builder->fsCodeAppendf("\t\tdst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);\n"); | 381 if (false) { |
| 382 builder->fsCodeAppendf("\t\tdst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);\
n"); |
| 383 } |
369 | 384 |
370 builder->fsCodeAppendf("\t\t%s = %s.x * src * dst + %s.y * src + %s.z * dst
+ %s.w;\n", outputColor, kUni, kUni, kUni, kUni); | 385 builder->fsCodeAppendf("\t\t%s = %s.x * src * dst + %s.y * src + %s.z * dst
+ %s.w;\n", outputColor, kUni, kUni, kUni, kUni); |
371 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu
tColor); | 386 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu
tColor); |
372 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); | 387 if (false) { |
| 388 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); |
| 389 } else { |
| 390 builder->fsCodeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor,
outputColor, outputColor); |
| 391 } |
373 } | 392 } |
374 | 393 |
375 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE
ffect& drawEffect) { | 394 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE
ffect& drawEffect) { |
376 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(
); | 395 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(
); |
377 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); | 396 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); |
378 GrTexture* bgTex = arith.backgroundTexture(); | 397 GrTexture* bgTex = arith.backgroundTexture(); |
379 if (bgTex) { | 398 if (bgTex) { |
380 fBackgroundEffectMatrix.setData(uman, | 399 fBackgroundEffectMatrix.setData(uman, |
381 GrEffect::MakeDivByTextureWHMatrix(bgTex
), | 400 GrEffect::MakeDivByTextureWHMatrix(bgTex
), |
382 drawEffect, | 401 drawEffect, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 background); | 444 background); |
426 } | 445 } |
427 return true; | 446 return true; |
428 } | 447 } |
429 | 448 |
430 #endif | 449 #endif |
431 | 450 |
432 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) | 451 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) |
433 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) | 452 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) |
434 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 453 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |