Caches.h revision c08820f587ad94698691a6657e87712de07e484c
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#include <utils/Singleton.h>
47
48#include <cutils/compiler.h>
49
50#include <SkPath.h>
51
52#include <vector>
53
54namespace android {
55namespace uirenderer {
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 class FlushMode {
86        Layers = 0,
87        Moderate,
88        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(FlushMode::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     * Returns the mesh used to draw regions. Calling this method will
128     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
129     * indices for the region mesh.
130     */
131    TextureVertex* getRegionMesh();
132
133    /**
134     * Displays the memory usage of each cache and the total sum.
135     */
136    void dumpMemoryUsage();
137    void dumpMemoryUsage(String8& log);
138
139    // Misc
140    GLint maxTextureSize;
141
142private:
143    // Declared before gradientCache and programCache which need this to initialize.
144    // TODO: cleanup / move elsewhere
145    Extensions mExtensions;
146public:
147    TextureCache textureCache;
148    LayerCache layerCache;
149    RenderBufferCache renderBufferCache;
150    GradientCache gradientCache;
151    PatchCache patchCache;
152    PathCache pathCache;
153    ProgramCache programCache;
154    TessellationCache tessellationCache;
155    TextDropShadowCache dropShadowCache;
156    FboCache fboCache;
157
158    GammaFontRenderer fontRenderer;
159
160    TaskManager tasks;
161
162    Dither dither;
163
164    bool gpuPixelBuffersEnabled;
165
166    // Debug methods
167    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
168    PFNGLPUSHGROUPMARKEREXTPROC startMark;
169    PFNGLPOPGROUPMARKEREXTPROC endMark;
170
171    void setProgram(const ProgramDescription& description);
172    void setProgram(Program* program);
173
174    Extensions& extensions() { return mExtensions; }
175    Program& program() { return *mProgram; }
176    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
177    TextureState& textureState() { return *mTextureState; }
178
179private:
180    void initExtensions();
181    void initConstraints();
182    void initStaticProperties();
183
184    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
185    static void startMarkNull(GLsizei length, const GLchar* marker) { }
186    static void endMarkNull() { }
187
188    RenderState* mRenderState;
189
190    // Used to render layers
191    std::unique_ptr<TextureVertex[]> mRegionMesh;
192
193    mutable Mutex mGarbageLock;
194    std::vector<Layer*> mLayerGarbage;
195
196    bool mInitialized;
197
198    // TODO: move below to RenderState
199    PixelBufferState* mPixelBufferState = nullptr;
200    TextureState* mTextureState = nullptr;
201    Program* mProgram = nullptr; // note: object owned by ProgramCache
202
203}; // class Caches
204
205}; // namespace uirenderer
206}; // namespace android
207
208#endif // ANDROID_HWUI_CACHES_H
209