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