Caches.h revision 0f6675332c04c74909425d1d328f02b32c0ff40e
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 17#ifndef ANDROID_HWUI_CACHES_H 18#define ANDROID_HWUI_CACHES_H 19 20#ifndef LOG_TAG 21 #define LOG_TAG "OpenGLRenderer" 22#endif 23 24#include <utils/Singleton.h> 25 26#include <cutils/compiler.h> 27 28#include "FontRenderer.h" 29#include "GammaFontRenderer.h" 30#include "TextureCache.h" 31#include "LayerCache.h" 32#include "RenderBufferCache.h" 33#include "GradientCache.h" 34#include "PatchCache.h" 35#include "ProgramCache.h" 36#include "ShapeCache.h" 37#include "PathCache.h" 38#include "TextDropShadowCache.h" 39#include "FboCache.h" 40#include "ResourceCache.h" 41#include "Stencil.h" 42#include "Dither.h" 43 44namespace android { 45namespace uirenderer { 46 47/////////////////////////////////////////////////////////////////////////////// 48// Globals 49/////////////////////////////////////////////////////////////////////////////// 50 51#define REQUIRED_TEXTURE_UNITS_COUNT 3 52 53#define REGION_MESH_QUAD_COUNT 512 54 55// Generates simple and textured vertices 56#define FV(x, y, u, v) { { x, y }, { u, v } } 57 58// This array is never used directly but used as a memcpy source in the 59// OpenGLRenderer constructor 60static const TextureVertex gMeshVertices[] = { 61 FV(0.0f, 0.0f, 0.0f, 0.0f), 62 FV(1.0f, 0.0f, 1.0f, 0.0f), 63 FV(0.0f, 1.0f, 0.0f, 1.0f), 64 FV(1.0f, 1.0f, 1.0f, 1.0f) 65}; 66static const GLsizei gMeshStride = sizeof(TextureVertex); 67static const GLsizei gVertexStride = sizeof(Vertex); 68static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex); 69static const GLsizei gMeshTextureOffset = 2 * sizeof(float); 70static const GLsizei gVertexAlphaOffset = 2 * sizeof(float); 71static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float); 72static const GLsizei gVertexAALengthOffset = 3 * sizeof(float); 73static const GLsizei gMeshCount = 4; 74 75static const GLenum gTextureUnits[] = { 76 GL_TEXTURE0, 77 GL_TEXTURE1, 78 GL_TEXTURE2 79}; 80 81/////////////////////////////////////////////////////////////////////////////// 82// Debug 83/////////////////////////////////////////////////////////////////////////////// 84 85struct CacheLogger { 86 CacheLogger() { 87 INIT_LOGD("Creating OpenGL renderer caches"); 88 } 89}; // struct CacheLogger 90 91/////////////////////////////////////////////////////////////////////////////// 92// Caches 93/////////////////////////////////////////////////////////////////////////////// 94 95class DisplayList; 96 97class ANDROID_API Caches: public Singleton<Caches> { 98 Caches(); 99 100 friend class Singleton<Caches>; 101 102 CacheLogger mLogger; 103 104public: 105 enum FlushMode { 106 kFlushMode_Layers = 0, 107 kFlushMode_Moderate, 108 kFlushMode_Full 109 }; 110 111 /** 112 * Initialize caches. 113 */ 114 void init(); 115 116 /** 117 * Initialize global system properties. 118 */ 119 bool initProperties(); 120 121 /** 122 * Flush the cache. 123 * 124 * @param mode Indicates how much of the cache should be flushed 125 */ 126 void flush(FlushMode mode); 127 128 /** 129 * Destroys all resources associated with this cache. This should 130 * be called after a flush(kFlushMode_Full). 131 */ 132 void terminate(); 133 134 /** 135 * Indicates whether the renderer is in debug mode. 136 * This debug mode provides limited information to app developers. 137 */ 138 DebugLevel getDebugLevel() const { 139 return mDebugLevel; 140 } 141 142 /** 143 * Call this on each frame to ensure that garbage is deleted from 144 * GPU memory. 145 */ 146 void clearGarbage(); 147 148 /** 149 * Can be used to delete a layer from a non EGL thread. 150 */ 151 void deleteLayerDeferred(Layer* layer); 152 153 /* 154 * Can be used to delete a display list from a non EGL thread. 155 */ 156 void deleteDisplayListDeferred(DisplayList* layer); 157 158 /** 159 * Binds the VBO used to render simple textured quads. 160 */ 161 bool bindMeshBuffer(); 162 163 /** 164 * Binds the specified VBO if needed. 165 */ 166 bool bindMeshBuffer(const GLuint buffer); 167 168 /** 169 * Unbinds the VBO used to render simple textured quads. 170 */ 171 bool unbindMeshBuffer(); 172 173 bool bindIndicesBuffer(const GLuint buffer); 174 bool unbindIndicesBuffer(); 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 bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride); 181 182 /** 183 * Binds an attrib to the specified float vertex pointer. 184 * Assumes a stride of gMeshStride and a size of 2. 185 */ 186 void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride); 187 188 /** 189 * Resets the vertex pointers. 190 */ 191 void resetVertexPointers(); 192 void resetTexCoordsVertexPointer(); 193 194 void enableTexCoordsVertexArray(); 195 void disableTexCoordsVertexArray(); 196 197 /** 198 * Activate the specified texture unit. The texture unit must 199 * be specified using an integer number (0 for GL_TEXTURE0 etc.) 200 */ 201 void activeTexture(GLuint textureUnit); 202 203 /** 204 * Sets the scissor for the current surface. 205 */ 206 bool setScissor(GLint x, GLint y, GLint width, GLint height); 207 208 /** 209 * Resets the scissor state. 210 */ 211 void resetScissor(); 212 213 bool enableScissor(); 214 bool disableScissor(); 215 void setScissorEnabled(bool enabled); 216 217 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard); 218 void endTiling(); 219 220 /** 221 * Returns the mesh used to draw regions. Calling this method will 222 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the 223 * indices for the region mesh. 224 */ 225 TextureVertex* getRegionMesh(); 226 227 /** 228 * Displays the memory usage of each cache and the total sum. 229 */ 230 void dumpMemoryUsage(); 231 void dumpMemoryUsage(String8& log); 232 233 bool hasRegisteredFunctors(); 234 void registerFunctors(uint32_t functorCount); 235 void unregisterFunctors(uint32_t functorCount); 236 237 bool blend; 238 GLenum lastSrcMode; 239 GLenum lastDstMode; 240 Program* currentProgram; 241 bool scissorEnabled; 242 243 bool drawDeferDisabled; 244 bool drawReorderDisabled; 245 246 // VBO to draw with 247 GLuint meshBuffer; 248 249 // Misc 250 GLint maxTextureSize; 251 252 // Debugging 253 bool debugLayersUpdates; 254 bool debugOverdraw; 255 256 enum StencilClipDebug { 257 kStencilHide, 258 kStencilShowHighlight, 259 kStencilShowRegion 260 }; 261 StencilClipDebug debugStencilClip; 262 263 TextureCache textureCache; 264 LayerCache layerCache; 265 RenderBufferCache renderBufferCache; 266 GradientCache gradientCache; 267 ProgramCache programCache; 268 PathCache pathCache; 269 RoundRectShapeCache roundRectShapeCache; 270 CircleShapeCache circleShapeCache; 271 OvalShapeCache ovalShapeCache; 272 RectShapeCache rectShapeCache; 273 ArcShapeCache arcShapeCache; 274 PatchCache patchCache; 275 TextDropShadowCache dropShadowCache; 276 FboCache fboCache; 277 ResourceCache resourceCache; 278 279 GammaFontRenderer* fontRenderer; 280 281 Dither dither; 282 Stencil stencil; 283 284 // Debug methods 285 PFNGLINSERTEVENTMARKEREXTPROC eventMark; 286 PFNGLPUSHGROUPMARKEREXTPROC startMark; 287 PFNGLPOPGROUPMARKEREXTPROC endMark; 288 289 PFNGLLABELOBJECTEXTPROC setLabel; 290 PFNGLGETOBJECTLABELEXTPROC getLabel; 291 292private: 293 void initFont(); 294 void initExtensions(); 295 void initConstraints(); 296 297 static void eventMarkNull(GLsizei length, const GLchar* marker) { } 298 static void startMarkNull(GLsizei length, const GLchar* marker) { } 299 static void endMarkNull() { } 300 301 static void setLabelNull(GLenum type, uint object, GLsizei length, 302 const char* label) { } 303 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize, 304 GLsizei* length, char* label) { 305 if (length) *length = 0; 306 if (label) *label = '\0'; 307 } 308 309 GLuint mCurrentBuffer; 310 GLuint mCurrentIndicesBuffer; 311 void* mCurrentPositionPointer; 312 GLsizei mCurrentPositionStride; 313 void* mCurrentTexCoordsPointer; 314 GLsizei mCurrentTexCoordsStride; 315 316 bool mTexCoordsArrayEnabled; 317 318 GLuint mTextureUnit; 319 320 GLint mScissorX; 321 GLint mScissorY; 322 GLint mScissorWidth; 323 GLint mScissorHeight; 324 325 Extensions& mExtensions; 326 327 // Used to render layers 328 TextureVertex* mRegionMesh; 329 GLuint mRegionMeshIndices; 330 331 mutable Mutex mGarbageLock; 332 Vector<Layer*> mLayerGarbage; 333 Vector<DisplayList*> mDisplayListGarbage; 334 335 DebugLevel mDebugLevel; 336 bool mInitialized; 337 338 uint32_t mFunctorsCount; 339}; // class Caches 340 341}; // namespace uirenderer 342}; // namespace android 343 344#endif // ANDROID_HWUI_CACHES_H 345