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