OSDN Git Service

016f91b1963ee4156c63aba977b1fa87e3146201
[android-x86/external-mesa.git] / src / glx / dri3_glx.c
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 (krh@redhat.com)
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 = %.1f\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    /* Check the renderType value */
263    if (!validate_renderType_against_config(config_base, render_type))
264        goto error_exit;
265
266    if (shareList) {
267       pcp_shared = (struct dri3_context *) shareList;
268       shared = pcp_shared->driContext;
269    }
270
271    pcp = calloc(1, sizeof *pcp);
272    if (pcp == NULL) {
273       *error = __DRI_CTX_ERROR_NO_MEMORY;
274       goto error_exit;
275    }
276
277    if (!glx_context_init(&pcp->base, &psc->base, config_base))
278       goto error_exit;
279
280    ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
281    ctx_attribs[num_ctx_attribs++] = major_ver;
282    ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
283    ctx_attribs[num_ctx_attribs++] = minor_ver;
284
285    /* Only send a value when the non-default value is requested.  By doing
286     * this we don't have to check the driver's DRI3 version before sending the
287     * default value.
288     */
289    if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
290       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
291       ctx_attribs[num_ctx_attribs++] = reset;
292    }
293
294    if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
295       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
296       ctx_attribs[num_ctx_attribs++] = release;
297    }
298
299    if (flags != 0) {
300       ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
301
302       /* The current __DRI_CTX_FLAG_* values are identical to the
303        * GLX_CONTEXT_*_BIT values.
304        */
305       ctx_attribs[num_ctx_attribs++] = flags;
306    }
307
308    pcp->driContext =
309       (*psc->image_driver->createContextAttribs) (psc->driScreen,
310                                                   api,
311                                                   config ? config->driConfig
312                                                          : NULL,
313                                                   shared,
314                                                   num_ctx_attribs / 2,
315                                                   ctx_attribs,
316                                                   error,
317                                                   pcp);
318
319    if (pcp->driContext == NULL)
320       goto error_exit;
321
322    pcp->base.vtable = &dri3_context_vtable;
323
324    return &pcp->base;
325
326 error_exit:
327    free(pcp);
328
329    return NULL;
330 }
331
332 static struct glx_context *
333 dri3_create_context(struct glx_screen *base,
334                     struct glx_config *config_base,
335                     struct glx_context *shareList, int renderType)
336 {
337    unsigned int error;
338    uint32_t attribs[2] = { GLX_RENDER_TYPE, renderType };
339
340    return dri3_create_context_attribs(base, config_base, shareList,
341                                       1, attribs, &error);
342 }
343
344 static void
345 dri3_destroy_drawable(__GLXDRIdrawable *base)
346 {
347    struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
348
349    loader_dri3_drawable_fini(&pdraw->loader_drawable);
350
351    free(pdraw);
352 }
353
354 static __GLXDRIdrawable *
355 dri3_create_drawable(struct glx_screen *base, XID xDrawable,
356                      GLXDrawable drawable, struct glx_config *config_base)
357 {
358    struct dri3_drawable *pdraw;
359    struct dri3_screen *psc = (struct dri3_screen *) base;
360    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
361
362    pdraw = calloc(1, sizeof(*pdraw));
363    if (!pdraw)
364       return NULL;
365
366    pdraw->base.destroyDrawable = dri3_destroy_drawable;
367    pdraw->base.xDrawable = xDrawable;
368    pdraw->base.drawable = drawable;
369    pdraw->base.psc = &psc->base;
370
371    (void) __glXInitialize(psc->base.dpy);
372
373    if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy),
374                                  xDrawable, psc->driScreen,
375                                  psc->is_different_gpu, config->driConfig,
376                                  &psc->loader_dri3_ext, &glx_dri3_vtable,
377                                  &pdraw->loader_drawable)) {
378       free(pdraw);
379       return NULL;
380    }
381
382    return &pdraw->base;
383 }
384
385 /** dri3_wait_for_msc
386  *
387  * Get the X server to send an event when the target msc/divisor/remainder is
388  * reached.
389  */
390 static int
391 dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
392                   int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
393 {
394    struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
395
396    loader_dri3_wait_for_msc(&priv->loader_drawable, target_msc, divisor,
397                             remainder, ust, msc, sbc);
398
399    return 1;
400 }
401
402 /** dri3_drawable_get_msc
403  *
404  * Return the current UST/MSC/SBC triplet by asking the server
405  * for an event
406  */
407 static int
408 dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
409                       int64_t *ust, int64_t *msc, int64_t *sbc)
410 {
411    return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc);
412 }
413
414 /** dri3_wait_for_sbc
415  *
416  * Wait for the completed swap buffer count to reach the specified
417  * target. Presumably the application knows that this will be reached with
418  * outstanding complete events, or we're going to be here awhile.
419  */
420 static int
421 dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
422                   int64_t *msc, int64_t *sbc)
423 {
424    struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
425
426    return loader_dri3_wait_for_sbc(&priv->loader_drawable, target_sbc,
427                                    ust, msc, sbc);
428 }
429
430 static void
431 dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y,
432                      int width, int height,
433                      Bool flush)
434 {
435    struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
436
437    loader_dri3_copy_sub_buffer(&priv->loader_drawable, x, y,
438                                width, height, flush);
439 }
440
441 static void
442 dri3_wait_x(struct glx_context *gc)
443 {
444    struct dri3_drawable *priv = (struct dri3_drawable *)
445       GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
446
447    if (priv)
448       loader_dri3_wait_x(&priv->loader_drawable);
449 }
450
451 static void
452 dri3_wait_gl(struct glx_context *gc)
453 {
454    struct dri3_drawable *priv = (struct dri3_drawable *)
455       GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
456
457    if (priv)
458       loader_dri3_wait_gl(&priv->loader_drawable);
459 }
460
461 /**
462  * Called by the driver when it needs to update the real front buffer with the
463  * contents of its fake front buffer.
464  */
465 static void
466 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
467 {
468    struct loader_dri3_drawable *draw = loaderPrivate;
469    struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
470    struct dri3_screen *psc;
471
472    if (!pdraw)
473       return;
474
475    if (!pdraw->base.psc)
476       return;
477
478    psc = (struct dri3_screen *) pdraw->base.psc;
479
480    (void) __glXInitialize(psc->base.dpy);
481
482    loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
483
484    (*psc->f->invalidate)(driDrawable);
485    loader_dri3_wait_gl(draw);
486 }
487
488 /**
489  * Make sure all pending swapbuffers have been submitted to hardware
490  *
491  * \param driDrawable[in]  Pointer to the dri drawable whose swaps we are
492  * flushing.
493  * \param loaderPrivate[in]  Pointer to the corresponding struct
494  * loader_dri_drawable.
495  */
496 static void
497 dri3_flush_swap_buffers(__DRIdrawable *driDrawable, void *loaderPrivate)
498 {
499    struct loader_dri3_drawable *draw = loaderPrivate;
500    struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
501    struct dri3_screen *psc;
502
503    if (!pdraw)
504       return;
505
506    if (!pdraw->base.psc)
507       return;
508
509    psc = (struct dri3_screen *) pdraw->base.psc;
510
511    (void) __glXInitialize(psc->base.dpy);
512    loader_dri3_swapbuffer_barrier(draw);
513 }
514
515 static void
516 dri_set_background_context(void *loaderPrivate)
517 {
518    struct dri3_context *pcp = (struct dri3_context *)loaderPrivate;
519    __glXSetCurrentContext(&pcp->base);
520 }
521
522 static GLboolean
523 dri_is_thread_safe(void *loaderPrivate)
524 {
525    /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
526     * during draw so we're safe here.
527     */
528    return true;
529 }
530
531 /* The image loader extension record for DRI3
532  */
533 static const __DRIimageLoaderExtension imageLoaderExtension = {
534    .base = { __DRI_IMAGE_LOADER, 3 },
535
536    .getBuffers          = loader_dri3_get_buffers,
537    .flushFrontBuffer    = dri3_flush_front_buffer,
538    .flushSwapBuffers    = dri3_flush_swap_buffers,
539 };
540
541 const __DRIuseInvalidateExtension dri3UseInvalidate = {
542    .base = { __DRI_USE_INVALIDATE, 1 }
543 };
544
545 static const __DRIbackgroundCallableExtension driBackgroundCallable = {
546    .base = { __DRI_BACKGROUND_CALLABLE, 2 },
547
548    .setBackgroundContext = dri_set_background_context,
549    .isThreadSafe         = dri_is_thread_safe,
550 };
551
552 static const __DRIextension *loader_extensions[] = {
553    &imageLoaderExtension.base,
554    &dri3UseInvalidate.base,
555    &driBackgroundCallable.base,
556    NULL
557 };
558
559 /** dri3_swap_buffers
560  *
561  * Make the current back buffer visible using the present extension
562  */
563 static int64_t
564 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
565                   int64_t remainder, Bool flush)
566 {
567    struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
568    unsigned flags = __DRI2_FLUSH_DRAWABLE;
569
570    if (flush)
571       flags |= __DRI2_FLUSH_CONTEXT;
572
573    return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
574                                        target_msc, divisor, remainder,
575                                        flags, false);
576 }
577
578 static int
579 dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
580 {
581    struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
582
583    return loader_dri3_query_buffer_age(&priv->loader_drawable);
584 }
585
586 /** dri3_destroy_screen
587  */
588 static void
589 dri3_destroy_screen(struct glx_screen *base)
590 {
591    struct dri3_screen *psc = (struct dri3_screen *) base;
592
593    /* Free the direct rendering per screen data */
594    loader_dri3_close_screen(psc->driScreen);
595    (*psc->core->destroyScreen) (psc->driScreen);
596    driDestroyConfigs(psc->driver_configs);
597    close(psc->fd);
598    free(psc);
599 }
600
601 /** dri3_set_swap_interval
602  *
603  * Record the application swap interval specification,
604  */
605 static int
606 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
607 {
608    assert(pdraw != NULL);
609
610    struct dri3_drawable *priv =  (struct dri3_drawable *) pdraw;
611    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
612    struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
613
614    if (psc->config)
615       psc->config->configQueryi(psc->driScreen,
616                                 "vblank_mode", &vblank_mode);
617
618    switch (vblank_mode) {
619    case DRI_CONF_VBLANK_NEVER:
620       if (interval != 0)
621          return GLX_BAD_VALUE;
622       break;
623    case DRI_CONF_VBLANK_ALWAYS_SYNC:
624       if (interval <= 0)
625          return GLX_BAD_VALUE;
626       break;
627    default:
628       break;
629    }
630
631    priv->swap_interval = interval;
632    loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
633
634    return 0;
635 }
636
637 /** dri3_get_swap_interval
638  *
639  * Return the stored swap interval
640  */
641 static int
642 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
643 {
644    assert(pdraw != NULL);
645
646    struct dri3_drawable *priv =  (struct dri3_drawable *) pdraw;
647
648   return priv->swap_interval;
649 }
650
651 static void
652 dri3_bind_tex_image(Display * dpy,
653                     GLXDrawable drawable,
654                     int buffer, const int *attrib_list)
655 {
656    struct glx_context *gc = __glXGetCurrentContext();
657    struct dri3_context *pcp = (struct dri3_context *) gc;
658    __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
659    struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
660    struct dri3_screen *psc;
661
662    if (pdraw != NULL) {
663       psc = (struct dri3_screen *) base->psc;
664
665       (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable);
666
667       XSync(dpy, false);
668
669       (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
670                                         pdraw->base.textureTarget,
671                                         pdraw->base.textureFormat,
672                                         pdraw->loader_drawable.dri_drawable);
673    }
674 }
675
676 static void
677 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
678 {
679    struct glx_context *gc = __glXGetCurrentContext();
680    struct dri3_context *pcp = (struct dri3_context *) gc;
681    __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
682    struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
683    struct dri3_screen *psc;
684
685    if (pdraw != NULL) {
686       psc = (struct dri3_screen *) base->psc;
687
688       if (psc->texBuffer->base.version >= 3 &&
689           psc->texBuffer->releaseTexBuffer != NULL)
690          (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
691                                               pdraw->base.textureTarget,
692                                               pdraw->loader_drawable.dri_drawable);
693    }
694 }
695
696 static const struct glx_context_vtable dri3_context_vtable = {
697    .destroy             = dri3_destroy_context,
698    .bind                = dri3_bind_context,
699    .unbind              = dri3_unbind_context,
700    .wait_gl             = dri3_wait_gl,
701    .wait_x              = dri3_wait_x,
702    .use_x_font          = DRI_glXUseXFont,
703    .bind_tex_image      = dri3_bind_tex_image,
704    .release_tex_image   = dri3_release_tex_image,
705    .get_proc_address    = NULL,
706    .interop_query_device_info = dri3_interop_query_device_info,
707    .interop_export_object = dri3_interop_export_object
708 };
709
710 /** dri3_bind_extensions
711  *
712  * Enable all of the extensions supported on DRI3
713  */
714 static void
715 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
716                      const char *driverName)
717 {
718    const __DRIextension **extensions;
719    unsigned mask;
720    int i;
721
722    extensions = psc->core->getExtensions(psc->driScreen);
723
724    __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
725    __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
726    __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
727    __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
728    __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
729
730    mask = psc->image_driver->getAPIMask(psc->driScreen);
731
732    __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
733    __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
734
735    if ((mask & ((1 << __DRI_API_GLES) |
736                 (1 << __DRI_API_GLES2) |
737                 (1 << __DRI_API_GLES3))) != 0) {
738       __glXEnableDirectExtension(&psc->base,
739                                  "GLX_EXT_create_context_es_profile");
740       __glXEnableDirectExtension(&psc->base,
741                                  "GLX_EXT_create_context_es2_profile");
742    }
743
744    for (i = 0; extensions[i]; i++) {
745       /* when on a different gpu than the server, the server pixmaps
746        * can have a tiling mode we can't read. Thus we can't create
747        * a texture from them.
748        */
749       if (!psc->is_different_gpu &&
750          (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
751          psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
752          __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
753       }
754
755       if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
756          psc->f = (__DRI2flushExtension *) extensions[i];
757          /* internal driver extension, no GL extension exposed */
758       }
759
760       if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
761          psc->image = (__DRIimageExtension *) extensions[i];
762
763       if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
764          psc->config = (__DRI2configQueryExtension *) extensions[i];
765
766       if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
767          __glXEnableDirectExtension(&psc->base,
768                                     "GLX_ARB_create_context_robustness");
769
770       if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
771          psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
772          __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
773       }
774
775       if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
776          psc->interop = (__DRI2interopExtension*)extensions[i];
777
778       if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
779          __glXEnableDirectExtension(&psc->base,
780                                     "GLX_ARB_context_flush_control");
781    }
782 }
783
784 static const struct glx_screen_vtable dri3_screen_vtable = {
785    .create_context         = dri3_create_context,
786    .create_context_attribs = dri3_create_context_attribs,
787    .query_renderer_integer = dri3_query_renderer_integer,
788    .query_renderer_string  = dri3_query_renderer_string,
789 };
790
791 /** dri3_create_screen
792  *
793  * Initialize DRI3 on the specified screen.
794  *
795  * Opens the DRI device, locates the appropriate DRI driver
796  * and loads that.
797  *
798  * Checks to see if the driver supports the necessary extensions
799  *
800  * Initializes the driver for the screen and sets up our structures
801  */
802
803 static struct glx_screen *
804 dri3_create_screen(int screen, struct glx_display * priv)
805 {
806    xcb_connection_t *c = XGetXCBConnection(priv->dpy);
807    const __DRIconfig **driver_configs;
808    const __DRIextension **extensions;
809    const struct dri3_display *const pdp = (struct dri3_display *)
810       priv->dri3Display;
811    struct dri3_screen *psc;
812    __GLXDRIscreen *psp;
813    struct glx_config *configs = NULL, *visuals = NULL;
814    char *driverName, *tmp;
815    int i;
816    unsigned char disable;
817
818    psc = calloc(1, sizeof *psc);
819    if (psc == NULL)
820       return NULL;
821
822    psc->fd = -1;
823
824    if (!glx_screen_init(&psc->base, screen, priv)) {
825       free(psc);
826       return NULL;
827    }
828
829    psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
830    if (psc->fd < 0) {
831       int conn_error = xcb_connection_has_error(c);
832
833       glx_screen_cleanup(&psc->base);
834       free(psc);
835       InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
836
837       if (conn_error)
838          ErrorMessageF("Connection closed during DRI3 initialization failure");
839
840       return NULL;
841    }
842
843    psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
844
845    driverName = loader_get_driver_for_fd(psc->fd);
846    if (!driverName) {
847       ErrorMessageF("No driver found\n");
848       goto handle_error;
849    }
850
851    psc->driver = driOpenDriver(driverName);
852    if (psc->driver == NULL) {
853       ErrorMessageF("driver pointer missing\n");
854       goto handle_error;
855    }
856
857    extensions = driGetDriverExtensions(psc->driver, driverName);
858    if (extensions == NULL)
859       goto handle_error;
860
861    for (i = 0; extensions[i]; i++) {
862       if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
863          psc->core = (__DRIcoreExtension *) extensions[i];
864       if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
865          psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
866    }
867
868
869    if (psc->core == NULL) {
870       ErrorMessageF("core dri driver extension not found\n");
871       goto handle_error;
872    }
873
874    if (psc->image_driver == NULL) {
875       ErrorMessageF("image driver extension not found\n");
876       goto handle_error;
877    }
878
879    psc->driScreen =
880       psc->image_driver->createNewScreen2(screen, psc->fd,
881                                           pdp->loader_extensions,
882                                           extensions,
883                                           &driver_configs, psc);
884
885    if (psc->driScreen == NULL) {
886       ErrorMessageF("failed to create dri screen\n");
887       goto handle_error;
888    }
889
890    dri3_bind_extensions(psc, priv, driverName);
891
892    if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
893       ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
894       goto handle_error;
895    }
896
897    if (!psc->f || psc->f->base.version < 4) {
898       ErrorMessageF("Version 4 or later of flush extension not found\n");
899       goto handle_error;
900    }
901
902    if (psc->is_different_gpu && psc->image->base.version < 9) {
903       ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
904       goto handle_error;
905    }
906
907    if (psc->is_different_gpu && !psc->image->blitImage) {
908       ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
909       goto handle_error;
910    }
911
912    if (!psc->is_different_gpu && (
913        !psc->texBuffer || psc->texBuffer->base.version < 2 ||
914        !psc->texBuffer->setTexBuffer2
915        )) {
916       ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
917       goto handle_error;
918    }
919
920    psc->loader_dri3_ext.core = psc->core;
921    psc->loader_dri3_ext.image_driver = psc->image_driver;
922    psc->loader_dri3_ext.flush = psc->f;
923    psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
924    psc->loader_dri3_ext.image = psc->image;
925    psc->loader_dri3_ext.config = psc->config;
926
927    configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
928    visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
929
930    if (!configs || !visuals) {
931        ErrorMessageF("No matching fbConfigs or visuals found\n");
932        goto handle_error;
933    }
934
935    glx_config_destroy_list(psc->base.configs);
936    psc->base.configs = configs;
937    glx_config_destroy_list(psc->base.visuals);
938    psc->base.visuals = visuals;
939
940    psc->driver_configs = driver_configs;
941
942    psc->base.vtable = &dri3_screen_vtable;
943    psp = &psc->vtable;
944    psc->base.driScreen = psp;
945    psp->destroyScreen = dri3_destroy_screen;
946    psp->createDrawable = dri3_create_drawable;
947    psp->swapBuffers = dri3_swap_buffers;
948
949    psp->getDrawableMSC = dri3_drawable_get_msc;
950    psp->waitForMSC = dri3_wait_for_msc;
951    psp->waitForSBC = dri3_wait_for_sbc;
952    psp->setSwapInterval = dri3_set_swap_interval;
953    psp->getSwapInterval = dri3_get_swap_interval;
954    if (psc->config->configQueryb(psc->driScreen,
955                                  "glx_disable_oml_sync_control",
956                                  &disable) || !disable)
957       __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
958
959    psp->copySubBuffer = dri3_copy_sub_buffer;
960    __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
961
962    psp->getBufferAge = dri3_get_buffer_age;
963    if (psc->config->configQueryb(psc->driScreen,
964                                  "glx_disable_ext_buffer_age",
965                                  &disable) || !disable)
966       __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
967
968    free(driverName);
969
970    tmp = getenv("LIBGL_SHOW_FPS");
971    psc->show_fps_interval = tmp ? atoi(tmp) : 0;
972    if (psc->show_fps_interval < 0)
973       psc->show_fps_interval = 0;
974
975    InfoMessageF("Using DRI3 for screen %d\n", screen);
976
977    return &psc->base;
978
979 handle_error:
980    CriticalErrorMessageF("failed to load driver: %s\n", driverName);
981
982    if (configs)
983        glx_config_destroy_list(configs);
984    if (visuals)
985        glx_config_destroy_list(visuals);
986    if (psc->driScreen)
987        psc->core->destroyScreen(psc->driScreen);
988    psc->driScreen = NULL;
989    if (psc->fd >= 0)
990       close(psc->fd);
991    if (psc->driver)
992       dlclose(psc->driver);
993
994    free(driverName);
995    glx_screen_cleanup(&psc->base);
996    free(psc);
997
998    return NULL;
999 }
1000
1001 /** dri_destroy_display
1002  *
1003  * Called from __glXFreeDisplayPrivate.
1004  */
1005 static void
1006 dri3_destroy_display(__GLXDRIdisplay * dpy)
1007 {
1008    free(dpy);
1009 }
1010
1011 /** dri3_create_display
1012  *
1013  * Allocate, initialize and return a __DRIdisplayPrivate object.
1014  * This is called from __glXInitialize() when we are given a new
1015  * display pointer. This is public to that function, but hidden from
1016  * outside of libGL.
1017  */
1018 _X_HIDDEN __GLXDRIdisplay *
1019 dri3_create_display(Display * dpy)
1020 {
1021    struct dri3_display                  *pdp;
1022    xcb_connection_t                     *c = XGetXCBConnection(dpy);
1023    xcb_dri3_query_version_cookie_t      dri3_cookie;
1024    xcb_dri3_query_version_reply_t       *dri3_reply;
1025    xcb_present_query_version_cookie_t   present_cookie;
1026    xcb_present_query_version_reply_t    *present_reply;
1027    xcb_generic_error_t                  *error;
1028    const xcb_query_extension_reply_t    *extension;
1029
1030    xcb_prefetch_extension_data(c, &xcb_dri3_id);
1031    xcb_prefetch_extension_data(c, &xcb_present_id);
1032
1033    extension = xcb_get_extension_data(c, &xcb_dri3_id);
1034    if (!(extension && extension->present))
1035       return NULL;
1036
1037    extension = xcb_get_extension_data(c, &xcb_present_id);
1038    if (!(extension && extension->present))
1039       return NULL;
1040
1041    dri3_cookie = xcb_dri3_query_version(c,
1042                                         XCB_DRI3_MAJOR_VERSION,
1043                                         XCB_DRI3_MINOR_VERSION);
1044
1045
1046    present_cookie = xcb_present_query_version(c,
1047                                    XCB_PRESENT_MAJOR_VERSION,
1048                                    XCB_PRESENT_MINOR_VERSION);
1049
1050    pdp = malloc(sizeof *pdp);
1051    if (pdp == NULL)
1052       return NULL;
1053
1054    dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1055    if (!dri3_reply) {
1056       free(error);
1057       goto no_extension;
1058    }
1059
1060    pdp->dri3Major = dri3_reply->major_version;
1061    pdp->dri3Minor = dri3_reply->minor_version;
1062    free(dri3_reply);
1063
1064    present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1065    if (!present_reply) {
1066       free(error);
1067       goto no_extension;
1068    }
1069    pdp->presentMajor = present_reply->major_version;
1070    pdp->presentMinor = present_reply->minor_version;
1071    free(present_reply);
1072
1073    pdp->base.destroyDisplay = dri3_destroy_display;
1074    pdp->base.createScreen = dri3_create_screen;
1075
1076    loader_set_logger(dri_message);
1077
1078    pdp->loader_extensions = loader_extensions;
1079
1080    return &pdp->base;
1081 no_extension:
1082    free(pdp);
1083    return NULL;
1084 }
1085
1086 #endif /* GLX_DIRECT_RENDERING */