Various fixes for linear blending and gradients

With linear blending turned off some textures were still
created as sRGB textures instead of linear textures.
Multi-stop gradients were not behaving properly on devices
with no support for float textures.
Gradients are now always interpolated in linear space
even if linear blending is off.
New functions to always force sRGB->linear->sRGB conversions.

Test: Manual testing
Bug: 29940137
Change-Id: Ie2f84ee2a65fd85570e88af813e841e0e625df6c
diff --git a/libs/hwui/Extensions.cpp b/libs/hwui/Extensions.cpp
index 4dc7536..1d67579 100644
--- a/libs/hwui/Extensions.cpp
+++ b/libs/hwui/Extensions.cpp
@@ -45,13 +45,18 @@
     mHas1BitStencil = extensions.has("GL_OES_stencil1");
     mHas4BitStencil = extensions.has("GL_OES_stencil4");
     mHasUnpackSubImage = extensions.has("GL_EXT_unpack_subimage");
-    mHasSRGB = extensions.has("GL_EXT_sRGB");
+
+#ifdef ANDROID_ENABLE_LINEAR_BLENDING
+    mHasSRGB = mVersionMajor >= 3 || extensions.has("GL_EXT_sRGB");
     mHasSRGBWriteControl = extensions.has("GL_EXT_sRGB_write_control");
 
-    // If linear blending is enabled, the device must have ES3.0 and GL_EXT_sRGB_write_control
-#ifdef ANDROID_ENABLE_LINEAR_BLENDING
-    assert(mVersionMajor >= 3 || mHasSRGB);
-    assert(mHasSRGBWriteControl);
+    // If linear blending is enabled, the device must have (ES3.0 or EXT_sRGB)
+    // and EXT_sRGB_write_control
+    LOG_ALWAYS_FATAL_IF(!mHasSRGB, "Linear blending requires ES 3.0 or EXT_sRGB");
+    LOG_ALWAYS_FATAL_IF(!mHasSRGBWriteControl, "Linear blending requires EXT_sRGB_write_control");
+#else
+    mHasSRGB = false;
+    mHasSRGBWriteControl = false;
 #endif
 
     const char* version = (const char*) glGetString(GL_VERSION);
diff --git a/libs/hwui/Extensions.h b/libs/hwui/Extensions.h
index cc73c23..2c38507 100644
--- a/libs/hwui/Extensions.h
+++ b/libs/hwui/Extensions.h
@@ -43,7 +43,7 @@
     inline bool hasPixelBufferObjects() const { return mVersionMajor >= 3; }
     inline bool hasOcclusionQueries() const { return mVersionMajor >= 3; }
     inline bool hasFloatTextures() const { return mVersionMajor >= 3; }
-    inline bool hasSRGB() const { return mVersionMajor >= 3 || mHasSRGB; }
+    inline bool hasSRGB() const { return mHasSRGB; }
     inline bool hasSRGBWriteControl() const { return hasSRGB() && mHasSRGBWriteControl; }
 
     inline int getMajorGlVersion() const { return mVersionMajor; }
diff --git a/libs/hwui/FloatColor.h b/libs/hwui/FloatColor.h
index 6d19b7c..9df7338 100644
--- a/libs/hwui/FloatColor.h
+++ b/libs/hwui/FloatColor.h
@@ -28,8 +28,20 @@
 struct FloatColor {
     // "color" is a gamma-encoded sRGB color
     // After calling this method, the color is stored as a pre-multiplied linear color
+    // if linear blending is enabled. Otherwise, the color is stored as a pre-multiplied
+    // gamma-encoded sRGB color
     void set(uint32_t color) {
         a = ((color >> 24) & 0xff) / 255.0f;
+        r = a * EOCF(((color >> 16) & 0xff) / 255.0f);
+        g = a * EOCF(((color >>  8) & 0xff) / 255.0f);
+        b = a * EOCF(((color      ) & 0xff) / 255.0f);
+    }
+
+    // "color" is a gamma-encoded sRGB color
+    // After calling this method, the color is stored as a pre-multiplied linear color
+    // if linear blending is enabled.
+    void setSRGB(uint32_t color) {
+        a = ((color >> 24) & 0xff) / 255.0f;
         r = a * EOCF_sRGB(((color >> 16) & 0xff) / 255.0f);
         g = a * EOCF_sRGB(((color >>  8) & 0xff) / 255.0f);
         b = a * EOCF_sRGB(((color      ) & 0xff) / 255.0f);
diff --git a/libs/hwui/GlopBuilder.cpp b/libs/hwui/GlopBuilder.cpp
index ff88d02..65922f6 100644
--- a/libs/hwui/GlopBuilder.cpp
+++ b/libs/hwui/GlopBuilder.cpp
@@ -289,10 +289,10 @@
             // Skia uses the range [0..255] for the addition vector, but we need
             // the [0..1] range to apply the vector in GLSL
             float* colorVector = mOutGlop->fill.filter.matrix.vector;
-            colorVector[0] = EOCF_sRGB(srcColorMatrix[4]  / 255.0f);
-            colorVector[1] = EOCF_sRGB(srcColorMatrix[9]  / 255.0f);
-            colorVector[2] = EOCF_sRGB(srcColorMatrix[14] / 255.0f);
-            colorVector[3] = EOCF_sRGB(srcColorMatrix[19] / 255.0f);
+            colorVector[0] = EOCF(srcColorMatrix[4]  / 255.0f);
+            colorVector[1] = EOCF(srcColorMatrix[9]  / 255.0f);
+            colorVector[2] = EOCF(srcColorMatrix[14] / 255.0f);
+            colorVector[3] =      srcColorMatrix[19] / 255.0f;  // alpha is linear
         } else {
             LOG_ALWAYS_FATAL("unsupported ColorFilter");
         }
diff --git a/libs/hwui/GradientCache.cpp b/libs/hwui/GradientCache.cpp
index 8573ab0..cd3ccf9 100644
--- a/libs/hwui/GradientCache.cpp
+++ b/libs/hwui/GradientCache.cpp
@@ -185,22 +185,28 @@
     return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
 }
 
-void GradientCache::mixBytes(FloatColor& start, FloatColor& end, float amount,
-        uint8_t*& dst) const {
+void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end,
+        float amount, uint8_t*& dst) const {
     float oppAmount = 1.0f - amount;
-    *dst++ = uint8_t(OECF_sRGB((start.r * oppAmount + end.r * amount) * 255.0f));
-    *dst++ = uint8_t(OECF_sRGB((start.g * oppAmount + end.g * amount) * 255.0f));
-    *dst++ = uint8_t(OECF_sRGB((start.b * oppAmount + end.b * amount) * 255.0f));
-    *dst++ = uint8_t(          (start.a * oppAmount + end.a * amount) * 255.0f);
+    *dst++ = uint8_t(OECF_sRGB(start.r * oppAmount + end.r * amount) * 255.0f);
+    *dst++ = uint8_t(OECF_sRGB(start.g * oppAmount + end.g * amount) * 255.0f);
+    *dst++ = uint8_t(OECF_sRGB(start.b * oppAmount + end.b * amount) * 255.0f);
+    *dst++ = uint8_t(         (start.a * oppAmount + end.a * amount) * 255.0f);
 }
 
-void GradientCache::mixFloats(FloatColor& start, FloatColor& end, float amount,
-        uint8_t*& dst) const {
+void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end,
+        float amount, uint8_t*& dst) const {
     float oppAmount = 1.0f - amount;
     float* d = (float*) dst;
+#if ANDROID_LINEAR_BLENDING_ENABLED
     *d++ = start.r * oppAmount + end.r * amount;
     *d++ = start.g * oppAmount + end.g * amount;
     *d++ = start.b * oppAmount + end.b * amount;
+#else
+    *d++ = OECF_sRGB(start.r * oppAmount + end.r * amount);
+    *d++ = OECF_sRGB(start.g * oppAmount + end.g * amount);
+    *d++ = OECF_sRGB(start.b * oppAmount + end.b * amount);
+#endif
     *d++ = start.a * oppAmount + end.a * amount;
     dst += 4 * sizeof(float);
 }
@@ -217,10 +223,10 @@
     ChannelMixer mix = gMixers[mUseFloatTexture];
 
     FloatColor start;
-    start.set(colors[0]);
+    start.setSRGB(colors[0]);
 
     FloatColor end;
-    end.set(colors[1]);
+    end.setSRGB(colors[1]);
 
     int currentPos = 1;
     float startPos = positions[0];
@@ -235,7 +241,7 @@
 
             currentPos++;
 
-            end.set(colors[currentPos]);
+            end.setSRGB(colors[currentPos]);
             distance = positions[currentPos] - startPos;
         }
 
diff --git a/libs/hwui/GradientCache.h b/libs/hwui/GradientCache.h
index 3fd8e80..5e35435 100644
--- a/libs/hwui/GradientCache.h
+++ b/libs/hwui/GradientCache.h
@@ -154,11 +154,13 @@
     size_t bytesPerPixel() const;
     size_t sourceBytesPerPixel() const;
 
-    typedef void (GradientCache::*ChannelMixer)(FloatColor& start, FloatColor& end,
+    typedef void (GradientCache::*ChannelMixer)(const FloatColor& start, const FloatColor& end,
             float amount, uint8_t*& dst) const;
 
-    void mixBytes(FloatColor& start, FloatColor& end, float amount, uint8_t*& dst) const;
-    void mixFloats(FloatColor& start, FloatColor& end, float amount, uint8_t*& dst) const;
+    void mixBytes(const FloatColor& start, const FloatColor& end,
+            float amount, uint8_t*& dst) const;
+    void mixFloats(const FloatColor& start, const FloatColor& end,
+            float amount, uint8_t*& dst) const;
 
     LruCache<GradientCacheEntry, Texture*> mCache;
 
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 315c60e..4ef6b85 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -163,25 +163,31 @@
 // When we are writing to an sRGB framebuffer, we must do the following:
 //     EOCF(OECF(color) + dither)
 // We approximate the transfer functions with gamma 2.0 to avoid branches and pow()
-// The dithering pattern is generated with a triangle noise generator in the range [-0.5,1.5[
+// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0]
 // TODO: Handle linear fp16 render targets
-const char* gFS_Dither_Functions =
-        "\nmediump float triangleNoise(const highp vec2 n) {\n"
+const char* gFS_Gradient_Functions =
+        "\nfloat triangleNoise(const highp vec2 n) {\n"
         "    highp vec2 p = fract(n * vec2(5.3987, 5.4421));\n"
         "    p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));\n"
         "    highp float xy = p.x * p.y;\n"
-        "    return fract(xy * 95.4307) + fract(xy * 75.04961) - 0.5;\n"
+        "    return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;\n"
         "}\n";
-const char* gFS_Dither_Preamble[2] = {
+const char* gFS_Gradient_Preamble[2] = {
         // Linear framebuffer
         "\nvec4 dither(const vec4 color) {\n"
         "    return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);"
+        "}\n"
+        "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
+        "    return pow(mix(a, b, v), vec4(vec3(1.0 / 2.2), 1.0));"
         "}\n",
         // sRGB framebuffer
         "\nvec4 dither(const vec4 color) {\n"
         "    vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n"
         "    return vec4(dithered * dithered, color.a);\n"
         "}\n"
+        "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
+        "    return mix(a, b, v);"
+        "}\n"
 };
 
 // Uses luminance coefficients from Rec.709 to choose the appropriate gamma
@@ -239,7 +245,7 @@
         "    gl_FragColor = dither(texture2D(gradientSampler, linear));\n"
         "}\n\n",
         "\nvoid main(void) {\n"
-        "    gl_FragColor = dither(mix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
+        "    gl_FragColor = dither(gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
         "}\n\n",
 };
 const char* gFS_Fast_SingleModulateGradient[2] = {
@@ -247,7 +253,7 @@
         "    gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n"
         "}\n\n",
         "\nvoid main(void) {\n"
-        "    gl_FragColor = dither(color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
+        "    gl_FragColor = dither(color.a * gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
         "}\n\n"
 };
 
@@ -279,19 +285,19 @@
         // Linear
         "    vec4 gradientColor = texture2D(gradientSampler, linear);\n",
 
-        "    vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
+        "    vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
 
         // Circular
         "    vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
 
-        "    vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
+        "    vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
 
         // Sweep
         "    highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
         "    vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
 
         "    highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
-        "    vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
+        "    vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
 };
 const char* gFS_Main_FetchBitmap =
         "    vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
@@ -665,8 +671,8 @@
             }
             fast = true;
         } else if (singleGradient) {
-            shader.append(gFS_Dither_Functions);
-            shader.append(gFS_Dither_Preamble[mHasSRGB]);
+            shader.append(gFS_Gradient_Functions);
+            shader.append(gFS_Gradient_Preamble[mHasSRGB]);
             if (!description.modulate) {
                 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
             } else {
@@ -705,8 +711,8 @@
         generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
     }
     if (description.hasGradient) {
-        shader.append(gFS_Dither_Functions);
-        shader.append(gFS_Dither_Preamble[mHasSRGB]);
+        shader.append(gFS_Gradient_Functions);
+        shader.append(gFS_Gradient_Preamble[mHasSRGB]);
     }
 
     // Begin the shader
diff --git a/libs/hwui/SkiaShader.cpp b/libs/hwui/SkiaShader.cpp
index 9838df2..5d9e5c0 100644
--- a/libs/hwui/SkiaShader.cpp
+++ b/libs/hwui/SkiaShader.cpp
@@ -173,8 +173,8 @@
         outData->gradientSampler = 0;
         outData->gradientTexture = nullptr;
 
-        outData->startColor.set(gradInfo.fColors[0]);
-        outData->endColor.set(gradInfo.fColors[1]);
+        outData->startColor.setSRGB(gradInfo.fColors[0]);
+        outData->endColor.setSRGB(gradInfo.fColors[1]);
     }
 
     return true;
diff --git a/libs/hwui/utils/Color.h b/libs/hwui/utils/Color.h
index c8f8c70..f9cc46d 100644
--- a/libs/hwui/utils/Color.h
+++ b/libs/hwui/utils/Color.h
@@ -85,10 +85,17 @@
     // Opto-electronic conversion function for the sRGB color space
     // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
     static constexpr float OECF_sRGB(float linear) {
-#ifdef ANDROID_ENABLE_LINEAR_BLENDING
         // IEC 61966-2-1:1999
         return linear <= 0.0031308f ?
                 linear * 12.92f : (powf(linear, 1.0f / 2.4f) * 1.055f) - 0.055f;
+    }
+
+    // Opto-electronic conversion function for the sRGB color space
+    // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
+    // This function returns the input unmodified if linear blending is not enabled
+    static constexpr float OECF(float linear) {
+#ifdef ANDROID_ENABLE_LINEAR_BLENDING
+        return OECF_sRGB(linear);
 #else
         return linear;
 #endif
@@ -97,9 +104,16 @@
     // Electro-optical conversion function for the sRGB color space
     // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
     static constexpr float EOCF_sRGB(float srgb) {
-#ifdef ANDROID_ENABLE_LINEAR_BLENDING
         // IEC 61966-2-1:1999
         return srgb <= 0.04045f ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
+    }
+
+    // Electro-optical conversion function for the sRGB color space
+    // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
+    // This function returns the input unmodified if linear blending is not enabled
+    static constexpr float EOCF(float srgb) {
+#ifdef ANDROID_ENABLE_LINEAR_BLENDING
+        return EOCF_sRGB(srgb);
 #else
         return srgb;
 #endif