VirtualBox

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

Last change on this file since 33988 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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