Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ca2d7970 authored by Jan Sebechlebsky's avatar Jan Sebechlebsky
Browse files

Cache handles to attributes & uniforms for texture shader

... instead of looking them up for every draw call.

Bug: 301023410
Test: atest virtual_camera_tests
Test: atest CtsVirtualCameraTestCases
Test: manual with sample app & canvas + video
Change-Id: I081c12fecf8d4e319a857c321342d16deb798597
parent 99492e3c
Loading
Loading
Loading
Loading
+28 −17
Original line number Diff line number Diff line
@@ -268,6 +268,32 @@ EglTextureProgram::EglTextureProgram(const TextureFormat textureFormat) {
  } else {
    ALOGE("External texture EGL shader program initialization failed.");
  }

  // Lookup and cache handles to uniforms & attributes.
  mPositionHandle = glGetAttribLocation(mProgram, "aPosition");
  mTextureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord");
  mTransformMatrixHandle =
      glGetUniformLocation(mProgram, "aTextureTransformMatrix");
  mTextureHandle = glGetUniformLocation(mProgram, "uTexture");

  // Pass vertex array to the shader.
  glEnableVertexAttribArray(mPositionHandle);
  glVertexAttribPointer(mPositionHandle, kCoordsPerVertex, GL_FLOAT, false,
                        kSquareCoords.size(), kSquareCoords.data());

  // Pass texture coordinates corresponding to vertex array to the shader.
  glEnableVertexAttribArray(mTextureCoordHandle);
  glVertexAttribPointer(mTextureCoordHandle, 2, GL_FLOAT, false,
                        kTextureCoords.size(), kTextureCoords.data());
}

EglTextureProgram::~EglTextureProgram() {
  if (mPositionHandle != -1) {
    glDisableVertexAttribArray(mPositionHandle);
  }
  if (mTextureCoordHandle != -1) {
    glDisableVertexAttribArray(mTextureCoordHandle);
  }
}

bool EglTextureProgram::draw(GLuint textureId,
@@ -278,29 +304,14 @@ bool EglTextureProgram::draw(GLuint textureId,
    return false;
  }

  // Pass vertex array to the shader.
  int positionHandle = glGetAttribLocation(mProgram, "aPosition");
  glEnableVertexAttribArray(positionHandle);
  glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
                        kSquareCoords.size(), kSquareCoords.data());

  // Pass texture coordinates corresponding to vertex array to the shader.
  int textureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord");
  glEnableVertexAttribArray(textureCoordHandle);
  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,
  glUniformMatrix4fv(mTextureCoordHandle, 1, /*transpose=*/GL_FALSE,
                     transformMatrix.data());

  // Configure texture for the shader.
  int textureHandle = glGetUniformLocation(mProgram, "uTexture");
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId);
  glUniform1i(textureHandle, 0);
  glUniform1i(mTextureHandle, 0);

  // Draw triangle strip forming a square filling the viewport.
  glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
+7 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ class EglTextureProgram : public EglProgram {
  enum class TextureFormat { RGBA, YUV };

  EglTextureProgram(TextureFormat textureFormat = TextureFormat::YUV);
  virtual ~EglTextureProgram();

  // Draw texture over whole viewport, applying transformMatrix to texture
  // coordinates.
@@ -70,6 +71,12 @@ class EglTextureProgram : public EglProgram {
  //
  // textureCoord = transformMatrix * vec4(s,t,0,1).xy
  bool draw(GLuint textureId, const std::array<float, 16>& transformMatrix);

 private:
  int mPositionHandle = -1;
  int mTextureCoordHandle = -1;
  int mTransformMatrixHandle = -1;
  int mTextureHandle = -1;
};

}  // namespace virtualcamera