VirtualBox

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

Last change on this file since 78251 was 78251, checked in by vboxsync, 6 years ago

HostServices/SharedOpenGL: Remove unused code enclosed by #ifdef USE_OSMESA

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