libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
buffercommand.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cassert>
14#include <functional>
15#include <promeki/function.h>
16#include <promeki/namespace.h>
17#include <promeki/error.h>
18#include <promeki/sharedptr.h>
19#include <promeki/atomic.h>
20#include <promeki/mutex.h>
22#include <promeki/memdomain.h>
24
25PROMEKI_NAMESPACE_BEGIN
26
27class EventLoop;
28
40#define PROMEKI_BUFFER_COMMAND(NAME, KIND_TAG) \
41public: \
42 Kind kind() const override { \
43 return KIND_TAG; \
44 } \
45 NAME *_promeki_clone() const { \
46 assert(false && #NAME " should not be cloned"); \
47 return nullptr; \
48 }
49
87class BufferCommand {
88 public:
90 enum Kind {
91 Map,
92 Unmap,
93 Copy,
94 };
95
97 using Ptr = SharedPtr<BufferCommand, false>;
98
103 RefCount _promeki_refct;
104
111 BufferCommand *_promeki_clone() const {
112 assert(false && "BufferCommand should never be cloned");
113 return nullptr;
114 }
115
116 BufferCommand() = default;
117 virtual ~BufferCommand() = default;
118
120 virtual Kind kind() const = 0;
121
128 static const char *kindName(Kind k);
129
131 const char *kindName() const { return kindName(kind()); }
132
133 // ---- Cancellation ----
134
146 Atomic<bool> cancelled{false};
147
148 // ---- Output (filled by the backend) ----
149
158 Error result = Error::Ok;
159
160 // ---- Completion ----
161
170 void markCompleted();
171
181 Error waitForCompletion(unsigned int timeoutMs = 0) const;
182
184 bool isCompleted() const { return _completed.value(); }
185
195 void setCompletionCallback(Function<void(Error)> cb, EventLoop *loop);
196
197 private:
198 Atomic<bool> _completed{false};
199 mutable Mutex _completionMutex;
200 mutable WaitCondition _completionCv;
201 Mutex _callbackMutex;
202 Function<void(Error)> _callback;
203 EventLoop *_callbackLoop = nullptr;
204};
205
222class BufferMapCommand : public BufferCommand {
223 PROMEKI_BUFFER_COMMAND(BufferMapCommand, Map)
224 public:
225 // ---- Inputs ----
226
228 MemDomain target;
230 MapFlags flags = MapFlags::Read;
231
232 // ---- Outputs ----
233
240 void *hostPtr = nullptr;
241};
242
256class BufferCopyCommand : public BufferCommand {
257 PROMEKI_BUFFER_COMMAND(BufferCopyCommand, Copy)
258 public:
259 // ---- Inputs ----
260
262 size_t bytes = 0;
264 size_t srcOffset = 0;
266 size_t dstOffset = 0;
267};
268
279class BufferUnmapCommand : public BufferCommand {
280 PROMEKI_BUFFER_COMMAND(BufferUnmapCommand, Unmap)
281 public:
282 // ---- Inputs ----
283
285 MemDomain target;
286};
287
288PROMEKI_NAMESPACE_END
289
290#endif // PROMEKI_ENABLE_CORE