blob: edf3a4707ae814c262d2c855df76c04c6ba0caa5 [file] [log] [blame]
Romain Guyfb8b7632010-08-23 21:05:08 -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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_CACHES_H
18#define ANDROID_HWUI_CACHES_H
Romain Guyfb8b7632010-08-23 21:05:08 -070019
Romain Guya2341a92010-09-08 18:04:33 -070020#ifndef LOG_TAG
21 #define LOG_TAG "OpenGLRenderer"
22#endif
Romain Guyfb8b7632010-08-23 21:05:08 -070023
24#include <utils/Singleton.h>
25
Romain Guy79537452011-10-12 13:48:51 -070026#include <cutils/compiler.h>
27
Romain Guy746b7402010-10-26 16:27:31 -070028#include "Extensions.h"
Romain Guy03750a02010-10-18 14:06:08 -070029#include "FontRenderer.h"
30#include "GammaFontRenderer.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070031#include "TextureCache.h"
32#include "LayerCache.h"
33#include "GradientCache.h"
34#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070035#include "ProgramCache.h"
Romain Guy01d58e42011-01-19 21:54:02 -080036#include "ShapeCache.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080037#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070038#include "TextDropShadowCache.h"
Romain Guye2d345e2010-09-24 18:39:22 -070039#include "FboCache.h"
Chet Haase5c13d892010-10-08 08:37:55 -070040#include "ResourceCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070041
42namespace android {
43namespace uirenderer {
44
Romain Guy03750a02010-10-18 14:06:08 -070045///////////////////////////////////////////////////////////////////////////////
46// Globals
47///////////////////////////////////////////////////////////////////////////////
48
49#define REQUIRED_TEXTURE_UNITS_COUNT 3
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051#define REGION_MESH_QUAD_COUNT 512
52
Romain Guy03750a02010-10-18 14:06:08 -070053// Generates simple and textured vertices
54#define FV(x, y, u, v) { { x, y }, { u, v } }
55
56// This array is never used directly but used as a memcpy source in the
57// OpenGLRenderer constructor
58static const TextureVertex gMeshVertices[] = {
59 FV(0.0f, 0.0f, 0.0f, 0.0f),
60 FV(1.0f, 0.0f, 1.0f, 0.0f),
61 FV(0.0f, 1.0f, 0.0f, 1.0f),
62 FV(1.0f, 1.0f, 1.0f, 1.0f)
63};
64static const GLsizei gMeshStride = sizeof(TextureVertex);
Chet Haase5b0200b2011-04-13 17:58:08 -070065static const GLsizei gVertexStride = sizeof(Vertex);
66static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
Chet Haase99585ad2011-05-02 15:00:16 -070067static const GLsizei gAAVertexStride = sizeof(AAVertex);
Romain Guy03750a02010-10-18 14:06:08 -070068static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
Chet Haase99585ad2011-05-02 15:00:16 -070069static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
70static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
Romain Guy03750a02010-10-18 14:06:08 -070071static const GLsizei gMeshCount = 4;
72
Romain Guya1d3c912011-12-13 14:55:06 -080073static const GLenum gTextureUnits[] = {
74 GL_TEXTURE0,
75 GL_TEXTURE1,
76 GL_TEXTURE2
77};
78
Romain Guy03750a02010-10-18 14:06:08 -070079///////////////////////////////////////////////////////////////////////////////
80// Debug
81///////////////////////////////////////////////////////////////////////////////
82
Romain Guyfb8b7632010-08-23 21:05:08 -070083struct CacheLogger {
84 CacheLogger() {
Romain Guyd6b2a002011-06-17 17:45:59 -070085 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guyfb8b7632010-08-23 21:05:08 -070086 }
87}; // struct CacheLogger
88
Romain Guy03750a02010-10-18 14:06:08 -070089///////////////////////////////////////////////////////////////////////////////
90// Caches
91///////////////////////////////////////////////////////////////////////////////
92
Romain Guy79537452011-10-12 13:48:51 -070093class ANDROID_API Caches: public Singleton<Caches> {
Chet Haasedd78cca2010-10-22 18:59:26 -070094 Caches();
Romain Guyfb8b7632010-08-23 21:05:08 -070095
96 friend class Singleton<Caches>;
97
Romain Guye190aa62010-11-10 19:01:29 -080098 CacheLogger mLogger;
Romain Guy9bca4792010-10-25 18:42:25 -070099
Romain Guyfb8b7632010-08-23 21:05:08 -0700100public:
Romain Guybdf76092011-07-18 15:00:43 -0700101 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700102 kFlushMode_Layers = 0,
103 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700104 kFlushMode_Full
105 };
106
107 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108 * Initializes the cache.
109 */
110 void init();
111
112 /**
Romain Guybdf76092011-07-18 15:00:43 -0700113 * Flush the cache.
114 *
115 * @param mode Indicates how much of the cache should be flushed
116 */
117 void flush(FlushMode mode);
118
Romain Guy5b3b3522010-10-27 18:57:51 -0700119 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800120 * Destroys all resources associated with this cache. This should
121 * be called after a flush(kFlushMode_Full).
122 */
123 void terminate();
124
125 /**
Romain Guye190aa62010-11-10 19:01:29 -0800126 * Indicates whether the renderer is in debug mode.
127 * This debug mode provides limited information to app developers.
128 */
129 DebugLevel getDebugLevel() const {
130 return mDebugLevel;
131 }
132
133 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800134 * Call this on each frame to ensure that garbage is deleted from
135 * GPU memory.
136 */
137 void clearGarbage();
138
139 /**
Romain Guyada830f2011-01-13 12:13:20 -0800140 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800141 */
Romain Guyada830f2011-01-13 12:13:20 -0800142 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800143
144 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700145 * Binds the VBO used to render simple textured quads.
146 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800147 bool bindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -0700148
149 /**
150 * Binds the specified VBO if needed.
151 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800152 bool bindMeshBuffer(const GLuint buffer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700153
154 /**
155 * Unbinds the VBO used to render simple textured quads.
156 */
Romain Guyf3a910b42011-12-12 20:35:21 -0800157 bool unbindMeshBuffer();
158
Romain Guy15bc6432011-12-13 13:11:32 -0800159 bool bindIndicesBuffer(const GLuint buffer);
160 bool unbindIndicesBuffer();
161
Romain Guyf3a910b42011-12-12 20:35:21 -0800162 /**
163 * Binds an attrib to the specified float vertex pointer.
164 * Assumes a stride of gMeshStride and a size of 2.
165 */
166 void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
167 GLsizei stride = gMeshStride);
168
169 /**
170 * Binds an attrib to the specified float vertex pointer.
171 * Assumes a stride of gMeshStride and a size of 2.
172 */
173 void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);
174
175 /**
176 * Resets the vertex pointers.
177 */
178 void resetVertexPointers();
179 void resetTexCoordsVertexPointer();
Romain Guy03750a02010-10-18 14:06:08 -0700180
Romain Guy15bc6432011-12-13 13:11:32 -0800181 void enableTexCoordsVertexArray();
182 void disbaleTexCoordsVertexArray();
183
Romain Guy5b3b3522010-10-27 18:57:51 -0700184 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800185 * Activate the specified texture unit. The texture unit must
186 * be specified using an integer number (0 for GL_TEXTURE0 etc.)
187 */
188 void activeTexture(GLuint textureUnit);
189
190 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700191 * Returns the mesh used to draw regions. Calling this method will
192 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
193 * indices for the region mesh.
194 */
195 TextureVertex* getRegionMesh();
196
Romain Guyc15008e2010-11-10 11:59:15 -0800197 /**
198 * Displays the memory usage of each cache and the total sum.
199 */
200 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700201 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800202
Romain Guyfb8b7632010-08-23 21:05:08 -0700203 bool blend;
204 GLenum lastSrcMode;
205 GLenum lastDstMode;
206 Program* currentProgram;
207
Romain Guy746b7402010-10-26 16:27:31 -0700208 // VBO to draw with
Romain Guy03750a02010-10-18 14:06:08 -0700209 GLuint meshBuffer;
Romain Guy03750a02010-10-18 14:06:08 -0700210
Romain Guy746b7402010-10-26 16:27:31 -0700211 // GL extensions
212 Extensions extensions;
213
214 // Misc
215 GLint maxTextureSize;
216
Romain Guyfb8b7632010-08-23 21:05:08 -0700217 TextureCache textureCache;
218 LayerCache layerCache;
219 GradientCache gradientCache;
220 ProgramCache programCache;
221 PathCache pathCache;
Romain Guy01d58e42011-01-19 21:54:02 -0800222 RoundRectShapeCache roundRectShapeCache;
223 CircleShapeCache circleShapeCache;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800224 OvalShapeCache ovalShapeCache;
225 RectShapeCache rectShapeCache;
Romain Guy8b2f5262011-01-23 16:15:02 -0800226 ArcShapeCache arcShapeCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700227 PatchCache patchCache;
228 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700229 FboCache fboCache;
Romain Guy11fd6542010-10-04 17:10:58 -0700230 GammaFontRenderer fontRenderer;
Chet Haase5c13d892010-10-08 08:37:55 -0700231 ResourceCache resourceCache;
Romain Guy29d89972010-09-22 16:10:57 -0700232
Romain Guye190aa62010-11-10 19:01:29 -0800233private:
Romain Guyf3a910b42011-12-12 20:35:21 -0800234 GLuint mCurrentBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -0800235 GLuint mCurrentIndicesBuffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800236 void* mCurrentPositionPointer;
237 void* mCurrentTexCoordsPointer;
238
Romain Guy15bc6432011-12-13 13:11:32 -0800239 bool mTexCoordsArrayEnabled;
240
Romain Guya1d3c912011-12-13 14:55:06 -0800241 GLuint mTextureUnit;
242
Romain Guyf3a910b42011-12-12 20:35:21 -0800243 // Used to render layers
244 TextureVertex* mRegionMesh;
245 GLuint mRegionMeshIndices;
246
247 mutable Mutex mGarbageLock;
248 Vector<Layer*> mLayerGarbage;
249
Romain Guye190aa62010-11-10 19:01:29 -0800250 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800251 bool mInitialized;
Romain Guyfb8b7632010-08-23 21:05:08 -0700252}; // class Caches
253
254}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700255}; // namespace android
256
Romain Guy5b3b3522010-10-27 18:57:51 -0700257#endif // ANDROID_HWUI_CACHES_H