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