blob: 02e87f7771b2e9caff95945d237ac92690b8b536 [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
28#include <stdlib.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050029#include <stdio.h>
Benjamin Franzke7f881c42011-05-30 10:49:55 +020030#include <string.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050031#include <xf86drm.h>
Benjamin Franzkeac3c2c82011-06-07 22:19:21 +020032#include <dlfcn.h>
Benjamin Franzke32f4cf32011-06-29 08:49:39 +020033#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <unistd.h>
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050037
38#include "egl_dri2.h"
Chad Versace8b9298a2014-01-28 12:34:19 -080039#include "egl_dri2_fallbacks.h"
Emil Velikov8d4357b2014-01-11 04:52:48 +000040#include "loader.h"
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -050041
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020042static struct gbm_bo *
43lock_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;
Giovanni Campagna8430af52014-06-15 13:49:49 +020047 struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020048 struct gbm_bo *bo;
49
50 if (dri2_surf->current == NULL) {
51 _eglError(EGL_BAD_SURFACE, "no front buffer");
52 return NULL;
53 }
54
55 bo = dri2_surf->current->bo;
Giovanni Campagna8430af52014-06-15 13:49:49 +020056
57 if (device->dri2) {
58 dri2_surf->current->locked = 1;
59 dri2_surf->current = NULL;
60 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020061
62 return bo;
63}
64
65static void
66release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
67{
68 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
69 struct dri2_egl_surface *dri2_surf = surf->dri_private;
70 int i;
71
72 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
73 if (dri2_surf->color_buffers[i].bo == bo) {
74 dri2_surf->color_buffers[i].locked = 0;
75 }
76 }
77}
78
79static int
80has_free_buffers(struct gbm_surface *_surf)
81{
82 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
83 struct dri2_egl_surface *dri2_surf = surf->dri_private;
84 int i;
85
86 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
87 if (!dri2_surf->color_buffers[i].locked)
88 return 1;
89
90 return 0;
91}
92
93static _EGLSurface *
Chad Versaced019cd82014-01-28 12:47:38 -080094dri2_drm_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
Chad Versace6d1f83e2014-01-07 14:54:51 -080095 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -080096 const EGLint *attrib_list)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +020097{
98 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
99 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
100 struct dri2_egl_surface *dri2_surf;
Chad Versace6d1f83e2014-01-07 14:54:51 -0800101 struct gbm_surface *window = native_window;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200102 struct gbm_dri_surface *surf;
103
104 (void) drv;
105
Matt Turner6bda0272012-09-04 23:09:22 -0700106 dri2_surf = calloc(1, sizeof *dri2_surf);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200107 if (!dri2_surf) {
108 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
109 return NULL;
110 }
111
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200112 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
113 goto cleanup_surf;
114
115 switch (type) {
116 case EGL_WINDOW_BIT:
Elvis Leecf775c92012-07-11 11:13:51 +0900117 if (!window)
118 return NULL;
Chad Versace6d1f83e2014-01-07 14:54:51 -0800119 surf = gbm_dri_surface(window);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200120 dri2_surf->gbm_surf = surf;
121 dri2_surf->base.Width = surf->base.width;
122 dri2_surf->base.Height = surf->base.height;
123 surf->dri_private = dri2_surf;
124 break;
125 default:
126 goto cleanup_surf;
127 }
128
Giovanni Campagna8430af52014-06-15 13:49:49 +0200129 if (dri2_dpy->dri2) {
130 dri2_surf->dri_drawable =
131 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
132 dri2_conf->dri_double_config,
133 dri2_surf->gbm_surf);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200134
Giovanni Campagna8430af52014-06-15 13:49:49 +0200135 } else {
136 assert(dri2_dpy->swrast != NULL);
137 dri2_surf->dri_drawable =
138 (*dri2_dpy->swrast->createNewDrawable) (dri2_dpy->dri_screen,
139 dri2_conf->dri_double_config,
140 dri2_surf->gbm_surf);
141
142 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200143 if (dri2_surf->dri_drawable == NULL) {
Giovanni Campagna8430af52014-06-15 13:49:49 +0200144 _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200145 goto cleanup_surf;
146 }
147
148 return &dri2_surf->base;
149
150 cleanup_surf:
151 free(dri2_surf);
152
153 return NULL;
154}
155
156static _EGLSurface *
Chad Versaced019cd82014-01-28 12:47:38 -0800157dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800158 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -0800159 const EGLint *attrib_list)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200160{
Chad Versaced019cd82014-01-28 12:47:38 -0800161 return dri2_drm_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800162 native_window, attrib_list);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200163}
164
Chad Versace1787f562014-02-09 09:13:08 -0800165static _EGLSurface *
166dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
167 _EGLConfig *conf, void *native_window,
168 const EGLint *attrib_list)
169{
170 /* From the EGL_MESA_platform_gbm spec, version 5:
171 *
172 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
173 * that belongs to the GBM platform. Any such call fails and generates
174 * EGL_BAD_PARAMETER.
175 */
176 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
177 return NULL;
178}
179
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200180static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800181dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200182{
183 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
184 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
185 int i;
186
187 if (!_eglPutSurface(surf))
188 return EGL_TRUE;
189
190 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
191
192 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
193 if (dri2_surf->color_buffers[i].bo)
194 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
195 }
196
197 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
198 if (dri2_surf->dri_buffers[i])
199 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
200 dri2_surf->dri_buffers[i]);
201 }
202
203 free(surf);
204
205 return EGL_TRUE;
206}
207
208static int
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800209get_back_bo(struct dri2_egl_surface *dri2_surf)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200210{
211 struct dri2_egl_display *dri2_dpy =
212 dri2_egl_display(dri2_surf->base.Resource.Display);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200213 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800214 int i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200215
216 if (dri2_surf->back == NULL) {
217 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
218 if (!dri2_surf->color_buffers[i].locked) {
219 dri2_surf->back = &dri2_surf->color_buffers[i];
220 break;
221 }
222 }
223 }
224
225 if (dri2_surf->back == NULL)
226 return -1;
227 if (dri2_surf->back->bo == NULL)
228 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
229 surf->base.width, surf->base.height,
230 surf->base.format, surf->base.flags);
231 if (dri2_surf->back->bo == NULL)
232 return -1;
233
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800234 return 0;
235}
236
Giovanni Campagna8430af52014-06-15 13:49:49 +0200237static int
238get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
239{
240 struct dri2_egl_display *dri2_dpy =
241 dri2_egl_display(dri2_surf->base.Resource.Display);
242 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
243
244 if (dri2_surf->current == NULL) {
245 assert(!dri2_surf->color_buffers[0].locked);
246 dri2_surf->current = &dri2_surf->color_buffers[0];
247 }
248
249 if (dri2_surf->current->bo == NULL)
250 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
251 surf->base.width, surf->base.height,
252 surf->base.format, surf->base.flags);
253 if (dri2_surf->current->bo == NULL)
254 return -1;
255
256 return 0;
257}
258
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800259static void
260back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
261{
262 struct dri2_egl_display *dri2_dpy =
263 dri2_egl_display(dri2_surf->base.Resource.Display);
264 struct gbm_dri_bo *bo;
265 int name, pitch;
266
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200267 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
268
269 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
270 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
271
272 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
273 buffer->name = name;
274 buffer->pitch = pitch;
275 buffer->cpp = 4;
276 buffer->flags = 0;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200277}
278
279static int
280get_aux_bo(struct dri2_egl_surface *dri2_surf,
281 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
282{
283 struct dri2_egl_display *dri2_dpy =
284 dri2_egl_display(dri2_surf->base.Resource.Display);
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700285 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200286
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700287 if (b == NULL) {
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200288 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
289 attachment, format,
290 dri2_surf->base.Width,
291 dri2_surf->base.Height);
Mandeep Singh Baines0695cf62012-04-10 14:48:14 -0700292 dri2_surf->dri_buffers[attachment] = b;
293 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200294 if (b == NULL)
295 return -1;
296
297 memcpy(buffer, b, sizeof *buffer);
298
299 return 0;
300}
301
302static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800303dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200304 int *width, int *height,
305 unsigned int *attachments, int count,
306 int *out_count, void *loaderPrivate)
307{
308 struct dri2_egl_surface *dri2_surf = loaderPrivate;
309 int i, j;
310
311 dri2_surf->buffer_count = 0;
312 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
313 assert(attachments[i] < __DRI_BUFFER_COUNT);
314 assert(dri2_surf->buffer_count < 5);
315
316 switch (attachments[i]) {
317 case __DRI_BUFFER_BACK_LEFT:
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800318 if (get_back_bo(dri2_surf) < 0) {
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200319 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
320 return NULL;
321 }
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800322 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200323 break;
324 default:
325 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
326 &dri2_surf->buffers[j]) < 0) {
327 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
328 return NULL;
329 }
330 break;
331 }
332 }
333
334 *out_count = j;
335 if (j == 0)
336 return NULL;
337
338 *width = dri2_surf->base.Width;
339 *height = dri2_surf->base.Height;
340
341 return dri2_surf->buffers;
342}
343
344static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800345dri2_drm_get_buffers(__DRIdrawable * driDrawable,
346 int *width, int *height,
347 unsigned int *attachments, int count,
348 int *out_count, void *loaderPrivate)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200349{
350 unsigned int *attachments_with_format;
351 __DRIbuffer *buffer;
352 const unsigned int format = 32;
353 int i;
354
Carl Worthecc89e42014-09-03 14:33:18 -0700355 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200356 if (!attachments_with_format) {
357 *out_count = 0;
358 return NULL;
359 }
360
361 for (i = 0; i < count; ++i) {
362 attachments_with_format[2*i] = attachments[i];
363 attachments_with_format[2*i + 1] = format;
364 }
365
366 buffer =
Chad Versaced019cd82014-01-28 12:47:38 -0800367 dri2_drm_get_buffers_with_format(driDrawable,
368 width, height,
369 attachments_with_format, count,
370 out_count, loaderPrivate);
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200371
372 free(attachments_with_format);
373
374 return buffer;
375}
376
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800377static int
Chad Versaced019cd82014-01-28 12:47:38 -0800378dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
379 unsigned int format,
380 uint32_t *stamp,
381 void *loaderPrivate,
382 uint32_t buffer_mask,
383 struct __DRIimageList *buffers)
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800384{
385 struct dri2_egl_surface *dri2_surf = loaderPrivate;
386 struct gbm_dri_bo *bo;
387
388 if (get_back_bo(dri2_surf) < 0)
389 return 0;
390
391 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
392 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
393 buffers->back = bo->image;
394
395 return 1;
396}
397
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200398static void
Chad Versaced019cd82014-01-28 12:47:38 -0800399dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200400{
401 (void) driDrawable;
402 (void) loaderPrivate;
403}
404
405static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800406dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200407{
408 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
409 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500410 int i;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200411
Giovanni Campagna8430af52014-06-15 13:49:49 +0200412 if (dri2_dpy->swrast) {
413 (*dri2_dpy->core->swapBuffers)(dri2_surf->dri_drawable);
414 } else {
415 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
416 if (dri2_surf->current)
417 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
418 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
419 if (dri2_surf->color_buffers[i].age > 0)
420 dri2_surf->color_buffers[i].age++;
Lionel Landwerlind175e7c2014-10-14 10:39:47 +0100421
422 /* Make sure we have a back buffer in case we're swapping without
423 * ever rendering. */
424 if (get_back_bo(dri2_surf) < 0) {
425 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
426 return EGL_FALSE;
427 }
428
Giovanni Campagna8430af52014-06-15 13:49:49 +0200429 dri2_surf->current = dri2_surf->back;
430 dri2_surf->current->age = 1;
431 dri2_surf->back = NULL;
432 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200433
Eric Anholt70e8ccc2014-12-21 11:51:33 -0800434 dri2_flush_drawable_for_swapbuffers(disp, draw);
Giovanni Campagna8430af52014-06-15 13:49:49 +0200435 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
436 }
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200437
438 return EGL_TRUE;
439}
440
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500441static EGLint
Chad Versaced019cd82014-01-28 12:47:38 -0800442dri2_drm_query_buffer_age(_EGLDriver *drv,
443 _EGLDisplay *disp, _EGLSurface *surface)
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500444{
445 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500446
Kristian Høgsberg04e3ef02013-11-08 22:06:51 -0800447 if (get_back_bo(dri2_surf) < 0) {
Kristian Høgsberg4e42e562012-12-13 23:39:45 -0500448 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
449 return 0;
450 }
451
452 return dri2_surf->back->age;
453}
454
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200455static _EGLImage *
Chad Versaced019cd82014-01-28 12:47:38 -0800456dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
457 EGLClientBuffer buffer, const EGLint *attr_list)
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200458{
459 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
460 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
461 struct dri2_egl_image *dri2_img;
462
463 dri2_img = malloc(sizeof *dri2_img);
464 if (!dri2_img) {
465 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
466 return NULL;
467 }
468
469 if (!_eglInitImage(&dri2_img->base, disp)) {
470 free(dri2_img);
471 return NULL;
472 }
473
474 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
475 if (dri2_img->dri_image == NULL) {
476 free(dri2_img);
477 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
478 return NULL;
479 }
480
481 return &dri2_img->base;
482}
483
484static _EGLImage *
485dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
486 _EGLContext *ctx, EGLenum target,
487 EGLClientBuffer buffer, const EGLint *attr_list)
488{
489 (void) drv;
490
491 switch (target) {
492 case EGL_NATIVE_PIXMAP_KHR:
Chad Versaced019cd82014-01-28 12:47:38 -0800493 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200494 default:
495 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
496 }
497}
498
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100499static int
500dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
501{
502 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
503
504 return drmAuthMagic(dri2_dpy->fd, id);
505}
506
Giovanni Campagna8430af52014-06-15 13:49:49 +0200507static void
508swrast_put_image2(__DRIdrawable *driDrawable,
509 int op,
510 int x,
511 int y,
512 int width,
513 int height,
514 int stride,
515 char *data,
516 void *loaderPrivate)
517{
518 struct dri2_egl_surface *dri2_surf = loaderPrivate;
519 int internal_stride, i;
520 struct gbm_dri_bo *bo;
521
522 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
523 op != __DRI_SWRAST_IMAGE_OP_SWAP)
524 return;
525
526 if (get_swrast_front_bo(dri2_surf) < 0)
527 return;
528
529 bo = gbm_dri_bo(dri2_surf->current->bo);
530 if (gbm_dri_bo_map(bo) == NULL)
531 return;
532
533 internal_stride = bo->base.base.stride;
534
535 for (i = 0; i < height; i++) {
536 memcpy(bo->map + (x + i) * internal_stride + y,
537 data + i * stride, stride);
538 }
539
540 gbm_dri_bo_unmap(bo);
541}
542
543static void
544swrast_get_image(__DRIdrawable *driDrawable,
545 int x,
546 int y,
547 int width,
548 int height,
549 char *data,
550 void *loaderPrivate)
551{
552 struct dri2_egl_surface *dri2_surf = loaderPrivate;
553 int internal_stride, stride, i;
554 struct gbm_dri_bo *bo;
555
556 if (get_swrast_front_bo(dri2_surf) < 0)
557 return;
558
559 bo = gbm_dri_bo(dri2_surf->current->bo);
560 if (gbm_dri_bo_map(bo) == NULL)
561 return;
562
563 internal_stride = bo->base.base.stride;
564 stride = width * 4;
565
566 for (i = 0; i < height; i++) {
567 memcpy(data + i * stride,
568 bo->map + (x + i) * internal_stride + y, stride);
569 }
570
571 gbm_dri_bo_unmap(bo);
572}
573
Chad Versace90502b12014-01-28 11:41:46 -0800574static struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
575 .authenticate = dri2_drm_authenticate,
Chad Versace0a0c8812014-01-28 16:39:09 -0800576 .create_window_surface = dri2_drm_create_window_surface,
Chad Versace1787f562014-02-09 09:13:08 -0800577 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
Chad Versacebf200762014-01-28 17:03:03 -0800578 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
Chad Versace958dd802014-01-28 17:03:03 -0800579 .destroy_surface = dri2_drm_destroy_surface,
Chad Versaceeef68a92014-01-28 17:03:03 -0800580 .create_image = dri2_drm_create_image_khr,
Chad Versace8b9298a2014-01-28 12:34:19 -0800581 .swap_interval = dri2_fallback_swap_interval,
Chad Versacead173bc2014-01-28 16:21:21 -0800582 .swap_buffers = dri2_drm_swap_buffers,
Chad Versaced03948a2014-01-28 16:26:44 -0800583 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
Chad Versace75d398e2014-01-28 17:03:03 -0800584 .swap_buffers_region = dri2_fallback_swap_buffers_region,
Chad Versace688a0e82014-01-28 17:03:03 -0800585 .post_sub_buffer = dri2_fallback_post_sub_buffer,
Chad Versacebc2cbc02014-01-28 17:03:03 -0800586 .copy_buffers = dri2_fallback_copy_buffers,
Chad Versace3fdfbd22014-01-28 17:03:03 -0800587 .query_buffer_age = dri2_drm_query_buffer_age,
Chad Versaceeadd5e02014-01-28 17:03:03 -0800588 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
Sarah Sharpc524f3e2014-05-06 12:10:57 -0700589 .get_sync_values = dri2_fallback_get_sync_values,
Chad Versace90502b12014-01-28 11:41:46 -0800590};
591
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500592EGLBoolean
593dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
594{
595 struct dri2_egl_display *dri2_dpy;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200596 struct gbm_device *gbm;
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200597 int fd = -1;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500598 int i;
599
Emil Velikov8d4357b2014-01-11 04:52:48 +0000600 loader_set_logger(_eglLog);
601
Matt Turner6bda0272012-09-04 23:09:22 -0700602 dri2_dpy = calloc(1, sizeof *dri2_dpy);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500603 if (!dri2_dpy)
604 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200605
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500606 disp->DriverData = (void *) dri2_dpy;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500607
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200608 gbm = disp->PlatformDisplay;
609 if (gbm == NULL) {
Jonathan Grayc973e442014-04-03 16:22:26 +1100610 char buf[64];
611 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
612 if (n != -1 && n < sizeof(buf))
613 fd = open(buf, O_RDWR);
614 if (fd < 0)
615 fd = open("/dev/dri/card0", O_RDWR);
Benjamin Franzke2a584532011-12-13 14:43:48 +0100616 dri2_dpy->own_device = 1;
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200617 gbm = gbm_create_device(fd);
618 if (gbm == NULL)
619 return EGL_FALSE;
620 }
621
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200622 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
623 free(dri2_dpy);
624 return EGL_FALSE;
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100625 }
626
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200627 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
628 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
629 free(dri2_dpy);
630 return EGL_FALSE;
631 }
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500632
Benjamin Franzke32f4cf32011-06-29 08:49:39 +0200633 if (fd < 0) {
634 fd = dup(gbm_device_get_fd(gbm));
635 if (fd < 0) {
636 free(dri2_dpy);
637 return EGL_FALSE;
638 }
639 }
640
641 dri2_dpy->fd = fd;
Emil Velikov8d4357b2014-01-11 04:52:48 +0000642 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
Emil Velikov5cb1cad2014-06-02 12:26:17 +0100643 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500644
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200645 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
646 dri2_dpy->core = dri2_dpy->gbm_dri->core;
647 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
648 dri2_dpy->image = dri2_dpy->gbm_dri->image;
Ander Conselvan de Oliveira410aa3e2012-01-25 16:24:15 +0200649 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200650 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200651 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
652
653 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
654 dri2_dpy->gbm_dri->lookup_user_data = disp;
655
Chad Versaced019cd82014-01-28 12:47:38 -0800656 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
657 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
658 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
659 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200660 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
661 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
Ander Conselvan de Oliveira4a976b62012-01-25 16:24:17 +0200662
663 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
664 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
665 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
666
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200667 dri2_setup_screen(disp);
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500668
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700669 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
670 EGLint format, attr_list[3];
671 unsigned int mask;
672
673 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
674 __DRI_ATTRIB_RED_MASK, &mask);
675 if (mask == 0x3ff00000)
676 format = GBM_FORMAT_XRGB2101010;
677 else if (mask == 0x00ff0000)
678 format = GBM_FORMAT_XRGB8888;
679 else if (mask == 0xf800)
680 format = GBM_FORMAT_RGB565;
681 else
682 continue;
683
684 attr_list[0] = EGL_NATIVE_VISUAL_ID;
685 attr_list[1] = format;
686 attr_list[2] = EGL_NONE;
687
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200688 dri2_add_config(disp, dri2_dpy->driver_configs[i],
Kristian Høgsbergdf479cf2013-09-25 14:57:22 -0700689 i + 1, EGL_WINDOW_BIT, attr_list, NULL);
690 }
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200691
Andreas Pokorny53b614b2014-09-03 22:43:41 +0200692 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
Giovanni Campagna8430af52014-06-15 13:49:49 +0200693 if (dri2_dpy->dri2)
694 disp->Extensions.EXT_buffer_age = EGL_TRUE;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500695
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100696#ifdef HAVE_WAYLAND_PLATFORM
Giovanni Campagnae57ad3d2014-07-23 19:28:52 +0100697 if (dri2_dpy->image) {
698 if (dri2_dpy->image->base.version >= 10 &&
699 dri2_dpy->image->getCapabilities != NULL) {
700 int capabilities;
701
702 capabilities =
703 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
704 disp->Extensions.WL_bind_wayland_display =
705 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
706 } else
707 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
708 }
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100709#endif
Benjamin Franzkee5fc4c82011-05-30 10:50:52 +0200710
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500711 /* we're supporting EGL 1.4 */
712 disp->VersionMajor = 1;
713 disp->VersionMinor = 4;
714
Chad Versace90502b12014-01-28 11:41:46 -0800715 /* Fill vtbl last to prevent accidentally calling virtual function during
716 * initialization.
717 */
718 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
719
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500720 return EGL_TRUE;
Kristian Høgsberg9dc5de52011-02-02 22:21:13 -0500721}