Clamp premul colors correctly after a gamut change
Textures that we sample on the GPU are always premul, so we should
actually clamp to alpha.
Colors that are xformed on the CPU are always unpremul, so clamping to
[0,1] is correct. Add a comment explaining that.
BUG=skia:
Change-Id: I180f2d410f24afc78bd03ab8636a83fb443d68e2
Reviewed-on: https://skia-review.googlesource.com/6581
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
diff --git a/src/gpu/GrColorSpaceXform.cpp b/src/gpu/GrColorSpaceXform.cpp
index cd8dc33..7690297 100644
--- a/src/gpu/GrColorSpaceXform.cpp
+++ b/src/gpu/GrColorSpaceXform.cpp
@@ -121,6 +121,7 @@
GrColor4f GrColorSpaceXform::apply(const GrColor4f& srcColor) {
GrColor4f result;
fSrcToDst.mapScalars(srcColor.fRGBA, result.fRGBA);
+ // We always operate on unpremul colors, so clamp to [0,1].
for (int i = 0; i < 4; ++i) {
result.fRGBA[i] = SkTPin(result.fRGBA[i], 0.0f, 1.0f);
}
diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.cpp b/src/gpu/glsl/GrGLSLShaderBuilder.cpp
index e2cbdee..5609a9c 100644
--- a/src/gpu/glsl/GrGLSLShaderBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLShaderBuilder.cpp
@@ -124,8 +124,9 @@
GrShaderVar("xform", kMat44f_GrSLType),
};
SkString functionBody;
- // Gamut xform, clamp to destination gamut
- functionBody.append("\tcolor.rgb = clamp((xform * vec4(color.rgb, 1.0)).rgb, 0.0, 1.0);\n");
+ // Gamut xform, clamp to destination gamut. We only support/have premultiplied textures, so we
+ // always just clamp to alpha.
+ functionBody.append("\tcolor.rgb = clamp((xform * vec4(color.rgb, 1.0)).rgb, 0.0, color.a);\n");
functionBody.append("\treturn color;");
SkString colorGamutXformFuncName;
this->emitFunction(kVec4f_GrSLType,