blob: 815d2674cb2d3e1a74865793710c5fd0306ce8c7 [file] [log] [blame]
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -05001/*
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
Emil Velikovbf0e4d22015-02-28 17:12:40 +000028#include <stdint.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050029#include <stdlib.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050030#include <stdio.h>
Benjamin Franzke7f881c42011-05-30 10:49:55 +020031#include <string.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050032#include <xf86drm.h>
Benjamin Franzkeac3c2c82011-06-07 22:19:21 +020033#include <dlfcn.h>
Benjamin Franzke32f4cf32011-06-29 08:49:39 +020034#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <unistd.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050038
39#include "egl_dri2.h"
Chad Versace8b9298a2014-01-28 12:34:19 -080040#include "egl_dri2_fallbacks.h"
Emil Velikov8d4357b2014-01-11 04:52:48 +000041#include "loader.h"
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050042
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020043static struct gbm_bo *
44lock_front_buffer(struct gbm_surface *_surf)
45{
46 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
47 struct dri2_egl_surface *dri2_surf = surf->dri_private;
Giovanni Campagna8430af52014-06-15 13:49:49 +020048 struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020049 struct gbm_bo *bo;
50
51 if (dri2_surf->current == NULL) {
52 _eglError(EGL_BAD_SURFACE, "no front buffer");
53 return NULL;
54 }
55
56 bo = dri2_surf->current->bo;
Giovanni Campagna8430af52014-06-15 13:49:49 +020057
58 if (device->dri2) {
59 dri2_surf->current->locked = 1;
60 dri2_surf->current = NULL;
61 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020062
63 return bo;
64}
65
66static void
67release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
68{
69 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
70 struct dri2_egl_surface *dri2_surf = surf->dri_private;
Rhys Kidd48649772015-08-06 16:34:07 +100071 unsigned i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020072
73 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
74 if (dri2_surf->color_buffers[i].bo == bo) {
75 dri2_surf->color_buffers[i].locked = 0;
76 }
77 }
78}
79
80static int
81has_free_buffers(struct gbm_surface *_surf)
82{
83 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
84 struct dri2_egl_surface *dri2_surf = surf->dri_private;
Rhys Kidd48649772015-08-06 16:34:07 +100085 unsigned i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020086
87 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
88 if (!dri2_surf->color_buffers[i].locked)
89 return 1;
90
91 return 0;
92}
93
94static _EGLSurface *
Chad Versaced019cd82014-01-28 12:47:38 -080095dri2_drm_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
Chad Versace6d1f83e2014-01-07 14:54:51 -080096 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -080097 const EGLint *attrib_list)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020098{
99 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
100 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
101 struct dri2_egl_surface *dri2_surf;
Chad Versace6d1f83e2014-01-07 14:54:51 -0800102 struct gbm_surface *window = native_window;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200103 struct gbm_dri_surface *surf;
Emil Velikov4ea5ed92015-09-13 12:36:54 +0100104 const __DRIconfig *config;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200105
106 (void) drv;
107
Matt Turner6bda0272012-09-04 23:09:22 -0700108 dri2_surf = calloc(1, sizeof *dri2_surf);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200109 if (!dri2_surf) {
110 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
111 return NULL;
112 }
113
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200114 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
115 goto cleanup_surf;
116
117 switch (type) {
118 case EGL_WINDOW_BIT:
Emil Velikov6098ef82015-06-18 20:16:46 +0100119 if (!window) {
120 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
121 goto cleanup_surf;
122 }
123
Chad Versace6d1f83e2014-01-07 14:54:51 -0800124 surf = gbm_dri_surface(window);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200125 dri2_surf->gbm_surf = surf;
126 dri2_surf->base.Width = surf->base.width;
127 dri2_surf->base.Height = surf->base.height;
128 surf->dri_private = dri2_surf;
129 break;
130 default:
131 goto cleanup_surf;
132 }
133
Emil Velikov4ea5ed92015-09-13 12:36:54 +0100134 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
135 dri2_surf->base.GLColorspace);
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200136
Emil Velikov4ea5ed92015-09-13 12:36:54 +0100137 if (dri2_dpy->dri2) {
Giovanni Campagna8430af52014-06-15 13:49:49 +0200138 dri2_surf->dri_drawable =
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200139 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
140 dri2_surf->gbm_surf);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200141
Giovanni Campagna8430af52014-06-15 13:49:49 +0200142 } else {
143 assert(dri2_dpy->swrast != NULL);
Emil Velikov4ea5ed92015-09-13 12:36:54 +0100144
Giovanni Campagna8430af52014-06-15 13:49:49 +0200145 dri2_surf->dri_drawable =
Emil Velikov4ea5ed92015-09-13 12:36:54 +0100146 (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen, config,
147 dri2_surf->gbm_surf);
Giovanni Campagna8430af52014-06-15 13:49:49 +0200148
149 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200150 if (dri2_surf->dri_drawable == NULL) {
Giovanni Campagna8430af52014-06-15 13:49:49 +0200151 _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200152 goto cleanup_surf;
153 }
154
155 return &dri2_surf->base;
156
157 cleanup_surf:
158 free(dri2_surf);
159
160 return NULL;
161}
162
163static _EGLSurface *
Chad Versaced019cd82014-01-28 12:47:38 -0800164dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800165 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -0800166 const EGLint *attrib_list)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200167{
Chad Versaced019cd82014-01-28 12:47:38 -0800168 return dri2_drm_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800169 native_window, attrib_list);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200170}
171
Chad Versace1787f562014-02-09 09:13:08 -0800172static _EGLSurface *
173dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
174 _EGLConfig *conf, void *native_window,
175 const EGLint *attrib_list)
176{
177 /* From the EGL_MESA_platform_gbm spec, version 5:
178 *
179 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
180 * that belongs to the GBM platform. Any such call fails and generates
181 * EGL_BAD_PARAMETER.
182 */
183 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
184 return NULL;
185}
186
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200187static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800188dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200189{
190 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
191 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
Rhys Kidd48649772015-08-06 16:34:07 +1000192 unsigned i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200193
194 if (!_eglPutSurface(surf))
195 return EGL_TRUE;
196
197 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
198
199 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
200 if (dri2_surf->color_buffers[i].bo)
201 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
202 }
203
204 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
205 if (dri2_surf->dri_buffers[i])
206 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
207 dri2_surf->dri_buffers[i]);
208 }
209
210 free(surf);
211
212 return EGL_TRUE;
213}
214
215static int
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800216get_back_bo(struct dri2_egl_surface *dri2_surf)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200217{
218 struct dri2_egl_display *dri2_dpy =
219 dri2_egl_display(dri2_surf->base.Resource.Display);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200220 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
Rhys Kidd48649772015-08-06 16:34:07 +1000221 unsigned i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200222
223 if (dri2_surf->back == NULL) {
224 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
225 if (!dri2_surf->color_buffers[i].locked) {
226 dri2_surf->back = &dri2_surf->color_buffers[i];
227 break;
228 }
229 }
230 }
231
232 if (dri2_surf->back == NULL)
233 return -1;
234 if (dri2_surf->back->bo == NULL)
235 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
236 surf->base.width, surf->base.height,
237 surf->base.format, surf->base.flags);
238 if (dri2_surf->back->bo == NULL)
239 return -1;
240
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800241 return 0;
242}
243
Giovanni Campagna8430af52014-06-15 13:49:49 +0200244static int
245get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
246{
247 struct dri2_egl_display *dri2_dpy =
248 dri2_egl_display(dri2_surf->base.Resource.Display);
249 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
250
251 if (dri2_surf->current == NULL) {
252 assert(!dri2_surf->color_buffers[0].locked);
253 dri2_surf->current = &dri2_surf->color_buffers[0];
254 }
255
256 if (dri2_surf->current->bo == NULL)
257 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
258 surf->base.width, surf->base.height,
259 surf->base.format, surf->base.flags);
260 if (dri2_surf->current->bo == NULL)
261 return -1;
262
263 return 0;
264}
265
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800266static void
267back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
268{
269 struct dri2_egl_display *dri2_dpy =
270 dri2_egl_display(dri2_surf->base.Resource.Display);
271 struct gbm_dri_bo *bo;
272 int name, pitch;
273
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200274 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
275
276 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
277 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
278
279 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
280 buffer->name = name;
281 buffer->pitch = pitch;
282 buffer->cpp = 4;
283 buffer->flags = 0;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200284}
285
286static int
287get_aux_bo(struct dri2_egl_surface *dri2_surf,
288 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
289{
290 struct dri2_egl_display *dri2_dpy =
291 dri2_egl_display(dri2_surf->base.Resource.Display);
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700292 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200293
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700294 if (b == NULL) {
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200295 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
296 attachment, format,
297 dri2_surf->base.Width,
298 dri2_surf->base.Height);
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700299 dri2_surf->dri_buffers[attachment] = b;
300 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200301 if (b == NULL)
302 return -1;
303
304 memcpy(buffer, b, sizeof *buffer);
305
306 return 0;
307}
308
309static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800310dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200311 int *width, int *height,
312 unsigned int *attachments, int count,
313 int *out_count, void *loaderPrivate)
314{
315 struct dri2_egl_surface *dri2_surf = loaderPrivate;
316 int i, j;
317
318 dri2_surf->buffer_count = 0;
319 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
320 assert(attachments[i] < __DRI_BUFFER_COUNT);
321 assert(dri2_surf->buffer_count < 5);
322
323 switch (attachments[i]) {
324 case __DRI_BUFFER_BACK_LEFT:
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800325 if (get_back_bo(dri2_surf) < 0) {
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200326 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
327 return NULL;
328 }
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800329 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200330 break;
331 default:
332 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
333 &dri2_surf->buffers[j]) < 0) {
334 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
335 return NULL;
336 }
337 break;
338 }
339 }
340
341 *out_count = j;
342 if (j == 0)
343 return NULL;
344
345 *width = dri2_surf->base.Width;
346 *height = dri2_surf->base.Height;
347
348 return dri2_surf->buffers;
349}
350
351static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800352dri2_drm_get_buffers(__DRIdrawable * driDrawable,
353 int *width, int *height,
354 unsigned int *attachments, int count,
355 int *out_count, void *loaderPrivate)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200356{
357 unsigned int *attachments_with_format;
358 __DRIbuffer *buffer;
359 const unsigned int format = 32;
360 int i;
361
Carl Worthecc89e42014-09-03 14:33:18 -0700362 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200363 if (!attachments_with_format) {
364 *out_count = 0;
365 return NULL;
366 }
367
368 for (i = 0; i < count; ++i) {
369 attachments_with_format[2*i] = attachments[i];
370 attachments_with_format[2*i + 1] = format;
371 }
372
373 buffer =
Chad Versaced019cd82014-01-28 12:47:38 -0800374 dri2_drm_get_buffers_with_format(driDrawable,
375 width, height,
376 attachments_with_format, count,
377 out_count, loaderPrivate);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200378
379 free(attachments_with_format);
380
381 return buffer;
382}
383
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800384static int
Chad Versaced019cd82014-01-28 12:47:38 -0800385dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
386 unsigned int format,
387 uint32_t *stamp,
388 void *loaderPrivate,
389 uint32_t buffer_mask,
390 struct __DRIimageList *buffers)
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800391{
392 struct dri2_egl_surface *dri2_surf = loaderPrivate;
393 struct gbm_dri_bo *bo;
394
395 if (get_back_bo(dri2_surf) < 0)
396 return 0;
397
398 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
399 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
400 buffers->back = bo->image;
401
402 return 1;
403}
404
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200405static void
Chad Versaced019cd82014-01-28 12:47:38 -0800406dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200407{
408 (void) driDrawable;
409 (void) loaderPrivate;
410}
411
412static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800413dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200414{
415 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
416 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
Rhys Kidd48649772015-08-06 16:34:07 +1000417 unsigned i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200418
Giovanni Campagna8430af52014-06-15 13:49:49 +0200419 if (dri2_dpy->swrast) {
420 (*dri2_dpy->core->swapBuffers)(dri2_surf->dri_drawable);
421 } else {
422 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
423 if (dri2_surf->current)
424 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
425 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
426 if (dri2_surf->color_buffers[i].age > 0)
427 dri2_surf->color_buffers[i].age++;
Lionel Landwerlind175e7c2014-10-14 10:39:47 +0100428
429 /* Make sure we have a back buffer in case we're swapping without
430 * ever rendering. */
431 if (get_back_bo(dri2_surf) < 0) {
432 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
433 return EGL_FALSE;
434 }
435
Giovanni Campagna8430af52014-06-15 13:49:49 +0200436 dri2_surf->current = dri2_surf->back;
437 dri2_surf->current->age = 1;
438 dri2_surf->back = NULL;
439 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200440
Eric Anholt70e8ccc2014-12-21 11:51:33 -0800441 dri2_flush_drawable_for_swapbuffers(disp, draw);
Giovanni Campagna8430af52014-06-15 13:49:49 +0200442 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
443 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200444
445 return EGL_TRUE;
446}
447
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500448static EGLint
Chad Versaced019cd82014-01-28 12:47:38 -0800449dri2_drm_query_buffer_age(_EGLDriver *drv,
450 _EGLDisplay *disp, _EGLSurface *surface)
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500451{
452 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500453
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800454 if (get_back_bo(dri2_surf) < 0) {
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500455 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
456 return 0;
457 }
458
459 return dri2_surf->back->age;
460}
461
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200462static _EGLImage *
Chad Versaced019cd82014-01-28 12:47:38 -0800463dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
464 EGLClientBuffer buffer, const EGLint *attr_list)
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200465{
466 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
467 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
468 struct dri2_egl_image *dri2_img;
469
470 dri2_img = malloc(sizeof *dri2_img);
471 if (!dri2_img) {
472 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
473 return NULL;
474 }
475
476 if (!_eglInitImage(&dri2_img->base, disp)) {
477 free(dri2_img);
478 return NULL;
479 }
480
481 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
482 if (dri2_img->dri_image == NULL) {
483 free(dri2_img);
484 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
485 return NULL;
486 }
487
488 return &dri2_img->base;
489}
490
491static _EGLImage *
492dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
493 _EGLContext *ctx, EGLenum target,
494 EGLClientBuffer buffer, const EGLint *attr_list)
495{
496 (void) drv;
497
498 switch (target) {
499 case EGL_NATIVE_PIXMAP_KHR:
Chad Versaced019cd82014-01-28 12:47:38 -0800500 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200501 default:
502 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
503 }
504}
505
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100506static int
507dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
508{
509 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
510
511 return drmAuthMagic(dri2_dpy->fd, id);
512}
513
Giovanni Campagna8430af52014-06-15 13:49:49 +0200514static void
515swrast_put_image2(__DRIdrawable *driDrawable,
516 int op,
517 int x,
518 int y,
519 int width,
520 int height,
521 int stride,
522 char *data,
523 void *loaderPrivate)
524{
525 struct dri2_egl_surface *dri2_surf = loaderPrivate;
526 int internal_stride, i;
527 struct gbm_dri_bo *bo;
528
529 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
530 op != __DRI_SWRAST_IMAGE_OP_SWAP)
531 return;
532
533 if (get_swrast_front_bo(dri2_surf) < 0)
534 return;
535
536 bo = gbm_dri_bo(dri2_surf->current->bo);
537 if (gbm_dri_bo_map(bo) == NULL)
538 return;
539
540 internal_stride = bo->base.base.stride;
541
542 for (i = 0; i < height; i++) {
543 memcpy(bo->map + (x + i) * internal_stride + y,
544 data + i * stride, stride);
545 }
546
547 gbm_dri_bo_unmap(bo);
548}
549
550static void
551swrast_get_image(__DRIdrawable *driDrawable,
552 int x,
553 int y,
554 int width,
555 int height,
556 char *data,
557 void *loaderPrivate)
558{
559 struct dri2_egl_surface *dri2_surf = loaderPrivate;
560 int internal_stride, stride, i;
561 struct gbm_dri_bo *bo;
562
563 if (get_swrast_front_bo(dri2_surf) < 0)
564 return;
565
566 bo = gbm_dri_bo(dri2_surf->current->bo);
567 if (gbm_dri_bo_map(bo) == NULL)
568 return;
569
570 internal_stride = bo->base.base.stride;
571 stride = width * 4;
572
573 for (i = 0; i < height; i++) {
574 memcpy(data + i * stride,
575 bo->map + (x + i) * internal_stride + y, stride);
576 }
577
578 gbm_dri_bo_unmap(bo);
579}
580
Chad Versace90502b12014-01-28 11:41:46 -0800581static struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
582 .authenticate = dri2_drm_authenticate,
Chad Versace0a0c8812014-01-28 16:39:09 -0800583 .create_window_surface = dri2_drm_create_window_surface,
Chad Versace1787f562014-02-09 09:13:08 -0800584 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
Chad Versacebf200762014-01-28 17:03:03 -0800585 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
Chad Versace958dd802014-01-28 17:03:03 -0800586 .destroy_surface = dri2_drm_destroy_surface,
Chad Versaceeef68a92014-01-28 17:03:03 -0800587 .create_image = dri2_drm_create_image_khr,
Chad Versace8b9298a2014-01-28 12:34:19 -0800588 .swap_interval = dri2_fallback_swap_interval,
Chad Versacead173bc2014-01-28 16:21:21 -0800589 .swap_buffers = dri2_drm_swap_buffers,
Chad Versaced03948a2014-01-28 16:26:44 -0800590 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
Chad Versace75d398e2014-01-28 17:03:03 -0800591 .swap_buffers_region = dri2_fallback_swap_buffers_region,
Chad Versace688a0e82014-01-28 17:03:03 -0800592 .post_sub_buffer = dri2_fallback_post_sub_buffer,
Chad Versacebc2cbc02014-01-28 17:03:03 -0800593 .copy_buffers = dri2_fallback_copy_buffers,
Chad Versace3fdfbd22014-01-28 17:03:03 -0800594 .query_buffer_age = dri2_drm_query_buffer_age,
Chad Versaceeadd5e02014-01-28 17:03:03 -0800595 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
Sarah Sharpc524f3e2014-05-06 12:10:57 -0700596 .get_sync_values = dri2_fallback_get_sync_values,
Chad Versace90502b12014-01-28 11:41:46 -0800597};
598
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500599EGLBoolean
600dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
601{
602 struct dri2_egl_display *dri2_dpy;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200603 struct gbm_device *gbm;
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200604 int fd = -1;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500605 int i;
606
Emil Velikov8d4357b2014-01-11 04:52:48 +0000607 loader_set_logger(_eglLog);
608
Matt Turner6bda0272012-09-04 23:09:22 -0700609 dri2_dpy = calloc(1, sizeof *dri2_dpy);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500610 if (!dri2_dpy)
611 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200612
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500613 disp->DriverData = (void *) dri2_dpy;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500614
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200615 gbm = disp->PlatformDisplay;
616 if (gbm == NULL) {
Jonathan Grayc973e442014-04-03 16:22:26 +1100617 char buf[64];
618 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
619 if (n != -1 && n < sizeof(buf))
Derek Foreman4f8f7902015-06-17 11:28:51 -0500620 fd = loader_open_device(buf);
Jonathan Grayc973e442014-04-03 16:22:26 +1100621 if (fd < 0)
Derek Foreman4f8f7902015-06-17 11:28:51 -0500622 fd = loader_open_device("/dev/dri/card0");
Benjamin Franzke2a584532011-12-13 14:43:48 +0100623 dri2_dpy->own_device = 1;
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200624 gbm = gbm_create_device(fd);
625 if (gbm == NULL)
Matt Turnere4f0d262015-07-15 09:00:41 -0700626 goto cleanup;
Emil Velikovbd5bcb52015-09-07 09:53:53 +0100627 } else {
628 fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
629 if (fd < 0)
630 goto cleanup;
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200631 }
632
Matt Turnere4f0d262015-07-15 09:00:41 -0700633 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0)
634 goto cleanup;
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100635
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200636 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
Matt Turnere4f0d262015-07-15 09:00:41 -0700637 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI)
638 goto cleanup;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500639
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200640 dri2_dpy->fd = fd;
Emil Velikov8d4357b2014-01-11 04:52:48 +0000641 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
Emil Velikov5cb1cad2014-06-02 12:26:17 +0100642 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500643
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200644 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
645 dri2_dpy->core = dri2_dpy->gbm_dri->core;
646 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
647 dri2_dpy->image = dri2_dpy->gbm_dri->image;
Ander Conselvan de Oliveira410aa3e2012-01-25 16:24:15 +0200648 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200649 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200650 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
651
652 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
653 dri2_dpy->gbm_dri->lookup_user_data = disp;
654
Chad Versaced019cd82014-01-28 12:47:38 -0800655 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
656 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
657 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
658 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200659 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
660 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200661
662 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
663 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
664 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
665
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200666 dri2_setup_screen(disp);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500667
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700668 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
669 EGLint format, attr_list[3];
Daniel Stone65c89652015-03-02 13:52:59 +0000670 unsigned int red, alpha;
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700671
672 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
Daniel Stone65c89652015-03-02 13:52:59 +0000673 __DRI_ATTRIB_RED_MASK, &red);
674 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
675 __DRI_ATTRIB_ALPHA_MASK, &alpha);
676 if (red == 0x3ff00000 && alpha == 0x00000000)
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700677 format = GBM_FORMAT_XRGB2101010;
Daniel Stone65c89652015-03-02 13:52:59 +0000678 else if (red == 0x3ff00000 && alpha == 0xc0000000)
679 format = GBM_FORMAT_ARGB2101010;
680 else if (red == 0x00ff0000 && alpha == 0x00000000)
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700681 format = GBM_FORMAT_XRGB8888;
Daniel Stone65c89652015-03-02 13:52:59 +0000682 else if (red == 0x00ff0000 && alpha == 0xff000000)
683 format = GBM_FORMAT_ARGB8888;
684 else if (red == 0xf800)
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700685 format = GBM_FORMAT_RGB565;
686 else
687 continue;
688
689 attr_list[0] = EGL_NATIVE_VISUAL_ID;
690 attr_list[1] = format;
691 attr_list[2] = EGL_NONE;
692
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200693 dri2_add_config(disp, dri2_dpy->driver_configs[i],
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700694 i + 1, EGL_WINDOW_BIT, attr_list, NULL);
695 }
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200696
Andreas Pokorny53b614b2014-09-03 22:43:41 +0200697 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200698 if (dri2_dpy->dri2)
699 disp->Extensions.EXT_buffer_age = EGL_TRUE;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500700
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100701#ifdef HAVE_WAYLAND_PLATFORM
Giovanni Campagnae57ad3d2014-07-23 19:28:52 +0100702 if (dri2_dpy->image) {
703 if (dri2_dpy->image->base.version >= 10 &&
704 dri2_dpy->image->getCapabilities != NULL) {
705 int capabilities;
706
707 capabilities =
708 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
709 disp->Extensions.WL_bind_wayland_display =
710 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
711 } else
712 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
713 }
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100714#endif
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200715
Chad Versace90502b12014-01-28 11:41:46 -0800716 /* Fill vtbl last to prevent accidentally calling virtual function during
717 * initialization.
718 */
719 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
720
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500721 return EGL_TRUE;
Matt Turnere4f0d262015-07-15 09:00:41 -0700722
723cleanup:
724 if (fd >= 0)
725 close(fd);
726
727 free(dri2_dpy);
728 return EGL_FALSE;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500729}