VirtualBox

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

Last change on this file since 35263 was 34295, checked in by vboxsync, 13 years ago

crOpenGL/wddm: more strict window info tracking

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

© 2023 Oracle
ContactPrivacy policyTerms of Use