-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrenderer-backend.cpp
More file actions
477 lines (409 loc) · 13.1 KB
/
renderer-backend.cpp
File metadata and controls
477 lines (409 loc) · 13.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (C) 2015, 2016 Igalia S.L.
* Copyright (C) 2015, 2016 Metrological
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define WL_EGL_PLATFORM 1
#include <wpe/wpe-egl.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <glib.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#ifndef NDEBUG
#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG_PRINT(...) (void(0))
#endif
#if defined(WPE_BACKEND_MESA)
#include <gbm.h>
#include <fcntl.h>
#include <unistd.h>
#define DEFAULT_CARD "/dev/dri/card0"
#define DEFAULT_MODE_WIDTH (1280)
#define DEFAULT_MODE_HEIGHT (720)
namespace GBM{
struct EGLOffscreenTarget {
~EGLOffscreenTarget()
{
if (surface)
gbm_surface_destroy(surface);
}
struct gbm_surface* surface;
void initialize(struct gbm_device* device, uint32_t width, uint32_t height)
{
if (device)
surface = gbm_surface_create(device, width, height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
else{
DEBUG_PRINT("EGLOffscreenTarget: gbm device is null\n");
return;
}
if (!surface)
DEBUG_PRINT("EGLOffscreenTarget: gbm surface created failed\n");
}
};
struct Backend {
Backend()
{
fd = open(DEFAULT_CARD, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
if (fd < 0){
DEBUG_PRINT("Backend: DRM device open failure\n");
return;
}
device = gbm_create_device(fd);
if (!device) {
DEBUG_PRINT("Backend: gbm device not created\n");
close(fd);
return;
}
}
~Backend()
{
if (device)
gbm_device_destroy(device);
if (fd >= 0)
close(fd);
}
int fd { -1 };
struct gbm_device* device;
};
}
#endif
namespace Westeros {
class EventSource {
public:
static GSourceFuncs sourceFuncs;
GSource source;
GPollFD pfd;
struct wl_display* display;
bool prepared;
};
GSourceFuncs EventSource::sourceFuncs = {
// prepare
[](GSource* base, gint* timeout) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
if (source->prepared)
return FALSE;
*timeout = -1;
while (wl_display_prepare_read(display) != 0) {
if (wl_display_dispatch_pending(display) < 0) {
DEBUG_PRINT("Wayland::Display: error in wayland prepare\n");
return FALSE;
}
}
wl_display_flush(display);
source->prepared = true;
return FALSE;
},
// check
[](GSource* base) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
if (!source->prepared)
return FALSE;
if (source->pfd.revents & G_IO_IN) {
if (wl_display_read_events(display) < 0) {
DEBUG_PRINT("Wayland::Display: error in wayland read\n");
return FALSE;
}
source->prepared = false;
return TRUE;
} else {
wl_display_cancel_read(display);
source->prepared = false;
return FALSE;
}
},
// dispatch
[](GSource* base, GSourceFunc, gpointer) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
if (source->pfd.revents & G_IO_IN) {
if (wl_display_dispatch_pending(display) < 0) {
DEBUG_PRINT("Wayland::Display: error in wayland dispatch\n");
return G_SOURCE_REMOVE;
}
}
if (source->pfd.revents & (G_IO_ERR | G_IO_HUP))
return G_SOURCE_REMOVE;
source->pfd.revents = 0;
return G_SOURCE_CONTINUE;
},
nullptr, // finalize
nullptr, // closure_callback
nullptr, // closure_marshall
};
class Backend {
public:
Backend();
~Backend();
struct wl_display* display() const { return m_display; }
struct wl_compositor* compositor() const { return m_compositor; }
void initialize();
void invalidate();
private:
static struct wl_registry_listener s_registryListener;
struct wl_display* m_display { nullptr };
struct wl_registry* m_registry { nullptr };
struct wl_compositor* m_compositor { nullptr };
GSource* m_eventSource { nullptr };
};
Backend::Backend()
{
const char* targetDisplay = getenv("WAYLAND_DISPLAY");
m_display = wl_display_connect(targetDisplay);
if (!m_display)
return;
m_registry = wl_display_get_registry(m_display);
wl_registry_add_listener(m_registry, &s_registryListener, this);
wl_display_roundtrip(m_display);
}
Backend::~Backend()
{
invalidate();
if (m_compositor)
wl_compositor_destroy(m_compositor);
if (m_registry)
wl_registry_destroy(m_registry);
if (m_display)
wl_display_disconnect(m_display);
}
void Backend::invalidate()
{
if (m_eventSource) {
if (m_eventSource->prepared && m_display)
wl_display_cancel_read(m_display);
g_source_destroy(m_eventSource);
m_eventSource = nullptr;
}
}
void Backend::initialize()
{
if (m_eventSource != nullptr)
return;
m_eventSource = g_source_new(&EventSource::sourceFuncs, sizeof(EventSource));
auto& source = *reinterpret_cast<EventSource*>(m_eventSource);
source.display = m_display;
source.prepared = false;
source.pfd.fd = wl_display_get_fd(m_display);
source.pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP;
source.pfd.revents = 0;
g_source_add_poll(m_eventSource, &source.pfd);
g_source_set_name(m_eventSource, "[WPE] WaylandDisplay");
g_source_set_priority(m_eventSource, G_PRIORITY_HIGH + 30);
g_source_set_can_recurse(m_eventSource, TRUE);
g_source_attach(m_eventSource, g_main_context_get_thread_default());
}
struct wl_registry_listener Backend::s_registryListener = {
// global
[](void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t)
{
auto& backend = *static_cast<Backend*>(data);
if (!std::strcmp(interface, "wl_compositor"))
backend.m_compositor = static_cast<struct wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
},
// global_remove
[](void*, struct wl_registry*, uint32_t)
{
// FIXME: if this can happen without the UI Process getting shut down
// we should probably destroy our cached display instance.
},
};
class EGLTarget {
public:
EGLTarget(struct wpe_renderer_backend_egl_target*);
virtual ~EGLTarget();
void initialize(const Backend&, uint32_t, uint32_t);
struct wl_egl_window* nativeWindow() const { return m_window; }
void resize(uint32_t width, uint32_t height);
void frameWillRender();
void frameRendered();
private:
static const struct wl_callback_listener s_frameListener;
struct wpe_renderer_backend_egl_target* m_target;
const Backend* m_backend;
struct wl_surface* m_surface;
struct wl_egl_window* m_window;
};
EGLTarget::EGLTarget(struct wpe_renderer_backend_egl_target* target)
: m_target(target)
{
}
EGLTarget::~EGLTarget()
{
if (m_window)
wl_egl_window_destroy(m_window);
if (m_surface)
wl_surface_destroy(m_surface);
if (m_backend && m_backend->display()) {
wl_display_flush(m_backend->display());
wl_display_roundtrip(m_backend->display());
const_cast<Backend&>(*m_backend).invalidate();
}
}
void EGLTarget::initialize(const Backend& backend, uint32_t width, uint32_t height)
{
const_cast<Backend&>(backend).initialize();
m_backend = &backend;
m_surface = wl_compositor_create_surface(backend.compositor());
if (m_surface)
m_window = wl_egl_window_create(m_surface, width, height);
}
void EGLTarget::resize(uint32_t width, uint32_t height)
{
if (m_window) {
wl_egl_window_resize(m_window, width, height, 0, 0);
if (m_backend && m_backend->display())
wl_display_flush(m_backend->display());
}
}
void EGLTarget::frameWillRender()
{
struct wl_callback* frameCallback = wl_surface_frame(m_surface);
wl_callback_add_listener(frameCallback, &s_frameListener, m_target);
}
void EGLTarget::frameRendered()
{
if (m_backend && m_backend->display())
wl_display_flush(m_backend->display());
}
const struct wl_callback_listener EGLTarget::s_frameListener = {
// frame
[](void* data, struct wl_callback* callback, uint32_t)
{
wl_callback_destroy(callback);
auto* target = static_cast<struct wpe_renderer_backend_egl_target*>(data);
wpe_renderer_backend_egl_target_dispatch_frame_complete(target);
},
};
} // namespace Westeros
extern "C" {
struct wpe_renderer_backend_egl_interface westeros_renderer_backend_egl_interface = {
// create
[](int) -> void*
{
return new Westeros::Backend;
},
// destroy
[](void* data)
{
auto* backend = static_cast<Westeros::Backend*>(data);
delete backend;
},
// get_native_display
[](void* data) -> EGLNativeDisplayType
{
auto& backend = *static_cast<Westeros::Backend*>(data);
return backend.display();
},
};
struct wpe_renderer_backend_egl_target_interface westeros_renderer_backend_egl_target_interface = {
// create
[](struct wpe_renderer_backend_egl_target* target, int) -> void*
{
return new Westeros::EGLTarget(target);
},
// destroy
[](void* data)
{
auto* target = static_cast<Westeros::EGLTarget*>(data);
delete target;
},
// initialize
[](void* data, void* backend_data, uint32_t width, uint32_t height)
{
auto& target = *static_cast<Westeros::EGLTarget*>(data);
const auto& backend = *static_cast<Westeros::Backend*>(backend_data);
target.initialize(backend, width, height);
},
// get_native_window
[](void* data) -> EGLNativeWindowType
{
auto& target = *static_cast<Westeros::EGLTarget*>(data);
return (EGLNativeWindowType)target.nativeWindow();
},
// resize
[](void* data, uint32_t width, uint32_t height)
{
auto& target = *static_cast<Westeros::EGLTarget*>(data);
target.resize(width, height);
},
// frame_will_render
[](void* data)
{
auto& target = *static_cast<Westeros::EGLTarget*>(data);
target.frameWillRender();
},
// frame_rendered
[](void* data)
{
auto& target = *static_cast<Westeros::EGLTarget*>(data);
target.frameRendered();
},
};
struct wpe_renderer_backend_egl_offscreen_target_interface westeros_renderer_backend_egl_offscreen_target_interface = {
// create
[]() -> void*
{
#if defined(WPE_BACKEND_MESA)
return new GBM::EGLOffscreenTarget;
#else
return nullptr;
#endif
},
// destroy
[](void* data)
{
#if defined(WPE_BACKEND_MESA)
auto* target = static_cast<GBM::EGLOffscreenTarget*>(data);
delete target;
#endif
},
// initialize
[](void* data, void* backend_data)
{
#if defined(WPE_BACKEND_MESA)
auto* backend = new GBM::Backend;
auto* target = static_cast<GBM::EGLOffscreenTarget*>(data);
target->initialize(backend->device, DEFAULT_MODE_WIDTH, DEFAULT_MODE_HEIGHT);
#endif
},
// get_native_window
[](void* data) -> EGLNativeWindowType
{
#if defined(WPE_BACKEND_MESA)
auto* target = static_cast<GBM::EGLOffscreenTarget*>(data);
DEBUG_PRINT("westeros_renderer_backend_egl_offscreen_target_interface: native window %x\n", target->surface);
return (EGLNativeWindowType)target->surface;
#else
return (EGLNativeWindowType)nullptr;
#endif
},
};
}