Merge branch '7.8' into master
[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 "state_tracker/xlib_sw_winsys.h"
35 #include "util/u_debug.h"
36 #include "softpipe/sp_public.h"
37 #include "llvmpipe/lp_public.h"
38 #include "cell/ppu/cell_public.h"
39 #include "target-helpers/wrap_screen.h"
40 #include "xm_public.h"
41
42 #include "state_tracker/st_manager.h"
43
44 /* advertise OpenGL support */
45 PUBLIC const int st_api_OpenGL = 1;
46
47 PUBLIC const struct st_module st_module_OpenGL = {
48 .api = ST_API_OPENGL,
49 .create_api = st_manager_create_api
50 };
51
52 /* Helper function to build a subset of a driver stack consisting of
53 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
54 * xlib winsys.
55 *
56 * This function could be shared, but currently causes headaches for
57 * the build systems, particularly scons if we try. Long term, want
58 * to avoid having global #defines for things like GALLIUM_LLVMPIPE,
59 * GALLIUM_CELL, etc. Scons already eliminates those #defines, so
60 * things that are painful for it now are likely to be painful for
61 * other build systems in the future.
62 */
63 static struct pipe_screen *
64 swrast_xlib_create_screen( Display *display )
65 {
66 const char *default_driver;
67 const char *driver;
68 struct sw_winsys *winsys;
69 struct pipe_screen *screen = NULL;
70
71 /* Create the underlying winsys, which performs presents to Xlib
72 * drawables:
73 */
74 winsys = xlib_create_sw_winsys( display );
75 if (winsys == NULL)
76 return NULL;
77
78 #if defined(GALLIUM_CELL)
79 default_driver = "cell";
80 #elif defined(GALLIUM_LLVMPIPE)
81 default_driver = "llvmpipe";
82 #elif defined(GALLIUM_SOFTPIPE)
83 default_driver = "softpipe";
84 #else
85 default_driver = "";
86 #endif
87
88 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
89
90 /* Create a software rasterizer on top of that winsys:
91 */
92 #if defined(GALLIUM_CELL)
93 if (screen == NULL &&
94 strcmp(driver, "cell") == 0)
95 screen = cell_create_screen( winsys );
96 #endif
97
98 #if defined(GALLIUM_LLVMPIPE)
99 if (screen == NULL &&
100 strcmp(driver, "llvmpipe") == 0)
101 screen = llvmpipe_create_screen( winsys );
102 #endif
103
104 #if defined(GALLIUM_SOFTPIPE)
105 if (screen == NULL)
106 screen = softpipe_create_screen( winsys );
107 #endif
108
109 if (screen == NULL)
110 goto fail;
111
112 /* Inject any wrapping layers we want to here:
113 */
114 return gallium_wrap_screen( screen );
115
116 fail:
117 if (winsys)
118 winsys->destroy( winsys );
119
120 return NULL;
121 }
122
123 static struct xm_driver xlib_driver =
124 {
125 .create_pipe_screen = swrast_xlib_create_screen,
126 .create_st_api = st_manager_create_api,
127 };
128
129
130 /* Build the rendering stack.
131 */
132 static void _init( void ) __attribute__((constructor));
133 static void _init( void )
134 {
135 /* Initialize the xlib libgl code, pass in the winsys:
136 */
137 xmesa_set_driver( &xlib_driver );
138 }
139
140
141 /***********************************************************************
142 *
143 * Butt-ugly hack to convince the linker not to throw away public GL
144 * symbols (they are all referenced from getprocaddress, I guess).
145 */
146 extern void (*linker_foo(const unsigned char *procName))();
147 extern void (*glXGetProcAddress(const unsigned char *procName))();
148
149 extern void (*linker_foo(const unsigned char *procName))()
150 {
151 return glXGetProcAddress(procName);
152 }
153
154
155 /**
156 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
157 * libglapi.a. We need to define them here.
158 */
159 #ifdef GLX_INDIRECT_RENDERING
160
161 #define GL_GLEXT_PROTOTYPES
162 #include "GL/gl.h"
163 #include "glapi/glapi.h"
164 #include "glapi/glapitable.h"
165 #include "glapi/glapidispatch.h"
166
167 #if defined(USE_MGL_NAMESPACE)
168 #define NAME(func) mgl##func
169 #else
170 #define NAME(func) gl##func
171 #endif
172
173 #define DISPATCH(FUNC, ARGS, MESSAGE) \
174 CALL_ ## FUNC(GET_DISPATCH(), ARGS);
175
176 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
177 return CALL_ ## FUNC(GET_DISPATCH(), ARGS);
178
179 /* skip normal ones */
180 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
181 #include "glapi/glapitemp.h"
182
183 #endif /* GLX_INDIRECT_RENDERING */