Apply transformation matrix to texture coordinates.

SurfaceTexture provides transformation matrix which should
be used to transform texture coordinates before sampling
to account for possible coordinate differences in the
underlying native buffer.

This was causing the texture to be flipped when rendered.
Also fix the square coordinates which caused stretching.

Bug: 301023410
Test: atest virtual_camera_tests
Test: atest CtsVirtualCameraTestCases
Test: manual with sample app & canvas + video

Change-Id: Ib7fd2bb03e6686ada193ea67541f051d981f2bad
diff --git a/services/camera/virtualcamera/util/EglProgram.cc b/services/camera/virtualcamera/util/EglProgram.cc
index 510fd33..202ec3e 100644
--- a/services/camera/virtualcamera/util/EglProgram.cc
+++ b/services/camera/virtualcamera/util/EglProgram.cc
@@ -68,12 +68,13 @@
     })";
 
 constexpr char kExternalTextureVertexShader[] = R"(#version 300 es
+  uniform mat4 aTextureTransformMatrix; // Transform matrix given by surface texture.
   in vec4 aPosition;
   in vec2 aTextureCoord;
   out vec2 vTextureCoord;
   void main() {
     gl_Position = aPosition;
-    vTextureCoord = aTextureCoord;
+    vTextureCoord = (aTextureTransformMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy;
   })";
 
 constexpr char kExternalYuvTextureFragmentShader[] = R"(#version 300 es
@@ -100,10 +101,12 @@
     })";
 
 constexpr int kCoordsPerVertex = 3;
-constexpr std::array<float, 12> kSquareCoords{-1.f, 1.0f, 0.0f,  // top left
-                                              -1.f, -1.f, 0.0f,  // bottom left
-                                              1.0f, -1.f, 0.0f,  // bottom right
-                                              1.0f, 1.0f, 0.0f};  // top right
+
+constexpr std::array<float, 12> kSquareCoords{
+    -1.f, -1.0f, 0.0f,   // top left
+    -1.f, 1.f,   0.0f,   // bottom left
+    1.0f, 1.f,   0.0f,   // bottom right
+    1.0f, -1.0f, 0.0f};  // top right
 
 constexpr std::array<float, 8> kTextureCoords{0.0f, 1.0f,   // top left
                                               0.0f, 0.0f,   // bottom left
@@ -267,7 +270,8 @@
   }
 }
 
-bool EglTextureProgram::draw(GLuint textureId) {
+bool EglTextureProgram::draw(GLuint textureId,
+                             const std::array<float, 16>& transformMatrix) {
   // Load compiled shader.
   glUseProgram(mProgram);
   if (checkEglError("glUseProgram")) {
@@ -286,6 +290,12 @@
   glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, false,
                         kTextureCoords.size(), kTextureCoords.data());
 
+  // Pass transformation matrix for the texture coordinates.
+  int transformMatrixHandle =
+      glGetUniformLocation(mProgram, "aTextureTransformMatrix");
+  glUniformMatrix4fv(transformMatrixHandle, 1, /*transpose=*/GL_FALSE,
+                     transformMatrix.data());
+
   // Configure texture for the shader.
   int textureHandle = glGetUniformLocation(mProgram, "uTexture");
   glActiveTexture(GL_TEXTURE0);