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