Merge commit 'origin/perrtblend'
[mesa.git] / src / gallium / state_trackers / egl / x11 / native_x11.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <string.h>
26 #include "util/u_debug.h"
27 #include "util/u_memory.h"
28 #include "state_tracker/drm_api.h"
29 #include "egllog.h"
30
31 #include "native_x11.h"
32 #include "x11_screen.h"
33
34 #define X11_PROBE_MAGIC 0x11980BE /* "X11PROBE" */
35
36 static struct drm_api *api;
37
38 static void
39 x11_probe_destroy(struct native_probe *nprobe)
40 {
41 if (nprobe->data)
42 free(nprobe->data);
43 free(nprobe);
44 }
45
46 struct native_probe *
47 native_create_probe(EGLNativeDisplayType dpy)
48 {
49 struct native_probe *nprobe;
50 struct x11_screen *xscr;
51 int scr;
52 const char *driver_name = NULL;
53 Display *xdpy;
54
55 nprobe = CALLOC_STRUCT(native_probe);
56 if (!nprobe)
57 return NULL;
58
59 xdpy = dpy;
60 if (!xdpy) {
61 xdpy = XOpenDisplay(NULL);
62 if (!xdpy) {
63 free(nprobe);
64 return NULL;
65 }
66 }
67
68 scr = DefaultScreen(xdpy);
69 xscr = x11_screen_create(xdpy, scr);
70 if (xscr) {
71 if (x11_screen_support(xscr, X11_SCREEN_EXTENSION_DRI2)) {
72 driver_name = x11_screen_probe_dri2(xscr);
73 if (driver_name)
74 nprobe->data = strdup(driver_name);
75 }
76
77 x11_screen_destroy(xscr);
78 }
79
80 if (xdpy != dpy)
81 XCloseDisplay(xdpy);
82
83 nprobe->magic = X11_PROBE_MAGIC;
84 nprobe->display = dpy;
85
86 nprobe->destroy = x11_probe_destroy;
87
88 return nprobe;
89 }
90
91 enum native_probe_result
92 native_get_probe_result(struct native_probe *nprobe)
93 {
94 if (!nprobe || nprobe->magic != X11_PROBE_MAGIC)
95 return NATIVE_PROBE_UNKNOWN;
96
97 if (!api)
98 api = drm_api_create();
99
100 /* this is a software driver */
101 if (!api)
102 return NATIVE_PROBE_SUPPORTED;
103
104 /* the display does not support DRI2 or the driver mismatches */
105 if (!nprobe->data || strcmp(api->name, (const char *) nprobe->data) != 0)
106 return NATIVE_PROBE_FALLBACK;
107
108 return NATIVE_PROBE_EXACT;
109 }
110
111 const char *
112 native_get_name(void)
113 {
114 static char x11_name[32];
115
116 if (!api)
117 api = drm_api_create();
118
119 if (api)
120 snprintf(x11_name, sizeof(x11_name), "X11/%s", api->name);
121 else
122 snprintf(x11_name, sizeof(x11_name), "X11");
123
124 return x11_name;
125 }
126
127 struct native_display *
128 native_create_display(EGLNativeDisplayType dpy)
129 {
130 struct native_display *ndpy = NULL;
131 boolean force_sw;
132
133 if (!api)
134 api = drm_api_create();
135
136 force_sw = debug_get_bool_option("EGL_SOFTWARE", FALSE);
137 if (api && !force_sw) {
138 ndpy = x11_create_dri2_display(dpy, api);
139 }
140
141 if (!ndpy) {
142 EGLint level = (force_sw) ? _EGL_INFO : _EGL_WARNING;
143
144 _eglLog(level, "use software fallback");
145 ndpy = x11_create_ximage_display(dpy, TRUE);
146 }
147
148 return ndpy;
149 }