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     impl_->GetResourceCacheLimits(maxResource, maxResourceBytes);
55 }
56 
SetResourceCacheLimits(int maxResource,size_t maxResourceBytes)57 void GPUContext::SetResourceCacheLimits(int maxResource, size_t maxResourceBytes)
58 {
59     impl_->SetResourceCacheLimits(maxResource, maxResourceBytes);
60 }
61 
Flush()62 void GPUContext::Flush()
63 {
64     impl_->Flush();
65 }
66 
FlushAndSubmit(bool syncCpu)67 void GPUContext::FlushAndSubmit(bool syncCpu)
68 {
69     impl_->FlushAndSubmit(syncCpu);
70 }
71 
Submit()72 void GPUContext::Submit()
73 {
74     impl_->Submit();
75 }
76 
PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)77 void GPUContext::PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)
78 {
79     impl_->PerformDeferredCleanup(msNotUsed);
80 }
81 
SetPersistentCache(PersistentCache * persistentCache)82 void GPUContextOptions::SetPersistentCache(PersistentCache* persistentCache)
83 {
84     persistentCache_ = persistentCache;
85 }
86 
SetAllowPathMaskCaching(bool allowPathMaskCaching)87 void GPUContextOptions::SetAllowPathMaskCaching(bool allowPathMaskCaching)
88 {
89     allowPathMaskCaching_ = allowPathMaskCaching;
90 }
91 
GetAllowPathMaskCaching() const92 bool GPUContextOptions::GetAllowPathMaskCaching() const
93 {
94     return allowPathMaskCaching_;
95 }
96 
GetResourceCacheUsage(int * resourceCount,size_t * resourceBytes) const97 void GPUContext::GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const
98 {
99     impl_->GetResourceCacheUsage(resourceCount, resourceBytes);
100 }
101 
FreeGpuResources()102 void GPUContext::FreeGpuResources()
103 {
104     impl_->FreeGpuResources();
105 }
106 
DumpGpuStats(std::string & out) const107 void GPUContext::DumpGpuStats(std::string& out) const
108 {
109     impl_->DumpGpuStats(out);
110 }
111 
ReleaseResourcesAndAbandonContext()112 void GPUContext::ReleaseResourcesAndAbandonContext()
113 {
114     impl_->ReleaseResourcesAndAbandonContext();
115 }
116 
PurgeUnlockedResources(bool scratchResourcesOnly)117 void GPUContext::PurgeUnlockedResources(bool scratchResourcesOnly)
118 {
119     impl_->PurgeUnlockedResources(scratchResourcesOnly);
120 }
121 
PurgeUnlockedResourcesByTag(bool scratchResourcesOnly,const GPUResourceTag & tag)122 void GPUContext::PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag)
123 {
124     impl_->PurgeUnlockedResourcesByTag(scratchResourcesOnly, tag);
125 }
126 
PurgeUnlockedResourcesByPid(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet)127 void GPUContext::PurgeUnlockedResourcesByPid(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet)
128 {
129     impl_->PurgeUnlockedResourcesByPid(scratchResourcesOnly, exitedPidSet);
130 }
131 
PurgeUnlockAndSafeCacheGpuResources()132 void GPUContext::PurgeUnlockAndSafeCacheGpuResources()
133 {
134     impl_->PurgeUnlockAndSafeCacheGpuResources();
135 }
PurgeCacheBetweenFrames(bool scratchResourcesOnly,const std::set<pid_t> & exitedPidSet,const std::set<pid_t> & protectedPidSet)136 void GPUContext::PurgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet,
137     const std::set<pid_t>& protectedPidSet)
138 {
139     impl_->PurgeCacheBetweenFrames(scratchResourcesOnly, exitedPidSet, protectedPidSet);
140 }
141 
ReleaseByTag(const GPUResourceTag & tag)142 void GPUContext::ReleaseByTag(const GPUResourceTag &tag)
143 {
144     impl_->ReleaseByTag(tag);
145 }
146 
DumpMemoryStatisticsByTag(TraceMemoryDump * traceMemoryDump,GPUResourceTag & tag) const147 void GPUContext::DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const
148 {
149     impl_->DumpMemoryStatisticsByTag(traceMemoryDump, tag);
150 }
151 
DumpMemoryStatistics(TraceMemoryDump * traceMemoryDump) const152 void GPUContext::DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) const
153 {
154     impl_->DumpMemoryStatistics(traceMemoryDump);
155 }
156 
SetCurrentGpuResourceTag(const GPUResourceTag & tag)157 void GPUContext::SetCurrentGpuResourceTag(const GPUResourceTag &tag)
158 {
159     impl_->SetCurrentGpuResourceTag(tag);
160 }
161 
GetUpdatedMemoryMap(std::unordered_map<pid_t,size_t> & out)162 void GPUContext::GetUpdatedMemoryMap(std::unordered_map<pid_t, size_t> &out)
163 {
164     impl_->GetUpdatedMemoryMap(out);
165 }
166 
InitGpuMemoryLimit(MemoryOverflowCalllback callback,uint64_t size)167 void GPUContext::InitGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size)
168 {
169     impl_->InitGpuMemoryLimit(callback, size);
170 }
171 
ResetContext()172 void GPUContext::ResetContext()
173 {
174     impl_->ResetContext();
175 }
176 
177 #ifdef RS_ENABLE_VK
StoreVkPipelineCacheData()178 void GPUContext::StoreVkPipelineCacheData()
179 {
180     impl_->StoreVkPipelineCacheData();
181 }
182 #endif
183 
RegisterPostFunc(const std::function<void (const std::function<void ()> & task)> & func)184 void GPUContext::RegisterPostFunc(const std::function<void(const std::function<void()>& task)>& func)
185 {
186     impl_->RegisterPostFunc(func);
187 }
188 
GetPersistentCache() const189 GPUContextOptions::PersistentCache* GPUContextOptions::GetPersistentCache() const
190 {
191     return persistentCache_;
192 }
193 
VmaDefragment()194 void GPUContext::VmaDefragment()
195 {
196     impl_->VmaDefragment();
197 }
198 
BeginFrame()199 void GPUContext::BeginFrame()
200 {
201     impl_->BeginFrame();
202 }
203 
EndFrame()204 void GPUContext::EndFrame()
205 {
206     impl_->EndFrame();
207 }
208 
SetGpuCacheSuppressWindowSwitch(bool enabled)209 void GPUContext::SetGpuCacheSuppressWindowSwitch(bool enabled)
210 {
211     impl_->SetGpuCacheSuppressWindowSwitch(enabled);
212 }
213 
SetGpuMemoryAsyncReclaimerSwitch(bool enabled,const std::function<void ()> & setThreadPriority)214 void GPUContext::SetGpuMemoryAsyncReclaimerSwitch(bool enabled, const std::function<void()>& setThreadPriority)
215 {
216     impl_->SetGpuMemoryAsyncReclaimerSwitch(enabled, setThreadPriority);
217 }
218 
FlushGpuMemoryInWaitQueue()219 void GPUContext::FlushGpuMemoryInWaitQueue()
220 {
221     impl_->FlushGpuMemoryInWaitQueue();
222 }
223 
SuppressGpuCacheBelowCertainRatio(const std::function<bool (void)> & nextFrameHasArrived)224 void GPUContext::SuppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived)
225 {
226     impl_->SuppressGpuCacheBelowCertainRatio(nextFrameHasArrived);
227 }
228 
229 } // namespace Drawing
230 } // namespace Rosen
231 } // namespace OHOS
232