blob: bef41938e38d34ece0ff5834c44c2805b65924a7 [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 Guyf6a11b82010-06-23 17:47:49 -070041 Snapshot() {
42 }
Romain Guybb9524b2010-06-22 18:56:38 -070043
Romain Guy9d5316e2010-06-24 19:30:36 -070044 Snapshot(const sp<Snapshot> s):
45 transform(s->transform),
46 clipRect(s->clipRect),
47 flags(kFlagDirtyTransform),
48 previous(s) {
Romain Guyf6a11b82010-06-23 17:47:49 -070049 }
Romain Guybb9524b2010-06-22 18:56:38 -070050
51 enum Flags {
52 kFlagClipSet = 0x1,
Romain Guy9d5316e2010-06-24 19:30:36 -070053 kFlagDirtyTransform = 0x2,
Romain Guybb9524b2010-06-22 18:56:38 -070054 };
55
Romain Guy9d5316e2010-06-24 19:30:36 -070056 const Rect& getMappedClip();
57
Romain Guyf6a11b82010-06-23 17:47:49 -070058 // Local transformations
59 mat4 transform;
60
Romain Guybb9524b2010-06-22 18:56:38 -070061 // 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;
Romain Guy9d5316e2010-06-24 19:30:36 -070069
70private:
71 // Clipping rectangle mapped with the transform
72 Rect mappedClip;
73}; // class Snapshot
74
Romain Guyc7d53492010-06-25 13:41:57 -070075struct SimpleVertex {
Romain Guy9d5316e2010-06-24 19:30:36 -070076 float position[2];
Romain Guyc7d53492010-06-25 13:41:57 -070077}; // struct SimpleVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070078
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
Romain Guyc7d53492010-06-25 13:41:57 -0700114 void use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix,
115 const GLfloat* transformMatrix);
116
Romain Guy9d5316e2010-06-24 19:30:36 -0700117 int position;
118 int color;
119
120 int projection;
121 int modelView;
Romain Guyc7d53492010-06-25 13:41:57 -0700122 int transform;
Romain Guy9d5316e2010-06-24 19:30:36 -0700123};
Romain Guybb9524b2010-06-22 18:56:38 -0700124
Romain Guyf6a11b82010-06-23 17:47:49 -0700125///////////////////////////////////////////////////////////////////////////////
126// Renderer
127///////////////////////////////////////////////////////////////////////////////
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -0700130public:
Romain Guy85bf02f2010-06-22 13:11:24 -0700131 OpenGLRenderer();
132 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -0700133
134 void setViewport(int width, int height);
135 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -0700136
Romain Guybb9524b2010-06-22 18:56:38 -0700137 int getSaveCount() const;
138 int save(int flags);
139 void restore();
140 void restoreToCount(int saveCount);
141
Romain Guyf6a11b82010-06-23 17:47:49 -0700142 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
Romain Guy9d5316e2010-06-24 19:30:36 -0700150 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -0700151 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -0700152 bool clipRect(float left, float top, float right, float bottom);
153
Romain Guy85bf02f2010-06-22 13:11:24 -0700154 void drawColor(int color, SkXfermode::Mode mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700155 void drawRect(float left, float top, float right, float bottom, SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700156
Romain Guy85bf02f2010-06-22 13:11:24 -0700157private:
Romain Guybb9524b2010-06-22 18:56:38 -0700158 int saveSnapshot();
159 bool restoreSnapshot();
160
161 void setScissorFromClip();
162
Romain Guyc7d53492010-06-25 13:41:57 -0700163 void drawColorRect(float left, float top, float right, float bottom, int color);
164
Romain Guybb9524b2010-06-22 18:56:38 -0700165 // Dimensions of the drawing surface
166 int mWidth, mHeight;
167
Romain Guy85bf02f2010-06-22 13:11:24 -0700168 // Matrix used for ortho projection in shaders
169 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700170
Romain Guyc7d53492010-06-25 13:41:57 -0700171 // Model-view matrix used to position/size objects
172 mat4 mModelView;
173
Romain Guybb9524b2010-06-22 18:56:38 -0700174 // Number of saved states
175 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700176 // Base state
177 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700178 // Current state
179 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700180
181 // Shaders
182 sp<DrawColorProgram> mDrawColorShader;
Romain Guybb9524b2010-06-22 18:56:38 -0700183}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700184
Romain Guy9d5316e2010-06-24 19:30:36 -0700185}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700186}; // namespace android
187
Romain Guy9d5316e2010-06-24 19:30:36 -0700188#endif // ANDROID_UI_OPENGL_RENDERER_H