-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathoutputwidget.cpp
More file actions
415 lines (341 loc) · 10.8 KB
/
Copy pathoutputwidget.cpp
File metadata and controls
415 lines (341 loc) · 10.8 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <iostream>
#include <sys/mman.h>
#include "outputwidget.hpp"
#include "gdk/wayland/gdkwayland.h"
#include "stream-chooser.hpp"
/* Copy Capture Callbacks */
static void frame_handle_transform(void*,
struct ext_image_copy_capture_frame_v1*,
uint32_t)
{}
static void frame_handle_damage(void*,
struct ext_image_copy_capture_frame_v1*,
int32_t, int32_t, int32_t, int32_t)
{}
static void frame_handle_presentation_time(void*,
struct ext_image_copy_capture_frame_v1*,
uint32_t, uint32_t, uint32_t)
{}
static void frame_handle_ready(void *data,
struct ext_image_copy_capture_frame_v1 *frame)
{
WayfireChooserOutput *output = (WayfireChooserOutput*)data;
output->buffer_ready();
ext_image_copy_capture_frame_v1_destroy(frame);
output->frame = nullptr;
}
static void frame_handle_failed(void *data,
struct ext_image_copy_capture_frame_v1 *frame,
uint32_t reason)
{
WayfireChooserOutput *output = (WayfireChooserOutput*)data;
ext_image_copy_capture_frame_v1_destroy(frame);
output->frame = nullptr;
}
static const struct ext_image_copy_capture_frame_v1_listener frame_listener = {
.transform = frame_handle_transform,
.damage = frame_handle_damage,
.presentation_time = frame_handle_presentation_time,
.ready = frame_handle_ready,
.failed = frame_handle_failed,
};
static void session_handle_buffer_size(void *data,
struct ext_image_copy_capture_session_v1*,
uint32_t width, uint32_t height)
{
WayfireChooserOutput *output = (WayfireChooserOutput*)data;
output->current_buffer_width = width;
output->current_buffer_height = height;
}
static void session_handle_shm_format(void *data,
struct ext_image_copy_capture_session_v1*,
uint32_t format)
{
WayfireChooserOutput *output = (WayfireChooserOutput*)data;
output->current_buffer_format = format;
}
static void session_handle_dmabuf_device(void*,
struct ext_image_copy_capture_session_v1*,
struct wl_array*)
{}
static void session_handle_dmabuf_format(void*,
struct ext_image_copy_capture_session_v1*,
uint32_t,
struct wl_array*)
{}
static void session_handle_done(void *data,
struct ext_image_copy_capture_session_v1*)
{}
static void session_handle_stopped(void*,
struct ext_image_copy_capture_session_v1 *session)
{}
static const struct ext_image_copy_capture_session_v1_listener recording_session_listener = {
.buffer_size = session_handle_buffer_size,
.shm_format = session_handle_shm_format,
.dmabuf_device = session_handle_dmabuf_device,
.dmabuf_format = session_handle_dmabuf_format,
.done = session_handle_done,
.stopped = session_handle_stopped,
};
static void frame_handle_linux_dmabuf(uint32_t width, uint32_t height, WayfireChooserOutput *output)
{
auto format = (output->current_buffer_format == WL_SHM_FORMAT_XRGB8888) ?
GBM_FORMAT_XRGB8888 : GBM_FORMAT_ARGB8888;
auto buffer = output->buffer;
if (buffer->gbm_fd > 0)
{
close(buffer->gbm_fd);
buffer->gbm_fd = -1;
}
if (buffer->bo)
{
gbm_bo_destroy(buffer->bo);
buffer->bo = nullptr;
}
if (buffer->buffer)
{
wl_buffer_destroy(buffer->buffer);
buffer->buffer = nullptr;
}
if (buffer->params)
{
zwp_linux_buffer_params_v1_destroy(buffer->params);
buffer->params = nullptr;
}
auto w = width;
auto h = height;
const uint64_t modifier = 0; // DRM_FORMAT_MOD_LINEAR
buffer->bo = gbm_bo_create_with_modifiers(WayfireStreamChooserApp::getInstance().gbm_device_ptr, w, h,
format, &modifier, 1);
if (buffer->bo == NULL)
{
buffer->bo = gbm_bo_create(WayfireStreamChooserApp::getInstance().gbm_device_ptr, w, h,
format, GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING);
}
if (buffer->bo == NULL)
{
perror("failed to create gbm bo");
return;
}
buffer->width = gbm_bo_get_width(buffer->bo);
buffer->height = gbm_bo_get_height(buffer->bo);
buffer->stride = gbm_bo_get_stride(buffer->bo);
buffer->params = zwp_linux_dmabuf_v1_create_params(WayfireStreamChooserApp::getInstance().dmabuf);
uint64_t mod = gbm_bo_get_modifier(buffer->bo);
buffer->gbm_fd = gbm_bo_get_fd(buffer->bo);
zwp_linux_buffer_params_v1_add(buffer->params,
buffer->gbm_fd, 0,
gbm_bo_get_offset(buffer->bo, 0),
gbm_bo_get_stride(buffer->bo),
mod >> 32, mod & 0xffffffff);
buffer->buffer = zwp_linux_buffer_params_v1_create_immed(buffer->params, w, h, format, 0);
}
void WayfireChooserOutput::start_output_source_ssession()
{
copy_capture_source = ext_output_image_capture_source_manager_v1_create_source(
WayfireStreamChooserApp::getInstance().output_capture_manager,
output_handle);
recording_session = ext_image_copy_capture_manager_v1_create_session(
WayfireStreamChooserApp::getInstance().manager,
copy_capture_source,
0);
ext_image_copy_capture_session_v1_add_listener(recording_session, &recording_session_listener, this);
}
void WayfireChooserOutput::stream()
{
streaming = true;
}
void WayfireChooserOutput::pause()
{
streaming = false;
}
WayfireChooserOutput::WayfireChooserOutput(std::shared_ptr<Gdk::Monitor> output) : output(output)
{
append(contents);
append(model);
append(connector);
set_valign(Gtk::Align::FILL);
set_halign(Gtk::Align::FILL);
output_handle = gdk_wayland_monitor_get_wl_output(output->gobj());
model.set_label(output->get_model());
connector.set_label(output->get_connector());
set_orientation(Gtk::Orientation::VERTICAL);
signals.push_back(output->signal_invalidate().connect([=]
{
WayfireStreamChooserApp::getInstance().remove_output(output->get_connector());
}));
signals.push_back(WayfireStreamChooserApp::getInstance().signal_resize().connect(
[=] (int width, int height)
{
set_size_request(-1, height / 3 + height * 0.075);
}));
buffer = std::make_shared<output_buffer>();
start_output_source_ssession();
initial_timeout = Glib::signal_timeout().connect(
[this] ()
{
this->pause();
return G_SOURCE_REMOVE;
}, 2000);
signals.push_back(
WayfireStreamChooserApp::getInstance().screen_list.signal_selected_children_changed().connect(
[=] ()
{
if (WayfireStreamChooserApp::getInstance().screen_list.get_selected_children()[0]->get_children()[0]
==
this)
{
this->initial_timeout.disconnect();
this->pause_timeout.disconnect();
this->stream();
return;
}
this->pause_timeout.disconnect();
this->pause_timeout = Glib::signal_timeout().connect(
[this] ()
{
this->pause();
return G_SOURCE_REMOVE;
}, 2000);
}));
auto motion_controller = Gtk::EventControllerMotion::create();
signals.push_back(motion_controller->signal_enter().connect(
[this] (double, double)
{
this->initial_timeout.disconnect();
this->pause_timeout.disconnect();
this->stream();
}));
add_controller(motion_controller);
motion_controller = Gtk::EventControllerMotion::create();
signals.push_back(motion_controller->signal_leave().connect(
[this] ()
{
this->pause_timeout.disconnect();
this->pause_timeout = Glib::signal_timeout().connect(
[this] ()
{
if (WayfireStreamChooserApp::getInstance().screen_list.get_selected_children()[0]->get_children()[
0] == this)
{
return G_SOURCE_REMOVE;
}
this->pause();
return G_SOURCE_REMOVE;
}, 2000);
}));
add_controller(motion_controller);
}
WayfireChooserOutput::~WayfireChooserOutput()
{
if (frame)
{
ext_image_copy_capture_frame_v1_destroy(frame);
}
if (copy_capture_source)
{
ext_image_capture_source_v1_destroy(copy_capture_source);
}
if (recording_session)
{
ext_image_copy_capture_session_v1_destroy(recording_session);
}
if (buffer->gbm_fd > 0)
{
close(buffer->gbm_fd);
}
if (buffer->bo)
{
gbm_bo_destroy(buffer->bo);
}
if (buffer->buffer)
{
wl_buffer_destroy(buffer->buffer);
}
if (buffer->params)
{
zwp_linux_buffer_params_v1_destroy(buffer->params);
}
for (auto signal : signals)
{
signal.disconnect();
}
}
void WayfireChooserOutput::print()
{
std::cout << "Monitor: " << output->get_connector() << std::endl;
exit(0);
}
void WayfireChooserOutput::frame_request()
{
if (!streaming)
{
return;
}
if ((current_buffer_width <= 0) || (current_buffer_height <= 0))
{
printf("%s invalid size\n", __func__);
return;
}
if (!recording_session)
{
return;
}
bool dirty = (buffer->width != current_buffer_width) || (buffer->height != current_buffer_height) ||
!buffer->buffer;
buffer->width = current_buffer_width;
buffer->height = current_buffer_height;
if (dirty)
{
frame_handle_linux_dmabuf(buffer->width, buffer->height, this);
}
if (!buffer->buffer)
{
return;
}
if (frame)
{
ext_image_copy_capture_frame_v1_destroy(frame);
frame = NULL;
}
frame = ext_image_copy_capture_session_v1_create_frame(recording_session);
buffer->frame = frame;
ext_image_copy_capture_frame_v1_add_listener(buffer->frame, &frame_listener, this);
ext_image_copy_capture_frame_v1_attach_buffer(buffer->frame, buffer->buffer);
ext_image_copy_capture_frame_v1_damage_buffer(buffer->frame, 0, 0, buffer->width, buffer->height);
ext_image_copy_capture_frame_v1_capture(buffer->frame);
}
void WayfireChooserOutput::buffer_ready()
{
if ((buffer == nullptr) || (buffer->buffer == nullptr))
{
printf("%s buffer null\n", __func__);
return;
}
uint32_t stride = 0;
void *map_data = NULL;
void *data = gbm_bo_map(buffer->bo, 0, 0, buffer->width, buffer->height,
GBM_BO_TRANSFER_READ, &stride, &map_data);
if (!data)
{
perror("failed to map bo");
return;
}
/* buffer->data is now valid */
std::shared_ptr<Glib::Bytes> bytes = 0;
size_t size = stride * buffer->height;
bytes = Glib::Bytes::create((unsigned char*)data, size);
gbm_bo_unmap(buffer->bo, map_data);
if (!bytes)
{
return;
}
auto builder = Gdk::MemoryTextureBuilder::create();
builder->set_bytes(bytes);
builder->set_width(buffer->width);
builder->set_height(buffer->height);
builder->set_stride(stride);
builder->set_format(Gdk::MemoryFormat::B8G8R8A8);
auto texture = builder->build();
contents.set_paintable(texture);
}