Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/egl/drivers/dri2/egl_dri2.c
4570 views
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 <[email protected]>
26
*/
27
28
#include <stdbool.h>
29
#include <stdint.h>
30
#include <stdbool.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <stdio.h>
34
#include <limits.h>
35
#include <dlfcn.h>
36
#include <fcntl.h>
37
#include <errno.h>
38
#include <unistd.h>
39
#include <c11/threads.h>
40
#include <time.h>
41
#ifdef HAVE_LIBDRM
42
#include <xf86drm.h>
43
#include "drm-uapi/drm_fourcc.h"
44
#endif
45
#include <GL/gl.h>
46
#include <GL/internal/dri_interface.h>
47
#include <sys/types.h>
48
#include <sys/stat.h>
49
50
#ifdef HAVE_WAYLAND_PLATFORM
51
#include <wayland-client.h>
52
#include "wayland-drm.h"
53
#include "wayland-drm-client-protocol.h"
54
#include "linux-dmabuf-unstable-v1-client-protocol.h"
55
#endif
56
57
#ifdef HAVE_X11_PLATFORM
58
#include "X11/Xlibint.h"
59
#endif
60
61
#include "egldefines.h"
62
#include "egl_dri2.h"
63
#include "GL/mesa_glinterop.h"
64
#include "loader/loader.h"
65
#include "util/libsync.h"
66
#include "util/os_file.h"
67
#include "util/u_atomic.h"
68
#include "util/u_vector.h"
69
#include "mapi/glapi/glapi.h"
70
#include "util/bitscan.h"
71
#include "util/u_math.h"
72
73
#define NUM_ATTRIBS 12
74
75
static const struct dri2_pbuffer_visual {
76
const char *format_name;
77
unsigned int dri_image_format;
78
int rgba_shifts[4];
79
unsigned int rgba_sizes[4];
80
} dri2_pbuffer_visuals[] = {
81
{
82
"ABGR16F",
83
__DRI_IMAGE_FORMAT_ABGR16161616F,
84
{ 0, 16, 32, 48 },
85
{ 16, 16, 16, 16 }
86
},
87
{
88
"XBGR16F",
89
__DRI_IMAGE_FORMAT_XBGR16161616F,
90
{ 0, 16, 32, -1 },
91
{ 16, 16, 16, 0 }
92
},
93
{
94
"A2RGB10",
95
__DRI_IMAGE_FORMAT_ARGB2101010,
96
{ 20, 10, 0, 30 },
97
{ 10, 10, 10, 2 }
98
},
99
{
100
"X2RGB10",
101
__DRI_IMAGE_FORMAT_XRGB2101010,
102
{ 20, 10, 0, -1 },
103
{ 10, 10, 10, 0 }
104
},
105
{
106
"ARGB8888",
107
__DRI_IMAGE_FORMAT_ARGB8888,
108
{ 16, 8, 0, 24 },
109
{ 8, 8, 8, 8 }
110
},
111
{
112
"RGB888",
113
__DRI_IMAGE_FORMAT_XRGB8888,
114
{ 16, 8, 0, -1 },
115
{ 8, 8, 8, 0 }
116
},
117
{
118
"RGB565",
119
__DRI_IMAGE_FORMAT_RGB565,
120
{ 11, 5, 0, -1 },
121
{ 5, 6, 5, 0 }
122
},
123
};
124
125
static void
126
dri_set_background_context(void *loaderPrivate)
127
{
128
_EGLContext *ctx = _eglGetCurrentContext();
129
_EGLThreadInfo *t = _eglGetCurrentThread();
130
131
_eglBindContextToThread(ctx, t);
132
}
133
134
static void
135
dri2_gl_flush()
136
{
137
static void (*glFlush)(void);
138
static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
139
140
mtx_lock(&glFlushMutex);
141
if (!glFlush)
142
glFlush = _glapi_get_proc_address("glFlush");
143
mtx_unlock(&glFlushMutex);
144
145
/* if glFlush is not available things are horribly broken */
146
if (!glFlush) {
147
_eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
148
return;
149
}
150
151
glFlush();
152
}
153
154
static GLboolean
155
dri_is_thread_safe(void *loaderPrivate)
156
{
157
struct dri2_egl_surface *dri2_surf = loaderPrivate;
158
UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
159
160
#ifdef HAVE_X11_PLATFORM
161
Display *xdpy = (Display*)display->PlatformDisplay;
162
163
/* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
164
* platform
165
*
166
* 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
167
* It wll be NULL if XInitThreads wasn't called.
168
*/
169
if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
170
return false;
171
#endif
172
173
return true;
174
}
175
176
const __DRIbackgroundCallableExtension background_callable_extension = {
177
.base = { __DRI_BACKGROUND_CALLABLE, 2 },
178
179
.setBackgroundContext = dri_set_background_context,
180
.isThreadSafe = dri_is_thread_safe,
181
};
182
183
const __DRIuseInvalidateExtension use_invalidate = {
184
.base = { __DRI_USE_INVALIDATE, 1 }
185
};
186
187
static void
188
dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
189
int *x, int *y, int *w, int *h,
190
void *loaderPrivate)
191
{
192
struct dri2_egl_surface *dri2_surf = loaderPrivate;
193
194
*x = *y = 0;
195
*w = dri2_surf->base.Width;
196
*h = dri2_surf->base.Height;
197
}
198
199
static int
200
dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
201
{
202
const int depth = dri2_surf->base.Config->BufferSize;
203
return depth ? util_next_power_of_two(depth / 8) : 0;
204
}
205
206
static void
207
dri2_put_image(__DRIdrawable * draw, int op,
208
int x, int y, int w, int h,
209
char *data, void *loaderPrivate)
210
{
211
struct dri2_egl_surface *dri2_surf = loaderPrivate;
212
const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
213
const int width = dri2_surf->base.Width;
214
const int height = dri2_surf->base.Height;
215
const int dst_stride = width*bpp;
216
const int src_stride = w*bpp;
217
const int x_offset = x*bpp;
218
int copy_width = src_stride;
219
220
if (!dri2_surf->swrast_device_buffer)
221
dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
222
223
if (dri2_surf->swrast_device_buffer) {
224
const char *src = data;
225
char *dst = dri2_surf->swrast_device_buffer;
226
227
dst += x_offset;
228
dst += y*dst_stride;
229
230
/* Drivers are allowed to submit OOB PutImage requests, so clip here. */
231
if (copy_width > dst_stride - x_offset)
232
copy_width = dst_stride - x_offset;
233
if (h > height - y)
234
h = height - y;
235
236
for (; 0 < h; --h) {
237
memcpy(dst, src, copy_width);
238
dst += dst_stride;
239
src += src_stride;
240
}
241
}
242
}
243
244
static void
245
dri2_get_image(__DRIdrawable * read,
246
int x, int y, int w, int h,
247
char *data, void *loaderPrivate)
248
{
249
struct dri2_egl_surface *dri2_surf = loaderPrivate;
250
const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
251
const int width = dri2_surf->base.Width;
252
const int height = dri2_surf->base.Height;
253
const int src_stride = width*bpp;
254
const int dst_stride = w*bpp;
255
const int x_offset = x*bpp;
256
int copy_width = dst_stride;
257
const char *src = dri2_surf->swrast_device_buffer;
258
char *dst = data;
259
260
if (!src) {
261
memset(data, 0, copy_width * h);
262
return;
263
}
264
265
src += x_offset;
266
src += y*src_stride;
267
268
/* Drivers are allowed to submit OOB GetImage requests, so clip here. */
269
if (copy_width > src_stride - x_offset)
270
copy_width = src_stride - x_offset;
271
if (h > height - y)
272
h = height - y;
273
274
for (; 0 < h; --h) {
275
memcpy(dst, src, copy_width);
276
src += src_stride;
277
dst += dst_stride;
278
}
279
280
}
281
282
/* HACK: technically we should have swrast_null, instead of these.
283
*/
284
const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
285
.base = { __DRI_SWRAST_LOADER, 1 },
286
.getDrawableInfo = dri2_get_pbuffer_drawable_info,
287
.putImage = dri2_put_image,
288
.getImage = dri2_get_image,
289
};
290
291
static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
292
[__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
293
[__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
294
[__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
295
[__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
296
[__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
297
[__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
298
[__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
299
[__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
300
[__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
301
[__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
302
[__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
303
[__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
304
[__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
305
};
306
307
const __DRIconfig *
308
dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
309
EGLenum colorspace)
310
{
311
const bool double_buffer = surface_type == EGL_WINDOW_BIT;
312
const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
313
314
return conf->dri_config[double_buffer][srgb];
315
}
316
317
static EGLBoolean
318
dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
319
{
320
if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
321
return EGL_FALSE;
322
323
if (!_eglMatchConfig(conf, criteria))
324
return EGL_FALSE;
325
326
return EGL_TRUE;
327
}
328
329
void
330
dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
331
const __DRIconfig *config, int *shifts,
332
unsigned int *sizes)
333
{
334
unsigned int mask;
335
336
if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
337
core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
338
core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
339
core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
340
} else {
341
/* Driver isn't exposing shifts, so convert masks to shifts */
342
core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
343
shifts[0] = ffs(mask) - 1;
344
core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
345
shifts[1] = ffs(mask) - 1;
346
core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
347
shifts[2] = ffs(mask) - 1;
348
core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
349
shifts[3] = ffs(mask) - 1;
350
}
351
352
core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
353
core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
354
core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
355
core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
356
}
357
358
void
359
dri2_get_render_type_float(const __DRIcoreExtension *core,
360
const __DRIconfig *config,
361
bool *is_float)
362
{
363
unsigned int render_type;
364
365
core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
366
*is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
367
}
368
369
unsigned int
370
dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
371
const __DRIconfig *config)
372
{
373
int shifts[4];
374
unsigned int sizes[4];
375
376
dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
377
378
for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
379
const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
380
381
if (shifts[0] == visual->rgba_shifts[0] &&
382
shifts[1] == visual->rgba_shifts[1] &&
383
shifts[2] == visual->rgba_shifts[2] &&
384
shifts[3] == visual->rgba_shifts[3] &&
385
sizes[0] == visual->rgba_sizes[0] &&
386
sizes[1] == visual->rgba_sizes[1] &&
387
sizes[2] == visual->rgba_sizes[2] &&
388
sizes[3] == visual->rgba_sizes[3]) {
389
return visual->dri_image_format;
390
}
391
}
392
393
return __DRI_IMAGE_FORMAT_NONE;
394
}
395
396
struct dri2_egl_config *
397
dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
398
EGLint surface_type, const EGLint *attr_list,
399
const int *rgba_shifts, const unsigned int *rgba_sizes)
400
{
401
struct dri2_egl_config *conf;
402
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
403
_EGLConfig base;
404
unsigned int attrib, value, double_buffer;
405
bool srgb = false;
406
EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
407
int dri_shifts[4] = { -1, -1, -1, -1 };
408
unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
409
_EGLConfig *matching_config;
410
EGLint num_configs = 0;
411
EGLint config_id;
412
413
_eglInitConfig(&base, disp, id);
414
415
double_buffer = 0;
416
bind_to_texture_rgb = 0;
417
bind_to_texture_rgba = 0;
418
419
for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
420
if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
421
break;
422
423
switch (attrib) {
424
case __DRI_ATTRIB_RENDER_TYPE:
425
if (value & __DRI_ATTRIB_FLOAT_BIT)
426
base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
427
if (value & __DRI_ATTRIB_RGBA_BIT)
428
value = EGL_RGB_BUFFER;
429
else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
430
value = EGL_LUMINANCE_BUFFER;
431
else
432
return NULL;
433
base.ColorBufferType = value;
434
break;
435
436
case __DRI_ATTRIB_CONFIG_CAVEAT:
437
if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
438
value = EGL_NON_CONFORMANT_CONFIG;
439
else if (value & __DRI_ATTRIB_SLOW_BIT)
440
value = EGL_SLOW_CONFIG;
441
else
442
value = EGL_NONE;
443
base.ConfigCaveat = value;
444
break;
445
446
case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
447
bind_to_texture_rgb = value;
448
break;
449
450
case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
451
bind_to_texture_rgba = value;
452
break;
453
454
case __DRI_ATTRIB_DOUBLE_BUFFER:
455
double_buffer = value;
456
break;
457
458
case __DRI_ATTRIB_RED_SIZE:
459
dri_sizes[0] = value;
460
base.RedSize = value;
461
break;
462
463
case __DRI_ATTRIB_RED_MASK:
464
dri_shifts[0] = ffs(value) - 1;
465
break;
466
467
case __DRI_ATTRIB_RED_SHIFT:
468
dri_shifts[0] = value;
469
break;
470
471
case __DRI_ATTRIB_GREEN_SIZE:
472
dri_sizes[1] = value;
473
base.GreenSize = value;
474
break;
475
476
case __DRI_ATTRIB_GREEN_MASK:
477
dri_shifts[1] = ffs(value) - 1;
478
break;
479
480
case __DRI_ATTRIB_GREEN_SHIFT:
481
dri_shifts[1] = value;
482
break;
483
484
case __DRI_ATTRIB_BLUE_SIZE:
485
dri_sizes[2] = value;
486
base.BlueSize = value;
487
break;
488
489
case __DRI_ATTRIB_BLUE_MASK:
490
dri_shifts[2] = ffs(value) - 1;
491
break;
492
493
case __DRI_ATTRIB_BLUE_SHIFT:
494
dri_shifts[2] = value;
495
break;
496
497
case __DRI_ATTRIB_ALPHA_SIZE:
498
dri_sizes[3] = value;
499
base.AlphaSize = value;
500
break;
501
502
case __DRI_ATTRIB_ALPHA_MASK:
503
dri_shifts[3] = ffs(value) - 1;
504
break;
505
506
case __DRI_ATTRIB_ALPHA_SHIFT:
507
dri_shifts[3] = value;
508
break;
509
510
case __DRI_ATTRIB_ACCUM_RED_SIZE:
511
case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
512
case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
513
case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
514
/* Don't expose visuals with the accumulation buffer. */
515
if (value > 0)
516
return NULL;
517
break;
518
519
case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
520
srgb = value != 0;
521
if (!disp->Extensions.KHR_gl_colorspace && srgb)
522
return NULL;
523
break;
524
525
case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
526
base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
527
break;
528
case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
529
base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
530
break;
531
case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
532
if (disp->Extensions.KHR_mutable_render_buffer)
533
surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
534
break;
535
default:
536
key = dri2_to_egl_attribute_map[attrib];
537
if (key != 0)
538
_eglSetConfigKey(&base, key, value);
539
break;
540
}
541
}
542
543
if (attr_list)
544
for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
545
_eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
546
547
if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
548
return NULL;
549
550
if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
551
return NULL;
552
553
base.NativeRenderable = EGL_TRUE;
554
555
base.SurfaceType = surface_type;
556
if (surface_type & (EGL_PBUFFER_BIT |
557
(disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
558
base.BindToTextureRGB = bind_to_texture_rgb;
559
if (base.AlphaSize > 0)
560
base.BindToTextureRGBA = bind_to_texture_rgba;
561
}
562
563
if (double_buffer) {
564
surface_type &= ~EGL_PIXMAP_BIT;
565
}
566
567
/* No support for pbuffer + MSAA for now.
568
*
569
* XXX TODO: pbuffer + MSAA does not work and causes crashes.
570
* See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
571
*/
572
if (base.Samples) {
573
surface_type &= ~EGL_PBUFFER_BIT;
574
}
575
576
if (!surface_type)
577
return NULL;
578
579
base.RenderableType = disp->ClientAPIs;
580
base.Conformant = disp->ClientAPIs;
581
582
base.MinSwapInterval = dri2_dpy->min_swap_interval;
583
base.MaxSwapInterval = dri2_dpy->max_swap_interval;
584
585
if (!_eglValidateConfig(&base, EGL_FALSE)) {
586
_eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
587
return NULL;
588
}
589
590
config_id = base.ConfigID;
591
base.ConfigID = EGL_DONT_CARE;
592
base.SurfaceType = EGL_DONT_CARE;
593
num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
594
(_EGLArrayForEach) dri2_match_config, &base);
595
596
if (num_configs == 1) {
597
conf = (struct dri2_egl_config *) matching_config;
598
599
if (!conf->dri_config[double_buffer][srgb])
600
conf->dri_config[double_buffer][srgb] = dri_config;
601
else
602
/* a similar config type is already added (unlikely) => discard */
603
return NULL;
604
}
605
else if (num_configs == 0) {
606
conf = calloc(1, sizeof *conf);
607
if (conf == NULL)
608
return NULL;
609
610
conf->dri_config[double_buffer][srgb] = dri_config;
611
612
memcpy(&conf->base, &base, sizeof base);
613
conf->base.SurfaceType = 0;
614
conf->base.ConfigID = config_id;
615
616
_eglLinkConfig(&conf->base);
617
}
618
else {
619
unreachable("duplicates should not be possible");
620
return NULL;
621
}
622
623
conf->base.SurfaceType |= surface_type;
624
625
return conf;
626
}
627
628
EGLBoolean
629
dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
630
{
631
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
632
unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
633
unsigned int config_count = 0;
634
635
for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
636
for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
637
struct dri2_egl_config *dri2_conf;
638
639
dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
640
config_count + 1, EGL_PBUFFER_BIT, NULL,
641
dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
642
643
if (dri2_conf) {
644
if (dri2_conf->base.ConfigID == config_count + 1)
645
config_count++;
646
format_count[j]++;
647
}
648
}
649
}
650
651
for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
652
if (!format_count[i]) {
653
_eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
654
dri2_pbuffer_visuals[i].format_name);
655
}
656
}
657
658
return (config_count != 0);
659
}
660
661
__DRIimage *
662
dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
663
{
664
_EGLDisplay *disp = data;
665
struct dri2_egl_image *dri2_img;
666
_EGLImage *img;
667
668
(void) screen;
669
670
mtx_lock(&disp->Mutex);
671
img = _eglLookupImage(image, disp);
672
mtx_unlock(&disp->Mutex);
673
674
if (img == NULL) {
675
_eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
676
return NULL;
677
}
678
679
dri2_img = dri2_egl_image(image);
680
681
return dri2_img->dri_image;
682
}
683
684
const __DRIimageLookupExtension image_lookup_extension = {
685
.base = { __DRI_IMAGE_LOOKUP, 1 },
686
687
.lookupEGLImage = dri2_lookup_egl_image
688
};
689
690
struct dri2_extension_match {
691
const char *name;
692
int version;
693
int offset;
694
};
695
696
static const struct dri2_extension_match dri3_driver_extensions[] = {
697
{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
698
{ __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
699
{ NULL, 0, 0 }
700
};
701
702
static const struct dri2_extension_match dri2_driver_extensions[] = {
703
{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
704
{ __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
705
{ NULL, 0, 0 }
706
};
707
708
static const struct dri2_extension_match dri2_core_extensions[] = {
709
{ __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
710
{ __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
711
{ __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
712
{ NULL, 0, 0 }
713
};
714
715
static const struct dri2_extension_match swrast_driver_extensions[] = {
716
{ __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
717
{ __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
718
{ NULL, 0, 0 }
719
};
720
721
static const struct dri2_extension_match swrast_core_extensions[] = {
722
{ __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
723
{ NULL, 0, 0 }
724
};
725
726
static const struct dri2_extension_match optional_driver_extensions[] = {
727
{ __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
728
{ NULL, 0, 0 }
729
};
730
731
static const struct dri2_extension_match optional_core_extensions[] = {
732
{ __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
733
{ __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
734
{ __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
735
{ __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
736
{ __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
737
{ __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
738
{ __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
739
{ __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
740
{ __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
741
{ __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
742
{ __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
743
{ NULL, 0, 0 }
744
};
745
746
static EGLBoolean
747
dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
748
const struct dri2_extension_match *matches,
749
const __DRIextension **extensions,
750
bool optional)
751
{
752
int ret = EGL_TRUE;
753
void *field;
754
755
for (int i = 0; extensions[i]; i++) {
756
_eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
757
for (int j = 0; matches[j].name; j++) {
758
if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
759
extensions[i]->version >= matches[j].version) {
760
field = ((char *) dri2_dpy + matches[j].offset);
761
*(const __DRIextension **) field = extensions[i];
762
_eglLog(_EGL_INFO, "found extension %s version %d",
763
extensions[i]->name, extensions[i]->version);
764
break;
765
}
766
}
767
}
768
769
for (int j = 0; matches[j].name; j++) {
770
field = ((char *) dri2_dpy + matches[j].offset);
771
if (*(const __DRIextension **) field == NULL) {
772
if (optional) {
773
_eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
774
matches[j].name, matches[j].version);
775
} else {
776
_eglLog(_EGL_WARNING, "did not find extension %s version %d",
777
matches[j].name, matches[j].version);
778
ret = EGL_FALSE;
779
}
780
}
781
}
782
783
return ret;
784
}
785
786
static const __DRIextension **
787
dri2_open_driver(_EGLDisplay *disp)
788
{
789
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
790
static const char *search_path_vars[] = {
791
"LIBGL_DRIVERS_PATH",
792
NULL,
793
};
794
795
return loader_open_driver(dri2_dpy->driver_name,
796
&dri2_dpy->driver,
797
search_path_vars);
798
}
799
800
static EGLBoolean
801
dri2_load_driver_common(_EGLDisplay *disp,
802
const struct dri2_extension_match *driver_extensions)
803
{
804
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
805
const __DRIextension **extensions;
806
807
extensions = dri2_open_driver(disp);
808
if (!extensions)
809
return EGL_FALSE;
810
811
if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
812
dlclose(dri2_dpy->driver);
813
dri2_dpy->driver = NULL;
814
return EGL_FALSE;
815
}
816
dri2_dpy->driver_extensions = extensions;
817
818
dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
819
820
return EGL_TRUE;
821
}
822
823
EGLBoolean
824
dri2_load_driver(_EGLDisplay *disp)
825
{
826
return dri2_load_driver_common(disp, dri2_driver_extensions);
827
}
828
829
EGLBoolean
830
dri2_load_driver_dri3(_EGLDisplay *disp)
831
{
832
return dri2_load_driver_common(disp, dri3_driver_extensions);
833
}
834
835
EGLBoolean
836
dri2_load_driver_swrast(_EGLDisplay *disp)
837
{
838
return dri2_load_driver_common(disp, swrast_driver_extensions);
839
}
840
841
static unsigned
842
dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
843
{
844
const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
845
unsigned int value = 0;
846
847
if (!rendererQuery ||
848
rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
849
return 0;
850
851
return value;
852
}
853
854
static const char *
855
dri2_query_driver_name(_EGLDisplay *disp)
856
{
857
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
858
return dri2_dpy->driver_name;
859
}
860
861
static char *
862
dri2_query_driver_config(_EGLDisplay *disp)
863
{
864
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
865
const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
866
867
if (ext->base.version >= 2)
868
return ext->getXml(dri2_dpy->driver_name);
869
870
return strdup(ext->xml);
871
}
872
873
874
void
875
dri2_setup_screen(_EGLDisplay *disp)
876
{
877
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
878
unsigned int api_mask;
879
880
/*
881
* EGL 1.5 specification defines the default value to 1. Moreover,
882
* eglSwapInterval() is required to clamp requested value to the supported
883
* range. Since the default value is implicitly assumed to be supported,
884
* use it as both minimum and maximum for the platforms that do not allow
885
* changing the interval. Platforms, which allow it (e.g. x11, wayland)
886
* override these values already.
887
*/
888
dri2_dpy->min_swap_interval = 1;
889
dri2_dpy->max_swap_interval = 1;
890
dri2_dpy->default_swap_interval = 1;
891
892
if (dri2_dpy->image_driver) {
893
api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
894
} else if (dri2_dpy->dri2) {
895
api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
896
} else {
897
assert(dri2_dpy->swrast);
898
api_mask = 1 << __DRI_API_OPENGL |
899
1 << __DRI_API_GLES |
900
1 << __DRI_API_GLES2 |
901
1 << __DRI_API_GLES3;
902
}
903
904
disp->ClientAPIs = 0;
905
if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
906
disp->ClientAPIs |= EGL_OPENGL_BIT;
907
if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
908
disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
909
if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
910
disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
911
if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
912
disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
913
914
assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
915
disp->Extensions.KHR_no_config_context = EGL_TRUE;
916
disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
917
918
if (dri2_dpy->configOptions) {
919
disp->Extensions.MESA_query_driver = EGL_TRUE;
920
}
921
922
/* Report back to EGL the bitmask of priorities supported */
923
disp->Extensions.IMG_context_priority =
924
dri2_renderer_query_integer(dri2_dpy,
925
__DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
926
927
disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
928
929
if (dri2_renderer_query_integer(dri2_dpy,
930
__DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
931
disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
932
933
if (dri2_dpy->image_driver ||
934
(dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
935
(dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
936
disp->Extensions.KHR_create_context = EGL_TRUE;
937
938
if (dri2_dpy->robustness)
939
disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
940
}
941
942
if (dri2_dpy->no_error)
943
disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
944
945
if (dri2_dpy->fence) {
946
disp->Extensions.KHR_fence_sync = EGL_TRUE;
947
disp->Extensions.KHR_wait_sync = EGL_TRUE;
948
if (dri2_dpy->fence->get_fence_from_cl_event)
949
disp->Extensions.KHR_cl_event2 = EGL_TRUE;
950
if (dri2_dpy->fence->base.version >= 2 &&
951
dri2_dpy->fence->get_capabilities) {
952
unsigned capabilities =
953
dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
954
disp->Extensions.ANDROID_native_fence_sync =
955
(capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
956
}
957
}
958
959
if (dri2_dpy->blob)
960
disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
961
962
disp->Extensions.KHR_reusable_sync = EGL_TRUE;
963
964
if (dri2_dpy->image) {
965
if (dri2_dpy->image->base.version >= 10 &&
966
dri2_dpy->image->getCapabilities != NULL) {
967
int capabilities;
968
969
capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
970
disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
971
972
if (dri2_dpy->image->base.version >= 11)
973
disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
974
} else {
975
disp->Extensions.MESA_drm_image = EGL_TRUE;
976
if (dri2_dpy->image->base.version >= 11)
977
disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
978
}
979
980
disp->Extensions.KHR_image_base = EGL_TRUE;
981
disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
982
if (dri2_dpy->image->base.version >= 5 &&
983
dri2_dpy->image->createImageFromTexture) {
984
disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
985
disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
986
987
if (dri2_renderer_query_integer(dri2_dpy,
988
__DRI2_RENDERER_HAS_TEXTURE_3D))
989
disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
990
}
991
#ifdef HAVE_LIBDRM
992
if (dri2_dpy->image->base.version >= 8 &&
993
dri2_dpy->image->createImageFromDmaBufs) {
994
disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
995
disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
996
}
997
#endif
998
}
999
1000
if (dri2_dpy->flush_control)
1001
disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1002
1003
if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1004
disp->Extensions.KHR_partial_update = EGL_TRUE;
1005
1006
disp->Extensions.EXT_protected_surface =
1007
dri2_renderer_query_integer(dri2_dpy,
1008
__DRI2_RENDERER_HAS_PROTECTED_CONTENT);
1009
}
1010
1011
void
1012
dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1013
{
1014
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1015
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1016
1017
/* Allow driconf to override applications.*/
1018
if (dri2_dpy->config)
1019
dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1020
"vblank_mode", &vblank_mode);
1021
switch (vblank_mode) {
1022
case DRI_CONF_VBLANK_NEVER:
1023
dri2_dpy->min_swap_interval = 0;
1024
dri2_dpy->max_swap_interval = 0;
1025
dri2_dpy->default_swap_interval = 0;
1026
break;
1027
case DRI_CONF_VBLANK_ALWAYS_SYNC:
1028
dri2_dpy->min_swap_interval = 1;
1029
dri2_dpy->max_swap_interval = max_swap_interval;
1030
dri2_dpy->default_swap_interval = 1;
1031
break;
1032
case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1033
dri2_dpy->min_swap_interval = 0;
1034
dri2_dpy->max_swap_interval = max_swap_interval;
1035
dri2_dpy->default_swap_interval = 0;
1036
break;
1037
default:
1038
case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1039
dri2_dpy->min_swap_interval = 0;
1040
dri2_dpy->max_swap_interval = max_swap_interval;
1041
dri2_dpy->default_swap_interval = 1;
1042
break;
1043
}
1044
}
1045
1046
/* All platforms but DRM call this function to create the screen and populate
1047
* the driver_configs. DRM inherits that information from its display - GBM.
1048
*/
1049
EGLBoolean
1050
dri2_create_screen(_EGLDisplay *disp)
1051
{
1052
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1053
1054
if (dri2_dpy->image_driver) {
1055
dri2_dpy->dri_screen =
1056
dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1057
dri2_dpy->loader_extensions,
1058
dri2_dpy->driver_extensions,
1059
&dri2_dpy->driver_configs,
1060
disp);
1061
} else if (dri2_dpy->dri2) {
1062
if (dri2_dpy->dri2->base.version >= 4) {
1063
dri2_dpy->dri_screen =
1064
dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1065
dri2_dpy->loader_extensions,
1066
dri2_dpy->driver_extensions,
1067
&dri2_dpy->driver_configs, disp);
1068
} else {
1069
dri2_dpy->dri_screen =
1070
dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1071
dri2_dpy->loader_extensions,
1072
&dri2_dpy->driver_configs, disp);
1073
}
1074
} else {
1075
assert(dri2_dpy->swrast);
1076
if (dri2_dpy->swrast->base.version >= 4) {
1077
dri2_dpy->dri_screen =
1078
dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1079
dri2_dpy->driver_extensions,
1080
&dri2_dpy->driver_configs, disp);
1081
} else {
1082
dri2_dpy->dri_screen =
1083
dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1084
&dri2_dpy->driver_configs, disp);
1085
}
1086
}
1087
1088
if (dri2_dpy->dri_screen == NULL) {
1089
_eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1090
return EGL_FALSE;
1091
}
1092
1093
dri2_dpy->own_dri_screen = true;
1094
return EGL_TRUE;
1095
}
1096
1097
EGLBoolean
1098
dri2_setup_extensions(_EGLDisplay *disp)
1099
{
1100
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1101
const struct dri2_extension_match *mandatory_core_extensions;
1102
const __DRIextension **extensions;
1103
1104
extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1105
1106
if (dri2_dpy->image_driver || dri2_dpy->dri2)
1107
mandatory_core_extensions = dri2_core_extensions;
1108
else
1109
mandatory_core_extensions = swrast_core_extensions;
1110
1111
if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1112
return EGL_FALSE;
1113
1114
#ifdef HAVE_DRI3_MODIFIERS
1115
dri2_dpy->multibuffers_available =
1116
(dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1117
dri2_dpy->dri3_minor_version >= 2)) &&
1118
(dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1119
dri2_dpy->present_minor_version >= 2)) &&
1120
(dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1121
#endif
1122
1123
dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1124
return EGL_TRUE;
1125
}
1126
1127
/**
1128
* Called via eglInitialize(), drv->Initialize().
1129
*
1130
* This must be guaranteed to be called exactly once, even if eglInitialize is
1131
* called many times (without a eglTerminate in between).
1132
*/
1133
static EGLBoolean
1134
dri2_initialize(_EGLDisplay *disp)
1135
{
1136
EGLBoolean ret = EGL_FALSE;
1137
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1138
1139
/* In the case where the application calls eglMakeCurrent(context1),
1140
* eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1141
* or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1142
* initialized, as we need it to be able to free context1 correctly.
1143
*
1144
* It would probably be safest to forcibly release the display with
1145
* dri2_display_release, to make sure the display is reinitialized correctly.
1146
* However, the EGL spec states that we need to keep a reference to the
1147
* current context (so we cannot call dri2_make_current(NULL)), and therefore
1148
* we would leak context1 as we would be missing the old display connection
1149
* to free it up correctly.
1150
*/
1151
if (dri2_dpy) {
1152
dri2_dpy->ref_count++;
1153
return EGL_TRUE;
1154
}
1155
1156
loader_set_logger(_eglLog);
1157
1158
switch (disp->Platform) {
1159
case _EGL_PLATFORM_SURFACELESS:
1160
ret = dri2_initialize_surfaceless(disp);
1161
break;
1162
case _EGL_PLATFORM_DEVICE:
1163
ret = dri2_initialize_device(disp);
1164
break;
1165
case _EGL_PLATFORM_X11:
1166
case _EGL_PLATFORM_XCB:
1167
ret = dri2_initialize_x11(disp);
1168
break;
1169
case _EGL_PLATFORM_DRM:
1170
ret = dri2_initialize_drm(disp);
1171
break;
1172
case _EGL_PLATFORM_WAYLAND:
1173
ret = dri2_initialize_wayland(disp);
1174
break;
1175
case _EGL_PLATFORM_ANDROID:
1176
ret = dri2_initialize_android(disp);
1177
break;
1178
default:
1179
unreachable("Callers ensure we cannot get here.");
1180
return EGL_FALSE;
1181
}
1182
1183
if (!ret)
1184
return EGL_FALSE;
1185
1186
dri2_dpy = dri2_egl_display(disp);
1187
dri2_dpy->ref_count++;
1188
1189
return EGL_TRUE;
1190
}
1191
1192
/**
1193
* Decrement display reference count, and free up display if necessary.
1194
*/
1195
static void
1196
dri2_display_release(_EGLDisplay *disp)
1197
{
1198
struct dri2_egl_display *dri2_dpy;
1199
1200
if (!disp)
1201
return;
1202
1203
dri2_dpy = dri2_egl_display(disp);
1204
1205
assert(dri2_dpy->ref_count > 0);
1206
dri2_dpy->ref_count--;
1207
1208
if (dri2_dpy->ref_count > 0)
1209
return;
1210
1211
_eglCleanupDisplay(disp);
1212
dri2_display_destroy(disp);
1213
}
1214
1215
void
1216
dri2_display_destroy(_EGLDisplay *disp)
1217
{
1218
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1219
1220
if (dri2_dpy->own_dri_screen) {
1221
if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1222
dri2_dpy->vtbl->close_screen_notify(disp);
1223
dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1224
}
1225
if (dri2_dpy->fd >= 0)
1226
close(dri2_dpy->fd);
1227
1228
/* Don't dlclose the driver when building with the address sanitizer, so you
1229
* get good symbols from the leak reports.
1230
*/
1231
#if !BUILT_WITH_ASAN || defined(NDEBUG)
1232
if (dri2_dpy->driver)
1233
dlclose(dri2_dpy->driver);
1234
#endif
1235
1236
free(dri2_dpy->driver_name);
1237
1238
#ifdef HAVE_WAYLAND_PLATFORM
1239
free(dri2_dpy->device_name);
1240
#endif
1241
1242
switch (disp->Platform) {
1243
case _EGL_PLATFORM_X11:
1244
dri2_teardown_x11(dri2_dpy);
1245
break;
1246
case _EGL_PLATFORM_DRM:
1247
dri2_teardown_drm(dri2_dpy);
1248
break;
1249
case _EGL_PLATFORM_WAYLAND:
1250
dri2_teardown_wayland(dri2_dpy);
1251
break;
1252
default:
1253
/* TODO: add teardown for other platforms */
1254
break;
1255
}
1256
1257
/* The drm platform does not create the screen/driver_configs but reuses
1258
* the ones from the gbm device. As such the gbm itself is responsible
1259
* for the cleanup.
1260
*/
1261
if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1262
for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1263
free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1264
free(dri2_dpy->driver_configs);
1265
}
1266
free(dri2_dpy);
1267
disp->DriverData = NULL;
1268
}
1269
1270
__DRIbuffer *
1271
dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1272
unsigned int att, unsigned int format)
1273
{
1274
struct dri2_egl_display *dri2_dpy =
1275
dri2_egl_display(dri2_surf->base.Resource.Display);
1276
1277
if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1278
return NULL;
1279
1280
if (!dri2_surf->local_buffers[att]) {
1281
dri2_surf->local_buffers[att] =
1282
dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1283
dri2_surf->base.Width, dri2_surf->base.Height);
1284
}
1285
1286
return dri2_surf->local_buffers[att];
1287
}
1288
1289
void
1290
dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1291
{
1292
struct dri2_egl_display *dri2_dpy =
1293
dri2_egl_display(dri2_surf->base.Resource.Display);
1294
1295
for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1296
if (dri2_surf->local_buffers[i]) {
1297
dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1298
dri2_surf->local_buffers[i]);
1299
dri2_surf->local_buffers[i] = NULL;
1300
}
1301
}
1302
}
1303
1304
/**
1305
* Called via eglTerminate(), drv->Terminate().
1306
*
1307
* This must be guaranteed to be called exactly once, even if eglTerminate is
1308
* called many times (without a eglInitialize in between).
1309
*/
1310
static EGLBoolean
1311
dri2_terminate(_EGLDisplay *disp)
1312
{
1313
/* Release all non-current Context/Surfaces. */
1314
_eglReleaseDisplayResources(disp);
1315
1316
dri2_display_release(disp);
1317
1318
return EGL_TRUE;
1319
}
1320
1321
/**
1322
* Set the error code after a call to
1323
* dri2_egl_display::dri2::createContextAttribs.
1324
*/
1325
static void
1326
dri2_create_context_attribs_error(int dri_error)
1327
{
1328
EGLint egl_error;
1329
1330
switch (dri_error) {
1331
case __DRI_CTX_ERROR_SUCCESS:
1332
return;
1333
1334
case __DRI_CTX_ERROR_NO_MEMORY:
1335
egl_error = EGL_BAD_ALLOC;
1336
break;
1337
1338
/* From the EGL_KHR_create_context spec, section "Errors":
1339
*
1340
* * If <config> does not support a client API context compatible
1341
* with the requested API major and minor version, [...] context flags,
1342
* and context reset notification behavior (for client API types where
1343
* these attributes are supported), then an EGL_BAD_MATCH error is
1344
* generated.
1345
*
1346
* * If an OpenGL ES context is requested and the values for
1347
* attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1348
* EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1349
* is not defined, than an EGL_BAD_MATCH error is generated.
1350
*
1351
* * If an OpenGL context is requested, the requested version is
1352
* greater than 3.2, and the value for attribute
1353
* EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1354
* bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1355
* EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1356
* one of these bits set; or if the implementation does not support
1357
* the requested profile, then an EGL_BAD_MATCH error is generated.
1358
*/
1359
case __DRI_CTX_ERROR_BAD_API:
1360
case __DRI_CTX_ERROR_BAD_VERSION:
1361
case __DRI_CTX_ERROR_BAD_FLAG:
1362
egl_error = EGL_BAD_MATCH;
1363
break;
1364
1365
/* From the EGL_KHR_create_context spec, section "Errors":
1366
*
1367
* * If an attribute name or attribute value in <attrib_list> is not
1368
* recognized (including unrecognized bits in bitmask attributes),
1369
* then an EGL_BAD_ATTRIBUTE error is generated."
1370
*/
1371
case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1372
case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1373
egl_error = EGL_BAD_ATTRIBUTE;
1374
break;
1375
1376
default:
1377
assert(!"unknown dri_error code");
1378
egl_error = EGL_BAD_MATCH;
1379
break;
1380
}
1381
1382
_eglError(egl_error, "dri2_create_context");
1383
}
1384
1385
static bool
1386
dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1387
struct dri2_egl_display *dri2_dpy,
1388
uint32_t *ctx_attribs,
1389
unsigned *num_attribs)
1390
{
1391
int pos = 0;
1392
1393
assert(*num_attribs >= NUM_ATTRIBS);
1394
1395
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1396
ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1397
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1398
ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1399
1400
if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
1401
/* If the implementation doesn't support the __DRI2_ROBUSTNESS
1402
* extension, don't even try to send it the robust-access flag.
1403
* It may explode. Instead, generate the required EGL error here.
1404
*/
1405
if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1406
&& !dri2_dpy->robustness) {
1407
_eglError(EGL_BAD_MATCH, "eglCreateContext");
1408
return false;
1409
}
1410
1411
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1412
ctx_attribs[pos++] = dri2_ctx->base.Flags |
1413
(dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
1414
}
1415
1416
if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1417
/* If the implementation doesn't support the __DRI2_ROBUSTNESS
1418
* extension, don't even try to send it a reset strategy. It may
1419
* explode. Instead, generate the required EGL error here.
1420
*/
1421
if (!dri2_dpy->robustness) {
1422
_eglError(EGL_BAD_CONFIG, "eglCreateContext");
1423
return false;
1424
}
1425
1426
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1427
ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1428
}
1429
1430
if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1431
unsigned val;
1432
1433
switch (dri2_ctx->base.ContextPriority) {
1434
case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1435
val = __DRI_CTX_PRIORITY_HIGH;
1436
break;
1437
case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1438
val = __DRI_CTX_PRIORITY_MEDIUM;
1439
break;
1440
case EGL_CONTEXT_PRIORITY_LOW_IMG:
1441
val = __DRI_CTX_PRIORITY_LOW;
1442
break;
1443
default:
1444
_eglError(EGL_BAD_CONFIG, "eglCreateContext");
1445
return false;
1446
}
1447
1448
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1449
ctx_attribs[pos++] = val;
1450
}
1451
1452
if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1453
ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1454
ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1455
}
1456
1457
*num_attribs = pos;
1458
1459
return true;
1460
}
1461
1462
/**
1463
* Called via eglCreateContext(), drv->CreateContext().
1464
*/
1465
static _EGLContext *
1466
dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1467
_EGLContext *share_list, const EGLint *attrib_list)
1468
{
1469
struct dri2_egl_context *dri2_ctx;
1470
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1471
struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1472
__DRIcontext *shared =
1473
dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1474
struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1475
const __DRIconfig *dri_config;
1476
int api;
1477
unsigned error;
1478
unsigned num_attribs = NUM_ATTRIBS;
1479
uint32_t ctx_attribs[NUM_ATTRIBS];
1480
1481
dri2_ctx = malloc(sizeof *dri2_ctx);
1482
if (!dri2_ctx) {
1483
_eglError(EGL_BAD_ALLOC, "eglCreateContext");
1484
return NULL;
1485
}
1486
1487
if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1488
goto cleanup;
1489
1490
/* The EGL_EXT_create_context_robustness spec says:
1491
*
1492
* "Add to the eglCreateContext context creation errors: [...]
1493
*
1494
* * If the reset notification behavior of <share_context> and the
1495
* newly created context are different then an EGL_BAD_MATCH error is
1496
* generated."
1497
*/
1498
if (share_list && share_list->ResetNotificationStrategy !=
1499
dri2_ctx->base.ResetNotificationStrategy) {
1500
_eglError(EGL_BAD_MATCH, "eglCreateContext");
1501
goto cleanup;
1502
}
1503
1504
/* The EGL_KHR_create_context_no_error spec says:
1505
*
1506
* "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1507
* used to create <share_context> does not match the value of
1508
* EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1509
*/
1510
if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1511
_eglError(EGL_BAD_MATCH, "eglCreateContext");
1512
goto cleanup;
1513
}
1514
1515
switch (dri2_ctx->base.ClientAPI) {
1516
case EGL_OPENGL_ES_API:
1517
switch (dri2_ctx->base.ClientMajorVersion) {
1518
case 1:
1519
api = __DRI_API_GLES;
1520
break;
1521
case 2:
1522
api = __DRI_API_GLES2;
1523
break;
1524
case 3:
1525
api = __DRI_API_GLES3;
1526
break;
1527
default:
1528
_eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1529
free(dri2_ctx);
1530
return NULL;
1531
}
1532
break;
1533
case EGL_OPENGL_API:
1534
if ((dri2_ctx->base.ClientMajorVersion >= 4
1535
|| (dri2_ctx->base.ClientMajorVersion == 3
1536
&& dri2_ctx->base.ClientMinorVersion >= 2))
1537
&& dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1538
api = __DRI_API_OPENGL_CORE;
1539
else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1540
dri2_ctx->base.ClientMinorVersion == 1)
1541
api = __DRI_API_OPENGL_CORE;
1542
else
1543
api = __DRI_API_OPENGL;
1544
break;
1545
default:
1546
_eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1547
free(dri2_ctx);
1548
return NULL;
1549
}
1550
1551
if (conf != NULL) {
1552
/* The config chosen here isn't necessarily
1553
* used for surfaces later.
1554
* A pixmap surface will use the single config.
1555
* This opportunity depends on disabling the
1556
* doubleBufferMode check in
1557
* src/mesa/main/context.c:check_compatible()
1558
*/
1559
if (dri2_config->dri_config[1][0])
1560
dri_config = dri2_config->dri_config[1][0];
1561
else
1562
dri_config = dri2_config->dri_config[0][0];
1563
}
1564
else
1565
dri_config = NULL;
1566
1567
if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1568
&num_attribs))
1569
goto cleanup;
1570
1571
if (dri2_dpy->image_driver) {
1572
dri2_ctx->dri_context =
1573
dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1574
api,
1575
dri_config,
1576
shared,
1577
num_attribs / 2,
1578
ctx_attribs,
1579
& error,
1580
dri2_ctx);
1581
dri2_create_context_attribs_error(error);
1582
} else if (dri2_dpy->dri2) {
1583
if (dri2_dpy->dri2->base.version >= 3) {
1584
dri2_ctx->dri_context =
1585
dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1586
api,
1587
dri_config,
1588
shared,
1589
num_attribs / 2,
1590
ctx_attribs,
1591
& error,
1592
dri2_ctx);
1593
dri2_create_context_attribs_error(error);
1594
} else {
1595
dri2_ctx->dri_context =
1596
dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1597
api,
1598
dri_config,
1599
shared,
1600
dri2_ctx);
1601
}
1602
} else {
1603
assert(dri2_dpy->swrast);
1604
if (dri2_dpy->swrast->base.version >= 3) {
1605
dri2_ctx->dri_context =
1606
dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1607
api,
1608
dri_config,
1609
shared,
1610
num_attribs / 2,
1611
ctx_attribs,
1612
& error,
1613
dri2_ctx);
1614
dri2_create_context_attribs_error(error);
1615
} else {
1616
dri2_ctx->dri_context =
1617
dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1618
api,
1619
dri_config,
1620
shared,
1621
dri2_ctx);
1622
}
1623
}
1624
1625
if (!dri2_ctx->dri_context)
1626
goto cleanup;
1627
1628
return &dri2_ctx->base;
1629
1630
cleanup:
1631
free(dri2_ctx);
1632
return NULL;
1633
}
1634
1635
/**
1636
* Called via eglDestroyContext(), drv->DestroyContext().
1637
*/
1638
static EGLBoolean
1639
dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1640
{
1641
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1642
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1643
1644
if (_eglPutContext(ctx)) {
1645
dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1646
free(dri2_ctx);
1647
}
1648
1649
return EGL_TRUE;
1650
}
1651
1652
EGLBoolean
1653
dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1654
_EGLConfig *conf, const EGLint *attrib_list,
1655
EGLBoolean enable_out_fence, void *native_surface)
1656
{
1657
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1658
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1659
1660
dri2_surf->out_fence_fd = -1;
1661
dri2_surf->enable_out_fence = false;
1662
if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1663
dri2_dpy->fence->get_capabilities &&
1664
(dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1665
__DRI_FENCE_CAP_NATIVE_FD)) {
1666
dri2_surf->enable_out_fence = enable_out_fence;
1667
}
1668
1669
return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1670
}
1671
1672
static void
1673
dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1674
{
1675
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1676
1677
if (dri2_surf->out_fence_fd >= 0)
1678
close(dri2_surf->out_fence_fd);
1679
1680
dri2_surf->out_fence_fd = fence_fd;
1681
}
1682
1683
void
1684
dri2_fini_surface(_EGLSurface *surf)
1685
{
1686
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1687
1688
dri2_surface_set_out_fence_fd(surf, -1);
1689
dri2_surf->enable_out_fence = false;
1690
}
1691
1692
static EGLBoolean
1693
dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1694
{
1695
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1696
1697
if (!_eglPutSurface(surf))
1698
return EGL_TRUE;
1699
1700
return dri2_dpy->vtbl->destroy_surface(disp, surf);
1701
}
1702
1703
static void
1704
dri2_surf_update_fence_fd(_EGLContext *ctx,
1705
_EGLDisplay *disp, _EGLSurface *surf)
1706
{
1707
__DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1708
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1709
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1710
int fence_fd = -1;
1711
void *fence;
1712
1713
if (!dri2_surf->enable_out_fence)
1714
return;
1715
1716
fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1717
if (fence) {
1718
fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1719
fence);
1720
dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1721
}
1722
dri2_surface_set_out_fence_fd(surf, fence_fd);
1723
}
1724
1725
EGLBoolean
1726
dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1727
const __DRIconfig *config,
1728
struct dri2_egl_surface *dri2_surf,
1729
void *loaderPrivate)
1730
{
1731
__DRIcreateNewDrawableFunc createNewDrawable;
1732
1733
if (dri2_dpy->image_driver)
1734
createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1735
else if (dri2_dpy->dri2)
1736
createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1737
else if (dri2_dpy->swrast)
1738
createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1739
else
1740
return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1741
1742
dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1743
config, loaderPrivate);
1744
if (dri2_surf->dri_drawable == NULL)
1745
return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1746
1747
return EGL_TRUE;
1748
}
1749
1750
/**
1751
* Called via eglMakeCurrent(), drv->MakeCurrent().
1752
*/
1753
static EGLBoolean
1754
dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1755
_EGLSurface *rsurf, _EGLContext *ctx)
1756
{
1757
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1758
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1759
_EGLDisplay *old_disp = NULL;
1760
struct dri2_egl_display *old_dri2_dpy = NULL;
1761
_EGLContext *old_ctx;
1762
_EGLSurface *old_dsurf, *old_rsurf;
1763
_EGLSurface *tmp_dsurf, *tmp_rsurf;
1764
__DRIdrawable *ddraw, *rdraw;
1765
__DRIcontext *cctx;
1766
EGLint egl_error = EGL_SUCCESS;
1767
1768
if (!dri2_dpy)
1769
return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1770
1771
/* make new bindings, set the EGL error otherwise */
1772
if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1773
return EGL_FALSE;
1774
1775
if (old_ctx) {
1776
__DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1777
old_disp = old_ctx->Resource.Display;
1778
old_dri2_dpy = dri2_egl_display(old_disp);
1779
1780
/* flush before context switch */
1781
dri2_gl_flush();
1782
1783
if (old_dsurf)
1784
dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1785
1786
/* Disable shared buffer mode */
1787
if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1788
old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1789
old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1790
}
1791
1792
dri2_dpy->core->unbindContext(old_cctx);
1793
}
1794
1795
ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1796
rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1797
cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1798
1799
if (cctx || ddraw || rdraw) {
1800
if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1801
_EGLContext *tmp_ctx;
1802
1803
/* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1804
* setting the error to EGL_BAD_MATCH is surely better than leaving it
1805
* as EGL_SUCCESS.
1806
*/
1807
egl_error = EGL_BAD_MATCH;
1808
1809
/* undo the previous _eglBindContext */
1810
_eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1811
assert(&dri2_ctx->base == ctx &&
1812
tmp_dsurf == dsurf &&
1813
tmp_rsurf == rsurf);
1814
1815
_eglPutSurface(dsurf);
1816
_eglPutSurface(rsurf);
1817
_eglPutContext(ctx);
1818
1819
_eglPutSurface(old_dsurf);
1820
_eglPutSurface(old_rsurf);
1821
_eglPutContext(old_ctx);
1822
1823
ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1824
rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1825
cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1826
1827
/* undo the previous dri2_dpy->core->unbindContext */
1828
if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1829
if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1830
old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1831
old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1832
}
1833
1834
return _eglError(egl_error, "eglMakeCurrent");
1835
}
1836
1837
/* We cannot restore the same state as it was before calling
1838
* eglMakeCurrent() and the spec isn't clear about what to do. We
1839
* can prevent EGL from calling into the DRI driver with no DRI
1840
* context bound.
1841
*/
1842
dsurf = rsurf = NULL;
1843
ctx = NULL;
1844
1845
_eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1846
assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1847
tmp_rsurf == old_rsurf);
1848
1849
_eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1850
} else {
1851
/* dri2_dpy->core->bindContext succeeded, so take a reference on the
1852
* dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1853
* EGLDisplay is terminated and then initialized again while a
1854
* context is still bound. See dri2_intitialize() for a more in depth
1855
* explanation. */
1856
dri2_dpy->ref_count++;
1857
}
1858
}
1859
1860
dri2_destroy_surface(disp, old_dsurf);
1861
dri2_destroy_surface(disp, old_rsurf);
1862
1863
if (old_ctx) {
1864
dri2_destroy_context(disp, old_ctx);
1865
dri2_display_release(old_disp);
1866
}
1867
1868
if (egl_error != EGL_SUCCESS)
1869
return _eglError(egl_error, "eglMakeCurrent");
1870
1871
if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1872
dri2_dpy->vtbl->set_shared_buffer_mode) {
1873
/* Always update the shared buffer mode. This is obviously needed when
1874
* the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1875
* EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1876
* case where external non-EGL API may have changed window's shared
1877
* buffer mode since we last saw it.
1878
*/
1879
bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1880
dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1881
}
1882
1883
return EGL_TRUE;
1884
}
1885
1886
__DRIdrawable *
1887
dri2_surface_get_dri_drawable(_EGLSurface *surf)
1888
{
1889
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1890
1891
return dri2_surf->dri_drawable;
1892
}
1893
1894
/*
1895
* Called from eglGetProcAddress() via drv->GetProcAddress().
1896
*/
1897
static _EGLProc
1898
dri2_get_proc_address(const char *procname)
1899
{
1900
return _glapi_get_proc_address(procname);
1901
}
1902
1903
static _EGLSurface*
1904
dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1905
void *native_window, const EGLint *attrib_list)
1906
{
1907
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1908
return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1909
attrib_list);
1910
}
1911
1912
static _EGLSurface*
1913
dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1914
void *native_pixmap, const EGLint *attrib_list)
1915
{
1916
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1917
if (!dri2_dpy->vtbl->create_pixmap_surface)
1918
return NULL;
1919
return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1920
attrib_list);
1921
}
1922
1923
static _EGLSurface*
1924
dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1925
const EGLint *attrib_list)
1926
{
1927
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1928
if (!dri2_dpy->vtbl->create_pbuffer_surface)
1929
return NULL;
1930
return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1931
}
1932
1933
static EGLBoolean
1934
dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1935
{
1936
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1937
if (!dri2_dpy->vtbl->swap_interval)
1938
return EGL_TRUE;
1939
return dri2_dpy->vtbl->swap_interval(disp, surf, interval);
1940
}
1941
1942
/**
1943
* Asks the client API to flush any rendering to the drawable so that we can
1944
* do our swapbuffers.
1945
*/
1946
void
1947
dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1948
{
1949
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1950
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1951
1952
if (dri2_dpy->flush) {
1953
if (dri2_dpy->flush->base.version >= 4) {
1954
/* We know there's a current context because:
1955
*
1956
* "If surface is not bound to the calling thread’s current
1957
* context, an EGL_BAD_SURFACE error is generated."
1958
*/
1959
_EGLContext *ctx = _eglGetCurrentContext();
1960
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1961
1962
/* From the EGL 1.4 spec (page 52):
1963
*
1964
* "The contents of ancillary buffers are always undefined
1965
* after calling eglSwapBuffers."
1966
*/
1967
dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1968
dri_drawable,
1969
__DRI2_FLUSH_DRAWABLE |
1970
__DRI2_FLUSH_INVALIDATE_ANCILLARY,
1971
__DRI2_THROTTLE_SWAPBUFFER);
1972
} else {
1973
dri2_dpy->flush->flush(dri_drawable);
1974
}
1975
}
1976
}
1977
1978
static EGLBoolean
1979
dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
1980
{
1981
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1982
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1983
_EGLContext *ctx = _eglGetCurrentContext();
1984
EGLBoolean ret;
1985
1986
if (ctx && surf)
1987
dri2_surf_update_fence_fd(ctx, disp, surf);
1988
ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
1989
1990
/* SwapBuffers marks the end of the frame; reset the damage region for
1991
* use again next time.
1992
*/
1993
if (ret && dri2_dpy->buffer_damage &&
1994
dri2_dpy->buffer_damage->set_damage_region)
1995
dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
1996
1997
return ret;
1998
}
1999
2000
static EGLBoolean
2001
dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2002
const EGLint *rects, EGLint n_rects)
2003
{
2004
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2005
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2006
_EGLContext *ctx = _eglGetCurrentContext();
2007
EGLBoolean ret;
2008
2009
if (ctx && surf)
2010
dri2_surf_update_fence_fd(ctx, disp, surf);
2011
if (dri2_dpy->vtbl->swap_buffers_with_damage)
2012
ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2013
rects, n_rects);
2014
else
2015
ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2016
2017
/* SwapBuffers marks the end of the frame; reset the damage region for
2018
* use again next time.
2019
*/
2020
if (ret && dri2_dpy->buffer_damage &&
2021
dri2_dpy->buffer_damage->set_damage_region)
2022
dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2023
2024
return ret;
2025
}
2026
2027
static EGLBoolean
2028
dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2029
EGLint numRects, const EGLint *rects)
2030
{
2031
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2032
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2033
EGLBoolean ret;
2034
2035
if (!dri2_dpy->vtbl->swap_buffers_region)
2036
return EGL_FALSE;
2037
ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2038
2039
/* SwapBuffers marks the end of the frame; reset the damage region for
2040
* use again next time.
2041
*/
2042
if (ret && dri2_dpy->buffer_damage &&
2043
dri2_dpy->buffer_damage->set_damage_region)
2044
dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2045
2046
return ret;
2047
}
2048
2049
static EGLBoolean
2050
dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2051
EGLint *rects, EGLint n_rects)
2052
{
2053
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2054
__DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2055
2056
if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2057
return EGL_FALSE;
2058
2059
dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2060
return EGL_TRUE;
2061
}
2062
2063
static EGLBoolean
2064
dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2065
EGLint x, EGLint y, EGLint width, EGLint height)
2066
{
2067
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2068
if (!dri2_dpy->vtbl->post_sub_buffer)
2069
return EGL_FALSE;
2070
return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2071
}
2072
2073
static EGLBoolean
2074
dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2075
{
2076
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2077
if (!dri2_dpy->vtbl->copy_buffers)
2078
return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2079
return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2080
}
2081
2082
static EGLint
2083
dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2084
{
2085
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2086
if (!dri2_dpy->vtbl->query_buffer_age)
2087
return 0;
2088
return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2089
}
2090
2091
static EGLBoolean
2092
dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2093
{
2094
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2095
_EGLSurface *surf = ctx->DrawSurface;
2096
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2097
2098
/* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2099
* we need to copy fake to real here.*/
2100
2101
if (dri2_dpy->flush != NULL)
2102
dri2_dpy->flush->flush(dri_drawable);
2103
2104
return EGL_TRUE;
2105
}
2106
2107
static EGLBoolean
2108
dri2_wait_native(EGLint engine)
2109
{
2110
if (engine != EGL_CORE_NATIVE_ENGINE)
2111
return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2112
/* glXWaitX(); */
2113
2114
return EGL_TRUE;
2115
}
2116
2117
static EGLBoolean
2118
dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2119
{
2120
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2121
struct dri2_egl_context *dri2_ctx;
2122
_EGLContext *ctx;
2123
GLint format, target;
2124
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2125
2126
ctx = _eglGetCurrentContext();
2127
dri2_ctx = dri2_egl_context(ctx);
2128
2129
if (!_eglBindTexImage(disp, surf, buffer))
2130
return EGL_FALSE;
2131
2132
switch (surf->TextureFormat) {
2133
case EGL_TEXTURE_RGB:
2134
format = __DRI_TEXTURE_FORMAT_RGB;
2135
break;
2136
case EGL_TEXTURE_RGBA:
2137
format = __DRI_TEXTURE_FORMAT_RGBA;
2138
break;
2139
default:
2140
assert(!"Unexpected texture format in dri2_bind_tex_image()");
2141
format = __DRI_TEXTURE_FORMAT_RGBA;
2142
}
2143
2144
switch (surf->TextureTarget) {
2145
case EGL_TEXTURE_2D:
2146
target = GL_TEXTURE_2D;
2147
break;
2148
default:
2149
target = GL_TEXTURE_2D;
2150
assert(!"Unexpected texture target in dri2_bind_tex_image()");
2151
}
2152
2153
dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2154
target, format,
2155
dri_drawable);
2156
2157
return EGL_TRUE;
2158
}
2159
2160
static EGLBoolean
2161
dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2162
{
2163
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2164
struct dri2_egl_context *dri2_ctx;
2165
_EGLContext *ctx;
2166
GLint target;
2167
__DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2168
2169
ctx = _eglGetCurrentContext();
2170
dri2_ctx = dri2_egl_context(ctx);
2171
2172
if (!_eglReleaseTexImage(disp, surf, buffer))
2173
return EGL_FALSE;
2174
2175
switch (surf->TextureTarget) {
2176
case EGL_TEXTURE_2D:
2177
target = GL_TEXTURE_2D;
2178
break;
2179
default:
2180
assert(!"missing texture target");
2181
}
2182
2183
if (dri2_dpy->tex_buffer->base.version >= 3 &&
2184
dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2185
dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2186
target, dri_drawable);
2187
}
2188
2189
return EGL_TRUE;
2190
}
2191
2192
static _EGLImage*
2193
dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2194
EGLClientBuffer buffer, const EGLint *attr_list)
2195
{
2196
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2197
return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2198
attr_list);
2199
}
2200
2201
_EGLImage *
2202
dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2203
{
2204
struct dri2_egl_image *dri2_img;
2205
2206
if (dri_image == NULL) {
2207
_eglError(EGL_BAD_ALLOC, "dri2_create_image");
2208
return NULL;
2209
}
2210
2211
dri2_img = malloc(sizeof *dri2_img);
2212
if (!dri2_img) {
2213
_eglError(EGL_BAD_ALLOC, "dri2_create_image");
2214
return NULL;
2215
}
2216
2217
_eglInitImage(&dri2_img->base, disp);
2218
2219
dri2_img->dri_image = dri_image;
2220
2221
return &dri2_img->base;
2222
}
2223
2224
/**
2225
* Translate a DRI Image extension error code into an EGL error code.
2226
*/
2227
static EGLint
2228
egl_error_from_dri_image_error(int dri_error)
2229
{
2230
switch (dri_error) {
2231
case __DRI_IMAGE_ERROR_SUCCESS:
2232
return EGL_SUCCESS;
2233
case __DRI_IMAGE_ERROR_BAD_ALLOC:
2234
return EGL_BAD_ALLOC;
2235
case __DRI_IMAGE_ERROR_BAD_MATCH:
2236
return EGL_BAD_MATCH;
2237
case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2238
return EGL_BAD_PARAMETER;
2239
case __DRI_IMAGE_ERROR_BAD_ACCESS:
2240
return EGL_BAD_ACCESS;
2241
default:
2242
assert(!"unknown dri_error code");
2243
return EGL_BAD_ALLOC;
2244
}
2245
}
2246
2247
static _EGLImage *
2248
dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2249
EGLClientBuffer buffer,
2250
const EGLint *attr_list)
2251
{
2252
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2253
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2254
GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2255
__DRIimage *dri_image;
2256
2257
if (renderbuffer == 0) {
2258
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2259
return EGL_NO_IMAGE_KHR;
2260
}
2261
2262
if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2263
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2264
return EGL_NO_IMAGE_KHR;
2265
}
2266
2267
if (dri2_dpy->image->base.version >= 17 &&
2268
dri2_dpy->image->createImageFromRenderbuffer2) {
2269
unsigned error = ~0;
2270
2271
dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2272
dri2_ctx->dri_context, renderbuffer, NULL, &error);
2273
2274
assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2275
2276
if (!dri_image) {
2277
_eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2278
return EGL_NO_IMAGE_KHR;
2279
}
2280
} else {
2281
dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2282
dri2_ctx->dri_context, renderbuffer, NULL);
2283
if (!dri_image) {
2284
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2285
return EGL_NO_IMAGE_KHR;
2286
}
2287
}
2288
2289
return dri2_create_image_from_dri(disp, dri_image);
2290
}
2291
2292
#ifdef HAVE_WAYLAND_PLATFORM
2293
2294
/* This structure describes how a wl_buffer maps to one or more
2295
* __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2296
* offsets and strides of the planes in the buffer. This table maps a
2297
* wl_drm format code to a description of the planes in the buffer
2298
* that lets us create a __DRIimage for each of the planes. */
2299
2300
static const struct wl_drm_components_descriptor {
2301
uint32_t dri_components;
2302
EGLint components;
2303
int nplanes;
2304
} wl_drm_components[] = {
2305
{ __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2306
{ __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2307
{ __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2308
{ __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2309
{ __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2310
};
2311
2312
static _EGLImage *
2313
dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2314
EGLClientBuffer _buffer,
2315
const EGLint *attr_list)
2316
{
2317
struct wl_drm_buffer *buffer;
2318
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2319
const struct wl_drm_components_descriptor *f;
2320
__DRIimage *dri_image;
2321
_EGLImageAttribs attrs;
2322
int32_t plane;
2323
2324
buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2325
(struct wl_resource *) _buffer);
2326
if (!buffer)
2327
return NULL;
2328
2329
if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2330
return NULL;
2331
2332
plane = attrs.PlaneWL;
2333
f = buffer->driver_format;
2334
if (plane < 0 || plane >= f->nplanes) {
2335
_eglError(EGL_BAD_PARAMETER,
2336
"dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2337
return NULL;
2338
}
2339
2340
dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2341
if (dri_image == NULL && plane == 0)
2342
dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2343
if (dri_image == NULL) {
2344
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2345
return NULL;
2346
}
2347
2348
return dri2_create_image_from_dri(disp, dri_image);
2349
}
2350
#endif
2351
2352
static EGLBoolean
2353
dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2354
EGLuint64KHR *ust, EGLuint64KHR *msc,
2355
EGLuint64KHR *sbc)
2356
{
2357
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2358
if (!dri2_dpy->vtbl->get_sync_values)
2359
return EGL_FALSE;
2360
return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2361
}
2362
2363
/**
2364
* Set the error code after a call to
2365
* dri2_egl_image::dri_image::createImageFromTexture.
2366
*/
2367
static void
2368
dri2_create_image_khr_texture_error(int dri_error)
2369
{
2370
EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2371
2372
if (egl_error != EGL_SUCCESS)
2373
_eglError(egl_error, "dri2_create_image_khr_texture");
2374
}
2375
2376
static _EGLImage *
2377
dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2378
EGLenum target,
2379
EGLClientBuffer buffer,
2380
const EGLint *attr_list)
2381
{
2382
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2383
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2384
struct dri2_egl_image *dri2_img;
2385
GLuint texture = (GLuint) (uintptr_t) buffer;
2386
_EGLImageAttribs attrs;
2387
GLuint depth;
2388
GLenum gl_target;
2389
unsigned error;
2390
2391
if (texture == 0) {
2392
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2393
return EGL_NO_IMAGE_KHR;
2394
}
2395
2396
if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2397
return EGL_NO_IMAGE_KHR;
2398
2399
switch (target) {
2400
case EGL_GL_TEXTURE_2D_KHR:
2401
if (!disp->Extensions.KHR_gl_texture_2D_image) {
2402
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2403
return EGL_NO_IMAGE_KHR;
2404
}
2405
depth = 0;
2406
gl_target = GL_TEXTURE_2D;
2407
break;
2408
case EGL_GL_TEXTURE_3D_KHR:
2409
if (!disp->Extensions.KHR_gl_texture_3D_image) {
2410
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2411
return EGL_NO_IMAGE_KHR;
2412
}
2413
2414
depth = attrs.GLTextureZOffset;
2415
gl_target = GL_TEXTURE_3D;
2416
break;
2417
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2418
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2419
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2420
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2421
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2422
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2423
if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2424
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2425
return EGL_NO_IMAGE_KHR;
2426
}
2427
2428
depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2429
gl_target = GL_TEXTURE_CUBE_MAP;
2430
break;
2431
default:
2432
unreachable("Unexpected target in dri2_create_image_khr_texture()");
2433
return EGL_NO_IMAGE_KHR;
2434
}
2435
2436
dri2_img = malloc(sizeof *dri2_img);
2437
if (!dri2_img) {
2438
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2439
return EGL_NO_IMAGE_KHR;
2440
}
2441
2442
_eglInitImage(&dri2_img->base, disp);
2443
2444
dri2_img->dri_image =
2445
dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2446
gl_target,
2447
texture,
2448
depth,
2449
attrs.GLTextureLevel,
2450
&error,
2451
NULL);
2452
dri2_create_image_khr_texture_error(error);
2453
2454
if (!dri2_img->dri_image) {
2455
free(dri2_img);
2456
return EGL_NO_IMAGE_KHR;
2457
}
2458
return &dri2_img->base;
2459
}
2460
2461
static EGLBoolean
2462
dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2463
EGLint attribute, EGLint *value)
2464
{
2465
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2466
if (!dri2_dpy->vtbl->query_surface)
2467
return _eglQuerySurface(disp, surf, attribute, value);
2468
return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2469
}
2470
2471
static struct wl_buffer*
2472
dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2473
{
2474
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2475
if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2476
return NULL;
2477
return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2478
}
2479
2480
#ifdef HAVE_LIBDRM
2481
static _EGLImage *
2482
dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2483
EGLClientBuffer buffer, const EGLint *attr_list)
2484
{
2485
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2486
EGLint format, name, pitch;
2487
_EGLImageAttribs attrs;
2488
__DRIimage *dri_image;
2489
2490
name = (EGLint) (uintptr_t) buffer;
2491
2492
if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2493
return NULL;
2494
2495
if (attrs.Width <= 0 || attrs.Height <= 0 ||
2496
attrs.DRMBufferStrideMESA <= 0) {
2497
_eglError(EGL_BAD_PARAMETER,
2498
"bad width, height or stride");
2499
return NULL;
2500
}
2501
2502
switch (attrs.DRMBufferFormatMESA) {
2503
case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2504
format = __DRI_IMAGE_FORMAT_ARGB8888;
2505
pitch = attrs.DRMBufferStrideMESA;
2506
break;
2507
default:
2508
_eglError(EGL_BAD_PARAMETER,
2509
"dri2_create_image_khr: unsupported pixmap depth");
2510
return NULL;
2511
}
2512
2513
dri_image =
2514
dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2515
attrs.Width,
2516
attrs.Height,
2517
format,
2518
name,
2519
pitch,
2520
NULL);
2521
2522
return dri2_create_image_from_dri(disp, dri_image);
2523
}
2524
2525
static EGLBoolean
2526
dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2527
{
2528
/**
2529
* The spec says:
2530
*
2531
* "Required attributes and their values are as follows:
2532
*
2533
* * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2534
*
2535
* * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2536
* by drm_fourcc.h and used as the pixel_format parameter of the
2537
* drm_mode_fb_cmd2 ioctl."
2538
*
2539
* and
2540
*
2541
* "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2542
* incomplete, EGL_BAD_PARAMETER is generated."
2543
*/
2544
if (attrs->Width <= 0 || attrs->Height <= 0 ||
2545
!attrs->DMABufFourCC.IsPresent)
2546
return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2547
2548
/**
2549
* Also:
2550
*
2551
* "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2552
* specified for a plane's pitch or offset isn't supported by EGL,
2553
* EGL_BAD_ACCESS is generated."
2554
*/
2555
for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2556
if (attrs->DMABufPlanePitches[i].IsPresent &&
2557
attrs->DMABufPlanePitches[i].Value <= 0)
2558
return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2559
}
2560
2561
/**
2562
* If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2563
* attribute values may be given.
2564
*
2565
* This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2566
* EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2567
*/
2568
for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2569
if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2570
attrs->DMABufPlaneModifiersHi[i].IsPresent)
2571
return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2572
}
2573
2574
/* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2575
* mandate it, we only accept the same modifier across all planes. */
2576
for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2577
if (attrs->DMABufPlaneFds[i].IsPresent) {
2578
if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2579
attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2580
(attrs->DMABufPlaneModifiersLo[0].Value !=
2581
attrs->DMABufPlaneModifiersLo[i].Value) ||
2582
(attrs->DMABufPlaneModifiersHi[0].Value !=
2583
attrs->DMABufPlaneModifiersHi[i].Value))
2584
return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2585
}
2586
}
2587
2588
return EGL_TRUE;
2589
}
2590
2591
/* Returns the total number of planes for the format or zero if it isn't a
2592
* valid fourcc format.
2593
*/
2594
static unsigned
2595
dri2_num_fourcc_format_planes(EGLint format)
2596
{
2597
switch (format) {
2598
case DRM_FORMAT_R8:
2599
case DRM_FORMAT_RG88:
2600
case DRM_FORMAT_GR88:
2601
case DRM_FORMAT_R16:
2602
case DRM_FORMAT_GR1616:
2603
case DRM_FORMAT_RGB332:
2604
case DRM_FORMAT_BGR233:
2605
case DRM_FORMAT_XRGB4444:
2606
case DRM_FORMAT_XBGR4444:
2607
case DRM_FORMAT_RGBX4444:
2608
case DRM_FORMAT_BGRX4444:
2609
case DRM_FORMAT_ARGB4444:
2610
case DRM_FORMAT_ABGR4444:
2611
case DRM_FORMAT_RGBA4444:
2612
case DRM_FORMAT_BGRA4444:
2613
case DRM_FORMAT_XRGB1555:
2614
case DRM_FORMAT_XBGR1555:
2615
case DRM_FORMAT_RGBX5551:
2616
case DRM_FORMAT_BGRX5551:
2617
case DRM_FORMAT_ARGB1555:
2618
case DRM_FORMAT_ABGR1555:
2619
case DRM_FORMAT_RGBA5551:
2620
case DRM_FORMAT_BGRA5551:
2621
case DRM_FORMAT_RGB565:
2622
case DRM_FORMAT_BGR565:
2623
case DRM_FORMAT_RGB888:
2624
case DRM_FORMAT_BGR888:
2625
case DRM_FORMAT_XRGB8888:
2626
case DRM_FORMAT_XBGR8888:
2627
case DRM_FORMAT_RGBX8888:
2628
case DRM_FORMAT_BGRX8888:
2629
case DRM_FORMAT_ARGB8888:
2630
case DRM_FORMAT_ABGR8888:
2631
case DRM_FORMAT_RGBA8888:
2632
case DRM_FORMAT_BGRA8888:
2633
case DRM_FORMAT_XRGB2101010:
2634
case DRM_FORMAT_XBGR2101010:
2635
case DRM_FORMAT_RGBX1010102:
2636
case DRM_FORMAT_BGRX1010102:
2637
case DRM_FORMAT_ARGB2101010:
2638
case DRM_FORMAT_ABGR2101010:
2639
case DRM_FORMAT_RGBA1010102:
2640
case DRM_FORMAT_BGRA1010102:
2641
case DRM_FORMAT_XBGR16161616F:
2642
case DRM_FORMAT_ABGR16161616F:
2643
case DRM_FORMAT_YUYV:
2644
case DRM_FORMAT_YVYU:
2645
case DRM_FORMAT_UYVY:
2646
case DRM_FORMAT_VYUY:
2647
case DRM_FORMAT_AYUV:
2648
case DRM_FORMAT_XYUV8888:
2649
case DRM_FORMAT_Y210:
2650
case DRM_FORMAT_Y212:
2651
case DRM_FORMAT_Y216:
2652
case DRM_FORMAT_Y410:
2653
case DRM_FORMAT_Y412:
2654
case DRM_FORMAT_Y416:
2655
return 1;
2656
2657
case DRM_FORMAT_NV12:
2658
case DRM_FORMAT_NV21:
2659
case DRM_FORMAT_NV16:
2660
case DRM_FORMAT_NV61:
2661
case DRM_FORMAT_P010:
2662
case DRM_FORMAT_P012:
2663
case DRM_FORMAT_P016:
2664
return 2;
2665
2666
case DRM_FORMAT_YUV410:
2667
case DRM_FORMAT_YVU410:
2668
case DRM_FORMAT_YUV411:
2669
case DRM_FORMAT_YVU411:
2670
case DRM_FORMAT_YUV420:
2671
case DRM_FORMAT_YVU420:
2672
case DRM_FORMAT_YUV422:
2673
case DRM_FORMAT_YVU422:
2674
case DRM_FORMAT_YUV444:
2675
case DRM_FORMAT_YVU444:
2676
return 3;
2677
2678
default:
2679
return 0;
2680
}
2681
}
2682
2683
/* Returns the total number of file descriptors. Zero indicates an error. */
2684
static unsigned
2685
dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2686
{
2687
unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2688
if (plane_n == 0) {
2689
_eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2690
return 0;
2691
}
2692
2693
for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2694
/**
2695
* The modifiers extension spec says:
2696
*
2697
* "Modifiers may modify any attribute of a buffer import, including
2698
* but not limited to adding extra planes to a format which
2699
* otherwise does not have those planes. As an example, a modifier
2700
* may add a plane for an external compression buffer to a
2701
* single-plane format. The exact meaning and effect of any
2702
* modifier is canonically defined by drm_fourcc.h, not as part of
2703
* this extension."
2704
*/
2705
if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2706
attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2707
plane_n = i + 1;
2708
}
2709
}
2710
2711
/**
2712
* The spec says:
2713
*
2714
* "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2715
* incomplete, EGL_BAD_PARAMETER is generated."
2716
*/
2717
for (unsigned i = 0; i < plane_n; ++i) {
2718
if (!attrs->DMABufPlaneFds[i].IsPresent ||
2719
!attrs->DMABufPlaneOffsets[i].IsPresent ||
2720
!attrs->DMABufPlanePitches[i].IsPresent) {
2721
_eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2722
return 0;
2723
}
2724
}
2725
2726
/**
2727
* The spec also says:
2728
*
2729
* "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2730
* attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2731
* generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2732
* or EGL_DMA_BUF_PLANE3_* attributes are specified."
2733
*/
2734
for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2735
if (attrs->DMABufPlaneFds[i].IsPresent ||
2736
attrs->DMABufPlaneOffsets[i].IsPresent ||
2737
attrs->DMABufPlanePitches[i].IsPresent) {
2738
_eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2739
return 0;
2740
}
2741
}
2742
2743
return plane_n;
2744
}
2745
2746
static EGLBoolean
2747
dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2748
EGLint *formats, EGLint *count)
2749
{
2750
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2751
if (max < 0 || (max > 0 && formats == NULL))
2752
return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2753
2754
if (dri2_dpy->image->base.version < 15 ||
2755
dri2_dpy->image->queryDmaBufFormats == NULL)
2756
return EGL_FALSE;
2757
2758
if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2759
formats, count))
2760
return EGL_FALSE;
2761
2762
if (max > 0) {
2763
/* Assert that all of the formats returned are actually fourcc formats.
2764
* Some day, if we want the internal interface function to be able to
2765
* return the fake fourcc formats defined in dri_interface.h, we'll have
2766
* to do something more clever here to pair the list down to just real
2767
* fourcc formats so that we don't leak the fake internal ones.
2768
*/
2769
for (int i = 0; i < *count; i++) {
2770
assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2771
}
2772
}
2773
2774
return EGL_TRUE;
2775
}
2776
2777
static EGLBoolean
2778
dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2779
EGLint max, EGLuint64KHR *modifiers,
2780
EGLBoolean *external_only, EGLint *count)
2781
{
2782
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2783
2784
if (dri2_num_fourcc_format_planes(format) == 0)
2785
return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2786
2787
if (max < 0)
2788
return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2789
2790
if (max > 0 && modifiers == NULL)
2791
return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2792
2793
if (dri2_dpy->image->base.version < 15 ||
2794
dri2_dpy->image->queryDmaBufModifiers == NULL)
2795
return EGL_FALSE;
2796
2797
if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2798
max, modifiers,
2799
(unsigned int *) external_only,
2800
count) == false)
2801
return _eglError(EGL_BAD_PARAMETER, "invalid format");
2802
2803
return EGL_TRUE;
2804
}
2805
2806
/**
2807
* The spec says:
2808
*
2809
* "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2810
* EGL will take a reference to the dma_buf(s) which it will release at any
2811
* time while the EGLDisplay is initialized. It is the responsibility of the
2812
* application to close the dma_buf file descriptors."
2813
*
2814
* Therefore we must never close or otherwise modify the file descriptors.
2815
*/
2816
_EGLImage *
2817
dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2818
EGLClientBuffer buffer, const EGLint *attr_list)
2819
{
2820
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2821
_EGLImage *res;
2822
_EGLImageAttribs attrs;
2823
__DRIimage *dri_image;
2824
unsigned num_fds;
2825
int fds[DMA_BUF_MAX_PLANES];
2826
int pitches[DMA_BUF_MAX_PLANES];
2827
int offsets[DMA_BUF_MAX_PLANES];
2828
uint64_t modifier;
2829
bool has_modifier = false;
2830
unsigned error;
2831
2832
/**
2833
* The spec says:
2834
*
2835
* ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2836
* error EGL_BAD_PARAMETER is generated."
2837
*/
2838
if (buffer != NULL) {
2839
_eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2840
return NULL;
2841
}
2842
2843
if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2844
return NULL;
2845
2846
if (!dri2_check_dma_buf_attribs(&attrs))
2847
return NULL;
2848
2849
num_fds = dri2_check_dma_buf_format(&attrs);
2850
if (!num_fds)
2851
return NULL;
2852
2853
for (unsigned i = 0; i < num_fds; ++i) {
2854
fds[i] = attrs.DMABufPlaneFds[i].Value;
2855
pitches[i] = attrs.DMABufPlanePitches[i].Value;
2856
offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2857
}
2858
2859
/* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2860
* will be present in attrs.DMABufPlaneModifiersLo[0] and
2861
* attrs.DMABufPlaneModifiersHi[0] */
2862
if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2863
modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2864
attrs.DMABufPlaneModifiersLo[0].Value);
2865
has_modifier = true;
2866
}
2867
2868
if (attrs.ProtectedContent) {
2869
if (dri2_dpy->image->base.version < 18 ||
2870
dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
2871
_eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
2872
return EGL_NO_IMAGE_KHR;
2873
}
2874
if (!has_modifier)
2875
modifier = DRM_FORMAT_MOD_INVALID;
2876
2877
dri_image =
2878
dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
2879
attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2880
modifier, fds, num_fds, pitches, offsets,
2881
attrs.DMABufYuvColorSpaceHint.Value,
2882
attrs.DMABufSampleRangeHint.Value,
2883
attrs.DMABufChromaHorizontalSiting.Value,
2884
attrs.DMABufChromaVerticalSiting.Value,
2885
attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
2886
&error,
2887
NULL);
2888
}
2889
else if (has_modifier) {
2890
if (dri2_dpy->image->base.version < 15 ||
2891
dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2892
_eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2893
return EGL_NO_IMAGE_KHR;
2894
}
2895
dri_image =
2896
dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2897
attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2898
modifier, fds, num_fds, pitches, offsets,
2899
attrs.DMABufYuvColorSpaceHint.Value,
2900
attrs.DMABufSampleRangeHint.Value,
2901
attrs.DMABufChromaHorizontalSiting.Value,
2902
attrs.DMABufChromaVerticalSiting.Value,
2903
&error,
2904
NULL);
2905
}
2906
else {
2907
dri_image =
2908
dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2909
attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2910
fds, num_fds, pitches, offsets,
2911
attrs.DMABufYuvColorSpaceHint.Value,
2912
attrs.DMABufSampleRangeHint.Value,
2913
attrs.DMABufChromaHorizontalSiting.Value,
2914
attrs.DMABufChromaVerticalSiting.Value,
2915
&error,
2916
NULL);
2917
}
2918
dri2_create_image_khr_texture_error(error);
2919
2920
if (!dri_image)
2921
return EGL_NO_IMAGE_KHR;
2922
2923
res = dri2_create_image_from_dri(disp, dri_image);
2924
2925
return res;
2926
}
2927
static _EGLImage *
2928
dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
2929
{
2930
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2931
struct dri2_egl_image *dri2_img;
2932
_EGLImageAttribs attrs;
2933
unsigned int dri_use, valid_mask;
2934
int format;
2935
2936
if (!attr_list) {
2937
_eglError(EGL_BAD_PARAMETER, __func__);
2938
return EGL_NO_IMAGE_KHR;
2939
}
2940
2941
if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2942
return EGL_NO_IMAGE_KHR;
2943
2944
if (attrs.Width <= 0 || attrs.Height <= 0) {
2945
_eglError(EGL_BAD_PARAMETER, __func__);
2946
return EGL_NO_IMAGE_KHR;
2947
}
2948
2949
switch (attrs.DRMBufferFormatMESA) {
2950
case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2951
format = __DRI_IMAGE_FORMAT_ARGB8888;
2952
break;
2953
default:
2954
_eglError(EGL_BAD_PARAMETER, __func__);
2955
return EGL_NO_IMAGE_KHR;
2956
}
2957
2958
valid_mask =
2959
EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2960
EGL_DRM_BUFFER_USE_SHARE_MESA |
2961
EGL_DRM_BUFFER_USE_CURSOR_MESA;
2962
if (attrs.DRMBufferUseMESA & ~valid_mask) {
2963
_eglError(EGL_BAD_PARAMETER, __func__);
2964
return EGL_NO_IMAGE_KHR;
2965
}
2966
2967
dri_use = 0;
2968
if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2969
dri_use |= __DRI_IMAGE_USE_SHARE;
2970
if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2971
dri_use |= __DRI_IMAGE_USE_SCANOUT;
2972
if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2973
dri_use |= __DRI_IMAGE_USE_CURSOR;
2974
2975
dri2_img = malloc(sizeof *dri2_img);
2976
if (!dri2_img) {
2977
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2978
return EGL_NO_IMAGE_KHR;
2979
}
2980
2981
_eglInitImage(&dri2_img->base, disp);
2982
2983
dri2_img->dri_image =
2984
dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2985
attrs.Width, attrs.Height,
2986
format, dri_use, dri2_img);
2987
if (dri2_img->dri_image == NULL) {
2988
free(dri2_img);
2989
_eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2990
return EGL_NO_IMAGE_KHR;
2991
}
2992
2993
return &dri2_img->base;
2994
}
2995
2996
static EGLBoolean
2997
dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
2998
EGLint *name, EGLint *handle, EGLint *stride)
2999
{
3000
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3001
struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3002
3003
if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3004
__DRI_IMAGE_ATTRIB_NAME, name))
3005
return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3006
3007
if (handle)
3008
dri2_dpy->image->queryImage(dri2_img->dri_image,
3009
__DRI_IMAGE_ATTRIB_HANDLE, handle);
3010
3011
if (stride)
3012
dri2_dpy->image->queryImage(dri2_img->dri_image,
3013
__DRI_IMAGE_ATTRIB_STRIDE, stride);
3014
3015
return EGL_TRUE;
3016
}
3017
3018
/**
3019
* Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3020
3021
* The spec provides a boolean return for the driver to reject exporting for
3022
* basically any reason, but doesn't specify any particular error cases. For
3023
* now, we just fail if we don't have a DRM fourcc for the format.
3024
*/
3025
static bool
3026
dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3027
{
3028
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3029
struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3030
EGLint fourcc;
3031
3032
if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3033
__DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3034
return false;
3035
}
3036
3037
return true;
3038
}
3039
3040
static EGLBoolean
3041
dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3042
EGLint *fourcc, EGLint *nplanes,
3043
EGLuint64KHR *modifiers)
3044
{
3045
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3046
struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3047
int num_planes;
3048
3049
if (!dri2_can_export_dma_buf_image(disp, img))
3050
return EGL_FALSE;
3051
3052
dri2_dpy->image->queryImage(dri2_img->dri_image,
3053
__DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3054
if (nplanes)
3055
*nplanes = num_planes;
3056
3057
if (fourcc)
3058
dri2_dpy->image->queryImage(dri2_img->dri_image,
3059
__DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3060
3061
if (modifiers) {
3062
int mod_hi, mod_lo;
3063
uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3064
bool query;
3065
3066
query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3067
__DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3068
&mod_hi);
3069
query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3070
__DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3071
&mod_lo);
3072
if (query)
3073
modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3074
3075
for (int i = 0; i < num_planes; i++)
3076
modifiers[i] = modifier;
3077
}
3078
3079
return EGL_TRUE;
3080
}
3081
3082
static EGLBoolean
3083
dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3084
int *fds, EGLint *strides, EGLint *offsets)
3085
{
3086
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3087
struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3088
EGLint nplanes;
3089
3090
if (!dri2_can_export_dma_buf_image(disp, img))
3091
return EGL_FALSE;
3092
3093
/* EGL_MESA_image_dma_buf_export spec says:
3094
* "If the number of fds is less than the number of planes, then
3095
* subsequent fd slots should contain -1."
3096
*/
3097
if (fds) {
3098
/* Query nplanes so that we know how big the given array is. */
3099
dri2_dpy->image->queryImage(dri2_img->dri_image,
3100
__DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3101
memset(fds, -1, nplanes * sizeof(int));
3102
}
3103
3104
/* rework later to provide multiple fds/strides/offsets */
3105
if (fds)
3106
dri2_dpy->image->queryImage(dri2_img->dri_image,
3107
__DRI_IMAGE_ATTRIB_FD, fds);
3108
3109
if (strides)
3110
dri2_dpy->image->queryImage(dri2_img->dri_image,
3111
__DRI_IMAGE_ATTRIB_STRIDE, strides);
3112
3113
if (offsets) {
3114
int img_offset;
3115
bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3116
__DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3117
if (ret)
3118
offsets[0] = img_offset;
3119
else
3120
offsets[0] = 0;
3121
}
3122
3123
return EGL_TRUE;
3124
}
3125
3126
#endif
3127
3128
_EGLImage *
3129
dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3130
EGLClientBuffer buffer, const EGLint *attr_list)
3131
{
3132
switch (target) {
3133
case EGL_GL_TEXTURE_2D_KHR:
3134
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3135
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3136
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3137
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3138
case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3139
case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3140
case EGL_GL_TEXTURE_3D_KHR:
3141
return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3142
case EGL_GL_RENDERBUFFER_KHR:
3143
return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3144
#ifdef HAVE_LIBDRM
3145
case EGL_DRM_BUFFER_MESA:
3146
return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3147
case EGL_LINUX_DMA_BUF_EXT:
3148
return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3149
#endif
3150
#ifdef HAVE_WAYLAND_PLATFORM
3151
case EGL_WAYLAND_BUFFER_WL:
3152
return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3153
#endif
3154
default:
3155
_eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3156
return EGL_NO_IMAGE_KHR;
3157
}
3158
}
3159
3160
static EGLBoolean
3161
dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3162
{
3163
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3164
struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3165
3166
dri2_dpy->image->destroyImage(dri2_img->dri_image);
3167
free(dri2_img);
3168
3169
return EGL_TRUE;
3170
}
3171
3172
#ifdef HAVE_WAYLAND_PLATFORM
3173
3174
static void
3175
dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3176
struct wl_drm_buffer *buffer)
3177
{
3178
_EGLDisplay *disp = user_data;
3179
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3180
__DRIimage *img;
3181
int dri_components = 0;
3182
3183
if (fd == -1)
3184
img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3185
buffer->width,
3186
buffer->height,
3187
buffer->format,
3188
(int*)&name, 1,
3189
buffer->stride,
3190
buffer->offset,
3191
NULL);
3192
else
3193
img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3194
buffer->width,
3195
buffer->height,
3196
buffer->format,
3197
&fd, 1,
3198
buffer->stride,
3199
buffer->offset,
3200
NULL);
3201
3202
if (img == NULL)
3203
return;
3204
3205
dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3206
3207
buffer->driver_format = NULL;
3208
for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3209
if (wl_drm_components[i].dri_components == dri_components)
3210
buffer->driver_format = &wl_drm_components[i];
3211
3212
if (buffer->driver_format == NULL)
3213
dri2_dpy->image->destroyImage(img);
3214
else
3215
buffer->driver_buffer = img;
3216
}
3217
3218
static void
3219
dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3220
{
3221
_EGLDisplay *disp = user_data;
3222
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3223
3224
dri2_dpy->image->destroyImage(buffer->driver_buffer);
3225
}
3226
3227
static EGLBoolean
3228
dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3229
{
3230
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3231
const struct wayland_drm_callbacks wl_drm_callbacks = {
3232
.authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3233
.reference_buffer = dri2_wl_reference_buffer,
3234
.release_buffer = dri2_wl_release_buffer,
3235
.is_format_supported = dri2_wl_is_format_supported
3236
};
3237
int flags = 0;
3238
char *device_name;
3239
uint64_t cap;
3240
3241
if (dri2_dpy->wl_server_drm)
3242
return EGL_FALSE;
3243
3244
device_name = drmGetRenderDeviceNameFromFd(dri2_dpy->fd);
3245
if (!device_name)
3246
device_name = strdup(dri2_dpy->device_name);
3247
if (!device_name)
3248
return EGL_FALSE;
3249
3250
if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3251
cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3252
dri2_dpy->image->base.version >= 7 &&
3253
dri2_dpy->image->createImageFromFds != NULL)
3254
flags |= WAYLAND_DRM_PRIME;
3255
3256
dri2_dpy->wl_server_drm =
3257
wayland_drm_init(wl_dpy, device_name,
3258
&wl_drm_callbacks, disp, flags);
3259
3260
free(device_name);
3261
3262
if (!dri2_dpy->wl_server_drm)
3263
return EGL_FALSE;
3264
3265
#ifdef HAVE_DRM_PLATFORM
3266
/* We have to share the wl_drm instance with gbm, so gbm can convert
3267
* wl_buffers to gbm bos. */
3268
if (dri2_dpy->gbm_dri)
3269
dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3270
#endif
3271
3272
return EGL_TRUE;
3273
}
3274
3275
static EGLBoolean
3276
dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3277
{
3278
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3279
3280
if (!dri2_dpy->wl_server_drm)
3281
return EGL_FALSE;
3282
3283
wayland_drm_uninit(dri2_dpy->wl_server_drm);
3284
dri2_dpy->wl_server_drm = NULL;
3285
3286
return EGL_TRUE;
3287
}
3288
3289
static EGLBoolean
3290
dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3291
EGLint attribute, EGLint *value)
3292
{
3293
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3294
struct wl_drm_buffer *buffer;
3295
const struct wl_drm_components_descriptor *format;
3296
3297
buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3298
if (!buffer)
3299
return EGL_FALSE;
3300
3301
format = buffer->driver_format;
3302
switch (attribute) {
3303
case EGL_TEXTURE_FORMAT:
3304
*value = format->components;
3305
return EGL_TRUE;
3306
case EGL_WIDTH:
3307
*value = buffer->width;
3308
return EGL_TRUE;
3309
case EGL_HEIGHT:
3310
*value = buffer->height;
3311
return EGL_TRUE;
3312
}
3313
3314
return EGL_FALSE;
3315
}
3316
#endif
3317
3318
static void
3319
dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3320
{
3321
p_atomic_inc(&sync->refcount);
3322
}
3323
3324
static void
3325
dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3326
struct dri2_egl_sync *dri2_sync)
3327
{
3328
if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3329
switch (dri2_sync->base.Type) {
3330
case EGL_SYNC_REUSABLE_KHR:
3331
cnd_destroy(&dri2_sync->cond);
3332
break;
3333
case EGL_SYNC_NATIVE_FENCE_ANDROID:
3334
if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3335
close(dri2_sync->base.SyncFd);
3336
break;
3337
default:
3338
break;
3339
}
3340
3341
if (dri2_sync->fence)
3342
dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3343
3344
free(dri2_sync);
3345
}
3346
}
3347
3348
static _EGLSync *
3349
dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3350
{
3351
_EGLContext *ctx = _eglGetCurrentContext();
3352
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3353
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3354
struct dri2_egl_sync *dri2_sync;
3355
EGLint ret;
3356
pthread_condattr_t attr;
3357
3358
dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3359
if (!dri2_sync) {
3360
_eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3361
return NULL;
3362
}
3363
3364
if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3365
free(dri2_sync);
3366
return NULL;
3367
}
3368
3369
switch (type) {
3370
case EGL_SYNC_FENCE_KHR:
3371
dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3372
if (!dri2_sync->fence) {
3373
/* Why did it fail? DRI doesn't return an error code, so we emit
3374
* a generic EGL error that doesn't communicate user error.
3375
*/
3376
_eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3377
free(dri2_sync);
3378
return NULL;
3379
}
3380
break;
3381
3382
case EGL_SYNC_CL_EVENT_KHR:
3383
dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3384
dri2_dpy->dri_screen,
3385
dri2_sync->base.CLEvent);
3386
/* this can only happen if the cl_event passed in is invalid. */
3387
if (!dri2_sync->fence) {
3388
_eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3389
free(dri2_sync);
3390
return NULL;
3391
}
3392
3393
/* the initial status must be "signaled" if the cl_event is signaled */
3394
if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3395
dri2_sync->fence, 0, 0))
3396
dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3397
break;
3398
3399
case EGL_SYNC_REUSABLE_KHR:
3400
/* intialize attr */
3401
ret = pthread_condattr_init(&attr);
3402
3403
if (ret) {
3404
_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3405
free(dri2_sync);
3406
return NULL;
3407
}
3408
3409
/* change clock attribute to CLOCK_MONOTONIC */
3410
ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3411
3412
if (ret) {
3413
_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3414
free(dri2_sync);
3415
return NULL;
3416
}
3417
3418
ret = pthread_cond_init(&dri2_sync->cond, &attr);
3419
3420
if (ret) {
3421
_eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3422
free(dri2_sync);
3423
return NULL;
3424
}
3425
3426
/* initial status of reusable sync must be "unsignaled" */
3427
dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3428
break;
3429
3430
case EGL_SYNC_NATIVE_FENCE_ANDROID:
3431
if (dri2_dpy->fence->create_fence_fd) {
3432
dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3433
dri2_ctx->dri_context,
3434
dri2_sync->base.SyncFd);
3435
}
3436
if (!dri2_sync->fence) {
3437
_eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3438
free(dri2_sync);
3439
return NULL;
3440
}
3441
break;
3442
}
3443
3444
p_atomic_set(&dri2_sync->refcount, 1);
3445
return &dri2_sync->base;
3446
}
3447
3448
static EGLBoolean
3449
dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3450
{
3451
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3452
struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3453
EGLint ret = EGL_TRUE;
3454
EGLint err;
3455
3456
/* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3457
* then unlock all threads possibly blocked by the reusable sync before
3458
* destroying it.
3459
*/
3460
if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3461
dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3462
dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3463
/* unblock all threads currently blocked by sync */
3464
err = cnd_broadcast(&dri2_sync->cond);
3465
3466
if (err) {
3467
_eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3468
ret = EGL_FALSE;
3469
}
3470
}
3471
3472
dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3473
3474
return ret;
3475
}
3476
3477
static EGLint
3478
dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3479
{
3480
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3481
struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3482
3483
assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3484
3485
if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3486
/* try to retrieve the actual native fence fd.. if rendering is
3487
* not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3488
*/
3489
sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3490
dri2_sync->fence);
3491
}
3492
3493
if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3494
/* if native fence fd still not created, return an error: */
3495
_eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3496
return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3497
}
3498
3499
assert(sync_valid_fd(sync->SyncFd));
3500
3501
return os_dupfd_cloexec(sync->SyncFd);
3502
}
3503
3504
static void
3505
dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3506
EGLSetBlobFuncANDROID set,
3507
EGLGetBlobFuncANDROID get)
3508
{
3509
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3510
dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3511
disp->BlobCacheSet,
3512
disp->BlobCacheGet);
3513
}
3514
3515
static EGLint
3516
dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3517
EGLint flags, EGLTime timeout)
3518
{
3519
_EGLContext *ctx = _eglGetCurrentContext();
3520
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3521
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3522
struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3523
unsigned wait_flags = 0;
3524
3525
EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3526
3527
/* The EGL_KHR_fence_sync spec states:
3528
*
3529
* "If no context is current for the bound API,
3530
* the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3531
*/
3532
if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3533
wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3534
3535
/* the sync object should take a reference while waiting */
3536
dri2_egl_ref_sync(dri2_sync);
3537
3538
switch (sync->Type) {
3539
case EGL_SYNC_FENCE_KHR:
3540
case EGL_SYNC_NATIVE_FENCE_ANDROID:
3541
case EGL_SYNC_CL_EVENT_KHR:
3542
if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3543
dri2_sync->fence, wait_flags,
3544
timeout))
3545
dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3546
else
3547
ret = EGL_TIMEOUT_EXPIRED_KHR;
3548
break;
3549
3550
case EGL_SYNC_REUSABLE_KHR:
3551
if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3552
(flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3553
/* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3554
dri2_gl_flush();
3555
}
3556
3557
/* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3558
if (timeout == EGL_FOREVER_KHR) {
3559
mtx_lock(&dri2_sync->mutex);
3560
cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3561
mtx_unlock(&dri2_sync->mutex);
3562
} else {
3563
/* if reusable sync has not been yet signaled */
3564
if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3565
/* timespecs for cnd_timedwait */
3566
struct timespec current;
3567
struct timespec expire;
3568
3569
/* We override the clock to monotonic when creating the condition
3570
* variable. */
3571
clock_gettime(CLOCK_MONOTONIC, &current);
3572
3573
/* calculating when to expire */
3574
expire.tv_nsec = timeout % 1000000000L;
3575
expire.tv_sec = timeout / 1000000000L;
3576
3577
expire.tv_nsec += current.tv_nsec;
3578
expire.tv_sec += current.tv_sec;
3579
3580
/* expire.nsec now is a number between 0 and 1999999998 */
3581
if (expire.tv_nsec > 999999999L) {
3582
expire.tv_sec++;
3583
expire.tv_nsec -= 1000000000L;
3584
}
3585
3586
mtx_lock(&dri2_sync->mutex);
3587
ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3588
mtx_unlock(&dri2_sync->mutex);
3589
3590
if (ret == thrd_busy) {
3591
if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3592
ret = EGL_TIMEOUT_EXPIRED_KHR;
3593
} else {
3594
_eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3595
ret = EGL_FALSE;
3596
}
3597
}
3598
}
3599
}
3600
break;
3601
}
3602
dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3603
3604
return ret;
3605
}
3606
3607
static EGLBoolean
3608
dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3609
{
3610
struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3611
EGLint ret;
3612
3613
if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3614
return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3615
3616
if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3617
return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3618
3619
dri2_sync->base.SyncStatus = mode;
3620
3621
if (mode == EGL_SIGNALED_KHR) {
3622
ret = cnd_broadcast(&dri2_sync->cond);
3623
3624
/* fail to broadcast */
3625
if (ret)
3626
return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3627
}
3628
3629
return EGL_TRUE;
3630
}
3631
3632
static EGLint
3633
dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3634
{
3635
_EGLContext *ctx = _eglGetCurrentContext();
3636
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3637
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3638
struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3639
3640
dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3641
dri2_sync->fence, 0);
3642
return EGL_TRUE;
3643
}
3644
3645
static int
3646
dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3647
struct mesa_glinterop_device_info *out)
3648
{
3649
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3650
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3651
3652
if (!dri2_dpy->interop)
3653
return MESA_GLINTEROP_UNSUPPORTED;
3654
3655
return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3656
}
3657
3658
static int
3659
dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3660
struct mesa_glinterop_export_in *in,
3661
struct mesa_glinterop_export_out *out)
3662
{
3663
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3664
struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3665
3666
if (!dri2_dpy->interop)
3667
return MESA_GLINTEROP_UNSUPPORTED;
3668
3669
return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3670
}
3671
3672
const _EGLDriver _eglDriver = {
3673
.Initialize = dri2_initialize,
3674
.Terminate = dri2_terminate,
3675
.CreateContext = dri2_create_context,
3676
.DestroyContext = dri2_destroy_context,
3677
.MakeCurrent = dri2_make_current,
3678
.CreateWindowSurface = dri2_create_window_surface,
3679
.CreatePixmapSurface = dri2_create_pixmap_surface,
3680
.CreatePbufferSurface = dri2_create_pbuffer_surface,
3681
.DestroySurface = dri2_destroy_surface,
3682
.GetProcAddress = dri2_get_proc_address,
3683
.WaitClient = dri2_wait_client,
3684
.WaitNative = dri2_wait_native,
3685
.BindTexImage = dri2_bind_tex_image,
3686
.ReleaseTexImage = dri2_release_tex_image,
3687
.SwapInterval = dri2_swap_interval,
3688
.SwapBuffers = dri2_swap_buffers,
3689
.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3690
.SwapBuffersRegionNOK = dri2_swap_buffers_region,
3691
.SetDamageRegion = dri2_set_damage_region,
3692
.PostSubBufferNV = dri2_post_sub_buffer,
3693
.CopyBuffers = dri2_copy_buffers,
3694
.QueryBufferAge = dri2_query_buffer_age,
3695
.CreateImageKHR = dri2_create_image,
3696
.DestroyImageKHR = dri2_destroy_image_khr,
3697
.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3698
.QuerySurface = dri2_query_surface,
3699
.QueryDriverName = dri2_query_driver_name,
3700
.QueryDriverConfig = dri2_query_driver_config,
3701
#ifdef HAVE_LIBDRM
3702
.CreateDRMImageMESA = dri2_create_drm_image_mesa,
3703
.ExportDRMImageMESA = dri2_export_drm_image_mesa,
3704
.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3705
.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3706
.QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3707
.QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3708
#endif
3709
#ifdef HAVE_WAYLAND_PLATFORM
3710
.BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3711
.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3712
.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3713
#endif
3714
.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3715
.CreateSyncKHR = dri2_create_sync,
3716
.ClientWaitSyncKHR = dri2_client_wait_sync,
3717
.SignalSyncKHR = dri2_signal_sync,
3718
.WaitSyncKHR = dri2_server_wait_sync,
3719
.DestroySyncKHR = dri2_destroy_sync,
3720
.GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3721
.GLInteropExportObject = dri2_interop_export_object,
3722
.DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3723
.SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,
3724
};
3725
3726