Merge branch 'master' of git://anongit.freedesktop.org/mesa/mesa
[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 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "egl_dri2.h"
39
40 static _EGLImage *
41 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
42 EGLClientBuffer buffer, const EGLint *attr_list)
43 {
44 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
45 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
46 struct dri2_egl_image *dri2_img;
47
48 dri2_img = malloc(sizeof *dri2_img);
49 if (!dri2_img) {
50 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
51 return NULL;
52 }
53
54 if (!_eglInitImage(&dri2_img->base, disp)) {
55 free(dri2_img);
56 return NULL;
57 }
58
59 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
60 if (dri2_img->dri_image == NULL) {
61 free(dri2_img);
62 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
63 return NULL;
64 }
65
66 return &dri2_img->base;
67 }
68
69 static _EGLImage *
70 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
71 _EGLContext *ctx, EGLenum target,
72 EGLClientBuffer buffer, const EGLint *attr_list)
73 {
74 (void) drv;
75
76 switch (target) {
77 case EGL_NATIVE_PIXMAP_KHR:
78 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
79 default:
80 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
81 }
82 }
83
84 static int
85 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
86 {
87 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
88
89 return drmAuthMagic(dri2_dpy->fd, id);
90 }
91
92 EGLBoolean
93 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
94 {
95 struct dri2_egl_display *dri2_dpy;
96 struct gbm_device *gbm;
97 int fd = -1;
98 int i;
99
100 dri2_dpy = malloc(sizeof *dri2_dpy);
101 if (!dri2_dpy)
102 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
103
104 memset(dri2_dpy, 0, sizeof *dri2_dpy);
105
106 disp->DriverData = (void *) dri2_dpy;
107
108 gbm = disp->PlatformDisplay;
109 if (gbm == NULL) {
110 fd = open("/dev/dri/card0", O_RDWR);
111 dri2_dpy->own_gbm_device = 1;
112 gbm = gbm_create_device(fd);
113 if (gbm == NULL)
114 return EGL_FALSE;
115 }
116
117 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
118 free(dri2_dpy);
119 return EGL_FALSE;
120 }
121
122 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
123 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
124 free(dri2_dpy);
125 return EGL_FALSE;
126 }
127
128 if (fd < 0) {
129 fd = dup(gbm_device_get_fd(gbm));
130 if (fd < 0) {
131 free(dri2_dpy);
132 return EGL_FALSE;
133 }
134 }
135
136 dri2_dpy->fd = fd;
137 dri2_dpy->device_name = dri2_get_device_name_for_fd(dri2_dpy->fd);
138 dri2_dpy->driver_name = dri2_dpy->gbm_dri->base.driver_name;
139
140 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
141 dri2_dpy->core = dri2_dpy->gbm_dri->core;
142 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
143 dri2_dpy->image = dri2_dpy->gbm_dri->image;
144 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
145
146 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
147 dri2_dpy->gbm_dri->lookup_user_data = disp;
148
149 dri2_setup_screen(disp);
150
151 for (i = 0; dri2_dpy->driver_configs[i]; i++)
152 dri2_add_config(disp, dri2_dpy->driver_configs[i],
153 i + 1, 0, 0, NULL);
154
155 drv->API.CreateImageKHR = dri2_drm_create_image_khr;
156
157 #ifdef HAVE_WAYLAND_PLATFORM
158 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
159 #endif
160 dri2_dpy->authenticate = dri2_drm_authenticate;
161
162 /* we're supporting EGL 1.4 */
163 disp->VersionMajor = 1;
164 disp->VersionMinor = 4;
165
166 return EGL_TRUE;
167 }