aboutsummaryrefslogtreecommitdiff
path: root/backend/drm/backend.c
blob: 7db6e9a103548b1d1ebfa67dffa084e9a789c26e (plain)
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wlr/common/list.h>

#include "backend/drm/backend.h"
#include "backend/drm/drm.h"
#include "backend/drm/session.h"
#include "backend/drm/udev.h"
#include "common/log.h"

struct wlr_drm_backend *wlr_drm_backend_init(struct wl_listener *add,
		struct wl_listener *rem,
		struct wl_listener *render)
{
	struct wlr_drm_backend *backend = calloc(1, sizeof *backend);
	if (!backend) {
		wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
		return NULL;
	}

	backend->displays = list_create();
	if (!backend->displays) {
		wlr_log(L_ERROR, "Failed to allocate list");
		goto error_backend;
	}

	backend->event_loop = wl_event_loop_create();
	if (!backend->event_loop) {
		wlr_log(L_ERROR, "Failed to create event loop");
		goto error_list;
	}

	if (!wlr_session_start(&backend->session)) {
		wlr_log(L_ERROR, "Failed to start session");
		goto error_loop;
	}

	if (!wlr_udev_init(backend)) {
		wlr_log(L_ERROR, "Failed to start udev");
		goto error_session;
	}

	backend->fd = wlr_udev_find_gpu(&backend->udev, &backend->session);
	if (backend->fd == -1) {
		wlr_log(L_ERROR, "Failed to open DRM device");
		goto error_udev;
	}

	if (!wlr_drm_renderer_init(&backend->renderer, backend, backend->fd)) {
		wlr_log(L_ERROR, "Failed to initialize renderer");
		goto error_fd;
	}

	wl_signal_init(&backend->signals.display_add);
	wl_signal_init(&backend->signals.display_rem);
	wl_signal_init(&backend->signals.display_render);

	if (add)
		wl_signal_add(&backend->signals.display_add, add);
	if (rem)
		wl_signal_add(&backend->signals.display_rem, rem);
	if (render)
		wl_signal_add(&backend->signals.display_render, render);

	wlr_drm_scan_connectors(backend);

	return backend;

error_fd:
	wlr_session_release_device(&backend->session, backend->fd);
error_udev:
	wlr_udev_free(&backend->udev);
error_session:
	wlr_session_end(&backend->session);
error_loop:
	wl_event_loop_destroy(backend->event_loop);
error_list:
	list_free(backend->displays);
error_backend:
	free(backend);
	return NULL;
}

static void free_display(void *item)
{
	struct wlr_drm_display *disp = item;
	wlr_drm_display_free(disp, true);
	free(disp);
}

void wlr_drm_backend_free(struct wlr_drm_backend *backend)
{
	if (!backend)
		return;

	list_foreach(backend->displays, free_display);

	wlr_drm_renderer_free(&backend->renderer);
	wlr_udev_free(&backend->udev);
	wlr_session_release_device(&backend->session, backend->fd);
	wlr_session_end(&backend->session);

	wl_event_source_remove(backend->event_src.drm);
	wl_event_source_remove(backend->event_src.udev);
	wl_event_loop_destroy(backend->event_loop);

	list_free(backend->displays);
	free(backend);
}

struct wl_event_loop *wlr_drm_backend_get_event_loop(struct wlr_drm_backend *backend)
{
	return backend->event_loop;
}