Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/glx/dri2_glx.c
4558 views
1
/*
2
* Copyright © 2008 Red Hat, Inc.
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Soft-
6
* ware"), to deal in the Software without restriction, including without
7
* limitation the rights to use, copy, modify, merge, publish, distribute,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, provided that the above copyright
10
* notice(s) and this permission notice appear in all copies of the Soft-
11
* ware and that both the above copyright notice(s) and this permission
12
* notice appear in supporting documentation.
13
*
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17
* RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18
* THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19
* QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22
* MANCE OF THIS SOFTWARE.
23
*
24
* Except as contained in this notice, the name of a copyright holder shall
25
* not be used in advertising or otherwise to promote the sale, use or
26
* other dealings in this Software without prior written authorization of
27
* the copyright holder.
28
*
29
* Authors:
30
* Kristian Høgsberg ([email protected])
31
*/
32
33
#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
34
35
#include <X11/Xlib.h>
36
#include <X11/extensions/Xfixes.h>
37
#include <X11/Xlib-xcb.h>
38
#include <xcb/xcb.h>
39
#include <xcb/dri2.h>
40
#include "glxclient.h"
41
#include <X11/extensions/dri2proto.h>
42
#include <dlfcn.h>
43
#include <fcntl.h>
44
#include <unistd.h>
45
#include <sys/types.h>
46
#include <sys/mman.h>
47
#include <sys/time.h>
48
#include "dri2.h"
49
#include "dri_common.h"
50
#include "dri2_priv.h"
51
#include "loader.h"
52
53
/* From driconf.h, user exposed so should be stable */
54
#define DRI_CONF_VBLANK_NEVER 0
55
#define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
56
#define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
57
#define DRI_CONF_VBLANK_ALWAYS_SYNC 3
58
59
#undef DRI2_MINOR
60
#define DRI2_MINOR 1
61
62
struct dri2_display
63
{
64
__GLXDRIdisplay base;
65
66
/*
67
** XFree86-DRI version information
68
*/
69
int driMajor;
70
int driMinor;
71
int driPatch;
72
int swapAvailable;
73
int invalidateAvailable;
74
75
__glxHashTable *dri2Hash;
76
77
const __DRIextension *loader_extensions[5];
78
};
79
80
struct dri2_drawable
81
{
82
__GLXDRIdrawable base;
83
__DRIdrawable *driDrawable;
84
__DRIbuffer buffers[5];
85
int bufferCount;
86
int width, height;
87
int have_back;
88
int have_fake_front;
89
int swap_interval;
90
91
uint64_t previous_time;
92
unsigned frames;
93
};
94
95
static const struct glx_context_vtable dri2_context_vtable;
96
97
/* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
98
* low halves separately. This helps you split them.
99
*/
100
static void
101
split_counter(uint64_t counter, uint32_t *hi, uint32_t *lo)
102
{
103
*hi = (counter >> 32);
104
*lo = counter & 0xffffffff;
105
}
106
107
static uint64_t
108
merge_counter(uint32_t hi, uint32_t lo)
109
{
110
return ((uint64_t)hi << 32) | lo;
111
}
112
113
static void
114
dri2_destroy_context(struct glx_context *context)
115
{
116
struct dri2_context *pcp = (struct dri2_context *) context;
117
struct dri2_screen *psc = (struct dri2_screen *) context->psc;
118
119
driReleaseDrawables(&pcp->base);
120
121
free((char *) context->extensions);
122
123
(*psc->core->destroyContext) (pcp->driContext);
124
125
free(pcp);
126
}
127
128
static Bool
129
dri2_bind_context(struct glx_context *context, struct glx_context *old,
130
GLXDrawable draw, GLXDrawable read)
131
{
132
struct dri2_context *pcp = (struct dri2_context *) context;
133
struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
134
struct dri2_drawable *pdraw, *pread;
135
__DRIdrawable *dri_draw = NULL, *dri_read = NULL;
136
struct glx_display *dpyPriv = psc->base.display;
137
struct dri2_display *pdp;
138
139
pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
140
pread = (struct dri2_drawable *) driFetchDrawable(context, read);
141
142
driReleaseDrawables(&pcp->base);
143
144
if (pdraw)
145
dri_draw = pdraw->driDrawable;
146
else if (draw != None)
147
return GLXBadDrawable;
148
149
if (pread)
150
dri_read = pread->driDrawable;
151
else if (read != None)
152
return GLXBadDrawable;
153
154
if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
155
return GLXBadContext;
156
157
/* If the server doesn't send invalidate events, we may miss a
158
* resize before the rendering starts. Invalidate the buffers now
159
* so the driver will recheck before rendering starts. */
160
pdp = (struct dri2_display *) dpyPriv->dri2Display;
161
if (!pdp->invalidateAvailable && pdraw) {
162
dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
163
if (pread != pdraw && pread)
164
dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
165
}
166
167
return Success;
168
}
169
170
static void
171
dri2_unbind_context(struct glx_context *context, struct glx_context *new)
172
{
173
struct dri2_context *pcp = (struct dri2_context *) context;
174
struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
175
176
(*psc->core->unbindContext) (pcp->driContext);
177
}
178
179
static struct glx_context *
180
dri2_create_context_attribs(struct glx_screen *base,
181
struct glx_config *config_base,
182
struct glx_context *shareList,
183
unsigned num_attribs,
184
const uint32_t *attribs,
185
unsigned *error)
186
{
187
struct dri2_context *pcp = NULL;
188
struct dri2_context *pcp_shared = NULL;
189
struct dri2_screen *psc = (struct dri2_screen *) base;
190
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
191
__DRIcontext *shared = NULL;
192
193
uint32_t minor_ver;
194
uint32_t major_ver;
195
uint32_t renderType;
196
uint32_t flags;
197
unsigned api;
198
int reset;
199
int release;
200
uint32_t ctx_attribs[2 * 6];
201
unsigned num_ctx_attribs = 0;
202
203
if (psc->dri2->base.version < 3) {
204
*error = __DRI_CTX_ERROR_NO_MEMORY;
205
goto error_exit;
206
}
207
208
/* Remap the GLX tokens to DRI2 tokens.
209
*/
210
if (!dri2_convert_glx_attribs(num_attribs, attribs,
211
&major_ver, &minor_ver, &renderType, &flags,
212
&api, &reset, &release, error))
213
goto error_exit;
214
215
if (!dri2_check_no_error(flags, shareList, major_ver, error)) {
216
goto error_exit;
217
}
218
219
/* Check the renderType value */
220
if (!validate_renderType_against_config(config_base, renderType))
221
goto error_exit;
222
223
if (shareList) {
224
/* If the shareList context is not a DRI2 context, we cannot possibly
225
* create a DRI2 context that shares it.
226
*/
227
if (shareList->vtable->destroy != dri2_destroy_context) {
228
return NULL;
229
}
230
231
pcp_shared = (struct dri2_context *) shareList;
232
shared = pcp_shared->driContext;
233
}
234
235
pcp = calloc(1, sizeof *pcp);
236
if (pcp == NULL) {
237
*error = __DRI_CTX_ERROR_NO_MEMORY;
238
goto error_exit;
239
}
240
241
if (!glx_context_init(&pcp->base, &psc->base, config_base))
242
goto error_exit;
243
244
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
245
ctx_attribs[num_ctx_attribs++] = major_ver;
246
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
247
ctx_attribs[num_ctx_attribs++] = minor_ver;
248
249
/* Only send a value when the non-default value is requested. By doing
250
* this we don't have to check the driver's DRI2 version before sending the
251
* default value.
252
*/
253
if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
254
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
255
ctx_attribs[num_ctx_attribs++] = reset;
256
}
257
258
if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
259
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
260
ctx_attribs[num_ctx_attribs++] = release;
261
}
262
263
if (flags != 0) {
264
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
265
266
/* The current __DRI_CTX_FLAG_* values are identical to the
267
* GLX_CONTEXT_*_BIT values.
268
*/
269
ctx_attribs[num_ctx_attribs++] = flags;
270
}
271
272
/* The renderType is retrieved from attribs, or set to default
273
* of GLX_RGBA_TYPE.
274
*/
275
pcp->base.renderType = renderType;
276
277
if (flags & __DRI_CTX_FLAG_NO_ERROR)
278
pcp->base.noError = GL_TRUE;
279
280
pcp->driContext =
281
(*psc->dri2->createContextAttribs) (psc->driScreen,
282
api,
283
config ? config->driConfig : NULL,
284
shared,
285
num_ctx_attribs / 2,
286
ctx_attribs,
287
error,
288
pcp);
289
290
if (pcp->driContext == NULL)
291
goto error_exit;
292
293
pcp->base.vtable = &dri2_context_vtable;
294
295
return &pcp->base;
296
297
error_exit:
298
free(pcp);
299
300
return NULL;
301
}
302
303
static void
304
dri2DestroyDrawable(__GLXDRIdrawable *base)
305
{
306
struct dri2_screen *psc = (struct dri2_screen *) base->psc;
307
struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
308
struct glx_display *dpyPriv = psc->base.display;
309
struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
310
311
__glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
312
(*psc->core->destroyDrawable) (pdraw->driDrawable);
313
314
/* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
315
* now, as the application explicitly asked to destroy the GLX
316
* drawable. Otherwise, for legacy drawables, we let the DRI2
317
* drawable linger on the server, since there's no good way of
318
* knowing when the application is done with it. The server will
319
* destroy the DRI2 drawable when it destroys the X drawable or the
320
* client exits anyway. */
321
if (pdraw->base.xDrawable != pdraw->base.drawable)
322
DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
323
324
free(pdraw);
325
}
326
327
static __GLXDRIdrawable *
328
dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
329
GLXDrawable drawable, struct glx_config *config_base)
330
{
331
struct dri2_drawable *pdraw;
332
struct dri2_screen *psc = (struct dri2_screen *) base;
333
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
334
struct glx_display *dpyPriv;
335
struct dri2_display *pdp;
336
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
337
338
dpyPriv = __glXInitialize(psc->base.dpy);
339
if (dpyPriv == NULL)
340
return NULL;
341
342
pdraw = calloc(1, sizeof(*pdraw));
343
if (!pdraw)
344
return NULL;
345
346
pdraw->base.destroyDrawable = dri2DestroyDrawable;
347
pdraw->base.xDrawable = xDrawable;
348
pdraw->base.drawable = drawable;
349
pdraw->base.psc = &psc->base;
350
pdraw->bufferCount = 0;
351
pdraw->swap_interval = 1; /* default may be overridden below */
352
pdraw->have_back = 0;
353
354
if (psc->config)
355
psc->config->configQueryi(psc->driScreen,
356
"vblank_mode", &vblank_mode);
357
358
switch (vblank_mode) {
359
case DRI_CONF_VBLANK_NEVER:
360
case DRI_CONF_VBLANK_DEF_INTERVAL_0:
361
pdraw->swap_interval = 0;
362
break;
363
case DRI_CONF_VBLANK_DEF_INTERVAL_1:
364
case DRI_CONF_VBLANK_ALWAYS_SYNC:
365
default:
366
pdraw->swap_interval = 1;
367
break;
368
}
369
370
DRI2CreateDrawable(psc->base.dpy, xDrawable);
371
pdp = (struct dri2_display *)dpyPriv->dri2Display;
372
/* Create a new drawable */
373
pdraw->driDrawable =
374
(*psc->dri2->createNewDrawable) (psc->driScreen,
375
config->driConfig, pdraw);
376
377
if (!pdraw->driDrawable) {
378
DRI2DestroyDrawable(psc->base.dpy, xDrawable);
379
free(pdraw);
380
return NULL;
381
}
382
383
if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
384
(*psc->core->destroyDrawable) (pdraw->driDrawable);
385
DRI2DestroyDrawable(psc->base.dpy, xDrawable);
386
free(pdraw);
387
return None;
388
}
389
390
/*
391
* Make sure server has the same swap interval we do for the new
392
* drawable.
393
*/
394
if (psc->vtable.setSwapInterval)
395
psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
396
397
return &pdraw->base;
398
}
399
400
static int
401
dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
402
int64_t *ust, int64_t *msc, int64_t *sbc)
403
{
404
xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
405
xcb_dri2_get_msc_cookie_t get_msc_cookie;
406
xcb_dri2_get_msc_reply_t *get_msc_reply;
407
408
get_msc_cookie = xcb_dri2_get_msc_unchecked(c, pdraw->xDrawable);
409
get_msc_reply = xcb_dri2_get_msc_reply(c, get_msc_cookie, NULL);
410
411
if (!get_msc_reply)
412
return 0;
413
414
*ust = merge_counter(get_msc_reply->ust_hi, get_msc_reply->ust_lo);
415
*msc = merge_counter(get_msc_reply->msc_hi, get_msc_reply->msc_lo);
416
*sbc = merge_counter(get_msc_reply->sbc_hi, get_msc_reply->sbc_lo);
417
free(get_msc_reply);
418
419
return 1;
420
}
421
422
static int
423
dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
424
int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
425
{
426
xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
427
xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
428
xcb_dri2_wait_msc_reply_t *wait_msc_reply;
429
uint32_t target_msc_hi, target_msc_lo;
430
uint32_t divisor_hi, divisor_lo;
431
uint32_t remainder_hi, remainder_lo;
432
433
split_counter(target_msc, &target_msc_hi, &target_msc_lo);
434
split_counter(divisor, &divisor_hi, &divisor_lo);
435
split_counter(remainder, &remainder_hi, &remainder_lo);
436
437
wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
438
target_msc_hi, target_msc_lo,
439
divisor_hi, divisor_lo,
440
remainder_hi, remainder_lo);
441
wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
442
443
if (!wait_msc_reply)
444
return 0;
445
446
*ust = merge_counter(wait_msc_reply->ust_hi, wait_msc_reply->ust_lo);
447
*msc = merge_counter(wait_msc_reply->msc_hi, wait_msc_reply->msc_lo);
448
*sbc = merge_counter(wait_msc_reply->sbc_hi, wait_msc_reply->sbc_lo);
449
free(wait_msc_reply);
450
451
return 1;
452
}
453
454
static int
455
dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
456
int64_t *msc, int64_t *sbc)
457
{
458
xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
459
xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie;
460
xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
461
uint32_t target_sbc_hi, target_sbc_lo;
462
463
split_counter(target_sbc, &target_sbc_hi, &target_sbc_lo);
464
465
wait_sbc_cookie = xcb_dri2_wait_sbc_unchecked(c, pdraw->xDrawable,
466
target_sbc_hi, target_sbc_lo);
467
wait_sbc_reply = xcb_dri2_wait_sbc_reply(c, wait_sbc_cookie, NULL);
468
469
if (!wait_sbc_reply)
470
return 0;
471
472
*ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
473
*msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
474
*sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
475
free(wait_sbc_reply);
476
477
return 1;
478
}
479
480
static __DRIcontext *
481
dri2GetCurrentContext()
482
{
483
struct glx_context *gc = __glXGetCurrentContext();
484
struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
485
486
return (gc != &dummyContext) ? dri2Ctx->driContext : NULL;
487
}
488
489
/**
490
* dri2Throttle - Request driver throttling
491
*
492
* This function uses the DRI2 throttle extension to give the
493
* driver the opportunity to throttle on flush front, copysubbuffer
494
* and swapbuffers.
495
*/
496
static void
497
dri2Throttle(struct dri2_screen *psc,
498
struct dri2_drawable *draw,
499
enum __DRI2throttleReason reason)
500
{
501
if (psc->throttle) {
502
__DRIcontext *ctx = dri2GetCurrentContext();
503
504
psc->throttle->throttle(ctx, draw->driDrawable, reason);
505
}
506
}
507
508
/**
509
* Asks the driver to flush any queued work necessary for serializing with the
510
* X command stream, and optionally the slightly more strict requirement of
511
* glFlush() equivalence (which would require flushing even if nothing had
512
* been drawn to a window system framebuffer, for example).
513
*/
514
static void
515
dri2Flush(struct dri2_screen *psc,
516
__DRIcontext *ctx,
517
struct dri2_drawable *draw,
518
unsigned flags,
519
enum __DRI2throttleReason throttle_reason)
520
{
521
if (ctx && psc->f && psc->f->base.version >= 4) {
522
psc->f->flush_with_flags(ctx, draw->driDrawable, flags, throttle_reason);
523
} else {
524
if (flags & __DRI2_FLUSH_CONTEXT)
525
glFlush();
526
527
if (psc->f)
528
psc->f->flush(draw->driDrawable);
529
530
dri2Throttle(psc, draw, throttle_reason);
531
}
532
}
533
534
static void
535
__dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
536
int width, int height,
537
enum __DRI2throttleReason reason, Bool flush)
538
{
539
struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
540
struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
541
XRectangle xrect;
542
XserverRegion region;
543
__DRIcontext *ctx = dri2GetCurrentContext();
544
unsigned flags;
545
546
/* Check we have the right attachments */
547
if (!priv->have_back)
548
return;
549
550
xrect.x = x;
551
xrect.y = priv->height - y - height;
552
xrect.width = width;
553
xrect.height = height;
554
555
flags = __DRI2_FLUSH_DRAWABLE;
556
if (flush)
557
flags |= __DRI2_FLUSH_CONTEXT;
558
dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_COPYSUBBUFFER);
559
560
region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
561
DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
562
DRI2BufferFrontLeft, DRI2BufferBackLeft);
563
564
/* Refresh the fake front (if present) after we just damaged the real
565
* front.
566
*/
567
if (priv->have_fake_front)
568
DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
569
DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
570
571
XFixesDestroyRegion(psc->base.dpy, region);
572
}
573
574
static void
575
dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
576
int width, int height, Bool flush)
577
{
578
__dri2CopySubBuffer(pdraw, x, y, width, height,
579
__DRI2_THROTTLE_COPYSUBBUFFER, flush);
580
}
581
582
583
static void
584
dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
585
{
586
XRectangle xrect;
587
XserverRegion region;
588
struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
589
590
xrect.x = 0;
591
xrect.y = 0;
592
xrect.width = priv->width;
593
xrect.height = priv->height;
594
595
if (psc->f)
596
(*psc->f->flush) (priv->driDrawable);
597
598
region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
599
DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
600
XFixesDestroyRegion(psc->base.dpy, region);
601
602
}
603
604
static void
605
dri2_wait_x(struct glx_context *gc)
606
{
607
struct dri2_drawable *priv = (struct dri2_drawable *)
608
GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
609
610
if (priv == NULL || !priv->have_fake_front)
611
return;
612
613
dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
614
}
615
616
static void
617
dri2_wait_gl(struct glx_context *gc)
618
{
619
struct dri2_drawable *priv = (struct dri2_drawable *)
620
GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
621
622
if (priv == NULL || !priv->have_fake_front)
623
return;
624
625
dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
626
}
627
628
/**
629
* Called by the driver when it needs to update the real front buffer with the
630
* contents of its fake front buffer.
631
*/
632
static void
633
dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
634
{
635
struct glx_display *priv;
636
struct dri2_display *pdp;
637
struct glx_context *gc;
638
struct dri2_drawable *pdraw = loaderPrivate;
639
struct dri2_screen *psc;
640
641
if (!pdraw)
642
return;
643
644
if (!pdraw->base.psc)
645
return;
646
647
psc = (struct dri2_screen *) pdraw->base.psc;
648
649
priv = __glXInitialize(psc->base.dpy);
650
651
if (priv == NULL)
652
return;
653
654
pdp = (struct dri2_display *) priv->dri2Display;
655
gc = __glXGetCurrentContext();
656
657
dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
658
659
/* Old servers don't send invalidate events */
660
if (!pdp->invalidateAvailable)
661
dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
662
663
dri2_wait_gl(gc);
664
}
665
666
667
static void
668
dri2DestroyScreen(struct glx_screen *base)
669
{
670
struct dri2_screen *psc = (struct dri2_screen *) base;
671
672
/* Free the direct rendering per screen data */
673
(*psc->core->destroyScreen) (psc->driScreen);
674
driDestroyConfigs(psc->driver_configs);
675
free(psc->driverName);
676
close(psc->fd);
677
free(psc);
678
}
679
680
/**
681
* Process list of buffer received from the server
682
*
683
* Processes the list of buffers received in a reply from the server to either
684
* \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
685
*/
686
static void
687
process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
688
unsigned count)
689
{
690
int i;
691
692
pdraw->bufferCount = count;
693
pdraw->have_fake_front = 0;
694
pdraw->have_back = 0;
695
696
/* This assumes the DRI2 buffer attachment tokens matches the
697
* __DRIbuffer tokens. */
698
for (i = 0; i < count; i++) {
699
pdraw->buffers[i].attachment = buffers[i].attachment;
700
pdraw->buffers[i].name = buffers[i].name;
701
pdraw->buffers[i].pitch = buffers[i].pitch;
702
pdraw->buffers[i].cpp = buffers[i].cpp;
703
pdraw->buffers[i].flags = buffers[i].flags;
704
if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
705
pdraw->have_fake_front = 1;
706
if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
707
pdraw->have_back = 1;
708
}
709
710
}
711
712
unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
713
{
714
struct glx_display *glx_dpy = __glXInitialize(dpy);
715
__GLXDRIdrawable *pdraw;
716
pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
717
if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
718
return 0;
719
return glx_dpy->codes.first_event + GLX_BufferSwapComplete;
720
}
721
722
static void show_fps(struct dri2_drawable *draw)
723
{
724
const int interval =
725
((struct dri2_screen *) draw->base.psc)->show_fps_interval;
726
struct timeval tv;
727
uint64_t current_time;
728
729
gettimeofday(&tv, 0);
730
current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
731
732
draw->frames++;
733
734
if (draw->previous_time + interval * 1000000 <= current_time) {
735
if (draw->previous_time) {
736
fprintf(stderr, "libGL: FPS = %.2f\n",
737
((uint64_t)draw->frames * 1000000) /
738
(double)(current_time - draw->previous_time));
739
}
740
draw->frames = 0;
741
draw->previous_time = current_time;
742
}
743
}
744
745
static int64_t
746
dri2XcbSwapBuffers(Display *dpy,
747
__GLXDRIdrawable *pdraw,
748
int64_t target_msc,
749
int64_t divisor,
750
int64_t remainder)
751
{
752
xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie;
753
xcb_dri2_swap_buffers_reply_t *swap_buffers_reply;
754
uint32_t target_msc_hi, target_msc_lo;
755
uint32_t divisor_hi, divisor_lo;
756
uint32_t remainder_hi, remainder_lo;
757
int64_t ret = 0;
758
xcb_connection_t *c = XGetXCBConnection(dpy);
759
760
split_counter(target_msc, &target_msc_hi, &target_msc_lo);
761
split_counter(divisor, &divisor_hi, &divisor_lo);
762
split_counter(remainder, &remainder_hi, &remainder_lo);
763
764
swap_buffers_cookie =
765
xcb_dri2_swap_buffers_unchecked(c, pdraw->xDrawable,
766
target_msc_hi, target_msc_lo,
767
divisor_hi, divisor_lo,
768
remainder_hi, remainder_lo);
769
770
/* Immediately wait on the swapbuffers reply. If we didn't, we'd have
771
* to do so some time before reusing a (non-pageflipped) backbuffer.
772
* Otherwise, the new rendering could get ahead of the X Server's
773
* dispatch of the swapbuffer and you'd display garbage.
774
*
775
* We use XSync() first to reap the invalidate events through the event
776
* filter, to ensure that the next drawing doesn't use an invalidated
777
* buffer.
778
*/
779
XSync(dpy, False);
780
781
swap_buffers_reply =
782
xcb_dri2_swap_buffers_reply(c, swap_buffers_cookie, NULL);
783
if (swap_buffers_reply) {
784
ret = merge_counter(swap_buffers_reply->swap_hi,
785
swap_buffers_reply->swap_lo);
786
free(swap_buffers_reply);
787
}
788
return ret;
789
}
790
791
static int64_t
792
dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
793
int64_t remainder, Bool flush)
794
{
795
struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
796
struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
797
struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
798
struct dri2_display *pdp =
799
(struct dri2_display *)dpyPriv->dri2Display;
800
int64_t ret = 0;
801
802
/* Check we have the right attachments */
803
if (!priv->have_back)
804
return ret;
805
806
/* Old servers can't handle swapbuffers */
807
if (!pdp->swapAvailable) {
808
__dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
809
__DRI2_THROTTLE_SWAPBUFFER, flush);
810
} else {
811
__DRIcontext *ctx = dri2GetCurrentContext();
812
unsigned flags = __DRI2_FLUSH_DRAWABLE;
813
if (flush)
814
flags |= __DRI2_FLUSH_CONTEXT;
815
dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
816
817
ret = dri2XcbSwapBuffers(pdraw->psc->dpy, pdraw,
818
target_msc, divisor, remainder);
819
}
820
821
if (psc->show_fps_interval) {
822
show_fps(priv);
823
}
824
825
/* Old servers don't send invalidate events */
826
if (!pdp->invalidateAvailable)
827
dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
828
829
return ret;
830
}
831
832
static __DRIbuffer *
833
dri2GetBuffers(__DRIdrawable * driDrawable,
834
int *width, int *height,
835
unsigned int *attachments, int count,
836
int *out_count, void *loaderPrivate)
837
{
838
struct dri2_drawable *pdraw = loaderPrivate;
839
DRI2Buffer *buffers;
840
841
buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
842
width, height, attachments, count, out_count);
843
if (buffers == NULL)
844
return NULL;
845
846
pdraw->width = *width;
847
pdraw->height = *height;
848
process_buffers(pdraw, buffers, *out_count);
849
850
free(buffers);
851
852
return pdraw->buffers;
853
}
854
855
static __DRIbuffer *
856
dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
857
int *width, int *height,
858
unsigned int *attachments, int count,
859
int *out_count, void *loaderPrivate)
860
{
861
struct dri2_drawable *pdraw = loaderPrivate;
862
DRI2Buffer *buffers;
863
864
buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
865
pdraw->base.xDrawable,
866
width, height, attachments,
867
count, out_count);
868
if (buffers == NULL)
869
return NULL;
870
871
pdraw->width = *width;
872
pdraw->height = *height;
873
process_buffers(pdraw, buffers, *out_count);
874
875
free(buffers);
876
877
return pdraw->buffers;
878
}
879
880
static int
881
dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
882
{
883
xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
884
struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
885
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
886
struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
887
888
if (psc->config)
889
psc->config->configQueryi(psc->driScreen,
890
"vblank_mode", &vblank_mode);
891
892
switch (vblank_mode) {
893
case DRI_CONF_VBLANK_NEVER:
894
if (interval != 0)
895
return GLX_BAD_VALUE;
896
break;
897
case DRI_CONF_VBLANK_ALWAYS_SYNC:
898
if (interval <= 0)
899
return GLX_BAD_VALUE;
900
break;
901
default:
902
break;
903
}
904
905
xcb_dri2_swap_interval(c, priv->base.xDrawable, interval);
906
priv->swap_interval = interval;
907
908
return 0;
909
}
910
911
static int
912
dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
913
{
914
struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
915
916
return priv->swap_interval;
917
}
918
919
static void
920
driSetBackgroundContext(void *loaderPrivate)
921
{
922
struct dri2_context *pcp = (struct dri2_context *) loaderPrivate;
923
__glXSetCurrentContext(&pcp->base);
924
}
925
926
static GLboolean
927
driIsThreadSafe(void *loaderPrivate)
928
{
929
struct dri2_context *pcp = (struct dri2_context *) loaderPrivate;
930
/* Check Xlib is running in thread safe mode
931
*
932
* 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
933
* It wll be NULL if XInitThreads wasn't called.
934
*/
935
return pcp->base.psc->dpy->lock_fns != NULL;
936
}
937
938
static const __DRIdri2LoaderExtension dri2LoaderExtension = {
939
.base = { __DRI_DRI2_LOADER, 3 },
940
941
.getBuffers = dri2GetBuffers,
942
.flushFrontBuffer = dri2FlushFrontBuffer,
943
.getBuffersWithFormat = dri2GetBuffersWithFormat,
944
};
945
946
static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
947
.base = { __DRI_DRI2_LOADER, 3 },
948
949
.getBuffers = dri2GetBuffers,
950
.flushFrontBuffer = dri2FlushFrontBuffer,
951
.getBuffersWithFormat = NULL,
952
};
953
954
static const __DRIuseInvalidateExtension dri2UseInvalidate = {
955
.base = { __DRI_USE_INVALIDATE, 1 }
956
};
957
958
static const __DRIbackgroundCallableExtension driBackgroundCallable = {
959
.base = { __DRI_BACKGROUND_CALLABLE, 2 },
960
961
.setBackgroundContext = driSetBackgroundContext,
962
.isThreadSafe = driIsThreadSafe,
963
};
964
965
_X_HIDDEN void
966
dri2InvalidateBuffers(Display *dpy, XID drawable)
967
{
968
__GLXDRIdrawable *pdraw =
969
dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
970
struct dri2_screen *psc;
971
struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
972
973
if (!pdraw)
974
return;
975
976
psc = (struct dri2_screen *) pdraw->psc;
977
978
if (psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
979
psc->f->invalidate(pdp->driDrawable);
980
}
981
982
static void
983
dri2_bind_tex_image(__GLXDRIdrawable *base,
984
int buffer, const int *attrib_list)
985
{
986
struct glx_context *gc = __glXGetCurrentContext();
987
struct dri2_context *pcp = (struct dri2_context *) gc;
988
struct glx_display *dpyPriv = __glXInitialize(gc->currentDpy);
989
struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
990
struct dri2_display *pdp;
991
struct dri2_screen *psc;
992
993
pdp = (struct dri2_display *) dpyPriv->dri2Display;
994
995
if (pdraw != NULL) {
996
psc = (struct dri2_screen *) base->psc;
997
998
if (!pdp->invalidateAvailable && psc->f &&
999
psc->f->base.version >= 3 && psc->f->invalidate)
1000
psc->f->invalidate(pdraw->driDrawable);
1001
1002
if (psc->texBuffer->base.version >= 2 &&
1003
psc->texBuffer->setTexBuffer2 != NULL) {
1004
(*psc->texBuffer->setTexBuffer2) (pcp->driContext,
1005
pdraw->base.textureTarget,
1006
pdraw->base.textureFormat,
1007
pdraw->driDrawable);
1008
}
1009
else {
1010
(*psc->texBuffer->setTexBuffer) (pcp->driContext,
1011
pdraw->base.textureTarget,
1012
pdraw->driDrawable);
1013
}
1014
}
1015
}
1016
1017
static void
1018
dri2_release_tex_image(__GLXDRIdrawable *base, int buffer)
1019
{
1020
struct glx_context *gc = __glXGetCurrentContext();
1021
struct dri2_context *pcp = (struct dri2_context *) gc;
1022
struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
1023
struct dri2_screen *psc;
1024
1025
if (pdraw != NULL) {
1026
psc = (struct dri2_screen *) base->psc;
1027
1028
if (psc->texBuffer->base.version >= 3 &&
1029
psc->texBuffer->releaseTexBuffer != NULL) {
1030
(*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
1031
pdraw->base.textureTarget,
1032
pdraw->driDrawable);
1033
}
1034
}
1035
}
1036
1037
static const struct glx_context_vtable dri2_context_vtable = {
1038
.destroy = dri2_destroy_context,
1039
.bind = dri2_bind_context,
1040
.unbind = dri2_unbind_context,
1041
.wait_gl = dri2_wait_gl,
1042
.wait_x = dri2_wait_x,
1043
.interop_query_device_info = dri2_interop_query_device_info,
1044
.interop_export_object = dri2_interop_export_object
1045
};
1046
1047
static void
1048
dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
1049
const char *driverName)
1050
{
1051
const struct dri2_display *const pdp = (struct dri2_display *)
1052
priv->dri2Display;
1053
const __DRIextension **extensions;
1054
int i;
1055
1056
extensions = psc->core->getExtensions(psc->driScreen);
1057
1058
__glXEnableDirectExtension(&psc->base, "GLX_EXT_swap_control");
1059
__glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
1060
__glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
1061
__glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
1062
1063
/*
1064
* GLX_INTEL_swap_event is broken on the server side, where it's
1065
* currently unconditionally enabled. This completely breaks
1066
* systems running on drivers which don't support that extension.
1067
* There's no way to test for its presence on this side, so instead
1068
* of disabling it unconditionally, just disable it for drivers
1069
* which are known to not support it, or for DDX drivers supporting
1070
* only an older (pre-ScheduleSwap) version of DRI2.
1071
*
1072
* This is a hack which is required until:
1073
* http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1074
* is merged and updated xserver makes it's way into distros:
1075
*/
1076
if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1077
__glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1078
}
1079
1080
if (psc->dri2->base.version >= 3) {
1081
const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
1082
1083
__glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
1084
__glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
1085
__glXEnableDirectExtension(&psc->base, "GLX_EXT_no_config_context");
1086
1087
if ((mask & ((1 << __DRI_API_GLES) |
1088
(1 << __DRI_API_GLES2) |
1089
(1 << __DRI_API_GLES3))) != 0) {
1090
__glXEnableDirectExtension(&psc->base,
1091
"GLX_EXT_create_context_es_profile");
1092
__glXEnableDirectExtension(&psc->base,
1093
"GLX_EXT_create_context_es2_profile");
1094
}
1095
}
1096
1097
for (i = 0; extensions[i]; i++) {
1098
if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
1099
psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
1100
__glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
1101
}
1102
1103
if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1104
psc->f = (__DRI2flushExtension *) extensions[i];
1105
/* internal driver extension, no GL extension exposed */
1106
}
1107
1108
if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1109
psc->config = (__DRI2configQueryExtension *) extensions[i];
1110
1111
if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1112
psc->throttle = (__DRI2throttleExtension *) extensions[i];
1113
1114
/* DRI2 version 3 is also required because
1115
* GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1116
*/
1117
if (psc->dri2->base.version >= 3
1118
&& strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1119
__glXEnableDirectExtension(&psc->base,
1120
"GLX_ARB_create_context_robustness");
1121
1122
/* DRI2 version 3 is also required because
1123
* GLX_ARB_create_context_no_error requires GLX_ARB_create_context.
1124
*/
1125
if (psc->dri2->base.version >= 3
1126
&& strcmp(extensions[i]->name, __DRI2_NO_ERROR) == 0)
1127
__glXEnableDirectExtension(&psc->base,
1128
"GLX_ARB_create_context_no_error");
1129
1130
/* DRI2 version 3 is also required because GLX_MESA_query_renderer
1131
* requires GLX_ARB_create_context_profile.
1132
*/
1133
if (psc->dri2->base.version >= 3
1134
&& strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
1135
psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
1136
__glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
1137
}
1138
1139
if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
1140
psc->interop = (__DRI2interopExtension*)extensions[i];
1141
1142
/* DRI2 version 3 is also required because
1143
* GLX_ARB_control_flush_control requires GLX_ARB_create_context.
1144
*/
1145
if (psc->dri2->base.version >= 3
1146
&& strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
1147
__glXEnableDirectExtension(&psc->base,
1148
"GLX_ARB_context_flush_control");
1149
}
1150
}
1151
1152
static char *
1153
dri2_get_driver_name(struct glx_screen *glx_screen)
1154
{
1155
struct dri2_screen *psc = (struct dri2_screen *)glx_screen;
1156
1157
return psc->driverName;
1158
}
1159
1160
static const struct glx_screen_vtable dri2_screen_vtable = {
1161
.create_context = dri_common_create_context,
1162
.create_context_attribs = dri2_create_context_attribs,
1163
.query_renderer_integer = dri2_query_renderer_integer,
1164
.query_renderer_string = dri2_query_renderer_string,
1165
.get_driver_name = dri2_get_driver_name,
1166
};
1167
1168
static struct glx_screen *
1169
dri2CreateScreen(int screen, struct glx_display * priv)
1170
{
1171
const __DRIconfig **driver_configs;
1172
const __DRIextension **extensions;
1173
const struct dri2_display *const pdp = (struct dri2_display *)
1174
priv->dri2Display;
1175
struct dri2_screen *psc;
1176
__GLXDRIscreen *psp;
1177
struct glx_config *configs = NULL, *visuals = NULL;
1178
char *driverName = NULL, *loader_driverName, *deviceName, *tmp;
1179
drm_magic_t magic;
1180
int i;
1181
1182
psc = calloc(1, sizeof *psc);
1183
if (psc == NULL)
1184
return NULL;
1185
1186
psc->fd = -1;
1187
1188
if (!glx_screen_init(&psc->base, screen, priv)) {
1189
free(psc);
1190
return NULL;
1191
}
1192
1193
if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1194
&driverName, &deviceName)) {
1195
glx_screen_cleanup(&psc->base);
1196
free(psc);
1197
InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1198
return NULL;
1199
}
1200
1201
psc->fd = loader_open_device(deviceName);
1202
if (psc->fd < 0) {
1203
ErrorMessageF("failed to open %s: %s\n", deviceName, strerror(errno));
1204
goto handle_error;
1205
}
1206
1207
if (drmGetMagic(psc->fd, &magic)) {
1208
ErrorMessageF("failed to get magic\n");
1209
goto handle_error;
1210
}
1211
1212
if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1213
ErrorMessageF("failed to authenticate magic %d\n", magic);
1214
goto handle_error;
1215
}
1216
1217
/* If Mesa knows about the appropriate driver for this fd, then trust it.
1218
* Otherwise, default to the server's value.
1219
*/
1220
loader_driverName = loader_get_driver_for_fd(psc->fd);
1221
if (loader_driverName) {
1222
free(driverName);
1223
driverName = loader_driverName;
1224
}
1225
psc->driverName = driverName;
1226
1227
extensions = driOpenDriver(driverName, &psc->driver);
1228
if (extensions == NULL)
1229
goto handle_error;
1230
1231
for (i = 0; extensions[i]; i++) {
1232
if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1233
psc->core = (__DRIcoreExtension *) extensions[i];
1234
if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1235
psc->dri2 = (__DRIdri2Extension *) extensions[i];
1236
}
1237
1238
if (psc->core == NULL || psc->dri2 == NULL) {
1239
ErrorMessageF("core dri or dri2 extension not found\n");
1240
goto handle_error;
1241
}
1242
1243
if (psc->dri2->base.version >= 4) {
1244
psc->driScreen =
1245
psc->dri2->createNewScreen2(screen, psc->fd,
1246
(const __DRIextension **)
1247
&pdp->loader_extensions[0],
1248
extensions,
1249
&driver_configs, psc);
1250
} else {
1251
psc->driScreen =
1252
psc->dri2->createNewScreen(screen, psc->fd,
1253
(const __DRIextension **)
1254
&pdp->loader_extensions[0],
1255
&driver_configs, psc);
1256
}
1257
1258
if (psc->driScreen == NULL) {
1259
ErrorMessageF("failed to create dri screen\n");
1260
goto handle_error;
1261
}
1262
1263
dri2BindExtensions(psc, priv, driverName);
1264
1265
configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1266
visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1267
1268
if (!configs || !visuals) {
1269
ErrorMessageF("No matching fbConfigs or visuals found\n");
1270
goto handle_error;
1271
}
1272
1273
glx_config_destroy_list(psc->base.configs);
1274
psc->base.configs = configs;
1275
glx_config_destroy_list(psc->base.visuals);
1276
psc->base.visuals = visuals;
1277
1278
psc->driver_configs = driver_configs;
1279
1280
psc->base.vtable = &dri2_screen_vtable;
1281
psp = &psc->vtable;
1282
psc->base.driScreen = psp;
1283
psp->destroyScreen = dri2DestroyScreen;
1284
psp->createDrawable = dri2CreateDrawable;
1285
psp->swapBuffers = dri2SwapBuffers;
1286
psp->getDrawableMSC = NULL;
1287
psp->waitForMSC = NULL;
1288
psp->waitForSBC = NULL;
1289
psp->setSwapInterval = NULL;
1290
psp->getSwapInterval = NULL;
1291
psp->getBufferAge = NULL;
1292
psp->bindTexImage = dri2_bind_tex_image;
1293
psp->releaseTexImage = dri2_release_tex_image;
1294
1295
if (pdp->driMinor >= 2) {
1296
psp->getDrawableMSC = dri2DrawableGetMSC;
1297
psp->waitForMSC = dri2WaitForMSC;
1298
psp->waitForSBC = dri2WaitForSBC;
1299
psp->setSwapInterval = dri2SetSwapInterval;
1300
psp->getSwapInterval = dri2GetSwapInterval;
1301
1302
__glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1303
}
1304
1305
__glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1306
1307
if (psc->config->base.version > 1 &&
1308
psc->config->configQuerys(psc->driScreen, "glx_extension_override",
1309
&tmp) == 0)
1310
__glXParseExtensionOverride(&psc->base, tmp);
1311
1312
if (psc->config->base.version > 1 &&
1313
psc->config->configQuerys(psc->driScreen,
1314
"indirect_gl_extension_override",
1315
&tmp) == 0)
1316
__IndirectGlParseExtensionOverride(&psc->base, tmp);
1317
1318
/* DRI2 supports SubBuffer through DRI2CopyRegion, so it's always
1319
* available.*/
1320
psp->copySubBuffer = dri2CopySubBuffer;
1321
__glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1322
1323
free(deviceName);
1324
1325
tmp = getenv("LIBGL_SHOW_FPS");
1326
psc->show_fps_interval = (tmp) ? atoi(tmp) : 0;
1327
if (psc->show_fps_interval < 0)
1328
psc->show_fps_interval = 0;
1329
1330
InfoMessageF("Using DRI2 for screen %d\n", screen);
1331
1332
return &psc->base;
1333
1334
handle_error:
1335
CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1336
1337
if (configs)
1338
glx_config_destroy_list(configs);
1339
if (visuals)
1340
glx_config_destroy_list(visuals);
1341
if (psc->driScreen)
1342
psc->core->destroyScreen(psc->driScreen);
1343
psc->driScreen = NULL;
1344
if (psc->fd >= 0)
1345
close(psc->fd);
1346
if (psc->driver)
1347
dlclose(psc->driver);
1348
1349
free(deviceName);
1350
glx_screen_cleanup(&psc->base);
1351
free(psc);
1352
1353
return NULL;
1354
}
1355
1356
/* Called from __glXFreeDisplayPrivate.
1357
*/
1358
static void
1359
dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1360
{
1361
struct dri2_display *pdp = (struct dri2_display *) dpy;
1362
1363
__glxHashDestroy(pdp->dri2Hash);
1364
free(dpy);
1365
}
1366
1367
_X_HIDDEN __GLXDRIdrawable *
1368
dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1369
{
1370
struct glx_display *d = __glXInitialize(dpy);
1371
struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1372
__GLXDRIdrawable *pdraw;
1373
1374
if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1375
return pdraw;
1376
1377
return NULL;
1378
}
1379
1380
/*
1381
* Allocate, initialize and return a __DRIdisplayPrivate object.
1382
* This is called from __glXInitialize() when we are given a new
1383
* display pointer.
1384
*/
1385
_X_HIDDEN __GLXDRIdisplay *
1386
dri2CreateDisplay(Display * dpy)
1387
{
1388
struct dri2_display *pdp;
1389
int eventBase, errorBase, i;
1390
1391
if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1392
return NULL;
1393
1394
pdp = malloc(sizeof *pdp);
1395
if (pdp == NULL)
1396
return NULL;
1397
1398
if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1399
free(pdp);
1400
return NULL;
1401
}
1402
1403
pdp->driPatch = 0;
1404
pdp->swapAvailable = (pdp->driMinor >= 2);
1405
pdp->invalidateAvailable = (pdp->driMinor >= 3);
1406
1407
pdp->base.destroyDisplay = dri2DestroyDisplay;
1408
pdp->base.createScreen = dri2CreateScreen;
1409
1410
i = 0;
1411
if (pdp->driMinor < 1)
1412
pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1413
else
1414
pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1415
1416
pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1417
1418
pdp->loader_extensions[i++] = &driBackgroundCallable.base;
1419
1420
pdp->loader_extensions[i++] = NULL;
1421
1422
pdp->dri2Hash = __glxHashCreate();
1423
if (pdp->dri2Hash == NULL) {
1424
free(pdp);
1425
return NULL;
1426
}
1427
1428
return &pdp->base;
1429
}
1430
1431
#endif /* GLX_DIRECT_RENDERING */
1432
1433