• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 Huawei Device Co., Ltd.. All rights reserved.
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 SKIA_GPUCONTEXT_H
17 #define SKIA_GPUCONTEXT_H
18 
19 #include <unordered_map>
20 #include <array>
21 
22 #include "include/core/SkExecutor.h"
23 #ifdef USE_M133_SKIA
24 #include "include/gpu/ganesh/GrTypes.h"
25 #include "include/gpu/ganesh/GrContextOptions.h"
26 #include "include/gpu/ganesh/GrDirectContext.h"
27 #include "include/gpu/ganesh/gl/GrGLDirectContext.h"
28 #ifdef RS_ENABLE_VK
29 #include "include/gpu/ganesh/vk/GrVkDirectContext.h"
30 #endif
31 #else
32 #include "include/gpu/GrContextOptions.h"
33 #include "include/gpu/GrDirectContext.h"
34 #endif
35 
36 #include "image/gpu_context.h"
37 #include "impl_interface/gpu_context_impl.h"
38 
39 
40 namespace OHOS {
41 namespace Rosen {
42 namespace Drawing {
43 class SkiaPersistentCache : public GrContextOptions::PersistentCache {
44 public:
45     explicit SkiaPersistentCache(GPUContextOptions::PersistentCache* cache);
~SkiaPersistentCache()46     ~SkiaPersistentCache() {}
47 
48     sk_sp<SkData> load(const SkData& key) override;
49     void store(const SkData& key, const SkData& data) override;
50 private:
51     GPUContextOptions::PersistentCache* cache_;
52 };
53 
54 class SkiaGPUContext : public GPUContextImpl {
55 public:
56     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
57 
58     SkiaGPUContext();
59     ~SkiaGPUContext() override = default;
60 
GetType()61     AdapterType GetType() const override
62     {
63         return AdapterType::SKIA_ADAPTER;
64     }
65 
66     bool BuildFromGL(const GPUContextOptions& options) override;
67 
68 #ifdef RS_ENABLE_VK
69 #ifdef USE_M133_SKIA
70     bool BuildFromVK(const skgpu::VulkanBackendContext& context) override;
71     bool BuildFromVK(const skgpu::VulkanBackendContext& context, const GPUContextOptions& options) override;
72 #else
73     bool BuildFromVK(const GrVkBackendContext& context) override;
74     bool BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options) override;
75 #endif
76 #endif
77 
78     void Flush() override;
79     void FlushAndSubmit(bool syncCpu) override;
80     void Submit() override;
81     void PerformDeferredCleanup(std::chrono::milliseconds msNotUsed) override;
82 
83     void GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const override;
84     void SetResourceCacheLimits(int maxResource, size_t maxResourceBytes) override;
85     void SetPurgeableResourceLimit(int purgeableMaxCount) override;
86 
87     void GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const override;
88 
89     void FreeGpuResources() override;
90 
91     void ReclaimResources() override;
92 
93     void DumpGpuStats(std::string& out) override;
94 
95     void DumpAllResource(std::stringstream& dump) override;
96 
97     void ReleaseResourcesAndAbandonContext() override;
98 
99     void PurgeUnlockedResources(bool scratchResourcesOnly) override;
100 
101     void PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag) override;
102 
103     void PurgeUnlockedResourcesByPid(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet) override;
104 
105     void RegisterVulkanErrorCallback(const std::function<void()>& vulkanErrorCallback) override;
106 
107     void PurgeUnlockAndSafeCacheGpuResources() override;
108 
109     void ReleaseByTag(const GPUResourceTag &tag) override;
110 
111     void PurgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet,
112         const std::set<pid_t>& protectedPidSet) override;
113 
114     void ResetContext() override;
115 
116     void DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) override;
117 
118     uint64_t NewDumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) override;
119 
120     void DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) override;
121 
122     void SetCurrentGpuResourceTag(const GPUResourceTag &tag) override;
123 
124     GPUResourceTag GetCurrentGpuResourceTag() const override;
125 
126     void GetUpdatedMemoryMap(std::unordered_map<pid_t, size_t> &out) override;
127 
128     void InitGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size) override;
129 #ifdef RS_ENABLE_VK
130     void StoreVkPipelineCacheData() override;
131 #endif
132 
133     sk_sp<GrDirectContext> GetGrContext() const;
134     void SetGrContext(const sk_sp<GrDirectContext>& grContext);
ExportSkiaContext()135     const sk_sp<GrDirectContext> ExportSkiaContext() const
136     {
137         return grContext_;
138     }
139     void RegisterPostFunc(const std::function<void(const std::function<void()>& task)>& func) override;
140 
141     static std::function<void(const std::function<void()>& task)> GetPostFunc(sk_sp<GrDirectContext> grContext);
142 
143     void VmaDefragment() override;
144 
145     void BeginFrame() override;
146 
147     void EndFrame() override;
148 
149     void SetGpuCacheSuppressWindowSwitch(bool enabled) override;
150 
151     void SetGpuMemoryAsyncReclaimerSwitch(bool enabled, const std::function<void()>& setThreadPriority) override;
152 
153     void FlushGpuMemoryInWaitQueue() override;
154 
155     void SuppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived) override;
156 
157     void GetHpsEffectSupport(std::vector<const char*>& instanceExtensions) override;
158 
159     void SetEarlyZEnabled(bool flag) override;
160 private:
161     sk_sp<GrDirectContext> grContext_;
162     std::shared_ptr<SkiaPersistentCache> skiaPersistentCache_;
163     static std::unordered_map<uintptr_t, std::function<void(const std::function<void()>& task)>> contextPostMap_;
164 };
165 } // namespace Drawing
166 } // namespace Rosen
167 } // namespace OHOS
168 #endif // SKIA_GPUCONTEXT_H
169