1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 RENDER_DEVICE_GPU_RESOURCE_CACHE_H 17 #define RENDER_DEVICE_GPU_RESOURCE_CACHE_H 18 19 #include <mutex> 20 21 #include <base/containers/array_view.h> 22 #include <base/containers/unordered_map.h> 23 #include <base/containers/vector.h> 24 #include <render/device/intf_gpu_resource_cache.h> 25 #include <render/namespace.h> 26 #include <render/resource_handle.h> 27 28 RENDER_BEGIN_NAMESPACE() 29 class GpuResourceManager; 30 31 /* Gpu resource cache. 32 Caches gpu resource for use and can be obtained for re-use. 33 */ 34 class GpuResourceCache final : public IGpuResourceCache { 35 public: 36 explicit GpuResourceCache(RENDER_NS::GpuResourceManager& gpuResourceMgr); 37 ~GpuResourceCache(); 38 39 GpuResourceCache(const GpuResourceCache&) = delete; 40 GpuResourceCache& operator=(const GpuResourceCache&) = delete; 41 42 RenderHandleReference ReserveGpuImage(const CacheGpuImageDesc& desc) override; 43 BASE_NS::vector<RenderHandleReference> ReserveGpuImages( 44 const BASE_NS::array_view<const CacheGpuImageDesc> descs) override; 45 CacheGpuImageDesc GetCacheGpuImageDesc(const RenderHandleReference& gpuImageHandle) const override; 46 47 CacheGpuImagePair ReserveGpuImagePair( 48 const CacheGpuImageDesc& desc, const SampleCountFlags sampleCountFlags) override; 49 50 uint64_t HashCacheGpuImageDesc(const CacheGpuImageDesc& desc) const; 51 52 void BeginFrame(const uint64_t frameCount); 53 54 struct ImageData { 55 RenderHandleReference handle; 56 uint64_t hash { 0 }; 57 }; 58 // access read handles when remapping in render graph 59 BASE_NS::array_view<const ImageData> GetImageData() const; 60 61 private: 62 RENDER_NS::GpuResourceManager& gpuResourceMgr_; 63 64 RenderHandleReference ReserveGpuImageImpl(const CacheGpuImageDesc& desc); 65 void AllocateAndRemapImages(); 66 void DestroyOldImages(); 67 68 // needs to be locked when touching image containers 69 mutable std::mutex mutex_; 70 struct FrameData { 71 BASE_NS::vector<ImageData> images; 72 }; 73 FrameData frameData_[2u]; 74 75 struct GpuBackedData { 76 uint64_t hash { 0 }; 77 uint64_t frameUseIndex { 0 }; 78 RenderHandleReference handle; 79 }; 80 BASE_NS::vector<GpuBackedData> gpuBackedImages_; 81 82 uint32_t writeIdx_ { 0 }; 83 uint64_t frameCounter_ { 0 }; 84 }; 85 RENDER_END_NAMESPACE() 86 #endif // RENDER_DEVICE_GPU_RESOURCE_CACHE_H 87