Caches.h revision 05f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5
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     * Sets the scissor for the current surface.
265     */
266    bool setScissor(GLint x, GLint y, GLint width, GLint height);
267
268    /**
269     * Resets the scissor state.
270     */
271    void resetScissor();
272
273    bool enableScissor();
274    bool disableScissor();
275    void setScissorEnabled(bool enabled);
276
277    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
278    void endTiling();
279
280    /**
281     * Returns the mesh used to draw regions. Calling this method will
282     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
283     * indices for the region mesh.
284     */
285    TextureVertex* getRegionMesh();
286
287    /**
288     * Displays the memory usage of each cache and the total sum.
289     */
290    void dumpMemoryUsage();
291    void dumpMemoryUsage(String8& log);
292
293    bool hasRegisteredFunctors();
294    void registerFunctors(uint32_t functorCount);
295    void unregisterFunctors(uint32_t functorCount);
296
297    bool blend;
298    GLenum lastSrcMode;
299    GLenum lastDstMode;
300    Program* currentProgram;
301    bool scissorEnabled;
302
303    bool drawDeferDisabled;
304    bool drawReorderDisabled;
305
306    // VBO to draw with
307    GLuint meshBuffer;
308
309    // Misc
310    GLint maxTextureSize;
311
312    // Debugging
313    bool debugLayersUpdates;
314    bool debugOverdraw;
315
316    enum StencilClipDebug {
317        kStencilHide,
318        kStencilShowHighlight,
319        kStencilShowRegion
320    };
321    StencilClipDebug debugStencilClip;
322
323    TextureCache textureCache;
324    LayerCache layerCache;
325    RenderBufferCache renderBufferCache;
326    GradientCache gradientCache;
327    ProgramCache programCache;
328    PathCache pathCache;
329    PatchCache patchCache;
330    TessellationCache tessellationCache;
331    TextDropShadowCache dropShadowCache;
332    FboCache fboCache;
333    ResourceCache resourceCache;
334
335    GammaFontRenderer* fontRenderer;
336
337    TaskManager tasks;
338
339    Dither dither;
340    Stencil stencil;
341
342    AssetAtlas assetAtlas;
343
344    bool gpuPixelBuffersEnabled;
345
346    // Debug methods
347    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
348    PFNGLPUSHGROUPMARKEREXTPROC startMark;
349    PFNGLPOPGROUPMARKEREXTPROC endMark;
350
351    PFNGLLABELOBJECTEXTPROC setLabel;
352    PFNGLGETOBJECTLABELEXTPROC getLabel;
353
354    // TEMPORARY properties
355    void initTempProperties();
356    void setTempProperty(const char* name, const char* value);
357
358    float propertyLightDiameter;
359    float propertyLightPosY;
360    float propertyLightPosZ;
361    float propertyAmbientRatio;
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