egl_dri2: use gbm_surface as the native window type in 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 #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 struct gbm_bo *
41 lock_front_buffer(struct gbm_surface *_surf)
42 {
43 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
44 struct dri2_egl_surface *dri2_surf = surf->dri_private;
45 struct gbm_bo *bo;
46
47 if (dri2_surf->current == NULL) {
48 _eglError(EGL_BAD_SURFACE, "no front buffer");
49 return NULL;
50 }
51
52 bo = dri2_surf->current->bo;
53 dri2_surf->current->locked = 1;
54 dri2_surf->current = NULL;
55
56 return bo;
57 }
58
59 static void
60 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
61 {
62 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
63 struct dri2_egl_surface *dri2_surf = surf->dri_private;
64 int i;
65
66 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
67 if (dri2_surf->color_buffers[i].bo == bo) {
68 dri2_surf->color_buffers[i].locked = 0;
69 }
70 }
71 }
72
73 static int
74 has_free_buffers(struct gbm_surface *_surf)
75 {
76 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
77 struct dri2_egl_surface *dri2_surf = surf->dri_private;
78 int i;
79
80 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
81 if (!dri2_surf->color_buffers[i].locked)
82 return 1;
83
84 return 0;
85 }
86
87 static _EGLSurface *
88 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
89 _EGLConfig *conf, EGLNativeWindowType window,
90 const EGLint *attrib_list)
91 {
92 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
93 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
94 struct dri2_egl_surface *dri2_surf;
95 struct gbm_dri_surface *surf;
96
97 (void) drv;
98
99 dri2_surf = malloc(sizeof *dri2_surf);
100 if (!dri2_surf) {
101 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
102 return NULL;
103 }
104
105 memset(dri2_surf, 0, sizeof *dri2_surf);
106 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
107 goto cleanup_surf;
108
109 switch (type) {
110 case EGL_WINDOW_BIT:
111 surf = gbm_dri_surface((struct gbm_surface *) window);
112 dri2_surf->gbm_surf = surf;
113 dri2_surf->base.Width = surf->base.width;
114 dri2_surf->base.Height = surf->base.height;
115 surf->dri_private = dri2_surf;
116 break;
117 default:
118 goto cleanup_surf;
119 }
120
121 dri2_surf->dri_drawable =
122 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
123 dri2_conf->dri_double_config,
124 dri2_surf->gbm_surf);
125
126 if (dri2_surf->dri_drawable == NULL) {
127 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
128 goto cleanup_surf;
129 }
130
131 return &dri2_surf->base;
132
133 cleanup_surf:
134 free(dri2_surf);
135
136 return NULL;
137 }
138
139 static _EGLSurface *
140 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
141 _EGLConfig *conf, EGLNativeWindowType window,
142 const EGLint *attrib_list)
143 {
144 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
145 window, attrib_list);
146 }
147
148 static EGLBoolean
149 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
150 {
151 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
152 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
153 int i;
154
155 if (!_eglPutSurface(surf))
156 return EGL_TRUE;
157
158 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
159
160 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
161 if (dri2_surf->color_buffers[i].bo)
162 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
163 }
164
165 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
166 if (dri2_surf->dri_buffers[i])
167 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
168 dri2_surf->dri_buffers[i]);
169 }
170
171 free(surf);
172
173 return EGL_TRUE;
174 }
175
176 static int
177 get_back_bo(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
178 {
179 struct dri2_egl_display *dri2_dpy =
180 dri2_egl_display(dri2_surf->base.Resource.Display);
181 struct gbm_dri_bo *bo;
182 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
183 int i, name, pitch;
184
185 if (dri2_surf->back == NULL) {
186 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
187 if (!dri2_surf->color_buffers[i].locked) {
188 dri2_surf->back = &dri2_surf->color_buffers[i];
189 break;
190 }
191 }
192 }
193
194 if (dri2_surf->back == NULL)
195 return -1;
196 if (dri2_surf->back->bo == NULL)
197 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
198 surf->base.width, surf->base.height,
199 surf->base.format, surf->base.flags);
200 if (dri2_surf->back->bo == NULL)
201 return -1;
202
203 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
204
205 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
206 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
207
208 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
209 buffer->name = name;
210 buffer->pitch = pitch;
211 buffer->cpp = 4;
212 buffer->flags = 0;
213
214 return 0;
215 }
216
217 static int
218 get_aux_bo(struct dri2_egl_surface *dri2_surf,
219 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
220 {
221 struct dri2_egl_display *dri2_dpy =
222 dri2_egl_display(dri2_surf->base.Resource.Display);
223 __DRIbuffer *b;
224
225 b = NULL;
226 if (dri2_surf->dri_buffers[attachment])
227 b = dri2_surf->dri_buffers[attachment];
228 if (b == NULL)
229 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
230 attachment, format,
231 dri2_surf->base.Width,
232 dri2_surf->base.Height);
233 if (b == NULL)
234 return -1;
235
236 memcpy(buffer, b, sizeof *buffer);
237
238 return 0;
239 }
240
241 static __DRIbuffer *
242 dri2_get_buffers_with_format(__DRIdrawable *driDrawable,
243 int *width, int *height,
244 unsigned int *attachments, int count,
245 int *out_count, void *loaderPrivate)
246 {
247 struct dri2_egl_surface *dri2_surf = loaderPrivate;
248 int i, j;
249
250 dri2_surf->buffer_count = 0;
251 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
252 assert(attachments[i] < __DRI_BUFFER_COUNT);
253 assert(dri2_surf->buffer_count < 5);
254
255 switch (attachments[i]) {
256 case __DRI_BUFFER_BACK_LEFT:
257 if (get_back_bo(dri2_surf, &dri2_surf->buffers[j]) < 0) {
258 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
259 return NULL;
260 }
261 break;
262 default:
263 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
264 &dri2_surf->buffers[j]) < 0) {
265 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
266 return NULL;
267 }
268 break;
269 }
270 }
271
272 *out_count = j;
273 if (j == 0)
274 return NULL;
275
276 *width = dri2_surf->base.Width;
277 *height = dri2_surf->base.Height;
278
279 return dri2_surf->buffers;
280 }
281
282 static __DRIbuffer *
283 dri2_get_buffers(__DRIdrawable * driDrawable,
284 int *width, int *height,
285 unsigned int *attachments, int count,
286 int *out_count, void *loaderPrivate)
287 {
288 unsigned int *attachments_with_format;
289 __DRIbuffer *buffer;
290 const unsigned int format = 32;
291 int i;
292
293 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
294 if (!attachments_with_format) {
295 *out_count = 0;
296 return NULL;
297 }
298
299 for (i = 0; i < count; ++i) {
300 attachments_with_format[2*i] = attachments[i];
301 attachments_with_format[2*i + 1] = format;
302 }
303
304 buffer =
305 dri2_get_buffers_with_format(driDrawable,
306 width, height,
307 attachments_with_format, count,
308 out_count, loaderPrivate);
309
310 free(attachments_with_format);
311
312 return buffer;
313 }
314
315 static void
316 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
317 {
318 (void) driDrawable;
319 (void) loaderPrivate;
320 }
321
322 static EGLBoolean
323 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
324 {
325 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
326 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
327
328 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
329 if (dri2_surf->current)
330 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
331 dri2_surf->current = dri2_surf->back;
332 dri2_surf->back = NULL;
333 }
334
335 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
336 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
337
338 return EGL_TRUE;
339 }
340
341 static _EGLImage *
342 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
343 EGLClientBuffer buffer, const EGLint *attr_list)
344 {
345 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
346 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
347 struct dri2_egl_image *dri2_img;
348
349 dri2_img = malloc(sizeof *dri2_img);
350 if (!dri2_img) {
351 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
352 return NULL;
353 }
354
355 if (!_eglInitImage(&dri2_img->base, disp)) {
356 free(dri2_img);
357 return NULL;
358 }
359
360 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
361 if (dri2_img->dri_image == NULL) {
362 free(dri2_img);
363 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
364 return NULL;
365 }
366
367 return &dri2_img->base;
368 }
369
370 static _EGLImage *
371 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
372 _EGLContext *ctx, EGLenum target,
373 EGLClientBuffer buffer, const EGLint *attr_list)
374 {
375 (void) drv;
376
377 switch (target) {
378 case EGL_NATIVE_PIXMAP_KHR:
379 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
380 default:
381 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
382 }
383 }
384
385 static int
386 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
387 {
388 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
389
390 return drmAuthMagic(dri2_dpy->fd, id);
391 }
392
393 EGLBoolean
394 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
395 {
396 struct dri2_egl_display *dri2_dpy;
397 struct gbm_device *gbm;
398 int fd = -1;
399 int i;
400
401 dri2_dpy = malloc(sizeof *dri2_dpy);
402 if (!dri2_dpy)
403 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
404
405 memset(dri2_dpy, 0, sizeof *dri2_dpy);
406
407 disp->DriverData = (void *) dri2_dpy;
408
409 gbm = disp->PlatformDisplay;
410 if (gbm == NULL) {
411 fd = open("/dev/dri/card0", O_RDWR);
412 dri2_dpy->own_device = 1;
413 gbm = gbm_create_device(fd);
414 if (gbm == NULL)
415 return EGL_FALSE;
416 }
417
418 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
419 free(dri2_dpy);
420 return EGL_FALSE;
421 }
422
423 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
424 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
425 free(dri2_dpy);
426 return EGL_FALSE;
427 }
428
429 if (fd < 0) {
430 fd = dup(gbm_device_get_fd(gbm));
431 if (fd < 0) {
432 free(dri2_dpy);
433 return EGL_FALSE;
434 }
435 }
436
437 dri2_dpy->fd = fd;
438 dri2_dpy->device_name = dri2_get_device_name_for_fd(dri2_dpy->fd);
439 dri2_dpy->driver_name = dri2_dpy->gbm_dri->base.driver_name;
440
441 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
442 dri2_dpy->core = dri2_dpy->gbm_dri->core;
443 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
444 dri2_dpy->image = dri2_dpy->gbm_dri->image;
445 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
446 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
447
448 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
449 dri2_dpy->gbm_dri->lookup_user_data = disp;
450
451 dri2_dpy->gbm_dri->get_buffers = dri2_get_buffers;
452 dri2_dpy->gbm_dri->flush_front_buffer = dri2_flush_front_buffer;
453 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_get_buffers_with_format;
454
455 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
456 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
457 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
458
459 dri2_setup_screen(disp);
460
461 for (i = 0; dri2_dpy->driver_configs[i]; i++)
462 dri2_add_config(disp, dri2_dpy->driver_configs[i],
463 i + 1, 0, EGL_WINDOW_BIT, NULL, NULL);
464
465 drv->API.CreateWindowSurface = dri2_create_window_surface;
466 drv->API.DestroySurface = dri2_destroy_surface;
467 drv->API.SwapBuffers = dri2_swap_buffers;
468 drv->API.CreateImageKHR = dri2_drm_create_image_khr;
469
470 #ifdef HAVE_WAYLAND_PLATFORM
471 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
472 #endif
473 dri2_dpy->authenticate = dri2_drm_authenticate;
474
475 /* we're supporting EGL 1.4 */
476 disp->VersionMajor = 1;
477 disp->VersionMinor = 4;
478
479 return EGL_TRUE;
480 }