Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcaptureslotregistry_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
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 QCAPTURESLOTREGISTRY_P_H
5#define QCAPTURESLOTREGISTRY_P_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 purely as an
12// implementation detail. 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 <QtMultimedia/qtmultimediaglobal.h>
19
20#include <QtCore/qloggingcategory.h>
21#include <QtCore/qmutex.h>
22#include <QtCore/qwaitcondition.h>
23#include <QtCore/qvarlengtharray.h>
24
25#include <array>
26#include <cstddef>
27#include <memory>
28#include <mutex>
29#include <optional>
30
31#ifdef __cpp_concepts
32# include <concepts>
33#endif
34
35QT_BEGIN_NAMESPACE
36
37QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(qLcCaptureSlots, Q_MULTIMEDIA_EXPORT)
38QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(qLcCaptureSlotsTrace, Q_MULTIMEDIA_EXPORT)
39
40namespace QtMultimediaPrivate {
41
42#ifdef __cpp_concepts
43// clang-format off
44
45template <typename Slot>
46concept CaptureSlot = std::movable<Slot>;
47
48template <typename Producer, typename Slot>
49concept CaptureSlotProducer = requires(Producer &producer, Slot slot) {
50 { producer.recycleSlot(std::move(slot)) } -> std::same_as<void>;
51};
52
53// clang-format on
54#endif
55
56#ifdef __cpp_concepts
57template <typename Producer, CaptureSlot Slot, size_t Capacity = 32>
58#else
59template <typename Producer, typename Slot, size_t Capacity = 32>
60#endif
61class CaptureSlotRegistry : public std::enable_shared_from_this<Producer>
62{
63public:
64 using SlotType = Slot;
65
66 CaptureSlotRegistry(std::size_t slotCount, std::size_t minFreeSlots)
67 : m_slotCount(slotCount), m_minFreeSlots(minFreeSlots)
68 {
69 Q_ASSERT(slotCount <= Capacity);
70#ifdef __cpp_concepts
71 static_assert(CaptureSlotProducer<Producer, Slot>);
72#endif
73
74 std::generate_n(std::back_inserter(m_freeSlotIndices), slotCount,
75 [n = slotCount]() mutable {
76 return --n;
77 });
78 }
79
80 class Lease
81 {
82 public:
83 Lease(const Lease &) = delete;
84 Lease(Lease &&other) noexcept = default;
85
86 Lease &operator=(const Lease &) = delete;
87 Lease &operator=(Lease &&other) noexcept
88 {
89 if (this != &other) {
90 release();
91 m_producer = std::exchange(other.m_producer, {});
92 m_index = other.m_index;
93 }
94 return *this;
95 }
96
97 ~Lease() { release(); }
98
99 private:
100 friend class CaptureSlotRegistry;
101
102 Lease(std::shared_ptr<Producer> producer, std::size_t index)
103 : m_producer(std::move(producer)), m_index(index)
104 {
105 }
106
107 void release()
108 {
109 if (m_producer)
110 std::exchange(m_producer, {})->releaseEntry(m_index);
111 }
112
113 std::shared_ptr<Producer> m_producer;
114 std::size_t m_index = 0;
115 };
116
117 // Moves the slot only when a lease is returned. On refusal the argument is left intact, so the
118 // caller can still recycle it or copy the buffer it stands for.
119 std::optional<Lease> tryReserveSlot(Slot &slot)
120 {
121 std::lock_guard lock(m_mutex);
122 if (m_stopped || m_outstandingCount + m_minFreeSlots >= m_slotCount) {
123 qCDebug(qLcCaptureSlotsTrace) << "refusing slot: stopped:" << m_stopped
124 << "outstanding:" << m_outstandingCount << "of"
125 << m_slotCount << "keeping" << m_minFreeSlots << "free";
126 return std::nullopt;
127 }
128
129 ++m_outstandingCount;
130 const std::size_t index = allocateEntry(Slot(std::move(slot)));
131 qCDebug(qLcCaptureSlotsTrace)
132 << "reserved entry" << index << "outstanding:" << m_outstandingCount;
133 return Lease{
134 this->shared_from_this(),
135 index,
136 };
137 }
138
139 std::optional<Lease> tryReserveSlot(Slot &&slot) { return tryReserveSlot(slot); }
140
141 std::size_t outstandingSlotCount() const
142 {
143 std::lock_guard lock(m_mutex);
144 return m_outstandingCount;
145 }
146
147 // Refuses new slots, and makes slots that are released from now on drop instead of recycling.
148 // Waits for recycles in flight, so the producer may tear down once this returns.
149 void stop()
150 {
151 qCDebug(qLcCaptureSlots) << "stopping with" << outstandingSlotCount()
152 << "slots outstanding";
153
154 std::unique_lock lock(m_mutex);
155 m_stopped = true;
156 while (m_releasesInFlight != 0)
157 m_releaseFinished.wait(&m_mutex);
158 }
159
160protected:
161 ~CaptureSlotRegistry() = default;
162
163private:
164 friend class Lease;
165
166 struct Entry
167 {
168 constexpr bool isFree() const { return !slot.has_value(); }
169 std::optional<Slot> slot;
170 };
171
172 Producer &producer() { return static_cast<Producer &>(*this); }
173
174 std::size_t allocateEntry(Slot slot)
175 {
176 Q_ASSERT(!m_freeSlotIndices.empty());
177 const std::size_t index = m_freeSlotIndices.back();
178 m_freeSlotIndices.pop_back();
179
180 Q_ASSERT(m_entries[index].isFree());
181 qCDebug(qLcCaptureSlotsTrace) << "using entry" << index;
182 m_entries[index] = Entry{ std::move(slot) };
183 return index;
184 }
185
186 void releaseEntry(std::size_t index)
187 {
188 std::optional<Slot> slot;
189 {
190 std::lock_guard lock(m_mutex);
191 Entry &entry = m_entries[index];
192 Q_ASSERT(entry.slot && "a lease releases its entry once");
193
194 slot = std::move(entry.slot);
195 entry = Entry{};
196 m_freeSlotIndices.push_back(index);
197 --m_outstandingCount;
198
199 if (m_stopped) {
200 qCDebug(qLcCaptureSlotsTrace) << "entry" << index << "dropped, recycling stopped";
201 return;
202 }
203 ++m_releasesInFlight;
204 }
205
206 qCDebug(qLcCaptureSlotsTrace) << "recycling entry" << index;
207 producer().recycleSlot(std::move(*slot));
208
209 std::lock_guard lock(m_mutex);
210 if (--m_releasesInFlight == 0)
211 m_releaseFinished.wakeAll();
212 }
213
214 const std::size_t m_slotCount;
215 const std::size_t m_minFreeSlots;
216
217 mutable QMutex m_mutex;
218 QWaitCondition m_releaseFinished;
219 std::array<Entry, Capacity> m_entries;
220 QVarLengthArray<std::size_t, Capacity> m_freeSlotIndices; // std::inplace_vector
221 std::size_t m_outstandingCount = 0;
222 std::size_t m_releasesInFlight = 0;
223 bool m_stopped = false;
224};
225
226} // namespace QtMultimediaPrivate
227
228QT_END_NAMESPACE
229
230#endif // QCAPTURESLOTREGISTRY_P_H