blob: 527bf3e46308c523e844ed279aeb6fbad0bcdbe6 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
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
Romain Guy9d5316e2010-06-24 19:30:36 -070017#ifndef ANDROID_UI_OPENGL_RENDERER_H
18#define ANDROID_UI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070022
Romain Guyf6a11b82010-06-23 17:47:49 -070023#include <SkMatrix.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070024#include <SkXfermode.h>
Romain Guye4d01122010-06-16 18:44:05 -070025
Romain Guy9d5316e2010-06-24 19:30:36 -070026#include <utils/KeyedVector.h>
Romain Guybb9524b2010-06-22 18:56:38 -070027#include <utils/RefBase.h>
28
Romain Guyf6a11b82010-06-23 17:47:49 -070029#include "Matrix.h"
Romain Guybb9524b2010-06-22 18:56:38 -070030#include "Rect.h"
31
Romain Guye4d01122010-06-16 18:44:05 -070032namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070033namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070034
Romain Guyf6a11b82010-06-23 17:47:49 -070035///////////////////////////////////////////////////////////////////////////////
36// Support
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guybb9524b2010-06-22 18:56:38 -070039class Snapshot: public LightRefBase<Snapshot> {
40public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070041 Snapshot() {
42 }
Romain Guybb9524b2010-06-22 18:56:38 -070043
Romain Guy7ae7ac42010-06-25 13:46:18 -070044 Snapshot(const sp<Snapshot> s):
45 transform(s->transform),
46 clipRect(s->clipRect),
47 flags(kFlagDirtyTransform),
Romain Guybd6b79b2010-06-26 00:13:53 -070048 previous(s),
49 layer(0.0f, 0.0f, 0.0f, 0.0f),
50 texture(0),
51 fbo(0),
52 alpha(255) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070053 }
Romain Guybb9524b2010-06-22 18:56:38 -070054
Romain Guy7ae7ac42010-06-25 13:46:18 -070055 enum Flags {
56 kFlagClipSet = 0x1,
57 kFlagDirtyTransform = 0x2,
Romain Guybd6b79b2010-06-26 00:13:53 -070058 kFlagIsLayer = 0x4,
Romain Guy7ae7ac42010-06-25 13:46:18 -070059 };
Romain Guybb9524b2010-06-22 18:56:38 -070060
Romain Guy7ae7ac42010-06-25 13:46:18 -070061 const Rect& getMappedClip();
Romain Guy9d5316e2010-06-24 19:30:36 -070062
Romain Guy7ae7ac42010-06-25 13:46:18 -070063 // Local transformations
64 mat4 transform;
Romain Guyf6a11b82010-06-23 17:47:49 -070065
Romain Guy7ae7ac42010-06-25 13:46:18 -070066 // Clipping rectangle at the time of this snapshot
67 Rect clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 // Dirty flags
70 int flags;
Romain Guybb9524b2010-06-22 18:56:38 -070071
Romain Guy7ae7ac42010-06-25 13:46:18 -070072 // Previous snapshot in the frames stack
73 sp<Snapshot> previous;
Romain Guy9d5316e2010-06-24 19:30:36 -070074
Romain Guybd6b79b2010-06-26 00:13:53 -070075 // Layer, only set if kFlagIsLayer is set
76 Rect layer;
77 GLuint texture;
78 GLuint fbo;
79 float alpha;
80
Romain Guy9d5316e2010-06-24 19:30:36 -070081private:
Romain Guy7ae7ac42010-06-25 13:46:18 -070082 // Clipping rectangle mapped with the transform
83 Rect mappedClip;
Romain Guy9d5316e2010-06-24 19:30:36 -070084}; // class Snapshot
85
Romain Guyc7d53492010-06-25 13:41:57 -070086struct SimpleVertex {
Romain Guy7ae7ac42010-06-25 13:46:18 -070087 float position[2];
Romain Guyc7d53492010-06-25 13:41:57 -070088}; // struct SimpleVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070089
Romain Guybd6b79b2010-06-26 00:13:53 -070090struct TextureVertex {
91 float position[2];
92 float texture[2];
93}; // struct TextureVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070094
95class Program: public LightRefBase<Program> {
96public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070097 Program(const char* vertex, const char* fragment);
98 ~Program();
Romain Guy9d5316e2010-06-24 19:30:36 -070099
Romain Guy7ae7ac42010-06-25 13:46:18 -0700100 void use();
Romain Guy9d5316e2010-06-24 19:30:36 -0700101
102protected:
Romain Guy7ae7ac42010-06-25 13:46:18 -0700103 int addAttrib(const char* name);
104 int getAttrib(const char* name);
Romain Guy9d5316e2010-06-24 19:30:36 -0700105
Romain Guy7ae7ac42010-06-25 13:46:18 -0700106 int addUniform(const char* name);
107 int getUniform(const char* name);
Romain Guy9d5316e2010-06-24 19:30:36 -0700108
109private:
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 GLuint buildShader(const char* source, GLenum type);
Romain Guy9d5316e2010-06-24 19:30:36 -0700111
Romain Guy7ae7ac42010-06-25 13:46:18 -0700112 // Handle of the OpenGL program
113 GLuint id;
Romain Guy9d5316e2010-06-24 19:30:36 -0700114
Romain Guy7ae7ac42010-06-25 13:46:18 -0700115 // Handles of the shaders
116 GLuint vertexShader;
117 GLuint fragmentShader;
Romain Guy9d5316e2010-06-24 19:30:36 -0700118
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 // Keeps track of attributes and uniforms slots
120 KeyedVector<const char*, int> attributes;
121 KeyedVector<const char*, int> uniforms;
Romain Guy9d5316e2010-06-24 19:30:36 -0700122}; // class Program
123
124class DrawColorProgram: public Program {
125public:
Romain Guy7ae7ac42010-06-25 13:46:18 -0700126 DrawColorProgram();
Romain Guybd6b79b2010-06-26 00:13:53 -0700127 DrawColorProgram(const char* vertex, const char* fragment);
Romain Guy9d5316e2010-06-24 19:30:36 -0700128
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 void use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix,
130 const GLfloat* transformMatrix);
Romain Guyc7d53492010-06-25 13:41:57 -0700131
Romain Guy7ae7ac42010-06-25 13:46:18 -0700132 int position;
133 int color;
Romain Guy9d5316e2010-06-24 19:30:36 -0700134
Romain Guy7ae7ac42010-06-25 13:46:18 -0700135 int projection;
136 int modelView;
137 int transform;
Romain Guybd6b79b2010-06-26 00:13:53 -0700138
139protected:
140 void getAttribsAndUniforms();
141};
142
143class DrawTextureProgram: public DrawColorProgram {
144public:
145 DrawTextureProgram();
146
147 int sampler;
148 int texCoords;
Romain Guy9d5316e2010-06-24 19:30:36 -0700149};
Romain Guybb9524b2010-06-22 18:56:38 -0700150
Romain Guyf6a11b82010-06-23 17:47:49 -0700151///////////////////////////////////////////////////////////////////////////////
152// Renderer
153///////////////////////////////////////////////////////////////////////////////
154
Romain Guy85bf02f2010-06-22 13:11:24 -0700155class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -0700156public:
Romain Guy85bf02f2010-06-22 13:11:24 -0700157 OpenGLRenderer();
158 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -0700159
160 void setViewport(int width, int height);
161 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -0700162
Romain Guybb9524b2010-06-22 18:56:38 -0700163 int getSaveCount() const;
164 int save(int flags);
165 void restore();
166 void restoreToCount(int saveCount);
167
Romain Guybd6b79b2010-06-26 00:13:53 -0700168 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
169 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
170
Romain Guyf6a11b82010-06-23 17:47:49 -0700171 void translate(float dx, float dy);
172 void rotate(float degrees);
173 void scale(float sx, float sy);
174
175 void setMatrix(SkMatrix* matrix);
176 void getMatrix(SkMatrix* matrix);
177 void concatMatrix(SkMatrix* matrix);
178
Romain Guy9d5316e2010-06-24 19:30:36 -0700179 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -0700180 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -0700181 bool clipRect(float left, float top, float right, float bottom);
182
Romain Guy85bf02f2010-06-22 13:11:24 -0700183 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700184 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700185
Romain Guy85bf02f2010-06-22 13:11:24 -0700186private:
Romain Guybb9524b2010-06-22 18:56:38 -0700187 int saveSnapshot();
188 bool restoreSnapshot();
189
190 void setScissorFromClip();
191
Romain Guyc7d53492010-06-25 13:41:57 -0700192 void drawColorRect(float left, float top, float right, float bottom, int color);
Romain Guybd6b79b2010-06-26 00:13:53 -0700193 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
194 float alpha);
Romain Guyc7d53492010-06-25 13:41:57 -0700195
Romain Guybb9524b2010-06-22 18:56:38 -0700196 // Dimensions of the drawing surface
197 int mWidth, mHeight;
198
Romain Guy85bf02f2010-06-22 13:11:24 -0700199 // Matrix used for ortho projection in shaders
200 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700201
Romain Guyc7d53492010-06-25 13:41:57 -0700202 // Model-view matrix used to position/size objects
203 mat4 mModelView;
204
Romain Guybb9524b2010-06-22 18:56:38 -0700205 // Number of saved states
206 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700207 // Base state
208 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700209 // Current state
210 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700211
212 // Shaders
213 sp<DrawColorProgram> mDrawColorShader;
Romain Guybd6b79b2010-06-26 00:13:53 -0700214 sp<DrawTextureProgram> mDrawTextureShader;
Romain Guybb9524b2010-06-22 18:56:38 -0700215}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700216
Romain Guy9d5316e2010-06-24 19:30:36 -0700217}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700218}; // namespace android
219
Romain Guy9d5316e2010-06-24 19:30:36 -0700220#endif // ANDROID_UI_OPENGL_RENDERER_H