VirtualBox

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

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

crOgl/wddm: per-context connections

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