native_drm.c revision 1191d203632e2954ce59163f87c9896b1c6ed40a
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.11
4 *
5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26#include "util/u_memory.h"
27#include "util/u_inlines.h"
28
29#include "pipe/p_compiler.h"
30#include "pipe/p_screen.h"
31#include "pipe/p_context.h"
32#include "pipe/p_state.h"
33#include "state_tracker/drm_driver.h"
34
35#include "egllog.h"
36#include <errno.h>
37
38#include "native_wayland.h"
39
40#include <wayland-client.h>
41#include "wayland-drm-client-protocol.h"
42#include "wayland-egl-priv.h"
43
44#include "common/native_wayland_drm_bufmgr_helper.h"
45
46#include <xf86drm.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50
51struct wayland_drm_display {
52   struct wayland_display base;
53
54   const struct native_event_handler *event_handler;
55
56   struct wl_drm *wl_drm;
57   struct wl_drm *wl_server_drm; /* for EGL_WL_bind_wayland_display */
58   int fd;
59   char *device_name;
60   boolean authenticated;
61};
62
63static INLINE struct wayland_drm_display *
64wayland_drm_display(const struct native_display *ndpy)
65{
66   return (struct wayland_drm_display *) ndpy;
67}
68
69static void
70wayland_drm_display_destroy(struct native_display *ndpy)
71{
72   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
73
74   if (drmdpy->fd)
75      close(drmdpy->fd);
76   if (drmdpy->wl_drm)
77      wl_drm_destroy(drmdpy->wl_drm);
78   if (drmdpy->device_name)
79      FREE(drmdpy->device_name);
80   if (drmdpy->base.configs)
81      FREE(drmdpy->base.configs);
82   if (drmdpy->base.own_dpy)
83      wl_display_destroy(drmdpy->base.dpy);
84
85   ndpy_uninit(ndpy);
86
87   FREE(drmdpy);
88}
89
90static struct wl_buffer *
91wayland_create_drm_buffer(struct wayland_display *display,
92                          struct wayland_surface *surface,
93                          enum native_attachment attachment)
94{
95   struct wayland_drm_display *drmdpy = (struct wayland_drm_display *) display;
96   struct pipe_screen *screen = drmdpy->base.base.screen;
97   struct pipe_resource *resource;
98   struct winsys_handle wsh;
99   uint width, height;
100   enum wl_drm_format format;
101
102   resource = resource_surface_get_single_resource(surface->rsurf, attachment);
103   resource_surface_get_size(surface->rsurf, &width, &height);
104
105   wsh.type = DRM_API_HANDLE_TYPE_SHARED;
106   screen->resource_get_handle(screen, resource, &wsh);
107
108   pipe_resource_reference(&resource, NULL);
109
110   switch (surface->color_format) {
111   case PIPE_FORMAT_B8G8R8A8_UNORM:
112      /* assume premultiplied */
113      format = WL_DRM_FORMAT_PREMULTIPLIED_ARGB32;
114      break;
115   case PIPE_FORMAT_B8G8R8X8_UNORM:
116      format = WL_DRM_FORMAT_XRGB32;
117      break;
118   default:
119      return NULL;
120      break;
121   }
122
123   return wl_drm_create_buffer(drmdpy->wl_drm, wsh.handle,
124                               width, height, wsh.stride, format);
125}
126
127static void
128drm_handle_device(void *data, struct wl_drm *drm, const char *device)
129{
130   struct wayland_drm_display *drmdpy = data;
131   drm_magic_t magic;
132
133   drmdpy->device_name = strdup(device);
134   if (!drmdpy->device_name)
135      return;
136
137   drmdpy->fd = open(drmdpy->device_name, O_RDWR);
138   if (drmdpy->fd == -1) {
139      _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
140              drmdpy->device_name, strerror(errno));
141      return;
142   }
143
144   drmGetMagic(drmdpy->fd, &magic);
145   wl_drm_authenticate(drmdpy->wl_drm, magic);
146}
147
148static void
149drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
150{
151   struct wayland_drm_display *drmdpy = data;
152
153   switch (format) {
154   case WL_DRM_FORMAT_ARGB32:
155      drmdpy->base.formats |= HAS_ARGB32;
156      break;
157   case WL_DRM_FORMAT_PREMULTIPLIED_ARGB32:
158      drmdpy->base.formats |= HAS_PREMUL_ARGB32;
159      break;
160   case WL_DRM_FORMAT_XRGB32:
161      drmdpy->base.formats |= HAS_XRGB32;
162      break;
163   }
164}
165
166static void
167drm_handle_authenticated(void *data, struct wl_drm *drm)
168{
169   struct wayland_drm_display *drmdpy = data;
170
171   drmdpy->authenticated = true;
172}
173
174static const struct wl_drm_listener drm_listener = {
175   drm_handle_device,
176   drm_handle_format,
177   drm_handle_authenticated
178};
179
180static boolean
181wayland_drm_display_init_screen(struct native_display *ndpy)
182{
183   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
184   uint32_t id;
185
186   id = wl_display_get_global(drmdpy->base.dpy, "wl_drm", 1);
187   if (id == 0)
188      wl_display_roundtrip(drmdpy->base.dpy);
189   id = wl_display_get_global(drmdpy->base.dpy, "wl_drm", 1);
190   if (id == 0)
191      return FALSE;
192
193   drmdpy->wl_drm = wl_display_bind(drmdpy->base.dpy, id, &wl_drm_interface);
194   if (!drmdpy->wl_drm)
195      return FALSE;
196
197   wl_drm_add_listener(drmdpy->wl_drm, &drm_listener, drmdpy);
198   wl_display_roundtrip(drmdpy->base.dpy);
199   if (drmdpy->fd == -1)
200      return FALSE;
201
202   wl_display_roundtrip(drmdpy->base.dpy);
203   if (!drmdpy->authenticated)
204      return FALSE;
205
206   if (drmdpy->base.formats == 0)
207      wl_display_roundtrip(drmdpy->base.dpy);
208   if (drmdpy->base.formats == 0)
209      return FALSE;
210
211   drmdpy->base.base.screen =
212      drmdpy->event_handler->new_drm_screen(&drmdpy->base.base,
213                                            NULL, drmdpy->fd);
214   if (!drmdpy->base.base.screen) {
215      _eglLog(_EGL_WARNING, "failed to create DRM screen");
216      return FALSE;
217   }
218
219   return TRUE;
220}
221
222static struct native_display_buffer wayland_drm_display_buffer = {
223   /* use the helpers */
224   drm_display_import_native_buffer,
225   drm_display_export_native_buffer
226};
227
228static int
229wayland_drm_display_authenticate(void *user_data, uint32_t magic)
230{
231   struct native_display *ndpy = user_data;
232   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
233   boolean current_authenticate, authenticated;
234
235   current_authenticate = drmdpy->authenticated;
236
237   wl_drm_authenticate(drmdpy->wl_drm, magic);
238   wl_display_roundtrip(drmdpy->base.dpy);
239   authenticated = drmdpy->authenticated;
240
241   drmdpy->authenticated = current_authenticate;
242
243   return authenticated ? 0 : -1;
244}
245
246static struct wayland_drm_callbacks wl_drm_callbacks = {
247   wayland_drm_display_authenticate,
248   egl_g3d_wl_drm_helper_reference_buffer,
249   egl_g3d_wl_drm_helper_unreference_buffer
250};
251
252static boolean
253wayland_drm_display_bind_wayland_display(struct native_display *ndpy,
254                                         struct wl_display *wl_dpy)
255{
256   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
257
258   if (drmdpy->wl_server_drm)
259      return FALSE;
260
261   drmdpy->wl_server_drm =
262      wayland_drm_init(wl_dpy, drmdpy->device_name,
263                       &wl_drm_callbacks, ndpy);
264
265   if (!drmdpy->wl_server_drm)
266      return FALSE;
267
268   return TRUE;
269}
270
271static boolean
272wayland_drm_display_unbind_wayland_display(struct native_display *ndpy,
273                                           struct wl_display *wl_dpy)
274{
275   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
276
277   if (!drmdpy->wl_server_drm)
278      return FALSE;
279
280   wayland_drm_uninit(drmdpy->wl_server_drm);
281   drmdpy->wl_server_drm = NULL;
282
283   return TRUE;
284}
285
286static struct native_display_wayland_bufmgr wayland_drm_display_wayland_bufmgr = {
287   wayland_drm_display_bind_wayland_display,
288   wayland_drm_display_unbind_wayland_display,
289   egl_g3d_wl_drm_common_wl_buffer_get_resource
290};
291
292
293struct wayland_display *
294wayland_create_drm_display(struct wl_display *dpy,
295                           const struct native_event_handler *event_handler)
296{
297   struct wayland_drm_display *drmdpy;
298
299   drmdpy = CALLOC_STRUCT(wayland_drm_display);
300   if (!drmdpy)
301      return NULL;
302
303   drmdpy->event_handler = event_handler;
304
305   drmdpy->base.dpy = dpy;
306   if (!drmdpy->base.dpy) {
307      wayland_drm_display_destroy(&drmdpy->base.base);
308      return NULL;
309   }
310
311   drmdpy->base.base.init_screen = wayland_drm_display_init_screen;
312   drmdpy->base.base.destroy = wayland_drm_display_destroy;
313   drmdpy->base.base.buffer = &wayland_drm_display_buffer;
314   drmdpy->base.base.wayland_bufmgr = &wayland_drm_display_wayland_bufmgr;
315
316   drmdpy->base.create_buffer = wayland_create_drm_buffer;
317
318   return &drmdpy->base;
319}
320
321/* vim: set sw=3 ts=8 sts=3 expandtab: */
322