Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_CACHES_H |
| 18 | #define ANDROID_HWUI_CACHES_H |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 19 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 20 | #ifndef LOG_TAG |
| 21 | #define LOG_TAG "OpenGLRenderer" |
| 22 | #endif |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 23 | |
| 24 | #include <utils/Singleton.h> |
| 25 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 26 | #include <cutils/compiler.h> |
| 27 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 28 | #include "Extensions.h" |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 29 | #include "FontRenderer.h" |
| 30 | #include "GammaFontRenderer.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 31 | #include "TextureCache.h" |
| 32 | #include "LayerCache.h" |
| 33 | #include "GradientCache.h" |
| 34 | #include "PatchCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 35 | #include "ProgramCache.h" |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 36 | #include "ShapeCache.h" |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 37 | #include "PathCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 38 | #include "TextDropShadowCache.h" |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 39 | #include "FboCache.h" |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 40 | #include "ResourceCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace uirenderer { |
| 44 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Globals |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | #define REQUIRED_TEXTURE_UNITS_COUNT 3 |
| 50 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 51 | #define REGION_MESH_QUAD_COUNT 512 |
| 52 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 53 | // 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 |
| 58 | static 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 | }; |
| 64 | static const GLsizei gMeshStride = sizeof(TextureVertex); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 65 | static const GLsizei gVertexStride = sizeof(Vertex); |
| 66 | static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 67 | static const GLsizei gAAVertexStride = sizeof(AAVertex); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 68 | static const GLsizei gMeshTextureOffset = 2 * sizeof(float); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 69 | static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); |
| 70 | static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 71 | static const GLsizei gMeshCount = 4; |
| 72 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 73 | static const GLenum gTextureUnits[] = { |
| 74 | GL_TEXTURE0, |
| 75 | GL_TEXTURE1, |
| 76 | GL_TEXTURE2 |
| 77 | }; |
| 78 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | // Debug |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 83 | struct CacheLogger { |
| 84 | CacheLogger() { |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 85 | INIT_LOGD("Creating OpenGL renderer caches"); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 86 | } |
| 87 | }; // struct CacheLogger |
| 88 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | // Caches |
| 91 | /////////////////////////////////////////////////////////////////////////////// |
| 92 | |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 93 | class DisplayList; |
| 94 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 95 | class ANDROID_API Caches: public Singleton<Caches> { |
Chet Haase | dd78cca | 2010-10-22 18:59:26 -0700 | [diff] [blame] | 96 | Caches(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 97 | |
| 98 | friend class Singleton<Caches>; |
| 99 | |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 100 | CacheLogger mLogger; |
Romain Guy | 9bca479 | 2010-10-25 18:42:25 -0700 | [diff] [blame] | 101 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 102 | public: |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 103 | enum FlushMode { |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 104 | kFlushMode_Layers = 0, |
| 105 | kFlushMode_Moderate, |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 106 | kFlushMode_Full |
| 107 | }; |
| 108 | |
| 109 | /** |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 110 | * Initializes the cache. |
| 111 | */ |
| 112 | void init(); |
| 113 | |
| 114 | /** |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 115 | * Flush the cache. |
| 116 | * |
| 117 | * @param mode Indicates how much of the cache should be flushed |
| 118 | */ |
| 119 | void flush(FlushMode mode); |
| 120 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 121 | /** |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 122 | * Destroys all resources associated with this cache. This should |
| 123 | * be called after a flush(kFlushMode_Full). |
| 124 | */ |
| 125 | void terminate(); |
| 126 | |
| 127 | /** |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 128 | * Indicates whether the renderer is in debug mode. |
| 129 | * This debug mode provides limited information to app developers. |
| 130 | */ |
| 131 | DebugLevel getDebugLevel() const { |
| 132 | return mDebugLevel; |
| 133 | } |
| 134 | |
| 135 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 136 | * Call this on each frame to ensure that garbage is deleted from |
| 137 | * GPU memory. |
| 138 | */ |
| 139 | void clearGarbage(); |
| 140 | |
| 141 | /** |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 142 | * Can be used to delete a layer from a non EGL thread. |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 143 | */ |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 144 | void deleteLayerDeferred(Layer* layer); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 145 | |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 146 | /* |
| 147 | * Can be used to delete a display list from a non EGL thread. |
| 148 | */ |
| 149 | void deleteDisplayListDeferred(DisplayList* layer); |
| 150 | |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 151 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 152 | * Binds the VBO used to render simple textured quads. |
| 153 | */ |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 154 | bool bindMeshBuffer(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Binds the specified VBO if needed. |
| 158 | */ |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 159 | bool bindMeshBuffer(const GLuint buffer); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Unbinds the VBO used to render simple textured quads. |
| 163 | */ |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 164 | bool unbindMeshBuffer(); |
| 165 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 166 | bool bindIndicesBuffer(const GLuint buffer); |
| 167 | bool unbindIndicesBuffer(); |
| 168 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 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 bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, |
| 174 | GLsizei stride = gMeshStride); |
| 175 | |
| 176 | /** |
| 177 | * Binds an attrib to the specified float vertex pointer. |
| 178 | * Assumes a stride of gMeshStride and a size of 2. |
| 179 | */ |
| 180 | void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices); |
| 181 | |
| 182 | /** |
| 183 | * Resets the vertex pointers. |
| 184 | */ |
| 185 | void resetVertexPointers(); |
| 186 | void resetTexCoordsVertexPointer(); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 187 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 188 | void enableTexCoordsVertexArray(); |
| 189 | void disbaleTexCoordsVertexArray(); |
| 190 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 191 | /** |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 192 | * Activate the specified texture unit. The texture unit must |
| 193 | * be specified using an integer number (0 for GL_TEXTURE0 etc.) |
| 194 | */ |
| 195 | void activeTexture(GLuint textureUnit); |
| 196 | |
| 197 | /** |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 198 | * Sets the scissor for the current surface. |
| 199 | */ |
| 200 | void setScissor(GLint x, GLint y, GLint width, GLint height); |
| 201 | |
| 202 | /** |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 203 | * Resets the scissor state. |
| 204 | */ |
| 205 | void resetScissor(); |
| 206 | |
| 207 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 208 | * Returns the mesh used to draw regions. Calling this method will |
| 209 | * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the |
| 210 | * indices for the region mesh. |
| 211 | */ |
| 212 | TextureVertex* getRegionMesh(); |
| 213 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Displays the memory usage of each cache and the total sum. |
| 216 | */ |
| 217 | void dumpMemoryUsage(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 218 | void dumpMemoryUsage(String8& log); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 219 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 220 | bool blend; |
| 221 | GLenum lastSrcMode; |
| 222 | GLenum lastDstMode; |
| 223 | Program* currentProgram; |
| 224 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 225 | // VBO to draw with |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 226 | GLuint meshBuffer; |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 227 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 228 | // GL extensions |
| 229 | Extensions extensions; |
| 230 | |
| 231 | // Misc |
| 232 | GLint maxTextureSize; |
| 233 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 234 | TextureCache textureCache; |
| 235 | LayerCache layerCache; |
| 236 | GradientCache gradientCache; |
| 237 | ProgramCache programCache; |
| 238 | PathCache pathCache; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 239 | RoundRectShapeCache roundRectShapeCache; |
| 240 | CircleShapeCache circleShapeCache; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 241 | OvalShapeCache ovalShapeCache; |
| 242 | RectShapeCache rectShapeCache; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 243 | ArcShapeCache arcShapeCache; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 244 | PatchCache patchCache; |
| 245 | TextDropShadowCache dropShadowCache; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 246 | FboCache fboCache; |
Romain Guy | 11fd654 | 2010-10-04 17:10:58 -0700 | [diff] [blame] | 247 | GammaFontRenderer fontRenderer; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 248 | ResourceCache resourceCache; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 249 | |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 250 | PFNGLINSERTEVENTMARKEREXTPROC eventMark; |
| 251 | PFNGLPUSHGROUPMARKEREXTPROC startMark; |
| 252 | PFNGLPOPGROUPMARKEREXTPROC endMark; |
| 253 | |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 254 | private: |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 255 | static void eventMarkNull(GLsizei length, const GLchar *marker) { } |
| 256 | static void startMarkNull(GLsizei length, const GLchar *marker) { } |
| 257 | static void endMarkNull() { } |
| 258 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 259 | GLuint mCurrentBuffer; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 260 | GLuint mCurrentIndicesBuffer; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 261 | void* mCurrentPositionPointer; |
| 262 | void* mCurrentTexCoordsPointer; |
| 263 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 264 | bool mTexCoordsArrayEnabled; |
| 265 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 266 | GLuint mTextureUnit; |
| 267 | |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 268 | GLint mScissorX; |
| 269 | GLint mScissorY; |
| 270 | GLint mScissorWidth; |
| 271 | GLint mScissorHeight; |
| 272 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 273 | // Used to render layers |
| 274 | TextureVertex* mRegionMesh; |
| 275 | GLuint mRegionMeshIndices; |
| 276 | |
| 277 | mutable Mutex mGarbageLock; |
| 278 | Vector<Layer*> mLayerGarbage; |
Romain Guy | bb0acdf | 2012-03-05 13:44:35 -0800 | [diff] [blame] | 279 | Vector<DisplayList*> mDisplayListGarbage; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 280 | |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 281 | DebugLevel mDebugLevel; |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 282 | bool mInitialized; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 283 | }; // class Caches |
| 284 | |
| 285 | }; // namespace uirenderer |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 286 | }; // namespace android |
| 287 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 288 | #endif // ANDROID_HWUI_CACHES_H |