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