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 DEVICE_GPU_RESOURCE_MANAGER_BASE_H
17 #define DEVICE_GPU_RESOURCE_MANAGER_BASE_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/unique_ptr.h>
22 #include <base/containers/vector.h>
23 #include <render/namespace.h>
24 
25 RENDER_BEGIN_NAMESPACE()
26 class Device;
27 
28 /* GpuResourceManagerBase.
29  * Not internally synchronized. */
30 class GpuResourceManagerBase {
31 public:
32     GpuResourceManagerBase() = default;
33     virtual ~GpuResourceManagerBase() = default;
34 
35     virtual void HandlePendingDeallocations() = 0;
36     virtual void HandlePendingDeallocationsImmediate() = 0;
37 
38 protected:
39     friend class GpuResourceManager;
40     virtual void Resize(const size_t maxSize) = 0;
41     virtual void Destroy(const uint32_t index) = 0;
42     virtual void DestroyImmediate(const uint32_t index) = 0;
43 };
44 
45 template<typename ResourceType, typename CreateInfoType>
46 class GpuResourceManagerTyped final : GpuResourceManagerBase {
47 public:
48     explicit GpuResourceManagerTyped(Device& device);
49     virtual ~GpuResourceManagerTyped() = default;
50 
51     GpuResourceManagerTyped(const GpuResourceManagerTyped&) = delete;
52     GpuResourceManagerTyped& operator=(const GpuResourceManagerTyped&) = delete;
53 
54     ResourceType* Get(const uint32_t index) const;
55 
56 protected:
57     friend class GpuResourceManager;
58 
59     // handle pending deallocations (waits safe frames to deallocate).
60     void HandlePendingDeallocations() override;
61     // handle pending deallocations immediately.
62     void HandlePendingDeallocationsImmediate() override;
63 
64     // resize the resource vector
65     void Resize(const size_t maxSize) override;
66     // deferred gpu resource destruction.
67     void Destroy(const uint32_t index) override;
68     // forced immediate Destroy, prefer not to use this
69     void DestroyImmediate(const uint32_t index) override;
70 
71     // create new, send old to pending deallocations if replacing
72     template<typename AdditionalInfoType>
73     void Create(const uint32_t index, const CreateInfoType& desc, BASE_NS::unique_ptr<ResourceType> optionalResource,
74         const bool useAdditionalDesc, const AdditionalInfoType& additonalDesc);
75 
76     Device& device_;
77 
78 #if (RENDER_VALIDATION_ENABLED == 1)
79     size_t GetValidResourceCount() const;
80 #endif
81 
82 private:
83     BASE_NS::vector<BASE_NS::unique_ptr<ResourceType>> resources_;
84     // resource, frame index
85     struct PendingDeallocData {
86         BASE_NS::unique_ptr<ResourceType> resource;
87         uint64_t frameIndex;
88     };
89     BASE_NS::vector<PendingDeallocData> pendingDeallocations_;
90 };
91 RENDER_END_NAMESPACE()
92 
93 #include "gpu_resource_manager_base.inl"
94 
95 #endif // DEVICE_GPU_RESOURCE_MANAGER_BASE_H
96