egl/dri2: Add plumbing for EGL_OPENGL_ES3_BIT_KHR
[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 <GL/gl.h>
38 #include <GL/internal/dri_interface.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "egl_dri2.h"
43
44 const __DRIuseInvalidateExtension use_invalidate = {
45 { __DRI_USE_INVALIDATE, 1 }
46 };
47
48 EGLint dri2_to_egl_attribute_map[] = {
49 0,
50 EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
51 EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
52 EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
53 EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
54 EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
55 EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */
56 EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
57 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
58 EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
59 EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
60 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
61 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
62 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
63 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
64 EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
65 EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
66 0, /* __DRI_ATTRIB_RENDER_TYPE, */
67 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
68 0, /* __DRI_ATTRIB_CONFORMANT */
69 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
70 0, /* __DRI_ATTRIB_STEREO */
71 0, /* __DRI_ATTRIB_AUX_BUFFERS */
72 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
73 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
74 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
75 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
76 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
77 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
78 0, /* __DRI_ATTRIB_FLOAT_MODE */
79 0, /* __DRI_ATTRIB_RED_MASK */
80 0, /* __DRI_ATTRIB_GREEN_MASK */
81 0, /* __DRI_ATTRIB_BLUE_MASK */
82 0, /* __DRI_ATTRIB_ALPHA_MASK */
83 EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
84 EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
85 EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
86 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
87 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
88 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
89 0, /* __DRI_ATTRIB_SWAP_METHOD */
90 EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
91 EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
92 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
93 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
94 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
95 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
96 EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */
97 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
98 };
99
100 static EGLBoolean
101 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
102 {
103 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
104 return EGL_FALSE;
105
106 if (!_eglMatchConfig(conf, criteria))
107 return EGL_FALSE;
108
109 return EGL_TRUE;
110 }
111
112 struct dri2_egl_config *
113 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
114 int depth, EGLint surface_type, const EGLint *attr_list,
115 const unsigned int *rgba_masks)
116 {
117 struct dri2_egl_config *conf;
118 struct dri2_egl_display *dri2_dpy;
119 _EGLConfig base;
120 unsigned int attrib, value, double_buffer;
121 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
122 unsigned int dri_masks[4] = { 0, 0, 0, 0 };
123 _EGLConfig *matching_config;
124 EGLint num_configs = 0;
125 EGLint config_id;
126 int i;
127
128 dri2_dpy = disp->DriverData;
129 _eglInitConfig(&base, disp, id);
130
131 i = 0;
132 double_buffer = 0;
133 bind_to_texture_rgb = 0;
134 bind_to_texture_rgba = 0;
135
136 while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
137 switch (attrib) {
138 case __DRI_ATTRIB_RENDER_TYPE:
139 if (value & __DRI_ATTRIB_RGBA_BIT)
140 value = EGL_RGB_BUFFER;
141 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
142 value = EGL_LUMINANCE_BUFFER;
143 else
144 /* not valid */;
145 _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value);
146 break;
147
148 case __DRI_ATTRIB_CONFIG_CAVEAT:
149 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
150 value = EGL_NON_CONFORMANT_CONFIG;
151 else if (value & __DRI_ATTRIB_SLOW_BIT)
152 value = EGL_SLOW_CONFIG;
153 else
154 value = EGL_NONE;
155 _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value);
156 break;
157
158 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
159 bind_to_texture_rgb = value;
160 break;
161
162 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
163 bind_to_texture_rgba = value;
164 break;
165
166 case __DRI_ATTRIB_DOUBLE_BUFFER:
167 double_buffer = value;
168 break;
169
170 case __DRI_ATTRIB_RED_MASK:
171 dri_masks[0] = value;
172 break;
173
174 case __DRI_ATTRIB_GREEN_MASK:
175 dri_masks[1] = value;
176 break;
177
178 case __DRI_ATTRIB_BLUE_MASK:
179 dri_masks[2] = value;
180 break;
181
182 case __DRI_ATTRIB_ALPHA_MASK:
183 dri_masks[3] = value;
184 break;
185
186 default:
187 key = dri2_to_egl_attribute_map[attrib];
188 if (key != 0)
189 _eglSetConfigKey(&base, key, value);
190 break;
191 }
192 }
193
194 if (attr_list)
195 for (i = 0; attr_list[i] != EGL_NONE; i += 2)
196 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
197
198 if (depth > 0 && depth != base.BufferSize)
199 return NULL;
200
201 if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks)))
202 return NULL;
203
204 base.NativeRenderable = EGL_TRUE;
205
206 base.SurfaceType = surface_type;
207 if (surface_type & (EGL_PBUFFER_BIT |
208 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
209 base.BindToTextureRGB = bind_to_texture_rgb;
210 if (base.AlphaSize > 0)
211 base.BindToTextureRGBA = bind_to_texture_rgba;
212 }
213
214 base.RenderableType = disp->ClientAPIs;
215 base.Conformant = disp->ClientAPIs;
216
217 if (!_eglValidateConfig(&base, EGL_FALSE)) {
218 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
219 return NULL;
220 }
221
222 config_id = base.ConfigID;
223 base.ConfigID = EGL_DONT_CARE;
224 base.SurfaceType = EGL_DONT_CARE;
225 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
226 (_EGLArrayForEach) dri2_match_config, &base);
227
228 if (num_configs == 1) {
229 conf = (struct dri2_egl_config *) matching_config;
230
231 if (double_buffer && !conf->dri_double_config)
232 conf->dri_double_config = dri_config;
233 else if (!double_buffer && !conf->dri_single_config)
234 conf->dri_single_config = dri_config;
235 else
236 /* a similar config type is already added (unlikely) => discard */
237 return NULL;
238 }
239 else if (num_configs == 0) {
240 conf = malloc(sizeof *conf);
241 if (conf == NULL)
242 return NULL;
243
244 memcpy(&conf->base, &base, sizeof base);
245 if (double_buffer) {
246 conf->dri_double_config = dri_config;
247 conf->dri_single_config = NULL;
248 } else {
249 conf->dri_single_config = dri_config;
250 conf->dri_double_config = NULL;
251 }
252 conf->base.SurfaceType = 0;
253 conf->base.ConfigID = config_id;
254
255 _eglLinkConfig(&conf->base);
256 }
257 else {
258 assert(0);
259 return NULL;
260 }
261
262 if (double_buffer) {
263 surface_type &= ~EGL_PIXMAP_BIT;
264
265 conf->base.MinSwapInterval = dri2_dpy->min_swap_interval;
266 conf->base.MaxSwapInterval = dri2_dpy->max_swap_interval;
267 }
268
269 conf->base.SurfaceType |= surface_type;
270
271 return conf;
272 }
273
274 __DRIimage *
275 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
276 {
277 _EGLDisplay *disp = data;
278 struct dri2_egl_image *dri2_img;
279 _EGLImage *img;
280
281 (void) screen;
282
283 img = _eglLookupImage(image, disp);
284 if (img == NULL) {
285 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
286 return NULL;
287 }
288
289 dri2_img = dri2_egl_image(image);
290
291 return dri2_img->dri_image;
292 }
293
294 const __DRIimageLookupExtension image_lookup_extension = {
295 { __DRI_IMAGE_LOOKUP, 1 },
296 dri2_lookup_egl_image
297 };
298
299 static const char dri_driver_path[] = DEFAULT_DRIVER_DIR;
300
301 struct dri2_extension_match {
302 const char *name;
303 int version;
304 int offset;
305 };
306
307 static struct dri2_extension_match dri2_driver_extensions[] = {
308 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
309 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
310 { NULL, 0, 0 }
311 };
312
313 static struct dri2_extension_match dri2_core_extensions[] = {
314 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
315 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
316 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
317 { NULL, 0, 0 }
318 };
319
320 static struct dri2_extension_match swrast_driver_extensions[] = {
321 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
322 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
323 { NULL, 0, 0 }
324 };
325
326 static struct dri2_extension_match swrast_core_extensions[] = {
327 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
328 { NULL, 0, 0 }
329 };
330
331 static EGLBoolean
332 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
333 struct dri2_extension_match *matches,
334 const __DRIextension **extensions)
335 {
336 int i, j, ret = EGL_TRUE;
337 void *field;
338
339 for (i = 0; extensions[i]; i++) {
340 _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name);
341 for (j = 0; matches[j].name; j++) {
342 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
343 extensions[i]->version >= matches[j].version) {
344 field = ((char *) dri2_dpy + matches[j].offset);
345 *(const __DRIextension **) field = extensions[i];
346 _eglLog(_EGL_INFO, "DRI2: found extension %s version %d",
347 extensions[i]->name, extensions[i]->version);
348 }
349 }
350 }
351
352 for (j = 0; matches[j].name; j++) {
353 field = ((char *) dri2_dpy + matches[j].offset);
354 if (*(const __DRIextension **) field == NULL) {
355 _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d",
356 matches[j].name, matches[j].version);
357 ret = EGL_FALSE;
358 }
359 }
360
361 return ret;
362 }
363
364 static const __DRIextension **
365 dri2_open_driver(_EGLDisplay *disp)
366 {
367 struct dri2_egl_display *dri2_dpy = disp->DriverData;
368 const __DRIextension **extensions;
369 char path[PATH_MAX], *search_paths, *p, *next, *end;
370
371 search_paths = NULL;
372 if (geteuid() == getuid()) {
373 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
374 search_paths = getenv("LIBGL_DRIVERS_PATH");
375 }
376 if (search_paths == NULL)
377 search_paths = DEFAULT_DRIVER_DIR;
378
379 dri2_dpy->driver = NULL;
380 end = search_paths + strlen(search_paths);
381 for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) {
382 int len;
383 next = strchr(p, ':');
384 if (next == NULL)
385 next = end;
386
387 len = next - p;
388 #if GLX_USE_TLS
389 snprintf(path, sizeof path,
390 "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name);
391 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
392 #endif
393 if (dri2_dpy->driver == NULL) {
394 snprintf(path, sizeof path,
395 "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name);
396 dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
397 if (dri2_dpy->driver == NULL)
398 _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror());
399 }
400 }
401
402 if (dri2_dpy->driver == NULL) {
403 _eglLog(_EGL_WARNING,
404 "DRI2: failed to open %s (search paths %s)",
405 dri2_dpy->driver_name, search_paths);
406 return NULL;
407 }
408
409 _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
410 extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
411 if (extensions == NULL) {
412 _eglLog(_EGL_WARNING,
413 "DRI2: driver exports no extensions (%s)", dlerror());
414 dlclose(dri2_dpy->driver);
415 }
416
417 return extensions;
418 }
419
420 EGLBoolean
421 dri2_load_driver(_EGLDisplay *disp)
422 {
423 struct dri2_egl_display *dri2_dpy = disp->DriverData;
424 const __DRIextension **extensions;
425
426 extensions = dri2_open_driver(disp);
427 if (!extensions)
428 return EGL_FALSE;
429
430 if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) {
431 dlclose(dri2_dpy->driver);
432 return EGL_FALSE;
433 }
434
435 return EGL_TRUE;
436 }
437
438 EGLBoolean
439 dri2_load_driver_swrast(_EGLDisplay *disp)
440 {
441 struct dri2_egl_display *dri2_dpy = disp->DriverData;
442 const __DRIextension **extensions;
443
444 dri2_dpy->driver_name = "swrast";
445 extensions = dri2_open_driver(disp);
446
447 if (!extensions)
448 return EGL_FALSE;
449
450 if (!dri2_bind_extensions(dri2_dpy, swrast_driver_extensions, extensions)) {
451 dlclose(dri2_dpy->driver);
452 return EGL_FALSE;
453 }
454
455 return EGL_TRUE;
456 }
457
458 void
459 dri2_setup_screen(_EGLDisplay *disp)
460 {
461 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
462 unsigned int api_mask;
463
464 if (dri2_dpy->dri2) {
465 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
466 } else {
467 assert(dri2_dpy->swrast);
468 api_mask = 1 << __DRI_API_OPENGL |
469 1 << __DRI_API_GLES |
470 1 << __DRI_API_GLES2 |
471 1 << __DRI_API_GLES3;
472 }
473
474 disp->ClientAPIs = 0;
475 if (api_mask & (1 <<__DRI_API_OPENGL))
476 disp->ClientAPIs |= EGL_OPENGL_BIT;
477 if (api_mask & (1 <<__DRI_API_GLES))
478 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
479 if (api_mask & (1 << __DRI_API_GLES2))
480 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
481 if (api_mask & (1 << __DRI_API_GLES3))
482 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
483
484 assert(dri2_dpy->dri2 || dri2_dpy->swrast);
485 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
486
487 if (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) {
488 disp->Extensions.KHR_create_context = EGL_TRUE;
489
490 if (dri2_dpy->robustness)
491 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
492 }
493
494 if (dri2_dpy->image) {
495 disp->Extensions.MESA_drm_image = EGL_TRUE;
496 disp->Extensions.KHR_image_base = EGL_TRUE;
497 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
498 }
499 }
500
501 EGLBoolean
502 dri2_create_screen(_EGLDisplay *disp)
503 {
504 const __DRIextension **extensions;
505 struct dri2_egl_display *dri2_dpy;
506
507 dri2_dpy = disp->DriverData;
508
509 if (dri2_dpy->dri2) {
510 dri2_dpy->dri_screen =
511 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
512 &dri2_dpy->driver_configs, disp);
513 } else {
514 assert(dri2_dpy->swrast);
515 dri2_dpy->dri_screen =
516 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->extensions,
517 &dri2_dpy->driver_configs, disp);
518 }
519
520 if (dri2_dpy->dri_screen == NULL) {
521 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
522 return EGL_FALSE;
523 }
524
525 dri2_dpy->own_dri_screen = 1;
526
527 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
528
529 if (dri2_dpy->dri2) {
530 unsigned i;
531
532 if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
533 goto cleanup_dri_screen;
534
535 for (i = 0; extensions[i]; i++) {
536 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) {
537 dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i];
538 }
539 if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) {
540 dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i];
541 }
542 }
543 } else {
544 assert(dri2_dpy->swrast);
545 if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions))
546 goto cleanup_dri_screen;
547 }
548
549 dri2_setup_screen(disp);
550
551 return EGL_TRUE;
552
553 cleanup_dri_screen:
554 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
555
556 return EGL_FALSE;
557 }
558
559 /**
560 * Called via eglInitialize(), GLX_drv->API.Initialize().
561 */
562 static EGLBoolean
563 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
564 {
565 /* not until swrast_dri is supported */
566 if (disp->Options.UseFallback)
567 return EGL_FALSE;
568
569 switch (disp->Platform) {
570 #ifdef HAVE_X11_PLATFORM
571 case _EGL_PLATFORM_X11:
572 if (disp->Options.TestOnly)
573 return EGL_TRUE;
574 return dri2_initialize_x11(drv, disp);
575 #endif
576
577 #ifdef HAVE_LIBUDEV
578 #ifdef HAVE_DRM_PLATFORM
579 case _EGL_PLATFORM_DRM:
580 if (disp->Options.TestOnly)
581 return EGL_TRUE;
582 return dri2_initialize_drm(drv, disp);
583 #endif
584 #ifdef HAVE_WAYLAND_PLATFORM
585 case _EGL_PLATFORM_WAYLAND:
586 if (disp->Options.TestOnly)
587 return EGL_TRUE;
588 return dri2_initialize_wayland(drv, disp);
589 #endif
590 #endif
591 #ifdef HAVE_ANDROID_PLATFORM
592 case _EGL_PLATFORM_ANDROID:
593 if (disp->Options.TestOnly)
594 return EGL_TRUE;
595 return dri2_initialize_android(drv, disp);
596 #endif
597
598 default:
599 return EGL_FALSE;
600 }
601 }
602
603 /**
604 * Called via eglTerminate(), drv->API.Terminate().
605 */
606 static EGLBoolean
607 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
608 {
609 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
610
611 _eglReleaseDisplayResources(drv, disp);
612 _eglCleanupDisplay(disp);
613
614 if (dri2_dpy->own_dri_screen)
615 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
616 if (dri2_dpy->fd)
617 close(dri2_dpy->fd);
618 if (dri2_dpy->driver)
619 dlclose(dri2_dpy->driver);
620 free(dri2_dpy->device_name);
621
622 if (disp->PlatformDisplay == NULL) {
623 switch (disp->Platform) {
624 #ifdef HAVE_X11_PLATFORM
625 case _EGL_PLATFORM_X11:
626 xcb_disconnect(dri2_dpy->conn);
627 break;
628 #endif
629 #ifdef HAVE_DRM_PLATFORM
630 case _EGL_PLATFORM_DRM:
631 if (dri2_dpy->own_device) {
632 gbm_device_destroy(&dri2_dpy->gbm_dri->base.base);
633 }
634 break;
635 #endif
636 default:
637 break;
638 }
639 }
640
641 free(dri2_dpy);
642 disp->DriverData = NULL;
643
644 return EGL_TRUE;
645 }
646
647 /**
648 * Set the error code after a call to
649 * dri2_egl_display::dri2::createContextAttribs.
650 */
651 static void
652 dri2_create_context_attribs_error(int dri_error)
653 {
654 EGLint egl_error;
655
656 switch (dri_error) {
657 case __DRI_CTX_ERROR_SUCCESS:
658 return;
659
660 case __DRI_CTX_ERROR_NO_MEMORY:
661 egl_error = EGL_BAD_ALLOC;
662 break;
663
664 /* From the EGL_KHR_create_context spec, section "Errors":
665 *
666 * * If <config> does not support a client API context compatible
667 * with the requested API major and minor version, [...] context flags,
668 * and context reset notification behavior (for client API types where
669 * these attributes are supported), then an EGL_BAD_MATCH error is
670 * generated.
671 *
672 * * If an OpenGL ES context is requested and the values for
673 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
674 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
675 * is not defined, than an EGL_BAD_MATCH error is generated.
676 *
677 * * If an OpenGL context is requested, the requested version is
678 * greater than 3.2, and the value for attribute
679 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
680 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
681 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
682 * one of these bits set; or if the implementation does not support
683 * the requested profile, then an EGL_BAD_MATCH error is generated.
684 */
685 case __DRI_CTX_ERROR_BAD_API:
686 case __DRI_CTX_ERROR_BAD_VERSION:
687 case __DRI_CTX_ERROR_BAD_FLAG:
688 egl_error = EGL_BAD_MATCH;
689 break;
690
691 /* From the EGL_KHR_create_context spec, section "Errors":
692 *
693 * * If an attribute name or attribute value in <attrib_list> is not
694 * recognized (including unrecognized bits in bitmask attributes),
695 * then an EGL_BAD_ATTRIBUTE error is generated."
696 */
697 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
698 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
699 egl_error = EGL_BAD_ATTRIBUTE;
700 break;
701
702 default:
703 assert(0);
704 egl_error = EGL_BAD_MATCH;
705 break;
706 }
707
708 _eglError(egl_error, "dri2_create_context");
709 }
710
711 /**
712 * Called via eglCreateContext(), drv->API.CreateContext().
713 */
714 static _EGLContext *
715 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
716 _EGLContext *share_list, const EGLint *attrib_list)
717 {
718 struct dri2_egl_context *dri2_ctx;
719 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
720 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
721 __DRIcontext *shared =
722 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
723 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
724 const __DRIconfig *dri_config;
725 int api;
726
727 (void) drv;
728
729 dri2_ctx = malloc(sizeof *dri2_ctx);
730 if (!dri2_ctx) {
731 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
732 return NULL;
733 }
734
735 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
736 goto cleanup;
737
738 switch (dri2_ctx->base.ClientAPI) {
739 case EGL_OPENGL_ES_API:
740 switch (dri2_ctx->base.ClientMajorVersion) {
741 case 1:
742 api = __DRI_API_GLES;
743 break;
744 case 2:
745 api = __DRI_API_GLES3;
746 break;
747 case 3:
748 api = __DRI_API_GLES3;
749 break;
750 default:
751 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
752 return NULL;
753 }
754 break;
755 case EGL_OPENGL_API:
756 if ((dri2_ctx->base.ClientMajorVersion >= 4
757 || (dri2_ctx->base.ClientMajorVersion == 3
758 && dri2_ctx->base.ClientMinorVersion >= 2))
759 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
760 api = __DRI_API_OPENGL_CORE;
761 else
762 api = __DRI_API_OPENGL;
763 break;
764 default:
765 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
766 return NULL;
767 }
768
769 if (conf != NULL) {
770 /* The config chosen here isn't necessarily
771 * used for surfaces later.
772 * A pixmap surface will use the single config.
773 * This opportunity depends on disabling the
774 * doubleBufferMode check in
775 * src/mesa/main/context.c:check_compatible()
776 */
777 if (dri2_config->dri_double_config)
778 dri_config = dri2_config->dri_double_config;
779 else
780 dri_config = dri2_config->dri_single_config;
781
782 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
783 * makes sure the back buffer will always be used.
784 */
785 if (conf->SurfaceType & EGL_WINDOW_BIT)
786 dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER;
787 }
788 else
789 dri_config = NULL;
790
791 if (dri2_dpy->dri2) {
792 if (dri2_dpy->dri2->base.version >= 3) {
793 unsigned error;
794 unsigned num_attribs = 0;
795 uint32_t ctx_attribs[8];
796
797 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
798 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMajorVersion;
799 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
800 ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMinorVersion;
801
802 if (dri2_ctx->base.Flags != 0) {
803 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
804 * extension, don't even try to send it the robust-access flag.
805 * It may explode. Instead, generate the required EGL error here.
806 */
807 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
808 && !dri2_dpy->robustness) {
809 _eglError(EGL_BAD_MATCH, "eglCreateContext");
810 goto cleanup;
811 }
812
813 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
814 ctx_attribs[num_attribs++] = dri2_ctx->base.Flags;
815 }
816
817 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
818 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
819 * extension, don't even try to send it a reset strategy. It may
820 * explode. Instead, generate the required EGL error here.
821 */
822 if (!dri2_dpy->robustness) {
823 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
824 goto cleanup;
825 }
826
827 ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
828 ctx_attribs[num_attribs++] = __DRI_CTX_RESET_LOSE_CONTEXT;
829 }
830
831 assert(num_attribs <= ARRAY_SIZE(ctx_attribs));
832
833 dri2_ctx->dri_context =
834 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
835 api,
836 dri_config,
837 shared,
838 num_attribs / 2,
839 ctx_attribs,
840 & error,
841 dri2_ctx);
842 dri2_create_context_attribs_error(error);
843 } else {
844 dri2_ctx->dri_context =
845 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
846 api,
847 dri_config,
848 shared,
849 dri2_ctx);
850 }
851 } else {
852 assert(dri2_dpy->swrast);
853 dri2_ctx->dri_context =
854 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
855 api,
856 dri_config,
857 shared,
858 dri2_ctx);
859 }
860
861 if (!dri2_ctx->dri_context)
862 goto cleanup;
863
864 return &dri2_ctx->base;
865
866 cleanup:
867 free(dri2_ctx);
868 return NULL;
869 }
870
871 /**
872 * Called via eglDestroyContext(), drv->API.DestroyContext().
873 */
874 static EGLBoolean
875 dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
876 {
877 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
878 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
879
880 if (_eglPutContext(ctx)) {
881 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
882 free(dri2_ctx);
883 }
884
885 return EGL_TRUE;
886 }
887
888 /**
889 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
890 */
891 static EGLBoolean
892 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
893 _EGLSurface *rsurf, _EGLContext *ctx)
894 {
895 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
896 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
897 struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
898 struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
899 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
900 _EGLContext *old_ctx;
901 _EGLSurface *old_dsurf, *old_rsurf;
902 __DRIdrawable *ddraw, *rdraw;
903 __DRIcontext *cctx;
904
905 /* make new bindings */
906 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
907 return EGL_FALSE;
908
909 /* flush before context switch */
910 if (old_ctx && dri2_drv->glFlush)
911 dri2_drv->glFlush();
912
913 ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
914 rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
915 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
916
917 if (old_ctx) {
918 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
919 dri2_dpy->core->unbindContext(old_cctx);
920 }
921
922 if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
923 dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
924 if (old_dsurf)
925 drv->API.DestroySurface(drv, disp, old_dsurf);
926 if (old_rsurf)
927 drv->API.DestroySurface(drv, disp, old_rsurf);
928 if (old_ctx)
929 drv->API.DestroyContext(drv, disp, old_ctx);
930
931 return EGL_TRUE;
932 } else {
933 /* undo the previous _eglBindContext */
934 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &dsurf, &rsurf);
935 assert(&dri2_ctx->base == ctx &&
936 &dri2_dsurf->base == dsurf &&
937 &dri2_rsurf->base == rsurf);
938
939 _eglPutSurface(dsurf);
940 _eglPutSurface(rsurf);
941 _eglPutContext(ctx);
942
943 _eglPutSurface(old_dsurf);
944 _eglPutSurface(old_rsurf);
945 _eglPutContext(old_ctx);
946
947 return EGL_FALSE;
948 }
949 }
950
951 /*
952 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
953 */
954 static _EGLProc
955 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
956 {
957 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
958
959 return dri2_drv->get_proc_address(procname);
960 }
961
962 static EGLBoolean
963 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
964 {
965 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
966 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
967
968 (void) drv;
969
970 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
971 * we need to copy fake to real here.*/
972
973 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
974
975 return EGL_TRUE;
976 }
977
978 static EGLBoolean
979 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
980 {
981 (void) drv;
982 (void) disp;
983
984 if (engine != EGL_CORE_NATIVE_ENGINE)
985 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
986 /* glXWaitX(); */
987
988 return EGL_TRUE;
989 }
990
991 static EGLBoolean
992 dri2_bind_tex_image(_EGLDriver *drv,
993 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
994 {
995 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
996 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
997 struct dri2_egl_context *dri2_ctx;
998 _EGLContext *ctx;
999 GLint format, target;
1000
1001 ctx = _eglGetCurrentContext();
1002 dri2_ctx = dri2_egl_context(ctx);
1003
1004 if (!_eglBindTexImage(drv, disp, surf, buffer))
1005 return EGL_FALSE;
1006
1007 switch (dri2_surf->base.TextureFormat) {
1008 case EGL_TEXTURE_RGB:
1009 format = __DRI_TEXTURE_FORMAT_RGB;
1010 break;
1011 case EGL_TEXTURE_RGBA:
1012 format = __DRI_TEXTURE_FORMAT_RGBA;
1013 break;
1014 default:
1015 assert(0);
1016 }
1017
1018 switch (dri2_surf->base.TextureTarget) {
1019 case EGL_TEXTURE_2D:
1020 target = GL_TEXTURE_2D;
1021 break;
1022 default:
1023 assert(0);
1024 }
1025
1026 (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
1027 target, format,
1028 dri2_surf->dri_drawable);
1029
1030 return EGL_TRUE;
1031 }
1032
1033 static EGLBoolean
1034 dri2_release_tex_image(_EGLDriver *drv,
1035 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
1036 {
1037 #if __DRI_TEX_BUFFER_VERSION >= 3
1038 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1039 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1040 struct dri2_egl_context *dri2_ctx;
1041 _EGLContext *ctx;
1042 GLint target;
1043
1044 ctx = _eglGetCurrentContext();
1045 dri2_ctx = dri2_egl_context(ctx);
1046
1047 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
1048 return EGL_FALSE;
1049
1050 switch (dri2_surf->base.TextureTarget) {
1051 case EGL_TEXTURE_2D:
1052 target = GL_TEXTURE_2D;
1053 break;
1054 default:
1055 assert(0);
1056 }
1057 if (dri2_dpy->tex_buffer->releaseTexBuffer!=NULL)
1058 (*dri2_dpy->tex_buffer->releaseTexBuffer)(dri2_ctx->dri_context,
1059 target,
1060 dri2_surf->dri_drawable);
1061 #endif
1062
1063 return EGL_TRUE;
1064 }
1065
1066 static _EGLImage *
1067 dri2_create_image(_EGLDisplay *disp, __DRIimage *dri_image)
1068 {
1069 struct dri2_egl_image *dri2_img;
1070
1071 if (dri_image == NULL) {
1072 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1073 return NULL;
1074 }
1075
1076 dri2_img = malloc(sizeof *dri2_img);
1077 if (!dri2_img) {
1078 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
1079 return NULL;
1080 }
1081
1082 if (!_eglInitImage(&dri2_img->base, disp)) {
1083 free(dri2_img);
1084 return NULL;
1085 }
1086
1087 dri2_img->dri_image = dri_image;
1088
1089 return &dri2_img->base;
1090 }
1091
1092 static _EGLImage *
1093 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
1094 EGLClientBuffer buffer,
1095 const EGLint *attr_list)
1096 {
1097 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1098 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1099 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
1100 __DRIimage *dri_image;
1101
1102 if (renderbuffer == 0) {
1103 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1104 return EGL_NO_IMAGE_KHR;
1105 }
1106
1107 dri_image =
1108 dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
1109 renderbuffer, NULL);
1110
1111 return dri2_create_image(disp, dri_image);
1112 }
1113
1114 static _EGLImage *
1115 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1116 EGLClientBuffer buffer, const EGLint *attr_list)
1117 {
1118 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1119 EGLint format, name, pitch, err;
1120 _EGLImageAttribs attrs;
1121 __DRIimage *dri_image;
1122
1123 name = (EGLint) (uintptr_t) buffer;
1124
1125 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1126 if (err != EGL_SUCCESS)
1127 return NULL;
1128
1129 if (attrs.Width <= 0 || attrs.Height <= 0 ||
1130 attrs.DRMBufferStrideMESA <= 0) {
1131 _eglError(EGL_BAD_PARAMETER,
1132 "bad width, height or stride");
1133 return NULL;
1134 }
1135
1136 switch (attrs.DRMBufferFormatMESA) {
1137 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1138 format = __DRI_IMAGE_FORMAT_ARGB8888;
1139 pitch = attrs.DRMBufferStrideMESA;
1140 break;
1141 default:
1142 _eglError(EGL_BAD_PARAMETER,
1143 "dri2_create_image_khr: unsupported pixmap depth");
1144 return NULL;
1145 }
1146
1147 dri_image =
1148 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1149 attrs.Width,
1150 attrs.Height,
1151 format,
1152 name,
1153 pitch,
1154 NULL);
1155
1156 return dri2_create_image(disp, dri_image);
1157 }
1158
1159 #ifdef HAVE_WAYLAND_PLATFORM
1160
1161 /* This structure describes how a wl_buffer maps to one or more
1162 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1163 * offsets and strides of the planes in the buffer. This table maps a
1164 * wl_drm format code to a description of the planes in the buffer
1165 * that lets us create a __DRIimage for each of the planes. */
1166
1167 static const struct wl_drm_components_descriptor {
1168 uint32_t dri_components;
1169 EGLint components;
1170 int nplanes;
1171 } wl_drm_components[] = {
1172 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
1173 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
1174 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
1175 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
1176 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
1177 };
1178
1179 static _EGLImage *
1180 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1181 EGLClientBuffer _buffer,
1182 const EGLint *attr_list)
1183 {
1184 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer;
1185 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1186 const struct wl_drm_components_descriptor *f;
1187 __DRIimage *dri_image;
1188 _EGLImageAttribs attrs;
1189 EGLint err;
1190 int32_t plane;
1191
1192 if (!wayland_buffer_is_drm(&buffer->buffer))
1193 return NULL;
1194
1195 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1196 plane = attrs.PlaneWL;
1197 if (err != EGL_SUCCESS) {
1198 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1199 return NULL;
1200 }
1201
1202 f = buffer->driver_format;
1203 if (plane < 0 || plane >= f->nplanes) {
1204 _eglError(EGL_BAD_PARAMETER,
1205 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1206 return NULL;
1207 }
1208
1209 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
1210
1211 if (dri_image == NULL) {
1212 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
1213 return NULL;
1214 }
1215
1216 return dri2_create_image(disp, dri_image);
1217 }
1218 #endif
1219
1220 _EGLImage *
1221 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1222 _EGLContext *ctx, EGLenum target,
1223 EGLClientBuffer buffer, const EGLint *attr_list)
1224 {
1225 (void) drv;
1226
1227 switch (target) {
1228 case EGL_GL_RENDERBUFFER_KHR:
1229 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
1230 case EGL_DRM_BUFFER_MESA:
1231 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
1232 #ifdef HAVE_WAYLAND_PLATFORM
1233 case EGL_WAYLAND_BUFFER_WL:
1234 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
1235 #endif
1236 default:
1237 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
1238 return EGL_NO_IMAGE_KHR;
1239 }
1240 }
1241
1242 static EGLBoolean
1243 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
1244 {
1245 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1246 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
1247
1248 (void) drv;
1249
1250 dri2_dpy->image->destroyImage(dri2_img->dri_image);
1251 free(dri2_img);
1252
1253 return EGL_TRUE;
1254 }
1255
1256 static _EGLImage *
1257 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
1258 const EGLint *attr_list)
1259 {
1260 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1261 struct dri2_egl_image *dri2_img;
1262 _EGLImageAttribs attrs;
1263 unsigned int dri_use, valid_mask;
1264 int format;
1265 EGLint err = EGL_SUCCESS;
1266
1267 (void) drv;
1268
1269 dri2_img = malloc(sizeof *dri2_img);
1270 if (!dri2_img) {
1271 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1272 return EGL_NO_IMAGE_KHR;
1273 }
1274
1275 if (!attr_list) {
1276 err = EGL_BAD_PARAMETER;
1277 goto cleanup_img;
1278 }
1279
1280 if (!_eglInitImage(&dri2_img->base, disp)) {
1281 err = EGL_BAD_PARAMETER;
1282 goto cleanup_img;
1283 }
1284
1285 err = _eglParseImageAttribList(&attrs, disp, attr_list);
1286 if (err != EGL_SUCCESS)
1287 goto cleanup_img;
1288
1289 if (attrs.Width <= 0 || attrs.Height <= 0) {
1290 _eglLog(_EGL_WARNING, "bad width or height (%dx%d)",
1291 attrs.Width, attrs.Height);
1292 goto cleanup_img;
1293 }
1294
1295 switch (attrs.DRMBufferFormatMESA) {
1296 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
1297 format = __DRI_IMAGE_FORMAT_ARGB8888;
1298 break;
1299 default:
1300 _eglLog(_EGL_WARNING, "bad image format value 0x%04x",
1301 attrs.DRMBufferFormatMESA);
1302 goto cleanup_img;
1303 }
1304
1305 valid_mask =
1306 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
1307 EGL_DRM_BUFFER_USE_SHARE_MESA |
1308 EGL_DRM_BUFFER_USE_CURSOR_MESA;
1309 if (attrs.DRMBufferUseMESA & ~valid_mask) {
1310 _eglLog(_EGL_WARNING, "bad image use bit 0x%04x",
1311 attrs.DRMBufferUseMESA & ~valid_mask);
1312 goto cleanup_img;
1313 }
1314
1315 dri_use = 0;
1316 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
1317 dri_use |= __DRI_IMAGE_USE_SHARE;
1318 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
1319 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1320 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
1321 dri_use |= __DRI_IMAGE_USE_CURSOR;
1322
1323 dri2_img->dri_image =
1324 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
1325 attrs.Width, attrs.Height,
1326 format, dri_use, dri2_img);
1327 if (dri2_img->dri_image == NULL) {
1328 err = EGL_BAD_ALLOC;
1329 goto cleanup_img;
1330 }
1331
1332 return &dri2_img->base;
1333
1334 cleanup_img:
1335 free(dri2_img);
1336 _eglError(err, "dri2_create_drm_image_mesa");
1337
1338 return EGL_NO_IMAGE_KHR;
1339 }
1340
1341 static EGLBoolean
1342 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
1343 EGLint *name, EGLint *handle, EGLint *stride)
1344 {
1345 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1346 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1347
1348 (void) drv;
1349
1350 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
1351 __DRI_IMAGE_ATTRIB_NAME, name)) {
1352 _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
1353 return EGL_FALSE;
1354 }
1355
1356 if (handle)
1357 dri2_dpy->image->queryImage(dri2_img->dri_image,
1358 __DRI_IMAGE_ATTRIB_HANDLE, handle);
1359
1360 if (stride)
1361 dri2_dpy->image->queryImage(dri2_img->dri_image,
1362 __DRI_IMAGE_ATTRIB_STRIDE, stride);
1363
1364 return EGL_TRUE;
1365 }
1366
1367 #ifdef HAVE_WAYLAND_PLATFORM
1368
1369 static void
1370 dri2_wl_reference_buffer(void *user_data, uint32_t name,
1371 struct wl_drm_buffer *buffer)
1372 {
1373 _EGLDisplay *disp = user_data;
1374 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1375 __DRIimage *img;
1376 int i, dri_components = 0;
1377
1378 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
1379 buffer->buffer.width,
1380 buffer->buffer.height,
1381 buffer->format, (int*)&name, 1,
1382 buffer->stride,
1383 buffer->offset,
1384 NULL);
1385
1386 if (img == NULL)
1387 return;
1388
1389 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
1390
1391 buffer->driver_format = NULL;
1392 for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
1393 if (wl_drm_components[i].dri_components == dri_components)
1394 buffer->driver_format = &wl_drm_components[i];
1395
1396 if (buffer->driver_format == NULL)
1397 dri2_dpy->image->destroyImage(img);
1398 else
1399 buffer->driver_buffer = img;
1400 }
1401
1402 static void
1403 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
1404 {
1405 _EGLDisplay *disp = user_data;
1406 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1407
1408 dri2_dpy->image->destroyImage(buffer->driver_buffer);
1409 }
1410
1411 static struct wayland_drm_callbacks wl_drm_callbacks = {
1412 .authenticate = NULL,
1413 .reference_buffer = dri2_wl_reference_buffer,
1414 .release_buffer = dri2_wl_release_buffer
1415 };
1416
1417 static EGLBoolean
1418 dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
1419 struct wl_display *wl_dpy)
1420 {
1421 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1422
1423 (void) drv;
1424
1425 if (dri2_dpy->wl_server_drm)
1426 return EGL_FALSE;
1427
1428 wl_drm_callbacks.authenticate =
1429 (int(*)(void *, uint32_t)) dri2_dpy->authenticate;
1430
1431 dri2_dpy->wl_server_drm =
1432 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
1433 &wl_drm_callbacks, disp);
1434
1435 if (!dri2_dpy->wl_server_drm)
1436 return EGL_FALSE;
1437
1438 return EGL_TRUE;
1439 }
1440
1441 static EGLBoolean
1442 dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
1443 struct wl_display *wl_dpy)
1444 {
1445 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1446
1447 (void) drv;
1448
1449 if (!dri2_dpy->wl_server_drm)
1450 return EGL_FALSE;
1451
1452 wayland_drm_uninit(dri2_dpy->wl_server_drm);
1453 dri2_dpy->wl_server_drm = NULL;
1454
1455 return EGL_TRUE;
1456 }
1457
1458 static EGLBoolean
1459 dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
1460 struct wl_buffer *_buffer,
1461 EGLint attribute, EGLint *value)
1462 {
1463 struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer;
1464 const struct wl_drm_components_descriptor *format;
1465
1466 if (!wayland_buffer_is_drm(&buffer->buffer))
1467 return EGL_FALSE;
1468
1469 format = buffer->driver_format;
1470 switch (attribute) {
1471 case EGL_TEXTURE_FORMAT:
1472 *value = format->components;
1473 return EGL_TRUE;
1474 case EGL_WIDTH:
1475 *value = buffer->buffer.width;
1476 break;
1477 case EGL_HEIGHT:
1478 *value = buffer->buffer.height;
1479 break;
1480 }
1481
1482 return EGL_FALSE;
1483 }
1484 #endif
1485
1486 static void
1487 dri2_unload(_EGLDriver *drv)
1488 {
1489 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1490
1491 if (dri2_drv->handle)
1492 dlclose(dri2_drv->handle);
1493 free(dri2_drv);
1494 }
1495
1496 static EGLBoolean
1497 dri2_load(_EGLDriver *drv)
1498 {
1499 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
1500 #ifdef HAVE_SHARED_GLAPI
1501 #ifdef HAVE_ANDROID_PLATFORM
1502 const char *libname = "libglapi.so";
1503 #else
1504 const char *libname = "libglapi.so.0";
1505 #endif
1506 #else
1507 /*
1508 * Both libGL.so and libglapi.so are glapi providers. There is no way to
1509 * tell which one to load.
1510 */
1511 const char *libname = NULL;
1512 #endif
1513 void *handle;
1514
1515 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
1516 handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
1517 if (handle) {
1518 dri2_drv->get_proc_address = (_EGLProc (*)(const char *))
1519 dlsym(handle, "_glapi_get_proc_address");
1520 if (!dri2_drv->get_proc_address || !libname) {
1521 /* no need to keep a reference */
1522 dlclose(handle);
1523 handle = NULL;
1524 }
1525 }
1526
1527 /* if glapi is not available, loading DRI drivers will fail */
1528 if (!dri2_drv->get_proc_address) {
1529 _eglLog(_EGL_WARNING, "DRI2: failed to find _glapi_get_proc_address");
1530 return EGL_FALSE;
1531 }
1532
1533 dri2_drv->glFlush = (void (*)(void))
1534 dri2_drv->get_proc_address("glFlush");
1535
1536 dri2_drv->handle = handle;
1537
1538 return EGL_TRUE;
1539 }
1540
1541 /**
1542 * This is the main entrypoint into the driver, called by libEGL.
1543 * Create a new _EGLDriver object and init its dispatch table.
1544 */
1545 _EGLDriver *
1546 _eglBuiltInDriverDRI2(const char *args)
1547 {
1548 struct dri2_egl_driver *dri2_drv;
1549
1550 (void) args;
1551
1552 dri2_drv = calloc(1, sizeof *dri2_drv);
1553 if (!dri2_drv)
1554 return NULL;
1555
1556 if (!dri2_load(&dri2_drv->base)) {
1557 free(dri2_drv);
1558 return NULL;
1559 }
1560
1561 _eglInitDriverFallbacks(&dri2_drv->base);
1562 dri2_drv->base.API.Initialize = dri2_initialize;
1563 dri2_drv->base.API.Terminate = dri2_terminate;
1564 dri2_drv->base.API.CreateContext = dri2_create_context;
1565 dri2_drv->base.API.DestroyContext = dri2_destroy_context;
1566 dri2_drv->base.API.MakeCurrent = dri2_make_current;
1567 dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
1568 dri2_drv->base.API.WaitClient = dri2_wait_client;
1569 dri2_drv->base.API.WaitNative = dri2_wait_native;
1570 dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
1571 dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
1572 dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
1573 dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
1574 dri2_drv->base.API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
1575 dri2_drv->base.API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
1576 #ifdef HAVE_WAYLAND_PLATFORM
1577 dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
1578 dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
1579 dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
1580 #endif
1581
1582 dri2_drv->base.Name = "DRI2";
1583 dri2_drv->base.Unload = dri2_unload;
1584
1585 return &dri2_drv->base;
1586 }