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() override; 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 BASE_NS::array_view<const CacheGpuImageDesc> descs) override; 45 CacheGpuImageDesc GetCacheGpuImageDesc(const RenderHandleReference& gpuImageHandle) const override; 46 47 CacheGpuImagePair ReserveGpuImagePair(const CacheGpuImageDesc& desc, SampleCountFlags sampleCountFlags) override; 48 49 static uint64_t HashCacheGpuImageDesc(const CacheGpuImageDesc& desc); 50 51 void BeginFrame(const uint64_t frameCount); 52 53 struct ImageData { 54 RenderHandleReference handle; 55 uint64_t hash { 0 }; 56 }; 57 // access read handles when remapping in render graph 58 BASE_NS::array_view<const ImageData> GetImageData() const; 59 60 private: 61 RENDER_NS::GpuResourceManager& gpuResourceMgr_; 62 63 RenderHandleReference ReserveGpuImageImpl(const CacheGpuImageDesc& desc); 64 void AllocateAndRemapImages(); 65 void DestroyOldImages(); 66 67 // needs to be locked when touching image containers 68 mutable std::mutex mutex_; 69 struct FrameData { 70 BASE_NS::vector<ImageData> images; 71 }; 72 FrameData frameData_[2u]; 73 74 struct GpuBackedData { 75 uint64_t hash { 0 }; 76 uint64_t frameUseIndex { 0 }; 77 RenderHandleReference handle; 78 }; 79 BASE_NS::vector<GpuBackedData> gpuBackedImages_; 80 81 uint32_t writeIdx_ { 0 }; 82 uint64_t frameCounter_ { 0 }; 83 }; 84 RENDER_END_NAMESPACE() 85 #endif // RENDER_DEVICE_GPU_RESOURCE_CACHE_H 86