Caches.h revision 0baaac5e9adf3ee280ae1239e2e58754a9d2b099
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 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
257    GammaFontRenderer* fontRenderer;
258
259    Dither dither;
260#if STENCIL_BUFFER_SIZE
261    Stencil stencil;
262#endif
263
264    // Debug methods
265    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
266    PFNGLPUSHGROUPMARKEREXTPROC startMark;
267    PFNGLPOPGROUPMARKEREXTPROC endMark;
268
269    PFNGLLABELOBJECTEXTPROC setLabel;
270    PFNGLGETOBJECTLABELEXTPROC getLabel;
271
272private:
273    void initFont();
274    void initExtensions();
275    void initConstraints();
276    void initProperties();
277
278    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
279    static void startMarkNull(GLsizei length, const GLchar* marker) { }
280    static void endMarkNull() { }
281
282    static void setLabelNull(GLenum type, uint object, GLsizei length,
283            const char* label) { }
284    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
285            GLsizei* length, char* label) {
286        if (length) *length = 0;
287        if (label) *label = '\0';
288    }
289
290    GLuint mCurrentBuffer;
291    GLuint mCurrentIndicesBuffer;
292    void* mCurrentPositionPointer;
293    void* mCurrentTexCoordsPointer;
294
295    bool mTexCoordsArrayEnabled;
296
297    GLuint mTextureUnit;
298
299    GLint mScissorX;
300    GLint mScissorY;
301    GLint mScissorWidth;
302    GLint mScissorHeight;
303
304    // Used to render layers
305    TextureVertex* mRegionMesh;
306    GLuint mRegionMeshIndices;
307
308    mutable Mutex mGarbageLock;
309    Vector<Layer*> mLayerGarbage;
310    Vector<DisplayList*> mDisplayListGarbage;
311
312    DebugLevel mDebugLevel;
313    bool mInitialized;
314}; // class Caches
315
316}; // namespace uirenderer
317}; // namespace android
318
319#endif // ANDROID_HWUI_CACHES_H
320