1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_mem.h"
|
---|
8 | #include "cr_spu.h"
|
---|
9 | #include "cr_error.h"
|
---|
10 | #include "cr_string.h"
|
---|
11 | #include "cr_url.h"
|
---|
12 | #include "renderspu.h"
|
---|
13 | #include <stdio.h>
|
---|
14 |
|
---|
15 | static SPUNamedFunctionTable _cr_render_table[1000];
|
---|
16 |
|
---|
17 | SPUFunctions render_functions = {
|
---|
18 | NULL, /* CHILD COPY */
|
---|
19 | NULL, /* DATA */
|
---|
20 | _cr_render_table /* THE ACTUAL FUNCTIONS */
|
---|
21 | };
|
---|
22 |
|
---|
23 | RenderSPU render_spu;
|
---|
24 | unsigned int render_spu_parent_window_id;
|
---|
25 |
|
---|
26 | #ifdef CHROMIUM_THREADSAFE
|
---|
27 | CRtsd _RenderTSD;
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | static void swapsyncConnect(void)
|
---|
31 | {
|
---|
32 | char hostname[4096], protocol[4096];
|
---|
33 | unsigned short port;
|
---|
34 |
|
---|
35 | crNetInit(NULL, NULL);
|
---|
36 |
|
---|
37 | if (!crParseURL( render_spu.swap_master_url, protocol, hostname,
|
---|
38 | &port, 9876))
|
---|
39 | crError( "Bad URL: %s", render_spu.swap_master_url );
|
---|
40 |
|
---|
41 | if (render_spu.is_swap_master)
|
---|
42 | {
|
---|
43 | int a;
|
---|
44 |
|
---|
45 | render_spu.swap_conns = (CRConnection **)crAlloc(
|
---|
46 | render_spu.num_swap_clients*sizeof(CRConnection *));
|
---|
47 | for (a=0; a<render_spu.num_swap_clients; a++)
|
---|
48 | {
|
---|
49 | render_spu.swap_conns[a] = crNetAcceptClient( protocol, hostname, port,
|
---|
50 | render_spu.swap_mtu, 1);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | else
|
---|
54 | {
|
---|
55 | render_spu.swap_conns = (CRConnection **)crAlloc(sizeof(CRConnection *));
|
---|
56 |
|
---|
57 | render_spu.swap_conns[0] = crNetConnectToServer(render_spu.swap_master_url,
|
---|
58 | port, render_spu.swap_mtu, 1);
|
---|
59 | if (!render_spu.swap_conns[0])
|
---|
60 | crError("Failed connection");
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | #ifdef RT_OS_WINDOWS
|
---|
65 | static DWORD WINAPI renderSPUWindowThreadProc(void* unused)
|
---|
66 | {
|
---|
67 | MSG msg;
|
---|
68 | bool bRet;
|
---|
69 |
|
---|
70 | (void) unused;
|
---|
71 |
|
---|
72 | /* Force system to create the message queue.
|
---|
73 | * Else, there's a chance that render spu will issue PostThreadMessage
|
---|
74 | * before this thread calls GetMessage for first time.
|
---|
75 | */
|
---|
76 | PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
77 |
|
---|
78 | crDebug("RenderSPU: Window thread started (%x)", crThreadID());
|
---|
79 | SetEvent(render_spu.hWinThreadReadyEvent);
|
---|
80 |
|
---|
81 | while( (bRet = GetMessage( &msg, 0, 0, 0 )) != 0)
|
---|
82 | {
|
---|
83 | if (bRet == -1)
|
---|
84 | {
|
---|
85 | crError("RenderSPU: Window thread GetMessage failed (%x)", GetLastError());
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | if (msg.message == WM_VBOX_RENDERSPU_CREATE_WINDOW)
|
---|
91 | {
|
---|
92 | LPCREATESTRUCT pCS = (LPCREATESTRUCT) msg.lParam;
|
---|
93 | HWND *phWnd;
|
---|
94 |
|
---|
95 | CRASSERT(msg.lParam && !msg.wParam && pCS->lpCreateParams);
|
---|
96 |
|
---|
97 | phWnd = pCS->lpCreateParams;
|
---|
98 |
|
---|
99 | *phWnd = CreateWindowEx(pCS->dwExStyle, pCS->lpszName, pCS->lpszClass, pCS->style,
|
---|
100 | pCS->x, pCS->y, pCS->cx, pCS->cy,
|
---|
101 | pCS->hwndParent, pCS->hMenu, pCS->hInstance, &render_spu);
|
---|
102 |
|
---|
103 | SetEvent(render_spu.hWinThreadReadyEvent);
|
---|
104 | }
|
---|
105 | else if (msg.message == WM_VBOX_RENDERSPU_DESTROY_WINDOW)
|
---|
106 | {
|
---|
107 | CRASSERT(msg.lParam && !msg.wParam);
|
---|
108 |
|
---|
109 | DestroyWindow(((VBOX_RENDERSPU_DESTROY_WINDOW*) msg.lParam)->hWnd);
|
---|
110 |
|
---|
111 | SetEvent(render_spu.hWinThreadReadyEvent);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | TranslateMessage(&msg);
|
---|
116 | DispatchMessage(&msg);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | render_spu.dwWinThreadId = 0;
|
---|
122 |
|
---|
123 | crDebug("RenderSPU: Window thread stopped (%x)", crThreadID());
|
---|
124 | SetEvent(render_spu.hWinThreadReadyEvent);
|
---|
125 |
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | static SPUFunctions *
|
---|
131 | renderSPUInit( int id, SPU *child, SPU *self,
|
---|
132 | unsigned int context_id, unsigned int num_contexts )
|
---|
133 | {
|
---|
134 | int numFuncs, numSpecial;
|
---|
135 | GLint defaultWin, defaultCtx;
|
---|
136 | WindowInfo *windowInfo;
|
---|
137 |
|
---|
138 | (void) child;
|
---|
139 | (void) context_id;
|
---|
140 | (void) num_contexts;
|
---|
141 |
|
---|
142 | self->privatePtr = (void *) &render_spu;
|
---|
143 |
|
---|
144 | #ifdef CHROMIUM_THREADSAFE
|
---|
145 | crDebug("Render SPU: thread-safe");
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | crMemZero(&render_spu, sizeof(render_spu));
|
---|
149 |
|
---|
150 | render_spu.id = id;
|
---|
151 | renderspuSetVBoxConfiguration(&render_spu);
|
---|
152 |
|
---|
153 | if (render_spu.swap_master_url)
|
---|
154 | swapsyncConnect();
|
---|
155 |
|
---|
156 |
|
---|
157 | /* Get our special functions. */
|
---|
158 | numSpecial = renderspuCreateFunctions( _cr_render_table );
|
---|
159 |
|
---|
160 | #ifdef RT_OS_WINDOWS
|
---|
161 | /* Start thread to create windows and process window messages */
|
---|
162 | crDebug("RenderSPU: Starting windows serving thread");
|
---|
163 | render_spu.hWinThreadReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
164 | if (!render_spu.hWinThreadReadyEvent)
|
---|
165 | {
|
---|
166 | crError("RenderSPU: Failed to create WinThreadReadyEvent! (%x)", GetLastError());
|
---|
167 | return NULL;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (!CreateThread(NULL, 0, renderSPUWindowThreadProc, 0, 0, &render_spu.dwWinThreadId))
|
---|
171 | {
|
---|
172 | crError("RenderSPU: Failed to start windows thread! (%x)", GetLastError());
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 | WaitForSingleObject(render_spu.hWinThreadReadyEvent, INFINITE);
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /* Get the OpenGL functions. */
|
---|
179 | numFuncs = crLoadOpenGL( &render_spu.ws, _cr_render_table + numSpecial );
|
---|
180 | if (numFuncs == 0) {
|
---|
181 | crError("The render SPU was unable to load the native OpenGL library");
|
---|
182 | return NULL;
|
---|
183 | }
|
---|
184 |
|
---|
185 | numFuncs += numSpecial;
|
---|
186 |
|
---|
187 | #ifdef GLX
|
---|
188 | if (!render_spu.use_glxchoosevisual) {
|
---|
189 | /* sometimes want to set this option with ATI drivers */
|
---|
190 | render_spu.ws.glXChooseVisual = NULL;
|
---|
191 | }
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | render_spu.window_id = 0;
|
---|
195 | render_spu.context_id = 0;
|
---|
196 | render_spu.contextTable = crAllocHashtable();
|
---|
197 | render_spu.windowTable = crAllocHashtable();
|
---|
198 |
|
---|
199 | CRASSERT(render_spu.default_visual & CR_RGB_BIT);
|
---|
200 |
|
---|
201 | #ifdef USE_OSMESA
|
---|
202 | if (render_spu.use_osmesa) {
|
---|
203 | if (!crLoadOSMesa(&render_spu.OSMesaCreateContext,
|
---|
204 | &render_spu.OSMesaMakeCurrent,
|
---|
205 | &render_spu.OSMesaDestroyContext)) {
|
---|
206 | crError("Unable to load OSMesa library");
|
---|
207 | }
|
---|
208 | }
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | #ifdef DARWIN
|
---|
212 | render_spu.hRootVisibleRegion = 0;
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Create the default window and context. Their indexes are zero and
|
---|
217 | * a client can use them without calling CreateContext or WindowCreate.
|
---|
218 | */
|
---|
219 | crDebug("Render SPU: Creating default window (visBits=0x%x, id=0)",
|
---|
220 | render_spu.default_visual);
|
---|
221 | defaultWin = renderspuWindowCreate( NULL, render_spu.default_visual );
|
---|
222 | if (defaultWin != 0) {
|
---|
223 | crError("Render SPU: Couldn't get a double-buffered, RGB visual with Z!");
|
---|
224 | return NULL;
|
---|
225 | }
|
---|
226 | crDebug( "Render SPU: WindowCreate returned %d (0=normal)", defaultWin );
|
---|
227 |
|
---|
228 | crDebug("Render SPU: Creating default context, visBits=0x%x",
|
---|
229 | render_spu.default_visual );
|
---|
230 | defaultCtx = renderspuCreateContext( NULL, render_spu.default_visual, 0 );
|
---|
231 | CRASSERT(defaultCtx == 0);
|
---|
232 |
|
---|
233 | renderspuMakeCurrent( defaultWin, 0, defaultCtx );
|
---|
234 |
|
---|
235 | /* Get windowInfo for the default window */
|
---|
236 | windowInfo = (WindowInfo *) crHashtableSearch(render_spu.windowTable, 0);
|
---|
237 | CRASSERT(windowInfo);
|
---|
238 | windowInfo->mapPending = GL_TRUE;
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Get the OpenGL extension functions.
|
---|
242 | * SIGH -- we have to wait until the very bitter end to load the
|
---|
243 | * extensions, because the context has to be bound before
|
---|
244 | * wglGetProcAddress will work correctly. No such issue with GLX though.
|
---|
245 | */
|
---|
246 | numFuncs += crLoadOpenGLExtensions( &render_spu.ws, _cr_render_table + numFuncs );
|
---|
247 | CRASSERT(numFuncs < 1000);
|
---|
248 |
|
---|
249 | #ifdef WINDOWS
|
---|
250 | /*
|
---|
251 | * Same problem as above, these are extensions so we need to
|
---|
252 | * load them after a context has been bound. As they're WGL
|
---|
253 | * extensions too, we can't simply tag them into the spu_loader.
|
---|
254 | * So we do them here for now.
|
---|
255 | * Grrr, NVIDIA driver uses EXT for GetExtensionsStringEXT,
|
---|
256 | * but ARB for others. Need furthur testing here....
|
---|
257 | */
|
---|
258 | render_spu.ws.wglGetExtensionsStringEXT =
|
---|
259 | (wglGetExtensionsStringEXTFunc_t)
|
---|
260 | render_spu.ws.wglGetProcAddress( "wglGetExtensionsStringEXT" );
|
---|
261 | render_spu.ws.wglChoosePixelFormatEXT =
|
---|
262 | (wglChoosePixelFormatEXTFunc_t)
|
---|
263 | render_spu.ws.wglGetProcAddress( "wglChoosePixelFormatARB" );
|
---|
264 | render_spu.ws.wglGetPixelFormatAttribivEXT =
|
---|
265 | (wglGetPixelFormatAttribivEXTFunc_t)
|
---|
266 | render_spu.ws.wglGetProcAddress( "wglGetPixelFormatAttribivARB" );
|
---|
267 | render_spu.ws.wglGetPixelFormatAttribfvEXT =
|
---|
268 | (wglGetPixelFormatAttribfvEXTFunc_t)
|
---|
269 | render_spu.ws.wglGetProcAddress( "wglGetPixelFormatAttribfvARB" );
|
---|
270 |
|
---|
271 | if (render_spu.ws.wglGetProcAddress("glTexImage3D"))
|
---|
272 | {
|
---|
273 | _cr_render_table[numFuncs].name = crStrdup("TexImage3D");
|
---|
274 | _cr_render_table[numFuncs].fn = (SPUGenericFunction) render_spu.ws.wglGetProcAddress("glTexImage3D");
|
---|
275 | ++numFuncs;
|
---|
276 | crDebug("Render SPU: Found glTexImage3D function");
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (render_spu.ws.wglGetExtensionsStringEXT) {
|
---|
280 | crDebug("WGL - found wglGetExtensionsStringEXT\n");
|
---|
281 | }
|
---|
282 | if (render_spu.ws.wglChoosePixelFormatEXT) {
|
---|
283 | crDebug("WGL - found wglChoosePixelFormatEXT\n");
|
---|
284 | }
|
---|
285 | #endif
|
---|
286 |
|
---|
287 | render_spu.barrierHash = crAllocHashtable();
|
---|
288 |
|
---|
289 | render_spu.cursorX = 0;
|
---|
290 | render_spu.cursorY = 0;
|
---|
291 | render_spu.use_L2 = 0;
|
---|
292 |
|
---|
293 | render_spu.gather_conns = NULL;
|
---|
294 |
|
---|
295 | crDebug("Render SPU: ---------- End of Init -------------");
|
---|
296 | return &render_functions;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | static void renderSPUSelfDispatch(SPUDispatchTable *self)
|
---|
301 | {
|
---|
302 | crSPUInitDispatchTable( &(render_spu.self) );
|
---|
303 | crSPUCopyDispatchTable( &(render_spu.self), self );
|
---|
304 |
|
---|
305 | render_spu.server = (CRServer *)(self->server);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | static void DeleteContextCallback( void *data )
|
---|
310 | {
|
---|
311 | ContextInfo *context = (ContextInfo *) data;
|
---|
312 | renderspu_SystemDestroyContext(context);
|
---|
313 | crFree(context);
|
---|
314 | }
|
---|
315 |
|
---|
316 | static void DeleteWindowCallback( void *data )
|
---|
317 | {
|
---|
318 | WindowInfo *window = (WindowInfo *) data;
|
---|
319 | renderspu_SystemDestroyWindow(window);
|
---|
320 | crFree(window);
|
---|
321 | }
|
---|
322 |
|
---|
323 | static int renderSPUCleanup(void)
|
---|
324 | {
|
---|
325 | crFreeHashtable(render_spu.contextTable, DeleteContextCallback);
|
---|
326 | render_spu.contextTable = NULL;
|
---|
327 | crFreeHashtable(render_spu.windowTable, DeleteWindowCallback);
|
---|
328 | render_spu.windowTable = NULL;
|
---|
329 | crFreeHashtable(render_spu.barrierHash, crFree);
|
---|
330 | render_spu.barrierHash = NULL;
|
---|
331 |
|
---|
332 | #ifdef RT_OS_DARWIN
|
---|
333 | if (render_spu.hRootVisibleRegion)
|
---|
334 | {
|
---|
335 | DisposeRgn(render_spu.hRootVisibleRegion);
|
---|
336 | render_spu.hRootVisibleRegion = 0;
|
---|
337 | }
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | #ifdef RT_OS_WINDOWS
|
---|
341 | if (render_spu.dwWinThreadId)
|
---|
342 | {
|
---|
343 | PostThreadMessage(render_spu.dwWinThreadId, WM_QUIT, 0, 0);
|
---|
344 | WaitForSingleObject(render_spu.hWinThreadReadyEvent, INFINITE);
|
---|
345 | }
|
---|
346 | CloseHandle(render_spu.hWinThreadReadyEvent);
|
---|
347 | render_spu.hWinThreadReadyEvent = NULL;
|
---|
348 | #endif
|
---|
349 |
|
---|
350 | return 1;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | extern SPUOptions renderSPUOptions[];
|
---|
355 |
|
---|
356 | int SPULoad( char **name, char **super, SPUInitFuncPtr *init,
|
---|
357 | SPUSelfDispatchFuncPtr *self, SPUCleanupFuncPtr *cleanup,
|
---|
358 | SPUOptionsPtr *options, int *flags )
|
---|
359 | {
|
---|
360 | *name = "render";
|
---|
361 | *super = NULL;
|
---|
362 | *init = renderSPUInit;
|
---|
363 | *self = renderSPUSelfDispatch;
|
---|
364 | *cleanup = renderSPUCleanup;
|
---|
365 | *options = renderSPUOptions;
|
---|
366 | *flags = (SPU_NO_PACKER|SPU_IS_TERMINAL|SPU_MAX_SERVERS_ZERO);
|
---|
367 |
|
---|
368 | return 1;
|
---|
369 | }
|
---|
370 |
|
---|
371 | DECLEXPORT(void) renderspuSetWindowId(unsigned int winId)
|
---|
372 | {
|
---|
373 | render_spu_parent_window_id = winId;
|
---|
374 | }
|
---|
375 |
|
---|
376 | static void renderspuWindowVisibleRegionCB(unsigned long key, void *data1, void *data2)
|
---|
377 | {
|
---|
378 | WindowInfo *window = (WindowInfo *) data1;
|
---|
379 | CRASSERT(window);
|
---|
380 |
|
---|
381 | renderspu_SystemWindowApplyVisibleRegion(window);
|
---|
382 | }
|
---|
383 |
|
---|
384 | DECLEXPORT(void) renderspuSetRootVisibleRegion(GLint cRects, GLint *pRects)
|
---|
385 | {
|
---|
386 | #ifdef RT_OS_DARWIN
|
---|
387 | renderspu_SystemSetRootVisibleRegion(cRects, pRects);
|
---|
388 |
|
---|
389 | crHashtableWalk(render_spu.windowTable, renderspuWindowVisibleRegionCB, NULL);
|
---|
390 | #endif
|
---|
391 | }
|
---|
392 |
|
---|
393 | #ifndef RT_OS_DARWIN
|
---|
394 | void renderspu_SystemWindowApplyVisibleRegion(WindowInfo *window)
|
---|
395 | {
|
---|
396 | }
|
---|
397 | #endif
|
---|