-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmain.cpp
More file actions
395 lines (344 loc) · 13.3 KB
/
main.cpp
File metadata and controls
395 lines (344 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2022 Metrological
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#define MODULE_NAME GraphicsBufferTestApp
#include <graphicsbuffer/GraphicsBufferType.h>
using namespace Thunder;
MODULE_NAME_ARCHIVE_DECLARATION
namespace Test {
const char* descriptors[] = {
_T("/tmp/buffer1.txt"),
_T("/tmp/buffer2.txt"),
};
const char bridgeConnector[] = _T("/tmp/connector");
class ServerBuffer : public Graphics::ServerBufferType<4> {
private:
using BaseClass = Graphics::ServerBufferType<4>;
protected:
ServerBuffer(
const uint32_t width, const uint32_t height,
const uint32_t format, const uint64_t modifier,
const Exchange::IGraphicsBuffer::DataType type)
: BaseClass(width, height, format, modifier, type)
, _dirty(false)
{
printf("Constructing server buffer.\n");
for (uint8_t index = 0; index < (sizeof(descriptors) / sizeof(const char*)); index++) {
Core::File file(string(descriptors[index]));
if (file.Create(Core::File::USER_READ | Core::File::USER_WRITE) == true) {
BaseClass::Add(static_cast<Core::File::Handle>(file), 0xAAAA + index, 0x5555 + index);
printf("Opening: [%d] -> name: %s\n", static_cast<Core::File::Handle>(file), descriptors[index]);
}
}
// Let's start monitoring the ServerBuffer to detect changes
Core::ResourceMonitor::Instance().Register(*this);
}
public:
ServerBuffer() = delete;
ServerBuffer(ServerBuffer&&) = delete;
ServerBuffer(const ServerBuffer&) = delete;
ServerBuffer& operator=(ServerBuffer&&) = delete;
ServerBuffer& operator=(const ServerBuffer&) = delete;
static Core::ProxyType<ServerBuffer> Create(const string& callsign, const uint32_t width, const uint32_t height, const Exchange::IGraphicsBuffer::DataType type)
{
return (_map.Instance<ServerBuffer>(callsign, width, height, 0xAA, 0x55, type));
}
static Core::ProxyType<ServerBuffer> Find(const string& callsign)
{
return (_map.Find(callsign));
}
~ServerBuffer() override
{
// Let's stop monitoring the ServerBuffer to detect changes
Core::ResourceMonitor::Instance().Unregister(*this);
printf("Destructing server buffer.\n");
}
public:
bool IsDirty() const
{
return (_dirty);
}
IIterator* Acquire()
{
IIterator* result = BaseClass::Acquire(0);
if (result != nullptr) {
_dirty = false;
}
return (result);
}
void Request() override
{
printf("We need to do our magic here :-)\n");
_dirty = true;
std::this_thread::sleep_for(std::chrono::milliseconds(4));
if (Rendered() == true) {
printf("Request rendered.\n");
std::this_thread::sleep_for(std::chrono::milliseconds(12));
if (Published() == true){
printf("Request published.\n");
} else {
printf("Request failed to publish.\n");
}
} else {
printf("Request failed to render.\n");
};
}
private:
std::atomic<bool> _dirty;
static Core::ProxyMapType<string, ServerBuffer> _map;
};
/* static */ Core::ProxyMapType<string, ServerBuffer> ServerBuffer::_map;
class ClientBuffer : public Graphics::ClientBufferType<4> {
private:
using BaseClass = Graphics::ClientBufferType<4>;
protected:
ClientBuffer(Core::PrivilegedRequest::Container& descriptors)
: BaseClass()
{
BaseClass::Load(descriptors);
// Let's start monitoring the ClientBuffer to detect changes
Core::ResourceMonitor::Instance().Register(*this);
}
public:
~ClientBuffer() override
{
// Let's stop monitoring the ClientBuffer to detect changes
Core::ResourceMonitor::Instance().Unregister(*this);
printf("Destructing client buffer.\n");
}
ClientBuffer() = delete;
ClientBuffer(ClientBuffer&&) = delete;
ClientBuffer(const ClientBuffer&) = delete;
ClientBuffer& operator=(const ClientBuffer&) = delete;
static Core::ProxyType<Exchange::IGraphicsBuffer> Create(Core::PrivilegedRequest::Container& descriptors)
{
return (Core::ProxyType<Exchange::IGraphicsBuffer>(Core::ProxyType<ClientBuffer>::Create(descriptors)));
}
public:
// seems we have been Rendered
void Rendered() override
{
printf("My buffer is rendered but might not be visible yet.\n");
}
// seems we have been Rendered
void Published() override
{
printf("My buffer is published so its now visible.\n");
}
};
class Dispatcher : public Core::PrivilegedRequest {
private:
class Callback : public Core::PrivilegedRequest::ICallback {
public:
Callback() = delete;
Callback(Callback&&) = delete;
Callback(const Callback&) = delete;
Callback& operator=(Callback&&) = delete;
Callback& operator=(const Callback&) = delete;
Callback(Dispatcher& parent) : _parent(parent) {}
~Callback() override = default;
public:
void Request(const uint32_t id, Container& descriptors) override {
_parent.Request(id, descriptors);
}
void Offer(const uint32_t id, Container&& descriptors) override {
_parent.Offer(id, std::move(descriptors));
}
private:
Dispatcher& _parent;
};
public:
Dispatcher(Dispatcher&&) = delete;
Dispatcher(const Dispatcher&) = delete;
Dispatcher& operator=(Dispatcher&&) = delete;
Dispatcher& operator=(const Dispatcher&) = delete;
Dispatcher()
: Core::PrivilegedRequest(&_callback)
, _callback(*this) {
}
~Dispatcher() override = default;
void Add(const uint32_t id, Graphics::ServerBufferType<4>& buffer)
{
_id = id;
_buffer = &buffer;
}
void Remove(const uint32_t id)
{
if (_id == id) {
_id = 0;
_buffer = nullptr;
}
}
uint32_t Request(const uint32_t waitTime, const string& identifier, const uint32_t requestId, Container& fds) {
return (Core::PrivilegedRequest::Request(waitTime, identifier, requestId, fds));
}
private:
void Request(const uint32_t id, Container& descriptors) {
if ((id == _id) && (_buffer != nullptr)) {
int container[Core::PrivilegedRequest::MaxDescriptorsPerRequest];
uint8_t result = _buffer->Descriptors(sizeof(container), container);
if (result > 0) {
printf("Handing out: %d descriptors: [", result);
for (uint8_t index = 0; index < (result - 1); index++) {
descriptors.emplace_back(container[index]);
printf("%d,", container[index]);
}
printf("%d]\n", container[result - 1]);
}
}
}
void Offer(const uint32_t id VARIABLE_IS_NOT_USED, Container&& descriptors VARIABLE_IS_NOT_USED)
{
}
private:
uint32_t _id;
Graphics::ServerBufferType<4>* _buffer;
Callback _callback;
};
} // namespace Test
bool server = false;
uint32_t bufferId = 0;
bool ParseOptions(int argc, const char** argv)
{
int index = 1;
bool showHelp = false;
while ((index < argc) && (showHelp == false)) {
if (strcmp(argv[index], "-server") == 0) {
server = true;
} else if (strcmp(argv[index], "-b") == 0) {
if (((index + 1) < argc) && (argv[index + 1][0] != '-')) {
index++;
bufferId = atoi(argv[index]);
}
} else {
showHelp = true;
}
index++;
}
if (showHelp == true) {
printf("Test passing file descriptors and information over a process boundary.\n");
printf("%s [-server] [-b <number>]\n", argv[0]);
printf(" -server Act as a server.\n");
printf(" -b <number> Act as a client and get the buffer associated with this number.\n");
}
return (showHelp == false);
}
int main(int argc, const char* argv[])
{
{
TCHAR element;
ParseOptions(argc, argv);
printf("Running as: [%s]\n", server ? _T("server") : _T("client"));
Test::Dispatcher bridge;
Core::ProxyType<Test::ServerBuffer> serverBuffer;
Core::ProxyType<Exchange::IGraphicsBuffer> buffer;
if (server == true) {
serverBuffer = Test::ServerBuffer::Create(_T("TestThing"), 1024, 1080, Exchange::IGraphicsBuffer::TYPE_RAW);
buffer = Core::ProxyType<Exchange::IGraphicsBuffer>(serverBuffer);
bridge.Open(string(Test::bridgeConnector));
printf("Server has created a buffer, known as: [%d]\n", bufferId);
bridge.Add(bufferId, *serverBuffer);
} else {
printf("Client instantiated to get information of buffer: [%d]\n", bufferId);
}
do {
printf("\n>");
element = toupper(getchar());
switch (element) {
case 'O':
if (buffer.IsValid() == false) {
Core::PrivilegedRequest::Container descriptors;
printf("Get the buffer interface, Sesame open:\n");
if (bridge.Request(1000, string(Test::bridgeConnector), bufferId, descriptors) == Core::ERROR_NONE) {
printf("Got the buffer on client side.\n");
buffer = Test::ClientBuffer::Create(descriptors);
} else {
printf("Failed to get the buffer.\n");
}
} else if (server == false) {
printf("Where done, we put it all in, close the vault, Sesame close:\n");
buffer.Release();
}
break;
case 'I':
if (buffer.IsValid() == false) {
printf("There are no buffers\n");
} else {
printf("Buffer info:\n");
printf("==========================================\n");
printf("Width: %d\n", buffer->Width());
printf("Height: %d\n", buffer->Height());
printf("Format: %d\n", buffer->Format());
printf("Type: %d\n", buffer->Type());
Core::ProxyType<Test::ServerBuffer> info = Core::ProxyType<Test::ServerBuffer>(buffer);
if (info != nullptr) {
printf("Dirty: %s\n", info->IsDirty() ? _T("true") : _T("false"));
}
}
break;
case 'W':
if (buffer == nullptr) {
printf("There are no buffers\n");
} else {
Exchange::IGraphicsBuffer::IIterator* planes = buffer->Acquire(10);
if (planes != nullptr) {
printf("Iterating ove the planes to write:\n");
while (planes->Next() == true) {
int fd = planes->Descriptor();
printf("Writing to [%d]:\n", fd);
PUSH_WARNING(DISABLE_WARNING_UNUSED_RESULT)
::write(fd, "Hello World !!!\n", 16);
POP_WARNING()
::fsync(fd);
}
}
buffer->Relinquish();
if (server == false) {
Core::ProxyType<Graphics::ClientBufferType<4> > info = Core::ProxyType<Graphics::ClientBufferType<4> >(buffer);
if (info != nullptr) {
printf("Request to render.\n");
info->RequestRender();
}
} else {
Core::ProxyType<Graphics::ServerBufferType<4> > info = Core::ProxyType<Graphics::ServerBufferType<4> >(buffer);
if (info != nullptr) {
printf("Render request handled.\n");
info->Rendered();
info->Published();
}
}
}
break;
case 'Q':
break;
case '?':
printf("Options available:\n");
printf("=======================================================================\n");
printf("<O> Open/Close the buffer.\n");
printf("<I> Information of the buffer.\n");
printf("<W> Write a the number in a file.\n");
printf("<?> Have no clue what I can do, tell me.\n");
break;
default:
break;
}
} while (element != 'Q');
}
Core::Singleton::Dispose();
return EXIT_SUCCESS;
}