Revert "st/glsl_to_tgsi: don't increase immediate index by 1."
[mesa.git] / src / gallium / targets / libgl-gdi / libgl_gdi.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 VMware, Inc.
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 * @file
31 * Softpipe/LLVMpipe support.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36
37 #include <windows.h>
38
39 #include "util/u_debug.h"
40 #include "stw_winsys.h"
41 #include "stw_device.h"
42 #include "gdi/gdi_sw_winsys.h"
43
44 #include "softpipe/sp_texture.h"
45 #include "softpipe/sp_screen.h"
46 #include "softpipe/sp_public.h"
47
48 #ifdef HAVE_LLVMPIPE
49 #include "llvmpipe/lp_texture.h"
50 #include "llvmpipe/lp_screen.h"
51 #include "llvmpipe/lp_public.h"
52 #endif
53
54
55 static boolean use_llvmpipe = FALSE;
56
57
58 static struct pipe_screen *
59 gdi_screen_create(void)
60 {
61 const char *default_driver;
62 const char *driver;
63 struct pipe_screen *screen = NULL;
64 struct sw_winsys *winsys;
65
66 winsys = gdi_create_sw_winsys();
67 if(!winsys)
68 goto no_winsys;
69
70 #ifdef HAVE_LLVMPIPE
71 default_driver = "llvmpipe";
72 #else
73 default_driver = "softpipe";
74 #endif
75
76 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
77
78 #ifdef HAVE_LLVMPIPE
79 if (strcmp(driver, "llvmpipe") == 0) {
80 screen = llvmpipe_create_screen( winsys );
81 }
82 #else
83 (void) driver;
84 #endif
85
86 if (screen == NULL) {
87 screen = softpipe_create_screen( winsys );
88 } else {
89 use_llvmpipe = TRUE;
90 }
91
92 if(!screen)
93 goto no_screen;
94
95 return screen;
96
97 no_screen:
98 winsys->destroy(winsys);
99 no_winsys:
100 return NULL;
101 }
102
103
104 static void
105 gdi_present(struct pipe_screen *screen,
106 struct pipe_resource *res,
107 HDC hDC)
108 {
109 /* This will fail if any interposing layer (trace, debug, etc) has
110 * been introduced between the state-trackers and the pipe driver.
111 *
112 * Ideally this would get replaced with a call to
113 * pipe_screen::flush_frontbuffer().
114 *
115 * Failing that, it may be necessary for intervening layers to wrap
116 * other structs such as this stw_winsys as well...
117 */
118
119 struct sw_winsys *winsys = NULL;
120 struct sw_displaytarget *dt = NULL;
121
122 #ifdef HAVE_LLVMPIPE
123 if (use_llvmpipe) {
124 winsys = llvmpipe_screen(screen)->winsys;
125 dt = llvmpipe_resource(res)->dt;
126 gdi_sw_display(winsys, dt, hDC);
127 return;
128 }
129 #endif
130
131 winsys = softpipe_screen(screen)->winsys,
132 dt = softpipe_resource(res)->dt,
133 gdi_sw_display(winsys, dt, hDC);
134 }
135
136
137 static const struct stw_winsys stw_winsys = {
138 &gdi_screen_create,
139 &gdi_present,
140 NULL, /* get_adapter_luid */
141 NULL, /* shared_surface_open */
142 NULL, /* shared_surface_close */
143 NULL /* compose */
144 };
145
146
147 EXTERN_C BOOL WINAPI
148 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
149
150
151 BOOL WINAPI
152 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
153 {
154 switch (fdwReason) {
155 case DLL_PROCESS_ATTACH:
156 stw_init(&stw_winsys);
157 stw_init_thread();
158 break;
159
160 case DLL_THREAD_ATTACH:
161 stw_init_thread();
162 break;
163
164 case DLL_THREAD_DETACH:
165 stw_cleanup_thread();
166 break;
167
168 case DLL_PROCESS_DETACH:
169 if (lpvReserved == NULL) {
170 // We're being unloaded from the process.
171 stw_cleanup_thread();
172 stw_cleanup();
173 } else {
174 // Process itself is terminating, and all threads and modules are
175 // being detached.
176 //
177 // The order threads (including llvmpipe rasterizer threads) are
178 // destroyed can not be relied up, so it's not safe to cleanup.
179 //
180 // However global destructors (e.g., LLVM's) will still be called, and
181 // if Microsoft OPENGL32.DLL's DllMain is called after us, it will
182 // still try to invoke DrvDeleteContext to destroys all outstanding,
183 // so set stw_dev to NULL to return immediately if that happens.
184 stw_dev = NULL;
185 }
186 break;
187 }
188 return TRUE;
189 }