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