wayland: Don't rely on static variable for identifying wl_drm buffers
[mesa.git] / src / egl / drivers / dri2 / egl_dri2.c
1 /*
2 * Copyright © 2010 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 <string.h>
30 #include <stdio.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37 #include <drm_fourcc.h>
38 #include <GL/gl.h>
39 #include <GL/internal/dri_interface.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include "egl_dri2.h"
44
45 #ifdef HAVE_WAYLAND_PLATFORM
46 #include "wayland-drm.h"
47 #endif
48
49 const __DRIuseInvalidateExtension use_invalidate = {
50 { __DRI_USE_INVALIDATE, 1 }
51 };
52
53 EGLint dri2_to_egl_attribute_map[] = {
54 0,
55 EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
56 EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
57 EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
58 EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
59 EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
60 EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */
61 EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
62 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
63 EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
64 EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
65 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
66 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
67 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
68 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
69 EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
70 EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
71 0, /* __DRI_ATTRIB_RENDER_TYPE, */
72 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
73 0, /* __DRI_ATTRIB_CONFORMANT */
74 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
75 0, /* __DRI_ATTRIB_STEREO */
76 0, /* __DRI_ATTRIB_AUX_BUFFERS */
77 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
78 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
79 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
80 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
81 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
82 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
83 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
84 0, /* __DRI_ATTRIB_RED_MASK */
85 0, /* __DRI_ATTRIB_GREEN_MASK */
86 0, /* __DRI_ATTRIB_BLUE_MASK */
87 0, /* __DRI_ATTRIB_ALPHA_MASK */
88 EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
89 EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
90 EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
91 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
92 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
93 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
94 0, /* __DRI_ATTRIB_SWAP_METHOD */
95 EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
96 EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
97 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
98 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
99 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
100 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
101 EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */
102 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
103 };
104
105 static EGLBoolean
106 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
107 {
108 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
109 return EGL_FALSE;
110
111 if (!_eglMatchConfig(conf, criteria))
112 return EGL_FALSE;
113
114 return EGL_TRUE;
115 }
116
117 struct dri2_egl_config *
118 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
119 int depth, EGLint surface_type, const EGLint *attr_list,
120 const unsigned int *rgba_masks)
121 {
122 struct dri2_egl_config *conf;
123 struct dri2_egl_display *dri2_dpy;
124 _EGLConfig base;
125 unsigned int attrib, value, double_buffer;
126 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
127 unsigned int dri_masks[4] = { 0, 0, 0, 0 };
128 _EGLConfig *matching_config;
129 EGLint num_configs = 0;
130 EGLint config_id;
131 int i;
132
133 dri2_dpy = disp->DriverData;
134 _eglInitConfig(&base, disp, id);
135
136 i = 0;
137 double_buffer = 0;
138 bind_to_texture_rgb = 0;
139 bind_to_texture_rgba = 0;
140
141 while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
142 switch (attrib) {
143 case __DRI_ATTRIB_RENDER_TYPE:
144 if (value & __DRI_ATTRIB_RGBA_BIT)
145 value = EGL_RGB_BUFFER;
146 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
147 value = EGL_LUMINANCE_BUFFER;
148 else
149 return NULL;
150 _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value);
151 break;
152
153 case __DRI_ATTRIB_CONFIG_CAVEAT:
154 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
155 value = EGL_NON_CONFORMANT_CONFIG;
156 else if (value & __DRI_ATTRIB_SLOW_BIT)
157 value = EGL_SLOW_CONFIG;
158 else
159 value = EGL_NONE;
160 _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value);
161 break;
162
163 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
164 bind_to_texture_rgb = value;
165 break;
166
167 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
168 bind_to_texture_rgba = value;
169 break;
170
171 case __DRI_ATTRIB_DOUBLE_BUFFER:
172 double_buffer = value;
173 break;
174
175 case __DRI_ATTRIB_RED_MASK:
176 dri_masks[0] = value;
177 break;
178
179 case __DRI_ATTRIB_GREEN_MASK:
180 dri_masks[1] = value;
181 break;
182
183 case __DRI_ATTRIB_BLUE_MASK:
184 dri_masks[2] = value;
185 break;
186
187 case __DRI_ATTRIB_ALPHA_MASK:
188 dri_masks[3] = value;
189 break;
190
191 default:
192 key = dri2_to_egl_attribute_map[attrib];
193 if (key != 0)
194 _eglSetConfigKey(&base, key, value);
195 break;
196 }
197 }
198
199 if (attr_list)
200 for (i = 0; attr_list[i] != EGL_NONE; i += 2)
201 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
202
203 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig. Otherwise
204 * it will only match a 32-bit RGBA visual. On a composited window manager
205 * on X11, this will make all of the EGLConfigs with destination alpha get
206 * blended by the compositor. This is probably not what the application
207 * wants... especially on drivers that only have 32-bit RGBA EGLConfigs!
208 */
209 if (depth > 0 && depth != base.BufferSize
210 && !(depth == 24 && base.BufferSize == 32))
211 return NULL;
212
213 if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks)))
214 return NULL;
215
216 base.NativeRenderable = EGL_TRUE;
217
218 base.SurfaceType = surface_type;
219 if (surface_type & (EGL_PBUFFER_BIT |
220 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
221 base.BindToTextureRGB = bind_to_texture_rgb;
222 if (base.AlphaSize > 0)
223 base.BindToTextureRGBA = bind_to_texture_rgba;
224 }
225
226 base.RenderableType = disp->ClientAPIs;
227 base.Conformant = disp->ClientAPIs;
228
229 base.MinSwapInterval = dri2_dpy->min_swap_interval;
230 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
231
232 if (!_eglValidateConfig(&base, EGL_FALSE)) {
233 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
234 return NULL;
235 }
236
237 config_id = base.ConfigID;
238 base.ConfigID = EGL_DONT_CARE;
239 base.SurfaceType = EGL_DONT_CARE;
240 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
241 (_EGLArrayForEach) dri2_match_config, &base);
242
243 if (num_configs == 1) {
244 conf = (struct dri2_egl_config *) matching_config;
245
246 if (double_buffer && !conf->dri_double_config)
247 conf->dri_double_config = dri_config;
248 else if (!double_buffer && !conf->dri_single_config)
249 conf->dri_single_config = dri_config;
250 else
251 /* a similar config type is already added (unlikely) => discard */
252 return NULL;
253 }
254 else if (num_configs == 0) {
255 conf = malloc(sizeof *conf);
256 if (conf == NULL)
257 return NULL;
258
259 memcpy(&conf->base, &base, sizeof base);
260 if (double_buffer) {
261 conf->dri_double_config = dri_config;
262 conf->dri_single_config = NULL;
263 } else {
264 conf->dri_single_config = dri_config;
265 conf->dri_double_config = NULL;
266 }
267 conf->base.SurfaceType = 0;
268 conf->base.ConfigID = config_id;
269
270 _eglLinkConfig(&conf->base);
271 }
272 else {
273 assert(0);
274 return NULL;
275 }
276
277 if (double_buffer) {
278 surface_type &= ~EGL_PIXMAP_BIT;
279 }
280
281 conf->base.SurfaceType |= surface_type;
282
283 return conf;
284 }
285
286 __DRIimage *
287 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
288 {
289 _EGLDisplay *disp = data;
290 struct dri2_egl_image *dri2_img;
291 _EGLImage *img;
292
293 (void) screen;
294
295 img = _eglLookupImage(image, disp);
296 if (img == NULL) {
297 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
298 return NULL;
299 }
300
301 dri2_img = dri2_egl_image(image);
302
303 return dri2_img->dri_image;
304 }
305
306 const __DRIimageLookupExtension image_lookup_extension = {
307 { __DRI_IMAGE_LOOKUP, 1 },
308 dri2_lookup_egl_image
309 };
310
311 static const char dri_driver_path[] = DEFAULT_DRIVER_DIR;
312
313 struct dri2_extension_match {
314 const char *name;
315 int version;
316 int offset;
317 };
318
319 static struct dri2_extension_match dri2_driver_extensions[] = {
320 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
321 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
322 { NULL, 0, 0 }
323 };
324
325 static struct dri2_extension_match dri2_core_extensions[] = {
326 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
327 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
328 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
329 { NULL, 0, 0 }
330 };
331
332 static struct dri2_extension_match swrast_driver_extensions[] = {
333 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
334 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
335 { NULL, 0, 0 }
336 };
337
338 static struct dri2_extension_match swrast_core_extensions[] = {
339 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
340 { NULL, 0, 0 }
341 };
342
343 static EGLBoolean
344 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
345 struct dri2_extension_match *matches,
346 const __DRIextension **extensions)
347 {
348 int i, j, ret = EGL_TRUE;
349 void *field;
350
351 for (i = 0; extensions[i]; i++) {
352 _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name);
353 for (j = 0; matches[j].name; j++) {
354 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
355 extensions[i]->version >= matches[j].version) {
356 field = ((char *) dri2_dpy + matches[j].offset);
357 *(const __DRIextension **) field = extensions[i];
358 _eglLog(_EGL_INFO, "DRI2: found extension %s version %d",
359 extensions[i]->name, extensions[i]->version);
360 }
361 }
362 }
363
364 for (j = 0; matches[j].name; j++) {
365 field = ((char *) dri2_dpy + matches[j].offset);
366 if (*(const __DRIextension **) field == NULL) {
367 _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d",
368 matches[j].name, matches[j].version);
369 ret = EGL_FALSE;
370 }
371 }
372
373 return ret;
374 }
375
376 static const __DRIextension **
377 dri2_open_driver(_EGLDisplay *disp)
378 {
379 struct dri2_egl_display *dri2_dpy = disp->DriverData;
380 const __DRIextension **extensions;
381 char path[PATH_MAX], *search_paths, *p, *next, *end;
382
383 search_paths = NULL;
384 if (geteuid() == getuid()) {
385 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
386 search_paths = getenv("LIBGL_DRIVERS_PATH");
387 }
388 if (search_paths == NULL)
389 search_paths = DEFAULT_DRIVER_DIR;
390
391 dri2_dpy->driver = NULL;
392 end = search_paths + strlen(search_paths);
393 for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) {
394 int len;
395 next = strchr(p, ':');
396 if (next == NULL)
397 next = end;
398
399 len = next - p;
400 #if GLX_USE_TLS
401 snprintf(path, sizeof path,
402 "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name);
403 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
404 #endif
405 if (dri2_dpy->driver == NULL) {
406 snprintf(path, sizeof path,
407 "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name);
408 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
409 if (dri2_dpy->driver == NULL)
410 _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror());
411 }
412 }
413
414 if (dri2_dpy->driver == NULL) {
415 _eglLog(_EGL_WARNING,
416 "DRI2: failed to open %s (search paths %s)",
417 dri2_dpy->driver_name, search_paths);
418 return NULL;
419 }
420
421 _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
422 extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
423 if (extensions == NULL) {
424 _eglLog(_EGL_WARNING,
425 "DRI2: driver exports no extensions (%s)", dlerror());
426 dlclose(dri2_dpy->driver);
427 }
428
429 return extensions;
430 }
431
432 EGLBoolean
433 dri2_load_driver(_EGLDisplay *disp)
434 {
435 struct dri2_egl_display *dri2_dpy = disp->DriverData;
436 const __DRIextension **extensions;
437
438 extensions = dri2_open_driver(disp);
439 if (!extensions)
440 return EGL_FALSE;
441
442 if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) {
443 dlclose(dri2_dpy->driver);
444 return EGL_FALSE;
445 }
446
447 return EGL_TRUE;
448 }
449
450 EGLBoolean
451 dri2_load_driver_swrast(_EGLDisplay *disp)
452 {
453 struct dri2_egl_display *dri2_dpy = disp->DriverData;
454 const __DRIextension **extensions;
455
456 dri2_dpy->driver_name = "swrast";
457 extensions = dri2_open_driver(disp);
458
459 if (!extensions)
460 return EGL_FALSE;
461
462 if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) {
463 dlclose(dri2_dpy->driver);
464 return EGL_FALSE;
465 }
466
467 return EGL_TRUE;
468 }
469
470 void
471 dri2_setup_screen(_EGLDisplay *disp)
472 {
473 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
474 unsigned int api_mask;
475
476 if (dri2_dpy->dri2) {
477 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
478 } else {
479 assert(dri2_dpy->swrast);
480 api_mask = 1 << __DRI_API_OPENGL |
481 1 << __DRI_API_GLES |
482 1 << __DRI_API_GLES2 |
483 1 << __DRI_API_GLES3;
484 }
485
486 disp->ClientAPIs = 0;
487 if (api_mask & (1 <<__DRI_API_OPENGL))
488 disp->ClientAPIs |= EGL_OPENGL_BIT;
489 if (api_mask & (1 <<__DRI_API_GLES))
490 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
491 if (api_mask & (1 << __DRI_API_GLES2))
492 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
493 if (api_mask & (1 << __DRI_API_GLES3))
494 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
495
496 assert(dri2_dpy->dri2 || dri2_dpy->swrast);
497 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
498
499 if (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) {
500 disp->Extensions.KHR_create_context = EGL_TRUE;
501
502 if (dri2_dpy->robustness)
503 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
504 }
505
506 if (dri2_dpy->image) {
507 disp->Extensions.MESA_drm_image = EGL_TRUE;
508 disp->Extensions.KHR_image_base = EGL_TRUE;
509 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
510 if (dri2_dpy->image->base.version >= 5 &&
511 dri2_dpy->image->createImageFromTexture) {
512 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
513 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
514 }
515 if (dri2_dpy->image->base.version >= 8 &&
516 dri2_dpy->image->createImageFromDmaBufs) {
517 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
518 }
519 }
520 }
521
522 EGLBoolean
523 dri2_create_screen(_EGLDisplay *disp)
524 {
525 const __DRIextension **extensions;
526 struct dri2_egl_display *dri2_dpy;
527
528 dri2_dpy = disp->DriverData;
529
530 if (dri2_dpy->dri2) {
531 dri2_dpy->dri_screen =
532 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
533 &dri2_dpy->driver_configs, disp);
534 } else {
535 assert(dri2_dpy->swrast);
536 dri2_dpy->dri_screen =
537 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions,
538 &dri2_dpy->driver_configs, disp);
539 }
540
541 if (dri2_dpy->dri_screen == NULL) {
542 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
543 return EGL_FALSE;
544 }
545
546 dri2_dpy->own_dri_screen = 1;
547
548 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
549
550 if (dri2_dpy->dri2) {
551 unsigned i;
552
553 if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
554 goto cleanup_dri_screen;
555
556 for (i = 0; extensions[i]; i++) {
557 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) {
558 dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i];
559 }
560 if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) {
561 dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i];
562 }
563 }
564 } else {
565 assert(dri2_dpy->swrast);
566 if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions))
567 goto cleanup_dri_screen;
568 }
569
570 dri2_setup_screen(disp);
571
572 return EGL_TRUE;
573
574 cleanup_dri_screen:
575 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
576
577 return EGL_FALSE;
578 }
579
580 /**
581 * Called via eglInitialize(), GLX_drv->API.Initialize().
582 */
583 static EGLBoolean
584 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
585 {
586 /* not until swrast_dri is supported */
587 if (disp->Options.UseFallback)
588 return EGL_FALSE;
589
590 switch (disp->Platform) {
591 #ifdef HAVE_X11_PLATFORM
592 case _EGL_PLATFORM_X11:
593 if (disp->Options.TestOnly)
594 return EGL_TRUE;
595 return dri2_initialize_x11(drv, disp);
596 #endif
597
598 #ifdef HAVE_LIBUDEV
599 #ifdef HAVE_DRM_PLATFORM
600 case _EGL_PLATFORM_DRM:
601 if (disp->Options.TestOnly)
602 return EGL_TRUE;
603 return dri2_initialize_drm(drv, disp);
604 #endif
605 #ifdef HAVE_WAYLAND_PLATFORM
606 case _EGL_PLATFORM_WAYLAND:
607 if (disp->Options.TestOnly)
608 return EGL_TRUE;
609 return dri2_initialize_wayland(drv, disp);
610 #endif
611 #endif
612 #ifdef HAVE_ANDROID_PLATFORM
613 case _EGL_PLATFORM_ANDROID:
614 if (disp->Options.TestOnly)
615 return EGL_TRUE;
616 return dri2_initialize_android(drv, disp);
617 #endif
618
619 default:
620 return EGL_FALSE;
621 }
622 }
623
624 /**
625 * Called via eglTerminate(), drv->API.Terminate().
626 */
627 static EGLBoolean
628 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
629 {
630 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
631
632 _eglReleaseDisplayResources(drv, disp);
633 _eglCleanupDisplay(disp);
634
635 if (dri2_dpy->own_dri_screen)
636 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
637 if (dri2_dpy->fd)
638 close(dri2_dpy->fd);
639 if (dri2_dpy->driver)
640 dlclose(dri2_dpy->driver);
641 free(dri2_dpy->device_name);
642
643 if (disp->PlatformDisplay == NULL) {
644 switch (disp->Platform) {
645 #ifdef HAVE_X11_PLATFORM
646 case _EGL_PLATFORM_X11:
647 xcb_disconnect(dri2_dpy->conn);
648 break;
649 #endif
650 #ifdef HAVE_DRM_PLATFORM
651 case _EGL_PLATFORM_DRM:
652 if (dri2_dpy->own_device) {
653 gbm_device_destroy(&dri2_dpy->gbm_dri->base.base);
654 }
655 break;
656 #endif
657 default:
658 break;
659 }
660 }
661
662 free(dri2_dpy);
663 disp->DriverData = NULL;
664
665 return EGL_TRUE;
666 }
667
668 /**
669 * Set the error code after a call to
670 * dri2_egl_display::dri2::createContextAttribs.
671 */
672 static void
673 dri2_create_context_attribs_error(int dri_error)
674 {
675 EGLint egl_error;
676
677 switch (dri_error) {
678 case __DRI_CTX_ERROR_SUCCESS:
679 return;
680
681 case __DRI_CTX_ERROR_NO_MEMORY:
682 egl_error = EGL_BAD_ALLOC;
683 break;
684
685 /* From the EGL_KHR_create_context spec, section "Errors":
686 *
687 * * If <config> does not support a client API context compatible
688 * with the requested API major and minor version, [...] context flags,
689 * and context reset notification behavior (for client API types where
690 * these attributes are supported), then an EGL_BAD_MATCH error is
691 * generated.
692 *
693 * * If an OpenGL ES context is requested and the values for
694 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
695 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
696 * is not defined, than an EGL_BAD_MATCH error is generated.
697 *
698 * * If an OpenGL context is requested, the requested version is
699 * greater than 3.2, and the value for attribute
700 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
701 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
702 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
703 * one of these bits set; or if the implementation does not support
704 * the requested profile, then an EGL_BAD_MATCH error is generated.
705 */
706 case __DRI_CTX_ERROR_BAD_API:
707 case __DRI_CTX_ERROR_BAD_VERSION:
708 case __DRI_CTX_ERROR_BAD_FLAG:
709 egl_error = EGL_BAD_MATCH;
710 break;
711
712 /* From the EGL_KHR_create_context spec, section "Errors":
713 *
714 * * If an attribute name or attribute value in <attrib_list> is not
715 * recognized (including unrecognized bits in bitmask attributes),
716 * then an EGL_BAD_ATTRIBUTE error is generated."
717 */
718 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
719 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
720 egl_error = EGL_BAD_ATTRIBUTE;
721 break;
722
723 default:
724 assert(0);
725 egl_error = EGL_BAD_MATCH;
726 break;
727 }
728
729 _eglError(egl_error, "dri2_create_context");
730 }
731
732 /**
733 * Called via eglCreateContext(), drv->API.CreateContext().
734 */
735 static _EGLContext *
736 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
737 _EGLContext *share_list, const EGLint *attrib_list)
738 {
739 struct dri2_egl_context *dri2_ctx;
740 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
741 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
742 __DRIcontext *shared =
743 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
744 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
745 const __DRIconfig *dri_config;
746 int api;
747
748 (void) drv;
749
750 dri2_ctx = malloc(sizeof *dri2_ctx);
751 if (!dri2_ctx) {
752 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
753 return NULL;
754 }
755
756 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
757 goto cleanup;
758
759 switch (dri2_ctx->base.ClientAPI) {
760 case EGL_OPENGL_ES_API:
761 switch (dri2_ctx->base.ClientMajorVersion) {
762 case 1:
763 api = __DRI_API_GLES;
764 break;
765 case 2:
766 api = __DRI_API_GLES2;
767 break;
768 case 3:
769 api = __DRI_API_GLES3;
770 break;
771 default:
772 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
773 return NULL;
774 }
775 break;
776 case EGL_OPENGL_API:
777 if ((dri2_ctx->base.ClientMajorVersion >= 4
778 || (dri2_ctx->base.ClientMajorVersion == 3
779 && dri2_ctx->base.ClientMinorVersion >= 2))
780 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
781 api = __DRI_API_OPENGL_CORE;
782 else
783 api = __DRI_API_OPENGL;
784 break;
785 default:
786 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
787 free(dri2_ctx);
788 return NULL;
789 }
790
791 if (conf != NULL) {
792 /* The config chosen here isn't necessarily
793 * used for surfaces later.
794 * A pixmap surface will use the single config.
795 * This opportunity depends on disabling the
796 * doubleBufferMode check in
797 * src/mesa/main/context.c:check_compatible()
798 */
799 if (dri2_config->dri_double_config)
800 dri_config = dri2_config->dri_double_config;
801 else
802 dri_config = dri2_config->dri_single_config;
803
804 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
805 * makes sure the back buffer will always be used.
806 */
807 if (conf->SurfaceType & EGL_WINDOW_BIT)
808 dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER;
809 }
810 else
811 dri_config = NULL;
812
813 if (dri2_dpy->dri2) {
814 if (dri2_dpy->dri2->base.version >= 3) {
815 unsigned error;
816 unsigned num_attribs = 0;
817 uint32_t ctx_attribs[8];
818
819 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
820 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMajorVersion;
821 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
822 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMinorVersion;
823
824 if (dri2_ctx->base.Flags != 0) {
825 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
826 * extension, don't even try to send it the robust-access flag.
827 * It may explode. Instead, generate the required EGL error here.
828 */
829 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
830 && !dri2_dpy->robustness) {
831 _eglError(EGL_BAD_MATCH, "eglCreateContext");
832 goto cleanup;
833 }
834
835 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
836 ctx_attribs[num_attribs++] = dri2_ctx->base.Flags;
837 }
838
839 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
840 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
841 * extension, don't even try to send it a reset strategy. It may
842 * explode. Instead, generate the required EGL error here.
843 */
844 if (!dri2_dpy->robustness) {
845 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
846 goto cleanup;
847 }
848
849 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
850 ctx_attribs[num_attribs++] = __DRI_CTX_RESET_LOSE_CONTEXT;
851 }
852
853 assert(num_attribs <= ARRAY_SIZE(ctx_attribs));
854
855 dri2_ctx->dri_context =
856 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
857 api,
858 dri_config,
859 shared,
860 num_attribs / 2,
861 ctx_attribs,
862 & error,
863 dri2_ctx);
864 dri2_create_context_attribs_error(error);
865 } else {
866 dri2_ctx->dri_context =
867 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
868 api,
869 dri_config,
870 shared,
871 dri2_ctx);
872 }
873 } else {
874 assert(dri2_dpy->swrast);
875 dri2_ctx->dri_context =
876 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
877 api,
878 dri_config,
879 shared,
880 dri2_ctx);
881 }
882
883 if (!dri2_ctx->dri_context)
884 goto cleanup;
885
886 return &dri2_ctx->base;
887
888 cleanup:
889 free(dri2_ctx);
890 return NULL;
891 }
892
893 /**
894 * Called via eglDestroyContext(), drv->API.DestroyContext().
895 */
896 static EGLBoolean
897 dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
898 {
899 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
900 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
901
902 if (_eglPutContext(ctx)) {
903 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
904 free(dri2_ctx);
905 }
906
907 return EGL_TRUE;
908 }
909
910 /**
911 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
912 */
913 static EGLBoolean
914 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
915 _EGLSurface *rsurf, _EGLContext *ctx)
916 {
917 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
918 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
919 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
920 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
921 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
922 _EGLContext *old_ctx;
923 _EGLSurface *old_dsurf, *old_rsurf;
924 __DRIdrawable *ddraw, *rdraw;
925 __DRIcontext *cctx;
926
927 /* make new bindings */
928 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
929 return EGL_FALSE;
930
931 /* flush before context switch */
932 if (old_ctx && dri2_drv->glFlush)
933 dri2_drv->glFlush();
934
935 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
936 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
937 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
938
939 if (old_ctx) {
940 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
941 dri2_dpy->core->unbindContext(old_cctx);
942 }
943
944 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
945 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
946 if (old_dsurf)
947 drv->API.DestroySurface(drv, disp, old_dsurf);
948 if (old_rsurf)
949 drv->API.DestroySurface(drv, disp, old_rsurf);
950 if (old_ctx)
951 drv->API.DestroyContext(drv, disp, old_ctx);
952
953 return EGL_TRUE;
954 } else {
955 /* undo the previous _eglBindContext */
956 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf);
957 assert(&dri2_ctx->base == ctx &&
958 &dri2_dsurf->base == dsurf &&
959 &dri2_rsurf->base == rsurf);
960
961 _eglPutSurface(dsurf);
962 _eglPutSurface(rsurf);
963 _eglPutContext(ctx);
964
965 _eglPutSurface(old_dsurf);
966 _eglPutSurface(old_rsurf);
967 _eglPutContext(old_ctx);
968
969 return EGL_FALSE;
970 }
971 }
972
973 /*
974 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
975 */
976 static _EGLProc
977 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
978 {
979 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
980
981 return dri2_drv->get_proc_address(procname);
982 }
983
984 static EGLBoolean
985 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
986 {
987 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
988 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
989
990 (void) drv;
991
992 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
993 * we need to copy fake to real here.*/
994
995 if (dri2_dpy->flush != NULL)
996 dri2_dpy->flush->flush(dri2_surf->dri_drawable);
997
998 return EGL_TRUE;
999 }
1000
1001 static EGLBoolean
1002 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
1003 {
1004 (void) drv;
1005 (void) disp;
1006
1007 if (engine != EGL_CORE_NATIVE_ENGINE)
1008 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1009 /* glXWaitX(); */
1010
1011 return EGL_TRUE;
1012 }
1013
1014 static EGLBoolean
1015 dri2_bind_tex_image(_EGLDriver *drv,
1016 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1017 {
1018 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1019 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1020 struct dri2_egl_context *dri2_ctx;
1021 _EGLContext *ctx;
1022 GLint format, target;
1023
1024 ctx = _eglGetCurrentContext();
1025 dri2_ctx = dri2_egl_context(ctx);
1026
1027 if (!_eglBindTexImage(drv, disp, surf, buffer))
1028 return EGL_FALSE;
1029
1030 switch (dri2_surf->base.TextureFormat) {
1031 case EGL_TEXTURE_RGB:
1032 format = __DRI_TEXTURE_FORMAT_RGB;
1033 break;
1034 case EGL_TEXTURE_RGBA:
1035 format = __DRI_TEXTURE_FORMAT_RGBA;
1036 break;
1037 default:
1038 assert(0);
1039 }
1040
1041 switch (dri2_surf->base.TextureTarget) {
1042 case EGL_TEXTURE_2D:
1043 target = GL_TEXTURE_2D;
1044 break;
1045 default:
1046 assert(0);
1047 }
1048
1049 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1050 target, format,
1051 dri2_surf->dri_drawable);
1052
1053 return EGL_TRUE;
1054 }
1055
1056 static EGLBoolean
1057 dri2_release_tex_image(_EGLDriver *drv,
1058 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1059 {
1060 #if __DRI_TEX_BUFFER_VERSION >= 3
1061 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1062 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1063 struct dri2_egl_context *dri2_ctx;
1064 _EGLContext *ctx;
1065 GLint target;
1066
1067 ctx = _eglGetCurrentContext();
1068 dri2_ctx = dri2_egl_context(ctx);
1069
1070 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
1071 return EGL_FALSE;
1072
1073 switch (dri2_surf->base.TextureTarget) {
1074 case EGL_TEXTURE_2D:
1075 target = GL_TEXTURE_2D;
1076 break;
1077 default:
1078 assert(0);
1079 }
1080 if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL)
1081 (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context,
1082 target,
1083 dri2_surf->dri_drawable);
1084 #endif
1085
1086 return EGL_TRUE;
1087 }
1088
1089 static _EGLImage *
1090 dri2_create_image(_EGLDisplay *disp, __DRIimage *dri_image)
1091 {
1092 struct dri2_egl_image *dri2_img;
1093
1094 if (dri_image == NULL) {
1095 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1096 return NULL;
1097 }
1098
1099 dri2_img = malloc(sizeof *dri2_img);
1100 if (!dri2_img) {
1101 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1102 return NULL;
1103 }
1104
1105 if (!_eglInitImage(&dri2_img->base, disp)) {
1106 free(dri2_img);
1107 return NULL;
1108 }
1109
1110 dri2_img->dri_image = dri_image;
1111
1112 return &dri2_img->base;
1113 }
1114
1115 static _EGLImage *
1116 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1117 EGLClientBuffer buffer,
1118 const EGLint *attr_list)
1119 {
1120 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1121 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1122 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
1123 __DRIimage *dri_image;
1124
1125 if (renderbuffer == 0) {
1126 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1127 return EGL_NO_IMAGE_KHR;
1128 }
1129
1130 dri_image =
1131 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1132 renderbuffer, NULL);
1133
1134 return dri2_create_image(disp, dri_image);
1135 }
1136
1137 static _EGLImage *
1138 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1139 EGLClientBuffer buffer, const EGLint *attr_list)
1140 {
1141 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1142 EGLint format, name, pitch, err;
1143 _EGLImageAttribs attrs;
1144 __DRIimage *dri_image;
1145
1146 name = (EGLint) (uintptr_t) buffer;
1147
1148 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1149 if (err != EGL_SUCCESS)
1150 return NULL;
1151
1152 if (attrs.Width <= 0 || attrs.Height <= 0 ||
1153 attrs.DRMBufferStrideMESA <= 0) {
1154 _eglError(EGL_BAD_PARAMETER,
1155 "bad width, height or stride");
1156 return NULL;
1157 }
1158
1159 switch (attrs.DRMBufferFormatMESA) {
1160 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1161 format = __DRI_IMAGE_FORMAT_ARGB8888;
1162 pitch = attrs.DRMBufferStrideMESA;
1163 break;
1164 default:
1165 _eglError(EGL_BAD_PARAMETER,
1166 "dri2_create_image_khr: unsupported pixmap depth");
1167 return NULL;
1168 }
1169
1170 dri_image =
1171 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1172 attrs.Width,
1173 attrs.Height,
1174 format,
1175 name,
1176 pitch,
1177 NULL);
1178
1179 return dri2_create_image(disp, dri_image);
1180 }
1181
1182 #ifdef HAVE_WAYLAND_PLATFORM
1183
1184 /* This structure describes how a wl_buffer maps to one or more
1185 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1186 * offsets and strides of the planes in the buffer. This table maps a
1187 * wl_drm format code to a description of the planes in the buffer
1188 * that lets us create a __DRIimage for each of the planes. */
1189
1190 static const struct wl_drm_components_descriptor {
1191 uint32_t dri_components;
1192 EGLint components;
1193 int nplanes;
1194 } wl_drm_components[] = {
1195 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
1196 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
1197 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
1198 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
1199 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
1200 };
1201
1202 static _EGLImage *
1203 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1204 EGLClientBuffer _buffer,
1205 const EGLint *attr_list)
1206 {
1207 struct wl_drm_buffer *buffer;
1208 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1209 const struct wl_drm_components_descriptor *f;
1210 __DRIimage *dri_image;
1211 _EGLImageAttribs attrs;
1212 EGLint err;
1213 int32_t plane;
1214
1215 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
1216 (struct wl_resource *) _buffer);
1217 if (!buffer)
1218 return NULL;
1219
1220 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1221 plane = attrs.PlaneWL;
1222 if (err != EGL_SUCCESS) {
1223 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1224 return NULL;
1225 }
1226
1227 f = buffer->driver_format;
1228 if (plane < 0 || plane >= f->nplanes) {
1229 _eglError(EGL_BAD_PARAMETER,
1230 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1231 return NULL;
1232 }
1233
1234 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
1235
1236 if (dri_image == NULL) {
1237 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1238 return NULL;
1239 }
1240
1241 return dri2_create_image(disp, dri_image);
1242 }
1243 #endif
1244
1245 /**
1246 * Set the error code after a call to
1247 * dri2_egl_image::dri_image::createImageFromTexture.
1248 */
1249 static void
1250 dri2_create_image_khr_texture_error(int dri_error)
1251 {
1252 EGLint egl_error;
1253
1254 switch (dri_error) {
1255 case __DRI_IMAGE_ERROR_SUCCESS:
1256 return;
1257
1258 case __DRI_IMAGE_ERROR_BAD_ALLOC:
1259 egl_error = EGL_BAD_ALLOC;
1260 break;
1261
1262 case __DRI_IMAGE_ERROR_BAD_MATCH:
1263 egl_error = EGL_BAD_MATCH;
1264 break;
1265
1266 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
1267 egl_error = EGL_BAD_PARAMETER;
1268 break;
1269
1270 default:
1271 assert(0);
1272 egl_error = EGL_BAD_MATCH;
1273 break;
1274 }
1275
1276 _eglError(egl_error, "dri2_create_image_khr_texture");
1277 }
1278
1279 static _EGLImage *
1280 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
1281 EGLenum target,
1282 EGLClientBuffer buffer,
1283 const EGLint *attr_list)
1284 {
1285 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1286 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1287 struct dri2_egl_image *dri2_img;
1288 GLuint texture = (GLuint) (uintptr_t) buffer;
1289 _EGLImageAttribs attrs;
1290 GLuint depth;
1291 GLenum gl_target;
1292 unsigned error;
1293
1294 if (texture == 0) {
1295 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1296 return EGL_NO_IMAGE_KHR;
1297 }
1298
1299 if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS)
1300 return EGL_NO_IMAGE_KHR;
1301
1302 switch (target) {
1303 case EGL_GL_TEXTURE_2D_KHR:
1304 depth = 0;
1305 gl_target = GL_TEXTURE_2D;
1306 break;
1307 case EGL_GL_TEXTURE_3D_KHR:
1308 depth = attrs.GLTextureZOffset;
1309 gl_target = GL_TEXTURE_3D;
1310 break;
1311 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
1312 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
1313 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
1314 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
1315 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
1316 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
1317 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
1318 gl_target = GL_TEXTURE_CUBE_MAP;
1319 break;
1320 default:
1321 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1322 return EGL_NO_IMAGE_KHR;
1323 }
1324
1325 dri2_img = malloc(sizeof *dri2_img);
1326 if (!dri2_img) {
1327 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1328 return EGL_NO_IMAGE_KHR;
1329 }
1330
1331 if (!_eglInitImage(&dri2_img->base, disp)) {
1332 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1333 free(dri2_img);
1334 return EGL_NO_IMAGE_KHR;
1335 }
1336
1337 dri2_img->dri_image =
1338 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
1339 gl_target,
1340 texture,
1341 depth,
1342 attrs.GLTextureLevel,
1343 &error,
1344 dri2_img);
1345 dri2_create_image_khr_texture_error(error);
1346
1347 if (!dri2_img->dri_image) {
1348 free(dri2_img);
1349 return EGL_NO_IMAGE_KHR;
1350 }
1351 return &dri2_img->base;
1352 }
1353
1354 static EGLBoolean
1355 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
1356 {
1357 unsigned i;
1358
1359 /**
1360 * The spec says:
1361 *
1362 * "Required attributes and their values are as follows:
1363 *
1364 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1365 *
1366 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1367 * by drm_fourcc.h and used as the pixel_format parameter of the
1368 * drm_mode_fb_cmd2 ioctl."
1369 *
1370 * and
1371 *
1372 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1373 * incomplete, EGL_BAD_PARAMETER is generated."
1374 */
1375 if (attrs->Width <= 0 || attrs->Height <= 0 ||
1376 !attrs->DMABufFourCC.IsPresent) {
1377 _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
1378 return EGL_FALSE;
1379 }
1380
1381 /**
1382 * Also:
1383 *
1384 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1385 * specified for a plane's pitch or offset isn't supported by EGL,
1386 * EGL_BAD_ACCESS is generated."
1387 */
1388 for (i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
1389 if (attrs->DMABufPlanePitches[i].IsPresent &&
1390 attrs->DMABufPlanePitches[i].Value <= 0) {
1391 _eglError(EGL_BAD_ACCESS, "invalid pitch");
1392 return EGL_FALSE;
1393 }
1394 }
1395
1396 return EGL_TRUE;
1397 }
1398
1399 /* Returns the total number of file descriptors. Zero indicates an error. */
1400 static unsigned
1401 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
1402 {
1403 unsigned i, plane_n;
1404
1405 switch (attrs->DMABufFourCC.Value) {
1406 case DRM_FORMAT_RGB332:
1407 case DRM_FORMAT_BGR233:
1408 case DRM_FORMAT_XRGB4444:
1409 case DRM_FORMAT_XBGR4444:
1410 case DRM_FORMAT_RGBX4444:
1411 case DRM_FORMAT_BGRX4444:
1412 case DRM_FORMAT_ARGB4444:
1413 case DRM_FORMAT_ABGR4444:
1414 case DRM_FORMAT_RGBA4444:
1415 case DRM_FORMAT_BGRA4444:
1416 case DRM_FORMAT_XRGB1555:
1417 case DRM_FORMAT_XBGR1555:
1418 case DRM_FORMAT_RGBX5551:
1419 case DRM_FORMAT_BGRX5551:
1420 case DRM_FORMAT_ARGB1555:
1421 case DRM_FORMAT_ABGR1555:
1422 case DRM_FORMAT_RGBA5551:
1423 case DRM_FORMAT_BGRA5551:
1424 case DRM_FORMAT_RGB565:
1425 case DRM_FORMAT_BGR565:
1426 case DRM_FORMAT_RGB888:
1427 case DRM_FORMAT_BGR888:
1428 case DRM_FORMAT_XRGB8888:
1429 case DRM_FORMAT_XBGR8888:
1430 case DRM_FORMAT_RGBX8888:
1431 case DRM_FORMAT_BGRX8888:
1432 case DRM_FORMAT_ARGB8888:
1433 case DRM_FORMAT_ABGR8888:
1434 case DRM_FORMAT_RGBA8888:
1435 case DRM_FORMAT_BGRA8888:
1436 case DRM_FORMAT_XRGB2101010:
1437 case DRM_FORMAT_XBGR2101010:
1438 case DRM_FORMAT_RGBX1010102:
1439 case DRM_FORMAT_BGRX1010102:
1440 case DRM_FORMAT_ARGB2101010:
1441 case DRM_FORMAT_ABGR2101010:
1442 case DRM_FORMAT_RGBA1010102:
1443 case DRM_FORMAT_BGRA1010102:
1444 case DRM_FORMAT_YUYV:
1445 case DRM_FORMAT_YVYU:
1446 case DRM_FORMAT_UYVY:
1447 case DRM_FORMAT_VYUY:
1448 plane_n = 1;
1449 break;
1450 case DRM_FORMAT_NV12:
1451 case DRM_FORMAT_NV21:
1452 case DRM_FORMAT_NV16:
1453 case DRM_FORMAT_NV61:
1454 plane_n = 2;
1455 break;
1456 case DRM_FORMAT_YUV410:
1457 case DRM_FORMAT_YVU410:
1458 case DRM_FORMAT_YUV411:
1459 case DRM_FORMAT_YVU411:
1460 case DRM_FORMAT_YUV420:
1461 case DRM_FORMAT_YVU420:
1462 case DRM_FORMAT_YUV422:
1463 case DRM_FORMAT_YVU422:
1464 case DRM_FORMAT_YUV444:
1465 case DRM_FORMAT_YVU444:
1466 plane_n = 3;
1467 break;
1468 default:
1469 _eglError(EGL_BAD_ATTRIBUTE, "invalid format");
1470 return 0;
1471 }
1472
1473 /**
1474 * The spec says:
1475 *
1476 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1477 * incomplete, EGL_BAD_PARAMETER is generated."
1478 */
1479 for (i = 0; i < plane_n; ++i) {
1480 if (!attrs->DMABufPlaneFds[i].IsPresent ||
1481 !attrs->DMABufPlaneOffsets[i].IsPresent ||
1482 !attrs->DMABufPlanePitches[i].IsPresent) {
1483 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
1484 return 0;
1485 }
1486 }
1487
1488 /**
1489 * The spec also says:
1490 *
1491 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
1492 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
1493 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
1494 * attributes are specified."
1495 */
1496 for (i = plane_n; i < 3; ++i) {
1497 if (attrs->DMABufPlaneFds[i].IsPresent ||
1498 attrs->DMABufPlaneOffsets[i].IsPresent ||
1499 attrs->DMABufPlanePitches[i].IsPresent) {
1500 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
1501 return 0;
1502 }
1503 }
1504
1505 return plane_n;
1506 }
1507
1508 /**
1509 * The spec says:
1510 *
1511 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target,
1512 * the EGL takes ownership of the file descriptor and is responsible for
1513 * closing it, which it may do at any time while the EGLDisplay is
1514 * initialized."
1515 */
1516 static void
1517 dri2_take_dma_buf_ownership(const int *fds, unsigned num_fds)
1518 {
1519 int already_closed[num_fds];
1520 unsigned num_closed = 0;
1521 unsigned i, j;
1522
1523 for (i = 0; i < num_fds; ++i) {
1524 /**
1525 * The same file descriptor can be referenced multiple times in case more
1526 * than one plane is found in the same buffer, just with a different
1527 * offset.
1528 */
1529 for (j = 0; j < num_closed; ++j) {
1530 if (already_closed[j] == fds[i])
1531 break;
1532 }
1533
1534 if (j == num_closed) {
1535 close(fds[i]);
1536 already_closed[num_closed++] = fds[i];
1537 }
1538 }
1539 }
1540
1541 static _EGLImage *
1542 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
1543 EGLClientBuffer buffer, const EGLint *attr_list)
1544 {
1545 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1546 _EGLImage *res;
1547 EGLint err;
1548 _EGLImageAttribs attrs;
1549 __DRIimage *dri_image;
1550 unsigned num_fds;
1551 unsigned i;
1552 int fds[3];
1553 int pitches[3];
1554 int offsets[3];
1555 unsigned error;
1556
1557 /**
1558 * The spec says:
1559 *
1560 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
1561 * error EGL_BAD_PARAMETER is generated."
1562 */
1563 if (buffer != NULL) {
1564 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
1565 return NULL;
1566 }
1567
1568 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1569 if (err != EGL_SUCCESS) {
1570 _eglError(err, "bad attribute");
1571 return NULL;
1572 }
1573
1574 if (!dri2_check_dma_buf_attribs(&attrs))
1575 return NULL;
1576
1577 num_fds = dri2_check_dma_buf_format(&attrs);
1578 if (!num_fds)
1579 return NULL;
1580
1581 for (i = 0; i < num_fds; ++i) {
1582 fds[i] = attrs.DMABufPlaneFds[i].Value;
1583 pitches[i] = attrs.DMABufPlanePitches[i].Value;
1584 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
1585 }
1586
1587 dri_image =
1588 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
1589 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
1590 fds, num_fds, pitches, offsets,
1591 attrs.DMABufYuvColorSpaceHint.Value,
1592 attrs.DMABufSampleRangeHint.Value,
1593 attrs.DMABufChromaHorizontalSiting.Value,
1594 attrs.DMABufChromaVerticalSiting.Value,
1595 &error,
1596 NULL);
1597 dri2_create_image_khr_texture_error(error);
1598
1599 if (!dri_image)
1600 return EGL_NO_IMAGE_KHR;
1601
1602 res = dri2_create_image(disp, dri_image);
1603 if (res)
1604 dri2_take_dma_buf_ownership(fds, num_fds);
1605
1606 return res;
1607 }
1608
1609 _EGLImage *
1610 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1611 _EGLContext *ctx, EGLenum target,
1612 EGLClientBuffer buffer, const EGLint *attr_list)
1613 {
1614 (void) drv;
1615
1616 switch (target) {
1617 case EGL_GL_TEXTURE_2D_KHR:
1618 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
1619 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
1620 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
1621 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
1622 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
1623 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
1624 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
1625 case EGL_GL_RENDERBUFFER_KHR:
1626 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1627 case EGL_DRM_BUFFER_MESA:
1628 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
1629 #ifdef HAVE_WAYLAND_PLATFORM
1630 case EGL_WAYLAND_BUFFER_WL:
1631 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
1632 #endif
1633 case EGL_LINUX_DMA_BUF_EXT:
1634 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
1635 default:
1636 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1637 return EGL_NO_IMAGE_KHR;
1638 }
1639 }
1640
1641 static EGLBoolean
1642 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1643 {
1644 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1645 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1646
1647 (void) drv;
1648
1649 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1650 free(dri2_img);
1651
1652 return EGL_TRUE;
1653 }
1654
1655 static _EGLImage *
1656 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
1657 const EGLint *attr_list)
1658 {
1659 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1660 struct dri2_egl_image *dri2_img;
1661 _EGLImageAttribs attrs;
1662 unsigned int dri_use, valid_mask;
1663 int format;
1664 EGLint err = EGL_SUCCESS;
1665
1666 (void) drv;
1667
1668 dri2_img = malloc(sizeof *dri2_img);
1669 if (!dri2_img) {
1670 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1671 return EGL_NO_IMAGE_KHR;
1672 }
1673
1674 if (!attr_list) {
1675 err = EGL_BAD_PARAMETER;
1676 goto cleanup_img;
1677 }
1678
1679 if (!_eglInitImage(&dri2_img->base, disp)) {
1680 err = EGL_BAD_PARAMETER;
1681 goto cleanup_img;
1682 }
1683
1684 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1685 if (err != EGL_SUCCESS)
1686 goto cleanup_img;
1687
1688 if (attrs.Width <= 0 || attrs.Height <= 0) {
1689 _eglLog(_EGL_WARNING, "bad width or height (%dx%d)",
1690 attrs.Width, attrs.Height);
1691 goto cleanup_img;
1692 }
1693
1694 switch (attrs.DRMBufferFormatMESA) {
1695 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1696 format = __DRI_IMAGE_FORMAT_ARGB8888;
1697 break;
1698 default:
1699 _eglLog(_EGL_WARNING, "bad image format value 0x%04x",
1700 attrs.DRMBufferFormatMESA);
1701 goto cleanup_img;
1702 }
1703
1704 valid_mask =
1705 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
1706 EGL_DRM_BUFFER_USE_SHARE_MESA |
1707 EGL_DRM_BUFFER_USE_CURSOR_MESA;
1708 if (attrs.DRMBufferUseMESA & ~valid_mask) {
1709 _eglLog(_EGL_WARNING, "bad image use bit 0x%04x",
1710 attrs.DRMBufferUseMESA & ~valid_mask);
1711 goto cleanup_img;
1712 }
1713
1714 dri_use = 0;
1715 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
1716 dri_use |= __DRI_IMAGE_USE_SHARE;
1717 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
1718 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1719 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
1720 dri_use |= __DRI_IMAGE_USE_CURSOR;
1721
1722 dri2_img->dri_image =
1723 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
1724 attrs.Width, attrs.Height,
1725 format, dri_use, dri2_img);
1726 if (dri2_img->dri_image == NULL) {
1727 err = EGL_BAD_ALLOC;
1728 goto cleanup_img;
1729 }
1730
1731 return &dri2_img->base;
1732
1733 cleanup_img:
1734 free(dri2_img);
1735 _eglError(err, "dri2_create_drm_image_mesa");
1736
1737 return EGL_NO_IMAGE_KHR;
1738 }
1739
1740 static EGLBoolean
1741 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
1742 EGLint *name, EGLint *handle, EGLint *stride)
1743 {
1744 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1745 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1746
1747 (void) drv;
1748
1749 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
1750 __DRI_IMAGE_ATTRIB_NAME, name)) {
1751 _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
1752 return EGL_FALSE;
1753 }
1754
1755 if (handle)
1756 dri2_dpy->image->queryImage(dri2_img->dri_image,
1757 __DRI_IMAGE_ATTRIB_HANDLE, handle);
1758
1759 if (stride)
1760 dri2_dpy->image->queryImage(dri2_img->dri_image,
1761 __DRI_IMAGE_ATTRIB_STRIDE, stride);
1762
1763 return EGL_TRUE;
1764 }
1765
1766 #ifdef HAVE_WAYLAND_PLATFORM
1767
1768 static void
1769 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
1770 struct wl_drm_buffer *buffer)
1771 {
1772 _EGLDisplay *disp = user_data;
1773 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1774 __DRIimage *img;
1775 int i, dri_components = 0;
1776
1777 if (fd == -1)
1778 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
1779 buffer->width,
1780 buffer->height,
1781 buffer->format,
1782 (int*)&name, 1,
1783 buffer->stride,
1784 buffer->offset,
1785 NULL);
1786 else
1787 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
1788 buffer->width,
1789 buffer->height,
1790 buffer->format,
1791 &fd, 1,
1792 buffer->stride,
1793 buffer->offset,
1794 NULL);
1795
1796 if (img == NULL)
1797 return;
1798
1799 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
1800
1801 buffer->driver_format = NULL;
1802 for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
1803 if (wl_drm_components[i].dri_components == dri_components)
1804 buffer->driver_format = &wl_drm_components[i];
1805
1806 if (buffer->driver_format == NULL)
1807 dri2_dpy->image->destroyImage(img);
1808 else
1809 buffer->driver_buffer = img;
1810 }
1811
1812 static void
1813 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
1814 {
1815 _EGLDisplay *disp = user_data;
1816 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1817
1818 dri2_dpy->image->destroyImage(buffer->driver_buffer);
1819 }
1820
1821 static struct wayland_drm_callbacks wl_drm_callbacks = {
1822 .authenticate = NULL,
1823 .reference_buffer = dri2_wl_reference_buffer,
1824 .release_buffer = dri2_wl_release_buffer
1825 };
1826
1827 static EGLBoolean
1828 dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
1829 struct wl_display *wl_dpy)
1830 {
1831 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1832 int ret, flags = 0;
1833 uint64_t cap;
1834
1835 (void) drv;
1836
1837 if (dri2_dpy->wl_server_drm)
1838 return EGL_FALSE;
1839
1840 wl_drm_callbacks.authenticate =
1841 (int(*)(void *, uint32_t)) dri2_dpy->authenticate;
1842
1843 ret = drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap);
1844 if (ret == 0 && cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
1845 dri2_dpy->image->base.version >= 7 &&
1846 dri2_dpy->image->createImageFromFds != NULL)
1847 flags |= WAYLAND_DRM_PRIME;
1848
1849 dri2_dpy->wl_server_drm =
1850 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
1851 &wl_drm_callbacks, disp, flags);
1852
1853 if (!dri2_dpy->wl_server_drm)
1854 return EGL_FALSE;
1855
1856 /* We have to share the wl_drm instance with gbm, so gbm can convert
1857 * wl_buffers to gbm bos. */
1858 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
1859
1860 return EGL_TRUE;
1861 }
1862
1863 static EGLBoolean
1864 dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
1865 struct wl_display *wl_dpy)
1866 {
1867 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1868
1869 (void) drv;
1870
1871 if (!dri2_dpy->wl_server_drm)
1872 return EGL_FALSE;
1873
1874 wayland_drm_uninit(dri2_dpy->wl_server_drm);
1875 dri2_dpy->wl_server_drm = NULL;
1876
1877 return EGL_TRUE;
1878 }
1879
1880 static EGLBoolean
1881 dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
1882 struct wl_resource *buffer_resource,
1883 EGLint attribute, EGLint *value)
1884 {
1885 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1886 struct wl_drm_buffer *buffer;
1887 const struct wl_drm_components_descriptor *format;
1888
1889 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
1890 if (!buffer)
1891 return EGL_FALSE;
1892
1893 format = buffer->driver_format;
1894 switch (attribute) {
1895 case EGL_TEXTURE_FORMAT:
1896 *value = format->components;
1897 return EGL_TRUE;
1898 case EGL_WIDTH:
1899 *value = buffer->width;
1900 return EGL_TRUE;
1901 case EGL_HEIGHT:
1902 *value = buffer->height;
1903 return EGL_TRUE;
1904 }
1905
1906 return EGL_FALSE;
1907 }
1908 #endif
1909
1910 static void
1911 dri2_unload(_EGLDriver *drv)
1912 {
1913 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1914
1915 if (dri2_drv->handle)
1916 dlclose(dri2_drv->handle);
1917 free(dri2_drv);
1918 }
1919
1920 static EGLBoolean
1921 dri2_load(_EGLDriver *drv)
1922 {
1923 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1924 #ifdef HAVE_SHARED_GLAPI
1925 #ifdef HAVE_ANDROID_PLATFORM
1926 const char *libname = "libglapi.so";
1927 #else
1928 const char *libname = "libglapi.so.0";
1929 #endif
1930 #else
1931 /*
1932 * Both libGL.so and libglapi.so are glapi providers. There is no way to
1933 * tell which one to load.
1934 */
1935 const char *libname = NULL;
1936 #endif
1937 void *handle;
1938
1939 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
1940 handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
1941 if (handle) {
1942 dri2_drv->get_proc_address = (_EGLProc (*)(const char *))
1943 dlsym(handle, "_glapi_get_proc_address");
1944 if (!dri2_drv->get_proc_address || !libname) {
1945 /* no need to keep a reference */
1946 dlclose(handle);
1947 handle = NULL;
1948 }
1949 }
1950
1951 /* if glapi is not available, loading DRI drivers will fail */
1952 if (!dri2_drv->get_proc_address) {
1953 _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address");
1954 return EGL_FALSE;
1955 }
1956
1957 dri2_drv->glFlush = (void (*)(void))
1958 dri2_drv->get_proc_address("glFlush");
1959
1960 dri2_drv->handle = handle;
1961
1962 return EGL_TRUE;
1963 }
1964
1965 /**
1966 * This is the main entrypoint into the driver, called by libEGL.
1967 * Create a new _EGLDriver object and init its dispatch table.
1968 */
1969 _EGLDriver *
1970 _eglBuiltInDriverDRI2(const char *args)
1971 {
1972 struct dri2_egl_driver *dri2_drv;
1973
1974 (void) args;
1975
1976 dri2_drv = calloc(1, sizeof *dri2_drv);
1977 if (!dri2_drv)
1978 return NULL;
1979
1980 if (!dri2_load(&dri2_drv->base)) {
1981 free(dri2_drv);
1982 return NULL;
1983 }
1984
1985 _eglInitDriverFallbacks(&dri2_drv->base);
1986 dri2_drv->base.API.Initialize = dri2_initialize;
1987 dri2_drv->base.API.Terminate = dri2_terminate;
1988 dri2_drv->base.API.CreateContext = dri2_create_context;
1989 dri2_drv->base.API.DestroyContext = dri2_destroy_context;
1990 dri2_drv->base.API.MakeCurrent = dri2_make_current;
1991 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
1992 dri2_drv->base.API.WaitClient = dri2_wait_client;
1993 dri2_drv->base.API.WaitNative = dri2_wait_native;
1994 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
1995 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
1996 dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
1997 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
1998 dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
1999 dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
2000 #ifdef HAVE_WAYLAND_PLATFORM
2001 dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
2002 dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
2003 dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
2004 #endif
2005
2006 dri2_drv->base.Name = "DRI2";
2007 dri2_drv->base.Unload = dri2_unload;
2008
2009 return &dri2_drv->base;
2010 }