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