Caches.h revision 4a473c7d9406a2d6f6792f0f48d933424740ec5c
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     * Returns a non-premultiplied ARGB color for the specified
154     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
155     */
156    uint32_t getOverdrawColor(uint32_t amount) const;
157
158    /**
159     * Call this on each frame to ensure that garbage is deleted from
160     * GPU memory.
161     */
162    void clearGarbage();
163
164    /**
165     * Can be used to delete a layer from a non EGL thread.
166     */
167    void deleteLayerDeferred(Layer* layer);
168
169    /*
170     * Can be used to delete a display list from a non EGL thread.
171     */
172    void deleteDisplayListDeferred(DisplayList* layer);
173
174    /**
175     * Binds the VBO used to render simple textured quads.
176     */
177    bool bindMeshBuffer();
178
179    /**
180     * Binds the specified VBO if needed.
181     */
182    bool bindMeshBuffer(const GLuint buffer);
183
184    /**
185     * Unbinds the VBO used to render simple textured quads.
186     */
187    bool unbindMeshBuffer();
188
189    /**
190     * Binds a global indices buffer that can draw up to
191     * gMaxNumberOfQuads quads.
192     */
193    bool bindIndicesBuffer();
194    bool bindIndicesBuffer(const GLuint buffer);
195    bool unbindIndicesBuffer();
196
197    /**
198     * Binds the specified buffer as the current GL unpack pixel buffer.
199     */
200    bool bindPixelBuffer(const GLuint buffer);
201
202    /**
203     * Resets the current unpack pixel buffer to 0 (default value.)
204     */
205    bool unbindPixelBuffer();
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 bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
212
213    /**
214     * Binds an attrib to the specified float vertex pointer.
215     * Assumes a stride of gMeshStride and a size of 2.
216     */
217    void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
218
219    /**
220     * Resets the vertex pointers.
221     */
222    void resetVertexPointers();
223    void resetTexCoordsVertexPointer();
224
225    void enableTexCoordsVertexArray();
226    void disableTexCoordsVertexArray();
227
228    /**
229     * Activate the specified texture unit. The texture unit must
230     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
231     */
232    void activeTexture(GLuint textureUnit);
233
234    /**
235     * Invalidate the cached value of the active texture unit.
236     */
237    void resetActiveTexture();
238
239    /**
240     * Binds the specified texture as a GL_TEXTURE_2D texture.
241     * All texture bindings must be performed with this method or
242     * bindTexture(GLenum, GLuint).
243     */
244    void bindTexture(GLuint texture);
245
246    /**
247     * Binds the specified texture with the specified render target.
248     * All texture bindings must be performed with this method or
249     * bindTexture(GLuint).
250     */
251    void bindTexture(GLenum target, GLuint texture);
252
253    /**
254     * Deletes the specified texture and clears it from the cache
255     * of bound textures.
256     * All textures must be deleted using this method.
257     */
258    void deleteTexture(GLuint texture);
259
260    /**
261     * Signals that the cache of bound textures should be cleared.
262     * Other users of the context may have altered which textures are bound.
263     */
264    void resetBoundTextures();
265
266    /**
267     * Clear the cache of bound textures.
268     */
269    void unbindTexture(GLuint texture);
270
271    /**
272     * Sets the scissor for the current surface.
273     */
274    bool setScissor(GLint x, GLint y, GLint width, GLint height);
275
276    /**
277     * Resets the scissor state.
278     */
279    void resetScissor();
280
281    bool enableScissor();
282    bool disableScissor();
283    void setScissorEnabled(bool enabled);
284
285    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
286    void endTiling();
287
288    /**
289     * Returns the mesh used to draw regions. Calling this method will
290     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
291     * indices for the region mesh.
292     */
293    TextureVertex* getRegionMesh();
294
295    /**
296     * Displays the memory usage of each cache and the total sum.
297     */
298    void dumpMemoryUsage();
299    void dumpMemoryUsage(String8& log);
300
301    bool hasRegisteredFunctors();
302    void registerFunctors(uint32_t functorCount);
303    void unregisterFunctors(uint32_t functorCount);
304
305    bool blend;
306    GLenum lastSrcMode;
307    GLenum lastDstMode;
308    Program* currentProgram;
309    bool scissorEnabled;
310
311    bool drawDeferDisabled;
312    bool drawReorderDisabled;
313
314    // VBO to draw with
315    GLuint meshBuffer;
316
317    // Misc
318    GLint maxTextureSize;
319
320    // Debugging
321    bool debugLayersUpdates;
322    bool debugOverdraw;
323
324    enum StencilClipDebug {
325        kStencilHide,
326        kStencilShowHighlight,
327        kStencilShowRegion
328    };
329    StencilClipDebug debugStencilClip;
330
331    TextureCache textureCache;
332    LayerCache layerCache;
333    RenderBufferCache renderBufferCache;
334    GradientCache gradientCache;
335    ProgramCache programCache;
336    PathCache pathCache;
337    PatchCache patchCache;
338    TextDropShadowCache dropShadowCache;
339    FboCache fboCache;
340    ResourceCache resourceCache;
341
342    GammaFontRenderer* fontRenderer;
343
344    TaskManager tasks;
345
346    Dither dither;
347    Stencil stencil;
348
349    AssetAtlas assetAtlas;
350
351    bool gpuPixelBuffersEnabled;
352
353    // Debug methods
354    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
355    PFNGLPUSHGROUPMARKEREXTPROC startMark;
356    PFNGLPOPGROUPMARKEREXTPROC endMark;
357
358    PFNGLLABELOBJECTEXTPROC setLabel;
359    PFNGLGETOBJECTLABELEXTPROC getLabel;
360
361private:
362    enum OverdrawColorSet {
363        kColorSet_Default = 0,
364        kColorSet_Deuteranomaly
365    };
366
367    void initFont();
368    void initExtensions();
369    void initConstraints();
370    void initStaticProperties();
371
372    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
373    static void startMarkNull(GLsizei length, const GLchar* marker) { }
374    static void endMarkNull() { }
375
376    static void setLabelNull(GLenum type, uint object, GLsizei length,
377            const char* label) { }
378    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
379            GLsizei* length, char* label) {
380        if (length) *length = 0;
381        if (label) *label = '\0';
382    }
383
384    GLuint mCurrentBuffer;
385    GLuint mCurrentIndicesBuffer;
386    GLuint mCurrentPixelBuffer;
387    void* mCurrentPositionPointer;
388    GLsizei mCurrentPositionStride;
389    void* mCurrentTexCoordsPointer;
390    GLsizei mCurrentTexCoordsStride;
391
392    bool mTexCoordsArrayEnabled;
393
394    GLuint mTextureUnit;
395
396    GLint mScissorX;
397    GLint mScissorY;
398    GLint mScissorWidth;
399    GLint mScissorHeight;
400
401    Extensions& mExtensions;
402
403    // Used to render layers
404    TextureVertex* mRegionMesh;
405
406    // Global index buffer
407    GLuint mMeshIndices;
408
409    mutable Mutex mGarbageLock;
410    Vector<Layer*> mLayerGarbage;
411    Vector<DisplayList*> mDisplayListGarbage;
412
413    DebugLevel mDebugLevel;
414    bool mInitialized;
415
416    uint32_t mFunctorsCount;
417
418    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
419
420    OverdrawColorSet mOverdrawDebugColorSet;
421}; // class Caches
422
423}; // namespace uirenderer
424}; // namespace android
425
426#endif // ANDROID_HWUI_CACHES_H
427