Program.h revision aaa3f358410701710e31f31de62f0b4521989661
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#ifndef SCREENRECORD_PROGRAM_H 18#define SCREENRECORD_PROGRAM_H 19 20#include <utils/Errors.h> 21 22#include <EGL/egl.h> 23#include <GLES2/gl2.h> 24 25namespace android { 26 27/* 28 * Utility class for GLES rendering. 29 * 30 * Not thread-safe. 31 */ 32class Program { 33public: 34 enum ProgramType { PROGRAM_UNKNOWN=0, PROGRAM_EXTERNAL_TEXTURE, 35 PROGRAM_TEXTURE_2D }; 36 37 Program() : 38 mProgramType(PROGRAM_UNKNOWN), 39 mProgram(0), 40 maPositionLoc(0), 41 maTextureCoordLoc(0), 42 muMVPMatrixLoc(0), 43 muGLCMatrixLoc(0), 44 muTextureLoc(0) 45 {} 46 ~Program() { release(); } 47 48 // Initialize the program for use with the specified texture type. 49 status_t setup(ProgramType type); 50 51 // Release the program and associated resources. 52 void release(); 53 54 // Blit the specified texture to { x, y, x+w, y+h }. 55 status_t blit(GLuint texName, const float* texMatrix, 56 int32_t x, int32_t y, int32_t w, int32_t h) const; 57 58 // Draw a number of triangles. 59 status_t drawTriangles(GLuint texName, const float* texMatrix, 60 const float* vertices, const float* texes, size_t count) const; 61 62 static const float kIdentity[]; 63 64private: 65 Program(const Program&); 66 Program& operator=(const Program&); 67 68 // Common code for draw functions. 69 status_t beforeDraw(GLuint texName, const float* texMatrix, 70 const float* vertices, const float* texes) const; 71 status_t afterDraw() const; 72 73 // GLES 2 shader utilities. 74 status_t createProgram(GLuint* outPgm, const char* vertexShader, 75 const char* fragmentShader); 76 static status_t compileShader(GLenum shaderType, const char* src, 77 GLuint* outShader); 78 static status_t linkShaderProgram(GLuint vs, GLuint fs, GLuint* outPgm); 79 80 ProgramType mProgramType; 81 GLuint mProgram; 82 83 GLint maPositionLoc; 84 GLint maTextureCoordLoc; 85 GLint muMVPMatrixLoc; 86 GLint muGLCMatrixLoc; 87 GLint muTextureLoc; 88}; 89 90}; // namespace android 91 92#endif /*SCREENRECORD_PROGRAM_H*/ 93