1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QT3DRENDER_RENDER_RENDERQUEUE_H |
5 | #define QT3DRENDER_RENDER_RENDERQUEUE_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of other Qt classes. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <vector> |
19 | #include <QMutex> |
20 | #include <Qt3DCore/private/vector_helper_p.h> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | namespace Qt3DRender { |
25 | |
26 | namespace Render { |
27 | |
28 | template<class RenderView> |
29 | class RenderQueue |
30 | { |
31 | public: |
32 | RenderQueue() |
33 | : m_noRender(false) |
34 | , m_wasReset(true) |
35 | , m_targetRenderViewCount(0) |
36 | , m_currentRenderViewCount(0) |
37 | , m_currentWorkQueue(1) |
38 | {} |
39 | |
40 | /* |
41 | Sets the number \a targetRenderViewCount of RenderView objects that make |
42 | up a frame. |
43 | */ |
44 | void setTargetRenderViewCount(int targetRenderViewCount) |
45 | { |
46 | Q_ASSERT(!m_noRender); |
47 | m_targetRenderViewCount = targetRenderViewCount; |
48 | m_currentWorkQueue.resize(targetRenderViewCount); |
49 | m_wasReset = false; |
50 | } |
51 | |
52 | inline int targetRenderViewCount() const { return m_targetRenderViewCount; } |
53 | inline int currentRenderViewCount() const { return m_currentRenderViewCount; } |
54 | |
55 | /* |
56 | Returns true if all the RenderView objects making up the current frame |
57 | have been queued. Returns false otherwise. |
58 | \note a frameQueue or size 0 is considered incomplete. |
59 | */ |
60 | inline bool isFrameQueueComplete() const |
61 | { |
62 | return (m_noRender |
63 | || (m_targetRenderViewCount > 0 && m_targetRenderViewCount == m_currentRenderViewCount)); |
64 | } |
65 | |
66 | /* |
67 | Queue up a RenderView for the frame being built. |
68 | Thread safe as this is called from the renderer which is locked. |
69 | Returns true if the renderView is complete |
70 | */ |
71 | bool queueRenderView(RenderView *renderView, uint submissionOrderIndex) |
72 | { |
73 | Q_ASSERT(!m_noRender); |
74 | m_currentWorkQueue[submissionOrderIndex] = renderView; |
75 | ++m_currentRenderViewCount; |
76 | Q_ASSERT(m_currentRenderViewCount <= m_targetRenderViewCount); |
77 | return isFrameQueueComplete(); |
78 | } |
79 | |
80 | /* |
81 | Called by the Rendering Thread to retrieve the a frame queue to render. |
82 | A call to reset is required after rendering of the frame. Otherwise under some |
83 | conditions the current but then invalidated frame queue could be reused. |
84 | */ |
85 | inline const std::vector<RenderView *> &nextFrameQueue() const { return m_currentWorkQueue; } |
86 | |
87 | /* |
88 | In case the framegraph changed or when the current number of render queue |
89 | needs to be reset. |
90 | */ |
91 | void reset() |
92 | { |
93 | m_currentRenderViewCount = 0; |
94 | m_targetRenderViewCount = 0; |
95 | Qt3DCore::deleteAll(m_currentWorkQueue); |
96 | m_currentWorkQueue.clear(); |
97 | m_noRender = false; |
98 | m_wasReset = true; |
99 | } |
100 | |
101 | void setNoRender() |
102 | { |
103 | Q_ASSERT(m_targetRenderViewCount == 0); |
104 | m_noRender = true; |
105 | } |
106 | inline bool isNoRender() const { return m_noRender; } |
107 | |
108 | inline bool wasReset() const { return m_wasReset; } |
109 | |
110 | inline QMutex *mutex() { return &m_mutex; } |
111 | |
112 | private: |
113 | bool m_noRender; |
114 | bool m_wasReset; |
115 | int m_targetRenderViewCount; |
116 | int m_currentRenderViewCount; |
117 | std::vector<RenderView *> m_currentWorkQueue; |
118 | QMutex m_mutex; |
119 | }; |
120 | |
121 | } // namespace Render |
122 | |
123 | } // namespace Qt3DRender |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | #endif // QT3DRENDER_RENDER_RENDERQUEUE_H |
128 |