Convert bitmaps to sRGB/scRGB when they have a color profile

This change also fixes an issue with RGBA16F bitmaps when modulated
with a color (for instance by setting an alpha on the Paint object).

The color space conversion is currently done entirely in the shader,
by doing these operations in order:

1. Sample the texture
2. Un-premultiply alpha
3. Apply the EOTF
4. Multiply by the 3x3 color space matrix
5. Apply the OETF
6. Premultiply alpha

Optimizations:
- Steps 2 & 6 are skipped for opaque (common) bitmaps
- Step 3 is skipped when the color space's EOTF is close
  to sRGB (Display P3 for instance). Instead, we use
  a hardware sRGB fetch (when the GPU supports it)
- When step 3 is necessary, we use one of four standard
  EOTF implementations, to save cycles when possible:
  + Linear (doesn't do anything)
  + Full parametric (ICC parametric curve type 4 as defined
    in ICC.1:2004-10, section 10.15)
  + Limited parametric (ICC parametric curve type 3)
  + Gamma (ICC parametric curve type 0)

Color space conversion could be done using texture samplers
instead, for instance 3D LUTs, with or without transfer
functions baked in, or 1D LUTs for transfer functions. This
would result in dependent texture fetches which may or may
not be an advantage over an ALU based implementation. The
current solution favor the use of ALUs to save precious
bandwidth.

Test: CtsUiRenderingTests, CtsGraphicsTests
Bug: 32984164
Change-Id: I10bc3db515e13973b45220f129c66b23f0f7f8fe
diff --git a/libs/hwui/renderstate/RenderState.cpp b/libs/hwui/renderstate/RenderState.cpp
index 8bce990..c8833d2 100644
--- a/libs/hwui/renderstate/RenderState.cpp
+++ b/libs/hwui/renderstate/RenderState.cpp
@@ -22,8 +22,11 @@
 #include "renderthread/CanvasContext.h"
 #include "renderthread/EglManager.h"
 #include "utils/GLUtils.h"
+
 #include <algorithm>
 
+#include <ui/ColorSpace.h>
+
 namespace android {
 namespace uirenderer {
 
@@ -359,6 +362,40 @@
             fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
     const AutoTexture autoCleanup(texture);
 
+    // If we have a shader and a base texture, the base texture is assumed to be an alpha mask
+    // which means the color space conversion applies to the shader's bitmap
+    Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture;
+    if (colorSpaceTexture != nullptr) {
+        if (colorSpaceTexture->hasColorSpaceConversion()) {
+            const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
+            glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1,
+                    GL_FALSE, connector->getTransform().asArray());
+        }
+
+        TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType();
+        if (transferFunction != TransferFunctionType::None) {
+            const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector();
+            const ColorSpace& source = connector->getSource();
+
+            switch (transferFunction) {
+                case TransferFunctionType::None:
+                    break;
+                case TransferFunctionType::Full:
+                    glUniform1fv(fill.program->getUniform("transferFunction"), 7,
+                            reinterpret_cast<const float*>(&source.getTransferParameters().g));
+                    break;
+                case TransferFunctionType::Limited:
+                    glUniform1fv(fill.program->getUniform("transferFunction"), 5,
+                            reinterpret_cast<const float*>(&source.getTransferParameters().g));
+                    break;
+                case TransferFunctionType::Gamma:
+                    glUniform1f(fill.program->getUniform("transferFunctionGamma"),
+                            source.getTransferParameters().g);
+                    break;
+            }
+        }
+    }
+
     // ------------------------------------
     // ---------- GL state setup ----------
     // ------------------------------------