• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include "DeviceInfo.h"
20 #include "Extensions.h"
21 #include "FboCache.h"
22 #include "GammaFontRenderer.h"
23 #include "GradientCache.h"
24 #include "PatchCache.h"
25 #include "ProgramCache.h"
26 #include "PathCache.h"
27 #include "RenderBufferCache.h"
28 #include "renderstate/PixelBufferState.h"
29 #include "renderstate/TextureState.h"
30 #include "ResourceCache.h"
31 #include "TessellationCache.h"
32 #include "TextDropShadowCache.h"
33 #include "TextureCache.h"
34 #include "thread/TaskProcessor.h"
35 #include "thread/TaskManager.h"
36 
37 #include <vector>
38 #include <memory>
39 
40 #include <GLES3/gl3.h>
41 
42 #include <utils/KeyedVector.h>
43 
44 #include <cutils/compiler.h>
45 
46 #include <SkPath.h>
47 
48 #include <vector>
49 
50 namespace android {
51 namespace uirenderer {
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 // Caches
55 ///////////////////////////////////////////////////////////////////////////////
56 
57 class RenderNode;
58 class RenderState;
59 
60 class ANDROID_API Caches {
61 public:
createInstance(RenderState & renderState)62     static Caches& createInstance(RenderState& renderState) {
63         LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
64         sInstance = new Caches(renderState);
65         return *sInstance;
66     }
67 
getInstance()68     static Caches& getInstance() {
69         LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
70         return *sInstance;
71     }
72 
hasInstance()73     static bool hasInstance() {
74         return sInstance != nullptr;
75     }
76 private:
77     explicit Caches(RenderState& renderState);
78     static Caches* sInstance;
79 
80 public:
81     enum class FlushMode {
82         Layers = 0,
83         Moderate,
84         Full
85     };
86 
87     /**
88      * Initialize caches.
89      */
90     bool init();
91 
isInitialized()92     bool isInitialized() { return mInitialized; }
93 
94     /**
95      * Flush the cache.
96      *
97      * @param mode Indicates how much of the cache should be flushed
98      */
99     void flush(FlushMode mode);
100 
101     /**
102      * Destroys all resources associated with this cache. This should
103      * be called after a flush(FlushMode::Full).
104      */
105     void terminate();
106 
107     /**
108      * Returns a non-premultiplied ARGB color for the specified
109      * amount of overdraw (1 for 1x, 2 for 2x, etc.)
110      */
111     uint32_t getOverdrawColor(uint32_t amount) const;
112 
113     /**
114      * Call this on each frame to ensure that garbage is deleted from
115      * GPU memory.
116      */
117     void clearGarbage();
118 
119     /**
120      * Can be used to delete a layer from a non EGL thread.
121      */
122     void deleteLayerDeferred(Layer* layer);
123 
124     /**
125      * Returns the mesh used to draw regions. Calling this method will
126      * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
127      * indices for the region mesh.
128      */
129     TextureVertex* getRegionMesh();
130 
131     /**
132      * Returns the GL RGBA internal format to use for the current device
133      * If the device supports linear blending and needSRGB is true,
134      * this function returns GL_SRGB8_ALPHA8, otherwise it returns GL_RGBA
135      */
136     constexpr GLint rgbaInternalFormat(bool needSRGB = true) const {
137         return extensions().hasLinearBlending() && needSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA;
138     }
139 
140     /**
141      * Displays the memory usage of each cache and the total sum.
142      */
143     void dumpMemoryUsage();
144     void dumpMemoryUsage(String8& log);
145 
146     // Misc
147     GLint maxTextureSize;
148 
149 public:
150     TextureCache textureCache;
151     RenderBufferCache renderBufferCache;
152     GradientCache gradientCache;
153     PatchCache patchCache;
154     PathCache pathCache;
155     ProgramCache programCache;
156     TessellationCache tessellationCache;
157     TextDropShadowCache dropShadowCache;
158     FboCache fboCache;
159 
160     GammaFontRenderer fontRenderer;
161 
162     TaskManager tasks;
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 
extensions()174     const Extensions& extensions() const { return DeviceInfo::get()->extensions(); }
program()175     Program& program() { return *mProgram; }
pixelBufferState()176     PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
textureState()177     TextureState& textureState() { return *mTextureState; }
178 
179 private:
180     void initExtensions();
181     void initConstraints();
182     void initStaticProperties();
183 
eventMarkNull(GLsizei length,const GLchar * marker)184     static void eventMarkNull(GLsizei length, const GLchar* marker) { }
startMarkNull(GLsizei length,const GLchar * marker)185     static void startMarkNull(GLsizei length, const GLchar* marker) { }
endMarkNull()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