summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/egl/drm/native_drm.c
blob: f298bda16eb4c9cb24f66ef724b5403310426a4f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Mesa 3-D graphics library
 * Version:  7.8
 *
 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "util/u_memory.h"
#include "egllog.h"

#include "native_drm.h"

#include "gbm_gallium_drmint.h"

#ifdef HAVE_LIBUDEV
#include <libudev.h>
#endif

static boolean
drm_display_is_format_supported(struct native_display *ndpy,
                                enum pipe_format fmt, boolean is_color)
{
   return ndpy->screen->is_format_supported(ndpy->screen,
         fmt, PIPE_TEXTURE_2D, 0,
         (is_color) ? PIPE_BIND_RENDER_TARGET :
         PIPE_BIND_DEPTH_STENCIL);
}

static const struct native_config **
drm_display_get_configs(struct native_display *ndpy, int *num_configs)
{
   struct drm_display *drmdpy = drm_display(ndpy);
   const struct native_config **configs;

   /* first time */
   if (!drmdpy->config) {
      struct native_config *nconf;
      enum pipe_format format;

      drmdpy->config = CALLOC(1, sizeof(*drmdpy->config));
      if (!drmdpy->config)
         return NULL;

      nconf = &drmdpy->config->base;

      nconf->buffer_mask =
         (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
         (1 << NATIVE_ATTACHMENT_BACK_LEFT);

      format = PIPE_FORMAT_B8G8R8A8_UNORM;
      if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE)) {
         format = PIPE_FORMAT_A8R8G8B8_UNORM;
         if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE))
            format = PIPE_FORMAT_NONE;
      }
      if (format == PIPE_FORMAT_NONE) {
         FREE(drmdpy->config);
         drmdpy->config = NULL;
         return NULL;
      }

      nconf->color_format = format;

      /* support KMS */
      if (drmdpy->resources)
         nconf->scanout_bit = TRUE;
   }

   configs = MALLOC(sizeof(*configs));
   if (configs) {
      configs[0] = &drmdpy->config->base;
      if (num_configs)
         *num_configs = 1;
   }

   return configs;
}

static int
drm_display_get_param(struct native_display *ndpy,
                      enum native_param_type param)
{
   int val;

   switch (param) {
   case NATIVE_PARAM_USE_NATIVE_BUFFER:
   case NATIVE_PARAM_PRESERVE_BUFFER:
   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
   default:
      val = 0;
      break;
   }

   return val;
}

static void
drm_display_destroy(struct native_display *ndpy)
{
   struct drm_display *drmdpy = drm_display(ndpy);

   if (drmdpy->config)
      FREE(drmdpy->config);

   drm_display_fini_modeset(&drmdpy->base);

   ndpy_uninit(ndpy);

   if (drmdpy->device_name)
      FREE(drmdpy->device_name);

   if (drmdpy->fd >= 0)
      close(drmdpy->fd);

   FREE(drmdpy);
}

static struct pipe_resource *
drm_display_import_buffer(struct native_display *ndpy,
                          const struct pipe_resource *templ,
                          void *buf)
{
   return ndpy->screen->resource_from_handle(ndpy->screen,
         templ, (struct winsys_handle *) buf);
}

static boolean
drm_display_export_buffer(struct native_display *ndpy,
                          struct pipe_resource *res,
                          void *buf)
{
   return ndpy->screen->resource_get_handle(ndpy->screen,
         res, (struct winsys_handle *) buf);
}

static struct native_display_buffer drm_display_buffer = {
   drm_display_import_buffer,
   drm_display_export_buffer
};

static int
drm_display_authenticate(void *user_data, uint32_t magic)
{
   struct native_display *ndpy = user_data;
   struct drm_display *drmdpy = drm_display(ndpy);

   return drmAuthMagic(drmdpy->fd, magic);
}

static char *
drm_get_device_name(int fd)
{
   char *device_name = NULL;
#ifdef HAVE_LIBUDEV
   struct udev *udev;
   struct udev_device *device;
   struct stat buf;
   const char *tmp;

   udev = udev_new();
   if (fstat(fd, &buf) < 0) {
      _eglLog(_EGL_WARNING, "failed to stat fd %d", fd);
      goto out;
   }

   device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
   if (device == NULL) {
      _eglLog(_EGL_WARNING,
              "could not create udev device for fd %d", fd);
      goto out;
   }

   tmp = udev_device_get_devnode(device);
   if (!tmp)
      goto out;
   device_name = strdup(tmp);

out:
   udev_device_unref(device);
   udev_unref(udev);

#endif
   return device_name;
}

#ifdef HAVE_WAYLAND_BACKEND

static struct wayland_drm_callbacks wl_drm_callbacks = {
   drm_display_authenticate,
   egl_g3d_wl_drm_helper_reference_buffer,
   egl_g3d_wl_drm_helper_unreference_buffer
};

static boolean
drm_display_bind_wayland_display(struct native_display *ndpy,
                                  struct wl_display *wl_dpy)
{
   struct drm_display *drmdpy = drm_display(ndpy);

   if (drmdpy->wl_server_drm)
      return FALSE;

   drmdpy->wl_server_drm = wayland_drm_init(wl_dpy,
         drmdpy->device_name,
         &wl_drm_callbacks, ndpy);

   if (!drmdpy->wl_server_drm)
      return FALSE;
   
   return TRUE;
}

static boolean
drm_display_unbind_wayland_display(struct native_display *ndpy,
                                    struct wl_display *wl_dpy)
{
   struct drm_display *drmdpy = drm_display(ndpy);

   if (!drmdpy->wl_server_drm)
      return FALSE;

   wayland_drm_uninit(drmdpy->wl_server_drm);
   drmdpy->wl_server_drm = NULL;

   return TRUE;
}

static struct native_display_wayland_bufmgr drm_display_wayland_bufmgr = {
   drm_display_bind_wayland_display,
   drm_display_unbind_wayland_display,
   egl_g3d_wl_drm_common_wl_buffer_get_resource
};

#endif /* HAVE_WAYLAND_BACKEND */

static struct native_surface *
drm_create_pixmap_surface(struct native_display *ndpy,
                              EGLNativePixmapType pix,
                              const struct native_config *nconf)
{
   struct gbm_gallium_drm_bo *bo = (void *) pix;

   return drm_display_create_surface_from_resource(ndpy, bo->resource);
}

static struct native_display *
drm_create_display(struct gbm_gallium_drm_device *gbmdrm,
                   struct native_event_handler *event_handler, void *user_data)
{
   struct drm_display *drmdpy;

   drmdpy = CALLOC_STRUCT(drm_display);
   if (!drmdpy)
      return NULL;

   drmdpy->fd = gbmdrm->base.base.fd;
   drmdpy->device_name = drm_get_device_name(drmdpy->fd);

   gbmdrm->lookup_egl_image = (struct pipe_resource *(*)(void *, void *))
      event_handler->lookup_egl_image;
   gbmdrm->lookup_egl_image_data = &drmdpy->base;

   drmdpy->event_handler = event_handler;
   drmdpy->base.user_data = user_data;

   drmdpy->base.screen = gbmdrm->screen;

   drmdpy->base.destroy = drm_display_destroy;
   drmdpy->base.get_param = drm_display_get_param;
   drmdpy->base.get_configs = drm_display_get_configs;

   drmdpy->base.create_pixmap_surface = drm_create_pixmap_surface;

   drmdpy->base.buffer = &drm_display_buffer;
#ifdef HAVE_WAYLAND_BACKEND
   if (drmdpy->device_name)
      drmdpy->base.wayland_bufmgr = &drm_display_wayland_bufmgr;
#endif
   drm_display_init_modeset(&drmdpy->base);

   return &drmdpy->base;
}

static struct native_event_handler *drm_event_handler;

static void
native_set_event_handler(struct native_event_handler *event_handler)
{
   drm_event_handler = event_handler;
}

static struct native_display *
native_create_display(void *dpy, boolean use_sw, void *user_data)
{
   struct gbm_gallium_drm_device *gbm;
   int fd;

   gbm = dpy;

   if (gbm == NULL) {
      fd = open("/dev/dri/card0", O_RDWR);
      gbm = gbm_gallium_drm_device(gbm_get_device(fd));
   }

   if (gbm == NULL)
      return NULL;
   
   if (strcmp(gbm_device_get_backend_name(&gbm->base.base), "drm") != 0 ||
       gbm->base.type != GBM_DRM_DRIVER_TYPE_GALLIUM)
      return NULL;

   return drm_create_display(gbm, drm_event_handler, user_data);
}

static const struct native_platform drm_platform = {
   "DRM", /* name */
   native_set_event_handler,
   native_create_display
};

const struct native_platform *
native_get_drm_platform(void)
{
   return &drm_platform;
}