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