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 ///////////////////////////////////////////////////////////////////////////////
     74 // Debug
     75 ///////////////////////////////////////////////////////////////////////////////
     76 
     77 struct CacheLogger {
     78     CacheLogger() {
     79         INIT_LOGD("Creating OpenGL renderer caches");
     80     }
     81 }; // struct CacheLogger
     82 
     83 ///////////////////////////////////////////////////////////////////////////////
     84 // Caches
     85 ///////////////////////////////////////////////////////////////////////////////
     86 
     87 class ANDROID_API Caches: public Singleton<Caches> {
     88     Caches();
     89 
     90     friend class Singleton<Caches>;
     91 
     92     CacheLogger mLogger;
     93 
     94     GLuint mCurrentBuffer;
     95 
     96     // Used to render layers
     97     TextureVertex* mRegionMesh;
     98     GLuint mRegionMeshIndices;
     99 
    100     mutable Mutex mGarbageLock;
    101     Vector<Layer*> mLayerGarbage;
    102 
    103 public:
    104     enum FlushMode {
    105         kFlushMode_Layers = 0,
    106         kFlushMode_Moderate,
    107         kFlushMode_Full
    108     };
    109 
    110     /**
    111      * Initializes the cache.
    112      */
    113     void init();
    114 
    115     /**
    116      * Flush the cache.
    117      *
    118      * @param mode Indicates how much of the cache should be flushed
    119      */
    120     void flush(FlushMode mode);
    121 
    122     /**
    123      * Destroys all resources associated with this cache. This should
    124      * be called after a flush(kFlushMode_Full).
    125      */
    126     void terminate();
    127 
    128     /**
    129      * Indicates whether the renderer is in debug mode.
    130      * This debug mode provides limited information to app developers.
    131      */
    132     DebugLevel getDebugLevel() const {
    133         return mDebugLevel;
    134     }
    135 
    136     /**
    137      * Call this on each frame to ensure that garbage is deleted from
    138      * GPU memory.
    139      */
    140     void clearGarbage();
    141 
    142     /**
    143      * Can be used to delete a layer from a non EGL thread.
    144      */
    145     void deleteLayerDeferred(Layer* layer);
    146 
    147     /**
    148      * Binds the VBO used to render simple textured quads.
    149      */
    150     void bindMeshBuffer();
    151 
    152     /**
    153      * Binds the specified VBO if needed.
    154      */
    155     void bindMeshBuffer(const GLuint buffer);
    156 
    157     /**
    158      * Unbinds the VBO used to render simple textured quads.
    159      */
    160     void unbindMeshBuffer();
    161 
    162     /**
    163      * Returns the mesh used to draw regions. Calling this method will
    164      * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
    165      * indices for the region mesh.
    166      */
    167     TextureVertex* getRegionMesh();
    168 
    169     /**
    170      * Displays the memory usage of each cache and the total sum.
    171      */
    172     void dumpMemoryUsage();
    173     void dumpMemoryUsage(String8& log);
    174 
    175     bool blend;
    176     GLenum lastSrcMode;
    177     GLenum lastDstMode;
    178     Program* currentProgram;
    179 
    180     // VBO to draw with
    181     GLuint meshBuffer;
    182 
    183     // GL extensions
    184     Extensions extensions;
    185 
    186     // Misc
    187     GLint maxTextureSize;
    188 
    189     TextureCache textureCache;
    190     LayerCache layerCache;
    191     GradientCache gradientCache;
    192     ProgramCache programCache;
    193     PathCache pathCache;
    194     RoundRectShapeCache roundRectShapeCache;
    195     CircleShapeCache circleShapeCache;
    196     OvalShapeCache ovalShapeCache;
    197     RectShapeCache rectShapeCache;
    198     ArcShapeCache arcShapeCache;
    199     PatchCache patchCache;
    200     TextDropShadowCache dropShadowCache;
    201     FboCache fboCache;
    202     GammaFontRenderer fontRenderer;
    203     ResourceCache resourceCache;
    204 
    205 private:
    206     DebugLevel mDebugLevel;
    207     bool mInitialized;
    208 }; // class Caches
    209 
    210 }; // namespace uirenderer
    211 }; // namespace android
    212 
    213 #endif // ANDROID_HWUI_CACHES_H
    214