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