Caches.h revision db009173f800bb0d626c42786b5bd0f57cc6545a
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    bool isInitialized() { return mInitialized; }
96
97    /**
98     * Flush the cache.
99     *
100     * @param mode Indicates how much of the cache should be flushed
101     */
102    void flush(FlushMode mode);
103
104    /**
105     * Destroys all resources associated with this cache. This should
106     * be called after a flush(FlushMode::Full).
107     */
108    void terminate();
109
110    /**
111     * Returns a non-premultiplied ARGB color for the specified
112     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
113     */
114    uint32_t getOverdrawColor(uint32_t amount) const;
115
116    /**
117     * Call this on each frame to ensure that garbage is deleted from
118     * GPU memory.
119     */
120    void clearGarbage();
121
122    /**
123     * Can be used to delete a layer from a non EGL thread.
124     */
125    void deleteLayerDeferred(Layer* layer);
126
127    /**
128     * Returns the mesh used to draw regions. Calling this method will
129     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
130     * indices for the region mesh.
131     */
132    TextureVertex* getRegionMesh();
133
134    /**
135     * Displays the memory usage of each cache and the total sum.
136     */
137    void dumpMemoryUsage();
138    void dumpMemoryUsage(String8& log);
139
140    // Misc
141    GLint maxTextureSize;
142
143private:
144    // Declared before gradientCache and programCache which need this to initialize.
145    // TODO: cleanup / move elsewhere
146    Extensions mExtensions;
147public:
148    TextureCache textureCache;
149    LayerCache layerCache;
150    RenderBufferCache renderBufferCache;
151    GradientCache gradientCache;
152    PatchCache patchCache;
153    PathCache pathCache;
154    ProgramCache programCache;
155    TessellationCache tessellationCache;
156    TextDropShadowCache dropShadowCache;
157    FboCache fboCache;
158
159    GammaFontRenderer fontRenderer;
160
161    TaskManager tasks;
162
163    Dither dither;
164
165    bool gpuPixelBuffersEnabled;
166
167    // Debug methods
168    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
169    PFNGLPUSHGROUPMARKEREXTPROC startMark;
170    PFNGLPOPGROUPMARKEREXTPROC endMark;
171
172    void setProgram(const ProgramDescription& description);
173    void setProgram(Program* program);
174
175    Extensions& extensions() { return mExtensions; }
176    Program& program() { return *mProgram; }
177    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
178    TextureState& textureState() { return *mTextureState; }
179
180private:
181    void initExtensions();
182    void initConstraints();
183    void initStaticProperties();
184
185    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
186    static void startMarkNull(GLsizei length, const GLchar* marker) { }
187    static void endMarkNull() { }
188
189    RenderState* mRenderState;
190
191    // Used to render layers
192    std::unique_ptr<TextureVertex[]> mRegionMesh;
193
194    mutable Mutex mGarbageLock;
195    std::vector<Layer*> mLayerGarbage;
196
197    bool mInitialized;
198
199    // TODO: move below to RenderState
200    PixelBufferState* mPixelBufferState = nullptr;
201    TextureState* mTextureState = nullptr;
202    Program* mProgram = nullptr; // note: object owned by ProgramCache
203
204}; // class Caches
205
206}; // namespace uirenderer
207}; // namespace android
208
209#endif // ANDROID_HWUI_CACHES_H
210