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 #ifdef NEW_SKIA 20 #include "include/gpu/GrDirectContext.h" 21 #else 22 #include "include/gpu/GrContext.h" 23 #endif 24 #include "include/gpu/GrContextOptions.h" 25 26 #include "impl_interface/gpu_context_impl.h" 27 #include "image/gpu_context.h" 28 #include "include/core/SkExecutor.h" 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 static std::unique_ptr<SkExecutor> threadPool; 59 void InitSkExecutor(); 60 61 #ifdef RS_ENABLE_VK 62 bool BuildFromVK(const GrVkBackendContext& context) override; 63 bool BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options) override; 64 #endif 65 66 void Flush() override; 67 void FlushAndSubmit(bool syncCpu) override; 68 void Submit() override; 69 void PerformDeferredCleanup(std::chrono::milliseconds msNotUsed) override; 70 71 void GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const override; 72 void SetResourceCacheLimits(int maxResource, size_t maxResourceBytes) override; 73 74 void GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const override; 75 76 void FreeGpuResources() override; 77 78 void DumpGpuStats(std::string& out) 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 PurgeUnlockAndSafeCacheGpuResources() override; 87 88 void ReleaseByTag(const GPUResourceTag &tag) override; 89 90 void DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) override; 91 92 void DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) override; 93 94 void SetCurrentGpuResourceTag(const GPUResourceTag &tag) override; 95 96 #ifdef RS_ENABLE_VK 97 void StoreVkPipelineCacheData() override; 98 #endif 99 100 #ifdef NEW_SKIA 101 sk_sp<GrDirectContext> GetGrContext() const; 102 void SetGrContext(const sk_sp<GrDirectContext>& grContext); 103 #else 104 sk_sp<GrContext> GetGrContext() const; 105 void SetGrContext(const sk_sp<GrContext>& grContext); 106 #endif 107 #ifdef NEW_SKIA ExportSkiaContext()108 const sk_sp<GrDirectContext> ExportSkiaContext() const 109 #else 110 const sk_sp<GrContext> ExportSkiaContext() const 111 #endif 112 { 113 return grContext_; 114 } 115 116 private: 117 #ifdef NEW_SKIA 118 sk_sp<GrDirectContext> grContext_; 119 #else 120 sk_sp<GrContext> grContext_; 121 #endif 122 std::shared_ptr<SkiaPersistentCache> skiaPersistentCache_; 123 }; 124 } // namespace Drawing 125 } // namespace Rosen 126 } // namespace OHOS 127 #endif // SKIA_GPUCONTEXT_H 128