main
wayland-drm.c
1/* wld: wayland-drm.c
2 *
3 * Copyright (c) 2013, 2014 Michael Forney
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include "drm-private.h"
25#include "drm.h"
26#include <wayland-client-protocol.h>
27#include "wayland-drm-client-protocol.h"
28#include "wayland-private.h"
29#include "wayland.h"
30#include "wld-private.h"
31
32#include <fcntl.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <xf86drm.h>
37
38struct drm_context {
39 struct wayland_context base;
40 struct wld_context *driver_context;
41 struct wl_drm *wl;
42 struct wl_registry *registry;
43 struct wl_array formats;
44 uint32_t capabilities;
45 int fd;
46};
47
48#define WAYLAND_IMPL_NAME drm
49#include "interface/context.h"
50#include "interface/wayland.h"
51IMPL(drm_context, wld_context)
52
53static void registry_global(void *data, struct wl_registry *registry,
54 uint32_t name, const char *interface,
55 uint32_t version);
56static void registry_global_remove(void *data, struct wl_registry *registry,
57 uint32_t name);
58
59static void drm_device(void *data, struct wl_drm *wl, const char *name);
60static void drm_format(void *data, struct wl_drm *wl, uint32_t format);
61static void drm_authenticated(void *data, struct wl_drm *wl);
62static void drm_capabilities(void *data, struct wl_drm *wl,
63 uint32_t capabilities);
64
65const static struct wl_registry_listener registry_listener = {
66 .global = ®istry_global,
67 .global_remove = ®istry_global_remove
68};
69
70const static struct wl_drm_listener drm_listener = {
71 .device = &drm_device,
72 .format = &drm_format,
73 .authenticated = &drm_authenticated,
74 .capabilities = &drm_capabilities
75};
76
77struct wayland_context *
78wayland_create_context(struct wl_display *display,
79 struct wl_event_queue *queue)
80{
81 struct drm_context *context;
82
83 if (!(context = malloc(sizeof *context)))
84 goto error0;
85
86 context_initialize(&context->base.base, &wld_context_impl);
87 context->wl = NULL;
88 context->fd = -1;
89 context->capabilities = 0;
90 wl_array_init(&context->formats);
91
92 if (!(context->registry = wl_display_get_registry(display)))
93 goto error1;
94
95 wl_registry_add_listener(context->registry, ®istry_listener, context);
96 wl_proxy_set_queue((struct wl_proxy *)context->registry, queue);
97
98 /* Wait for wl_drm global. */
99 wl_display_roundtrip_queue(display, queue);
100
101 if (!context->wl) {
102 DEBUG("No wl_drm global\n");
103 goto error2;
104 }
105
106 wl_drm_add_listener(context->wl, &drm_listener, context);
107
108 /* Wait for DRM capabilities and device. */
109 wl_display_roundtrip_queue(display, queue);
110
111 if (!(context->capabilities & WL_DRM_CAPABILITY_PRIME)) {
112 DEBUG("No PRIME support\n");
113 goto error3;
114 }
115
116 if (context->fd == -1) {
117 DEBUG("No DRM device\n");
118 goto error3;
119 }
120
121 if (!(context->driver_context = wld_drm_create_context(context->fd))) {
122 DEBUG("Couldn't initialize context for DRM device\n");
123 goto error4;
124 }
125
126 return &context->base;
127
128error4:
129 close(context->fd);
130error3:
131 wl_drm_destroy(context->wl);
132error2:
133 wl_registry_destroy(context->registry);
134error1:
135 wl_array_release(&context->formats);
136 free(context);
137error0:
138 return NULL;
139}
140
141bool
142wayland_has_format(struct wld_context *base, uint32_t format)
143{
144 struct drm_context *context = drm_context(base);
145 uint32_t *supported_format;
146
147 wl_array_for_each (supported_format, &context->formats) {
148 if (*supported_format == format)
149 return true;
150 }
151
152 return false;
153}
154
155EXPORT
156int
157wld_wayland_drm_get_fd(struct wld_context *base)
158{
159 struct drm_context *context = drm_context(base);
160
161 return context->fd;
162}
163
164struct wld_renderer *
165context_create_renderer(struct wld_context *base)
166{
167 struct drm_context *context = drm_context(base);
168
169 return wld_create_renderer(context->driver_context);
170}
171
172struct buffer *
173context_create_buffer(struct wld_context *base,
174 uint32_t width, uint32_t height,
175 uint32_t format, uint32_t flags)
176{
177 struct drm_context *context = drm_context(base);
178 struct buffer *buffer;
179 union wld_object object;
180 struct wl_buffer *wl;
181
182 if (!wayland_has_format(base, format))
183 goto error0;
184
185 buffer = context->driver_context->impl->create_buffer(context->driver_context, width, height, format, flags);
186
187 if (!buffer)
188 goto error0;
189
190 if (!wld_export(&buffer->base, WLD_DRM_OBJECT_PRIME_FD, &object))
191 goto error1;
192
193 wl = wl_drm_create_prime_buffer(context->wl, object.i, width, height,
194 format, 0, buffer->base.pitch, 0, 0, 0, 0);
195 close(object.i);
196
197 if (!wl)
198 goto error1;
199
200 if (!wayland_buffer_add_exporter(buffer, wl))
201 goto error2;
202
203 return buffer;
204
205error2:
206 wl_buffer_destroy(wl);
207error1:
208 wld_buffer_unreference(&buffer->base);
209error0:
210 return NULL;
211}
212
213struct buffer *
214context_import_buffer(struct wld_context *context,
215 uint32_t type, union wld_object object,
216 uint32_t width, uint32_t height,
217 uint32_t format, uint32_t pitch)
218{
219 return NULL;
220}
221
222void
223context_destroy(struct wld_context *base)
224{
225 struct drm_context *context = drm_context(base);
226
227 wld_destroy_context(context->driver_context);
228 close(context->fd);
229 wl_drm_destroy(context->wl);
230 wl_registry_destroy(context->registry);
231 wl_array_release(&context->formats);
232 wl_event_queue_destroy(context->base.queue);
233 free(context);
234}
235
236void
237registry_global(void *data, struct wl_registry *registry, uint32_t name,
238 const char *interface, uint32_t version)
239{
240 struct drm_context *context = data;
241
242 if (strcmp(interface, "wl_drm") == 0 && version >= 2)
243 context->wl = wl_registry_bind(registry, name, &wl_drm_interface, 2);
244}
245
246void
247registry_global_remove(void *data, struct wl_registry *registry,
248 uint32_t name)
249{
250}
251
252void
253drm_device(void *data, struct wl_drm *wl, const char *name)
254{
255 struct drm_context *context = data;
256
257 context->fd = open(name, O_RDWR);
258 if (context->fd == -1) {
259 DEBUG("Couldn't open DRM device '%s'\n", name);
260 return;
261 }
262}
263
264void
265drm_format(void *data, struct wl_drm *wl, uint32_t format)
266{
267 struct drm_context *context = data;
268
269 *((uint32_t *)wl_array_add(&context->formats, sizeof format)) = format;
270}
271
272void
273drm_authenticated(void *data, struct wl_drm *wl)
274{
275}
276
277void
278drm_capabilities(void *data, struct wl_drm *wl, uint32_t capabilities)
279{
280 struct drm_context *context = data;
281
282 context->capabilities = capabilities;
283}