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