Add an API to set the transform on a TextureView's surface texture.
Bug #5156689

Change-Id: I635a625885c9b832a60d44ece0de7613ceb84109
diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h
index dd75497..a8ae5c6 100644
--- a/libs/hwui/Layer.h
+++ b/libs/hwui/Layer.h
@@ -203,6 +203,10 @@
         return texTransform;
     }
 
+    inline mat4& getTransform() {
+        return transform;
+    }
+
     /**
      * Bounds of the layer.
      */
@@ -282,6 +286,11 @@
      */
     mat4 texTransform;
 
+    /**
+     * Optional transform.
+     */
+    mat4 transform;
+
 }; // struct Layer
 
 }; // namespace uirenderer
diff --git a/libs/hwui/Matrix.cpp b/libs/hwui/Matrix.cpp
index 9fc5131..769c99c 100644
--- a/libs/hwui/Matrix.cpp
+++ b/libs/hwui/Matrix.cpp
@@ -51,6 +51,7 @@
     data[kTranslateZ]   = 0.0f;
     data[kPerspective2] = 1.0f;
 
+    mIsIdentity = true;
     mSimpleMatrix = true;
 }
 
@@ -71,14 +72,21 @@
     return mSimpleMatrix;
 }
 
+bool Matrix4::isIdentity() {
+    return mIsIdentity;
+}
+
 void Matrix4::load(const float* v) {
     memcpy(data, v, sizeof(data));
+    // TODO: Do something smarter here
     mSimpleMatrix = false;
+    mIsIdentity = false;
 }
 
 void Matrix4::load(const Matrix4& v) {
     memcpy(data, v.data, sizeof(data));
     mSimpleMatrix = v.mSimpleMatrix;
+    mIsIdentity = v.mIsIdentity;
 }
 
 void Matrix4::load(const SkMatrix& v) {
@@ -99,6 +107,7 @@
     data[kScaleZ] = 1.0f;
 
     mSimpleMatrix = (v.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
+    mIsIdentity = v.isIdentity();
 }
 
 void Matrix4::copyTo(SkMatrix& v) const {
@@ -148,6 +157,7 @@
             v.data[kSkewX] * v.data[kSkewY]) * scale;
 
     mSimpleMatrix = v.mSimpleMatrix;
+    mIsIdentity = v.mIsIdentity;
 }
 
 void Matrix4::copyTo(float* v) const {
@@ -166,20 +176,27 @@
     for (int i = 0; i < 16; i++) {
         data[i] *= v;
     }
+    mIsIdentity = false;
 }
 
 void Matrix4::loadTranslate(float x, float y, float z) {
     loadIdentity();
+
     data[kTranslateX] = x;
     data[kTranslateY] = y;
     data[kTranslateZ] = z;
+
+    mIsIdentity = false;
 }
 
 void Matrix4::loadScale(float sx, float sy, float sz) {
     loadIdentity();
+
     data[kScaleX] = sx;
     data[kScaleY] = sy;
     data[kScaleZ] = sz;
+
+    mIsIdentity = false;
 }
 
 void Matrix4::loadSkew(float sx, float sy) {
@@ -198,6 +215,7 @@
     data[kPerspective2] = 1.0f;
 
     mSimpleMatrix = false;
+    mIsIdentity = false;
 }
 
 void Matrix4::loadRotate(float angle, float x, float y, float z) {
@@ -238,6 +256,7 @@
     data[kScaleZ] = z * z * nc +  c;
 
     mSimpleMatrix = false;
+    mIsIdentity = false;
 }
 
 void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
@@ -262,16 +281,20 @@
     }
 
     mSimpleMatrix = u.mSimpleMatrix && v.mSimpleMatrix;
+    mIsIdentity = false;
 }
 
 void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
     loadIdentity();
+
     data[kScaleX] = 2.0f / (right - left);
     data[kScaleY] = 2.0f / (top - bottom);
     data[kScaleZ] = -2.0f / (far - near);
     data[kTranslateX] = -(right + left) / (right - left);
     data[kTranslateY] = -(top + bottom) / (top - bottom);
     data[kTranslateZ] = -(far + near) / (far - near);
+
+    mIsIdentity = false;
 }
 
 #define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c)
diff --git a/libs/hwui/Matrix.h b/libs/hwui/Matrix.h
index 2fa6ab7..56fd37d 100644
--- a/libs/hwui/Matrix.h
+++ b/libs/hwui/Matrix.h
@@ -112,6 +112,7 @@
 
     bool isPureTranslate();
     bool isSimple();
+    bool isIdentity();
 
     bool changesBounds();
 
@@ -128,6 +129,7 @@
 
 private:
     bool mSimpleMatrix;
+    bool mIsIdentity;
 
     inline float get(int i, int j) const {
         return data[i * 4 + j];
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 4864cff..a0f806a 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -627,6 +627,12 @@
 void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
     float alpha = layer->getAlpha() / 255.0f;
 
+    mat4& transform = layer->getTransform();
+    if (!transform.isIdentity()) {
+        save(0);
+        mSnapshot->transform->multiply(transform);
+    }
+
     setupDraw();
     if (layer->getRenderTarget() == GL_TEXTURE_2D) {
         setupDrawWithTexture();
@@ -663,6 +669,10 @@
     glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
 
     finishDrawTexture();
+
+    if (!transform.isIdentity()) {
+        restore();
+    }
 }
 
 void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {