2f8cd2d404ae01262621c0bea82db98141d2bbe9
[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_api.h"
40 #include "state_tracker/st_gl_api.h"
41
42 /* Helper function to choose and instantiate one of the software rasterizers:
43 * cell, llvmpipe, softpipe.
44 *
45 * This function could be shared, but currently causes headaches for
46 * the build systems, particularly scons if we try. Long term, want
47 * to avoid having global #defines for things like GALLIUM_LLVMPIPE,
48 * GALLIUM_CELL, etc. Scons already eliminates those #defines, so
49 * things that are painful for it now are likely to be painful for
50 * other build systems in the future.
51 *
52 * Copies (full or partial):
53 * targets/libgl-xlib
54 * targets/graw-xlib
55 * targets/dri-swrast
56 * winsys/sw/drm
57 * drivers/sw
58 *
59 */
60
61 #ifdef GALLIUM_SOFTPIPE
62 #include "softpipe/sp_public.h"
63 #endif
64
65 #ifdef GALLIUM_LLVMPIPE
66 #include "llvmpipe/lp_public.h"
67 #endif
68
69 #ifdef GALLIUM_CELL
70 #include "cell/ppu/cell_public.h"
71 #endif
72
73 #ifdef GALLIUM_GALAHAD
74 #include "galahad/glhd_public.h"
75 #endif
76
77 static struct pipe_screen *
78 swrast_create_screen(struct sw_winsys *winsys)
79 {
80 const char *default_driver;
81 const char *driver;
82 struct pipe_screen *screen = NULL;
83
84 #if defined(GALLIUM_CELL)
85 default_driver = "cell";
86 #elif defined(GALLIUM_LLVMPIPE)
87 default_driver = "llvmpipe";
88 #elif defined(GALLIUM_SOFTPIPE)
89 default_driver = "softpipe";
90 #else
91 default_driver = "";
92 #endif
93
94 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
95
96 #if defined(GALLIUM_CELL)
97 if (screen == NULL && strcmp(driver, "cell") == 0)
98 screen = cell_create_screen( winsys );
99 #endif
100
101 #if defined(GALLIUM_LLVMPIPE)
102 if (screen == NULL && strcmp(driver, "llvmpipe") == 0)
103 screen = llvmpipe_create_screen( winsys );
104 #endif
105
106 #if defined(GALLIUM_SOFTPIPE)
107 if (screen == NULL)
108 screen = softpipe_create_screen( winsys );
109 #endif
110
111 #if defined(GALLIUM_GALAHAD)
112 if (screen) {
113 struct pipe_screen *galahad_screen = galahad_screen_create(screen);
114 if (galahad_screen)
115 screen = galahad_screen;
116 }
117 #endif
118
119 return screen;
120 }
121
122 /* Helper function to build a subset of a driver stack consisting of
123 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
124 * xlib winsys.
125 */
126 static struct pipe_screen *
127 swrast_xlib_create_screen( Display *display )
128 {
129 struct sw_winsys *winsys;
130 struct pipe_screen *screen = NULL;
131
132 /* Create the underlying winsys, which performs presents to Xlib
133 * drawables:
134 */
135 winsys = xlib_create_sw_winsys( display );
136 if (winsys == NULL)
137 return NULL;
138
139 /* Create a software rasterizer on top of that winsys:
140 */
141 screen = swrast_create_screen( winsys );
142 if (screen == NULL)
143 goto fail;
144
145 /* Inject any wrapping layers we want to here:
146 */
147 return gallium_wrap_screen( screen );
148
149 fail:
150 if (winsys)
151 winsys->destroy( winsys );
152
153 return NULL;
154 }
155
156 static struct xm_driver xlib_driver =
157 {
158 .create_pipe_screen = swrast_xlib_create_screen,
159 .create_st_api = st_gl_api_create,
160 };
161
162
163 /* Build the rendering stack.
164 */
165 static void _init( void ) __attribute__((constructor));
166 static void _init( void )
167 {
168 /* Initialize the xlib libgl code, pass in the winsys:
169 */
170 xmesa_set_driver( &xlib_driver );
171 }
172
173
174 /***********************************************************************
175 *
176 * Butt-ugly hack to convince the linker not to throw away public GL
177 * symbols (they are all referenced from getprocaddress, I guess).
178 */
179 extern void (*linker_foo(const unsigned char *procName))();
180 extern void (*glXGetProcAddress(const unsigned char *procName))();
181
182 extern void (*linker_foo(const unsigned char *procName))()
183 {
184 return glXGetProcAddress(procName);
185 }
186
187
188 /**
189 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
190 * libglapi.a. We need to define them here.
191 */
192 #ifdef GLX_INDIRECT_RENDERING
193
194 #define GL_GLEXT_PROTOTYPES
195 #include "GL/gl.h"
196 #include "glapi/glapi.h"
197 #include "glapi/glapitable.h"
198 #include "glapi/glapidispatch.h"
199
200 #if defined(USE_MGL_NAMESPACE)
201 #define NAME(func) mgl##func
202 #else
203 #define NAME(func) gl##func
204 #endif
205
206 #define DISPATCH(FUNC, ARGS, MESSAGE) \
207 CALL_ ## FUNC(GET_DISPATCH(), ARGS);
208
209 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
210 return CALL_ ## FUNC(GET_DISPATCH(), ARGS);
211
212 /* skip normal ones */
213 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
214 #include "glapi/glapitemp.h"
215
216 #endif /* GLX_INDIRECT_RENDERING */