2018-10-17 08:33:19 +02:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2017-08-13 23:05:57 +02:00
|
|
|
#include <assert.h>
|
2017-06-13 04:22:40 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2018-10-01 22:57:36 +02:00
|
|
|
#include <time.h>
|
2017-06-13 04:22:40 +02:00
|
|
|
#include <wlr/backend/interface.h>
|
2021-10-22 22:14:37 +02:00
|
|
|
#include <wlr/types/wlr_buffer.h>
|
2017-06-21 18:10:07 +02:00
|
|
|
#include <wlr/util/log.h>
|
2021-10-22 22:14:37 +02:00
|
|
|
#include "backend/backend.h"
|
2018-02-12 21:29:23 +01:00
|
|
|
#include "backend/multi.h"
|
2017-06-13 04:22:40 +02:00
|
|
|
|
|
|
|
struct subbackend_state {
|
|
|
|
struct wlr_backend *backend;
|
|
|
|
struct wlr_backend *container;
|
2018-02-12 10:36:43 +01:00
|
|
|
struct wl_listener new_input;
|
|
|
|
struct wl_listener new_output;
|
|
|
|
struct wl_listener destroy;
|
2017-10-14 21:55:45 +02:00
|
|
|
struct wl_list link;
|
2017-06-13 04:22:40 +02:00
|
|
|
};
|
|
|
|
|
2018-09-17 21:55:01 +02:00
|
|
|
static struct wlr_multi_backend *multi_backend_from_backend(
|
|
|
|
struct wlr_backend *wlr_backend) {
|
|
|
|
assert(wlr_backend_is_multi(wlr_backend));
|
2023-07-11 17:54:08 +02:00
|
|
|
struct wlr_multi_backend *backend = wl_container_of(wlr_backend, backend, backend);
|
|
|
|
return backend;
|
2018-09-17 21:55:01 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:28:47 +01:00
|
|
|
static bool multi_backend_start(struct wlr_backend *wlr_backend) {
|
2018-09-17 21:55:01 +02:00
|
|
|
struct wlr_multi_backend *backend = multi_backend_from_backend(wlr_backend);
|
2017-10-14 21:55:45 +02:00
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &backend->backends, link) {
|
2017-08-13 15:59:14 +02:00
|
|
|
if (!wlr_backend_start(sub->backend)) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to initialize backend.");
|
2017-06-13 04:22:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:33:26 +01:00
|
|
|
static void subbackend_state_destroy(struct subbackend_state *sub) {
|
2018-02-12 10:36:43 +01:00
|
|
|
wl_list_remove(&sub->new_input.link);
|
|
|
|
wl_list_remove(&sub->new_output.link);
|
|
|
|
wl_list_remove(&sub->destroy.link);
|
2017-12-20 00:33:26 +01:00
|
|
|
wl_list_remove(&sub->link);
|
|
|
|
free(sub);
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:28:47 +01:00
|
|
|
static void multi_backend_destroy(struct wlr_backend *wlr_backend) {
|
2018-09-17 21:55:01 +02:00
|
|
|
struct wlr_multi_backend *backend = multi_backend_from_backend(wlr_backend);
|
2018-01-30 19:45:57 +01:00
|
|
|
|
2017-12-28 17:54:30 +01:00
|
|
|
wl_list_remove(&backend->display_destroy.link);
|
2018-01-30 19:45:57 +01:00
|
|
|
|
2024-01-25 11:21:33 +01:00
|
|
|
wlr_backend_finish(wlr_backend);
|
|
|
|
|
2020-05-02 16:42:26 +02:00
|
|
|
// Some backends may depend on other backends, ie. destroying a backend may
|
|
|
|
// also destroy other backends
|
|
|
|
while (!wl_list_empty(&backend->backends)) {
|
|
|
|
struct subbackend_state *sub =
|
|
|
|
wl_container_of(backend->backends.next, sub, link);
|
2017-06-13 04:22:40 +02:00
|
|
|
wlr_backend_destroy(sub->backend);
|
|
|
|
}
|
2018-01-30 19:45:57 +01:00
|
|
|
|
2017-08-12 17:43:36 +02:00
|
|
|
free(backend);
|
2017-06-13 04:22:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:05:23 +01:00
|
|
|
static int multi_backend_get_drm_fd(struct wlr_backend *backend) {
|
|
|
|
struct wlr_multi_backend *multi = multi_backend_from_backend(backend);
|
|
|
|
|
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &multi->backends, link) {
|
|
|
|
if (sub->backend->impl->get_drm_fd) {
|
2021-04-12 10:39:51 +02:00
|
|
|
return wlr_backend_get_drm_fd(sub->backend);
|
2020-12-01 16:05:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-22 22:14:37 +02:00
|
|
|
static uint32_t multi_backend_get_buffer_caps(struct wlr_backend *backend) {
|
|
|
|
struct wlr_multi_backend *multi = multi_backend_from_backend(backend);
|
|
|
|
|
|
|
|
if (wl_list_empty(&multi->backends)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t caps = WLR_BUFFER_CAP_DATA_PTR | WLR_BUFFER_CAP_DMABUF
|
|
|
|
| WLR_BUFFER_CAP_SHM;
|
|
|
|
|
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &multi->backends, link) {
|
|
|
|
uint32_t backend_caps = backend_get_buffer_caps(sub->backend);
|
|
|
|
if (backend_caps != 0) {
|
|
|
|
// only count backend capable of presenting a buffer
|
|
|
|
caps = caps & backend_caps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:57:18 +01:00
|
|
|
static const struct wlr_backend_impl backend_impl = {
|
2017-08-13 15:59:14 +02:00
|
|
|
.start = multi_backend_start,
|
2017-08-11 04:15:37 +02:00
|
|
|
.destroy = multi_backend_destroy,
|
2020-12-01 16:05:23 +01:00
|
|
|
.get_drm_fd = multi_backend_get_drm_fd,
|
2021-10-22 22:14:37 +02:00
|
|
|
.get_buffer_caps = multi_backend_get_buffer_caps,
|
2017-06-13 04:22:40 +02:00
|
|
|
};
|
|
|
|
|
2017-12-21 14:13:36 +01:00
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_multi_backend *backend =
|
|
|
|
wl_container_of(listener, backend, display_destroy);
|
|
|
|
multi_backend_destroy((struct wlr_backend*)backend);
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:25:46 +01:00
|
|
|
struct wlr_backend *wlr_multi_backend_create(struct wl_display *display) {
|
2023-10-03 07:51:07 +02:00
|
|
|
struct wlr_multi_backend *backend = calloc(1, sizeof(*backend));
|
2017-08-12 17:43:36 +02:00
|
|
|
if (!backend) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Backend allocation failed");
|
2017-06-13 04:22:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-11 08:51:19 +02:00
|
|
|
|
2017-10-14 21:55:45 +02:00
|
|
|
wl_list_init(&backend->backends);
|
2017-08-13 15:59:14 +02:00
|
|
|
wlr_backend_init(&backend->backend, &backend_impl);
|
2017-08-12 17:43:36 +02:00
|
|
|
|
2017-12-20 11:54:41 +01:00
|
|
|
wl_signal_init(&backend->events.backend_add);
|
|
|
|
wl_signal_init(&backend->events.backend_remove);
|
|
|
|
|
2017-12-21 14:13:36 +01:00
|
|
|
backend->display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(display, &backend->display_destroy);
|
|
|
|
|
2017-08-12 17:43:36 +02:00
|
|
|
return &backend->backend;
|
2017-06-13 04:22:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 05:08:35 +02:00
|
|
|
bool wlr_backend_is_multi(struct wlr_backend *b) {
|
|
|
|
return b->impl == &backend_impl;
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:36:43 +01:00
|
|
|
static void new_input_reemit(struct wl_listener *listener, void *data) {
|
2017-06-13 04:22:40 +02:00
|
|
|
struct subbackend_state *state = wl_container_of(listener,
|
2018-02-12 10:36:43 +01:00
|
|
|
state, new_input);
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&state->container->events.new_input, data);
|
2017-06-13 04:22:40 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:36:43 +01:00
|
|
|
static void new_output_reemit(struct wl_listener *listener, void *data) {
|
2017-06-13 04:22:40 +02:00
|
|
|
struct subbackend_state *state = wl_container_of(listener,
|
2018-02-12 10:36:43 +01:00
|
|
|
state, new_output);
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&state->container->events.new_output, data);
|
2017-06-13 04:22:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 00:33:26 +01:00
|
|
|
static void handle_subbackend_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
2018-02-12 10:36:43 +01:00
|
|
|
struct subbackend_state *state = wl_container_of(listener, state, destroy);
|
2017-12-20 00:33:26 +01:00
|
|
|
subbackend_state_destroy(state);
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:49:00 +01:00
|
|
|
static struct subbackend_state *multi_backend_get_subbackend(struct wlr_multi_backend *multi,
|
|
|
|
struct wlr_backend *backend) {
|
|
|
|
struct subbackend_state *sub = NULL;
|
|
|
|
wl_list_for_each(sub, &multi->backends, link) {
|
|
|
|
if (sub->backend == backend) {
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:08:08 +02:00
|
|
|
bool wlr_multi_backend_add(struct wlr_backend *_multi,
|
2017-06-13 04:22:40 +02:00
|
|
|
struct wlr_backend *backend) {
|
2021-11-19 16:44:45 +01:00
|
|
|
assert(_multi && backend);
|
|
|
|
assert(_multi != backend);
|
|
|
|
|
2018-09-17 21:55:01 +02:00
|
|
|
struct wlr_multi_backend *multi = multi_backend_from_backend(_multi);
|
2017-12-20 00:49:00 +01:00
|
|
|
|
|
|
|
if (multi_backend_get_subbackend(multi, backend)) {
|
|
|
|
// already added
|
2018-05-15 23:08:08 +02:00
|
|
|
return true;
|
2017-12-20 00:49:00 +01:00
|
|
|
}
|
|
|
|
|
2023-10-03 07:51:07 +02:00
|
|
|
struct subbackend_state *sub = calloc(1, sizeof(*sub));
|
2018-05-15 23:08:08 +02:00
|
|
|
if (sub == NULL) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Could not add backend: allocation failed");
|
2018-05-15 23:08:08 +02:00
|
|
|
return false;
|
2017-08-15 07:56:47 +02:00
|
|
|
}
|
2020-04-14 15:01:57 +02:00
|
|
|
wl_list_insert(multi->backends.prev, &sub->link);
|
2017-08-16 09:23:21 +02:00
|
|
|
|
2017-06-13 04:22:40 +02:00
|
|
|
sub->backend = backend;
|
2017-08-12 17:43:36 +02:00
|
|
|
sub->container = &multi->backend;
|
2017-06-13 04:22:40 +02:00
|
|
|
|
2018-02-12 10:36:43 +01:00
|
|
|
wl_signal_add(&backend->events.destroy, &sub->destroy);
|
|
|
|
sub->destroy.notify = handle_subbackend_destroy;
|
2017-12-20 00:33:26 +01:00
|
|
|
|
2018-02-12 10:36:43 +01:00
|
|
|
wl_signal_add(&backend->events.new_input, &sub->new_input);
|
|
|
|
sub->new_input.notify = new_input_reemit;
|
2017-12-20 00:33:26 +01:00
|
|
|
|
2018-02-12 10:36:43 +01:00
|
|
|
wl_signal_add(&backend->events.new_output, &sub->new_output);
|
|
|
|
sub->new_output.notify = new_output_reemit;
|
2017-12-20 11:54:41 +01:00
|
|
|
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&multi->events.backend_add, backend);
|
2018-05-15 23:08:08 +02:00
|
|
|
return true;
|
2017-06-13 04:22:40 +02:00
|
|
|
}
|
2017-08-06 03:37:49 +02:00
|
|
|
|
2017-12-20 00:49:00 +01:00
|
|
|
void wlr_multi_backend_remove(struct wlr_backend *_multi,
|
|
|
|
struct wlr_backend *backend) {
|
2018-09-17 21:55:01 +02:00
|
|
|
struct wlr_multi_backend *multi = multi_backend_from_backend(_multi);
|
2017-12-20 00:49:00 +01:00
|
|
|
|
|
|
|
struct subbackend_state *sub =
|
|
|
|
multi_backend_get_subbackend(multi, backend);
|
|
|
|
|
|
|
|
if (sub) {
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&multi->events.backend_remove, backend);
|
2017-12-20 00:49:00 +01:00
|
|
|
subbackend_state_destroy(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 11:51:23 +01:00
|
|
|
bool wlr_multi_is_empty(struct wlr_backend *_backend) {
|
|
|
|
assert(wlr_backend_is_multi(_backend));
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
|
|
|
|
return wl_list_length(&backend->backends) < 1;
|
|
|
|
}
|
2018-09-19 13:53:51 +02:00
|
|
|
|
|
|
|
void wlr_multi_for_each_backend(struct wlr_backend *_backend,
|
|
|
|
void (*callback)(struct wlr_backend *backend, void *data), void *data) {
|
|
|
|
assert(wlr_backend_is_multi(_backend));
|
|
|
|
struct wlr_multi_backend *backend = (struct wlr_multi_backend *)_backend;
|
|
|
|
struct subbackend_state *sub;
|
|
|
|
wl_list_for_each(sub, &backend->backends, link) {
|
|
|
|
callback(sub->backend, data);
|
|
|
|
}
|
|
|
|
}
|