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 #include "image/gpu_context.h"
17
18 #include "impl_factory.h"
19 #ifdef RS_ENABLE_VK
20 #include "include/gpu/vk/GrVkBackendContext.h"
21 #endif
22 #include "utils/system_properties.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
GPUContext()27 GPUContext::GPUContext() : impl_(ImplFactory::CreateGPUContextImpl()) {}
28
BuildFromGL(const GPUContextOptions & options)29 bool GPUContext::BuildFromGL(const GPUContextOptions& options)
30 {
31 return impl_->BuildFromGL(options);
32 }
33
34 #ifdef RS_ENABLE_VK
BuildFromVK(const GrVkBackendContext & context)35 bool GPUContext::BuildFromVK(const GrVkBackendContext& context)
36 {
37 if (!SystemProperties::IsUseVulkan()) {
38 return false;
39 }
40 return impl_->BuildFromVK(context);
41 }
42
BuildFromVK(const GrVkBackendContext & context,const GPUContextOptions & options)43 bool GPUContext::BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options)
44 {
45 if (!SystemProperties::IsUseVulkan()) {
46 return false;
47 }
48 return impl_->BuildFromVK(context, options);
49 }
50 #endif
51
GetResourceCacheLimits(int * maxResource,size_t * maxResourceBytes) const52 void GPUContext::GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const
53 {
54 if (impl_ != nullptr) {
55 impl_->GetResourceCacheLimits(maxResource, maxResourceBytes);
56 }
57 }
58
SetResourceCacheLimits(int maxResource,size_t maxResourceBytes)59 void GPUContext::SetResourceCacheLimits(int maxResource, size_t maxResourceBytes)
60 {
61 impl_->SetResourceCacheLimits(maxResource, maxResourceBytes);
62 }
63
SetPurgeableResourceLimit(int purgeableMaxCount)64 void GPUContext::SetPurgeableResourceLimit(int purgeableMaxCount)
65 {
66 if (impl_) {
67 impl_->SetPurgeableResourceLimit(purgeableMaxCount);
68 }
69 }
70
Flush()71 void GPUContext::Flush()
72 {
73 impl_->Flush();
74 }
75
FlushAndSubmit(bool syncCpu)76 void GPUContext::FlushAndSubmit(bool syncCpu)
77 {
78 impl_->FlushAndSubmit(syncCpu);
79 }
80
Submit()81 void GPUContext::Submit()
82 {
83 impl_->Submit();
84 }
85
PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)86 void GPUContext::PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)
87 {
88 impl_->PerformDeferredCleanup(msNotUsed);
89 }
90
SetPersistentCache(PersistentCache * persistentCache)91 void GPUContextOptions::SetPersistentCache(PersistentCache* persistentCache)
92 {
93 persistentCache_ = persistentCache;
94 }
95
SetAllowPathMaskCaching(bool allowPathMaskCaching)96 void GPUContextOptions::SetAllowPathMaskCaching(bool allowPathMaskCaching)
97 {
98 allowPathMaskCaching_ = allowPathMaskCaching;
99 }
100
GetAllowPathMaskCaching() const101 bool GPUContextOptions::GetAllowPathMaskCaching() const
102 {
103 return allowPathMaskCaching_;
104 }
105
GetResourceCacheUsage(int * resourceCount,size_t * resourceBytes) const106 void GPUContext::GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const
107 {
108 impl_->GetResourceCacheUsage(resourceCount, resourceBytes);
109 }
110
FreeGpuResources()111 void GPUContext::FreeGpuResources()
112 {
113 impl_->FreeGpuResources();
114 }
115
DumpAllResource(std::stringstream & dump) const116 void GPUContext::DumpAllResource(std::stringstream& dump) const
117 {
118 impl_->DumpAllResource(dump);
119 }
120
DumpGpuStats(std::string & out) const121 void GPUContext::DumpGpuStats(std::string& out) const
122 {
123 impl_->DumpGpuStats(out);
124 }
125
ReleaseResourcesAndAbandonContext()126 void GPUContext::ReleaseResourcesAndAbandonContext()
127 {
128 impl_->ReleaseResourcesAndAbandonContext();
129 }
130
PurgeUnlockedResources(bool scratchResourcesOnly)131 void GPUContext::PurgeUnlockedResources(bool scratchResourcesOnly)
132 {
133 impl_->PurgeUnlockedResources(scratchResourcesOnly);
134 }
135
PurgeUnlockedResourcesByTag(bool scratchResourcesOnly,const GPUResourceTag & tag)136 void GPUContext::PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag)
137 {
138 impl_->PurgeUnlockedResourcesByTag(scratchResourcesOnly, tag);
139 }
140
PurgeUnlockedResourcesByPid(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet)141 void GPUContext::PurgeUnlockedResourcesByPid(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet)
142 {
143 impl_->PurgeUnlockedResourcesByPid(scratchResourcesOnly, exitedPidSet);
144 }
145
RegisterVulkanErrorCallback(const std::function<void ()> & vulkanErrorCallback)146 void GPUContext::RegisterVulkanErrorCallback(const std::function<void()>& vulkanErrorCallback)
147 {
148 impl_->RegisterVulkanErrorCallback(vulkanErrorCallback);
149 }
150
PurgeUnlockAndSafeCacheGpuResources()151 void GPUContext::PurgeUnlockAndSafeCacheGpuResources()
152 {
153 impl_->PurgeUnlockAndSafeCacheGpuResources();
154 }
PurgeCacheBetweenFrames(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet,const std::set<pid_t> & protectedPidSet)155 void GPUContext::PurgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet,
156 const std::set<pid_t>& protectedPidSet)
157 {
158 impl_->PurgeCacheBetweenFrames(scratchResourcesOnly, exitedPidSet, protectedPidSet);
159 }
160
ReleaseByTag(const GPUResourceTag & tag)161 void GPUContext::ReleaseByTag(const GPUResourceTag &tag)
162 {
163 impl_->ReleaseByTag(tag);
164 }
165
DumpMemoryStatisticsByTag(TraceMemoryDump * traceMemoryDump,GPUResourceTag & tag) const166 void GPUContext::DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const
167 {
168 impl_->DumpMemoryStatisticsByTag(traceMemoryDump, tag);
169 }
170
DumpMemoryStatistics(TraceMemoryDump * traceMemoryDump) const171 void GPUContext::DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) const
172 {
173 impl_->DumpMemoryStatistics(traceMemoryDump);
174 }
175
SetCurrentGpuResourceTag(const GPUResourceTag & tag)176 void GPUContext::SetCurrentGpuResourceTag(const GPUResourceTag &tag)
177 {
178 impl_->SetCurrentGpuResourceTag(tag);
179 }
180
GetUpdatedMemoryMap(std::unordered_map<pid_t,size_t> & out)181 void GPUContext::GetUpdatedMemoryMap(std::unordered_map<pid_t, size_t> &out)
182 {
183 impl_->GetUpdatedMemoryMap(out);
184 }
185
InitGpuMemoryLimit(MemoryOverflowCalllback callback,uint64_t size)186 void GPUContext::InitGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size)
187 {
188 impl_->InitGpuMemoryLimit(callback, size);
189 }
190
ResetContext()191 void GPUContext::ResetContext()
192 {
193 impl_->ResetContext();
194 }
195
196 #ifdef RS_ENABLE_VK
StoreVkPipelineCacheData()197 void GPUContext::StoreVkPipelineCacheData()
198 {
199 impl_->StoreVkPipelineCacheData();
200 }
201 #endif
202
RegisterPostFunc(const std::function<void (const std::function<void ()> & task)> & func)203 void GPUContext::RegisterPostFunc(const std::function<void(const std::function<void()>& task)>& func)
204 {
205 impl_->RegisterPostFunc(func);
206 }
207
GetPersistentCache() const208 GPUContextOptions::PersistentCache* GPUContextOptions::GetPersistentCache() const
209 {
210 return persistentCache_;
211 }
212
VmaDefragment()213 void GPUContext::VmaDefragment()
214 {
215 impl_->VmaDefragment();
216 }
217
BeginFrame()218 void GPUContext::BeginFrame()
219 {
220 impl_->BeginFrame();
221 }
222
EndFrame()223 void GPUContext::EndFrame()
224 {
225 impl_->EndFrame();
226 }
227
SetGpuCacheSuppressWindowSwitch(bool enabled)228 void GPUContext::SetGpuCacheSuppressWindowSwitch(bool enabled)
229 {
230 impl_->SetGpuCacheSuppressWindowSwitch(enabled);
231 }
232
SetGpuMemoryAsyncReclaimerSwitch(bool enabled,const std::function<void ()> & setThreadPriority)233 void GPUContext::SetGpuMemoryAsyncReclaimerSwitch(bool enabled, const std::function<void()>& setThreadPriority)
234 {
235 impl_->SetGpuMemoryAsyncReclaimerSwitch(enabled, setThreadPriority);
236 }
237
FlushGpuMemoryInWaitQueue()238 void GPUContext::FlushGpuMemoryInWaitQueue()
239 {
240 impl_->FlushGpuMemoryInWaitQueue();
241 }
242
SuppressGpuCacheBelowCertainRatio(const std::function<bool (void)> & nextFrameHasArrived)243 void GPUContext::SuppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived)
244 {
245 impl_->SuppressGpuCacheBelowCertainRatio(nextFrameHasArrived);
246 }
247
SetStoreCachePath(const std::string & filePath)248 void GPUContextOptions::SetStoreCachePath(const std::string& filePath)
249 {
250 filePath_ = filePath;
251 }
252
GetStoreCachePath() const253 std::string GPUContextOptions::GetStoreCachePath() const
254 {
255 return filePath_;
256 }
257 } // namespace Drawing
258 } // namespace Rosen
259 } // namespace OHOS
260