VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_init.c@ 45132

Last change on this file since 45132 was 45132, checked in by vboxsync, 12 years ago

crOpenGL: seamles mode support impl; bugfizes & cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.3 KB
Line 
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 "cr_environment.h"
13#include "renderspu.h"
14#include <stdio.h>
15
16#ifdef RT_OS_DARWIN
17# include <iprt/semaphore.h>
18#endif /* RT_OS_DARWIN */
19
20static SPUNamedFunctionTable _cr_render_table[1000];
21
22SPUFunctions render_functions = {
23 NULL, /* CHILD COPY */
24 NULL, /* DATA */
25 _cr_render_table /* THE ACTUAL FUNCTIONS */
26};
27
28RenderSPU render_spu;
29uint64_t render_spu_parent_window_id = 0;
30
31#ifdef CHROMIUM_THREADSAFE
32CRtsd _RenderTSD;
33#endif
34
35static void swapsyncConnect(void)
36{
37 char hostname[4096], protocol[4096];
38 unsigned short port;
39
40 crNetInit(NULL, NULL);
41
42 if (!crParseURL( render_spu.swap_master_url, protocol, hostname,
43 &port, 9876))
44 crError( "Bad URL: %s", render_spu.swap_master_url );
45
46 if (render_spu.is_swap_master)
47 {
48 int a;
49
50 render_spu.swap_conns = (CRConnection **)crAlloc(
51 render_spu.num_swap_clients*sizeof(CRConnection *));
52 for (a=0; a<render_spu.num_swap_clients; a++)
53 {
54 render_spu.swap_conns[a] = crNetAcceptClient( protocol, hostname, port,
55 render_spu.swap_mtu, 1);
56 }
57 }
58 else
59 {
60 render_spu.swap_conns = (CRConnection **)crAlloc(sizeof(CRConnection *));
61
62 render_spu.swap_conns[0] = crNetConnectToServer(render_spu.swap_master_url,
63 port, render_spu.swap_mtu, 1);
64 if (!render_spu.swap_conns[0])
65 crError("Failed connection");
66 }
67}
68
69#ifdef RT_OS_WINDOWS
70static DWORD WINAPI renderSPUWindowThreadProc(void* unused)
71{
72 MSG msg;
73 bool bRet;
74
75 (void) unused;
76
77 /* Force system to create the message queue.
78 * Else, there's a chance that render spu will issue PostThreadMessage
79 * before this thread calls GetMessage for first time.
80 */
81 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
82
83 crDebug("RenderSPU: Window thread started (%x)", crThreadID());
84 SetEvent(render_spu.hWinThreadReadyEvent);
85
86 while( (bRet = GetMessage( &msg, 0, 0, 0 )) != 0)
87 {
88 if (bRet == -1)
89 {
90 crError("RenderSPU: Window thread GetMessage failed (%x)", GetLastError());
91 break;
92 }
93 else
94 {
95 if (msg.message == WM_VBOX_RENDERSPU_CREATE_WINDOW)
96 {
97 LPCREATESTRUCT pCS = (LPCREATESTRUCT) msg.lParam;
98 HWND hWnd;
99 WindowInfo *pWindow = (WindowInfo *)pCS->lpCreateParams;
100
101 CRASSERT(msg.lParam && !msg.wParam && pCS->lpCreateParams);
102
103 hWnd = CreateWindowEx(pCS->dwExStyle, pCS->lpszName, pCS->lpszClass, pCS->style,
104 pCS->x, pCS->y, pCS->cx, pCS->cy,
105 pCS->hwndParent, pCS->hMenu, pCS->hInstance, &render_spu);
106
107 pWindow->hWnd = hWnd;
108
109 SetEvent(render_spu.hWinThreadReadyEvent);
110 }
111 else if (msg.message == WM_VBOX_RENDERSPU_DESTROY_WINDOW)
112 {
113 CRASSERT(msg.lParam && !msg.wParam);
114
115 DestroyWindow(((VBOX_RENDERSPU_DESTROY_WINDOW*) msg.lParam)->hWnd);
116
117 SetEvent(render_spu.hWinThreadReadyEvent);
118 }
119 else
120 {
121 TranslateMessage(&msg);
122 DispatchMessage(&msg);
123 }
124 }
125 }
126
127 render_spu.dwWinThreadId = 0;
128
129 crDebug("RenderSPU: Window thread stopped (%x)", crThreadID());
130 SetEvent(render_spu.hWinThreadReadyEvent);
131
132 return 0;
133}
134#endif
135
136static SPUFunctions *
137renderSPUInit( int id, SPU *child, SPU *self,
138 unsigned int context_id, unsigned int num_contexts )
139{
140 int numFuncs, numSpecial;
141 GLint defaultWin, defaultCtx;
142 WindowInfo *windowInfo;
143 const char * pcpwSetting;
144
145 (void) child;
146 (void) context_id;
147 (void) num_contexts;
148
149 self->privatePtr = (void *) &render_spu;
150
151#ifdef CHROMIUM_THREADSAFE
152 crDebug("Render SPU: thread-safe");
153#endif
154
155 crMemZero(&render_spu, sizeof(render_spu));
156
157 render_spu.id = id;
158 renderspuSetVBoxConfiguration(&render_spu);
159
160 if (render_spu.swap_master_url)
161 swapsyncConnect();
162
163
164 /* Get our special functions. */
165 numSpecial = renderspuCreateFunctions( _cr_render_table );
166
167#ifdef RT_OS_WINDOWS
168 /* Start thread to create windows and process window messages */
169 crDebug("RenderSPU: Starting windows serving thread");
170 render_spu.hWinThreadReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
171 if (!render_spu.hWinThreadReadyEvent)
172 {
173 crError("RenderSPU: Failed to create WinThreadReadyEvent! (%x)", GetLastError());
174 return NULL;
175 }
176
177 if (!CreateThread(NULL, 0, renderSPUWindowThreadProc, 0, 0, &render_spu.dwWinThreadId))
178 {
179 crError("RenderSPU: Failed to start windows thread! (%x)", GetLastError());
180 return NULL;
181 }
182 WaitForSingleObject(render_spu.hWinThreadReadyEvent, INFINITE);
183#endif
184
185 /* Get the OpenGL functions. */
186 numFuncs = crLoadOpenGL( &render_spu.ws, _cr_render_table + numSpecial );
187 if (numFuncs == 0) {
188 crError("The render SPU was unable to load the native OpenGL library");
189 return NULL;
190 }
191
192 numFuncs += numSpecial;
193
194 render_spu.contextTable = crAllocHashtableEx(1, INT32_MAX);
195 render_spu.windowTable = crAllocHashtableEx(1, INT32_MAX);
196
197 pcpwSetting = crGetenv("CR_RENDER_ENABLE_SINGLE_PRESENT_CONTEXT");
198 if (pcpwSetting)
199 {
200 if (pcpwSetting[0] == '0')
201 pcpwSetting = NULL;
202 }
203
204 if (pcpwSetting)
205 {
206 /* TODO: need proper blitter synchronization, do not use so far!
207 * the problem is that rendering can be done in multiple thread: the main command (hgcm) thread and the redraw thread
208 * we currently use per-window synchronization, while we'll need a per-blitter synchronization if one blitter is used for multiple windows
209 * this is not done currently */
210 crWarning("TODO: need proper blitter synchronization, do not use so far!");
211 render_spu.blitterTable = crAllocHashtable();
212 CRASSERT(render_spu.blitterTable);
213 }
214 else
215 render_spu.blitterTable = NULL;
216
217 CRASSERT(render_spu.default_visual & CR_RGB_BIT);
218
219#ifdef GLX
220 {
221 int rc = renderspu_SystemInit();
222 if (!RT_SUCCESS(rc))
223 {
224 crError("renderspu_SystemInit failed rc %d", rc);
225 return NULL;
226 }
227 }
228#endif
229
230#ifdef USE_OSMESA
231 if (render_spu.use_osmesa) {
232 if (!crLoadOSMesa(&render_spu.OSMesaCreateContext,
233 &render_spu.OSMesaMakeCurrent,
234 &render_spu.OSMesaDestroyContext)) {
235 crError("Unable to load OSMesa library");
236 }
237 }
238#endif
239
240#ifdef DARWIN
241# ifdef VBOX_WITH_COCOA_QT
242# else /* VBOX_WITH_COCOA_QT */
243 render_spu.hRootVisibleRegion = 0;
244 render_spu.currentBufferName = 1;
245 render_spu.uiDockUpdateTS = 0;
246 /* Create a mutex for synchronizing events from the main Qt thread & this
247 thread */
248 RTSemFastMutexCreate(&render_spu.syncMutex);
249 /* Create our window groups */
250 CreateWindowGroup(kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrSharedActivation | kWindowGroupAttrHideOnCollapse | kWindowGroupAttrFixedLevel, &render_spu.pMasterGroup);
251 CreateWindowGroup(kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrSharedActivation | kWindowGroupAttrHideOnCollapse | kWindowGroupAttrFixedLevel, &render_spu.pParentGroup);
252 /* Make the correct z-layering */
253 SendWindowGroupBehind (render_spu.pParentGroup, render_spu.pMasterGroup);
254 /* and set the gParentGroup as parent for gMasterGroup. */
255 SetWindowGroupParent (render_spu.pMasterGroup, render_spu.pParentGroup);
256 /* Install the event handlers */
257 EventTypeSpec eventList[] =
258 {
259 {kEventClassVBox, kEventVBoxUpdateContext}, /* Update the context after show/size/move events */
260 {kEventClassVBox, kEventVBoxBoundsChanged} /* Clip/Pos the OpenGL windows when the main window is changed in pos/size */
261 };
262 /* We need to process events from our main window */
263 render_spu.hParentEventHandler = NewEventHandlerUPP(windowEvtHndlr);
264 InstallApplicationEventHandler (render_spu.hParentEventHandler,
265 GetEventTypeCount(eventList), eventList,
266 NULL, NULL);
267 render_spu.fInit = true;
268# endif /* VBOX_WITH_COCOA_QT */
269#endif /* DARWIN */
270
271 /*
272 * Create the default window and context. Their indexes are zero and
273 * a client can use them without calling CreateContext or WindowCreate.
274 */
275 crDebug("Render SPU: Creating default window (visBits=0x%x, id=0)",
276 render_spu.default_visual);
277 defaultWin = renderspuWindowCreateEx( NULL, render_spu.default_visual, CR_RENDER_DEFAULT_WINDOW_ID );
278 if (defaultWin != CR_RENDER_DEFAULT_WINDOW_ID) {
279 crError("Render SPU: Couldn't get a double-buffered, RGB visual with Z!");
280 return NULL;
281 }
282 crDebug( "Render SPU: WindowCreate returned %d (0=normal)", defaultWin );
283
284 crDebug("Render SPU: Creating default context, visBits=0x%x",
285 render_spu.default_visual );
286 defaultCtx = renderspuCreateContextEx( NULL, render_spu.default_visual, CR_RENDER_DEFAULT_CONTEXT_ID, 0 );
287 if (defaultCtx != CR_RENDER_DEFAULT_CONTEXT_ID) {
288 crError("Render SPU: failed to create default context!");
289 return NULL;
290 }
291
292 renderspuMakeCurrent( defaultWin, 0, defaultCtx );
293
294 /* Get windowInfo for the default window */
295 windowInfo = (WindowInfo *) crHashtableSearch(render_spu.windowTable, CR_RENDER_DEFAULT_WINDOW_ID);
296 CRASSERT(windowInfo);
297 windowInfo->mapPending = GL_TRUE;
298
299 /*
300 * Get the OpenGL extension functions.
301 * SIGH -- we have to wait until the very bitter end to load the
302 * extensions, because the context has to be bound before
303 * wglGetProcAddress will work correctly. No such issue with GLX though.
304 */
305 numFuncs += crLoadOpenGLExtensions( &render_spu.ws, _cr_render_table + numFuncs );
306 CRASSERT(numFuncs < 1000);
307
308#ifdef WINDOWS
309 /*
310 * Same problem as above, these are extensions so we need to
311 * load them after a context has been bound. As they're WGL
312 * extensions too, we can't simply tag them into the spu_loader.
313 * So we do them here for now.
314 * Grrr, NVIDIA driver uses EXT for GetExtensionsStringEXT,
315 * but ARB for others. Need further testing here....
316 */
317 render_spu.ws.wglGetExtensionsStringEXT =
318 (wglGetExtensionsStringEXTFunc_t)
319 render_spu.ws.wglGetProcAddress( "wglGetExtensionsStringEXT" );
320 render_spu.ws.wglChoosePixelFormatEXT =
321 (wglChoosePixelFormatEXTFunc_t)
322 render_spu.ws.wglGetProcAddress( "wglChoosePixelFormatARB" );
323 render_spu.ws.wglGetPixelFormatAttribivEXT =
324 (wglGetPixelFormatAttribivEXTFunc_t)
325 render_spu.ws.wglGetProcAddress( "wglGetPixelFormatAttribivARB" );
326 render_spu.ws.wglGetPixelFormatAttribfvEXT =
327 (wglGetPixelFormatAttribfvEXTFunc_t)
328 render_spu.ws.wglGetProcAddress( "wglGetPixelFormatAttribfvARB" );
329
330 if (render_spu.ws.wglGetProcAddress("glCopyTexSubImage3D"))
331 {
332 _cr_render_table[numFuncs].name = crStrdup("CopyTexSubImage3D");
333 _cr_render_table[numFuncs].fn = (SPUGenericFunction) render_spu.ws.wglGetProcAddress("glCopyTexSubImage3D");
334 ++numFuncs;
335 crDebug("Render SPU: Found glCopyTexSubImage3D function");
336 }
337
338 if (render_spu.ws.wglGetProcAddress("glDrawRangeElements"))
339 {
340 _cr_render_table[numFuncs].name = crStrdup("DrawRangeElements");
341 _cr_render_table[numFuncs].fn = (SPUGenericFunction) render_spu.ws.wglGetProcAddress("glDrawRangeElements");
342 ++numFuncs;
343 crDebug("Render SPU: Found glDrawRangeElements function");
344 }
345
346 if (render_spu.ws.wglGetProcAddress("glTexSubImage3D"))
347 {
348 _cr_render_table[numFuncs].name = crStrdup("TexSubImage3D");
349 _cr_render_table[numFuncs].fn = (SPUGenericFunction) render_spu.ws.wglGetProcAddress("glTexSubImage3D");
350 ++numFuncs;
351 crDebug("Render SPU: Found glTexSubImage3D function");
352 }
353
354 if (render_spu.ws.wglGetProcAddress("glTexImage3D"))
355 {
356 _cr_render_table[numFuncs].name = crStrdup("TexImage3D");
357 _cr_render_table[numFuncs].fn = (SPUGenericFunction) render_spu.ws.wglGetProcAddress("glTexImage3D");
358 ++numFuncs;
359 crDebug("Render SPU: Found glTexImage3D function");
360 }
361
362 if (render_spu.ws.wglGetExtensionsStringEXT) {
363 crDebug("WGL - found wglGetExtensionsStringEXT\n");
364 }
365 if (render_spu.ws.wglChoosePixelFormatEXT) {
366 crDebug("WGL - found wglChoosePixelFormatEXT\n");
367 }
368#endif
369
370 render_spu.barrierHash = crAllocHashtable();
371
372 render_spu.cursorX = 0;
373 render_spu.cursorY = 0;
374 render_spu.use_L2 = 0;
375
376 render_spu.gather_conns = NULL;
377
378 crDebug("Render SPU: ---------- End of Init -------------");
379
380 return &render_functions;
381}
382
383static void renderSPUSelfDispatch(SPUDispatchTable *self)
384{
385 crSPUInitDispatchTable( &(render_spu.self) );
386 crSPUCopyDispatchTable( &(render_spu.self), self );
387
388 render_spu.blitterDispatch = &(render_spu.self);
389
390 render_spu.server = (CRServer *)(self->server);
391
392 {
393 GLfloat version;
394 version = crStrToFloat((const char *) render_spu.ws.glGetString(GL_VERSION));
395
396 if (version>=2.f || crStrstr((const char*)render_spu.ws.glGetString(GL_EXTENSIONS), "GL_ARB_vertex_shader"))
397 {
398 GLint mu=0;
399 render_spu.self.GetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, &mu);
400 crInfo("Render SPU: GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB=%i", mu);
401 }
402 }
403}
404
405
406static void DeleteContextCallback( void *data )
407{
408 ContextInfo *context = (ContextInfo *) data;
409 renderspuContextMarkDeletedAndRelease(context);
410}
411
412static void DeleteWindowCallback( void *data )
413{
414 WindowInfo *window = (WindowInfo *) data;
415 renderspu_SystemDestroyWindow(window);
416 crFree(window);
417}
418
419static void DeleteBlitterCallback( void *data )
420{
421 PCR_BLITTER pBlitter = (PCR_BLITTER) data;
422 CrBltTerm(pBlitter);
423 crFree(pBlitter);
424}
425
426static void renderspuBlitterCleanupCB(unsigned long key, void *data1, void *data2)
427{
428 WindowInfo *window = (WindowInfo *) data1;
429 CRASSERT(window);
430
431 renderspuVBoxPresentBlitterCleanup( window );
432}
433
434static int renderSPUCleanup(void)
435{
436 renderspuVBoxCompositorClearAll();
437
438 if (render_spu.blitterTable)
439 {
440 crFreeHashtable(render_spu.blitterTable, DeleteBlitterCallback);
441 render_spu.blitterTable = NULL;
442 }
443 else
444 {
445 crHashtableWalk(render_spu.windowTable, renderspuBlitterCleanupCB, NULL);
446 }
447
448 crFreeHashtable(render_spu.contextTable, DeleteContextCallback);
449 render_spu.contextTable = NULL;
450 crFreeHashtable(render_spu.windowTable, DeleteWindowCallback);
451 render_spu.windowTable = NULL;
452 crFreeHashtable(render_spu.barrierHash, crFree);
453 render_spu.barrierHash = NULL;
454
455#ifdef RT_OS_DARWIN
456# ifndef VBOX_WITH_COCOA_QT
457 render_spu.fInit = false;
458 DisposeEventHandlerUPP(render_spu.hParentEventHandler);
459 ReleaseWindowGroup(render_spu.pMasterGroup);
460 ReleaseWindowGroup(render_spu.pParentGroup);
461 if (render_spu.hRootVisibleRegion)
462 {
463 DisposeRgn(render_spu.hRootVisibleRegion);
464 render_spu.hRootVisibleRegion = 0;
465 }
466 render_spu.currentBufferName = 1;
467 render_spu.uiDockUpdateTS = 0;
468 RTSemFastMutexDestroy(render_spu.syncMutex);
469# else /* VBOX_WITH_COCOA_QT */
470# endif /* VBOX_WITH_COCOA_QT */
471#endif /* RT_OS_DARWIN */
472
473#ifdef RT_OS_WINDOWS
474 if (render_spu.dwWinThreadId)
475 {
476 HANDLE hNative;
477
478 hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
479 false, render_spu.dwWinThreadId);
480 if (!hNative)
481 {
482 crWarning("Failed to get handle for window thread(%#x)", GetLastError());
483 }
484
485 if (PostThreadMessage(render_spu.dwWinThreadId, WM_QUIT, 0, 0))
486 {
487 WaitForSingleObject(render_spu.hWinThreadReadyEvent, INFINITE);
488
489 /*wait for os thread to actually finish*/
490 if (hNative && WaitForSingleObject(hNative, 3000)==WAIT_TIMEOUT)
491 {
492 crDebug("Wait failed, terminating");
493 if (!TerminateThread(hNative, 1))
494 {
495 crWarning("TerminateThread failed");
496 }
497 }
498 }
499
500 if (hNative)
501 {
502 CloseHandle(hNative);
503 }
504 }
505 CloseHandle(render_spu.hWinThreadReadyEvent);
506 render_spu.hWinThreadReadyEvent = NULL;
507#endif
508
509 crUnloadOpenGL();
510
511#ifdef CHROMIUM_THREADSAFE
512 crFreeTSD(&_RenderTSD);
513#endif
514
515 return 1;
516}
517
518
519extern SPUOptions renderSPUOptions[];
520
521int SPULoad( char **name, char **super, SPUInitFuncPtr *init,
522 SPUSelfDispatchFuncPtr *self, SPUCleanupFuncPtr *cleanup,
523 SPUOptionsPtr *options, int *flags )
524{
525 *name = "render";
526 *super = NULL;
527 *init = renderSPUInit;
528 *self = renderSPUSelfDispatch;
529 *cleanup = renderSPUCleanup;
530 *options = renderSPUOptions;
531 *flags = (SPU_NO_PACKER|SPU_IS_TERMINAL|SPU_MAX_SERVERS_ZERO);
532
533 return 1;
534}
535
536DECLEXPORT(void) renderspuSetWindowId(uint64_t winId)
537{
538 render_spu_parent_window_id = winId;
539}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette