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