native_drm.c revision a23bf646bdeb72381e7f2bc784d47748cdd7d1ce
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Mesa 3-D graphics library
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Version:  7.11
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copy of this software and associated documentation files (the "Software"),
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * to deal in the Software without restriction, including without limitation
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * and/or sell copies of the Software, and to permit persons to whom the
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Software is furnished to do so, subject to the following conditions:
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * The above copyright notice and this permission notice shall be included
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in all copies or substantial portions of the Software.
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DEALINGS IN THE SOFTWARE.
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "util/u_memory.h"
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "util/u_inlines.h"
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "pipe/p_compiler.h"
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "pipe/p_screen.h"
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "pipe/p_context.h"
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "pipe/p_state.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "state_tracker/drm_driver.h"
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#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   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
70sync_callback(void *data)
71{
72   int *done = data;
73
74   *done = 1;
75}
76
77static void
78force_roundtrip(struct wl_display *display)
79{
80   int done = 0;
81
82   wl_display_sync_callback(display, sync_callback, &done);
83   wl_display_iterate(display, WL_DISPLAY_WRITABLE);
84   while (!done)
85      wl_display_iterate(display, WL_DISPLAY_READABLE);
86}
87
88static void
89wayland_drm_display_destroy(struct native_display *ndpy)
90{
91   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
92
93   if (drmdpy->fd)
94      close(drmdpy->fd);
95   if (drmdpy->wl_drm)
96      wl_drm_destroy(drmdpy->wl_drm);
97   if (drmdpy->device_name)
98      FREE(drmdpy->device_name);
99   if (drmdpy->base.config)
100      FREE(drmdpy->base.config);
101   if (drmdpy->base.own_dpy)
102      wl_display_destroy(drmdpy->base.dpy);
103
104   ndpy_uninit(ndpy);
105
106   FREE(drmdpy);
107}
108
109static struct wl_buffer *
110wayland_create_drm_buffer(struct wayland_display *display,
111                          struct wayland_surface *surface,
112                          enum native_attachment attachment)
113{
114   struct wayland_drm_display *drmdpy = (struct wayland_drm_display *) display;
115   struct pipe_screen *screen = drmdpy->base.base.screen;
116   struct pipe_resource *resource;
117   struct winsys_handle wsh;
118   uint width, height;
119   struct wl_visual *visual;
120
121   resource = resource_surface_get_single_resource(surface->rsurf, attachment);
122   resource_surface_get_size(surface->rsurf, &width, &height);
123
124   wsh.type = DRM_API_HANDLE_TYPE_SHARED;
125   screen->resource_get_handle(screen, resource, &wsh);
126
127   pipe_resource_reference(&resource, NULL);
128
129   switch (surface->type) {
130   case WL_WINDOW_SURFACE:
131      visual = surface->win->visual;
132      break;
133   case WL_PIXMAP_SURFACE:
134      visual = surface->pix->visual;
135      break;
136   default:
137      return NULL;
138   }
139
140   return wl_drm_create_buffer(drmdpy->wl_drm, wsh.handle,
141                               width, height, wsh.stride, visual);
142}
143
144static void
145drm_handle_device(void *data, struct wl_drm *drm, const char *device)
146{
147   struct wayland_drm_display *drmdpy = data;
148   drm_magic_t magic;
149
150   drmdpy->device_name = strdup(device);
151   if (!drmdpy->device_name)
152      return;
153
154   drmdpy->fd = open(drmdpy->device_name, O_RDWR);
155   if (drmdpy->fd == -1) {
156      _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
157              drmdpy->device_name, strerror(errno));
158      return;
159   }
160
161   drmGetMagic(drmdpy->fd, &magic);
162   wl_drm_authenticate(drmdpy->wl_drm, magic);
163}
164
165static void
166drm_handle_authenticated(void *data, struct wl_drm *drm)
167{
168   struct wayland_drm_display *drmdpy = data;
169
170   drmdpy->authenticated = true;
171}
172
173static const struct wl_drm_listener drm_listener = {
174   drm_handle_device,
175   drm_handle_authenticated
176};
177
178static boolean
179wayland_drm_display_init_screen(struct native_display *ndpy)
180{
181   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
182   uint32_t id;
183
184   id = wl_display_get_global(drmdpy->base.dpy, "wl_drm", 1);
185   if (id == 0)
186      force_roundtrip(drmdpy->base.dpy);
187   id = wl_display_get_global(drmdpy->base.dpy, "wl_drm", 1);
188   if (id == 0)
189      return FALSE;
190
191   drmdpy->wl_drm = wl_drm_create(drmdpy->base.dpy, id, 1);
192   if (!drmdpy->wl_drm)
193      return FALSE;
194
195   wl_drm_add_listener(drmdpy->wl_drm, &drm_listener, drmdpy);
196   force_roundtrip(drmdpy->base.dpy);
197   if (drmdpy->fd == -1)
198      return FALSE;
199
200   force_roundtrip(drmdpy->base.dpy);
201   if (!drmdpy->authenticated)
202      return FALSE;
203
204   drmdpy->base.base.screen =
205      drmdpy->event_handler->new_drm_screen(&drmdpy->base.base,
206                                            NULL, drmdpy->fd);
207   if (!drmdpy->base.base.screen) {
208      _eglLog(_EGL_WARNING, "failed to create DRM screen");
209      return FALSE;
210   }
211
212   return TRUE;
213}
214
215static struct pipe_resource *
216wayland_drm_display_import_buffer(struct native_display *ndpy,
217                                  const struct pipe_resource *templ,
218                                  void *buf)
219{
220   return ndpy->screen->resource_from_handle(ndpy->screen,
221                                             templ, (struct winsys_handle *) buf);
222}
223
224static boolean
225wayland_drm_display_export_buffer(struct native_display *ndpy,
226                                  struct pipe_resource *res,
227                                  void *buf)
228{
229   return ndpy->screen->resource_get_handle(ndpy->screen,
230                                            res, (struct winsys_handle *) buf);
231}
232
233static struct native_display_buffer wayland_drm_display_buffer = {
234   wayland_drm_display_import_buffer,
235   wayland_drm_display_export_buffer
236};
237
238static int
239wayland_drm_display_authenticate(void *user_data, uint32_t magic)
240{
241   struct native_display *ndpy = user_data;
242   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
243   boolean current_authenticate, authenticated;
244
245   current_authenticate = drmdpy->authenticated;
246
247   wl_drm_authenticate(drmdpy->wl_drm, magic);
248   force_roundtrip(drmdpy->base.dpy);
249   authenticated = drmdpy->authenticated;
250
251   drmdpy->authenticated = current_authenticate;
252
253   return authenticated ? 0 : -1;
254}
255
256static struct wayland_drm_callbacks wl_drm_callbacks = {
257   wayland_drm_display_authenticate,
258   egl_g3d_wl_drm_helper_reference_buffer,
259   egl_g3d_wl_drm_helper_unreference_buffer
260};
261
262static boolean
263wayland_drm_display_bind_wayland_display(struct native_display *ndpy,
264                                         struct wl_display *wl_dpy)
265{
266   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
267
268   if (drmdpy->wl_server_drm)
269      return FALSE;
270
271   drmdpy->wl_server_drm =
272      wayland_drm_init(wl_dpy, drmdpy->device_name,
273                       &wl_drm_callbacks, ndpy);
274
275   if (!drmdpy->wl_server_drm)
276      return FALSE;
277
278   return TRUE;
279}
280
281static boolean
282wayland_drm_display_unbind_wayland_display(struct native_display *ndpy,
283                                           struct wl_display *wl_dpy)
284{
285   struct wayland_drm_display *drmdpy = wayland_drm_display(ndpy);
286
287   if (!drmdpy->wl_server_drm)
288      return FALSE;
289
290   wayland_drm_uninit(drmdpy->wl_server_drm);
291   drmdpy->wl_server_drm = NULL;
292
293   return TRUE;
294}
295
296static struct native_display_wayland_bufmgr wayland_drm_display_wayland_bufmgr = {
297   wayland_drm_display_bind_wayland_display,
298   wayland_drm_display_unbind_wayland_display,
299   egl_g3d_wl_drm_common_wl_buffer_get_resource
300};
301
302
303struct wayland_display *
304wayland_create_drm_display(struct wl_display *dpy,
305                           struct native_event_handler *event_handler,
306                           void *user_data)
307{
308   struct wayland_drm_display *drmdpy;
309
310   drmdpy = CALLOC_STRUCT(wayland_drm_display);
311   if (!drmdpy)
312      return NULL;
313
314   drmdpy->event_handler = event_handler;
315   drmdpy->base.base.user_data = user_data;
316
317   drmdpy->base.dpy = dpy;
318   if (!drmdpy->base.dpy) {
319      wayland_drm_display_destroy(&drmdpy->base.base);
320      return NULL;
321   }
322
323   if (!wayland_drm_display_init_screen(&drmdpy->base.base)) {
324      wayland_drm_display_destroy(&drmdpy->base.base);
325      return NULL;
326   }
327   drmdpy->base.base.destroy = wayland_drm_display_destroy;
328   drmdpy->base.base.buffer = &wayland_drm_display_buffer;
329   drmdpy->base.base.wayland_bufmgr = &wayland_drm_display_wayland_bufmgr;
330
331   drmdpy->base.create_buffer = wayland_create_drm_buffer;
332
333   return &drmdpy->base;
334}
335
336/* vim: set sw=3 ts=8 sts=3 expandtab: */
337