OpenGLRenderer.h revision 7ae7ac48aa2b53453c9805075171ecd5bcafd7de
1/*
2 * Copyright (C) 2010 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 ANDROID_UI_OPENGL_RENDERER_H
18#define ANDROID_UI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <SkMatrix.h>
24#include <SkXfermode.h>
25
26#include <utils/KeyedVector.h>
27#include <utils/RefBase.h>
28
29#include "Matrix.h"
30#include "Rect.h"
31
32namespace android {
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Support
37///////////////////////////////////////////////////////////////////////////////
38
39class Snapshot: public LightRefBase<Snapshot> {
40public:
41    Snapshot() {
42    }
43
44    Snapshot(const sp<Snapshot> s):
45            transform(s->transform),
46            clipRect(s->clipRect),
47            flags(kFlagDirtyTransform),
48            previous(s) {
49    }
50
51    enum Flags {
52        kFlagClipSet = 0x1,
53        kFlagDirtyTransform = 0x2,
54    };
55
56    const Rect& getMappedClip();
57
58    // Local transformations
59    mat4 transform;
60
61    // Clipping rectangle at the time of this snapshot
62    Rect clipRect;
63
64    // Dirty flags
65    int flags;
66
67    // Previous snapshot in the frames stack
68    sp<Snapshot> previous;
69
70private:
71    // Clipping rectangle mapped with the transform
72    Rect mappedClip;
73}; // class Snapshot
74
75struct SimpleVertex {
76    float position[2];
77}; // struct SimpleVertex
78
79typedef char* shader;
80
81class Program: public LightRefBase<Program> {
82public:
83    Program(const char* vertex, const char* fragment);
84    ~Program();
85
86    void use();
87
88protected:
89    int addAttrib(const char* name);
90    int getAttrib(const char* name);
91
92    int addUniform(const char* name);
93    int getUniform(const char* name);
94
95private:
96    GLuint buildShader(const char* source, GLenum type);
97
98    // Handle of the OpenGL program
99    GLuint id;
100
101    // Handles of the shaders
102    GLuint vertexShader;
103    GLuint fragmentShader;
104
105    // Keeps track of attributes and uniforms slots
106    KeyedVector<const char*, int> attributes;
107    KeyedVector<const char*, int> uniforms;
108}; // class Program
109
110class DrawColorProgram: public Program {
111public:
112    DrawColorProgram();
113
114    void use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix,
115             const GLfloat* transformMatrix);
116
117    int position;
118    int color;
119
120    int projection;
121    int modelView;
122    int transform;
123};
124
125///////////////////////////////////////////////////////////////////////////////
126// Renderer
127///////////////////////////////////////////////////////////////////////////////
128
129class OpenGLRenderer {
130public:
131    OpenGLRenderer();
132    ~OpenGLRenderer();
133
134    void setViewport(int width, int height);
135    void prepare();
136
137    int getSaveCount() const;
138    int save(int flags);
139    void restore();
140    void restoreToCount(int saveCount);
141
142    void translate(float dx, float dy);
143    void rotate(float degrees);
144    void scale(float sx, float sy);
145
146    void setMatrix(SkMatrix* matrix);
147    void getMatrix(SkMatrix* matrix);
148    void concatMatrix(SkMatrix* matrix);
149
150    const Rect& getClipBounds();
151    bool quickReject(float left, float top, float right, float bottom);
152    bool clipRect(float left, float top, float right, float bottom);
153
154    void drawColor(int color, SkXfermode::Mode mode);
155    void drawRect(float left, float top, float right, float bottom, SkPaint* paint);
156
157private:
158    int saveSnapshot();
159    bool restoreSnapshot();
160
161    void setScissorFromClip();
162
163    void drawColorRect(float left, float top, float right, float bottom, int color);
164
165    // Dimensions of the drawing surface
166    int mWidth, mHeight;
167
168    // Matrix used for ortho projection in shaders
169    float mOrthoMatrix[16];
170
171    // Model-view matrix used to position/size objects
172    mat4 mModelView;
173
174    // Number of saved states
175    int mSaveCount;
176    // Base state
177    Snapshot mFirstSnapshot;
178    // Current state
179    sp<Snapshot> mSnapshot;
180
181    // Shaders
182    sp<DrawColorProgram> mDrawColorShader;
183}; // class OpenGLRenderer
184
185}; // namespace uirenderer
186}; // namespace android
187
188#endif // ANDROID_UI_OPENGL_RENDERER_H
189