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