Clamp premul colors correctly after a gamut change
authorBrian Osman <brianosman@google.com>
Wed, 4 Jan 2017 17:54:07 +0000 (12:54 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 4 Jan 2017 18:28:26 +0000 (18:28 +0000)
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>
src/gpu/GrColorSpaceXform.cpp
src/gpu/glsl/GrGLSLShaderBuilder.cpp

index cd8dc33..7690297 100644 (file)
@@ -121,6 +121,7 @@ bool GrColorSpaceXform::Equals(const GrColorSpaceXform* a, const GrColorSpaceXfo
 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);
     }
index e2cbdee..5609a9c 100644 (file)
@@ -124,8 +124,9 @@ void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
         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,