Caches.h revision 088c514cb13f3b8f8683588c2f398f18df1547c9
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
346    GammaFontRenderer* fontRenderer;
347
348    TaskManager tasks;
349
350    Dither dither;
351    Stencil stencil;
352
353    AssetAtlas assetAtlas;
354
355    bool gpuPixelBuffersEnabled;
356
357    // Debug methods
358    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
359    PFNGLPUSHGROUPMARKEREXTPROC startMark;
360    PFNGLPOPGROUPMARKEREXTPROC endMark;
361
362    PFNGLLABELOBJECTEXTPROC setLabel;
363    PFNGLGETOBJECTLABELEXTPROC getLabel;
364
365    // TEMPORARY properties
366    void initTempProperties();
367    void setTempProperty(const char* name, const char* value);
368
369    float propertyLightDiameter;
370    float propertyLightPosY;
371    float propertyLightPosZ;
372    float propertyAmbientRatio;
373    int propertyAmbientShadowStrength;
374    int propertySpotShadowStrength;
375
376private:
377    enum OverdrawColorSet {
378        kColorSet_Default = 0,
379        kColorSet_Deuteranomaly
380    };
381
382    void initFont();
383    void initExtensions();
384    void initConstraints();
385    void initStaticProperties();
386
387    bool bindIndicesBufferInternal(const GLuint buffer);
388
389    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
390    static void startMarkNull(GLsizei length, const GLchar* marker) { }
391    static void endMarkNull() { }
392
393    static void setLabelNull(GLenum type, uint object, GLsizei length,
394            const char* label) { }
395    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
396            GLsizei* length, char* label) {
397        if (length) *length = 0;
398        if (label) *label = '\0';
399    }
400
401    GLuint mCurrentBuffer;
402    GLuint mCurrentIndicesBuffer;
403    GLuint mCurrentPixelBuffer;
404    const void* mCurrentPositionPointer;
405    GLsizei mCurrentPositionStride;
406    const void* mCurrentTexCoordsPointer;
407    GLsizei mCurrentTexCoordsStride;
408
409    bool mTexCoordsArrayEnabled;
410
411    GLuint mTextureUnit;
412
413    GLint mScissorX;
414    GLint mScissorY;
415    GLint mScissorWidth;
416    GLint mScissorHeight;
417
418    Extensions& mExtensions;
419
420    // Used to render layers
421    TextureVertex* mRegionMesh;
422
423    // Global index buffer
424    GLuint mMeshIndices;
425    GLuint mShadowStripsIndices;
426
427    mutable Mutex mGarbageLock;
428    Vector<Layer*> mLayerGarbage;
429
430    DebugLevel mDebugLevel;
431    bool mInitialized;
432
433    uint32_t mFunctorsCount;
434
435    // Caches texture bindings for the GL_TEXTURE_2D target
436    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
437
438    OverdrawColorSet mOverdrawDebugColorSet;
439
440    RenderState* mRenderState;
441}; // class Caches
442
443}; // namespace uirenderer
444}; // namespace android
445
446#endif // ANDROID_HWUI_CACHES_H
447