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