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