Caches.h revision 99585adeb4167ca357a72eb866f34c1af944f4b9
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 "Extensions.h"
27#include "FontRenderer.h"
28#include "GammaFontRenderer.h"
29#include "TextureCache.h"
30#include "LayerCache.h"
31#include "GradientCache.h"
32#include "PatchCache.h"
33#include "ProgramCache.h"
34#include "ShapeCache.h"
35#include "PathCache.h"
36#include "TextDropShadowCache.h"
37#include "FboCache.h"
38#include "ResourceCache.h"
39
40namespace android {
41namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Globals
45///////////////////////////////////////////////////////////////////////////////
46
47#define REQUIRED_TEXTURE_UNITS_COUNT 3
48
49#define REGION_MESH_QUAD_COUNT 512
50
51// Generates simple and textured vertices
52#define FV(x, y, u, v) { { x, y }, { u, v } }
53
54// This array is never used directly but used as a memcpy source in the
55// OpenGLRenderer constructor
56static const TextureVertex gMeshVertices[] = {
57        FV(0.0f, 0.0f, 0.0f, 0.0f),
58        FV(1.0f, 0.0f, 1.0f, 0.0f),
59        FV(0.0f, 1.0f, 0.0f, 1.0f),
60        FV(1.0f, 1.0f, 1.0f, 1.0f)
61};
62static const GLsizei gMeshStride = sizeof(TextureVertex);
63static const GLsizei gVertexStride = sizeof(Vertex);
64static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
65static const GLsizei gAAVertexStride = sizeof(AAVertex);
66static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
67static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
68static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
69static const GLsizei gMeshCount = 4;
70
71///////////////////////////////////////////////////////////////////////////////
72// Debug
73///////////////////////////////////////////////////////////////////////////////
74
75struct CacheLogger {
76    CacheLogger() {
77        LOGD("Creating OpenGL renderer caches");
78    }
79}; // struct CacheLogger
80
81///////////////////////////////////////////////////////////////////////////////
82// Caches
83///////////////////////////////////////////////////////////////////////////////
84
85class Caches: public Singleton<Caches> {
86    Caches();
87    ~Caches();
88
89    friend class Singleton<Caches>;
90
91    CacheLogger mLogger;
92
93    GLuint mCurrentBuffer;
94
95    // Used to render layers
96    TextureVertex* mRegionMesh;
97    GLuint mRegionMeshIndices;
98
99    mutable Mutex mGarbageLock;
100    Vector<Layer*> mLayerGarbage;
101
102public:
103    /**
104     * Indicates whether the renderer is in debug mode.
105     * This debug mode provides limited information to app developers.
106     */
107    DebugLevel getDebugLevel() const {
108        return mDebugLevel;
109    }
110
111    /**
112     * Call this on each frame to ensure that garbage is deleted from
113     * GPU memory.
114     */
115    void clearGarbage();
116
117    /**
118     * Can be used to delete a layer from a non EGL thread.
119     */
120    void deleteLayerDeferred(Layer* layer);
121
122    /**
123     * Binds the VBO used to render simple textured quads.
124     */
125    void bindMeshBuffer();
126
127    /**
128     * Binds the specified VBO if needed.
129     */
130    void bindMeshBuffer(const GLuint buffer);
131
132    /**
133     * Unbinds the VBO used to render simple textured quads.
134     */
135    void unbindMeshBuffer();
136
137    /**
138     * Returns the mesh used to draw regions. Calling this method will
139     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
140     * indices for the region mesh.
141     */
142    TextureVertex* getRegionMesh();
143
144    /**
145     * Displays the memory usage of each cache and the total sum.
146     */
147    void dumpMemoryUsage();
148    void dumpMemoryUsage(String8& log);
149
150    bool blend;
151    GLenum lastSrcMode;
152    GLenum lastDstMode;
153    Program* currentProgram;
154
155    // VBO to draw with
156    GLuint meshBuffer;
157
158    // GL extensions
159    Extensions extensions;
160
161    // Misc
162    GLint maxTextureSize;
163
164    TextureCache textureCache;
165    LayerCache layerCache;
166    GradientCache gradientCache;
167    ProgramCache programCache;
168    PathCache pathCache;
169    RoundRectShapeCache roundRectShapeCache;
170    CircleShapeCache circleShapeCache;
171    OvalShapeCache ovalShapeCache;
172    RectShapeCache rectShapeCache;
173    ArcShapeCache arcShapeCache;
174    PatchCache patchCache;
175    TextDropShadowCache dropShadowCache;
176    FboCache fboCache;
177    GammaFontRenderer fontRenderer;
178    ResourceCache resourceCache;
179
180private:
181    DebugLevel mDebugLevel;
182}; // class Caches
183
184}; // namespace uirenderer
185}; // namespace android
186
187#endif // ANDROID_HWUI_CACHES_H
188