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