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