• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GPU_CONTEXT_H
17 #define GPU_CONTEXT_H
18 
19 #include "impl_interface/gpu_context_impl.h"
20 #include "utils/drawing_macros.h"
21 #include "utils/data.h"
22 #include "trace_memory_dump.h"
23 
24 typedef void* EGLContext;
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 enum class PathRenderers : uint32_t {
29     NONE              = 0,
30     DASHLINE          = 1 << 0,
31     STENCILANDCOVER   = 1 << 1,
32     COVERAGECOUNTING  = 1 << 2,
33     AAHAIRLINE        = 1 << 3,
34     AACONVEX          = 1 << 4,
35     AALINEARIZING     = 1 << 5,
36     SMALL             = 1 << 6,
37     TESSELLATING      = 1 << 7,
38 
39     ALL               = (TESSELLATING | (TESSELLATING - 1)),
40     DEFAULT           = ALL & ~COVERAGECOUNTING
41 };
42 
43 struct GPUResourceTag {
GPUResourceTagGPUResourceTag44     GPUResourceTag()
45         : fPid(0), fTid(0), fWid(0), fFid(0) {}
GPUResourceTagGPUResourceTag46     GPUResourceTag(uint32_t pid, uint32_t tid, uint32_t wid, uint32_t fid)
47         : fPid(pid), fTid(tid), fWid(wid), fFid(fid) {}
48     uint32_t fPid;
49     uint32_t fTid;
50     uint32_t fWid;
51     uint32_t fFid;
52 };
53 
54 /*
55  * @brief  Option to create a GPUContext. Currently only supports setting persistent cache,
56            other options may be expanded in the future
57  */
58 class DRAWING_API GPUContextOptions {
59 public:
60     /*
61      * @brief  Cache compiled shaders for use between sessions.
62      */
63     class PersistentCache {
64     public:
65         PersistentCache() = default;
66         virtual ~PersistentCache() = default;
67 
68         /*
69          * @brief  Returns the data for the key if it exists in the cache.
70          */
71         virtual std::shared_ptr<Data> Load(const Data& key) = 0;
72 
73         /*
74          * @brief  Stores the data and key.
75          */
76         virtual void Store(const Data& key, const Data& data) = 0;
77     };
78 
79     /*
80      * @brief  Gets persistent cache object.
81      */
82     PersistentCache* GetPersistentCache() const;
83     /*
84      * @brief                  Sets persistent cache object.
85      * @param persistentCache  A pointer to persistent cache object.
86      */
87     void SetPersistentCache(PersistentCache* persistentCache);
88 
89     void SetAllowPathMaskCaching(bool allowPathMaskCaching);
90     bool GetAllowPathMaskCaching() const;
91 
92 private:
93     PersistentCache* persistentCache_ = nullptr;
94     bool allowPathMaskCaching_ = true;
95 };
96 
97 class DRAWING_API GPUContext {
98 public:
99     GPUContext();
~GPUContext()100     ~GPUContext() {}
101 
102     /*
103      * @brief           Creates a GL GPUContext for a backend context.
104      * @param options   Option to create a GL GPUContext.
105      */
106     bool BuildFromGL(const GPUContextOptions& options);
107 
108    /*
109      * @brief           Creates a VK GPUContext for a backend context.
110      * @param options   Option to create a VK GPUContext.
111      */
112 #ifdef RS_ENABLE_VK
113     bool BuildFromVK(const GrVkBackendContext& context);
114     bool BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options);
115 #endif
116 
117     /*
118      * @brief   Call to ensure all drawing to the context has been flushed to underlying 3D API specific objects.
119      */
120     void Flush();
121 
122     /*
123      * @brief   Call to ensure all drawing to the context has been submitted to underlying 3D API.
124      */
125     void Submit();
126 
127     /*
128      * @brief   Call to ensure all drawing to the context has been flushed and submitted to underlying 3D API.
129      */
130     void FlushAndSubmit(bool syncCpu = false);
131 
132     /*
133      * @brief             Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds
134                           or are otherwise marked for deletion.
135      * @param msNotUsed   Only unlocked resources not used in these last milliseconds will be cleaned up.
136      */
137     void PerformDeferredCleanup(std::chrono::milliseconds msNotUsed);
138 
139     /*
140      * @brief                   Gets the current GPU resource cache limits.
141      * @param maxResource       If non-null, returns maximum number of resources that can be held in the cache.
142      * @param maxResourceBytes  If non-null, returns maximum number of bytes of video memory
143                                 that can be held in the cache.
144      */
145     void GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const;
146 
147     /*
148      * @brief                   Specify the GPU resource cache limits.
149      * @param maxResource       The maximum number of resources that can be held in the cache.
150      * @param maxResourceBytes  The maximum number of bytes of video memory that can be held in the cache.
151      */
152     void SetResourceCacheLimits(int maxResource, size_t maxResourceBytes);
153 
154     /*
155      * @brief                   Gets the current GPU resource cache usage.
156      * @param resourceCount     If non-null, returns the number of resources that are held in the cache.
157      * @param resourceBytes     If non-null, returns the total number of bytes of video memory held in the cache.
158      */
159     void GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
160 
161     /*
162      * @brief                   Free GPU created by the contetx.
163      */
164     void FreeGpuResources();
165 
166     /*
167      * @brief                   Dump GPU stats.
168      * @param out               Dump GPU stat string.
169      */
170     void DumpGpuStats(std::string& out) const;
171 
172     /*
173      * @brief                   After returning it will assume that the underlying context may no longer be valid.
174      */
175     void ReleaseResourcesAndAbandonContext();
176 
177     /*
178      * @brief                   Purge unlocked resources from the cache until
179      *                          the provided byte count has been reached or we have purged all unlocked resources.
180      */
181     void PurgeUnlockedResources(bool scratchResourcesOnly);
182 
183     /*
184      * @brief                   Purge unlocked resources by tag from the cache until
185      *                          the provided byte count has been reached or we have purged all unlocked resources.
186      */
187     void PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag);
188 
189     /*
190      * @brief                   Purge unlocked resources from the safe cache until
191      *                          the provided byte count has been reached or we have purged all unlocked resources.
192      */
193     void PurgeUnlockAndSafeCacheGpuResources();
194 
195     /*
196      * @brief                   Releases GPUResource objects and removes them from the cache by tag.
197      */
198     void ReleaseByTag(const GPUResourceTag &tag);
199 
200     /*
201      * @brief                   Enumerates all cached GPU resources and dumps their memory to traceMemoryDump.
202      */
203     void DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const;
204 
205     /*
206      * @brief                   Enumerates all cached GPU resources and dumps their memory to traceMemoryDump.
207      */
208     void DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) const;
209 
210     /*
211      * @brief                   Set current resource tag for gpu cache recycle.
212      */
213     void SetCurrentGpuResourceTag(const GPUResourceTag &tag);
214 
215     /*
216      * @brief                   Store vulkan pipeline cache
217     */
218 #ifdef RS_ENABLE_VK
219     void StoreVkPipelineCacheData();
220 #endif
221 
222     /*
223      * @brief   Get the adaptation layer instance, called in the adaptation layer.
224      * @return  Adaptation Layer instance.
225      */
226     template<typename T>
GetImpl()227     T* GetImpl() const
228     {
229         return impl_->DowncastingTo<T>();
230     }
231 private:
232     std::shared_ptr<GPUContextImpl> impl_;
233 };
234 } // namespace Drawing
235 } // namespace Rosen
236 } // namespace OHOS
237 
238 #endif // !GPU_CONTEXT_H
239