Caches.h revision 54c1a64d5441a964890b44280e4457e11f4f924a
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 hasRegisteredFunctors();
231    void registerFunctors(uint32_t functorCount);
232    void unregisterFunctors(uint32_t functorCount);
233
234    bool blend;
235    GLenum lastSrcMode;
236    GLenum lastDstMode;
237    Program* currentProgram;
238    bool scissorEnabled;
239
240    // VBO to draw with
241    GLuint meshBuffer;
242
243    // GL extensions
244    Extensions extensions;
245
246    // Misc
247    GLint maxTextureSize;
248    bool debugLayersUpdates;
249    bool debugOverdraw;
250
251    TextureCache textureCache;
252    LayerCache layerCache;
253    GradientCache gradientCache;
254    ProgramCache programCache;
255    PathCache pathCache;
256    RoundRectShapeCache roundRectShapeCache;
257    CircleShapeCache circleShapeCache;
258    OvalShapeCache ovalShapeCache;
259    RectShapeCache rectShapeCache;
260    ArcShapeCache arcShapeCache;
261    PatchCache patchCache;
262    TextDropShadowCache dropShadowCache;
263    FboCache fboCache;
264    ResourceCache resourceCache;
265
266    GammaFontRenderer* fontRenderer;
267
268    Dither dither;
269#if STENCIL_BUFFER_SIZE
270    Stencil stencil;
271#endif
272
273    // Debug methods
274    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
275    PFNGLPUSHGROUPMARKEREXTPROC startMark;
276    PFNGLPOPGROUPMARKEREXTPROC endMark;
277
278    PFNGLLABELOBJECTEXTPROC setLabel;
279    PFNGLGETOBJECTLABELEXTPROC getLabel;
280
281private:
282    void initFont();
283    void initExtensions();
284    void initConstraints();
285    void initProperties();
286
287    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
288    static void startMarkNull(GLsizei length, const GLchar* marker) { }
289    static void endMarkNull() { }
290
291    static void setLabelNull(GLenum type, uint object, GLsizei length,
292            const char* label) { }
293    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
294            GLsizei* length, char* label) {
295        if (length) *length = 0;
296        if (label) *label = '\0';
297    }
298
299    GLuint mCurrentBuffer;
300    GLuint mCurrentIndicesBuffer;
301    void* mCurrentPositionPointer;
302    void* mCurrentTexCoordsPointer;
303
304    bool mTexCoordsArrayEnabled;
305
306    GLuint mTextureUnit;
307
308    GLint mScissorX;
309    GLint mScissorY;
310    GLint mScissorWidth;
311    GLint mScissorHeight;
312
313    // Used to render layers
314    TextureVertex* mRegionMesh;
315    GLuint mRegionMeshIndices;
316
317    mutable Mutex mGarbageLock;
318    Vector<Layer*> mLayerGarbage;
319    Vector<DisplayList*> mDisplayListGarbage;
320
321    DebugLevel mDebugLevel;
322    bool mInitialized;
323
324    uint32_t mFunctorsCount;
325}; // class Caches
326
327}; // namespace uirenderer
328}; // namespace android
329
330#endif // ANDROID_HWUI_CACHES_H
331