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