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