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