Caches.h revision 65fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6
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 <vector>
25
26#include <GLES3/gl3.h>
27
28#include <utils/KeyedVector.h>
29#include <utils/Singleton.h>
30#include <utils/Vector.h>
31
32#include <cutils/compiler.h>
33
34#include <SkPath.h>
35
36#include "thread/TaskProcessor.h"
37#include "thread/TaskManager.h"
38
39#include "AssetAtlas.h"
40#include "Extensions.h"
41#include "TextureCache.h"
42#include "LayerCache.h"
43#include "RenderBufferCache.h"
44#include "GradientCache.h"
45#include "PatchCache.h"
46#include "ProgramCache.h"
47#include "PathCache.h"
48#include "TessellationCache.h"
49#include "TextDropShadowCache.h"
50#include "FboCache.h"
51#include "ResourceCache.h"
52#include "Stencil.h"
53#include "Dither.h"
54
55namespace android {
56namespace uirenderer {
57
58class GammaFontRenderer;
59
60///////////////////////////////////////////////////////////////////////////////
61// Globals
62///////////////////////////////////////////////////////////////////////////////
63
64// GL ES 2.0 defines that at least 16 texture units must be supported
65#define REQUIRED_TEXTURE_UNITS_COUNT 3
66
67// Maximum number of quads that pre-allocated meshes can draw
68static const uint32_t gMaxNumberOfQuads = 2048;
69
70// Generates simple and textured vertices
71#define FV(x, y, u, v) { x, y, u, v }
72
73// This array is never used directly but used as a memcpy source in the
74// OpenGLRenderer constructor
75static const TextureVertex gMeshVertices[] = {
76        FV(0.0f, 0.0f, 0.0f, 0.0f),
77        FV(1.0f, 0.0f, 1.0f, 0.0f),
78        FV(0.0f, 1.0f, 0.0f, 1.0f),
79        FV(1.0f, 1.0f, 1.0f, 1.0f)
80};
81static const GLsizei gMeshStride = sizeof(TextureVertex);
82static const GLsizei gVertexStride = sizeof(Vertex);
83static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
84static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
85static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
86static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
87static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
88static const GLsizei gMeshCount = 4;
89
90// Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT
91static const GLenum gTextureUnits[] = {
92    GL_TEXTURE0,
93    GL_TEXTURE1,
94    GL_TEXTURE2
95};
96
97///////////////////////////////////////////////////////////////////////////////
98// Debug
99///////////////////////////////////////////////////////////////////////////////
100
101struct CacheLogger {
102    CacheLogger() {
103        INIT_LOGD("Creating OpenGL renderer caches");
104    }
105}; // struct CacheLogger
106
107///////////////////////////////////////////////////////////////////////////////
108// Caches
109///////////////////////////////////////////////////////////////////////////////
110
111class RenderNode;
112class RenderState;
113
114class ANDROID_API Caches: public Singleton<Caches> {
115    Caches();
116
117    friend class Singleton<Caches>;
118
119    CacheLogger mLogger;
120
121public:
122    enum FlushMode {
123        kFlushMode_Layers = 0,
124        kFlushMode_Moderate,
125        kFlushMode_Full
126    };
127
128    /**
129     * Initialize caches.
130     */
131    bool init();
132
133    /**
134     * Initialize global system properties.
135     */
136    bool initProperties();
137
138    void setRenderState(RenderState* renderState) { mRenderState = renderState; }
139
140    /**
141     * Flush the cache.
142     *
143     * @param mode Indicates how much of the cache should be flushed
144     */
145    void flush(FlushMode mode);
146
147    /**
148     * Destroys all resources associated with this cache. This should
149     * be called after a flush(kFlushMode_Full).
150     */
151    void terminate();
152
153    /**
154     * Indicates whether the renderer is in debug mode.
155     * This debug mode provides limited information to app developers.
156     */
157    DebugLevel getDebugLevel() const {
158        return mDebugLevel;
159    }
160
161    /**
162     * Returns a non-premultiplied ARGB color for the specified
163     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
164     */
165    uint32_t getOverdrawColor(uint32_t amount) const;
166
167    /**
168     * Call this on each frame to ensure that garbage is deleted from
169     * GPU memory.
170     */
171    void clearGarbage();
172
173    /**
174     * Can be used to delete a layer from a non EGL thread.
175     */
176    void deleteLayerDeferred(Layer* layer);
177
178    /**
179     * Binds the VBO used to render simple textured quads.
180     */
181    bool bindMeshBuffer();
182
183    /**
184     * Binds the specified VBO if needed.
185     */
186    bool bindMeshBuffer(const GLuint buffer);
187
188    /**
189     * Unbinds the VBO used to render simple textured quads.
190     */
191    bool unbindMeshBuffer();
192
193    /**
194     * Binds a global indices buffer that can draw up to
195     * gMaxNumberOfQuads quads.
196     */
197    bool bindQuadIndicesBuffer();
198    bool bindShadowIndicesBuffer();
199    bool unbindIndicesBuffer();
200
201    /**
202     * Binds the specified buffer as the current GL unpack pixel buffer.
203     */
204    bool bindPixelBuffer(const GLuint buffer);
205
206    /**
207     * Resets the current unpack pixel buffer to 0 (default value.)
208     */
209    bool unbindPixelBuffer();
210
211    /**
212     * Binds an attrib to the specified float vertex pointer.
213     * Assumes a stride of gMeshStride and a size of 2.
214     */
215    void bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride = gMeshStride);
216
217    /**
218     * Binds an attrib to the specified float vertex pointer.
219     * Assumes a stride of gMeshStride and a size of 2.
220     */
221    void bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride = gMeshStride);
222
223    /**
224     * Resets the vertex pointers.
225     */
226    void resetVertexPointers();
227    void resetTexCoordsVertexPointer();
228
229    void enableTexCoordsVertexArray();
230    void disableTexCoordsVertexArray();
231
232    /**
233     * Activate the specified texture unit. The texture unit must
234     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
235     */
236    void activeTexture(GLuint textureUnit);
237
238    /**
239     * Invalidate the cached value of the active texture unit.
240     */
241    void resetActiveTexture();
242
243    /**
244     * Binds the specified texture as a GL_TEXTURE_2D texture.
245     * All texture bindings must be performed with this method or
246     * bindTexture(GLenum, GLuint).
247     */
248    void bindTexture(GLuint texture);
249
250    /**
251     * Binds the specified texture with the specified render target.
252     * All texture bindings must be performed with this method or
253     * bindTexture(GLuint).
254     */
255    void bindTexture(GLenum target, GLuint texture);
256
257    /**
258     * Deletes the specified texture and clears it from the cache
259     * of bound textures.
260     * All textures must be deleted using this method.
261     */
262    void deleteTexture(GLuint texture);
263
264    /**
265     * Signals that the cache of bound textures should be cleared.
266     * Other users of the context may have altered which textures are bound.
267     */
268    void resetBoundTextures();
269
270    /**
271     * Clear the cache of bound textures.
272     */
273    void unbindTexture(GLuint texture);
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
300    bool drawDeferDisabled;
301    bool drawReorderDisabled;
302
303    // VBO to draw with
304    GLuint meshBuffer;
305
306    // Misc
307    GLint maxTextureSize;
308
309    // Debugging
310    bool debugLayersUpdates;
311    bool debugOverdraw;
312
313    enum StencilClipDebug {
314        kStencilHide,
315        kStencilShowHighlight,
316        kStencilShowRegion
317    };
318    StencilClipDebug debugStencilClip;
319
320    TextureCache textureCache;
321    LayerCache layerCache;
322    RenderBufferCache renderBufferCache;
323    GradientCache gradientCache;
324    ProgramCache programCache;
325    PathCache pathCache;
326    PatchCache patchCache;
327    TessellationCache tessellationCache;
328    TextDropShadowCache dropShadowCache;
329    FboCache fboCache;
330
331    GammaFontRenderer* fontRenderer;
332
333    TaskManager tasks;
334
335    Dither dither;
336    Stencil stencil;
337
338    bool gpuPixelBuffersEnabled;
339
340    // Debug methods
341    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
342    PFNGLPUSHGROUPMARKEREXTPROC startMark;
343    PFNGLPOPGROUPMARKEREXTPROC endMark;
344
345    PFNGLLABELOBJECTEXTPROC setLabel;
346    PFNGLGETOBJECTLABELEXTPROC getLabel;
347
348    // TEMPORARY properties
349    void initTempProperties();
350    void setTempProperty(const char* name, const char* value);
351
352    float propertyLightDiameter;
353    float propertyLightPosY;
354    float propertyLightPosZ;
355    float propertyAmbientRatio;
356    int propertyAmbientShadowStrength;
357    int propertySpotShadowStrength;
358
359private:
360    enum OverdrawColorSet {
361        kColorSet_Default = 0,
362        kColorSet_Deuteranomaly
363    };
364
365    void initFont();
366    void initExtensions();
367    void initConstraints();
368    void initStaticProperties();
369
370    bool bindIndicesBufferInternal(const GLuint buffer);
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    const void* mCurrentPositionPointer;
388    GLsizei mCurrentPositionStride;
389    const void* mCurrentTexCoordsPointer;
390    GLsizei mCurrentTexCoordsStride;
391
392    bool mTexCoordsArrayEnabled;
393
394    GLuint mTextureUnit;
395
396    Extensions& mExtensions;
397
398    // Used to render layers
399    std::unique_ptr<TextureVertex[]> mRegionMesh;
400
401    // Global index buffer
402    GLuint mMeshIndices;
403    GLuint mShadowStripsIndices;
404
405    mutable Mutex mGarbageLock;
406    Vector<Layer*> mLayerGarbage;
407
408    DebugLevel mDebugLevel;
409    bool mInitialized;
410
411    uint32_t mFunctorsCount;
412
413    // Caches texture bindings for the GL_TEXTURE_2D target
414    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
415
416    OverdrawColorSet mOverdrawDebugColorSet;
417
418    RenderState* mRenderState;
419}; // class Caches
420
421}; // namespace uirenderer
422}; // namespace android
423
424#endif // ANDROID_HWUI_CACHES_H
425