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