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