egl_dri2: Add missing header
[mesa.git] / src / egl / drivers / dri2 / platform_drm.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 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <xf86drm.h>
32 #include <dlfcn.h>
33
34 #include "egl_dri2.h"
35
36 static int
37 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
38 {
39 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
40
41 return drmAuthMagic(dri2_dpy->fd, id);
42 }
43
44 EGLBoolean
45 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
46 {
47 struct dri2_egl_display *dri2_dpy;
48 int i;
49
50 dri2_dpy = malloc(sizeof *dri2_dpy);
51 if (!dri2_dpy)
52 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
53
54 memset(dri2_dpy, 0, sizeof *dri2_dpy);
55
56 disp->DriverData = (void *) dri2_dpy;
57 dri2_dpy->fd = (int) (intptr_t) disp->PlatformDisplay;
58
59 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
60 if (dri2_dpy->driver_name == NULL)
61 return _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
62
63 dri2_dpy->device_name = dri2_get_device_name_for_fd(dri2_dpy->fd);
64 if (dri2_dpy->device_name == NULL) {
65 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get device name");
66 goto cleanup_driver_name;
67 }
68
69 if (!dri2_load_driver(disp))
70 goto cleanup_device_name;
71
72 dri2_dpy->extensions[0] = &image_lookup_extension.base;
73 dri2_dpy->extensions[1] = &use_invalidate.base;
74 dri2_dpy->extensions[2] = NULL;
75
76 if (!dri2_create_screen(disp))
77 goto cleanup_driver;
78
79 for (i = 0; dri2_dpy->driver_configs[i]; i++)
80 dri2_add_config(disp, dri2_dpy->driver_configs[i], i + 1, 0, 0, NULL);
81
82 #ifdef HAVE_WAYLAND_PLATFORM
83 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
84 #endif
85 dri2_dpy->authenticate = dri2_drm_authenticate;
86
87 /* we're supporting EGL 1.4 */
88 disp->VersionMajor = 1;
89 disp->VersionMinor = 4;
90
91 return EGL_TRUE;
92
93 cleanup_driver:
94 dlclose(dri2_dpy->driver);
95 cleanup_device_name:
96 free(dri2_dpy->device_name);
97 cleanup_driver_name:
98 free(dri2_dpy->driver_name);
99
100 return EGL_FALSE;
101 }