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 | /**
|
---|
8 | * \mainpage OpenGL_stub
|
---|
9 | *
|
---|
10 | * \section OpenGL_stubIntroduction Introduction
|
---|
11 | *
|
---|
12 | * Chromium consists of all the top-level files in the cr
|
---|
13 | * directory. The OpenGL_stub module basically takes care of API dispatch,
|
---|
14 | * and OpenGL state management.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This file manages OpenGL rendering contexts in the faker library.
|
---|
20 | * The big issue is switching between Chromium and native GL context
|
---|
21 | * management. This is where we support multiple client OpenGL
|
---|
22 | * windows. Typically, one window is handled by Chromium while any
|
---|
23 | * other windows are handled by the native OpenGL library.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "chromium.h"
|
---|
27 | #include "cr_error.h"
|
---|
28 | #include "cr_spu.h"
|
---|
29 | #include "cr_mem.h"
|
---|
30 | #include "cr_string.h"
|
---|
31 | #include "stub.h"
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This function should be called from MakeCurrent(). It'll detect if
|
---|
35 | * we're in a multi-thread situation, and do the right thing for dispatch.
|
---|
36 | */
|
---|
37 | #ifdef CHROMIUM_THREADSAFE
|
---|
38 | static void
|
---|
39 | stubCheckMultithread( void )
|
---|
40 | {
|
---|
41 | static unsigned long knownID;
|
---|
42 | static GLboolean firstCall = GL_TRUE;
|
---|
43 |
|
---|
44 | if (stub.threadSafe)
|
---|
45 | return; /* nothing new, nothing to do */
|
---|
46 |
|
---|
47 | if (firstCall) {
|
---|
48 | knownID = crThreadID();
|
---|
49 | firstCall = GL_FALSE;
|
---|
50 | }
|
---|
51 | else if (knownID != crThreadID()) {
|
---|
52 | /* going thread-safe now! */
|
---|
53 | stub.threadSafe = GL_TRUE;
|
---|
54 | crSPUCopyDispatchTable(&glim, &stubThreadsafeDispatch);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Install the given dispatch table as the table used for all gl* calls.
|
---|
62 | */
|
---|
63 | static void
|
---|
64 | stubSetDispatch( SPUDispatchTable *table )
|
---|
65 | {
|
---|
66 | CRASSERT(table);
|
---|
67 |
|
---|
68 | #ifdef CHROMIUM_THREADSAFE
|
---|
69 | /* always set the per-thread dispatch pointer */
|
---|
70 | crSetTSD(&stub.dispatchTSD, (void *) table);
|
---|
71 | if (stub.threadSafe) {
|
---|
72 | /* Do nothing - the thread-safe dispatch functions will call GetTSD()
|
---|
73 | * to get a pointer to the dispatch table, and jump through it.
|
---|
74 | */
|
---|
75 | }
|
---|
76 | else
|
---|
77 | #endif
|
---|
78 | {
|
---|
79 | /* Single thread mode - just install the caller's dispatch table */
|
---|
80 | /* This conditional is an optimization to try to avoid unnecessary
|
---|
81 | * copying. It seems to work with atlantis, multiwin, etc. but
|
---|
82 | * _could_ be a problem. (Brian)
|
---|
83 | */
|
---|
84 | if (glim.copy_of != table->copy_of)
|
---|
85 | crSPUCopyDispatchTable(&glim, table);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | void stubForcedFlush(GLint con)
|
---|
90 | {
|
---|
91 | #if 0
|
---|
92 | GLint buffer;
|
---|
93 | stub.spu->dispatch_table.GetIntegerv(GL_DRAW_BUFFER, &buffer);
|
---|
94 | stub.spu->dispatch_table.DrawBuffer(GL_FRONT);
|
---|
95 | stub.spu->dispatch_table.Flush();
|
---|
96 | stub.spu->dispatch_table.DrawBuffer(buffer);
|
---|
97 | #else
|
---|
98 | if (con)
|
---|
99 | {
|
---|
100 | stub.spu->dispatch_table.VBoxConFlush(con);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | stub.spu->dispatch_table.Flush();
|
---|
105 | }
|
---|
106 | #endif
|
---|
107 | }
|
---|
108 |
|
---|
109 | void stubConChromiumParameteriCR(GLint con, GLenum param, GLint value)
|
---|
110 | {
|
---|
111 | // if (con)
|
---|
112 | stub.spu->dispatch_table.VBoxConChromiumParameteriCR(con, param, value);
|
---|
113 | // else
|
---|
114 | // crError("VBoxConChromiumParameteriCR called with null connection");
|
---|
115 | }
|
---|
116 |
|
---|
117 | void stubConChromiumParametervCR(GLint con, GLenum target, GLenum type, GLsizei count, const GLvoid *values)
|
---|
118 | {
|
---|
119 | // if (con)
|
---|
120 | stub.spu->dispatch_table.VBoxConChromiumParametervCR(con, target, type, count, values);
|
---|
121 | // else
|
---|
122 | // crError("VBoxConChromiumParameteriCR called with null connection");
|
---|
123 | }
|
---|
124 |
|
---|
125 | void stubConFlush(GLint con)
|
---|
126 | {
|
---|
127 | if (con)
|
---|
128 | stub.spu->dispatch_table.VBoxConFlush(con);
|
---|
129 | else
|
---|
130 | crError("stubConFlush called with null connection");
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void stubWindowCleanupForContextsCB(unsigned long key, void *data1, void *data2)
|
---|
134 | {
|
---|
135 | ContextInfo *context = (ContextInfo *) data1;
|
---|
136 | RT_NOREF(key);
|
---|
137 |
|
---|
138 | CRASSERT(context);
|
---|
139 |
|
---|
140 | if (context->currentDrawable == data2)
|
---|
141 | context->currentDrawable = NULL;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void stubDestroyWindow( GLint con, GLint window )
|
---|
145 | {
|
---|
146 | WindowInfo *winInfo = (WindowInfo *)
|
---|
147 | crHashtableSearch(stub.windowTable, (unsigned int) window);
|
---|
148 | if (winInfo && winInfo->type == CHROMIUM && stub.spu)
|
---|
149 | {
|
---|
150 | crHashtableLock(stub.windowTable);
|
---|
151 |
|
---|
152 | stub.spu->dispatch_table.VBoxWindowDestroy(con, winInfo->spuWindow );
|
---|
153 |
|
---|
154 | #ifdef WINDOWS
|
---|
155 | if (winInfo->hVisibleRegion != INVALID_HANDLE_VALUE)
|
---|
156 | {
|
---|
157 | DeleteObject(winInfo->hVisibleRegion);
|
---|
158 | }
|
---|
159 | #elif defined(GLX)
|
---|
160 | if (winInfo->pVisibleRegions)
|
---|
161 | {
|
---|
162 | XFree(winInfo->pVisibleRegions);
|
---|
163 | }
|
---|
164 | # ifdef CR_NEWWINTRACK
|
---|
165 | if (winInfo->syncDpy)
|
---|
166 | {
|
---|
167 | XCloseDisplay(winInfo->syncDpy);
|
---|
168 | }
|
---|
169 | # endif
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | stubForcedFlush(con);
|
---|
173 |
|
---|
174 | crHashtableWalk(stub.contextTable, stubWindowCleanupForContextsCB, winInfo);
|
---|
175 |
|
---|
176 | crHashtableDelete(stub.windowTable, window, crFree);
|
---|
177 |
|
---|
178 | crHashtableUnlock(stub.windowTable);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Create a new _Chromium_ window, not GLX, WGL or CGL.
|
---|
184 | * Called by crWindowCreate() only.
|
---|
185 | */
|
---|
186 | GLint
|
---|
187 | stubNewWindow( const char *dpyName, GLint visBits )
|
---|
188 | {
|
---|
189 | WindowInfo *winInfo;
|
---|
190 | GLint spuWin, size[2];
|
---|
191 |
|
---|
192 | spuWin = stub.spu->dispatch_table.WindowCreate( dpyName, visBits );
|
---|
193 | if (spuWin < 0) {
|
---|
194 | return -1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
|
---|
198 | if (!winInfo) {
|
---|
199 | stub.spu->dispatch_table.WindowDestroy(spuWin);
|
---|
200 | return -1;
|
---|
201 | }
|
---|
202 |
|
---|
203 | winInfo->type = CHROMIUM;
|
---|
204 |
|
---|
205 | /* Ask the head SPU for the initial window size */
|
---|
206 | size[0] = size[1] = 0;
|
---|
207 | stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, 0, GL_INT, 2, size);
|
---|
208 | if (size[0] == 0 && size[1] == 0) {
|
---|
209 | /* use some reasonable defaults */
|
---|
210 | size[0] = size[1] = 512;
|
---|
211 | }
|
---|
212 | winInfo->width = size[0];
|
---|
213 | winInfo->height = size[1];
|
---|
214 | #ifdef VBOX_WITH_WDDM
|
---|
215 | if (stub.bRunningUnderWDDM)
|
---|
216 | {
|
---|
217 | crError("Should not be here: WindowCreate/Destroy & VBoxPackGetInjectID require connection id!");
|
---|
218 | winInfo->mapped = 0;
|
---|
219 | }
|
---|
220 | else
|
---|
221 | #endif
|
---|
222 | {
|
---|
223 | winInfo->mapped = 1;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (!dpyName)
|
---|
227 | dpyName = "";
|
---|
228 |
|
---|
229 | crStrncpy(winInfo->dpyName, dpyName, MAX_DPY_NAME);
|
---|
230 | winInfo->dpyName[MAX_DPY_NAME-1] = 0;
|
---|
231 |
|
---|
232 | /* Use spuWin as the hash table index and GLX/WGL handle */
|
---|
233 | #ifdef WINDOWS
|
---|
234 | winInfo->drawable = (HDC) spuWin;
|
---|
235 | winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
|
---|
236 | #elif defined(Darwin)
|
---|
237 | winInfo->drawable = (CGSWindowID) spuWin;
|
---|
238 | #elif defined(GLX)
|
---|
239 | winInfo->drawable = (GLXDrawable) spuWin;
|
---|
240 | winInfo->pVisibleRegions = NULL;
|
---|
241 | winInfo->cVisibleRegions = 0;
|
---|
242 | #endif
|
---|
243 | #ifdef CR_NEWWINTRACK
|
---|
244 | winInfo->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID(0);
|
---|
245 | #endif
|
---|
246 | winInfo->spuWindow = spuWin;
|
---|
247 |
|
---|
248 | crHashtableAdd(stub.windowTable, (unsigned int) spuWin, winInfo);
|
---|
249 |
|
---|
250 | return spuWin;
|
---|
251 | }
|
---|
252 |
|
---|
253 | #ifdef GLX
|
---|
254 | # if 0 /* unused */
|
---|
255 | static XErrorHandler oldErrorHandler;
|
---|
256 | static unsigned char lastXError = Success;
|
---|
257 |
|
---|
258 | static int
|
---|
259 | errorHandler (Display *dpy, XErrorEvent *e)
|
---|
260 | {
|
---|
261 | RT_NOREF(dpy);
|
---|
262 |
|
---|
263 | lastXError = e->error_code;
|
---|
264 | return 0;
|
---|
265 | }
|
---|
266 | # endif /* unused */
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | GLboolean
|
---|
270 | stubIsWindowVisible(WindowInfo *win)
|
---|
271 | {
|
---|
272 | #if defined(WINDOWS)
|
---|
273 | # ifdef VBOX_WITH_WDDM
|
---|
274 | if (stub.bRunningUnderWDDM)
|
---|
275 | return win->mapped;
|
---|
276 | # endif
|
---|
277 | return GL_TRUE;
|
---|
278 | #elif defined(Darwin)
|
---|
279 | return GL_TRUE;
|
---|
280 | #elif defined(GLX)
|
---|
281 | Display *dpy = stubGetWindowDisplay(win);
|
---|
282 | if (dpy)
|
---|
283 | {
|
---|
284 | XWindowAttributes attr;
|
---|
285 | XLOCK(dpy);
|
---|
286 | XGetWindowAttributes(dpy, win->drawable, &attr);
|
---|
287 | XUNLOCK(dpy);
|
---|
288 |
|
---|
289 | if (attr.map_state == IsUnmapped)
|
---|
290 | {
|
---|
291 | return GL_FALSE;
|
---|
292 | }
|
---|
293 | # if 1
|
---|
294 | return GL_TRUE;
|
---|
295 | # else
|
---|
296 | if (attr.override_redirect)
|
---|
297 | {
|
---|
298 | return GL_TRUE;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (!stub.bXExtensionsChecked)
|
---|
302 | {
|
---|
303 | stubCheckXExtensions(win);
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (!stub.bHaveXComposite)
|
---|
307 | {
|
---|
308 | return GL_TRUE;
|
---|
309 | }
|
---|
310 | else
|
---|
311 | {
|
---|
312 | Pixmap p;
|
---|
313 |
|
---|
314 | crLockMutex(&stub.mutex);
|
---|
315 |
|
---|
316 | XLOCK(dpy);
|
---|
317 | XSync(dpy, false);
|
---|
318 | oldErrorHandler = XSetErrorHandler(errorHandler);
|
---|
319 | /** @todo this will create new pixmap for window every call*/
|
---|
320 | p = XCompositeNameWindowPixmap(dpy, win->drawable);
|
---|
321 | XSync(dpy, false);
|
---|
322 | XSetErrorHandler(oldErrorHandler);
|
---|
323 | XUNLOCK(dpy);
|
---|
324 |
|
---|
325 | switch (lastXError)
|
---|
326 | {
|
---|
327 | case Success:
|
---|
328 | XFreePixmap(dpy, p);
|
---|
329 | crUnlockMutex(&stub.mutex);
|
---|
330 | return GL_FALSE;
|
---|
331 | break;
|
---|
332 | case BadMatch:
|
---|
333 | /*Window isn't redirected*/
|
---|
334 | lastXError = Success;
|
---|
335 | break;
|
---|
336 | default:
|
---|
337 | crWarning("Unexpected XError %i", (int)lastXError);
|
---|
338 | lastXError = Success;
|
---|
339 | }
|
---|
340 |
|
---|
341 | crUnlockMutex(&stub.mutex);
|
---|
342 |
|
---|
343 | return GL_TRUE;
|
---|
344 | }
|
---|
345 | # endif
|
---|
346 | }
|
---|
347 | else {
|
---|
348 | /* probably created by crWindowCreate() */
|
---|
349 | return win->mapped;
|
---|
350 | }
|
---|
351 | #endif
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Given a Windows HDC or GLX Drawable, return the corresponding
|
---|
357 | * WindowInfo structure. Create a new one if needed.
|
---|
358 | */
|
---|
359 | WindowInfo *
|
---|
360 | #ifdef WINDOWS
|
---|
361 | stubGetWindowInfo( HDC drawable )
|
---|
362 | #elif defined(Darwin)
|
---|
363 | stubGetWindowInfo( CGSWindowID drawable )
|
---|
364 | #elif defined(GLX)
|
---|
365 | stubGetWindowInfo( Display *dpy, GLXDrawable drawable )
|
---|
366 | #endif
|
---|
367 | {
|
---|
368 | #ifndef WINDOWS
|
---|
369 | WindowInfo *winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) drawable);
|
---|
370 | #else
|
---|
371 | WindowInfo *winInfo;
|
---|
372 | HWND hwnd;
|
---|
373 | hwnd = WindowFromDC(drawable);
|
---|
374 |
|
---|
375 | if (!hwnd)
|
---|
376 | {
|
---|
377 | return NULL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) hwnd);
|
---|
381 | #endif
|
---|
382 | if (!winInfo) {
|
---|
383 | winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
|
---|
384 | if (!winInfo)
|
---|
385 | return NULL;
|
---|
386 | #ifdef GLX
|
---|
387 | crStrncpy(winInfo->dpyName, DisplayString(dpy), MAX_DPY_NAME);
|
---|
388 | winInfo->dpyName[MAX_DPY_NAME-1] = 0;
|
---|
389 | winInfo->dpy = dpy;
|
---|
390 | winInfo->pVisibleRegions = NULL;
|
---|
391 | #elif defined(Darwin)
|
---|
392 | winInfo->connection = _CGSDefaultConnection(); // store our connection as default
|
---|
393 | #elif defined(WINDOWS)
|
---|
394 | winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
|
---|
395 | winInfo->hWnd = hwnd;
|
---|
396 | #endif
|
---|
397 | winInfo->drawable = drawable;
|
---|
398 | winInfo->type = UNDECIDED;
|
---|
399 | winInfo->spuWindow = -1;
|
---|
400 | #ifdef VBOX_WITH_WDDM
|
---|
401 | if (stub.bRunningUnderWDDM)
|
---|
402 | winInfo->mapped = 0;
|
---|
403 | else
|
---|
404 | #endif
|
---|
405 | {
|
---|
406 | winInfo->mapped = -1; /* don't know */
|
---|
407 | }
|
---|
408 | winInfo->pOwner = NULL;
|
---|
409 | #ifdef CR_NEWWINTRACK
|
---|
410 | winInfo->u32ClientID = -1;
|
---|
411 | #endif
|
---|
412 | #ifndef WINDOWS
|
---|
413 | crHashtableAdd(stub.windowTable, (unsigned int) drawable, winInfo);
|
---|
414 | #else
|
---|
415 | crHashtableAdd(stub.windowTable, (unsigned int) hwnd, winInfo);
|
---|
416 | #endif
|
---|
417 | }
|
---|
418 | #ifdef WINDOWS
|
---|
419 | else
|
---|
420 | {
|
---|
421 | winInfo->drawable = drawable;
|
---|
422 | }
|
---|
423 | #endif
|
---|
424 | return winInfo;
|
---|
425 | }
|
---|
426 |
|
---|
427 | static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2);
|
---|
428 |
|
---|
429 | static void
|
---|
430 | stubContextFree( ContextInfo *context )
|
---|
431 | {
|
---|
432 | crMemZero(context, sizeof(ContextInfo)); /* just to be safe */
|
---|
433 | crFree(context);
|
---|
434 | }
|
---|
435 |
|
---|
436 | static void
|
---|
437 | stubDestroyContextLocked( ContextInfo *context )
|
---|
438 | {
|
---|
439 | unsigned long contextId = context->id;
|
---|
440 | if (context->type == NATIVE) {
|
---|
441 | #ifdef WINDOWS
|
---|
442 | stub.wsInterface.wglDeleteContext( context->hglrc );
|
---|
443 | #elif defined(Darwin)
|
---|
444 | stub.wsInterface.CGLDestroyContext( context->cglc );
|
---|
445 | #elif defined(GLX)
|
---|
446 | stub.wsInterface.glXDestroyContext( context->dpy, context->glxContext );
|
---|
447 | #endif
|
---|
448 | }
|
---|
449 | else if (context->type == CHROMIUM) {
|
---|
450 | /* Have pack SPU or tilesort SPU, etc. destroy the context */
|
---|
451 | CRASSERT(context->spuContext >= 0);
|
---|
452 | stub.spu->dispatch_table.DestroyContext( context->spuContext );
|
---|
453 | crHashtableWalk(stub.windowTable, stubWindowCheckOwnerCB, context);
|
---|
454 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
455 | if (context->spuConnection)
|
---|
456 | {
|
---|
457 | stub.spu->dispatch_table.VBoxConDestroy(context->spuConnection);
|
---|
458 | context->spuConnection = 0;
|
---|
459 | }
|
---|
460 | #endif
|
---|
461 | }
|
---|
462 |
|
---|
463 | #ifdef GLX
|
---|
464 | crFreeHashtable(context->pGLXPixmapsHash, crFree);
|
---|
465 | #endif
|
---|
466 |
|
---|
467 | crHashtableDelete(stub.contextTable, contextId, NULL);
|
---|
468 | }
|
---|
469 |
|
---|
470 | #ifdef CHROMIUM_THREADSAFE
|
---|
471 | static DECLCALLBACK(void) stubContextDtor(void*pvContext)
|
---|
472 | {
|
---|
473 | stubContextFree((ContextInfo*)pvContext);
|
---|
474 | }
|
---|
475 | #endif
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Allocate a new ContextInfo object, initialize it, put it into the
|
---|
479 | * context hash table. If type==CHROMIUM, call the head SPU's
|
---|
480 | * CreateContext() function too.
|
---|
481 | */
|
---|
482 | ContextInfo *
|
---|
483 | stubNewContext(char *dpyName, GLint visBits, ContextType type, unsigned long shareCtx
|
---|
484 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
485 | , struct VBOXUHGSMI *pHgsmi
|
---|
486 | #endif
|
---|
487 | )
|
---|
488 | {
|
---|
489 | GLint spuContext = -1, spuShareCtx = 0, spuConnection = 0;
|
---|
490 | ContextInfo *context;
|
---|
491 |
|
---|
492 | if (shareCtx > 0) {
|
---|
493 | /* translate shareCtx to a SPU context ID */
|
---|
494 | context = (ContextInfo *)
|
---|
495 | crHashtableSearch(stub.contextTable, shareCtx);
|
---|
496 | if (context)
|
---|
497 | spuShareCtx = context->spuContext;
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (type == CHROMIUM) {
|
---|
501 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
502 | if (pHgsmi)
|
---|
503 | {
|
---|
504 | spuConnection = stub.spu->dispatch_table.VBoxConCreate(pHgsmi);
|
---|
505 | if (!spuConnection)
|
---|
506 | {
|
---|
507 | crWarning("VBoxConCreate failed");
|
---|
508 | return NULL;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | #endif
|
---|
512 | spuContext
|
---|
513 | = stub.spu->dispatch_table.VBoxCreateContext(spuConnection, dpyName, visBits, spuShareCtx);
|
---|
514 | if (spuContext < 0)
|
---|
515 | {
|
---|
516 | crWarning("VBoxCreateContext failed");
|
---|
517 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
518 | if (spuConnection)
|
---|
519 | stub.spu->dispatch_table.VBoxConDestroy(spuConnection);
|
---|
520 | #endif
|
---|
521 | return NULL;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | context = crCalloc(sizeof(ContextInfo));
|
---|
526 | if (!context) {
|
---|
527 | stub.spu->dispatch_table.DestroyContext(spuContext);
|
---|
528 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
529 | if (spuConnection)
|
---|
530 | stub.spu->dispatch_table.VBoxConDestroy(spuConnection);
|
---|
531 | #endif
|
---|
532 | return NULL;
|
---|
533 | }
|
---|
534 |
|
---|
535 | if (!dpyName)
|
---|
536 | dpyName = "";
|
---|
537 |
|
---|
538 | context->id = stub.freeContextNumber++;
|
---|
539 | context->type = type;
|
---|
540 | context->spuContext = spuContext;
|
---|
541 | context->visBits = visBits;
|
---|
542 | context->currentDrawable = NULL;
|
---|
543 | crStrncpy(context->dpyName, dpyName, MAX_DPY_NAME);
|
---|
544 | context->dpyName[MAX_DPY_NAME-1] = 0;
|
---|
545 |
|
---|
546 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
547 | context->spuConnection = spuConnection;
|
---|
548 | context->pHgsmi = pHgsmi;
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | #ifdef CHROMIUM_THREADSAFE
|
---|
552 | VBoxTlsRefInit(context, stubContextDtor);
|
---|
553 | #endif
|
---|
554 |
|
---|
555 | #if defined(GLX) || defined(DARWIN)
|
---|
556 | context->share = (ContextInfo *)
|
---|
557 | crHashtableSearch(stub.contextTable, (unsigned long) shareCtx);
|
---|
558 | #endif
|
---|
559 |
|
---|
560 | #ifdef GLX
|
---|
561 | context->pGLXPixmapsHash = crAllocHashtable();
|
---|
562 | context->damageQueryFailed = GL_FALSE;
|
---|
563 | context->damageEventsBase = 0;
|
---|
564 | #endif
|
---|
565 |
|
---|
566 | crHashtableAdd(stub.contextTable, context->id, (void *) context);
|
---|
567 |
|
---|
568 | return context;
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | #ifdef Darwin
|
---|
573 |
|
---|
574 | #define SET_ATTR(l,i,a) ( (l)[(i)++] = (a) )
|
---|
575 | #define SET_ATTR_V(l,i,a,v) ( SET_ATTR(l,i,a), SET_ATTR(l,i,v) )
|
---|
576 |
|
---|
577 | void stubSetPFA( ContextInfo *ctx, CGLPixelFormatAttribute *attribs, int size, GLint *num ) {
|
---|
578 | GLuint visual = ctx->visBits;
|
---|
579 | int i = 0;
|
---|
580 |
|
---|
581 | CRASSERT(visual & CR_RGB_BIT);
|
---|
582 |
|
---|
583 | SET_ATTR_V(attribs, i, kCGLPFAColorSize, 8);
|
---|
584 |
|
---|
585 | if( visual & CR_DEPTH_BIT )
|
---|
586 | SET_ATTR_V(attribs, i, kCGLPFADepthSize, 16);
|
---|
587 |
|
---|
588 | if( visual & CR_ACCUM_BIT )
|
---|
589 | SET_ATTR_V(attribs, i, kCGLPFAAccumSize, 1);
|
---|
590 |
|
---|
591 | if( visual & CR_STENCIL_BIT )
|
---|
592 | SET_ATTR_V(attribs, i, kCGLPFAStencilSize, 1);
|
---|
593 |
|
---|
594 | if( visual & CR_ALPHA_BIT )
|
---|
595 | SET_ATTR_V(attribs, i, kCGLPFAAlphaSize, 1);
|
---|
596 |
|
---|
597 | if( visual & CR_DOUBLE_BIT )
|
---|
598 | SET_ATTR(attribs, i, kCGLPFADoubleBuffer);
|
---|
599 |
|
---|
600 | if( visual & CR_STEREO_BIT )
|
---|
601 | SET_ATTR(attribs, i, kCGLPFAStereo);
|
---|
602 |
|
---|
603 | /* SET_ATTR_V(attribs, i, kCGLPFASampleBuffers, 1);
|
---|
604 | SET_ATTR_V(attribs, i, kCGLPFASamples, 0);
|
---|
605 | SET_ATTR_V(attribs, i, kCGLPFADisplayMask, 0); */
|
---|
606 | SET_ATTR(attribs, i, kCGLPFABackingStore);
|
---|
607 | //SET_ATTR(attribs, i, kCGLPFAWindow); // kCGLPFAWindow deprecated starting from OSX 10.7
|
---|
608 | SET_ATTR_V(attribs, i, kCGLPFADisplayMask, ctx->disp_mask);
|
---|
609 |
|
---|
610 | SET_ATTR(attribs, i, 0);
|
---|
611 |
|
---|
612 | *num = i;
|
---|
613 | }
|
---|
614 |
|
---|
615 | #endif
|
---|
616 |
|
---|
617 | #ifndef GLX
|
---|
618 | /**
|
---|
619 | * This creates a native GLX/WGL context.
|
---|
620 | */
|
---|
621 | static GLboolean
|
---|
622 | InstantiateNativeContext( WindowInfo *window, ContextInfo *context )
|
---|
623 | {
|
---|
624 | #ifdef WINDOWS
|
---|
625 | context->hglrc = stub.wsInterface.wglCreateContext( window->drawable );
|
---|
626 | return context->hglrc ? GL_TRUE : GL_FALSE;
|
---|
627 | #elif defined(Darwin)
|
---|
628 | CGLContextObj shareCtx = NULL;
|
---|
629 | CGLPixelFormatObj pix;
|
---|
630 | long npix;
|
---|
631 |
|
---|
632 | CGLPixelFormatAttribute attribs[16];
|
---|
633 | GLint ind = 0;
|
---|
634 |
|
---|
635 | if( context->share ) {
|
---|
636 | if( context->cglc != context->share->cglc ) {
|
---|
637 | crWarning("CGLCreateContext() is trying to share a non-existant "
|
---|
638 | "CGL context. Setting share context to zero.");
|
---|
639 | shareCtx = 0;
|
---|
640 | }
|
---|
641 | else
|
---|
642 | shareCtx = context->cglc;
|
---|
643 | }
|
---|
644 |
|
---|
645 | stubSetPFA( context, attribs, 16, &ind );
|
---|
646 |
|
---|
647 | stub.wsInterface.CGLChoosePixelFormat( attribs, &pix, &npix );
|
---|
648 | stub.wsInterface.CGLCreateContext( pix, shareCtx, &context->cglc );
|
---|
649 | if( !context->cglc )
|
---|
650 | crError("InstantiateNativeContext: Couldn't Create the context!");
|
---|
651 |
|
---|
652 | stub.wsInterface.CGLDestroyPixelFormat( pix );
|
---|
653 |
|
---|
654 | if( context->parambits ) {
|
---|
655 | /* Set the delayed parameters */
|
---|
656 | if( context->parambits & VISBIT_SWAP_RECT )
|
---|
657 | stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapRectangle, context->swap_rect );
|
---|
658 |
|
---|
659 | if( context->parambits & VISBIT_SWAP_INTERVAL )
|
---|
660 | stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapInterval, &(context->swap_interval) );
|
---|
661 |
|
---|
662 | if( context->parambits & VISBIT_CLIENT_STORAGE )
|
---|
663 | stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPClientStorage, (long*)&(context->client_storage) );
|
---|
664 |
|
---|
665 | context->parambits = 0;
|
---|
666 | }
|
---|
667 |
|
---|
668 | return context->cglc ? GL_TRUE : GL_FALSE;
|
---|
669 | #elif defined(GLX)
|
---|
670 | GLXContext shareCtx = 0;
|
---|
671 |
|
---|
672 | /* sort out context sharing here */
|
---|
673 | if (context->share) {
|
---|
674 | if (context->glxContext != context->share->glxContext) {
|
---|
675 | crWarning("glXCreateContext() is trying to share a non-existant "
|
---|
676 | "GLX context. Setting share context to zero.");
|
---|
677 | shareCtx = 0;
|
---|
678 | }
|
---|
679 | else {
|
---|
680 | shareCtx = context->glxContext;
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | context->glxContext = stub.wsInterface.glXCreateContext( window->dpy,
|
---|
685 | context->visual, shareCtx, context->direct );
|
---|
686 |
|
---|
687 | return context->glxContext ? GL_TRUE : GL_FALSE;
|
---|
688 | #endif
|
---|
689 | }
|
---|
690 | #endif /* !GLX */
|
---|
691 |
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Utility functions to get window size and titlebar text.
|
---|
695 | */
|
---|
696 | #ifdef WINDOWS
|
---|
697 |
|
---|
698 | void
|
---|
699 | stubGetWindowGeometry(WindowInfo *window, int *x, int *y,
|
---|
700 | unsigned int *w, unsigned int *h )
|
---|
701 | {
|
---|
702 | RECT rect;
|
---|
703 |
|
---|
704 | if (!window->drawable || !window->hWnd) {
|
---|
705 | *w = *h = 0;
|
---|
706 | return;
|
---|
707 | }
|
---|
708 |
|
---|
709 | if (window->hWnd!=WindowFromDC(window->drawable))
|
---|
710 | {
|
---|
711 | crWarning("Window(%i) DC is no longer valid", window->spuWindow);
|
---|
712 | return;
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (!GetClientRect(window->hWnd, &rect))
|
---|
716 | {
|
---|
717 | crWarning("GetClientRect failed for %p", window->hWnd);
|
---|
718 | *w = *h = 0;
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | *w = rect.right - rect.left;
|
---|
722 | *h = rect.bottom - rect.top;
|
---|
723 |
|
---|
724 | if (!ClientToScreen( window->hWnd, (LPPOINT) &rect ))
|
---|
725 | {
|
---|
726 | crWarning("ClientToScreen failed for %p", window->hWnd);
|
---|
727 | *w = *h = 0;
|
---|
728 | return;
|
---|
729 | }
|
---|
730 | *x = rect.left;
|
---|
731 | *y = rect.top;
|
---|
732 | }
|
---|
733 |
|
---|
734 | static void
|
---|
735 | GetWindowTitle( const WindowInfo *window, char *title )
|
---|
736 | {
|
---|
737 | /* XXX - we don't handle recurseUp */
|
---|
738 | if (window->hWnd)
|
---|
739 | GetWindowText(window->hWnd, title, 100);
|
---|
740 | else
|
---|
741 | title[0] = 0;
|
---|
742 | }
|
---|
743 |
|
---|
744 | static void
|
---|
745 | GetCursorPosition(WindowInfo *window, int pos[2])
|
---|
746 | {
|
---|
747 | RECT rect;
|
---|
748 | POINT point;
|
---|
749 | GLint size[2], x, y;
|
---|
750 | unsigned int NativeHeight, NativeWidth, ChromiumHeight, ChromiumWidth;
|
---|
751 | float WidthRatio, HeightRatio;
|
---|
752 | static int DebugFlag = 0;
|
---|
753 |
|
---|
754 | // apparently the "window" parameter passed to this
|
---|
755 | // function contains the native window information
|
---|
756 | HWND NATIVEhwnd = window->hWnd;
|
---|
757 |
|
---|
758 | if (NATIVEhwnd!=WindowFromDC(window->drawable))
|
---|
759 | {
|
---|
760 | crWarning("Window(%i) DC is no longer valid", window->spuWindow);
|
---|
761 | return;
|
---|
762 | }
|
---|
763 |
|
---|
764 | // get the native window's height and width
|
---|
765 | stubGetWindowGeometry(window, &x, &y, &NativeWidth, &NativeHeight);
|
---|
766 |
|
---|
767 | // get the spu window's height and width
|
---|
768 | stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, window->spuWindow, GL_INT, 2, size);
|
---|
769 | ChromiumWidth = size[0];
|
---|
770 | ChromiumHeight = size[1];
|
---|
771 |
|
---|
772 | // get the ratio of the size of the native window to the cr window
|
---|
773 | WidthRatio = (float)ChromiumWidth / (float)NativeWidth;
|
---|
774 | HeightRatio = (float)ChromiumHeight / (float)NativeHeight;
|
---|
775 |
|
---|
776 | // output some debug information at the beginning
|
---|
777 | if(DebugFlag)
|
---|
778 | {
|
---|
779 | DebugFlag = 0;
|
---|
780 | crDebug("Native Window Handle = %d", NATIVEhwnd);
|
---|
781 | crDebug("Native Width = %i", NativeWidth);
|
---|
782 | crDebug("Native Height = %i", NativeHeight);
|
---|
783 | crDebug("Chromium Width = %i", ChromiumWidth);
|
---|
784 | crDebug("Chromium Height = %i", ChromiumHeight);
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (NATIVEhwnd)
|
---|
788 | {
|
---|
789 | GetClientRect( NATIVEhwnd, &rect );
|
---|
790 | GetCursorPos (&point);
|
---|
791 |
|
---|
792 | // make sure these coordinates are relative to the native window,
|
---|
793 | // not the whole desktop
|
---|
794 | ScreenToClient(NATIVEhwnd, &point);
|
---|
795 |
|
---|
796 | // calculate the new position of the virtual cursor
|
---|
797 | pos[0] = (int)(point.x * WidthRatio);
|
---|
798 | pos[1] = (int)((NativeHeight - point.y) * HeightRatio);
|
---|
799 | }
|
---|
800 | else
|
---|
801 | {
|
---|
802 | pos[0] = 0;
|
---|
803 | pos[1] = 0;
|
---|
804 | }
|
---|
805 | }
|
---|
806 |
|
---|
807 | #elif defined(Darwin)
|
---|
808 |
|
---|
809 | extern OSStatus CGSGetScreenRectForWindow( CGSConnectionID cid, CGSWindowID wid, float *outRect );
|
---|
810 | extern OSStatus CGSGetWindowBounds( CGSConnectionID cid, CGSWindowID wid, float *bounds );
|
---|
811 |
|
---|
812 | void
|
---|
813 | stubGetWindowGeometry( const WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h )
|
---|
814 | {
|
---|
815 | float rect[4];
|
---|
816 |
|
---|
817 | if( !window ||
|
---|
818 | !window->connection ||
|
---|
819 | !window->drawable ||
|
---|
820 | CGSGetWindowBounds( window->connection, window->drawable, rect ) != noErr )
|
---|
821 | {
|
---|
822 | *x = *y = 0;
|
---|
823 | *w = *h = 0;
|
---|
824 | } else {
|
---|
825 | *x = (int) rect[0];
|
---|
826 | *y = (int) rect[1];
|
---|
827 | *w = (int) rect[2];
|
---|
828 | *h = (int) rect[3];
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 |
|
---|
833 | static void
|
---|
834 | GetWindowTitle( const WindowInfo *window, char *title )
|
---|
835 | {
|
---|
836 | /* XXX \todo Darwin window Title */
|
---|
837 | title[0] = '\0';
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | static void
|
---|
842 | GetCursorPosition( const WindowInfo *window, int pos[2] )
|
---|
843 | {
|
---|
844 | Point mouse_pos;
|
---|
845 | float window_rect[4];
|
---|
846 |
|
---|
847 | GetMouse( &mouse_pos );
|
---|
848 | CGSGetScreenRectForWindow( window->connection, window->drawable, window_rect );
|
---|
849 |
|
---|
850 | pos[0] = mouse_pos.h - (int) window_rect[0];
|
---|
851 | pos[1] = (int) window_rect[3] - (mouse_pos.v - (int) window_rect[1]);
|
---|
852 |
|
---|
853 | /*crDebug( "%i %i", pos[0], pos[1] );*/
|
---|
854 | }
|
---|
855 |
|
---|
856 | #elif defined(GLX)
|
---|
857 |
|
---|
858 | void
|
---|
859 | stubGetWindowGeometry(WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h)
|
---|
860 | {
|
---|
861 | Window root, child;
|
---|
862 | unsigned int border, depth;
|
---|
863 | Display *dpy;
|
---|
864 |
|
---|
865 | dpy = stubGetWindowDisplay(window);
|
---|
866 |
|
---|
867 | /// @todo Performing those checks is expensive operation, especially for simple apps with high FPS.
|
---|
868 | // Disabling those triples glxgears fps, thus using xevents instead of per frame polling is much more preferred.
|
---|
869 | /// @todo Check similar on windows guests, though doubtful as there're no XSync like calls on windows.
|
---|
870 | if (window && dpy)
|
---|
871 | {
|
---|
872 | XLOCK(dpy);
|
---|
873 | }
|
---|
874 |
|
---|
875 | if (!window
|
---|
876 | || !dpy
|
---|
877 | || !window->drawable
|
---|
878 | || !XGetGeometry(dpy, window->drawable, &root, x, y, w, h, &border, &depth)
|
---|
879 | || !XTranslateCoordinates(dpy, window->drawable, root, 0, 0, x, y, &child))
|
---|
880 | {
|
---|
881 | crWarning("Failed to get windows geometry for %p, try xwininfo", window);
|
---|
882 | *x = *y = 0;
|
---|
883 | *w = *h = 0;
|
---|
884 | }
|
---|
885 |
|
---|
886 | if (window && dpy)
|
---|
887 | {
|
---|
888 | XUNLOCK(dpy);
|
---|
889 | }
|
---|
890 | }
|
---|
891 |
|
---|
892 | static char *
|
---|
893 | GetWindowTitleHelper( Display *dpy, Window window, GLboolean recurseUp )
|
---|
894 | {
|
---|
895 | while (1) {
|
---|
896 | char *name;
|
---|
897 | if (!XFetchName(dpy, window, &name))
|
---|
898 | return NULL;
|
---|
899 | if (name[0]) {
|
---|
900 | return name;
|
---|
901 | }
|
---|
902 | else if (recurseUp) {
|
---|
903 | /* This window has no name, try the parent */
|
---|
904 | Status stat;
|
---|
905 | Window root, parent, *children;
|
---|
906 | unsigned int numChildren;
|
---|
907 | stat = XQueryTree( dpy, window, &root, &parent,
|
---|
908 | &children, &numChildren );
|
---|
909 | if (!stat || window == root)
|
---|
910 | return NULL;
|
---|
911 | if (children)
|
---|
912 | XFree(children);
|
---|
913 | window = parent;
|
---|
914 | }
|
---|
915 | else {
|
---|
916 | XFree(name);
|
---|
917 | return NULL;
|
---|
918 | }
|
---|
919 | }
|
---|
920 | }
|
---|
921 |
|
---|
922 | static void
|
---|
923 | GetWindowTitle( const WindowInfo *window, char *title )
|
---|
924 | {
|
---|
925 | char *t = GetWindowTitleHelper(window->dpy, window->drawable, GL_TRUE);
|
---|
926 | if (t) {
|
---|
927 | crStrcpy(title, t);
|
---|
928 | XFree(t);
|
---|
929 | }
|
---|
930 | else {
|
---|
931 | title[0] = 0;
|
---|
932 | }
|
---|
933 | }
|
---|
934 |
|
---|
935 |
|
---|
936 | /**
|
---|
937 | *Return current cursor position in local window coords.
|
---|
938 | */
|
---|
939 | static void
|
---|
940 | GetCursorPosition(WindowInfo *window, int pos[2] )
|
---|
941 | {
|
---|
942 | int rootX, rootY;
|
---|
943 | Window root, child;
|
---|
944 | unsigned int mask;
|
---|
945 | int x, y;
|
---|
946 |
|
---|
947 | XLOCK(window->dpy);
|
---|
948 |
|
---|
949 | Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
|
---|
950 | &rootX, &rootY, &pos[0], &pos[1], &mask);
|
---|
951 | if (q) {
|
---|
952 | unsigned int w, h;
|
---|
953 | stubGetWindowGeometry( window, &x, &y, &w, &h );
|
---|
954 | /* invert Y */
|
---|
955 | pos[1] = (int) h - pos[1] - 1;
|
---|
956 | }
|
---|
957 | else {
|
---|
958 | pos[0] = pos[1] = 0;
|
---|
959 | }
|
---|
960 |
|
---|
961 | XUNLOCK(window->dpy);
|
---|
962 | }
|
---|
963 |
|
---|
964 | #endif
|
---|
965 |
|
---|
966 |
|
---|
967 | /**
|
---|
968 | * This function is called by MakeCurrent() and determines whether or
|
---|
969 | * not a new rendering context should be bound to Chromium or the native
|
---|
970 | * OpenGL.
|
---|
971 | * \return GL_FALSE if native OpenGL should be used, or GL_TRUE if Chromium
|
---|
972 | * should be used.
|
---|
973 | */
|
---|
974 | static GLboolean
|
---|
975 | stubCheckUseChromium( WindowInfo *window )
|
---|
976 | {
|
---|
977 | int x, y;
|
---|
978 | unsigned int w, h;
|
---|
979 |
|
---|
980 | /* If the provided window is CHROMIUM, we're clearly intended
|
---|
981 | * to create a CHROMIUM context.
|
---|
982 | */
|
---|
983 | if (window->type == CHROMIUM)
|
---|
984 | return GL_TRUE;
|
---|
985 |
|
---|
986 | if (stub.ignoreFreeglutMenus) {
|
---|
987 | const char *glutMenuTitle = "freeglut menu";
|
---|
988 | char title[1000];
|
---|
989 | GetWindowTitle(window, title);
|
---|
990 | if (crStrcmp(title, glutMenuTitle) == 0) {
|
---|
991 | crDebug("GL faker: Ignoring freeglut menu window");
|
---|
992 | return GL_FALSE;
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | /* If the user's specified a window count for Chromium, see if
|
---|
997 | * this window satisfies that criterium.
|
---|
998 | */
|
---|
999 | stub.matchChromiumWindowCounter++;
|
---|
1000 | if (stub.matchChromiumWindowCount > 0) {
|
---|
1001 | if (stub.matchChromiumWindowCounter != stub.matchChromiumWindowCount) {
|
---|
1002 | crDebug("Using native GL, app window doesn't meet match_window_count");
|
---|
1003 | return GL_FALSE;
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | /* If the user's specified a window list to ignore, see if this
|
---|
1008 | * window satisfies that criterium.
|
---|
1009 | */
|
---|
1010 | if (stub.matchChromiumWindowID) {
|
---|
1011 | GLuint i;
|
---|
1012 |
|
---|
1013 | for (i = 0; i <= stub.numIgnoreWindowID; i++) {
|
---|
1014 | if (stub.matchChromiumWindowID[i] == stub.matchChromiumWindowCounter) {
|
---|
1015 | crDebug("Ignore window ID %d, using native GL", stub.matchChromiumWindowID[i]);
|
---|
1016 | return GL_FALSE;
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /* If the user's specified a minimum window size for Chromium, see if
|
---|
1022 | * this window satisfies that criterium.
|
---|
1023 | */
|
---|
1024 | if (stub.minChromiumWindowWidth > 0 &&
|
---|
1025 | stub.minChromiumWindowHeight > 0) {
|
---|
1026 | stubGetWindowGeometry( window, &x, &y, &w, &h );
|
---|
1027 | if (w >= stub.minChromiumWindowWidth &&
|
---|
1028 | h >= stub.minChromiumWindowHeight) {
|
---|
1029 |
|
---|
1030 | /* Check for maximum sized window now too */
|
---|
1031 | if (stub.maxChromiumWindowWidth &&
|
---|
1032 | stub.maxChromiumWindowHeight) {
|
---|
1033 | if (w < stub.maxChromiumWindowWidth &&
|
---|
1034 | h < stub.maxChromiumWindowHeight)
|
---|
1035 | return GL_TRUE;
|
---|
1036 | else
|
---|
1037 | return GL_FALSE;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | return GL_TRUE;
|
---|
1041 | }
|
---|
1042 | crDebug("Using native GL, app window doesn't meet minimum_window_size");
|
---|
1043 | return GL_FALSE;
|
---|
1044 | }
|
---|
1045 | else if (stub.matchWindowTitle) {
|
---|
1046 | /* If the user's specified a window title for Chromium, see if this
|
---|
1047 | * window satisfies that criterium.
|
---|
1048 | */
|
---|
1049 | GLboolean wildcard = GL_FALSE;
|
---|
1050 | char title[1000];
|
---|
1051 | char *titlePattern;
|
---|
1052 | int len;
|
---|
1053 | /* check for leading '*' wildcard */
|
---|
1054 | if (stub.matchWindowTitle[0] == '*') {
|
---|
1055 | titlePattern = crStrdup( stub.matchWindowTitle + 1 );
|
---|
1056 | wildcard = GL_TRUE;
|
---|
1057 | }
|
---|
1058 | else {
|
---|
1059 | titlePattern = crStrdup( stub.matchWindowTitle );
|
---|
1060 | }
|
---|
1061 | /* check for trailing '*' wildcard */
|
---|
1062 | len = crStrlen(titlePattern);
|
---|
1063 | if (len > 0 && titlePattern[len - 1] == '*') {
|
---|
1064 | titlePattern[len - 1] = '\0'; /* terminate here */
|
---|
1065 | wildcard = GL_TRUE;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | GetWindowTitle( window, title );
|
---|
1069 | if (title[0]) {
|
---|
1070 | if (wildcard) {
|
---|
1071 | if (crStrstr(title, titlePattern)) {
|
---|
1072 | crFree(titlePattern);
|
---|
1073 | return GL_TRUE;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | else if (crStrcmp(title, titlePattern) == 0) {
|
---|
1077 | crFree(titlePattern);
|
---|
1078 | return GL_TRUE;
|
---|
1079 | }
|
---|
1080 | }
|
---|
1081 | crFree(titlePattern);
|
---|
1082 | crDebug("Using native GL, app window title doesn't match match_window_title string (\"%s\" != \"%s\")", title, stub.matchWindowTitle);
|
---|
1083 | return GL_FALSE;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /* Window title and size don't matter */
|
---|
1087 | CRASSERT(stub.minChromiumWindowWidth == 0);
|
---|
1088 | CRASSERT(stub.minChromiumWindowHeight == 0);
|
---|
1089 | CRASSERT(stub.matchWindowTitle == NULL);
|
---|
1090 |
|
---|
1091 | /* User hasn't specified a width/height or window title.
|
---|
1092 | * We'll use chromium for this window (and context) if no other is.
|
---|
1093 | */
|
---|
1094 |
|
---|
1095 | return GL_TRUE; /* use Chromium! */
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2)
|
---|
1099 | {
|
---|
1100 | WindowInfo *pWindow = (WindowInfo *) data1;
|
---|
1101 | ContextInfo *pCtx = (ContextInfo *) data2;
|
---|
1102 |
|
---|
1103 | RT_NOREF(key);
|
---|
1104 |
|
---|
1105 |
|
---|
1106 | if (pWindow->pOwner == pCtx)
|
---|
1107 | {
|
---|
1108 | #ifdef WINDOWS
|
---|
1109 | /* Note: can't use WindowFromDC(context->pOwnWindow->drawable) here
|
---|
1110 | because GL context is already released from DC and actual guest window
|
---|
1111 | could be destroyed.
|
---|
1112 | */
|
---|
1113 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->hWnd);
|
---|
1114 | #else
|
---|
1115 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->drawable);
|
---|
1116 | #endif
|
---|
1117 | }
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | GLboolean stubCtxCreate(ContextInfo *context)
|
---|
1121 | {
|
---|
1122 | /*
|
---|
1123 | * Create a Chromium context.
|
---|
1124 | */
|
---|
1125 | #if defined(GLX) || defined(DARWIN)
|
---|
1126 | GLint spuShareCtx = context->share ? context->share->spuContext : 0;
|
---|
1127 | #else
|
---|
1128 | GLint spuShareCtx = 0;
|
---|
1129 | #endif
|
---|
1130 | GLint spuConnection = 0;
|
---|
1131 | CRASSERT(stub.spu);
|
---|
1132 | CRASSERT(stub.spu->dispatch_table.CreateContext);
|
---|
1133 | context->type = CHROMIUM;
|
---|
1134 |
|
---|
1135 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
1136 | if (context->pHgsmi)
|
---|
1137 | {
|
---|
1138 | spuConnection = stub.spu->dispatch_table.VBoxConCreate(context->pHgsmi);
|
---|
1139 | if (!spuConnection)
|
---|
1140 | {
|
---|
1141 | crError("VBoxConCreate failed");
|
---|
1142 | return GL_FALSE;
|
---|
1143 | }
|
---|
1144 | context->spuConnection = spuConnection;
|
---|
1145 | }
|
---|
1146 | #endif
|
---|
1147 |
|
---|
1148 | context->spuContext
|
---|
1149 | = stub.spu->dispatch_table.VBoxCreateContext(spuConnection, context->dpyName,
|
---|
1150 | context->visBits,
|
---|
1151 | spuShareCtx);
|
---|
1152 |
|
---|
1153 | return GL_TRUE;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | GLboolean stubCtxCheckCreate(ContextInfo *context)
|
---|
1157 | {
|
---|
1158 | if (context->type == UNDECIDED)
|
---|
1159 | return stubCtxCreate(context);
|
---|
1160 | return CHROMIUM == context->type;
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 |
|
---|
1164 | GLboolean
|
---|
1165 | stubMakeCurrent( WindowInfo *window, ContextInfo *context )
|
---|
1166 | {
|
---|
1167 | GLboolean retVal = GL_FALSE;
|
---|
1168 |
|
---|
1169 | /*
|
---|
1170 | * Get WindowInfo and ContextInfo pointers.
|
---|
1171 | */
|
---|
1172 |
|
---|
1173 | if (!context || !window) {
|
---|
1174 | ContextInfo * currentContext = stubGetCurrentContext();
|
---|
1175 | if (currentContext)
|
---|
1176 | currentContext->currentDrawable = NULL;
|
---|
1177 | if (context)
|
---|
1178 | context->currentDrawable = NULL;
|
---|
1179 | stubSetCurrentContext(NULL);
|
---|
1180 | return GL_TRUE; /* OK */
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | #ifdef CHROMIUM_THREADSAFE
|
---|
1184 | stubCheckMultithread();
|
---|
1185 | #endif
|
---|
1186 |
|
---|
1187 | if (context->type == UNDECIDED) {
|
---|
1188 | /* Here's where we really create contexts */
|
---|
1189 | #ifdef CHROMIUM_THREADSAFE
|
---|
1190 | crLockMutex(&stub.mutex);
|
---|
1191 | #endif
|
---|
1192 |
|
---|
1193 | if (stubCheckUseChromium(window)) {
|
---|
1194 | GLint spuConnection = 0;
|
---|
1195 |
|
---|
1196 | if (!stubCtxCreate(context))
|
---|
1197 | {
|
---|
1198 | crWarning("stubCtxCreate failed");
|
---|
1199 | return GL_FALSE;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
1203 | spuConnection = context->spuConnection;
|
---|
1204 | #endif
|
---|
1205 |
|
---|
1206 | if (window->spuWindow == -1)
|
---|
1207 | {
|
---|
1208 | /*crDebug("(1)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
|
---|
1209 | window->spuWindow = stub.spu->dispatch_table.VBoxWindowCreate(spuConnection, window->dpyName, context->visBits );
|
---|
1210 | #ifdef CR_NEWWINTRACK
|
---|
1211 | window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID(spuConnection);
|
---|
1212 | #endif
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 | #ifndef GLX
|
---|
1216 | else {
|
---|
1217 | /*
|
---|
1218 | * Create a native OpenGL context.
|
---|
1219 | */
|
---|
1220 | if (!InstantiateNativeContext(window, context))
|
---|
1221 | {
|
---|
1222 | # ifdef CHROMIUM_THREADSAFE
|
---|
1223 | crUnlockMutex(&stub.mutex);
|
---|
1224 | # endif
|
---|
1225 | return 0; /* false */
|
---|
1226 | }
|
---|
1227 | context->type = NATIVE;
|
---|
1228 | }
|
---|
1229 | #endif /* !GLX */
|
---|
1230 |
|
---|
1231 | #ifdef CHROMIUM_THREADSAFE
|
---|
1232 | crUnlockMutex(&stub.mutex);
|
---|
1233 | #endif
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | if (context->type == NATIVE) {
|
---|
1238 | /*
|
---|
1239 | * Native OpenGL MakeCurrent().
|
---|
1240 | */
|
---|
1241 | #ifdef WINDOWS
|
---|
1242 | retVal = (GLboolean) stub.wsInterface.wglMakeCurrent( window->drawable, context->hglrc );
|
---|
1243 | #elif defined(Darwin)
|
---|
1244 | // XXX \todo We need to differentiate between these two..
|
---|
1245 | retVal = ( stub.wsInterface.CGLSetSurface(context->cglc, window->connection, window->drawable, window->surface) == noErr );
|
---|
1246 | retVal = ( stub.wsInterface.CGLSetCurrentContext(context->cglc) == noErr );
|
---|
1247 | #elif defined(GLX)
|
---|
1248 | retVal = (GLboolean) stub.wsInterface.glXMakeCurrent( window->dpy, window->drawable, context->glxContext );
|
---|
1249 | #endif
|
---|
1250 | }
|
---|
1251 | else {
|
---|
1252 | /*
|
---|
1253 | * SPU chain MakeCurrent().
|
---|
1254 | */
|
---|
1255 | CRASSERT(context->type == CHROMIUM);
|
---|
1256 | CRASSERT(context->spuContext >= 0);
|
---|
1257 |
|
---|
1258 | /*if (context->currentDrawable && context->currentDrawable != window)
|
---|
1259 | crDebug("Rebinding context %p to a different window", context);*/
|
---|
1260 |
|
---|
1261 | if (window->type == NATIVE) {
|
---|
1262 | crWarning("Can't rebind a chromium context to a native window\n");
|
---|
1263 | retVal = 0;
|
---|
1264 | }
|
---|
1265 | else {
|
---|
1266 | if (window->spuWindow == -1)
|
---|
1267 | {
|
---|
1268 | /*crDebug("(2)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
|
---|
1269 | window->spuWindow = stub.spu->dispatch_table.VBoxWindowCreate(
|
---|
1270 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
1271 | context->spuConnection,
|
---|
1272 | #else
|
---|
1273 | 0,
|
---|
1274 | #endif
|
---|
1275 | window->dpyName, context->visBits );
|
---|
1276 | #ifdef CR_NEWWINTRACK
|
---|
1277 | window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID(
|
---|
1278 | # if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
1279 | context->spuConnection
|
---|
1280 | # else
|
---|
1281 | 0
|
---|
1282 | # endif
|
---|
1283 | );
|
---|
1284 | #endif
|
---|
1285 | if (context->currentDrawable && context->currentDrawable->type==CHROMIUM
|
---|
1286 | && context->currentDrawable->pOwner==context)
|
---|
1287 | {
|
---|
1288 | #ifdef WINDOWS
|
---|
1289 | if (context->currentDrawable->hWnd!=WindowFromDC(context->currentDrawable->drawable))
|
---|
1290 | {
|
---|
1291 | stubDestroyWindow(CR_CTX_CON(context), (GLint)context->currentDrawable->hWnd);
|
---|
1292 | }
|
---|
1293 | #else
|
---|
1294 | Window root;
|
---|
1295 | int x, y;
|
---|
1296 | unsigned int border, depth, w, h;
|
---|
1297 |
|
---|
1298 | XLOCK(context->currentDrawable->dpy);
|
---|
1299 | if (!XGetGeometry(context->currentDrawable->dpy, context->currentDrawable->drawable, &root, &x, &y, &w, &h, &border, &depth))
|
---|
1300 | {
|
---|
1301 | stubDestroyWindow(CR_CTX_CON(context), (GLint)context->currentDrawable->drawable);
|
---|
1302 | }
|
---|
1303 | XUNLOCK(context->currentDrawable->dpy);
|
---|
1304 | #endif
|
---|
1305 |
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | if (window->spuWindow != (GLint)window->drawable)
|
---|
1310 | stub.spu->dispatch_table.MakeCurrent( window->spuWindow, (GLint) window->drawable, context->spuContext );
|
---|
1311 | else
|
---|
1312 | stub.spu->dispatch_table.MakeCurrent( window->spuWindow, 0, /* native window handle */ context->spuContext );
|
---|
1313 |
|
---|
1314 | retVal = 1;
|
---|
1315 | }
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | window->type = context->type;
|
---|
1319 | window->pOwner = context;
|
---|
1320 | context->currentDrawable = window;
|
---|
1321 | stubSetCurrentContext(context);
|
---|
1322 |
|
---|
1323 | if (retVal) {
|
---|
1324 | /* Now, if we've transitions from Chromium to native rendering, or
|
---|
1325 | * vice versa, we have to change all the OpenGL entrypoint pointers.
|
---|
1326 | */
|
---|
1327 | if (context->type == NATIVE) {
|
---|
1328 | /* Switch to native API */
|
---|
1329 | /*printf(" Switching to native API\n");*/
|
---|
1330 | stubSetDispatch(&stub.nativeDispatch);
|
---|
1331 | }
|
---|
1332 | else if (context->type == CHROMIUM) {
|
---|
1333 | /* Switch to stub (SPU) API */
|
---|
1334 | /*printf(" Switching to spu API\n");*/
|
---|
1335 | stubSetDispatch(&stub.spuDispatch);
|
---|
1336 | }
|
---|
1337 | else {
|
---|
1338 | /* no API switch needed */
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | if (!window->width && window->type == CHROMIUM) {
|
---|
1343 | /* One time window setup */
|
---|
1344 | int x, y;
|
---|
1345 | unsigned int winW, winH;
|
---|
1346 |
|
---|
1347 | stubGetWindowGeometry( window, &x, &y, &winW, &winH );
|
---|
1348 |
|
---|
1349 | /* If we're not using GLX/WGL (no app window) we'll always get
|
---|
1350 | * a width and height of zero here. In that case, skip the viewport
|
---|
1351 | * call since we're probably using a tilesort SPU with fake_window_dims
|
---|
1352 | * which the tilesort SPU will use for the viewport.
|
---|
1353 | */
|
---|
1354 | window->width = winW;
|
---|
1355 | window->height = winH;
|
---|
1356 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
1357 | if (stubIsWindowVisible(window))
|
---|
1358 | #endif
|
---|
1359 | {
|
---|
1360 | if (stub.trackWindowSize)
|
---|
1361 | stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
|
---|
1362 | if (stub.trackWindowPos)
|
---|
1363 | stub.spuDispatch.WindowPosition(window->spuWindow, x, y);
|
---|
1364 | if (winW > 0 && winH > 0)
|
---|
1365 | stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
|
---|
1366 | }
|
---|
1367 | #ifdef VBOX_WITH_WDDM
|
---|
1368 | if (stub.trackWindowVisibleRgn)
|
---|
1369 | stub.spu->dispatch_table.WindowVisibleRegion(window->spuWindow, 0, NULL);
|
---|
1370 | #endif
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | /* Update window mapping state.
|
---|
1374 | * Basically, this lets us hide render SPU windows which correspond
|
---|
1375 | * to unmapped application windows. Without this, "pertly" (for example)
|
---|
1376 | * opens *lots* of temporary windows which otherwise clutter the screen.
|
---|
1377 | */
|
---|
1378 | if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
|
---|
1379 | const int mapped = stubIsWindowVisible(window);
|
---|
1380 | if (mapped != window->mapped) {
|
---|
1381 | crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
|
---|
1382 | stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
|
---|
1383 | window->mapped = mapped;
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | return retVal;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | void
|
---|
1391 | stubDestroyContext( unsigned long contextId )
|
---|
1392 | {
|
---|
1393 | ContextInfo *context;
|
---|
1394 |
|
---|
1395 | if (!stub.contextTable) {
|
---|
1396 | return;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /* the lock order is windowTable->contextTable (see wglMakeCurrent_prox, glXMakeCurrent)
|
---|
1400 | * this is why we need to take a windowTable lock since we will later do stub.windowTable access & locking */
|
---|
1401 | crHashtableLock(stub.windowTable);
|
---|
1402 | crHashtableLock(stub.contextTable);
|
---|
1403 |
|
---|
1404 | context = (ContextInfo *) crHashtableSearch(stub.contextTable, contextId);
|
---|
1405 | if (context)
|
---|
1406 | stubDestroyContextLocked(context);
|
---|
1407 | else
|
---|
1408 | crError("No context.");
|
---|
1409 |
|
---|
1410 | #ifdef CHROMIUM_THREADSAFE
|
---|
1411 | if (stubGetCurrentContext() == context) {
|
---|
1412 | stubSetCurrentContext(NULL);
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | VBoxTlsRefMarkDestroy(context);
|
---|
1416 | VBoxTlsRefRelease(context);
|
---|
1417 | #else
|
---|
1418 | if (stubGetCurrentContext() == context) {
|
---|
1419 | stubSetCurrentContext(NULL);
|
---|
1420 | }
|
---|
1421 | stubContextFree(context);
|
---|
1422 | #endif
|
---|
1423 | crHashtableUnlock(stub.contextTable);
|
---|
1424 | crHashtableUnlock(stub.windowTable);
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | void
|
---|
1428 | stubSwapBuffers(WindowInfo *window, GLint flags)
|
---|
1429 | {
|
---|
1430 | if (!window)
|
---|
1431 | return;
|
---|
1432 |
|
---|
1433 | /* Determine if this window is being rendered natively or through
|
---|
1434 | * Chromium.
|
---|
1435 | */
|
---|
1436 |
|
---|
1437 | if (window->type == NATIVE) {
|
---|
1438 | /*printf("*** Swapping native window %d\n", (int) drawable);*/
|
---|
1439 | #ifdef WINDOWS
|
---|
1440 | (void) stub.wsInterface.wglSwapBuffers( window->drawable );
|
---|
1441 | #elif defined(Darwin)
|
---|
1442 | /* ...is this ok? */
|
---|
1443 | /* stub.wsInterface.CGLFlushDrawable( context->cglc ); */
|
---|
1444 | crDebug("stubSwapBuffers: unable to swap (no context!)");
|
---|
1445 | #elif defined(GLX)
|
---|
1446 | stub.wsInterface.glXSwapBuffers( window->dpy, window->drawable );
|
---|
1447 | #endif
|
---|
1448 | }
|
---|
1449 | else if (window->type == CHROMIUM) {
|
---|
1450 | /* Let the SPU do the buffer swap */
|
---|
1451 | /*printf("*** Swapping chromium window %d\n", (int) drawable);*/
|
---|
1452 | if (stub.appDrawCursor) {
|
---|
1453 | int pos[2];
|
---|
1454 | GetCursorPosition(window, pos);
|
---|
1455 | stub.spu->dispatch_table.ChromiumParametervCR(GL_CURSOR_POSITION_CR, GL_INT, 2, pos);
|
---|
1456 | }
|
---|
1457 | stub.spu->dispatch_table.SwapBuffers( window->spuWindow, flags );
|
---|
1458 | }
|
---|
1459 | else {
|
---|
1460 | crDebug("Calling SwapBuffers on a window we haven't seen before (no-op).");
|
---|
1461 | }
|
---|
1462 | }
|
---|