/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GPU_CONTEXT_H #define GPU_CONTEXT_H #include "impl_interface/gpu_context_impl.h" #include "utils/drawing_macros.h" #include "utils/data.h" #include "trace_memory_dump.h" typedef void* EGLContext; namespace OHOS { namespace Rosen { namespace Drawing { enum class PathRenderers : uint32_t { NONE = 0, DASHLINE = 1 << 0, STENCILANDCOVER = 1 << 1, COVERAGECOUNTING = 1 << 2, AAHAIRLINE = 1 << 3, AACONVEX = 1 << 4, AALINEARIZING = 1 << 5, SMALL = 1 << 6, TESSELLATING = 1 << 7, ALL = (TESSELLATING | (TESSELLATING - 1)), DEFAULT = ALL & ~COVERAGECOUNTING }; struct GPUResourceTag { GPUResourceTag() : fPid(0), fTid(0), fWid(0), fFid(0) {} GPUResourceTag(uint32_t pid, uint32_t tid, uint32_t wid, uint32_t fid) : fPid(pid), fTid(tid), fWid(wid), fFid(fid) {} uint32_t fPid; uint32_t fTid; uint32_t fWid; uint32_t fFid; }; /* * @brief Option to create a GPUContext. Currently only supports setting persistent cache, other options may be expanded in the future */ class DRAWING_API GPUContextOptions { public: /* * @brief Cache compiled shaders for use between sessions. */ class PersistentCache { public: PersistentCache() = default; virtual ~PersistentCache() = default; /* * @brief Returns the data for the key if it exists in the cache. */ virtual std::shared_ptr Load(const Data& key) = 0; /* * @brief Stores the data and key. */ virtual void Store(const Data& key, const Data& data) = 0; }; /* * @brief Gets persistent cache object. */ PersistentCache* GetPersistentCache() const; /* * @brief Sets persistent cache object. * @param persistentCache A pointer to persistent cache object. */ void SetPersistentCache(PersistentCache* persistentCache); void SetAllowPathMaskCaching(bool allowPathMaskCaching); bool GetAllowPathMaskCaching() const; private: PersistentCache* persistentCache_ = nullptr; bool allowPathMaskCaching_ = true; }; class DRAWING_API GPUContext { public: GPUContext(); ~GPUContext() {} /* * @brief Creates a GL GPUContext for a backend context. * @param options Option to create a GL GPUContext. */ bool BuildFromGL(const GPUContextOptions& options); /* * @brief Creates a VK GPUContext for a backend context. * @param options Option to create a VK GPUContext. */ #ifdef RS_ENABLE_VK bool BuildFromVK(const GrVkBackendContext& context); bool BuildFromVK(const GrVkBackendContext& context, const GPUContextOptions& options); #endif /* * @brief Call to ensure all drawing to the context has been flushed to underlying 3D API specific objects. */ void Flush(); /* * @brief Call to ensure all drawing to the context has been submitted to underlying 3D API. */ void Submit(); /* * @brief Call to ensure all drawing to the context has been flushed and submitted to underlying 3D API. */ void FlushAndSubmit(bool syncCpu = false); /* * @brief Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds or are otherwise marked for deletion. * @param msNotUsed Only unlocked resources not used in these last milliseconds will be cleaned up. */ void PerformDeferredCleanup(std::chrono::milliseconds msNotUsed); /* * @brief Gets the current GPU resource cache limits. * @param maxResource If non-null, returns maximum number of resources that can be held in the cache. * @param maxResourceBytes If non-null, returns maximum number of bytes of video memory that can be held in the cache. */ void GetResourceCacheLimits(int* maxResource, size_t* maxResourceBytes) const; /* * @brief Specify the GPU resource cache limits. * @param maxResource The maximum number of resources that can be held in the cache. * @param maxResourceBytes The maximum number of bytes of video memory that can be held in the cache. */ void SetResourceCacheLimits(int maxResource, size_t maxResourceBytes); /* * @brief Gets the current GPU resource cache usage. * @param resourceCount If non-null, returns the number of resources that are held in the cache. * @param resourceBytes If non-null, returns the total number of bytes of video memory held in the cache. */ void GetResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const; /* * @brief Free GPU created by the contetx. */ void FreeGpuResources(); /* * @brief Dump GPU stats. * @param out Dump GPU stat string. */ void DumpGpuStats(std::string& out) const; /* * @brief After returning it will assume that the underlying context may no longer be valid. */ void ReleaseResourcesAndAbandonContext(); /* * @brief Purge unlocked resources from the cache until * the provided byte count has been reached or we have purged all unlocked resources. */ void PurgeUnlockedResources(bool scratchResourcesOnly); /* * @brief Purge unlocked resources by tag from the cache until * the provided byte count has been reached or we have purged all unlocked resources. */ void PurgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GPUResourceTag &tag); /* * @brief Purge unlocked resources from the safe cache until * the provided byte count has been reached or we have purged all unlocked resources. */ void PurgeUnlockAndSafeCacheGpuResources(); /* * @brief Releases GPUResource objects and removes them from the cache by tag. */ void ReleaseByTag(const GPUResourceTag &tag); /* * @brief Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */ void DumpMemoryStatisticsByTag(TraceMemoryDump* traceMemoryDump, GPUResourceTag &tag) const; /* * @brief Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */ void DumpMemoryStatistics(TraceMemoryDump* traceMemoryDump) const; /* * @brief Set current resource tag for gpu cache recycle. */ void SetCurrentGpuResourceTag(const GPUResourceTag &tag); /* * @brief Store vulkan pipeline cache */ #ifdef RS_ENABLE_VK void StoreVkPipelineCacheData(); #endif /* * @brief Get the adaptation layer instance, called in the adaptation layer. * @return Adaptation Layer instance. */ template T* GetImpl() const { return impl_->DowncastingTo(); } private: std::shared_ptr impl_; }; } // namespace Drawing } // namespace Rosen } // namespace OHOS #endif // !GPU_CONTEXT_H