Caches.h revision fb10ffe484b04a77c3aaf8a78ed87d018a6edcda
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;
181private:
182    // Declared before gradientCache and programCache which need this to initialize.
183    // TODO: cleanup / move elsewhere
184    Extensions mExtensions;
185public:
186    TextureCache textureCache;
187    LayerCache layerCache;
188    RenderBufferCache renderBufferCache;
189    GradientCache gradientCache;
190    PatchCache patchCache;
191    PathCache pathCache;
192    ProgramCache programCache;
193    TessellationCache tessellationCache;
194    TextDropShadowCache dropShadowCache;
195    FboCache fboCache;
196
197    GammaFontRenderer* fontRenderer;
198
199    TaskManager tasks;
200
201    Dither dither;
202
203    bool gpuPixelBuffersEnabled;
204
205    // Debug methods
206    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
207    PFNGLPUSHGROUPMARKEREXTPROC startMark;
208    PFNGLPOPGROUPMARKEREXTPROC endMark;
209
210    // TEMPORARY properties
211    void initTempProperties();
212    void setTempProperty(const char* name, const char* value);
213
214    float propertyLightRadius;
215    float propertyLightPosY;
216    float propertyLightPosZ;
217    float propertyAmbientRatio;
218    int propertyAmbientShadowStrength;
219    int propertySpotShadowStrength;
220
221    void setProgram(const ProgramDescription& description);
222    void setProgram(Program* program);
223
224    Extensions& extensions() { return mExtensions; }
225    Program& program() { return *mProgram; }
226    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
227    TextureState& textureState() { return *mTextureState; }
228
229private:
230    enum OverdrawColorSet {
231        kColorSet_Default = 0,
232        kColorSet_Deuteranomaly
233    };
234
235    void initFont();
236    void initExtensions();
237    void initConstraints();
238    void initStaticProperties();
239
240    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
241    static void startMarkNull(GLsizei length, const GLchar* marker) { }
242    static void endMarkNull() { }
243
244    RenderState* mRenderState;
245
246    // Used to render layers
247    std::unique_ptr<TextureVertex[]> mRegionMesh;
248
249    mutable Mutex mGarbageLock;
250    Vector<Layer*> mLayerGarbage;
251
252    DebugLevel mDebugLevel;
253    bool mInitialized;
254
255    uint32_t mFunctorsCount;
256
257    OverdrawColorSet mOverdrawDebugColorSet;
258
259    // TODO: move below to RenderState
260    PixelBufferState* mPixelBufferState = nullptr;
261    TextureState* mTextureState = nullptr;
262    Program* mProgram = nullptr; // note: object owned by ProgramCache
263
264}; // class Caches
265
266}; // namespace uirenderer
267}; // namespace android
268
269#endif // ANDROID_HWUI_CACHES_H
270