Loading...
Searching...
No Matches
PacedFramePump.hpp
Go to the documentation of this file.
1#pragma once
2
41#include <score_plugin_gfx_export.h>
42
43#include <atomic>
44#include <cstdint>
45#include <functional>
46#include <memory>
47#include <semaphore>
48#include <thread>
49#include <vector>
50
51namespace score::gfx::interop
52{
53
54class SCORE_PLUGIN_GFX_EXPORT PacedFramePump
55{
56public:
57 struct Hooks
58 {
64 std::function<bool()> waitForTick;
67 std::function<bool()> canAccept;
71 std::function<bool(void* framePtr)> submit;
77 std::function<void(void* framePtr)> discard;
78 };
79
81 explicit PacedFramePump(Hooks hooks, int ringDepth = 3);
83
84 PacedFramePump(const PacedFramePump&) = delete;
85 PacedFramePump& operator=(const PacedFramePump&) = delete;
86
88 void start();
90 void stop();
91
94 bool push(void* framePtr);
95
96 uint64_t goodXfers() const { return m_goodXfers.load(std::memory_order_relaxed); }
97 uint64_t drops() const { return m_drops.load(std::memory_order_relaxed); }
98 uint64_t underruns() const { return m_underruns.load(std::memory_order_relaxed); }
99
100private:
101 void run();
102
103 Hooks m_hooks;
104 const int m_depth;
105
106 // Slot stores a frame pointer. nullptr = empty. A non-null write followed by
107 // a writeIdx release-bump publishes it; the consumer reads with acquire.
108 std::vector<std::atomic<void*>> m_slots;
109 std::atomic<uint32_t> m_writeIdx{0};
110 std::atomic<uint32_t> m_readIdx{0};
111
112 // One permit per successfully push()ed frame; the consumer blocks here (not
113 // in waitForTick) while idle. Drained-ahead frames return their permits via
114 // try_acquire so the count tracks the ring.
115 std::counting_semaphore<> m_framesAvail{0};
116
117 std::thread m_thread;
118 std::atomic<bool> m_running{false};
119
120 std::atomic<uint64_t> m_goodXfers{0};
121 std::atomic<uint64_t> m_drops{0};
122 std::atomic<uint64_t> m_underruns{0};
123};
124
125} // namespace score::gfx::interop
Definition PacedFramePump.hpp:55
Definition PacedFramePump.hpp:58
std::function< void(void *framePtr)> discard
Definition PacedFramePump.hpp:77
std::function< bool(void *framePtr)> submit
Definition PacedFramePump.hpp:71
std::function< bool()> waitForTick
Definition PacedFramePump.hpp:64
std::function< bool()> canAccept
Definition PacedFramePump.hpp:67