VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/context.c@ 41971

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

crOpenGL/wddm: fix flikering on window creation

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