Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/glx/drisw_glx.c
4558 views
1
/*
2
* Copyright 2008 George Sapountzis
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, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*/
23
24
#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
25
26
#include <xcb/xproto.h>
27
#include <xcb/shm.h>
28
#include <X11/Xlib.h>
29
#include <X11/Xlib-xcb.h>
30
#include "glxclient.h"
31
#include <dlfcn.h>
32
#include "dri_common.h"
33
#include "drisw_priv.h"
34
#include <X11/extensions/shmproto.h>
35
#include <assert.h>
36
37
static int xshm_error = 0;
38
static int xshm_opcode = -1;
39
40
/**
41
* Catches potential Xlib errors.
42
*/
43
static int
44
handle_xerror(Display *dpy, XErrorEvent *event)
45
{
46
(void) dpy;
47
48
assert(xshm_opcode != -1);
49
if (event->request_code != xshm_opcode)
50
return 0;
51
52
xshm_error = event->error_code;
53
return 0;
54
}
55
56
static Bool
57
XCreateDrawable(struct drisw_drawable * pdp, int shmid, Display * dpy)
58
{
59
if (pdp->ximage) {
60
XDestroyImage(pdp->ximage);
61
pdp->ximage = NULL;
62
if ((pdp->shminfo.shmid > 0) && (shmid != pdp->shminfo.shmid))
63
XShmDetach(dpy, &pdp->shminfo);
64
}
65
66
if (!xshm_error && shmid >= 0) {
67
pdp->shminfo.shmid = shmid;
68
pdp->ximage = XShmCreateImage(dpy,
69
NULL,
70
pdp->xDepth,
71
ZPixmap, /* format */
72
NULL, /* data */
73
&pdp->shminfo, /* shminfo */
74
0, 0); /* width, height */
75
if (pdp->ximage != NULL) {
76
int (*old_handler)(Display *, XErrorEvent *);
77
78
/* dispatch pending errors */
79
XSync(dpy, False);
80
81
old_handler = XSetErrorHandler(handle_xerror);
82
/* This may trigger the X protocol error we're ready to catch: */
83
XShmAttach(dpy, &pdp->shminfo);
84
XSync(dpy, False);
85
86
if (xshm_error) {
87
/* we are on a remote display, this error is normal, don't print it */
88
XDestroyImage(pdp->ximage);
89
pdp->ximage = NULL;
90
}
91
92
(void) XSetErrorHandler(old_handler);
93
}
94
}
95
96
if (pdp->ximage == NULL) {
97
pdp->shminfo.shmid = -1;
98
pdp->ximage = XCreateImage(dpy,
99
NULL,
100
pdp->xDepth,
101
ZPixmap, 0, /* format, offset */
102
NULL, /* data */
103
0, 0, /* width, height */
104
32, /* bitmap_pad */
105
0); /* bytes_per_line */
106
}
107
108
/**
109
* swrast does not handle 24-bit depth with 24 bpp, so let X do the
110
* the conversion for us.
111
*/
112
if (pdp->ximage->bits_per_pixel == 24)
113
pdp->ximage->bits_per_pixel = 32;
114
115
return True;
116
}
117
118
static void
119
XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
120
{
121
if (pdp->ximage)
122
XDestroyImage(pdp->ximage);
123
124
if (pdp->shminfo.shmid > 0)
125
XShmDetach(dpy, &pdp->shminfo);
126
127
XFreeGC(dpy, pdp->gc);
128
}
129
130
/**
131
* swrast loader functions
132
*/
133
134
static void
135
swrastGetDrawableInfo(__DRIdrawable * draw,
136
int *x, int *y, int *w, int *h,
137
void *loaderPrivate)
138
{
139
struct drisw_drawable *pdp = loaderPrivate;
140
__GLXDRIdrawable *pdraw = &(pdp->base);
141
Display *dpy = pdraw->psc->dpy;
142
Drawable drawable;
143
144
Window root;
145
unsigned uw, uh, bw, depth;
146
147
drawable = pdraw->xDrawable;
148
149
XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
150
*w = uw;
151
*h = uh;
152
}
153
154
/**
155
* Align renderbuffer pitch.
156
*
157
* This should be chosen by the driver and the loader (libGL, xserver/glx)
158
* should use the driver provided pitch.
159
*
160
* It seems that the xorg loader (that is the xserver loading swrast_dri for
161
* indirect rendering, not client-side libGL) requires that the pitch is
162
* exactly the image width padded to 32 bits. XXX
163
*
164
* The above restriction can probably be overcome by using ScratchPixmap and
165
* CopyArea in the xserver, similar to ShmPutImage, and setting the width of
166
* the scratch pixmap to 'pitch / cpp'.
167
*/
168
static inline int
169
bytes_per_line(unsigned pitch_bits, unsigned mul)
170
{
171
unsigned mask = mul - 1;
172
173
return ((pitch_bits + mask) & ~mask) / 8;
174
}
175
176
static void
177
swrastXPutImage(__DRIdrawable * draw, int op,
178
int srcx, int srcy, int x, int y,
179
int w, int h, int stride,
180
int shmid, char *data, void *loaderPrivate)
181
{
182
struct drisw_drawable *pdp = loaderPrivate;
183
__GLXDRIdrawable *pdraw = &(pdp->base);
184
Display *dpy = pdraw->psc->dpy;
185
Drawable drawable;
186
XImage *ximage;
187
GC gc = pdp->gc;
188
189
if (!pdp->ximage || shmid != pdp->shminfo.shmid) {
190
if (!XCreateDrawable(pdp, shmid, dpy))
191
return;
192
}
193
194
drawable = pdraw->xDrawable;
195
ximage = pdp->ximage;
196
ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
197
ximage->data = data;
198
199
ximage->width = ximage->bytes_per_line / ((ximage->bits_per_pixel + 7)/ 8);
200
ximage->height = h;
201
202
if (pdp->shminfo.shmid >= 0) {
203
XShmPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h, False);
204
XSync(dpy, False);
205
} else {
206
XPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h);
207
}
208
ximage->data = NULL;
209
}
210
211
static void
212
swrastPutImageShm(__DRIdrawable * draw, int op,
213
int x, int y, int w, int h, int stride,
214
int shmid, char *shmaddr, unsigned offset,
215
void *loaderPrivate)
216
{
217
struct drisw_drawable *pdp = loaderPrivate;
218
219
pdp->shminfo.shmaddr = shmaddr;
220
swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, shmid,
221
shmaddr + offset, loaderPrivate);
222
}
223
224
static void
225
swrastPutImageShm2(__DRIdrawable * draw, int op,
226
int x, int y,
227
int w, int h, int stride,
228
int shmid, char *shmaddr, unsigned offset,
229
void *loaderPrivate)
230
{
231
struct drisw_drawable *pdp = loaderPrivate;
232
233
pdp->shminfo.shmaddr = shmaddr;
234
swrastXPutImage(draw, op, x, 0, x, y, w, h, stride, shmid,
235
shmaddr + offset, loaderPrivate);
236
}
237
238
static void
239
swrastPutImage2(__DRIdrawable * draw, int op,
240
int x, int y, int w, int h, int stride,
241
char *data, void *loaderPrivate)
242
{
243
swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, -1,
244
data, loaderPrivate);
245
}
246
247
static void
248
swrastPutImage(__DRIdrawable * draw, int op,
249
int x, int y, int w, int h,
250
char *data, void *loaderPrivate)
251
{
252
swrastXPutImage(draw, op, 0, 0, x, y, w, h, 0, -1,
253
data, loaderPrivate);
254
}
255
256
static void
257
swrastGetImage2(__DRIdrawable * read,
258
int x, int y, int w, int h, int stride,
259
char *data, void *loaderPrivate)
260
{
261
struct drisw_drawable *prp = loaderPrivate;
262
__GLXDRIdrawable *pread = &(prp->base);
263
Display *dpy = pread->psc->dpy;
264
Drawable readable;
265
XImage *ximage;
266
267
if (!prp->ximage || prp->shminfo.shmid >= 0) {
268
if (!XCreateDrawable(prp, -1, dpy))
269
return;
270
}
271
272
readable = pread->xDrawable;
273
274
ximage = prp->ximage;
275
ximage->data = data;
276
ximage->width = w;
277
ximage->height = h;
278
ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
279
280
XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
281
282
ximage->data = NULL;
283
}
284
285
static void
286
swrastGetImage(__DRIdrawable * read,
287
int x, int y, int w, int h,
288
char *data, void *loaderPrivate)
289
{
290
swrastGetImage2(read, x, y, w, h, 0, data, loaderPrivate);
291
}
292
293
static GLboolean
294
swrastGetImageShm2(__DRIdrawable * read,
295
int x, int y, int w, int h,
296
int shmid, void *loaderPrivate)
297
{
298
struct drisw_drawable *prp = loaderPrivate;
299
__GLXDRIdrawable *pread = &(prp->base);
300
Display *dpy = pread->psc->dpy;
301
Drawable readable;
302
XImage *ximage;
303
304
if (!prp->ximage || shmid != prp->shminfo.shmid) {
305
if (!XCreateDrawable(prp, shmid, dpy))
306
return GL_FALSE;
307
}
308
309
if (prp->shminfo.shmid == -1)
310
return GL_FALSE;
311
readable = pread->xDrawable;
312
313
ximage = prp->ximage;
314
ximage->data = prp->shminfo.shmaddr; /* no offset */
315
ximage->width = w;
316
ximage->height = h;
317
ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
318
319
XShmGetImage(dpy, readable, ximage, x, y, ~0L);
320
return GL_TRUE;
321
}
322
323
static void
324
swrastGetImageShm(__DRIdrawable * read,
325
int x, int y, int w, int h,
326
int shmid, void *loaderPrivate)
327
{
328
swrastGetImageShm2(read, x, y, w, h, shmid, loaderPrivate);
329
}
330
331
static const __DRIswrastLoaderExtension swrastLoaderExtension_shm = {
332
.base = {__DRI_SWRAST_LOADER, 6 },
333
334
.getDrawableInfo = swrastGetDrawableInfo,
335
.putImage = swrastPutImage,
336
.getImage = swrastGetImage,
337
.putImage2 = swrastPutImage2,
338
.getImage2 = swrastGetImage2,
339
.putImageShm = swrastPutImageShm,
340
.getImageShm = swrastGetImageShm,
341
.putImageShm2 = swrastPutImageShm2,
342
.getImageShm2 = swrastGetImageShm2,
343
};
344
345
static const __DRIextension *loader_extensions_shm[] = {
346
&swrastLoaderExtension_shm.base,
347
NULL
348
};
349
350
static const __DRIswrastLoaderExtension swrastLoaderExtension = {
351
.base = {__DRI_SWRAST_LOADER, 3 },
352
353
.getDrawableInfo = swrastGetDrawableInfo,
354
.putImage = swrastPutImage,
355
.getImage = swrastGetImage,
356
.putImage2 = swrastPutImage2,
357
.getImage2 = swrastGetImage2,
358
};
359
360
static const __DRIextension *loader_extensions_noshm[] = {
361
&swrastLoaderExtension.base,
362
NULL
363
};
364
365
/**
366
* GLXDRI functions
367
*/
368
369
static void
370
drisw_destroy_context(struct glx_context *context)
371
{
372
struct drisw_context *pcp = (struct drisw_context *) context;
373
struct drisw_screen *psc = (struct drisw_screen *) context->psc;
374
375
driReleaseDrawables(&pcp->base);
376
377
free((char *) context->extensions);
378
379
(*psc->core->destroyContext) (pcp->driContext);
380
381
free(pcp);
382
}
383
384
static int
385
drisw_bind_context(struct glx_context *context, struct glx_context *old,
386
GLXDrawable draw, GLXDrawable read)
387
{
388
struct drisw_context *pcp = (struct drisw_context *) context;
389
struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
390
struct drisw_drawable *pdraw, *pread;
391
392
pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
393
pread = (struct drisw_drawable *) driFetchDrawable(context, read);
394
395
driReleaseDrawables(&pcp->base);
396
397
if ((*psc->core->bindContext) (pcp->driContext,
398
pdraw ? pdraw->driDrawable : NULL,
399
pread ? pread->driDrawable : NULL))
400
return Success;
401
402
return GLXBadContext;
403
}
404
405
static void
406
drisw_unbind_context(struct glx_context *context, struct glx_context *new)
407
{
408
struct drisw_context *pcp = (struct drisw_context *) context;
409
struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
410
411
(*psc->core->unbindContext) (pcp->driContext);
412
}
413
414
static void
415
drisw_wait_gl(struct glx_context *context)
416
{
417
glFinish();
418
}
419
420
static void
421
drisw_wait_x(struct glx_context *context)
422
{
423
XSync(context->currentDpy, False);
424
}
425
426
static void
427
drisw_bind_tex_image(__GLXDRIdrawable *base,
428
int buffer, const int *attrib_list)
429
{
430
struct glx_context *gc = __glXGetCurrentContext();
431
struct drisw_context *pcp = (struct drisw_context *) gc;
432
struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
433
struct drisw_screen *psc;
434
435
if (pdraw != NULL) {
436
psc = (struct drisw_screen *) base->psc;
437
438
if (!psc->texBuffer)
439
return;
440
441
if (psc->texBuffer->base.version >= 2 &&
442
psc->texBuffer->setTexBuffer2 != NULL) {
443
(*psc->texBuffer->setTexBuffer2) (pcp->driContext,
444
pdraw->base.textureTarget,
445
pdraw->base.textureFormat,
446
pdraw->driDrawable);
447
}
448
else {
449
(*psc->texBuffer->setTexBuffer) (pcp->driContext,
450
pdraw->base.textureTarget,
451
pdraw->driDrawable);
452
}
453
}
454
}
455
456
static void
457
drisw_release_tex_image(__GLXDRIdrawable *base, int buffer)
458
{
459
struct glx_context *gc = __glXGetCurrentContext();
460
struct drisw_context *pcp = (struct drisw_context *) gc;
461
struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
462
struct drisw_screen *psc;
463
464
if (pdraw != NULL) {
465
psc = (struct drisw_screen *) base->psc;
466
467
if (!psc->texBuffer)
468
return;
469
470
if (psc->texBuffer->base.version >= 3 &&
471
psc->texBuffer->releaseTexBuffer != NULL) {
472
(*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
473
pdraw->base.textureTarget,
474
pdraw->driDrawable);
475
}
476
}
477
}
478
479
static const struct glx_context_vtable drisw_context_vtable = {
480
.destroy = drisw_destroy_context,
481
.bind = drisw_bind_context,
482
.unbind = drisw_unbind_context,
483
.wait_gl = drisw_wait_gl,
484
.wait_x = drisw_wait_x,
485
};
486
487
static struct glx_context *
488
drisw_create_context_attribs(struct glx_screen *base,
489
struct glx_config *config_base,
490
struct glx_context *shareList,
491
unsigned num_attribs,
492
const uint32_t *attribs,
493
unsigned *error)
494
{
495
struct drisw_context *pcp, *pcp_shared;
496
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
497
struct drisw_screen *psc = (struct drisw_screen *) base;
498
__DRIcontext *shared = NULL;
499
500
uint32_t minor_ver;
501
uint32_t major_ver;
502
uint32_t renderType;
503
uint32_t flags;
504
unsigned api;
505
int reset;
506
int release;
507
uint32_t ctx_attribs[2 * 5];
508
unsigned num_ctx_attribs = 0;
509
510
if (!psc->base.driScreen)
511
return NULL;
512
513
if (psc->swrast->base.version < 3)
514
return NULL;
515
516
/* Remap the GLX tokens to DRI2 tokens.
517
*/
518
if (!dri2_convert_glx_attribs(num_attribs, attribs,
519
&major_ver, &minor_ver, &renderType, &flags,
520
&api, &reset, &release, error))
521
return NULL;
522
523
if (!dri2_check_no_error(flags, shareList, major_ver, error))
524
return NULL;
525
526
/* Check the renderType value */
527
if (!validate_renderType_against_config(config_base, renderType)) {
528
return NULL;
529
}
530
531
if (reset != __DRI_CTX_RESET_NO_NOTIFICATION)
532
return NULL;
533
534
if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH &&
535
release != __DRI_CTX_RELEASE_BEHAVIOR_NONE)
536
return NULL;
537
538
if (shareList) {
539
/* If the shareList context is not a DRISW context, we cannot possibly
540
* create a DRISW context that shares it.
541
*/
542
if (shareList->vtable->destroy != drisw_destroy_context) {
543
return NULL;
544
}
545
546
pcp_shared = (struct drisw_context *) shareList;
547
shared = pcp_shared->driContext;
548
}
549
550
pcp = calloc(1, sizeof *pcp);
551
if (pcp == NULL)
552
return NULL;
553
554
if (!glx_context_init(&pcp->base, &psc->base, config_base)) {
555
free(pcp);
556
return NULL;
557
}
558
559
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
560
ctx_attribs[num_ctx_attribs++] = major_ver;
561
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
562
ctx_attribs[num_ctx_attribs++] = minor_ver;
563
if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
564
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
565
ctx_attribs[num_ctx_attribs++] = release;
566
}
567
568
if (flags != 0) {
569
ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
570
571
/* The current __DRI_CTX_FLAG_* values are identical to the
572
* GLX_CONTEXT_*_BIT values.
573
*/
574
ctx_attribs[num_ctx_attribs++] = flags;
575
576
if (flags & __DRI_CTX_FLAG_NO_ERROR)
577
pcp->base.noError = GL_TRUE;
578
}
579
580
pcp->base.renderType = renderType;
581
582
pcp->driContext =
583
(*psc->swrast->createContextAttribs) (psc->driScreen,
584
api,
585
config ? config->driConfig : 0,
586
shared,
587
num_ctx_attribs / 2,
588
ctx_attribs,
589
error,
590
pcp);
591
if (pcp->driContext == NULL) {
592
free(pcp);
593
return NULL;
594
}
595
596
pcp->base.vtable = &drisw_context_vtable;
597
598
return &pcp->base;
599
}
600
601
static void
602
driswDestroyDrawable(__GLXDRIdrawable * pdraw)
603
{
604
struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
605
struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
606
607
(*psc->core->destroyDrawable) (pdp->driDrawable);
608
609
XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
610
free(pdp);
611
}
612
613
static __GLXDRIdrawable *
614
driswCreateDrawable(struct glx_screen *base, XID xDrawable,
615
GLXDrawable drawable, struct glx_config *modes)
616
{
617
struct drisw_drawable *pdp;
618
__GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
619
struct drisw_screen *psc = (struct drisw_screen *) base;
620
const __DRIswrastExtension *swrast = psc->swrast;
621
Display *dpy = psc->base.dpy;
622
623
pdp = calloc(1, sizeof(*pdp));
624
if (!pdp)
625
return NULL;
626
627
pdp->base.xDrawable = xDrawable;
628
pdp->base.drawable = drawable;
629
pdp->base.psc = &psc->base;
630
pdp->config = modes;
631
pdp->gc = XCreateGC(dpy, xDrawable, 0, NULL);
632
pdp->xDepth = 0;
633
634
/* Use the visual depth, if this fbconfig corresponds to a visual */
635
if (pdp->config->visualID != 0) {
636
int matches = 0;
637
XVisualInfo *visinfo, template;
638
639
template.visualid = pdp->config->visualID;
640
template.screen = pdp->config->screen;
641
visinfo = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask,
642
&template, &matches);
643
644
if (visinfo && matches) {
645
pdp->xDepth = visinfo->depth;
646
XFree(visinfo);
647
}
648
}
649
650
/* Otherwise, or if XGetVisualInfo failed, ask the server */
651
if (pdp->xDepth == 0) {
652
Window root;
653
int x, y;
654
unsigned uw, uh, bw, depth;
655
656
XGetGeometry(dpy, xDrawable, &root, &x, &y, &uw, &uh, &bw, &depth);
657
pdp->xDepth = depth;
658
}
659
660
/* Create a new drawable */
661
pdp->driDrawable =
662
(*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
663
664
if (!pdp->driDrawable) {
665
XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
666
free(pdp);
667
return NULL;
668
}
669
670
pdp->base.destroyDrawable = driswDestroyDrawable;
671
672
return &pdp->base;
673
}
674
675
static int64_t
676
driswSwapBuffers(__GLXDRIdrawable * pdraw,
677
int64_t target_msc, int64_t divisor, int64_t remainder,
678
Bool flush)
679
{
680
struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
681
struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
682
683
(void) target_msc;
684
(void) divisor;
685
(void) remainder;
686
687
if (flush) {
688
glFlush();
689
}
690
691
(*psc->core->swapBuffers) (pdp->driDrawable);
692
693
return 0;
694
}
695
696
static void
697
driswCopySubBuffer(__GLXDRIdrawable * pdraw,
698
int x, int y, int width, int height, Bool flush)
699
{
700
struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
701
struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
702
703
if (flush) {
704
glFlush();
705
}
706
707
(*psc->copySubBuffer->copySubBuffer) (pdp->driDrawable,
708
x, y, width, height);
709
}
710
711
static void
712
driswDestroyScreen(struct glx_screen *base)
713
{
714
struct drisw_screen *psc = (struct drisw_screen *) base;
715
716
/* Free the direct rendering per screen data */
717
(*psc->core->destroyScreen) (psc->driScreen);
718
driDestroyConfigs(psc->driver_configs);
719
psc->driScreen = NULL;
720
if (psc->driver)
721
dlclose(psc->driver);
722
free(psc);
723
}
724
725
#define SWRAST_DRIVER_NAME "swrast"
726
727
static char *
728
drisw_get_driver_name(struct glx_screen *glx_screen)
729
{
730
return strdup(SWRAST_DRIVER_NAME);
731
}
732
733
static const struct glx_screen_vtable drisw_screen_vtable = {
734
.create_context = dri_common_create_context,
735
.create_context_attribs = drisw_create_context_attribs,
736
.query_renderer_integer = drisw_query_renderer_integer,
737
.query_renderer_string = drisw_query_renderer_string,
738
.get_driver_name = drisw_get_driver_name,
739
};
740
741
static void
742
driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
743
{
744
int i;
745
746
__glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
747
748
if (psc->swrast->base.version >= 3) {
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
/* DRISW version >= 2 implies support for OpenGL ES.
754
*/
755
__glXEnableDirectExtension(&psc->base,
756
"GLX_EXT_create_context_es_profile");
757
__glXEnableDirectExtension(&psc->base,
758
"GLX_EXT_create_context_es2_profile");
759
}
760
761
if (psc->copySubBuffer)
762
__glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
763
764
/* FIXME: Figure out what other extensions can be ported here from dri2. */
765
for (i = 0; extensions[i]; i++) {
766
if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
767
psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
768
__glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
769
}
770
/* DRISW version 3 is also required because GLX_MESA_query_renderer
771
* requires GLX_ARB_create_context_profile.
772
*/
773
if (psc->swrast->base.version >= 3
774
&& strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
775
psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
776
__glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
777
}
778
779
if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
780
__glXEnableDirectExtension(&psc->base,
781
"GLX_ARB_create_context_robustness");
782
783
if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0) {
784
__glXEnableDirectExtension(&psc->base,
785
"GLX_ARB_context_flush_control");
786
}
787
788
if (strcmp(extensions[i]->name, __DRI2_NO_ERROR) == 0)
789
__glXEnableDirectExtension(&psc->base,
790
"GLX_ARB_create_context_no_error");
791
}
792
}
793
794
static int
795
check_xshm(Display *dpy)
796
{
797
xcb_connection_t *c = XGetXCBConnection(dpy);
798
xcb_void_cookie_t cookie;
799
xcb_generic_error_t *error;
800
int ret = True;
801
int ignore;
802
803
if (!XQueryExtension(dpy, "MIT-SHM", &xshm_opcode, &ignore, &ignore))
804
return False;
805
806
cookie = xcb_shm_detach_checked(c, 0);
807
if ((error = xcb_request_check(c, cookie))) {
808
/* BadRequest means we're a remote client. If we were local we'd
809
* expect BadValue since 'info' has an invalid segment name.
810
*/
811
if (error->error_code == BadRequest)
812
ret = False;
813
free(error);
814
}
815
816
return ret;
817
}
818
819
static struct glx_screen *
820
driswCreateScreen(int screen, struct glx_display *priv)
821
{
822
__GLXDRIscreen *psp;
823
const __DRIconfig **driver_configs;
824
const __DRIextension **extensions;
825
struct drisw_screen *psc;
826
struct glx_config *configs = NULL, *visuals = NULL;
827
int i;
828
const __DRIextension **loader_extensions_local;
829
830
psc = calloc(1, sizeof *psc);
831
if (psc == NULL)
832
return NULL;
833
834
if (!glx_screen_init(&psc->base, screen, priv)) {
835
free(psc);
836
return NULL;
837
}
838
839
extensions = driOpenDriver(SWRAST_DRIVER_NAME, &psc->driver);
840
if (extensions == NULL)
841
goto handle_error;
842
843
if (!check_xshm(psc->base.dpy))
844
loader_extensions_local = loader_extensions_noshm;
845
else
846
loader_extensions_local = loader_extensions_shm;
847
848
for (i = 0; extensions[i]; i++) {
849
if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
850
psc->core = (__DRIcoreExtension *) extensions[i];
851
if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
852
psc->swrast = (__DRIswrastExtension *) extensions[i];
853
if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0)
854
psc->copySubBuffer = (__DRIcopySubBufferExtension *) extensions[i];
855
}
856
857
if (psc->core == NULL || psc->swrast == NULL) {
858
ErrorMessageF("core dri extension not found\n");
859
goto handle_error;
860
}
861
862
if (psc->swrast->base.version >= 4) {
863
psc->driScreen =
864
psc->swrast->createNewScreen2(screen, loader_extensions_local,
865
extensions,
866
&driver_configs, psc);
867
} else {
868
psc->driScreen =
869
psc->swrast->createNewScreen(screen, loader_extensions_local,
870
&driver_configs, psc);
871
}
872
if (psc->driScreen == NULL) {
873
ErrorMessageF("failed to create dri screen\n");
874
goto handle_error;
875
}
876
877
extensions = psc->core->getExtensions(psc->driScreen);
878
driswBindExtensions(psc, extensions);
879
880
configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
881
visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
882
883
if (!configs || !visuals) {
884
ErrorMessageF("No matching fbConfigs or visuals found\n");
885
goto handle_error;
886
}
887
888
glx_config_destroy_list(psc->base.configs);
889
psc->base.configs = configs;
890
glx_config_destroy_list(psc->base.visuals);
891
psc->base.visuals = visuals;
892
893
psc->driver_configs = driver_configs;
894
895
psc->base.vtable = &drisw_screen_vtable;
896
psp = &psc->vtable;
897
psc->base.driScreen = psp;
898
psp->destroyScreen = driswDestroyScreen;
899
psp->createDrawable = driswCreateDrawable;
900
psp->swapBuffers = driswSwapBuffers;
901
psp->bindTexImage = drisw_bind_tex_image;
902
psp->releaseTexImage = drisw_release_tex_image;
903
904
if (psc->copySubBuffer)
905
psp->copySubBuffer = driswCopySubBuffer;
906
907
return &psc->base;
908
909
handle_error:
910
if (configs)
911
glx_config_destroy_list(configs);
912
if (visuals)
913
glx_config_destroy_list(visuals);
914
if (psc->driScreen)
915
psc->core->destroyScreen(psc->driScreen);
916
psc->driScreen = NULL;
917
918
if (psc->driver)
919
dlclose(psc->driver);
920
glx_screen_cleanup(&psc->base);
921
free(psc);
922
923
CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME);
924
925
return NULL;
926
}
927
928
/* Called from __glXFreeDisplayPrivate.
929
*/
930
static void
931
driswDestroyDisplay(__GLXDRIdisplay * dpy)
932
{
933
free(dpy);
934
}
935
936
/*
937
* Allocate, initialize and return a __DRIdisplayPrivate object.
938
* This is called from __glXInitialize() when we are given a new
939
* display pointer.
940
*/
941
_X_HIDDEN __GLXDRIdisplay *
942
driswCreateDisplay(Display * dpy)
943
{
944
struct drisw_display *pdpyp;
945
946
pdpyp = malloc(sizeof *pdpyp);
947
if (pdpyp == NULL)
948
return NULL;
949
950
pdpyp->base.destroyDisplay = driswDestroyDisplay;
951
pdpyp->base.createScreen = driswCreateScreen;
952
953
return &pdpyp->base;
954
}
955
956
#endif /* GLX_DIRECT_RENDERING */
957
958