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_UTIL_RENDER_FRAME_UTIL_H
17 #define RENDER_UTIL_RENDER_FRAME_UTIL_H
18 
19 #include <mutex>
20 
21 #include <base/containers/vector.h>
22 #include <render/util/intf_render_frame_util.h>
23 
24 #include "device/gpu_semaphore.h"
25 
26 RENDER_BEGIN_NAMESPACE()
27 class IDevice;
28 class IRenderContext;
29 class IRenderDataStoreDefaultStaging;
30 class IRenderDataStoreDefaultGpuResourceDataCopy;
31 
32 class RenderFrameUtil : public IRenderFrameUtil {
33 public:
34     explicit RenderFrameUtil(const IRenderContext& renderContext);
35     ~RenderFrameUtil() override = default;
36 
37     void BeginFrame();
38     void EndFrame();
39 
40     bool HasGpuSignals() const;
41     BASE_NS::array_view<BASE_NS::unique_ptr<GpuSemaphore>> GetGpuSemaphores();
42 
43     void CopyToCpu(const RenderHandleReference& handle, const CopyFlags flags) override;
44     BASE_NS::array_view<IRenderFrameUtil::FrameCopyData> GetFrameCopyData() override;
45     const FrameCopyData& GetFrameCopyData(const RenderHandleReference& handle) override;
46 
47     void SetBackBufferConfiguration(const BackBufferConfiguration& backBufferConfiguration) override;
48 
49     void AddGpuSignal(const SignalData& signalData) override;
50     BASE_NS::array_view<SignalData> GetFrameGpuSignalData() override;
51     SignalData GetFrameGpuSignalData(const RenderHandleReference& handle) override;
52 
53 private:
54     struct InternalFrameCopyData {
55         uint64_t frameIndex { 0 };
56         RenderHandleReference handle {};
57         CopyFlags copyFlags { 0u };
58     };
59     struct CopyData {
60         BASE_NS::vector<InternalFrameCopyData> copyData;
61     };
62 
63     bool ValidateInput(const RenderHandleReference& handle);
64     void ProcessFrameCopyData();
65     void ProcessFrameInputCopyData(const CopyData& copyData);
66     void ProcessFrameSignalData();
67     void ProcessFrameBackBufferConfiguration();
68     void ProcessFrameSignalDeferredDestroy();
69 
70     const IRenderContext& renderContext_;
71     IDevice& device_;
72 
73     IRenderDataStoreDefaultStaging* dsStaging_ { nullptr };
74     IRenderDataStoreDefaultGpuResourceDataCopy* dsCpuToGpuCopy_ { nullptr };
75 
76     // used for all
77     mutable std::mutex mutex_;
78 
79     CopyData preFrame_;
80     CopyData postFrame_;
81     // buffered for non-wait-for-idle copies, resized for command buffering count + 1
82     BASE_NS::vector<CopyData> bufferedPostFrame_;
83     uint32_t bufferedIndex_ { 0u };
84 
85     // will hold the actual results from where the array_view is created for the user
86     BASE_NS::vector<FrameCopyData> thisFrameCopiedData_;
87     FrameCopyData defaultCopyData_;
88 
89     BASE_NS::vector<SignalData> preSignalData_;
90     BASE_NS::vector<SignalData> postSignalData_;
91     // will hold the actual results from where the array_view is created for the user
92     struct FrameSignalData {
93         BASE_NS::vector<BASE_NS::unique_ptr<GpuSemaphore>> gpuSemaphores;
94         BASE_NS::vector<SignalData> signalData;
95     };
96     FrameSignalData thisFrameSignalData_;
97 
98     struct FrameBackBufferConfiguration {
99         BackBufferConfiguration bbc;
100         bool force { false };
101     };
102     FrameBackBufferConfiguration preBackBufferConfig_;
103     FrameBackBufferConfiguration postBackBufferConfig_;
104 
105     struct GpuSignalBufferedDestroy {
106         uint64_t frameUseIndex { 0 };
107         BASE_NS::unique_ptr<GpuSemaphore> gpuSemaphore;
108     };
109     BASE_NS::vector<GpuSignalBufferedDestroy> gpuSignalDeferredDestroy_;
110 
111     // if there's even a single copy operation with wait we can copy all data
112     bool frameHasWaitForIdle_ { false };
113 };
114 RENDER_END_NAMESPACE()
115 
116 #endif // RENDER_UTIL_RENDER_FRAME_UTIL_H
117