Caches.h revision 2dc236b2bae13b9a0ed9b3f7320502aecd7983b3
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    /**
276     * Sets the scissor for the current surface.
277     */
278    bool setScissor(GLint x, GLint y, GLint width, GLint height);
279
280    /**
281     * Resets the scissor state.
282     */
283    void resetScissor();
284
285    bool enableScissor();
286    bool disableScissor();
287    void setScissorEnabled(bool enabled);
288
289    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
290    void endTiling();
291
292    /**
293     * Returns the mesh used to draw regions. Calling this method will
294     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
295     * indices for the region mesh.
296     */
297    TextureVertex* getRegionMesh();
298
299    /**
300     * Displays the memory usage of each cache and the total sum.
301     */
302    void dumpMemoryUsage();
303    void dumpMemoryUsage(String8& log);
304
305    bool hasRegisteredFunctors();
306    void registerFunctors(uint32_t functorCount);
307    void unregisterFunctors(uint32_t functorCount);
308
309    bool blend;
310    GLenum lastSrcMode;
311    GLenum lastDstMode;
312    Program* currentProgram;
313    bool scissorEnabled;
314
315    bool drawDeferDisabled;
316    bool drawReorderDisabled;
317
318    // VBO to draw with
319    GLuint meshBuffer;
320
321    // Misc
322    GLint maxTextureSize;
323
324    // Debugging
325    bool debugLayersUpdates;
326    bool debugOverdraw;
327
328    enum StencilClipDebug {
329        kStencilHide,
330        kStencilShowHighlight,
331        kStencilShowRegion
332    };
333    StencilClipDebug debugStencilClip;
334
335    TextureCache textureCache;
336    LayerCache layerCache;
337    RenderBufferCache renderBufferCache;
338    GradientCache gradientCache;
339    ProgramCache programCache;
340    PathCache pathCache;
341    PatchCache patchCache;
342    TessellationCache tessellationCache;
343    TextDropShadowCache dropShadowCache;
344    FboCache fboCache;
345    ResourceCache resourceCache;
346
347    GammaFontRenderer* fontRenderer;
348
349    TaskManager tasks;
350
351    Dither dither;
352    Stencil stencil;
353
354    AssetAtlas assetAtlas;
355
356    bool gpuPixelBuffersEnabled;
357
358    // Debug methods
359    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
360    PFNGLPUSHGROUPMARKEREXTPROC startMark;
361    PFNGLPOPGROUPMARKEREXTPROC endMark;
362
363    PFNGLLABELOBJECTEXTPROC setLabel;
364    PFNGLGETOBJECTLABELEXTPROC getLabel;
365
366    // TEMPORARY properties
367    void initTempProperties();
368    void setTempProperty(const char* name, const char* value);
369
370    float propertyLightDiameter;
371    float propertyLightPosY;
372    float propertyLightPosZ;
373    float propertyAmbientRatio;
374    int propertyAmbientShadowStrength;
375    int propertySpotShadowStrength;
376
377private:
378    enum OverdrawColorSet {
379        kColorSet_Default = 0,
380        kColorSet_Deuteranomaly
381    };
382
383    void initFont();
384    void initExtensions();
385    void initConstraints();
386    void initStaticProperties();
387
388    bool bindIndicesBufferInternal(const GLuint buffer);
389
390    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
391    static void startMarkNull(GLsizei length, const GLchar* marker) { }
392    static void endMarkNull() { }
393
394    static void setLabelNull(GLenum type, uint object, GLsizei length,
395            const char* label) { }
396    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
397            GLsizei* length, char* label) {
398        if (length) *length = 0;
399        if (label) *label = '\0';
400    }
401
402    GLuint mCurrentBuffer;
403    GLuint mCurrentIndicesBuffer;
404    GLuint mCurrentPixelBuffer;
405    const void* mCurrentPositionPointer;
406    GLsizei mCurrentPositionStride;
407    const void* mCurrentTexCoordsPointer;
408    GLsizei mCurrentTexCoordsStride;
409
410    bool mTexCoordsArrayEnabled;
411
412    GLuint mTextureUnit;
413
414    GLint mScissorX;
415    GLint mScissorY;
416    GLint mScissorWidth;
417    GLint mScissorHeight;
418
419    Extensions& mExtensions;
420
421    // Used to render layers
422    TextureVertex* mRegionMesh;
423
424    // Global index buffer
425    GLuint mMeshIndices;
426    GLuint mShadowStripsIndices;
427
428    mutable Mutex mGarbageLock;
429    Vector<Layer*> mLayerGarbage;
430
431    DebugLevel mDebugLevel;
432    bool mInitialized;
433
434    uint32_t mFunctorsCount;
435
436    // Caches texture bindings for the GL_TEXTURE_2D target
437    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
438
439    OverdrawColorSet mOverdrawDebugColorSet;
440
441    RenderState* mRenderState;
442}; // class Caches
443
444}; // namespace uirenderer
445}; // namespace android
446
447#endif // ANDROID_HWUI_CACHES_H
448