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