Program.h revision 587c6fefcd3c1d05c608ff511cf3534bc765256e
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 }.  Inverts the
55    // content if "invert" is set.
56    status_t blit(GLuint texName, const float* texMatrix,
57            int32_t x, int32_t y, int32_t w, int32_t h,
58            bool invert = false) const;
59
60    // Draw a number of triangles.
61    status_t drawTriangles(GLuint texName, const float* texMatrix,
62            const float* vertices, const float* texes, size_t count) const;
63
64    static const float kIdentity[];
65
66private:
67    Program(const Program&);
68    Program& operator=(const Program&);
69
70    // Common code for draw functions.
71    status_t beforeDraw(GLuint texName, const float* texMatrix,
72            const float* vertices, const float* texes, bool invert) const;
73    status_t afterDraw() const;
74
75    // GLES 2 shader utilities.
76    status_t createProgram(GLuint* outPgm, const char* vertexShader,
77            const char* fragmentShader);
78    static status_t compileShader(GLenum shaderType, const char* src,
79            GLuint* outShader);
80    static status_t linkShaderProgram(GLuint vs, GLuint fs, GLuint* outPgm);
81
82    ProgramType mProgramType;
83    GLuint mProgram;
84
85    GLint maPositionLoc;
86    GLint maTextureCoordLoc;
87    GLint muMVPMatrixLoc;
88    GLint muGLCMatrixLoc;
89    GLint muTextureLoc;
90};
91
92}; // namespace android
93
94#endif /*SCREENRECORD_PROGRAM_H*/
95