TextureRenderer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "TextureRenderer.h"
  17. #include "GLTest.h"
  18. #include <gui/GLConsumer.h>
  19. #include <GLES2/gl2.h>
  20. #include <GLES2/gl2ext.h>
  21. #include <gtest/gtest.h>
  22. namespace android {
  23. TextureRenderer::TextureRenderer(GLuint texName,
  24. const sp<GLConsumer>& st) : mTexName(texName), mST(st) {
  25. }
  26. void TextureRenderer::SetUp() {
  27. const char vsrc[] =
  28. "attribute vec4 vPosition;\n"
  29. "varying vec2 texCoords;\n"
  30. "uniform mat4 texMatrix;\n"
  31. "void main() {\n"
  32. " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
  33. " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
  34. " gl_Position = vPosition;\n"
  35. "}\n";
  36. const char fsrc[] =
  37. "#extension GL_OES_EGL_image_external : require\n"
  38. "precision mediump float;\n"
  39. "uniform samplerExternalOES texSampler;\n"
  40. "varying vec2 texCoords;\n"
  41. "void main() {\n"
  42. " gl_FragColor = texture2D(texSampler, texCoords);\n"
  43. "}\n";
  44. {
  45. SCOPED_TRACE("creating shader program");
  46. ASSERT_NO_FATAL_FAILURE(GLTest::createProgram(vsrc, fsrc, &mPgm));
  47. }
  48. mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
  49. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  50. ASSERT_NE(-1, mPositionHandle);
  51. mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
  52. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  53. ASSERT_NE(-1, mTexSamplerHandle);
  54. mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
  55. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  56. ASSERT_NE(-1, mTexMatrixHandle);
  57. }
  58. // drawTexture draws the GLConsumer over the entire GL viewport.
  59. void TextureRenderer::drawTexture() {
  60. static const GLfloat triangleVertices[] = {
  61. -1.0f, 1.0f,
  62. -1.0f, -1.0f,
  63. 1.0f, -1.0f,
  64. 1.0f, 1.0f,
  65. };
  66. glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
  67. triangleVertices);
  68. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  69. glEnableVertexAttribArray(mPositionHandle);
  70. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  71. glUseProgram(mPgm);
  72. glUniform1i(mTexSamplerHandle, 0);
  73. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  74. glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
  75. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  76. // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
  77. // they're setting the defautls for that target, but when hacking
  78. // things to use GL_TEXTURE_2D they are needed to achieve the same
  79. // behavior.
  80. glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
  81. GL_LINEAR);
  82. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  83. glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
  84. GL_LINEAR);
  85. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  86. glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
  87. GL_CLAMP_TO_EDGE);
  88. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  89. glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
  90. GL_CLAMP_TO_EDGE);
  91. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  92. GLfloat texMatrix[16];
  93. mST->getTransformMatrix(texMatrix);
  94. glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
  95. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  96. ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
  97. }
  98. } // namespace android