Caches.h revision 6c15ffa196fc9b7724c189d833c3435d8db12266
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
25#include "AssetAtlas.h"
26#include "Dither.h"
27#include "Extensions.h"
28#include "FboCache.h"
29#include "GradientCache.h"
30#include "LayerCache.h"
31#include "PatchCache.h"
32#include "ProgramCache.h"
33#include "PathCache.h"
34#include "RenderBufferCache.h"
35#include "renderstate/PixelBufferState.h"
36#include "renderstate/TextureState.h"
37#include "ResourceCache.h"
38#include "TessellationCache.h"
39#include "TextDropShadowCache.h"
40#include "TextureCache.h"
41#include "thread/TaskProcessor.h"
42#include "thread/TaskManager.h"
43
44#include <vector>
45#include <memory>
46
47#include <GLES3/gl3.h>
48
49#include <utils/KeyedVector.h>
50#include <utils/Singleton.h>
51#include <utils/Vector.h>
52
53#include <cutils/compiler.h>
54
55#include <SkPath.h>
56
57namespace android {
58namespace uirenderer {
59
60class GammaFontRenderer;
61
62///////////////////////////////////////////////////////////////////////////////
63// Caches
64///////////////////////////////////////////////////////////////////////////////
65
66class RenderNode;
67class RenderState;
68
69class ANDROID_API Caches {
70public:
71    static Caches& createInstance(RenderState& renderState) {
72        LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
73        sInstance = new Caches(renderState);
74        return *sInstance;
75    }
76
77    static Caches& getInstance() {
78        LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
79        return *sInstance;
80    }
81
82    static bool hasInstance() {
83        return sInstance != 0;
84    }
85private:
86    Caches(RenderState& renderState);
87    static Caches* sInstance;
88
89public:
90    enum FlushMode {
91        kFlushMode_Layers = 0,
92        kFlushMode_Moderate,
93        kFlushMode_Full
94    };
95
96    /**
97     * Initialize caches.
98     */
99    bool init();
100
101    /**
102     * Initialize global system properties.
103     */
104    bool initProperties();
105
106    /**
107     * Flush the cache.
108     *
109     * @param mode Indicates how much of the cache should be flushed
110     */
111    void flush(FlushMode mode);
112
113    /**
114     * Destroys all resources associated with this cache. This should
115     * be called after a flush(kFlushMode_Full).
116     */
117    void terminate();
118
119    /**
120     * Indicates whether the renderer is in debug mode.
121     * This debug mode provides limited information to app developers.
122     */
123    DebugLevel getDebugLevel() const {
124        return mDebugLevel;
125    }
126
127    /**
128     * Returns a non-premultiplied ARGB color for the specified
129     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
130     */
131    uint32_t getOverdrawColor(uint32_t amount) const;
132
133    /**
134     * Call this on each frame to ensure that garbage is deleted from
135     * GPU memory.
136     */
137    void clearGarbage();
138
139    /**
140     * Can be used to delete a layer from a non EGL thread.
141     */
142    void deleteLayerDeferred(Layer* layer);
143
144
145    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
146    void endTiling();
147
148    /**
149     * Returns the mesh used to draw regions. Calling this method will
150     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
151     * indices for the region mesh.
152     */
153    TextureVertex* getRegionMesh();
154
155    /**
156     * Displays the memory usage of each cache and the total sum.
157     */
158    void dumpMemoryUsage();
159    void dumpMemoryUsage(String8& log);
160
161    bool hasRegisteredFunctors();
162    void registerFunctors(uint32_t functorCount);
163    void unregisterFunctors(uint32_t functorCount);
164
165    bool drawDeferDisabled;
166    bool drawReorderDisabled;
167
168    // Misc
169    GLint maxTextureSize;
170
171    // Debugging
172    bool debugLayersUpdates;
173    bool debugOverdraw;
174
175    enum StencilClipDebug {
176        kStencilHide,
177        kStencilShowHighlight,
178        kStencilShowRegion
179    };
180    StencilClipDebug debugStencilClip;
181
182    TextureCache textureCache;
183    LayerCache layerCache;
184    RenderBufferCache renderBufferCache;
185    GradientCache gradientCache;
186    ProgramCache programCache;
187    PathCache pathCache;
188    PatchCache patchCache;
189    TessellationCache tessellationCache;
190    TextDropShadowCache dropShadowCache;
191    FboCache fboCache;
192
193    GammaFontRenderer* fontRenderer;
194
195    TaskManager tasks;
196
197    Dither dither;
198
199    bool gpuPixelBuffersEnabled;
200
201    // Debug methods
202    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
203    PFNGLPUSHGROUPMARKEREXTPROC startMark;
204    PFNGLPOPGROUPMARKEREXTPROC endMark;
205
206    PFNGLLABELOBJECTEXTPROC setLabel;
207    PFNGLGETOBJECTLABELEXTPROC getLabel;
208
209    // TEMPORARY properties
210    void initTempProperties();
211    void setTempProperty(const char* name, const char* value);
212
213    float propertyLightDiameter;
214    float propertyLightPosY;
215    float propertyLightPosZ;
216    float propertyAmbientRatio;
217    int propertyAmbientShadowStrength;
218    int propertySpotShadowStrength;
219
220    void setProgram(const ProgramDescription& description);
221    void setProgram(Program* program);
222
223    Program& program() { return *mProgram; }
224    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
225    TextureState& textureState() { return *mTextureState; }
226
227private:
228    enum OverdrawColorSet {
229        kColorSet_Default = 0,
230        kColorSet_Deuteranomaly
231    };
232
233    void initFont();
234    void initExtensions();
235    void initConstraints();
236    void initStaticProperties();
237
238    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
239    static void startMarkNull(GLsizei length, const GLchar* marker) { }
240    static void endMarkNull() { }
241
242    static void setLabelNull(GLenum type, uint object, GLsizei length,
243            const char* label) { }
244    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
245            GLsizei* length, char* label) {
246        if (length) *length = 0;
247        if (label) *label = '\0';
248    }
249
250    RenderState* mRenderState;
251    Extensions& mExtensions;
252
253    // Used to render layers
254    std::unique_ptr<TextureVertex[]> mRegionMesh;
255
256    mutable Mutex mGarbageLock;
257    Vector<Layer*> mLayerGarbage;
258
259    DebugLevel mDebugLevel;
260    bool mInitialized;
261
262    uint32_t mFunctorsCount;
263
264    OverdrawColorSet mOverdrawDebugColorSet;
265
266    // TODO: move below to RenderState
267    PixelBufferState* mPixelBufferState = nullptr;
268    TextureState* mTextureState = nullptr;
269    Program* mProgram = nullptr; // note: object owned by ProgramCache
270
271}; // class Caches
272
273}; // namespace uirenderer
274}; // namespace android
275
276#endif // ANDROID_HWUI_CACHES_H
277