Caches.h revision 13631f3da855f200a151e7837ed9f6b079622b58
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
73static const GLenum gTextureUnits[] = {
74    GL_TEXTURE0,
75    GL_TEXTURE1,
76    GL_TEXTURE2
77};
78
79///////////////////////////////////////////////////////////////////////////////
80// Debug
81///////////////////////////////////////////////////////////////////////////////
82
83struct CacheLogger {
84    CacheLogger() {
85        INIT_LOGD("Creating OpenGL renderer caches");
86    }
87}; // struct CacheLogger
88
89///////////////////////////////////////////////////////////////////////////////
90// Caches
91///////////////////////////////////////////////////////////////////////////////
92
93class ANDROID_API Caches: public Singleton<Caches> {
94    Caches();
95
96    friend class Singleton<Caches>;
97
98    CacheLogger mLogger;
99
100public:
101    enum FlushMode {
102        kFlushMode_Layers = 0,
103        kFlushMode_Moderate,
104        kFlushMode_Full
105    };
106
107    /**
108     * Initializes the cache.
109     */
110    void init();
111
112    /**
113     * Flush the cache.
114     *
115     * @param mode Indicates how much of the cache should be flushed
116     */
117    void flush(FlushMode mode);
118
119    /**
120     * Destroys all resources associated with this cache. This should
121     * be called after a flush(kFlushMode_Full).
122     */
123    void terminate();
124
125    /**
126     * Indicates whether the renderer is in debug mode.
127     * This debug mode provides limited information to app developers.
128     */
129    DebugLevel getDebugLevel() const {
130        return mDebugLevel;
131    }
132
133    /**
134     * Call this on each frame to ensure that garbage is deleted from
135     * GPU memory.
136     */
137    void clearGarbage();
138
139    /**
140     * Can be used to delete a layer from a non EGL thread.
141     */
142    void deleteLayerDeferred(Layer* layer);
143
144    /**
145     * Binds the VBO used to render simple textured quads.
146     */
147    bool bindMeshBuffer();
148
149    /**
150     * Binds the specified VBO if needed.
151     */
152    bool bindMeshBuffer(const GLuint buffer);
153
154    /**
155     * Unbinds the VBO used to render simple textured quads.
156     */
157    bool unbindMeshBuffer();
158
159    bool bindIndicesBuffer(const GLuint buffer);
160    bool unbindIndicesBuffer();
161
162    /**
163     * Binds an attrib to the specified float vertex pointer.
164     * Assumes a stride of gMeshStride and a size of 2.
165     */
166    void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
167            GLsizei stride = gMeshStride);
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 bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);
174
175    /**
176     * Resets the vertex pointers.
177     */
178    void resetVertexPointers();
179    void resetTexCoordsVertexPointer();
180
181    void enableTexCoordsVertexArray();
182    void disbaleTexCoordsVertexArray();
183
184    /**
185     * Activate the specified texture unit. The texture unit must
186     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
187     */
188    void activeTexture(GLuint textureUnit);
189
190    /**
191     * Sets the scissor for the current surface.
192     */
193    void setScissor(GLint x, GLint y, GLint width, GLint height);
194
195    /**
196     * Resets the scissor state.
197     */
198    void resetScissor();
199
200    /**
201     * Returns the mesh used to draw regions. Calling this method will
202     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
203     * indices for the region mesh.
204     */
205    TextureVertex* getRegionMesh();
206
207    /**
208     * Displays the memory usage of each cache and the total sum.
209     */
210    void dumpMemoryUsage();
211    void dumpMemoryUsage(String8& log);
212
213    bool blend;
214    GLenum lastSrcMode;
215    GLenum lastDstMode;
216    Program* currentProgram;
217
218    // VBO to draw with
219    GLuint meshBuffer;
220
221    // GL extensions
222    Extensions extensions;
223
224    // Misc
225    GLint maxTextureSize;
226
227    TextureCache textureCache;
228    LayerCache layerCache;
229    GradientCache gradientCache;
230    ProgramCache programCache;
231    PathCache pathCache;
232    RoundRectShapeCache roundRectShapeCache;
233    CircleShapeCache circleShapeCache;
234    OvalShapeCache ovalShapeCache;
235    RectShapeCache rectShapeCache;
236    ArcShapeCache arcShapeCache;
237    PatchCache patchCache;
238    TextDropShadowCache dropShadowCache;
239    FboCache fboCache;
240    GammaFontRenderer fontRenderer;
241    ResourceCache resourceCache;
242
243    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
244    PFNGLPUSHGROUPMARKEREXTPROC startMark;
245    PFNGLPOPGROUPMARKEREXTPROC endMark;
246
247private:
248    static void eventMarkNull(GLsizei length, const GLchar *marker) { }
249    static void startMarkNull(GLsizei length, const GLchar *marker) { }
250    static void endMarkNull() { }
251
252    GLuint mCurrentBuffer;
253    GLuint mCurrentIndicesBuffer;
254    void* mCurrentPositionPointer;
255    void* mCurrentTexCoordsPointer;
256
257    bool mTexCoordsArrayEnabled;
258
259    GLuint mTextureUnit;
260
261    GLint mScissorX;
262    GLint mScissorY;
263    GLint mScissorWidth;
264    GLint mScissorHeight;
265
266    // Used to render layers
267    TextureVertex* mRegionMesh;
268    GLuint mRegionMeshIndices;
269
270    mutable Mutex mGarbageLock;
271    Vector<Layer*> mLayerGarbage;
272
273    DebugLevel mDebugLevel;
274    bool mInitialized;
275}; // class Caches
276
277}; // namespace uirenderer
278}; // namespace android
279
280#endif // ANDROID_HWUI_CACHES_H
281