Caches.h revision 31e08e953fe7bdb1b1cbc247156cb6a19917a2f1
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     * Binds the specified texture as a GL_TEXTURE_2D texture.
230     * All texture bindings must be performed with this method or
231     * bindTexture(GLenum, GLuint).
232     */
233    void bindTexture(GLuint texture);
234
235    /**
236     * Binds the specified texture with the specified render target.
237     * All texture bindings must be performed with this method or
238     * bindTexture(GLuint).
239     */
240    void bindTexture(GLenum target, GLuint texture);
241
242    /**
243     * Deletes the specified texture and clears it from the cache
244     * of bound textures.
245     * All textures must be deleted using this method.
246     */
247    void deleteTexture(GLuint texture);
248
249    /**
250     * Signals that the cache of bound textures should be cleared.
251     * Other users of the context may have altered which textures are bound.
252     */
253    void resetBoundTextures();
254
255    /**
256     * Sets the scissor for the current surface.
257     */
258    bool setScissor(GLint x, GLint y, GLint width, GLint height);
259
260    /**
261     * Resets the scissor state.
262     */
263    void resetScissor();
264
265    bool enableScissor();
266    bool disableScissor();
267    void setScissorEnabled(bool enabled);
268
269    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
270    void endTiling();
271
272    /**
273     * Returns the mesh used to draw regions. Calling this method will
274     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
275     * indices for the region mesh.
276     */
277    TextureVertex* getRegionMesh();
278
279    /**
280     * Displays the memory usage of each cache and the total sum.
281     */
282    void dumpMemoryUsage();
283    void dumpMemoryUsage(String8& log);
284
285    bool hasRegisteredFunctors();
286    void registerFunctors(uint32_t functorCount);
287    void unregisterFunctors(uint32_t functorCount);
288
289    bool blend;
290    GLenum lastSrcMode;
291    GLenum lastDstMode;
292    Program* currentProgram;
293    bool scissorEnabled;
294
295    bool drawDeferDisabled;
296    bool drawReorderDisabled;
297
298    // VBO to draw with
299    GLuint meshBuffer;
300
301    // Misc
302    GLint maxTextureSize;
303
304    // Debugging
305    bool debugLayersUpdates;
306    bool debugOverdraw;
307
308    enum StencilClipDebug {
309        kStencilHide,
310        kStencilShowHighlight,
311        kStencilShowRegion
312    };
313    StencilClipDebug debugStencilClip;
314
315    TextureCache textureCache;
316    LayerCache layerCache;
317    RenderBufferCache renderBufferCache;
318    GradientCache gradientCache;
319    ProgramCache programCache;
320    PathCache pathCache;
321    PatchCache patchCache;
322    TextDropShadowCache dropShadowCache;
323    FboCache fboCache;
324    ResourceCache resourceCache;
325
326    GammaFontRenderer* fontRenderer;
327
328    TaskManager tasks;
329
330    Dither dither;
331    Stencil stencil;
332
333    AssetAtlas assetAtlas;
334
335    bool gpuPixelBuffersEnabled;
336
337    // Debug methods
338    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
339    PFNGLPUSHGROUPMARKEREXTPROC startMark;
340    PFNGLPOPGROUPMARKEREXTPROC endMark;
341
342    PFNGLLABELOBJECTEXTPROC setLabel;
343    PFNGLGETOBJECTLABELEXTPROC getLabel;
344
345private:
346    void initFont();
347    void initExtensions();
348    void initConstraints();
349    void initStaticProperties();
350
351    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
352    static void startMarkNull(GLsizei length, const GLchar* marker) { }
353    static void endMarkNull() { }
354
355    static void setLabelNull(GLenum type, uint object, GLsizei length,
356            const char* label) { }
357    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
358            GLsizei* length, char* label) {
359        if (length) *length = 0;
360        if (label) *label = '\0';
361    }
362
363    GLuint mCurrentBuffer;
364    GLuint mCurrentIndicesBuffer;
365    GLuint mCurrentPixelBuffer;
366    void* mCurrentPositionPointer;
367    GLsizei mCurrentPositionStride;
368    void* mCurrentTexCoordsPointer;
369    GLsizei mCurrentTexCoordsStride;
370
371    bool mTexCoordsArrayEnabled;
372
373    GLuint mTextureUnit;
374
375    GLint mScissorX;
376    GLint mScissorY;
377    GLint mScissorWidth;
378    GLint mScissorHeight;
379
380    Extensions& mExtensions;
381
382    // Used to render layers
383    TextureVertex* mRegionMesh;
384
385    // Global index buffer
386    GLuint mMeshIndices;
387
388    mutable Mutex mGarbageLock;
389    Vector<Layer*> mLayerGarbage;
390    Vector<DisplayList*> mDisplayListGarbage;
391
392    DebugLevel mDebugLevel;
393    bool mInitialized;
394
395    uint32_t mFunctorsCount;
396
397    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
398}; // class Caches
399
400}; // namespace uirenderer
401}; // namespace android
402
403#endif // ANDROID_HWUI_CACHES_H
404