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