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 
     42 namespace android {
     43 namespace uirenderer {
     44 
     45 ///////////////////////////////////////////////////////////////////////////////
     46 // Globals
     47 ///////////////////////////////////////////////////////////////////////////////
     48 
     49 #define REQUIRED_TEXTURE_UNITS_COUNT 3
     50 
     51 #define REGION_MESH_QUAD_COUNT 512
     52 
     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);
     65 static const GLsizei gVertexStride = sizeof(Vertex);
     66 static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
     67 static const GLsizei gAAVertexStride = sizeof(AAVertex);
     68 static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
     69 static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
     70 static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
     71 static const GLsizei gMeshCount = 4;
     72 
     73 static const GLenum gTextureUnits[] = {
     74     GL_TEXTURE0,
     75     GL_TEXTURE1,
     76     GL_TEXTURE2
     77 };
     78 
     79 ///////////////////////////////////////////////////////////////////////////////
     80 // Debug
     81 ///////////////////////////////////////////////////////////////////////////////
     82 
     83 struct CacheLogger {
     84     CacheLogger() {
     85         INIT_LOGD("Creating OpenGL renderer caches");
     86     }
     87 }; // struct CacheLogger
     88 
     89 ///////////////////////////////////////////////////////////////////////////////
     90 // Caches
     91 ///////////////////////////////////////////////////////////////////////////////
     92 
     93 class DisplayList;
     94 
     95 class ANDROID_API Caches: public Singleton<Caches> {
     96     Caches();
     97 
     98     friend class Singleton<Caches>;
     99 
    100     CacheLogger mLogger;
    101 
    102 public:
    103     enum FlushMode {
    104         kFlushMode_Layers = 0,
    105         kFlushMode_Moderate,
    106         kFlushMode_Full
    107     };
    108 
    109     /**
    110      * Initialize caches.
    111      */
    112     void init();
    113 
    114     /**
    115      * Flush the cache.
    116      *
    117      * @param mode Indicates how much of the cache should be flushed
    118      */
    119     void flush(FlushMode mode);
    120 
    121     /**
    122      * Destroys all resources associated with this cache. This should
    123      * be called after a flush(kFlushMode_Full).
    124      */
    125     void terminate();
    126 
    127     /**
    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     /**
    136      * Call this on each frame to ensure that garbage is deleted from
    137      * GPU memory.
    138      */
    139     void clearGarbage();
    140 
    141     /**
    142      * Can be used to delete a layer from a non EGL thread.
    143      */
    144     void deleteLayerDeferred(Layer* layer);
    145 
    146     /*
    147      * Can be used to delete a display list from a non EGL thread.
    148      */
    149     void deleteDisplayListDeferred(DisplayList* layer);
    150 
    151     /**
    152      * Binds the VBO used to render simple textured quads.
    153      */
    154     bool bindMeshBuffer();
    155 
    156     /**
    157      * Binds the specified VBO if needed.
    158      */
    159     bool bindMeshBuffer(const GLuint buffer);
    160 
    161     /**
    162      * Unbinds the VBO used to render simple textured quads.
    163      */
    164     bool unbindMeshBuffer();
    165 
    166     bool bindIndicesBuffer(const GLuint buffer);
    167     bool unbindIndicesBuffer();
    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 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();
    187 
    188     void enableTexCoordsVertexArray();
    189     void disbaleTexCoordsVertexArray();
    190 
    191     /**
    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     /**
    198      * Sets the scissor for the current surface.
    199      */
    200     void setScissor(GLint x, GLint y, GLint width, GLint height);
    201 
    202     /**
    203      * Resets the scissor state.
    204      */
    205     void resetScissor();
    206 
    207     /**
    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 
    214     /**
    215      * Displays the memory usage of each cache and the total sum.
    216      */
    217     void dumpMemoryUsage();
    218     void dumpMemoryUsage(String8& log);
    219 
    220     bool blend;
    221     GLenum lastSrcMode;
    222     GLenum lastDstMode;
    223     Program* currentProgram;
    224 
    225     // VBO to draw with
    226     GLuint meshBuffer;
    227 
    228     // GL extensions
    229     Extensions extensions;
    230 
    231     // Misc
    232     GLint maxTextureSize;
    233 
    234     TextureCache textureCache;
    235     LayerCache layerCache;
    236     GradientCache gradientCache;
    237     ProgramCache programCache;
    238     PathCache pathCache;
    239     RoundRectShapeCache roundRectShapeCache;
    240     CircleShapeCache circleShapeCache;
    241     OvalShapeCache ovalShapeCache;
    242     RectShapeCache rectShapeCache;
    243     ArcShapeCache arcShapeCache;
    244     PatchCache patchCache;
    245     TextDropShadowCache dropShadowCache;
    246     FboCache fboCache;
    247     GammaFontRenderer fontRenderer;
    248     ResourceCache resourceCache;
    249 
    250     // Debug methods
    251     PFNGLINSERTEVENTMARKEREXTPROC eventMark;
    252     PFNGLPUSHGROUPMARKEREXTPROC startMark;
    253     PFNGLPOPGROUPMARKEREXTPROC endMark;
    254 
    255     PFNGLLABELOBJECTEXTPROC setLabel;
    256     PFNGLGETOBJECTLABELEXTPROC getLabel;
    257 
    258 private:
    259     void initExtensions();
    260     void initConstraints();
    261 
    262     static void eventMarkNull(GLsizei length, const GLchar* marker) { }
    263     static void startMarkNull(GLsizei length, const GLchar* marker) { }
    264     static void endMarkNull() { }
    265 
    266     static void setLabelNull(GLenum type, uint object, GLsizei length,
    267             const char* label) { }
    268     static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
    269             GLsizei* length, char* label) {
    270         if (length) *length = 0;
    271         if (label) *label = '\0';
    272     }
    273 
    274     GLuint mCurrentBuffer;
    275     GLuint mCurrentIndicesBuffer;
    276     void* mCurrentPositionPointer;
    277     void* mCurrentTexCoordsPointer;
    278 
    279     bool mTexCoordsArrayEnabled;
    280 
    281     GLuint mTextureUnit;
    282 
    283     GLint mScissorX;
    284     GLint mScissorY;
    285     GLint mScissorWidth;
    286     GLint mScissorHeight;
    287 
    288     // Used to render layers
    289     TextureVertex* mRegionMesh;
    290     GLuint mRegionMeshIndices;
    291 
    292     mutable Mutex mGarbageLock;
    293     Vector<Layer*> mLayerGarbage;
    294     Vector<DisplayList*> mDisplayListGarbage;
    295 
    296     DebugLevel mDebugLevel;
    297     bool mInitialized;
    298 }; // class Caches
    299 
    300 }; // namespace uirenderer
    301 }; // namespace android
    302 
    303 #endif // ANDROID_HWUI_CACHES_H
    304