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