native_drm.c revision 737bd7367e5a99cf64ec4bfc4420e3380b60878f
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.8
4 *
5 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
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 <string.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30
31#include "util/u_memory.h"
32#include "egllog.h"
33
34#include "native_drm.h"
35
36#include "gbm_gallium_drmint.h"
37
38#ifdef HAVE_LIBUDEV
39#include <libudev.h>
40#endif
41
42static boolean
43drm_display_is_format_supported(struct native_display *ndpy,
44                                enum pipe_format fmt, boolean is_color)
45{
46   return ndpy->screen->is_format_supported(ndpy->screen,
47         fmt, PIPE_TEXTURE_2D, 0,
48         (is_color) ? PIPE_BIND_RENDER_TARGET :
49         PIPE_BIND_DEPTH_STENCIL);
50}
51
52static const struct native_config **
53drm_display_get_configs(struct native_display *ndpy, int *num_configs)
54{
55   struct drm_display *drmdpy = drm_display(ndpy);
56   const struct native_config **configs;
57
58   /* first time */
59   if (!drmdpy->config) {
60      struct native_config *nconf;
61      enum pipe_format format;
62
63      drmdpy->config = CALLOC(1, sizeof(*drmdpy->config));
64      if (!drmdpy->config)
65         return NULL;
66
67      nconf = &drmdpy->config->base;
68
69      nconf->buffer_mask =
70         (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
71         (1 << NATIVE_ATTACHMENT_BACK_LEFT);
72
73      format = PIPE_FORMAT_B8G8R8A8_UNORM;
74      if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE)) {
75         format = PIPE_FORMAT_A8R8G8B8_UNORM;
76         if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE))
77            format = PIPE_FORMAT_NONE;
78      }
79      if (format == PIPE_FORMAT_NONE) {
80         FREE(drmdpy->config);
81         drmdpy->config = NULL;
82         return NULL;
83      }
84
85      nconf->color_format = format;
86
87      /* support KMS */
88      if (drmdpy->resources)
89         nconf->scanout_bit = TRUE;
90   }
91
92   configs = MALLOC(sizeof(*configs));
93   if (configs) {
94      configs[0] = &drmdpy->config->base;
95      if (num_configs)
96         *num_configs = 1;
97   }
98
99   return configs;
100}
101
102static int
103drm_display_get_param(struct native_display *ndpy,
104                      enum native_param_type param)
105{
106   int val;
107
108   switch (param) {
109   case NATIVE_PARAM_USE_NATIVE_BUFFER:
110   case NATIVE_PARAM_PRESERVE_BUFFER:
111   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
112   default:
113      val = 0;
114      break;
115   }
116
117   return val;
118}
119
120static void
121drm_display_destroy(struct native_display *ndpy)
122{
123   struct drm_display *drmdpy = drm_display(ndpy);
124
125   if (drmdpy->config)
126      FREE(drmdpy->config);
127
128   drm_display_fini_modeset(&drmdpy->base);
129
130   ndpy_uninit(ndpy);
131
132   if (drmdpy->device_name)
133      FREE(drmdpy->device_name);
134
135   if (drmdpy->fd >= 0)
136      close(drmdpy->fd);
137
138   FREE(drmdpy);
139}
140
141static struct pipe_resource *
142drm_display_import_buffer(struct native_display *ndpy,
143                          const struct pipe_resource *templ,
144                          void *buf)
145{
146   return ndpy->screen->resource_from_handle(ndpy->screen,
147         templ, (struct winsys_handle *) buf);
148}
149
150static boolean
151drm_display_export_buffer(struct native_display *ndpy,
152                          struct pipe_resource *res,
153                          void *buf)
154{
155   return ndpy->screen->resource_get_handle(ndpy->screen,
156         res, (struct winsys_handle *) buf);
157}
158
159static struct native_display_buffer drm_display_buffer = {
160   drm_display_import_buffer,
161   drm_display_export_buffer
162};
163
164static int
165drm_display_authenticate(void *user_data, uint32_t magic)
166{
167   struct native_display *ndpy = user_data;
168   struct drm_display *drmdpy = drm_display(ndpy);
169
170   return drmAuthMagic(drmdpy->fd, magic);
171}
172
173static char *
174drm_get_device_name(int fd)
175{
176   char *device_name = NULL;
177#ifdef HAVE_LIBUDEV
178   struct udev *udev;
179   struct udev_device *device;
180   struct stat buf;
181   const char *tmp;
182
183   udev = udev_new();
184   if (fstat(fd, &buf) < 0) {
185      _eglLog(_EGL_WARNING, "failed to stat fd %d", fd);
186      goto out;
187   }
188
189   device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
190   if (device == NULL) {
191      _eglLog(_EGL_WARNING,
192              "could not create udev device for fd %d", fd);
193      goto out;
194   }
195
196   tmp = udev_device_get_devnode(device);
197   if (!tmp)
198      goto out;
199   device_name = strdup(tmp);
200
201out:
202   udev_device_unref(device);
203   udev_unref(udev);
204
205#endif
206   return device_name;
207}
208
209#ifdef HAVE_WAYLAND_BACKEND
210
211static struct wayland_drm_callbacks wl_drm_callbacks = {
212   drm_display_authenticate,
213   egl_g3d_wl_drm_helper_reference_buffer,
214   egl_g3d_wl_drm_helper_unreference_buffer
215};
216
217static boolean
218drm_display_bind_wayland_display(struct native_display *ndpy,
219                                  struct wl_display *wl_dpy)
220{
221   struct drm_display *drmdpy = drm_display(ndpy);
222
223   if (drmdpy->wl_server_drm)
224      return FALSE;
225
226   drmdpy->wl_server_drm = wayland_drm_init(wl_dpy,
227         drmdpy->device_name,
228         &wl_drm_callbacks, ndpy);
229
230   if (!drmdpy->wl_server_drm)
231      return FALSE;
232
233   return TRUE;
234}
235
236static boolean
237drm_display_unbind_wayland_display(struct native_display *ndpy,
238                                    struct wl_display *wl_dpy)
239{
240   struct drm_display *drmdpy = drm_display(ndpy);
241
242   if (!drmdpy->wl_server_drm)
243      return FALSE;
244
245   wayland_drm_uninit(drmdpy->wl_server_drm);
246   drmdpy->wl_server_drm = NULL;
247
248   return TRUE;
249}
250
251static struct native_display_wayland_bufmgr drm_display_wayland_bufmgr = {
252   drm_display_bind_wayland_display,
253   drm_display_unbind_wayland_display,
254   egl_g3d_wl_drm_common_wl_buffer_get_resource
255};
256
257#endif /* HAVE_WAYLAND_BACKEND */
258
259static struct native_surface *
260drm_create_pixmap_surface(struct native_display *ndpy,
261                              EGLNativePixmapType pix,
262                              const struct native_config *nconf)
263{
264   struct gbm_gallium_drm_bo *bo = (void *) pix;
265
266   return drm_display_create_surface_from_resource(ndpy, bo->resource);
267}
268
269static struct native_display *
270drm_create_display(struct gbm_gallium_drm_device *gbmdrm,
271                   struct native_event_handler *event_handler, void *user_data)
272{
273   struct drm_display *drmdpy;
274
275   drmdpy = CALLOC_STRUCT(drm_display);
276   if (!drmdpy)
277      return NULL;
278
279   drmdpy->fd = gbmdrm->base.base.fd;
280   drmdpy->device_name = drm_get_device_name(drmdpy->fd);
281
282   gbmdrm->lookup_egl_image = (struct pipe_resource *(*)(void *, void *))
283      event_handler->lookup_egl_image;
284   gbmdrm->lookup_egl_image_data = &drmdpy->base;
285
286   drmdpy->event_handler = event_handler;
287   drmdpy->base.user_data = user_data;
288
289   drmdpy->base.screen = gbmdrm->screen;
290
291   drmdpy->base.destroy = drm_display_destroy;
292   drmdpy->base.get_param = drm_display_get_param;
293   drmdpy->base.get_configs = drm_display_get_configs;
294
295   drmdpy->base.create_pixmap_surface = drm_create_pixmap_surface;
296
297   drmdpy->base.buffer = &drm_display_buffer;
298#ifdef HAVE_WAYLAND_BACKEND
299   if (drmdpy->device_name)
300      drmdpy->base.wayland_bufmgr = &drm_display_wayland_bufmgr;
301#endif
302   drm_display_init_modeset(&drmdpy->base);
303
304   return &drmdpy->base;
305}
306
307static struct native_event_handler *drm_event_handler;
308
309static void
310native_set_event_handler(struct native_event_handler *event_handler)
311{
312   drm_event_handler = event_handler;
313}
314
315static struct native_display *
316native_create_display(void *dpy, boolean use_sw, void *user_data)
317{
318   struct gbm_gallium_drm_device *gbm;
319   int fd;
320
321   gbm = dpy;
322
323   if (gbm == NULL) {
324      fd = open("/dev/dri/card0", O_RDWR);
325      gbm = gbm_gallium_drm_device(gbm_create_device(fd));
326   }
327
328   if (gbm == NULL)
329      return NULL;
330
331   if (strcmp(gbm_device_get_backend_name(&gbm->base.base), "drm") != 0 ||
332       gbm->base.type != GBM_DRM_DRIVER_TYPE_GALLIUM)
333      return NULL;
334
335   return drm_create_display(gbm, drm_event_handler, user_data);
336}
337
338static const struct native_platform drm_platform = {
339   "DRM", /* name */
340   native_set_event_handler,
341   native_create_display
342};
343
344const struct native_platform *
345native_get_drm_platform(void)
346{
347   return &drm_platform;
348}
349