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 71 void GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const override; 72 73 void FreeGpuResources() override; 74 75 void DumpGpuStats(std::string& out) override; 76 77 void ReleaseResourcesAndAbandonContext() override; 78 79 void PurgeUnlockedResources(bool scratchResourcesOnly) override; 80 81 void PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag) override; 82 83 void PurgeUnlockedResourcesByPid(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet) override; 84 85 void PurgeUnlockAndSafeCacheGpuResources() override; 86 87 void ReleaseByTag(const GPUResourceTag &tag) override; 88 89 void PurgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<pid_t>& exitedPidSet, 90 const std::set<pid_t>& protectedPidSet) override; 91 92 void ResetContext() override; 93 94 void DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) override; 95 96 void DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) override; 97 98 void SetCurrentGpuResourceTag(const GPUResourceTag &tag) override; 99 100 void GetUpdatedMemoryMap(std::unordered_map<pid_t, size_t> &out) override; 101 102 void InitGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size) override; 103 #ifdef RS_ENABLE_VK 104 void StoreVkPipelineCacheData() override; 105 #endif 106 107 sk_sp<GrDirectContext> GetGrContext() const; 108 void SetGrContext(const sk_sp<GrDirectContext>& grContext); ExportSkiaContext()109 const sk_sp<GrDirectContext> ExportSkiaContext() const 110 { 111 return grContext_; 112 } 113 void RegisterPostFunc(const std::function<void(const std::function<void()>& task)>& func) override; 114 115 static std::function<void(const std::function<void()>& task)> GetPostFunc(sk_sp<GrDirectContext> grContext); 116 117 void VmaDefragment() override; 118 119 void BeginFrame() override; 120 121 void EndFrame() override; 122 123 void SetGpuCacheSuppressWindowSwitch(bool enabled) override; 124 125 void SetGpuMemoryAsyncReclaimerSwitch(bool enabled, const std::function<void()>& setThreadPriority) override; 126 127 void FlushGpuMemoryInWaitQueue() override; 128 129 void SuppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived) override; 130 private: 131 sk_sp<GrDirectContext> grContext_; 132 std::shared_ptr<SkiaPersistentCache> skiaPersistentCache_; 133 static std::unordered_map<uintptr_t, std::function<void(const std::function<void()>& task)>> contextPostMap_; 134 }; 135 } // namespace Drawing 136 } // namespace Rosen 137 } // namespace OHOS 138 #endif // SKIA_GPUCONTEXT_H 139