Caches.h revision 149173d28c0843aba86b0810ce75b34be6a0d08f
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
47#include <cutils/compiler.h>
48
49#include <SkPath.h>
50
51#include <vector>
52
53namespace android {
54namespace uirenderer {
55
56class GammaFontRenderer;
57
58///////////////////////////////////////////////////////////////////////////////
59// Caches
60///////////////////////////////////////////////////////////////////////////////
61
62class RenderNode;
63class RenderState;
64
65class ANDROID_API Caches {
66public:
67    static Caches& createInstance(RenderState& renderState) {
68        LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
69        sInstance = new Caches(renderState);
70        return *sInstance;
71    }
72
73    static Caches& getInstance() {
74        LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
75        return *sInstance;
76    }
77
78    static bool hasInstance() {
79        return sInstance != nullptr;
80    }
81private:
82    Caches(RenderState& renderState);
83    static Caches* sInstance;
84
85public:
86    enum FlushMode {
87        kFlushMode_Layers = 0,
88        kFlushMode_Moderate,
89        kFlushMode_Full
90    };
91
92    /**
93     * Initialize caches.
94     */
95    bool init();
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(kFlushMode_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
182    void initFont();
183    void initExtensions();
184    void initConstraints();
185    void initStaticProperties();
186
187    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
188    static void startMarkNull(GLsizei length, const GLchar* marker) { }
189    static void endMarkNull() { }
190
191    RenderState* mRenderState;
192
193    // Used to render layers
194    std::unique_ptr<TextureVertex[]> mRegionMesh;
195
196    mutable Mutex mGarbageLock;
197    std::vector<Layer*> mLayerGarbage;
198
199    bool mInitialized;
200
201    // TODO: move below to RenderState
202    PixelBufferState* mPixelBufferState = nullptr;
203    TextureState* mTextureState = nullptr;
204    Program* mProgram = nullptr; // note: object owned by ProgramCache
205
206}; // class Caches
207
208}; // namespace uirenderer
209}; // namespace android
210
211#endif // ANDROID_HWUI_CACHES_H
212