Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/glx/dri3_glx.c
4558 views
1
/*
2
* Copyright © 2013 Keith Packard
3
*
4
* Permission to use, copy, modify, distribute, and sell this software and its
5
* documentation for any purpose is hereby granted without fee, provided that
6
* the above copyright notice appear in all copies and that both that copyright
7
* notice and this permission notice appear in supporting documentation, and
8
* that the name of the copyright holders not be used in advertising or
9
* publicity pertaining to distribution of the software without specific,
10
* written prior permission. The copyright holders make no representations
11
* about the suitability of this software for any purpose. It is provided "as
12
* is" without express or implied warranty.
13
*
14
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20
* OF THIS SOFTWARE.
21
*/
22
23
/*
24
* Portions of this code were adapted from dri2_glx.c which carries the
25
* following copyright:
26
*
27
* Copyright © 2008 Red Hat, Inc.
28
*
29
* Permission is hereby granted, free of charge, to any person obtaining a
30
* copy of this software and associated documentation files (the "Soft-
31
* ware"), to deal in the Software without restriction, including without
32
* limitation the rights to use, copy, modify, merge, publish, distribute,
33
* and/or sell copies of the Software, and to permit persons to whom the
34
* Software is furnished to do so, provided that the above copyright
35
* notice(s) and this permission notice appear in all copies of the Soft-
36
* ware and that both the above copyright notice(s) and this permission
37
* notice appear in supporting documentation.
38
*
39
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
41
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
42
* RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
43
* THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
44
* QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
45
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
46
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
47
* MANCE OF THIS SOFTWARE.
48
*
49
* Except as contained in this notice, the name of a copyright holder shall
50
* not be used in advertising or otherwise to promote the sale, use or
51
* other dealings in this Software without prior written authorization of
52
* the copyright holder.
53
*
54
* Authors:
55
* Kristian Høgsberg ([email protected])
56
*/
57
58
#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
59
60
#include <X11/Xlib.h>
61
#include <X11/extensions/Xfixes.h>
62
#include <X11/Xlib-xcb.h>
63
#include <X11/xshmfence.h>
64
#include <xcb/xcb.h>
65
#include <xcb/dri3.h>
66
#include <xcb/present.h>
67
#include <GL/gl.h>
68
#include "glxclient.h"
69
#include <dlfcn.h>
70
#include <fcntl.h>
71
#include <unistd.h>
72
#include <sys/types.h>
73
#include <sys/mman.h>
74
#include <sys/time.h>
75
76
#include "dri_common.h"
77
#include "dri3_priv.h"
78
#include "loader.h"
79
#include "dri2.h"
80
81
static struct dri3_drawable *
82
loader_drawable_to_dri3_drawable(struct loader_dri3_drawable *draw) {
83
size_t offset = offsetof(struct dri3_drawable, loader_drawable);
84
if (!draw)
85
return NULL;
86
return (struct dri3_drawable *)(((void*) draw) - offset);
87
}
88
89
static void
90
glx_dri3_set_drawable_size(struct loader_dri3_drawable *draw,
91
int width, int height)
92
{
93
/* Nothing to do */
94
}
95
96
static bool
97
glx_dri3_in_current_context(struct loader_dri3_drawable *draw)
98
{
99
struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
100
101
if (!priv)
102
return false;
103
104
struct dri3_context *pcp = (struct dri3_context *) __glXGetCurrentContext();
105
struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
106
107
return (&pcp->base != &dummyContext) && pcp->base.psc == &psc->base;
108
}
109
110
static __DRIcontext *
111
glx_dri3_get_dri_context(struct loader_dri3_drawable *draw)
112
{
113
struct glx_context *gc = __glXGetCurrentContext();
114
struct dri3_context *dri3Ctx = (struct dri3_context *) gc;
115
116
return (gc != &dummyContext) ? dri3Ctx->driContext : NULL;
117
}
118
119
static __DRIscreen *
120
glx_dri3_get_dri_screen(void)
121
{
122
struct glx_context *gc = __glXGetCurrentContext();
123
struct dri3_context *pcp = (struct dri3_context *) gc;
124
struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
125
126
return (gc != &dummyContext && psc) ? psc->driScreen : NULL;
127
}
128
129
static void
130
glx_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags)
131
{
132
loader_dri3_flush(draw, flags, __DRI2_THROTTLE_SWAPBUFFER);
133
}
134
135
static void
136
glx_dri3_show_fps(struct loader_dri3_drawable *draw, uint64_t current_ust)
137
{
138
struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
139
const uint64_t interval =
140
((struct dri3_screen *) priv->base.psc)->show_fps_interval;
141
142
if (!interval)
143
return;
144
145
priv->frames++;
146
147
/* DRI3+Present together uses microseconds for UST. */
148
if (priv->previous_ust + interval * 1000000 <= current_ust) {
149
if (priv->previous_ust) {
150
fprintf(stderr, "libGL: FPS = %.2f\n",
151
((uint64_t) priv->frames * 1000000) /
152
(double)(current_ust - priv->previous_ust));
153
}
154
priv->frames = 0;
155
priv->previous_ust = current_ust;
156
}
157
}
158
159
static const struct loader_dri3_vtable glx_dri3_vtable = {
160
.set_drawable_size = glx_dri3_set_drawable_size,
161
.in_current_context = glx_dri3_in_current_context,
162
.get_dri_context = glx_dri3_get_dri_context,
163
.get_dri_screen = glx_dri3_get_dri_screen,
164
.flush_drawable = glx_dri3_flush_drawable,
165
.show_fps = glx_dri3_show_fps,
166
};
167
168
169
static const struct glx_context_vtable dri3_context_vtable;
170
171
static void
172
dri3_destroy_context(struct glx_context *context)
173
{
174
struct dri3_context *pcp = (struct dri3_context *) context;
175
struct dri3_screen *psc = (struct dri3_screen *) context->psc;
176
177
driReleaseDrawables(&pcp->base);
178
179
free((char *) context->extensions);
180
181
(*psc->core->destroyContext) (pcp->driContext);
182
183
free(pcp);
184
}
185
186
static Bool
187
dri3_bind_context(struct glx_context *context, struct glx_context *old,
188
GLXDrawable draw, GLXDrawable read)
189
{
190
struct dri3_context *pcp = (struct dri3_context *) context;
191
struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
192
struct dri3_drawable *pdraw, *pread;
193
__DRIdrawable *dri_draw = NULL, *dri_read = NULL;
194
195
pdraw = (struct dri3_drawable *) driFetchDrawable(context, draw);
196
pread = (struct dri3_drawable *) driFetchDrawable(context, read);
197
198
driReleaseDrawables(&pcp->base);
199
200
if (pdraw)
201
dri_draw = pdraw->loader_drawable.dri_drawable;
202
else if (draw != None)
203
return GLXBadDrawable;
204
205
if (pread)
206
dri_read = pread->loader_drawable.dri_drawable;
207
else if (read != None)
208
return GLXBadDrawable;
209
210
if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
211
return GLXBadContext;
212
213
if (dri_draw)
214
psc->f->invalidate(dri_draw);
215
if (dri_read && dri_read != dri_draw)
216
psc->f->invalidate(dri_read);
217
218
return Success;
219
}
220
221
static void
222
dri3_unbind_context(struct glx_context *context, struct glx_context *new)
223
{
224
struct dri3_context *pcp = (struct dri3_context *) context;
225
struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
226
227
(*psc->core->unbindContext) (pcp->driContext);
228
}
229
230
static struct glx_context *
231
dri3_create_context_attribs(struct glx_screen *base,
232
struct glx_config *config_base,
233
struct glx_context *shareList,
234
unsigned num_attribs,
235
const uint32_t *attribs,
236
unsigned *error)
237
{
238
struct dri3_context *pcp = NULL;
239
struct dri3_context *pcp_shared = NULL;
240
struct dri3_screen *psc = (struct dri3_screen *) base;
241
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
242
__DRIcontext *shared = NULL;
243
244
uint32_t minor_ver = 1;
245
uint32_t major_ver = 2;
246
uint32_t flags = 0;
247
unsigned api;
248
int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
249
int release = __DRI_CTX_RELEASE_BEHAVIOR_FLUSH;
250
uint32_t ctx_attribs[2 * 6];
251
unsigned num_ctx_attribs = 0;
252
uint32_t render_type;
253
254
/* Remap the GLX tokens to DRI2 tokens.
255
*/
256
if (!dri2_convert_glx_attribs(num_attribs, attribs,
257
&major_ver, &minor_ver,
258
&render_type, &flags, &api,
259
&reset, &release, error))
260
goto error_exit;
261
262
if (!dri2_check_no_error(flags, shareList, major_ver, error)) {
263
goto error_exit;
264
}
265
266
/* Check the renderType value */
267
if (!validate_renderType_against_config(config_base, render_type))
268
goto error_exit;
269
270
if (shareList) {
271
/* If the shareList context is not a DRI3 context, we cannot possibly
272
* create a DRI3 context that shares it.
273
*/
274
if (shareList->vtable->destroy != dri3_destroy_context) {
275
return NULL;
276
}
277
278
pcp_shared = (struct dri3_context *) shareList;
279
shared = pcp_shared->driContext;
280
}
281
282
pcp = calloc(1, sizeof *pcp);
283
if (pcp == NULL) {
284
*error = __DRI_CTX_ERROR_NO_MEMORY;
285
goto error_exit;
286
}
287
288
if (!glx_context_init(&pcp->base, &psc->base, config_base))
289
goto error_exit;
290
291
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
292
ctx_attribs[num_ctx_attribs++] = major_ver;
293
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
294
ctx_attribs[num_ctx_attribs++] = minor_ver;
295
296
/* Only send a value when the non-default value is requested. By doing
297
* this we don't have to check the driver's DRI3 version before sending the
298
* default value.
299
*/
300
if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
301
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
302
ctx_attribs[num_ctx_attribs++] = reset;
303
}
304
305
if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
306
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
307
ctx_attribs[num_ctx_attribs++] = release;
308
}
309
310
if (flags != 0) {
311
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
312
313
/* The current __DRI_CTX_FLAG_* values are identical to the
314
* GLX_CONTEXT_*_BIT values.
315
*/
316
ctx_attribs[num_ctx_attribs++] = flags;
317
318
if (flags & __DRI_CTX_FLAG_NO_ERROR)
319
pcp->base.noError = GL_TRUE;
320
}
321
322
pcp->driContext =
323
(*psc->image_driver->createContextAttribs) (psc->driScreen,
324
api,
325
config ? config->driConfig
326
: NULL,
327
shared,
328
num_ctx_attribs / 2,
329
ctx_attribs,
330
error,
331
pcp);
332
333
if (pcp->driContext == NULL)
334
goto error_exit;
335
336
pcp->base.vtable = &dri3_context_vtable;
337
338
return &pcp->base;
339
340
error_exit:
341
free(pcp);
342
343
return NULL;
344
}
345
346
static void
347
dri3_destroy_drawable(__GLXDRIdrawable *base)
348
{
349
struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
350
351
loader_dri3_drawable_fini(&pdraw->loader_drawable);
352
353
free(pdraw);
354
}
355
356
static __GLXDRIdrawable *
357
dri3_create_drawable(struct glx_screen *base, XID xDrawable,
358
GLXDrawable drawable, struct glx_config *config_base)
359
{
360
struct dri3_drawable *pdraw;
361
struct dri3_screen *psc = (struct dri3_screen *) base;
362
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
363
bool has_multibuffer = false;
364
#ifdef HAVE_DRI3_MODIFIERS
365
const struct dri3_display *const pdp = (struct dri3_display *)
366
base->display->dri3Display;
367
#endif
368
369
pdraw = calloc(1, sizeof(*pdraw));
370
if (!pdraw)
371
return NULL;
372
373
pdraw->base.destroyDrawable = dri3_destroy_drawable;
374
pdraw->base.xDrawable = xDrawable;
375
pdraw->base.drawable = drawable;
376
pdraw->base.psc = &psc->base;
377
378
#ifdef HAVE_DRI3_MODIFIERS
379
if ((psc->image && psc->image->base.version >= 15) &&
380
(pdp->dri3Major > 1 || (pdp->dri3Major == 1 && pdp->dri3Minor >= 2)) &&
381
(pdp->presentMajor > 1 ||
382
(pdp->presentMajor == 1 && pdp->presentMinor >= 2)))
383
has_multibuffer = true;
384
#endif
385
386
(void) __glXInitialize(psc->base.dpy);
387
388
if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy),
389
xDrawable, psc->driScreen,
390
psc->is_different_gpu, has_multibuffer,
391
config->driConfig,
392
&psc->loader_dri3_ext, &glx_dri3_vtable,
393
&pdraw->loader_drawable)) {
394
free(pdraw);
395
return NULL;
396
}
397
398
pdraw->loader_drawable.dri_screen_display_gpu = psc->driScreenDisplayGPU;
399
return &pdraw->base;
400
}
401
402
/** dri3_wait_for_msc
403
*
404
* Get the X server to send an event when the target msc/divisor/remainder is
405
* reached.
406
*/
407
static int
408
dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
409
int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
410
{
411
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
412
413
loader_dri3_wait_for_msc(&priv->loader_drawable, target_msc, divisor,
414
remainder, ust, msc, sbc);
415
416
return 1;
417
}
418
419
/** dri3_drawable_get_msc
420
*
421
* Return the current UST/MSC/SBC triplet by asking the server
422
* for an event
423
*/
424
static int
425
dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
426
int64_t *ust, int64_t *msc, int64_t *sbc)
427
{
428
return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc);
429
}
430
431
/** dri3_wait_for_sbc
432
*
433
* Wait for the completed swap buffer count to reach the specified
434
* target. Presumably the application knows that this will be reached with
435
* outstanding complete events, or we're going to be here awhile.
436
*/
437
static int
438
dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
439
int64_t *msc, int64_t *sbc)
440
{
441
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
442
443
return loader_dri3_wait_for_sbc(&priv->loader_drawable, target_sbc,
444
ust, msc, sbc);
445
}
446
447
static void
448
dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y,
449
int width, int height,
450
Bool flush)
451
{
452
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
453
454
loader_dri3_copy_sub_buffer(&priv->loader_drawable, x, y,
455
width, height, flush);
456
}
457
458
static void
459
dri3_wait_x(struct glx_context *gc)
460
{
461
struct dri3_drawable *priv = (struct dri3_drawable *)
462
GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
463
464
if (priv)
465
loader_dri3_wait_x(&priv->loader_drawable);
466
}
467
468
static void
469
dri3_wait_gl(struct glx_context *gc)
470
{
471
struct dri3_drawable *priv = (struct dri3_drawable *)
472
GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
473
474
if (priv)
475
loader_dri3_wait_gl(&priv->loader_drawable);
476
}
477
478
/**
479
* Called by the driver when it needs to update the real front buffer with the
480
* contents of its fake front buffer.
481
*/
482
static void
483
dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
484
{
485
struct loader_dri3_drawable *draw = loaderPrivate;
486
struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
487
struct dri3_screen *psc;
488
489
if (!pdraw)
490
return;
491
492
if (!pdraw->base.psc)
493
return;
494
495
psc = (struct dri3_screen *) pdraw->base.psc;
496
497
(void) __glXInitialize(psc->base.dpy);
498
499
loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
500
501
psc->f->invalidate(driDrawable);
502
loader_dri3_wait_gl(draw);
503
}
504
505
/**
506
* Make sure all pending swapbuffers have been submitted to hardware
507
*
508
* \param driDrawable[in] Pointer to the dri drawable whose swaps we are
509
* flushing.
510
* \param loaderPrivate[in] Pointer to the corresponding struct
511
* loader_dri_drawable.
512
*/
513
static void
514
dri3_flush_swap_buffers(__DRIdrawable *driDrawable, void *loaderPrivate)
515
{
516
struct loader_dri3_drawable *draw = loaderPrivate;
517
struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
518
struct dri3_screen *psc;
519
520
if (!pdraw)
521
return;
522
523
if (!pdraw->base.psc)
524
return;
525
526
psc = (struct dri3_screen *) pdraw->base.psc;
527
528
(void) __glXInitialize(psc->base.dpy);
529
loader_dri3_swapbuffer_barrier(draw);
530
}
531
532
static void
533
dri_set_background_context(void *loaderPrivate)
534
{
535
struct dri3_context *pcp = (struct dri3_context *)loaderPrivate;
536
__glXSetCurrentContext(&pcp->base);
537
}
538
539
static GLboolean
540
dri_is_thread_safe(void *loaderPrivate)
541
{
542
/* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
543
* during draw so we're safe here.
544
*/
545
return true;
546
}
547
548
/* The image loader extension record for DRI3
549
*/
550
static const __DRIimageLoaderExtension imageLoaderExtension = {
551
.base = { __DRI_IMAGE_LOADER, 3 },
552
553
.getBuffers = loader_dri3_get_buffers,
554
.flushFrontBuffer = dri3_flush_front_buffer,
555
.flushSwapBuffers = dri3_flush_swap_buffers,
556
};
557
558
const __DRIuseInvalidateExtension dri3UseInvalidate = {
559
.base = { __DRI_USE_INVALIDATE, 1 }
560
};
561
562
static const __DRIbackgroundCallableExtension driBackgroundCallable = {
563
.base = { __DRI_BACKGROUND_CALLABLE, 2 },
564
565
.setBackgroundContext = dri_set_background_context,
566
.isThreadSafe = dri_is_thread_safe,
567
};
568
569
static const __DRIextension *loader_extensions[] = {
570
&imageLoaderExtension.base,
571
&dri3UseInvalidate.base,
572
&driBackgroundCallable.base,
573
NULL
574
};
575
576
/** dri3_swap_buffers
577
*
578
* Make the current back buffer visible using the present extension
579
*/
580
static int64_t
581
dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
582
int64_t remainder, Bool flush)
583
{
584
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
585
unsigned flags = __DRI2_FLUSH_DRAWABLE;
586
587
if (flush)
588
flags |= __DRI2_FLUSH_CONTEXT;
589
590
return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
591
target_msc, divisor, remainder,
592
flags, NULL, 0, false);
593
}
594
595
static int
596
dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
597
{
598
struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
599
600
return loader_dri3_query_buffer_age(&priv->loader_drawable);
601
}
602
603
/** dri3_destroy_screen
604
*/
605
static void
606
dri3_destroy_screen(struct glx_screen *base)
607
{
608
struct dri3_screen *psc = (struct dri3_screen *) base;
609
610
/* Free the direct rendering per screen data */
611
if (psc->is_different_gpu) {
612
if (psc->driScreenDisplayGPU) {
613
loader_dri3_close_screen(psc->driScreenDisplayGPU);
614
(*psc->core->destroyScreen) (psc->driScreenDisplayGPU);
615
}
616
close(psc->fd_display_gpu);
617
}
618
loader_dri3_close_screen(psc->driScreen);
619
(*psc->core->destroyScreen) (psc->driScreen);
620
driDestroyConfigs(psc->driver_configs);
621
close(psc->fd);
622
free(psc);
623
}
624
625
/** dri3_set_swap_interval
626
*
627
* Record the application swap interval specification,
628
*/
629
static int
630
dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
631
{
632
assert(pdraw != NULL);
633
634
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
635
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
636
struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
637
638
if (psc->config)
639
psc->config->configQueryi(psc->driScreen,
640
"vblank_mode", &vblank_mode);
641
642
switch (vblank_mode) {
643
case DRI_CONF_VBLANK_NEVER:
644
if (interval != 0)
645
return GLX_BAD_VALUE;
646
break;
647
case DRI_CONF_VBLANK_ALWAYS_SYNC:
648
if (interval <= 0)
649
return GLX_BAD_VALUE;
650
break;
651
default:
652
break;
653
}
654
655
loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
656
657
return 0;
658
}
659
660
/** dri3_get_swap_interval
661
*
662
* Return the stored swap interval
663
*/
664
static int
665
dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
666
{
667
assert(pdraw != NULL);
668
669
struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
670
671
return priv->loader_drawable.swap_interval;
672
}
673
674
static void
675
dri3_bind_tex_image(__GLXDRIdrawable *base,
676
int buffer, const int *attrib_list)
677
{
678
struct glx_context *gc = __glXGetCurrentContext();
679
struct dri3_context *pcp = (struct dri3_context *) gc;
680
struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
681
struct dri3_screen *psc;
682
683
if (pdraw != NULL) {
684
psc = (struct dri3_screen *) base->psc;
685
686
psc->f->invalidate(pdraw->loader_drawable.dri_drawable);
687
688
XSync(gc->currentDpy, false);
689
690
(*psc->texBuffer->setTexBuffer2) (pcp->driContext,
691
pdraw->base.textureTarget,
692
pdraw->base.textureFormat,
693
pdraw->loader_drawable.dri_drawable);
694
}
695
}
696
697
static void
698
dri3_release_tex_image(__GLXDRIdrawable *base, int buffer)
699
{
700
struct glx_context *gc = __glXGetCurrentContext();
701
struct dri3_context *pcp = (struct dri3_context *) gc;
702
struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
703
struct dri3_screen *psc;
704
705
if (pdraw != NULL) {
706
psc = (struct dri3_screen *) base->psc;
707
708
if (psc->texBuffer->base.version >= 3 &&
709
psc->texBuffer->releaseTexBuffer != NULL)
710
(*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
711
pdraw->base.textureTarget,
712
pdraw->loader_drawable.dri_drawable);
713
}
714
}
715
716
static const struct glx_context_vtable dri3_context_vtable = {
717
.destroy = dri3_destroy_context,
718
.bind = dri3_bind_context,
719
.unbind = dri3_unbind_context,
720
.wait_gl = dri3_wait_gl,
721
.wait_x = dri3_wait_x,
722
.interop_query_device_info = dri3_interop_query_device_info,
723
.interop_export_object = dri3_interop_export_object
724
};
725
726
/** dri3_bind_extensions
727
*
728
* Enable all of the extensions supported on DRI3
729
*/
730
static void
731
dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
732
const char *driverName)
733
{
734
const __DRIextension **extensions;
735
unsigned mask;
736
int i;
737
738
extensions = psc->core->getExtensions(psc->driScreen);
739
740
__glXEnableDirectExtension(&psc->base, "GLX_EXT_swap_control");
741
__glXEnableDirectExtension(&psc->base, "GLX_EXT_swap_control_tear");
742
__glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
743
__glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
744
__glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
745
__glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
746
747
mask = psc->image_driver->getAPIMask(psc->driScreen);
748
749
__glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
750
__glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
751
__glXEnableDirectExtension(&psc->base, "GLX_EXT_no_config_context");
752
753
if ((mask & ((1 << __DRI_API_GLES) |
754
(1 << __DRI_API_GLES2) |
755
(1 << __DRI_API_GLES3))) != 0) {
756
__glXEnableDirectExtension(&psc->base,
757
"GLX_EXT_create_context_es_profile");
758
__glXEnableDirectExtension(&psc->base,
759
"GLX_EXT_create_context_es2_profile");
760
}
761
762
for (i = 0; extensions[i]; i++) {
763
/* when on a different gpu than the server, the server pixmaps
764
* can have a tiling mode we can't read. Thus we can't create
765
* a texture from them.
766
*/
767
if (!psc->is_different_gpu &&
768
(strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
769
psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
770
__glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
771
}
772
773
if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
774
psc->f = (__DRI2flushExtension *) extensions[i];
775
/* internal driver extension, no GL extension exposed */
776
}
777
778
if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
779
psc->image = (__DRIimageExtension *) extensions[i];
780
781
if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
782
psc->config = (__DRI2configQueryExtension *) extensions[i];
783
784
if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
785
__glXEnableDirectExtension(&psc->base,
786
"GLX_ARB_create_context_robustness");
787
788
if (strcmp(extensions[i]->name, __DRI2_NO_ERROR) == 0)
789
__glXEnableDirectExtension(&psc->base,
790
"GLX_ARB_create_context_no_error");
791
792
if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
793
psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
794
__glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
795
}
796
797
if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
798
psc->interop = (__DRI2interopExtension*)extensions[i];
799
800
if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
801
__glXEnableDirectExtension(&psc->base,
802
"GLX_ARB_context_flush_control");
803
}
804
}
805
806
static char *
807
dri3_get_driver_name(struct glx_screen *glx_screen)
808
{
809
struct dri3_screen *psc = (struct dri3_screen *)glx_screen;
810
811
return loader_get_driver_for_fd(psc->fd);
812
}
813
814
static const struct glx_screen_vtable dri3_screen_vtable = {
815
.create_context = dri_common_create_context,
816
.create_context_attribs = dri3_create_context_attribs,
817
.query_renderer_integer = dri3_query_renderer_integer,
818
.query_renderer_string = dri3_query_renderer_string,
819
.get_driver_name = dri3_get_driver_name,
820
};
821
822
/** dri3_create_screen
823
*
824
* Initialize DRI3 on the specified screen.
825
*
826
* Opens the DRI device, locates the appropriate DRI driver
827
* and loads that.
828
*
829
* Checks to see if the driver supports the necessary extensions
830
*
831
* Initializes the driver for the screen and sets up our structures
832
*/
833
834
static struct glx_screen *
835
dri3_create_screen(int screen, struct glx_display * priv)
836
{
837
xcb_connection_t *c = XGetXCBConnection(priv->dpy);
838
const __DRIconfig **driver_configs;
839
const __DRIextension **extensions;
840
const struct dri3_display *const pdp = (struct dri3_display *)
841
priv->dri3Display;
842
struct dri3_screen *psc;
843
__GLXDRIscreen *psp;
844
struct glx_config *configs = NULL, *visuals = NULL;
845
char *driverName, *driverNameDisplayGPU, *tmp;
846
int i;
847
848
psc = calloc(1, sizeof *psc);
849
if (psc == NULL)
850
return NULL;
851
852
psc->fd = -1;
853
psc->fd_display_gpu = -1;
854
855
if (!glx_screen_init(&psc->base, screen, priv)) {
856
free(psc);
857
return NULL;
858
}
859
860
psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
861
if (psc->fd < 0) {
862
int conn_error = xcb_connection_has_error(c);
863
864
glx_screen_cleanup(&psc->base);
865
free(psc);
866
InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
867
868
if (conn_error)
869
ErrorMessageF("Connection closed during DRI3 initialization failure");
870
871
return NULL;
872
}
873
874
psc->fd_display_gpu = fcntl(psc->fd, F_DUPFD_CLOEXEC, 3);
875
psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
876
if (!psc->is_different_gpu) {
877
close(psc->fd_display_gpu);
878
psc->fd_display_gpu = -1;
879
}
880
881
driverName = loader_get_driver_for_fd(psc->fd);
882
if (!driverName) {
883
ErrorMessageF("No driver found\n");
884
goto handle_error;
885
}
886
887
extensions = driOpenDriver(driverName, &psc->driver);
888
if (extensions == NULL)
889
goto handle_error;
890
891
for (i = 0; extensions[i]; i++) {
892
if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
893
psc->core = (__DRIcoreExtension *) extensions[i];
894
if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
895
psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
896
}
897
898
899
if (psc->core == NULL) {
900
ErrorMessageF("core dri driver extension not found\n");
901
goto handle_error;
902
}
903
904
if (psc->image_driver == NULL) {
905
ErrorMessageF("image driver extension not found\n");
906
goto handle_error;
907
}
908
909
if (psc->is_different_gpu) {
910
driverNameDisplayGPU = loader_get_driver_for_fd(psc->fd_display_gpu);
911
if (driverNameDisplayGPU) {
912
913
/* check if driver name is matching so that non mesa drivers
914
* will not crash. Also need this check since image extension
915
* pointer from render gpu is shared with display gpu. Image
916
* extension pointer is shared because it keeps things simple.
917
*/
918
if (strcmp(driverName, driverNameDisplayGPU) == 0) {
919
psc->driScreenDisplayGPU =
920
psc->image_driver->createNewScreen2(screen, psc->fd_display_gpu,
921
pdp->loader_extensions,
922
extensions,
923
&driver_configs, psc);
924
}
925
926
free(driverNameDisplayGPU);
927
}
928
}
929
930
psc->driScreen =
931
psc->image_driver->createNewScreen2(screen, psc->fd,
932
pdp->loader_extensions,
933
extensions,
934
&driver_configs, psc);
935
936
if (psc->driScreen == NULL) {
937
ErrorMessageF("failed to create dri screen\n");
938
goto handle_error;
939
}
940
941
dri3_bind_extensions(psc, priv, driverName);
942
943
if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
944
ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
945
goto handle_error;
946
}
947
948
if (!psc->f || psc->f->base.version < 4) {
949
ErrorMessageF("Version 4 or later of flush extension not found\n");
950
goto handle_error;
951
}
952
953
if (psc->is_different_gpu && psc->image->base.version < 9) {
954
ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
955
goto handle_error;
956
}
957
958
if (psc->is_different_gpu && !psc->image->blitImage) {
959
ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
960
goto handle_error;
961
}
962
963
if (!psc->is_different_gpu && (
964
!psc->texBuffer || psc->texBuffer->base.version < 2 ||
965
!psc->texBuffer->setTexBuffer2
966
)) {
967
ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
968
goto handle_error;
969
}
970
971
psc->loader_dri3_ext.core = psc->core;
972
psc->loader_dri3_ext.image_driver = psc->image_driver;
973
psc->loader_dri3_ext.flush = psc->f;
974
psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
975
psc->loader_dri3_ext.image = psc->image;
976
psc->loader_dri3_ext.config = psc->config;
977
978
configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
979
visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
980
981
if (!configs || !visuals) {
982
ErrorMessageF("No matching fbConfigs or visuals found\n");
983
goto handle_error;
984
}
985
986
glx_config_destroy_list(psc->base.configs);
987
psc->base.configs = configs;
988
glx_config_destroy_list(psc->base.visuals);
989
psc->base.visuals = visuals;
990
991
psc->driver_configs = driver_configs;
992
993
psc->base.vtable = &dri3_screen_vtable;
994
psp = &psc->vtable;
995
psc->base.driScreen = psp;
996
psp->destroyScreen = dri3_destroy_screen;
997
psp->createDrawable = dri3_create_drawable;
998
psp->swapBuffers = dri3_swap_buffers;
999
1000
psp->getDrawableMSC = dri3_drawable_get_msc;
1001
psp->waitForMSC = dri3_wait_for_msc;
1002
psp->waitForSBC = dri3_wait_for_sbc;
1003
psp->setSwapInterval = dri3_set_swap_interval;
1004
psp->getSwapInterval = dri3_get_swap_interval;
1005
psp->bindTexImage = dri3_bind_tex_image;
1006
psp->releaseTexImage = dri3_release_tex_image;
1007
1008
__glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1009
__glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1010
1011
psp->copySubBuffer = dri3_copy_sub_buffer;
1012
__glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1013
1014
psp->getBufferAge = dri3_get_buffer_age;
1015
__glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
1016
1017
if (psc->config->base.version > 1 &&
1018
psc->config->configQuerys(psc->driScreen, "glx_extension_override",
1019
&tmp) == 0)
1020
__glXParseExtensionOverride(&psc->base, tmp);
1021
1022
if (psc->config->base.version > 1 &&
1023
psc->config->configQuerys(psc->driScreen,
1024
"indirect_gl_extension_override",
1025
&tmp) == 0)
1026
__IndirectGlParseExtensionOverride(&psc->base, tmp);
1027
1028
free(driverName);
1029
1030
tmp = getenv("LIBGL_SHOW_FPS");
1031
psc->show_fps_interval = tmp ? atoi(tmp) : 0;
1032
if (psc->show_fps_interval < 0)
1033
psc->show_fps_interval = 0;
1034
1035
InfoMessageF("Using DRI3 for screen %d\n", screen);
1036
1037
return &psc->base;
1038
1039
handle_error:
1040
CriticalErrorMessageF("failed to load driver: %s\n", driverName ? driverName : "(null)");
1041
1042
if (configs)
1043
glx_config_destroy_list(configs);
1044
if (visuals)
1045
glx_config_destroy_list(visuals);
1046
if (psc->driScreen)
1047
psc->core->destroyScreen(psc->driScreen);
1048
psc->driScreen = NULL;
1049
if (psc->driScreenDisplayGPU)
1050
psc->core->destroyScreen(psc->driScreenDisplayGPU);
1051
psc->driScreenDisplayGPU = NULL;
1052
if (psc->fd >= 0)
1053
close(psc->fd);
1054
if (psc->fd_display_gpu >= 0)
1055
close(psc->fd_display_gpu);
1056
if (psc->driver)
1057
dlclose(psc->driver);
1058
1059
free(driverName);
1060
glx_screen_cleanup(&psc->base);
1061
free(psc);
1062
1063
return NULL;
1064
}
1065
1066
/** dri_destroy_display
1067
*
1068
* Called from __glXFreeDisplayPrivate.
1069
*/
1070
static void
1071
dri3_destroy_display(__GLXDRIdisplay * dpy)
1072
{
1073
free(dpy);
1074
}
1075
1076
/* Only request versions of these protocols which we actually support. */
1077
#define DRI3_SUPPORTED_MAJOR 1
1078
#define PRESENT_SUPPORTED_MAJOR 1
1079
1080
#ifdef HAVE_DRI3_MODIFIERS
1081
#define DRI3_SUPPORTED_MINOR 2
1082
#define PRESENT_SUPPORTED_MINOR 2
1083
#else
1084
#define PRESENT_SUPPORTED_MINOR 0
1085
#define DRI3_SUPPORTED_MINOR 0
1086
#endif
1087
1088
/** dri3_create_display
1089
*
1090
* Allocate, initialize and return a __DRIdisplayPrivate object.
1091
* This is called from __glXInitialize() when we are given a new
1092
* display pointer. This is public to that function, but hidden from
1093
* outside of libGL.
1094
*/
1095
_X_HIDDEN __GLXDRIdisplay *
1096
dri3_create_display(Display * dpy)
1097
{
1098
struct dri3_display *pdp;
1099
xcb_connection_t *c = XGetXCBConnection(dpy);
1100
xcb_dri3_query_version_cookie_t dri3_cookie;
1101
xcb_dri3_query_version_reply_t *dri3_reply;
1102
xcb_present_query_version_cookie_t present_cookie;
1103
xcb_present_query_version_reply_t *present_reply;
1104
xcb_generic_error_t *error;
1105
const xcb_query_extension_reply_t *extension;
1106
1107
xcb_prefetch_extension_data(c, &xcb_dri3_id);
1108
xcb_prefetch_extension_data(c, &xcb_present_id);
1109
1110
extension = xcb_get_extension_data(c, &xcb_dri3_id);
1111
if (!(extension && extension->present))
1112
return NULL;
1113
1114
extension = xcb_get_extension_data(c, &xcb_present_id);
1115
if (!(extension && extension->present))
1116
return NULL;
1117
1118
dri3_cookie = xcb_dri3_query_version(c,
1119
DRI3_SUPPORTED_MAJOR,
1120
DRI3_SUPPORTED_MINOR);
1121
present_cookie = xcb_present_query_version(c,
1122
PRESENT_SUPPORTED_MAJOR,
1123
PRESENT_SUPPORTED_MINOR);
1124
1125
pdp = malloc(sizeof *pdp);
1126
if (pdp == NULL)
1127
return NULL;
1128
1129
dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1130
if (!dri3_reply) {
1131
free(error);
1132
goto no_extension;
1133
}
1134
1135
pdp->dri3Major = dri3_reply->major_version;
1136
pdp->dri3Minor = dri3_reply->minor_version;
1137
free(dri3_reply);
1138
1139
present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1140
if (!present_reply) {
1141
free(error);
1142
goto no_extension;
1143
}
1144
pdp->presentMajor = present_reply->major_version;
1145
pdp->presentMinor = present_reply->minor_version;
1146
free(present_reply);
1147
1148
pdp->base.destroyDisplay = dri3_destroy_display;
1149
pdp->base.createScreen = dri3_create_screen;
1150
1151
pdp->loader_extensions = loader_extensions;
1152
1153
return &pdp->base;
1154
no_extension:
1155
free(pdp);
1156
return NULL;
1157
}
1158
1159
#endif /* GLX_DIRECT_RENDERING */
1160
1161