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