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