blob: 88cbc1c78377afba36c7b4eb85481d35a17cfcdb [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
75struct Vertex {
76 float position[2];
77 float color[4];
78}; // struct Vertex
79
80typedef char* shader;
81
82class Program: public LightRefBase<Program> {
83public:
84 Program(const char* vertex, const char* fragment);
85 ~Program();
86
87 void use();
88
89protected:
90 int addAttrib(const char* name);
91 int getAttrib(const char* name);
92
93 int addUniform(const char* name);
94 int getUniform(const char* name);
95
96private:
97 GLuint buildShader(const char* source, GLenum type);
98
99 // Handle of the OpenGL program
100 GLuint id;
101
102 // Handles of the shaders
103 GLuint vertexShader;
104 GLuint fragmentShader;
105
106 // Keeps track of attributes and uniforms slots
107 KeyedVector<const char*, int> attributes;
108 KeyedVector<const char*, int> uniforms;
109}; // class Program
110
111class DrawColorProgram: public Program {
112public:
113 DrawColorProgram();
114
115 int position;
116 int color;
117
118 int projection;
119 int modelView;
120};
Romain Guybb9524b2010-06-22 18:56:38 -0700121
Romain Guyf6a11b82010-06-23 17:47:49 -0700122///////////////////////////////////////////////////////////////////////////////
123// Renderer
124///////////////////////////////////////////////////////////////////////////////
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -0700127public:
Romain Guy85bf02f2010-06-22 13:11:24 -0700128 OpenGLRenderer();
129 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -0700130
131 void setViewport(int width, int height);
132 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -0700133
Romain Guybb9524b2010-06-22 18:56:38 -0700134 int getSaveCount() const;
135 int save(int flags);
136 void restore();
137 void restoreToCount(int saveCount);
138
Romain Guyf6a11b82010-06-23 17:47:49 -0700139 void translate(float dx, float dy);
140 void rotate(float degrees);
141 void scale(float sx, float sy);
142
143 void setMatrix(SkMatrix* matrix);
144 void getMatrix(SkMatrix* matrix);
145 void concatMatrix(SkMatrix* matrix);
146
Romain Guy9d5316e2010-06-24 19:30:36 -0700147 const Rect& getClipBounds();
Romain Guybb9524b2010-06-22 18:56:38 -0700148 bool clipRect(float left, float top, float right, float bottom);
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150 void drawColor(int color, SkXfermode::Mode mode);
Romain Guy08ae3172010-06-21 19:35:50 -0700151
Romain Guy85bf02f2010-06-22 13:11:24 -0700152private:
Romain Guybb9524b2010-06-22 18:56:38 -0700153 int saveSnapshot();
154 bool restoreSnapshot();
155
156 void setScissorFromClip();
157
158 // Dimensions of the drawing surface
159 int mWidth, mHeight;
160
Romain Guy85bf02f2010-06-22 13:11:24 -0700161 // Matrix used for ortho projection in shaders
162 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700163
164 // Number of saved states
165 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700166 // Base state
167 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700168 // Current state
169 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700170
171 // Shaders
172 sp<DrawColorProgram> mDrawColorShader;
Romain Guybb9524b2010-06-22 18:56:38 -0700173}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700174
Romain Guy9d5316e2010-06-24 19:30:36 -0700175}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700176}; // namespace android
177
Romain Guy9d5316e2010-06-24 19:30:36 -0700178#endif // ANDROID_UI_OPENGL_RENDERER_H