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
17#include "TextureRenderer.h"
18
19#include "GLTest.h"
20
21#include <gui/GLConsumer.h>
22
23#include <GLES2/gl2.h>
24#include <GLES2/gl2ext.h>
25
26#include <gtest/gtest.h>
27
28namespace android {
29
30TextureRenderer::TextureRenderer(GLuint texName,
31        const sp<GLConsumer>& st) : mTexName(texName), mST(st) {
32}
33
34void TextureRenderer::SetUp() {
35    const char vsrc[] =
36        "attribute vec4 vPosition;\n"
37        "varying vec2 texCoords;\n"
38        "uniform mat4 texMatrix;\n"
39        "void main() {\n"
40        "  vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
41        "  texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
42        "  gl_Position = vPosition;\n"
43        "}\n";
44
45    const char fsrc[] =
46        "#extension GL_OES_EGL_image_external : require\n"
47        "precision mediump float;\n"
48        "uniform samplerExternalOES texSampler;\n"
49        "varying vec2 texCoords;\n"
50        "void main() {\n"
51        "  gl_FragColor = texture2D(texSampler, texCoords);\n"
52        "}\n";
53
54    {
55        SCOPED_TRACE("creating shader program");
56        ASSERT_NO_FATAL_FAILURE(GLTest::createProgram(vsrc, fsrc, &mPgm));
57    }
58
59    mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
60    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
61    ASSERT_NE(-1, mPositionHandle);
62    mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
63    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
64    ASSERT_NE(-1, mTexSamplerHandle);
65    mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
66    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
67    ASSERT_NE(-1, mTexMatrixHandle);
68}
69
70// drawTexture draws the GLConsumer over the entire GL viewport.
71void TextureRenderer::drawTexture() {
72    static const GLfloat triangleVertices[] = {
73        -1.0f, 1.0f,
74        -1.0f, -1.0f,
75        1.0f, -1.0f,
76        1.0f, 1.0f,
77    };
78
79    glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
80            triangleVertices);
81    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
82    glEnableVertexAttribArray(mPositionHandle);
83    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
84
85    glUseProgram(mPgm);
86    glUniform1i(mTexSamplerHandle, 0);
87    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
88    glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
89    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
90
91    // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
92    // they're setting the defautls for that target, but when hacking
93    // things to use GL_TEXTURE_2D they are needed to achieve the same
94    // behavior.
95    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
96            GL_LINEAR);
97    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
98    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
99            GL_LINEAR);
100    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
101    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
102            GL_CLAMP_TO_EDGE);
103    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
104    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
105            GL_CLAMP_TO_EDGE);
106    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
107
108    GLfloat texMatrix[16];
109    mST->getTransformMatrix(texMatrix);
110    glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
111
112    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
113    ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
114}
115
116} // namespace android
117