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 #ifdef GALLIUM_SOFTPIPE
45 #include "softpipe/sp_texture.h"
46 #include "softpipe/sp_screen.h"
47 #include "softpipe/sp_public.h"
48 #endif
49
50 #ifdef GALLIUM_LLVMPIPE
51 #include "llvmpipe/lp_texture.h"
52 #include "llvmpipe/lp_screen.h"
53 #include "llvmpipe/lp_public.h"
54 #endif
55
56 #ifdef GALLIUM_SWR
57 #include "swr/swr_public.h"
58 #endif
59
60 #ifdef GALLIUM_LLVMPIPE
61 static boolean use_llvmpipe = FALSE;
62 #endif
63 #ifdef GALLIUM_SWR
64 static boolean use_swr = FALSE;
65 #endif
66
67 static struct pipe_screen *
gdi_screen_create(void)68 gdi_screen_create(void)
69 {
70 const char *default_driver;
71 const char *driver;
72 struct pipe_screen *screen = NULL;
73 struct sw_winsys *winsys;
74
75 winsys = gdi_create_sw_winsys();
76 if(!winsys)
77 goto no_winsys;
78
79 #ifdef GALLIUM_LLVMPIPE
80 default_driver = "llvmpipe";
81 #elif GALLIUM_SWR
82 default_driver = "swr";
83 #elif defined(GALLIUM_SOFTPIPE)
84 default_driver = "softpipe";
85 #else
86 #error "no suitable default-driver"
87 #endif
88
89 driver = debug_get_option("GALLIUM_DRIVER", default_driver);
90
91 #ifdef GALLIUM_LLVMPIPE
92 if (strcmp(driver, "llvmpipe") == 0) {
93 screen = llvmpipe_create_screen( winsys );
94 if (screen)
95 use_llvmpipe = TRUE;
96 }
97 #endif
98 #ifdef GALLIUM_SWR
99 if (strcmp(driver, "swr") == 0) {
100 screen = swr_create_screen( winsys );
101 if (screen)
102 use_swr = TRUE;
103 }
104 #endif
105 (void) driver;
106
107 #ifdef GALLIUM_SOFTPIPE
108 if (screen == NULL)
109 screen = softpipe_create_screen( winsys );
110 #endif
111 if (!screen)
112 goto no_screen;
113
114 return screen;
115
116 no_screen:
117 winsys->destroy(winsys);
118 no_winsys:
119 return NULL;
120 }
121
122
123 static void
gdi_present(struct pipe_screen * screen,struct pipe_resource * res,HDC hDC)124 gdi_present(struct pipe_screen *screen,
125 struct pipe_resource *res,
126 HDC hDC)
127 {
128 /* This will fail if any interposing layer (trace, debug, etc) has
129 * been introduced between the gallium frontends and the pipe driver.
130 *
131 * Ideally this would get replaced with a call to
132 * pipe_screen::flush_frontbuffer().
133 *
134 * Failing that, it may be necessary for intervening layers to wrap
135 * other structs such as this stw_winsys as well...
136 */
137
138 struct sw_winsys *winsys = NULL;
139 struct sw_displaytarget *dt = NULL;
140
141 #ifdef GALLIUM_LLVMPIPE
142 if (use_llvmpipe) {
143 winsys = llvmpipe_screen(screen)->winsys;
144 dt = llvmpipe_resource(res)->dt;
145 gdi_sw_display(winsys, dt, hDC);
146 return;
147 }
148 #endif
149
150 #ifdef GALLIUM_SWR
151 if (use_swr) {
152 swr_gdi_swap(screen, res, hDC);
153 return;
154 }
155 #endif
156
157 #ifdef GALLIUM_SOFTPIPE
158 winsys = softpipe_screen(screen)->winsys,
159 dt = softpipe_resource(res)->dt,
160 gdi_sw_display(winsys, dt, hDC);
161 #endif
162 }
163
164
165 static const struct stw_winsys stw_winsys = {
166 &gdi_screen_create,
167 &gdi_present,
168 NULL, /* get_adapter_luid */
169 NULL, /* shared_surface_open */
170 NULL, /* shared_surface_close */
171 NULL /* compose */
172 };
173
174
175 EXTERN_C BOOL WINAPI
176 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
177
178
179 BOOL WINAPI
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)180 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
181 {
182 switch (fdwReason) {
183 case DLL_PROCESS_ATTACH:
184 stw_init(&stw_winsys);
185 stw_init_thread();
186 break;
187
188 case DLL_THREAD_ATTACH:
189 stw_init_thread();
190 break;
191
192 case DLL_THREAD_DETACH:
193 stw_cleanup_thread();
194 break;
195
196 case DLL_PROCESS_DETACH:
197 if (lpvReserved == NULL) {
198 // We're being unloaded from the process.
199 stw_cleanup_thread();
200 stw_cleanup();
201 } else {
202 // Process itself is terminating, and all threads and modules are
203 // being detached.
204 //
205 // The order threads (including llvmpipe rasterizer threads) are
206 // destroyed can not be relied up, so it's not safe to cleanup.
207 //
208 // However global destructors (e.g., LLVM's) will still be called, and
209 // if Microsoft OPENGL32.DLL's DllMain is called after us, it will
210 // still try to invoke DrvDeleteContext to destroys all outstanding,
211 // so set stw_dev to NULL to return immediately if that happens.
212 stw_dev = NULL;
213 }
214 break;
215 }
216 return TRUE;
217 }
218