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