Merge remote branch 'origin/7.8'
[mesa.git] / src / gallium / targets / libgl-xlib / xlib.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Keith Whitwell
32 */
33 #include "pipe/p_compiler.h"
34 #include "util/u_debug.h"
35 #include "target-helpers/wrap_screen.h"
36 #include "state_tracker/xlib_sw_winsys.h"
37 #include "xm_public.h"
38
39 #include "state_tracker/st_manager.h"
40
41 /* advertise OpenGL support */
42 PUBLIC const int st_api_OpenGL = 1;
43
44 PUBLIC const struct st_module st_module_OpenGL = {
45 .api = ST_API_OPENGL,
46 .create_api = st_manager_create_api
47 };
48
49 /* Helper function to choose and instantiate one of the software rasterizers:
50 * cell, llvmpipe, softpipe.
51 *
52 * This function could be shared, but currently causes headaches for
53 * the build systems, particularly scons if we try. Long term, want
54 * to avoid having global #defines for things like GALLIUM_LLVMPIPE,
55 * GALLIUM_CELL, etc. Scons already eliminates those #defines, so
56 * things that are painful for it now are likely to be painful for
57 * other build systems in the future.
58 *
59 * Copies (full or partial):
60 * targets/libgl-xlib
61 * targets/graw-xlib
62 * targets/dri-swrast
63 * winsys/sw/drm
64 * drivers/sw
65 *
66 */
67
68 #ifdef GALLIUM_SOFTPIPE
69 #include "softpipe/sp_public.h"
70 #endif
71
72 #ifdef GALLIUM_LLVMPIPE
73 #include "llvmpipe/lp_public.h"
74 #endif
75
76 #ifdef GALLIUM_CELL
77 #include "cell/ppu/cell_public.h"
78 #endif
79
80 static struct pipe_screen *
81 swrast_create_screen(struct sw_winsys *winsys)
82 {
83 const char *default_driver;
84 const char *driver;
85 struct pipe_screen *screen = NULL;
86
87 #if defined(GALLIUM_CELL)
88 default_driver = "cell";
89 #elif defined(GALLIUM_LLVMPIPE)
90 default_driver = "llvmpipe";
91 #elif defined(GALLIUM_SOFTPIPE)
92 default_driver = "softpipe";
93 #else
94 default_driver = "";
95 #endif
96
97 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
98
99 #if defined(GALLIUM_CELL)
100 if (screen == NULL && strcmp(driver, "cell") == 0)
101 screen = cell_create_screen( winsys );
102 #endif
103
104 #if defined(GALLIUM_LLVMPIPE)
105 if (screen == NULL && strcmp(driver, "llvmpipe") == 0)
106 screen = llvmpipe_create_screen( winsys );
107 #endif
108
109 #if defined(GALLIUM_SOFTPIPE)
110 if (screen == NULL)
111 screen = softpipe_create_screen( winsys );
112 #endif
113
114 return screen;
115 }
116
117 /* Helper function to build a subset of a driver stack consisting of
118 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
119 * xlib winsys.
120 */
121 static struct pipe_screen *
122 swrast_xlib_create_screen( Display *display )
123 {
124 struct sw_winsys *winsys;
125 struct pipe_screen *screen = NULL;
126
127 /* Create the underlying winsys, which performs presents to Xlib
128 * drawables:
129 */
130 winsys = xlib_create_sw_winsys( display );
131 if (winsys == NULL)
132 return NULL;
133
134 /* Create a software rasterizer on top of that winsys:
135 */
136 screen = swrast_create_screen( winsys );
137 if (screen == NULL)
138 goto fail;
139
140 /* Inject any wrapping layers we want to here:
141 */
142 return gallium_wrap_screen( screen );
143
144 fail:
145 if (winsys)
146 winsys->destroy( winsys );
147
148 return NULL;
149 }
150
151 static struct xm_driver xlib_driver =
152 {
153 .create_pipe_screen = swrast_xlib_create_screen,
154 .create_st_api = st_manager_create_api,
155 };
156
157
158 /* Build the rendering stack.
159 */
160 static void _init( void ) __attribute__((constructor));
161 static void _init( void )
162 {
163 /* Initialize the xlib libgl code, pass in the winsys:
164 */
165 xmesa_set_driver( &xlib_driver );
166 }
167
168
169 /***********************************************************************
170 *
171 * Butt-ugly hack to convince the linker not to throw away public GL
172 * symbols (they are all referenced from getprocaddress, I guess).
173 */
174 extern void (*linker_foo(const unsigned char *procName))();
175 extern void (*glXGetProcAddress(const unsigned char *procName))();
176
177 extern void (*linker_foo(const unsigned char *procName))()
178 {
179 return glXGetProcAddress(procName);
180 }
181
182
183 /**
184 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
185 * libglapi.a. We need to define them here.
186 */
187 #ifdef GLX_INDIRECT_RENDERING
188
189 #define GL_GLEXT_PROTOTYPES
190 #include "GL/gl.h"
191 #include "glapi/glapi.h"
192 #include "glapi/glapitable.h"
193 #include "glapi/glapidispatch.h"
194
195 #if defined(USE_MGL_NAMESPACE)
196 #define NAME(func) mgl##func
197 #else
198 #define NAME(func) gl##func
199 #endif
200
201 #define DISPATCH(FUNC, ARGS, MESSAGE) \
202 CALL_ ## FUNC(GET_DISPATCH(), ARGS);
203
204 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
205 return CALL_ ## FUNC(GET_DISPATCH(), ARGS);
206
207 /* skip normal ones */
208 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
209 #include "glapi/glapitemp.h"
210
211 #endif /* GLX_INDIRECT_RENDERING */