• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #include "image/gpu_context.h"
17 
18 #include "config/DrawingConfig.h"
19 #include "impl_factory.h"
20 #include "static_factory.h"
21 #ifdef RS_ENABLE_VK
22 #ifdef USE_M133_SKIA
23 #include "include/gpu/vk/VulkanBackendContext.h"
24 #else
25 #include "include/gpu/vk/GrVkBackendContext.h"
26 #endif
27 #endif
28 #include "utils/system_properties.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33 
SetCurrentNodeId(uint64_t nodeId)34 void GPUResourceTag::SetCurrentNodeId(uint64_t nodeId)
35 {
36 #ifdef ROSEN_OHOS
37     if (SystemProperties::IsVkImageDfxEnabled()) {
38         StaticFactory::SetCurrentNodeId(nodeId);
39     }
40 #endif
41 }
42 
GPUContext()43 GPUContext::GPUContext() : impl_(ImplFactory::CreateGPUContextImpl()) {}
44 
BuildFromGL(const GPUContextOptions & options)45 bool GPUContext::BuildFromGL(const GPUContextOptions& options)
46 {
47     return impl_->BuildFromGL(options);
48 }
49 
50 #ifdef RS_ENABLE_VK
51 #ifdef USE_M133_SKIA
BuildFromVK(const skgpu::VulkanBackendContext & context)52 bool GPUContext::BuildFromVK(const skgpu::VulkanBackendContext& context)
53 #else
54 bool GPUContext::BuildFromVK(const GrVkBackendContext& context)
55 #endif
56 {
57     if (!SystemProperties::IsUseVulkan()) {
58         return false;
59     }
60     return impl_->BuildFromVK(context);
61 }
62 
63 #ifdef USE_M133_SKIA
BuildFromVK(const skgpu::VulkanBackendContext & context,const GPUContextOptions & options)64 bool GPUContext::BuildFromVK(const skgpu::VulkanBackendContext& context, const GPUContextOptions& options)
65 #else
66 bool GPUContext::BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options)
67 #endif
68 {
69     if (!SystemProperties::IsUseVulkan()) {
70         return false;
71     }
72     return impl_->BuildFromVK(context, options);
73 }
74 #endif
75 
GetResourceCacheLimits(int * maxResource,size_t * maxResourceBytes) const76 void GPUContext::GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const
77 {
78     if (impl_ != nullptr) {
79         impl_->GetResourceCacheLimits(maxResource, maxResourceBytes);
80     }
81 }
82 
SetResourceCacheLimits(int maxResource,size_t maxResourceBytes)83 void GPUContext::SetResourceCacheLimits(int maxResource, size_t maxResourceBytes)
84 {
85     impl_->SetResourceCacheLimits(maxResource, maxResourceBytes);
86 }
87 
SetPurgeableResourceLimit(int purgeableMaxCount)88 void GPUContext::SetPurgeableResourceLimit(int purgeableMaxCount)
89 {
90     if (impl_) {
91         impl_->SetPurgeableResourceLimit(purgeableMaxCount);
92     }
93 }
94 
Flush()95 void GPUContext::Flush()
96 {
97     impl_->Flush();
98 }
99 
FlushAndSubmit(bool syncCpu)100 void GPUContext::FlushAndSubmit(bool syncCpu)
101 {
102     impl_->FlushAndSubmit(syncCpu);
103 #ifdef DRAWING_DISABLE_API
104     DrawingConfig::UpdateDrawingProperties();
105 #endif
106 }
107 
Submit()108 void GPUContext::Submit()
109 {
110     impl_->Submit();
111 }
112 
PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)113 void GPUContext::PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)
114 {
115     impl_->PerformDeferredCleanup(msNotUsed);
116 }
117 
SetPersistentCache(PersistentCache * persistentCache)118 void GPUContextOptions::SetPersistentCache(PersistentCache* persistentCache)
119 {
120     persistentCache_ = persistentCache;
121 }
122 
SetAllowPathMaskCaching(bool allowPathMaskCaching)123 void GPUContextOptions::SetAllowPathMaskCaching(bool allowPathMaskCaching)
124 {
125     allowPathMaskCaching_ = allowPathMaskCaching;
126 }
127 
GetAllowPathMaskCaching() const128 bool GPUContextOptions::GetAllowPathMaskCaching() const
129 {
130     return allowPathMaskCaching_;
131 }
132 
GetResourceCacheUsage(int * resourceCount,size_t * resourceBytes) const133 void GPUContext::GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const
134 {
135     impl_->GetResourceCacheUsage(resourceCount, resourceBytes);
136 }
137 
FreeGpuResources()138 void GPUContext::FreeGpuResources()
139 {
140     impl_->FreeGpuResources();
141 }
142 
ReclaimResources()143 void GPUContext::ReclaimResources()
144 {
145     impl_->ReclaimResources();
146 }
147 
DumpAllResource(std::stringstream & dump) const148 void GPUContext::DumpAllResource(std::stringstream& dump) const
149 {
150 #ifdef ROSEN_OHOS
151     if (SystemProperties::IsVkImageDfxEnabled()) {
152         impl_->DumpAllResource(dump);
153     }
154 #endif
155 }
156 
DumpGpuStats(std::string & out) const157 void GPUContext::DumpGpuStats(std::string& out) const
158 {
159     impl_->DumpGpuStats(out);
160 }
161 
ReleaseResourcesAndAbandonContext()162 void GPUContext::ReleaseResourcesAndAbandonContext()
163 {
164     impl_->ReleaseResourcesAndAbandonContext();
165 }
166 
PurgeUnlockedResources(bool scratchResourcesOnly)167 void GPUContext::PurgeUnlockedResources(bool scratchResourcesOnly)
168 {
169     impl_->PurgeUnlockedResources(scratchResourcesOnly);
170 }
171 
PurgeUnlockedResourcesByTag(bool scratchResourcesOnly,const GPUResourceTag & tag)172 void GPUContext::PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag)
173 {
174     impl_->PurgeUnlockedResourcesByTag(scratchResourcesOnly, tag);
175 }
176 
PurgeUnlockedResourcesByPid(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet)177 void GPUContext::PurgeUnlockedResourcesByPid(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet)
178 {
179     impl_->PurgeUnlockedResourcesByPid(scratchResourcesOnly, exitedPidSet);
180 }
181 
RegisterVulkanErrorCallback(const std::function<void ()> & vulkanErrorCallback)182 void GPUContext::RegisterVulkanErrorCallback(const std::function<void()>& vulkanErrorCallback)
183 {
184     impl_->RegisterVulkanErrorCallback(vulkanErrorCallback);
185 }
186 
PurgeUnlockAndSafeCacheGpuResources()187 void GPUContext::PurgeUnlockAndSafeCacheGpuResources()
188 {
189     impl_->PurgeUnlockAndSafeCacheGpuResources();
190 }
PurgeCacheBetweenFrames(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet,const std::set<pid_t> & protectedPidSet)191 void GPUContext::PurgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet,
192     const std::set<pid_t>& protectedPidSet)
193 {
194     impl_->PurgeCacheBetweenFrames(scratchResourcesOnly, exitedPidSet, protectedPidSet);
195 }
196 
ReleaseByTag(const GPUResourceTag & tag)197 void GPUContext::ReleaseByTag(const GPUResourceTag &tag)
198 {
199     impl_->ReleaseByTag(tag);
200 }
201 
DumpMemoryStatisticsByTag(TraceMemoryDump * traceMemoryDump,GPUResourceTag & tag) const202 void GPUContext::DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const
203 {
204     impl_->DumpMemoryStatisticsByTag(traceMemoryDump, tag);
205 }
206 
NewDumpMemoryStatisticsByTag(TraceMemoryDump * traceMemoryDump,GPUResourceTag & tag) const207 uint64_t GPUContext::NewDumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const
208 {
209     return impl_->NewDumpMemoryStatisticsByTag(traceMemoryDump, tag);
210 }
211 
DumpMemoryStatistics(TraceMemoryDump * traceMemoryDump) const212 void GPUContext::DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) const
213 {
214     impl_->DumpMemoryStatistics(traceMemoryDump);
215 }
216 
SetCurrentGpuResourceTag(const GPUResourceTag & tag)217 void GPUContext::SetCurrentGpuResourceTag(const GPUResourceTag &tag)
218 {
219     impl_->SetCurrentGpuResourceTag(tag);
220 }
221 
GetCurrentGpuResourceTag() const222 GPUResourceTag GPUContext::GetCurrentGpuResourceTag() const
223 {
224     return impl_->GetCurrentGpuResourceTag();
225 }
226 
GetUpdatedMemoryMap(std::unordered_map<pid_t,size_t> & out)227 void GPUContext::GetUpdatedMemoryMap(std::unordered_map<pid_t, size_t> &out)
228 {
229     impl_->GetUpdatedMemoryMap(out);
230 }
231 
InitGpuMemoryLimit(MemoryOverflowCalllback callback,uint64_t size)232 void GPUContext::InitGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size)
233 {
234     impl_->InitGpuMemoryLimit(callback, size);
235 }
236 
ResetContext()237 void GPUContext::ResetContext()
238 {
239     impl_->ResetContext();
240 }
241 
242 // subtree parallel feature interface
243 // generate submit information
GenerateSubmitInfo(int seq)244 void GPUContext::GenerateSubmitInfo(int seq)
245 {
246     impl_->GenerateSubmitInfo(seq);
247 }
248 
249 // subtree parallel feature interface
250 // generate draw op
FlushCommands()251 void GPUContext::FlushCommands()
252 {
253     impl_->FlushCommands();
254 }
255 
256 #ifdef RS_ENABLE_VK
StoreVkPipelineCacheData()257 void GPUContext::StoreVkPipelineCacheData()
258 {
259     impl_->StoreVkPipelineCacheData();
260 }
261 #endif
262 
RegisterPostFunc(const std::function<void (const std::function<void ()> & task)> & func)263 void GPUContext::RegisterPostFunc(const std::function<void(const std::function<void()>& task)>& func)
264 {
265     impl_->RegisterPostFunc(func);
266 }
267 
GetPersistentCache() const268 GPUContextOptions::PersistentCache* GPUContextOptions::GetPersistentCache() const
269 {
270     return persistentCache_;
271 }
272 
VmaDefragment()273 void GPUContext::VmaDefragment()
274 {
275     impl_->VmaDefragment();
276 }
277 
BeginFrame()278 void GPUContext::BeginFrame()
279 {
280     impl_->BeginFrame();
281 }
282 
EndFrame()283 void GPUContext::EndFrame()
284 {
285     impl_->EndFrame();
286 }
287 
SetGpuCacheSuppressWindowSwitch(bool enabled)288 void GPUContext::SetGpuCacheSuppressWindowSwitch(bool enabled)
289 {
290     impl_->SetGpuCacheSuppressWindowSwitch(enabled);
291 }
292 
SetGpuMemoryAsyncReclaimerSwitch(bool enabled,const std::function<void ()> & setThreadPriority)293 void GPUContext::SetGpuMemoryAsyncReclaimerSwitch(bool enabled, const std::function<void()>& setThreadPriority)
294 {
295     impl_->SetGpuMemoryAsyncReclaimerSwitch(enabled, setThreadPriority);
296 }
297 
FlushGpuMemoryInWaitQueue()298 void GPUContext::FlushGpuMemoryInWaitQueue()
299 {
300     impl_->FlushGpuMemoryInWaitQueue();
301 }
302 
SetEarlyZEnabled(bool flag)303 void GPUContext::SetEarlyZEnabled(bool flag)
304 {
305     impl_->SetEarlyZEnabled(flag);
306 }
307 
SuppressGpuCacheBelowCertainRatio(const std::function<bool (void)> & nextFrameHasArrived)308 void GPUContext::SuppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived)
309 {
310     impl_->SuppressGpuCacheBelowCertainRatio(nextFrameHasArrived);
311 }
312 
GetHpsEffectSupport(std::vector<const char * > & instanceExtensions)313 void GPUContext::GetHpsEffectSupport(std::vector<const char*>& instanceExtensions)
314 {
315     impl_->GetHpsEffectSupport(instanceExtensions);
316 }
317 
SetStoreCachePath(const std::string & filePath)318 void GPUContextOptions::SetStoreCachePath(const std::string& filePath)
319 {
320     filePath_ = filePath;
321 }
322 
GetStoreCachePath() const323 std::string GPUContextOptions::GetStoreCachePath() const
324 {
325     return filePath_;
326 }
327 
SetIsUniRender(bool isUniRender)328 void GPUContextOptions::SetIsUniRender(bool isUniRender)
329 {
330     isUniRender_ = isUniRender;
331 }
332 
GetIsUniRender() const333 bool GPUContextOptions::GetIsUniRender() const
334 {
335     return isUniRender_;
336 }
337 } // namespace Drawing
338 } // namespace Rosen
339 } // namespace OHOS
340