cd3122199eb6babfa635c1e35389d03d18699782
[mesa.git] / src / gbm / backends / dri / gbm_dri.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <limits.h>
34
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <unistd.h>
38 #include <dlfcn.h>
39 #include <xf86drm.h>
40
41 #include <GL/gl.h> /* dri_interface needs GL types */
42 #include <GL/internal/dri_interface.h>
43
44 #include "gbm_driint.h"
45
46 #include "gbmint.h"
47
48 /* For importing wl_buffer */
49 #if HAVE_WAYLAND_PLATFORM
50 #include "../../../egl/wayland/wayland-drm/wayland-drm.h"
51 #endif
52
53 static __DRIimage *
54 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
55 {
56 struct gbm_dri_device *dri = data;
57
58 if (dri->lookup_image == NULL)
59 return NULL;
60
61 return dri->lookup_image(screen, image, dri->lookup_user_data);
62 }
63
64 static __DRIbuffer *
65 dri_get_buffers(__DRIdrawable * driDrawable,
66 int *width, int *height,
67 unsigned int *attachments, int count,
68 int *out_count, void *data)
69 {
70 struct gbm_dri_surface *surf = data;
71 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
72
73 if (dri->get_buffers == NULL)
74 return NULL;
75
76 return dri->get_buffers(driDrawable, width, height, attachments,
77 count, out_count, surf->dri_private);
78 }
79
80 static void
81 dri_flush_front_buffer(__DRIdrawable * driDrawable, void *data)
82 {
83 struct gbm_dri_surface *surf = data;
84 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
85
86 if (dri->flush_front_buffer != NULL)
87 dri->flush_front_buffer(driDrawable, surf->dri_private);
88 }
89
90 static __DRIbuffer *
91 dri_get_buffers_with_format(__DRIdrawable * driDrawable,
92 int *width, int *height,
93 unsigned int *attachments, int count,
94 int *out_count, void *data)
95 {
96 struct gbm_dri_surface *surf = data;
97 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
98
99 if (dri->get_buffers_with_format == NULL)
100 return NULL;
101
102 return
103 dri->get_buffers_with_format(driDrawable, width, height, attachments,
104 count, out_count, surf->dri_private);
105 }
106
107 static int
108 image_get_buffers(__DRIdrawable *driDrawable,
109 unsigned int format,
110 uint32_t *stamp,
111 void *loaderPrivate,
112 uint32_t buffer_mask,
113 struct __DRIimageList *buffers)
114 {
115 struct gbm_dri_surface *surf = loaderPrivate;
116 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
117
118 if (dri->image_get_buffers == NULL)
119 return 0;
120
121 return dri->image_get_buffers(driDrawable, format, stamp,
122 surf->dri_private, buffer_mask, buffers);
123 }
124
125 static const __DRIuseInvalidateExtension use_invalidate = {
126 { __DRI_USE_INVALIDATE, 1 }
127 };
128
129 static const __DRIimageLookupExtension image_lookup_extension = {
130 { __DRI_IMAGE_LOOKUP, 1 },
131 dri_lookup_egl_image
132 };
133
134 static const __DRIdri2LoaderExtension dri2_loader_extension = {
135 { __DRI_DRI2_LOADER, 3 },
136 dri_get_buffers,
137 dri_flush_front_buffer,
138 dri_get_buffers_with_format,
139 };
140
141 static const __DRIimageLoaderExtension image_loader_extension = {
142 { __DRI_IMAGE_LOADER, 1 },
143 image_get_buffers,
144 dri_flush_front_buffer,
145 };
146
147
148 struct dri_extension_match {
149 const char *name;
150 int version;
151 int offset;
152 };
153
154 static struct dri_extension_match dri_core_extensions[] = {
155 { __DRI2_FLUSH, 1, offsetof(struct gbm_dri_device, flush) },
156 { __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image) },
157 { NULL, 0, 0 }
158 };
159
160 static struct dri_extension_match gbm_dri_device_extensions[] = {
161 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core) },
162 { __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2) },
163 { NULL, 0, 0 }
164 };
165
166 static int
167 dri_bind_extensions(struct gbm_dri_device *dri,
168 struct dri_extension_match *matches,
169 const __DRIextension **extensions)
170 {
171 int i, j, ret = 0;
172 void *field;
173
174 for (i = 0; extensions[i]; i++) {
175 for (j = 0; matches[j].name; j++) {
176 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
177 extensions[i]->version >= matches[j].version) {
178 field = ((char *) dri + matches[j].offset);
179 *(const __DRIextension **) field = extensions[i];
180 }
181 }
182 }
183
184 for (j = 0; matches[j].name; j++) {
185 field = ((char *) dri + matches[j].offset);
186 if (*(const __DRIextension **) field == NULL) {
187 ret = -1;
188 }
189 }
190
191 return ret;
192 }
193
194 static int
195 dri_load_driver(struct gbm_dri_device *dri)
196 {
197 const __DRIextension **extensions = NULL;
198 char path[PATH_MAX], *search_paths, *p, *next, *end;
199 char *get_extensions_name;
200
201 search_paths = NULL;
202 if (geteuid() == getuid()) {
203 /* don't allow setuid apps to use GBM_DRIVERS_PATH */
204 search_paths = getenv("GBM_DRIVERS_PATH");
205 }
206 if (search_paths == NULL)
207 search_paths = DEFAULT_DRIVER_DIR;
208
209 dri->driver = NULL;
210 end = search_paths + strlen(search_paths);
211 for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
212 int len;
213 next = strchr(p, ':');
214 if (next == NULL)
215 next = end;
216
217 len = next - p;
218 #if GLX_USE_TLS
219 snprintf(path, sizeof path,
220 "%.*s/tls/%s_dri.so", len, p, dri->base.driver_name);
221 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
222 #endif
223 if (dri->driver == NULL) {
224 snprintf(path, sizeof path,
225 "%.*s/%s_dri.so", len, p, dri->base.driver_name);
226 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
227 }
228 /* not need continue to loop all paths once the driver is found */
229 if (dri->driver != NULL)
230 break;
231 }
232
233 if (dri->driver == NULL) {
234 fprintf(stderr, "gbm: failed to open any driver (search paths %s)",
235 search_paths);
236 return -1;
237 }
238
239 if (asprintf(&get_extensions_name, "%s_%s",
240 __DRI_DRIVER_GET_EXTENSIONS, dri->base.driver_name) != -1) {
241 const __DRIextension **(*get_extensions)(void);
242
243 get_extensions = dlsym(dri->driver, get_extensions_name);
244 free(get_extensions_name);
245
246 if (get_extensions)
247 extensions = get_extensions();
248 }
249
250 if (!extensions)
251 extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
252 if (extensions == NULL) {
253 fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
254 dlclose(dri->driver);
255 return -1;
256 }
257 dri->driver_extensions = extensions;
258
259 if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
260 dlclose(dri->driver);
261 fprintf(stderr, "failed to bind extensions\n");
262 return -1;
263 }
264
265 return 0;
266 }
267
268 static int
269 dri_screen_create(struct gbm_dri_device *dri)
270 {
271 const __DRIextension **extensions;
272 int ret = 0;
273
274 dri->base.driver_name = dri_fd_get_driver_name(dri->base.base.fd);
275 if (dri->base.driver_name == NULL)
276 return -1;
277
278 ret = dri_load_driver(dri);
279 if (ret) {
280 fprintf(stderr, "failed to load driver: %s\n", dri->base.driver_name);
281 return ret;
282 };
283
284 dri->extensions[0] = &image_lookup_extension.base;
285 dri->extensions[1] = &use_invalidate.base;
286 dri->extensions[2] = &dri2_loader_extension.base;
287 dri->extensions[3] = &image_loader_extension.base;
288 dri->extensions[4] = NULL;
289
290 if (dri->dri2 == NULL)
291 return -1;
292
293 if (dri->dri2->base.version >= 4) {
294 dri->screen = dri->dri2->createNewScreen2(0, dri->base.base.fd,
295 dri->extensions,
296 dri->driver_extensions,
297 &dri->driver_configs, dri);
298 } else {
299 dri->screen = dri->dri2->createNewScreen(0, dri->base.base.fd,
300 dri->extensions,
301 &dri->driver_configs, dri);
302 }
303 if (dri->screen == NULL)
304 return -1;
305
306 extensions = dri->core->getExtensions(dri->screen);
307 if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
308 ret = -1;
309 goto free_screen;
310 }
311
312 dri->lookup_image = NULL;
313 dri->lookup_user_data = NULL;
314
315 return 0;
316
317 free_screen:
318 dri->core->destroyScreen(dri->screen);
319
320 return ret;
321 }
322
323 static int
324 gbm_dri_is_format_supported(struct gbm_device *gbm,
325 uint32_t format,
326 uint32_t usage)
327 {
328 switch (format) {
329 case GBM_BO_FORMAT_XRGB8888:
330 case GBM_FORMAT_XRGB8888:
331 break;
332 case GBM_BO_FORMAT_ARGB8888:
333 case GBM_FORMAT_ARGB8888:
334 if (usage & GBM_BO_USE_SCANOUT)
335 return 0;
336 break;
337 default:
338 return 0;
339 }
340
341 if (usage & GBM_BO_USE_CURSOR_64X64 &&
342 usage & GBM_BO_USE_RENDERING)
343 return 0;
344
345 return 1;
346 }
347
348 static int
349 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
350 {
351 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
352
353 if (bo->image != NULL)
354 return -1;
355
356 memcpy(bo->map, buf, count);
357
358 return 0;
359 }
360
361 static void
362 gbm_dri_bo_destroy(struct gbm_bo *_bo)
363 {
364 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
365 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
366 struct drm_mode_destroy_dumb arg;
367
368 if (bo->image != NULL) {
369 dri->image->destroyImage(bo->image);
370 } else {
371 munmap(bo->map, bo->size);
372 memset(&arg, 0, sizeof(arg));
373 arg.handle = bo->handle;
374 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
375 }
376
377 free(bo);
378 }
379
380 static uint32_t
381 gbm_dri_to_gbm_format(uint32_t dri_format)
382 {
383 uint32_t ret = 0;
384
385 switch (dri_format) {
386 case __DRI_IMAGE_FORMAT_RGB565:
387 ret = GBM_FORMAT_RGB565;
388 break;
389 case __DRI_IMAGE_FORMAT_XRGB8888:
390 ret = GBM_FORMAT_XRGB8888;
391 break;
392 case __DRI_IMAGE_FORMAT_ARGB8888:
393 ret = GBM_FORMAT_ARGB8888;
394 break;
395 case __DRI_IMAGE_FORMAT_ABGR8888:
396 ret = GBM_FORMAT_ABGR8888;
397 break;
398 default:
399 ret = 0;
400 break;
401 }
402
403 return ret;
404 }
405
406 static struct gbm_bo *
407 gbm_dri_bo_import(struct gbm_device *gbm,
408 uint32_t type, void *buffer, uint32_t usage)
409 {
410 struct gbm_dri_device *dri = gbm_dri_device(gbm);
411 struct gbm_dri_bo *bo;
412 __DRIimage *image;
413 unsigned dri_use = 0;
414 int gbm_format;
415
416 /* Required for query image WIDTH & HEIGHT */
417 if (dri->image->base.version < 4)
418 return NULL;
419
420 switch (type) {
421 #if HAVE_WAYLAND_PLATFORM
422 case GBM_BO_IMPORT_WL_BUFFER:
423 {
424 struct wl_drm_buffer *wb;
425
426 if (!dri->wl_drm)
427 return NULL;
428
429 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
430 if (!wb)
431 return NULL;
432
433 image = wb->driver_buffer;
434
435 switch (wb->format) {
436 case WL_DRM_FORMAT_XRGB8888:
437 gbm_format = GBM_FORMAT_XRGB8888;
438 break;
439 case WL_DRM_FORMAT_ARGB8888:
440 gbm_format = GBM_FORMAT_ARGB8888;
441 break;
442 case WL_DRM_FORMAT_RGB565:
443 gbm_format = GBM_FORMAT_RGB565;
444 break;
445 case WL_DRM_FORMAT_YUYV:
446 gbm_format = GBM_FORMAT_YUYV;
447 break;
448 default:
449 return NULL;
450 }
451 break;
452 }
453 #endif
454
455 case GBM_BO_IMPORT_EGL_IMAGE:
456 {
457 int dri_format;
458 if (dri->lookup_image == NULL)
459 return NULL;
460
461 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
462 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
463 gbm_format = gbm_dri_to_gbm_format(dri_format);
464 if (gbm_format == 0)
465 return NULL;
466 break;
467 }
468
469 default:
470 return NULL;
471 }
472
473
474 bo = calloc(1, sizeof *bo);
475 if (bo == NULL)
476 return NULL;
477
478 bo->image = dri->image->dupImage(image, NULL);
479
480 if (usage & GBM_BO_USE_SCANOUT)
481 dri_use |= __DRI_IMAGE_USE_SCANOUT;
482 if (usage & GBM_BO_USE_CURSOR_64X64)
483 dri_use |= __DRI_IMAGE_USE_CURSOR;
484 if (dri->image->base.version >= 2 &&
485 !dri->image->validateUsage(bo->image, dri_use)) {
486 free(bo);
487 return NULL;
488 }
489
490 bo->base.base.gbm = gbm;
491 bo->base.base.format = gbm_format;
492
493 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
494 (int*)&bo->base.base.width);
495 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
496 (int*)&bo->base.base.height);
497 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
498 (int*)&bo->base.base.stride);
499 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
500 &bo->base.base.handle.s32);
501
502 return &bo->base.base;
503 }
504
505 static struct gbm_bo *
506 create_dumb(struct gbm_device *gbm,
507 uint32_t width, uint32_t height,
508 uint32_t format, uint32_t usage)
509 {
510 struct gbm_dri_device *dri = gbm_dri_device(gbm);
511 struct drm_mode_create_dumb create_arg;
512 struct drm_mode_map_dumb map_arg;
513 struct gbm_dri_bo *bo;
514 struct drm_mode_destroy_dumb destroy_arg;
515 int ret;
516
517 if (!(usage & GBM_BO_USE_CURSOR_64X64))
518 return NULL;
519 if (format != GBM_FORMAT_ARGB8888)
520 return NULL;
521
522 bo = calloc(1, sizeof *bo);
523 if (bo == NULL)
524 return NULL;
525
526 create_arg.bpp = 32;
527 create_arg.width = width;
528 create_arg.height = height;
529
530 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
531 if (ret)
532 goto free_bo;
533
534 bo->base.base.gbm = gbm;
535 bo->base.base.width = width;
536 bo->base.base.height = height;
537 bo->base.base.stride = create_arg.pitch;
538 bo->base.base.format = format;
539 bo->base.base.handle.u32 = create_arg.handle;
540 bo->handle = create_arg.handle;
541 bo->size = create_arg.size;
542
543 memset(&map_arg, 0, sizeof(map_arg));
544 map_arg.handle = bo->handle;
545
546 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
547 if (ret)
548 goto destroy_dumb;
549
550 bo->map = mmap(0, bo->size, PROT_WRITE,
551 MAP_SHARED, dri->base.base.fd, map_arg.offset);
552 if (bo->map == MAP_FAILED)
553 goto destroy_dumb;
554
555 return &bo->base.base;
556
557 destroy_dumb:
558 memset(&destroy_arg, 0, sizeof destroy_arg);
559 destroy_arg.handle = create_arg.handle;
560 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
561 free_bo:
562 free(bo);
563
564 return NULL;
565 }
566
567 static struct gbm_bo *
568 gbm_dri_bo_create(struct gbm_device *gbm,
569 uint32_t width, uint32_t height,
570 uint32_t format, uint32_t usage)
571 {
572 struct gbm_dri_device *dri = gbm_dri_device(gbm);
573 struct gbm_dri_bo *bo;
574 int dri_format;
575 unsigned dri_use = 0;
576
577 if (usage & GBM_BO_USE_WRITE)
578 return create_dumb(gbm, width, height, format, usage);
579
580 bo = calloc(1, sizeof *bo);
581 if (bo == NULL)
582 return NULL;
583
584 bo->base.base.gbm = gbm;
585 bo->base.base.width = width;
586 bo->base.base.height = height;
587 bo->base.base.format = format;
588
589 switch (format) {
590 case GBM_FORMAT_RGB565:
591 dri_format =__DRI_IMAGE_FORMAT_RGB565;
592 break;
593 case GBM_FORMAT_XRGB8888:
594 case GBM_BO_FORMAT_XRGB8888:
595 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
596 break;
597 case GBM_FORMAT_ARGB8888:
598 case GBM_BO_FORMAT_ARGB8888:
599 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
600 break;
601 case GBM_FORMAT_ABGR8888:
602 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
603 break;
604 case GBM_FORMAT_ARGB2101010:
605 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
606 break;
607 case GBM_FORMAT_XRGB2101010:
608 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
609 break;
610 default:
611 return NULL;
612 }
613
614 if (usage & GBM_BO_USE_SCANOUT)
615 dri_use |= __DRI_IMAGE_USE_SCANOUT;
616 if (usage & GBM_BO_USE_CURSOR_64X64)
617 dri_use |= __DRI_IMAGE_USE_CURSOR;
618
619 /* Gallium drivers requires shared in order to get the handle/stride */
620 dri_use |= __DRI_IMAGE_USE_SHARE;
621
622 bo->image =
623 dri->image->createImage(dri->screen,
624 width, height,
625 dri_format, dri_use,
626 bo);
627 if (bo->image == NULL)
628 return NULL;
629
630 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
631 &bo->base.base.handle.s32);
632 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
633 (int *) &bo->base.base.stride);
634
635 return &bo->base.base;
636 }
637
638 static struct gbm_surface *
639 gbm_dri_surface_create(struct gbm_device *gbm,
640 uint32_t width, uint32_t height,
641 uint32_t format, uint32_t flags)
642 {
643 struct gbm_dri_surface *surf;
644
645 surf = calloc(1, sizeof *surf);
646 if (surf == NULL)
647 return NULL;
648
649 surf->base.gbm = gbm;
650 surf->base.width = width;
651 surf->base.height = height;
652 surf->base.format = format;
653 surf->base.flags = flags;
654
655 return &surf->base;
656 }
657
658 static void
659 gbm_dri_surface_destroy(struct gbm_surface *_surf)
660 {
661 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
662
663 free(surf);
664 }
665
666 static void
667 dri_destroy(struct gbm_device *gbm)
668 {
669 struct gbm_dri_device *dri = gbm_dri_device(gbm);
670
671 dri->core->destroyScreen(dri->screen);
672 free(dri->driver_configs);
673 dlclose(dri->driver);
674 free(dri->base.driver_name);
675
676 free(dri);
677 }
678
679 static struct gbm_device *
680 dri_device_create(int fd)
681 {
682 struct gbm_dri_device *dri;
683 int ret;
684
685 dri = calloc(1, sizeof *dri);
686
687 dri->base.base.fd = fd;
688 dri->base.base.bo_create = gbm_dri_bo_create;
689 dri->base.base.bo_import = gbm_dri_bo_import;
690 dri->base.base.is_format_supported = gbm_dri_is_format_supported;
691 dri->base.base.bo_write = gbm_dri_bo_write;
692 dri->base.base.bo_destroy = gbm_dri_bo_destroy;
693 dri->base.base.destroy = dri_destroy;
694 dri->base.base.surface_create = gbm_dri_surface_create;
695 dri->base.base.surface_destroy = gbm_dri_surface_destroy;
696
697 dri->base.type = GBM_DRM_DRIVER_TYPE_DRI;
698 dri->base.base.name = "drm";
699
700 ret = dri_screen_create(dri);
701 if (ret)
702 goto err_dri;
703
704 return &dri->base.base;
705
706 err_dri:
707 free(dri);
708
709 return NULL;
710 }
711
712 struct gbm_backend gbm_dri_backend = {
713 .backend_name = "dri",
714 .create_device = dri_device_create,
715 };