Caches.h revision 5a4690bf26932c0d6940e4af8516d920e09ae81a
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#include "AssetAtlas.h"
21#include "Dither.h"
22#include "Extensions.h"
23#include "FboCache.h"
24#include "GradientCache.h"
25#include "LayerCache.h"
26#include "PatchCache.h"
27#include "ProgramCache.h"
28#include "PathCache.h"
29#include "RenderBufferCache.h"
30#include "renderstate/PixelBufferState.h"
31#include "renderstate/TextureState.h"
32#include "ResourceCache.h"
33#include "TessellationCache.h"
34#include "TextDropShadowCache.h"
35#include "TextureCache.h"
36#include "thread/TaskProcessor.h"
37#include "thread/TaskManager.h"
38
39#include <vector>
40#include <memory>
41
42#include <GLES3/gl3.h>
43
44#include <utils/KeyedVector.h>
45#include <utils/Singleton.h>
46#include <utils/Vector.h>
47
48#include <cutils/compiler.h>
49
50#include <SkPath.h>
51
52namespace android {
53namespace uirenderer {
54
55class GammaFontRenderer;
56
57///////////////////////////////////////////////////////////////////////////////
58// Caches
59///////////////////////////////////////////////////////////////////////////////
60
61class RenderNode;
62class RenderState;
63
64class ANDROID_API Caches {
65public:
66    static Caches& createInstance(RenderState& renderState) {
67        LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
68        sInstance = new Caches(renderState);
69        return *sInstance;
70    }
71
72    static Caches& getInstance() {
73        LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
74        return *sInstance;
75    }
76
77    static bool hasInstance() {
78        return sInstance != nullptr;
79    }
80private:
81    Caches(RenderState& renderState);
82    static Caches* sInstance;
83
84public:
85    enum FlushMode {
86        kFlushMode_Layers = 0,
87        kFlushMode_Moderate,
88        kFlushMode_Full
89    };
90
91    /**
92     * Initialize caches.
93     */
94    bool init();
95
96    /**
97     * Flush the cache.
98     *
99     * @param mode Indicates how much of the cache should be flushed
100     */
101    void flush(FlushMode mode);
102
103    /**
104     * Destroys all resources associated with this cache. This should
105     * be called after a flush(kFlushMode_Full).
106     */
107    void terminate();
108
109    /**
110     * Returns a non-premultiplied ARGB color for the specified
111     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
112     */
113    uint32_t getOverdrawColor(uint32_t amount) const;
114
115    /**
116     * Call this on each frame to ensure that garbage is deleted from
117     * GPU memory.
118     */
119    void clearGarbage();
120
121    /**
122     * Can be used to delete a layer from a non EGL thread.
123     */
124    void deleteLayerDeferred(Layer* layer);
125
126
127    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
128    void endTiling();
129
130    /**
131     * Returns the mesh used to draw regions. Calling this method will
132     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
133     * indices for the region mesh.
134     */
135    TextureVertex* getRegionMesh();
136
137    /**
138     * Displays the memory usage of each cache and the total sum.
139     */
140    void dumpMemoryUsage();
141    void dumpMemoryUsage(String8& log);
142
143    bool hasRegisteredFunctors();
144    void registerFunctors(uint32_t functorCount);
145    void unregisterFunctors(uint32_t functorCount);
146
147    // Misc
148    GLint maxTextureSize;
149
150private:
151    // Declared before gradientCache and programCache which need this to initialize.
152    // TODO: cleanup / move elsewhere
153    Extensions mExtensions;
154public:
155    TextureCache textureCache;
156    LayerCache layerCache;
157    RenderBufferCache renderBufferCache;
158    GradientCache gradientCache;
159    PatchCache patchCache;
160    PathCache pathCache;
161    ProgramCache programCache;
162    TessellationCache tessellationCache;
163    TextDropShadowCache dropShadowCache;
164    FboCache fboCache;
165
166    GammaFontRenderer* fontRenderer;
167
168    TaskManager tasks;
169
170    Dither dither;
171
172    bool gpuPixelBuffersEnabled;
173
174    // Debug methods
175    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
176    PFNGLPUSHGROUPMARKEREXTPROC startMark;
177    PFNGLPOPGROUPMARKEREXTPROC endMark;
178
179    void setProgram(const ProgramDescription& description);
180    void setProgram(Program* program);
181
182    Extensions& extensions() { return mExtensions; }
183    Program& program() { return *mProgram; }
184    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
185    TextureState& textureState() { return *mTextureState; }
186
187private:
188
189    void initFont();
190    void initExtensions();
191    void initConstraints();
192    void initStaticProperties();
193
194    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
195    static void startMarkNull(GLsizei length, const GLchar* marker) { }
196    static void endMarkNull() { }
197
198    RenderState* mRenderState;
199
200    // Used to render layers
201    std::unique_ptr<TextureVertex[]> mRegionMesh;
202
203    mutable Mutex mGarbageLock;
204    Vector<Layer*> mLayerGarbage;
205
206    bool mInitialized;
207
208    uint32_t mFunctorsCount;
209
210    // TODO: move below to RenderState
211    PixelBufferState* mPixelBufferState = nullptr;
212    TextureState* mTextureState = nullptr;
213    Program* mProgram = nullptr; // note: object owned by ProgramCache
214
215}; // class Caches
216
217}; // namespace uirenderer
218}; // namespace android
219
220#endif // ANDROID_HWUI_CACHES_H
221