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