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