VirtualBox

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

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

crOpenGL: check call return values and add warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 34.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 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 HWND hwnd;
504
505 if (!window->drawable) {
506 *w = *h = 0;
507 return;
508 }
509
510 hwnd = WindowFromDC( window->drawable );
511
512 if (!hwnd) {
513 *w = 0;
514 *h = 0;
515 }
516 else {
517 if (!GetClientRect(hwnd, &rect))
518 {
519 crWarning("GetClientRect failed for %p", hwnd);
520 *w = *h = 0;
521 return;
522 }
523 *w = rect.right - rect.left;
524 *h = rect.bottom - rect.top;
525 if (!ClientToScreen( hwnd, (LPPOINT) &rect ))
526 {
527 crWarning("ClientToScreen failed for %p", hwnd);
528 *w = *h = 0;
529 return;
530 }
531 *x = rect.left;
532 *y = rect.top;
533 }
534}
535
536static void
537GetWindowTitle( const WindowInfo *window, char *title )
538{
539 HWND hwnd;
540 /* XXX - we don't handle recurseUp */
541 hwnd = WindowFromDC( window->drawable );
542 if (hwnd)
543 GetWindowText(hwnd, title, 100);
544 else
545 title[0] = 0;
546}
547
548static void
549GetCursorPosition(WindowInfo *window, int pos[2])
550{
551 RECT rect;
552 POINT point;
553 GLint size[2], x, y;
554 unsigned int NativeHeight, NativeWidth, ChromiumHeight, ChromiumWidth;
555 float WidthRatio, HeightRatio;
556 static int DebugFlag = 0;
557
558 // apparently the "window" parameter passed to this
559 // function contains the native window information
560 HWND NATIVEhwnd = WindowFromDC( window->drawable );
561
562 // get the native window's height and width
563 stubGetWindowGeometry(window, &x, &y, &NativeWidth, &NativeHeight);
564
565 // get the spu window's height and width
566 stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, window->spuWindow, GL_INT, 2, size);
567 ChromiumWidth = size[0];
568 ChromiumHeight = size[1];
569
570 // get the ratio of the size of the native window to the cr window
571 WidthRatio = (float)ChromiumWidth / (float)NativeWidth;
572 HeightRatio = (float)ChromiumHeight / (float)NativeHeight;
573
574 // output some debug information at the beginning
575 if(DebugFlag)
576 {
577 DebugFlag = 0;
578 crDebug("Native Window Handle = %d", NATIVEhwnd);
579 crDebug("Native Width = %i", NativeWidth);
580 crDebug("Native Height = %i", NativeHeight);
581 crDebug("Chromium Width = %i", ChromiumWidth);
582 crDebug("Chromium Height = %i", ChromiumHeight);
583 }
584
585 if (NATIVEhwnd)
586 {
587 GetClientRect( NATIVEhwnd, &rect );
588 GetCursorPos (&point);
589
590 // make sure these coordinates are relative to the native window,
591 // not the whole desktop
592 ScreenToClient(NATIVEhwnd, &point);
593
594 // calculate the new position of the virtual cursor
595 pos[0] = (int)(point.x * WidthRatio);
596 pos[1] = (int)((NativeHeight - point.y) * HeightRatio);
597 }
598 else
599 {
600 pos[0] = 0;
601 pos[1] = 0;
602 }
603}
604
605#elif defined(Darwin)
606
607extern OSStatus CGSGetScreenRectForWindow( CGSConnectionID cid, CGSWindowID wid, float *outRect );
608extern OSStatus CGSGetWindowBounds( CGSConnectionID cid, CGSWindowID wid, float *bounds );
609
610void
611stubGetWindowGeometry( const WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h )
612{
613 float rect[4];
614
615 if( !window ||
616 !window->connection ||
617 !window->drawable ||
618 CGSGetWindowBounds( window->connection, window->drawable, rect ) != noErr )
619 {
620 *x = *y = 0;
621 *w = *h = 0;
622 } else {
623 *x = (int) rect[0];
624 *y = (int) rect[1];
625 *w = (int) rect[2];
626 *h = (int) rect[3];
627 }
628}
629
630
631static void
632GetWindowTitle( const WindowInfo *window, char *title )
633{
634 /* XXX \todo Darwin window Title */
635 title[0] = '\0';
636}
637
638
639static void
640GetCursorPosition( const WindowInfo *window, int pos[2] )
641{
642 Point mouse_pos;
643 float window_rect[4];
644
645 GetMouse( &mouse_pos );
646 CGSGetScreenRectForWindow( window->connection, window->drawable, window_rect );
647
648 pos[0] = mouse_pos.h - (int) window_rect[0];
649 pos[1] = (int) window_rect[3] - (mouse_pos.v - (int) window_rect[1]);
650
651 /*crDebug( "%i %i", pos[0], pos[1] );*/
652}
653
654#elif defined(GLX)
655
656void
657stubGetWindowGeometry(WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h)
658{
659 Window root, child;
660 unsigned int border, depth;
661 Display *dpy;
662
663 dpy = stubGetWindowDisplay(window);
664
665 //@todo: Performing those checks is expensive operation, especially for simple apps with high FPS.
666 // Disabling those triples glxgears fps, thus using xevents instead of per frame polling is much more preferred.
667 //@todo: Check similar on windows guests, though doubtful as there're no XSync like calls on windows.
668 if (window && dpy)
669 {
670 XLOCK(dpy);
671 }
672
673 if (!window
674 || !dpy
675 || !window->drawable
676 || !XGetGeometry(dpy, window->drawable, &root, x, y, w, h, &border, &depth)
677 || !XTranslateCoordinates(dpy, window->drawable, root, 0, 0, x, y, &child))
678 {
679 crWarning("Failed to get windows geometry for %p, try xwininfo", window);
680 *x = *y = 0;
681 *w = *h = 0;
682 }
683
684 if (window && dpy)
685 {
686 XUNLOCK(dpy);
687 }
688}
689
690static char *
691GetWindowTitleHelper( Display *dpy, Window window, GLboolean recurseUp )
692{
693 while (1) {
694 char *name;
695 if (!XFetchName(dpy, window, &name))
696 return NULL;
697 if (name[0]) {
698 return name;
699 }
700 else if (recurseUp) {
701 /* This window has no name, try the parent */
702 Status stat;
703 Window root, parent, *children;
704 unsigned int numChildren;
705 stat = XQueryTree( dpy, window, &root, &parent,
706 &children, &numChildren );
707 if (!stat || window == root)
708 return NULL;
709 if (children)
710 XFree(children);
711 window = parent;
712 }
713 else {
714 XFree(name);
715 return NULL;
716 }
717 }
718}
719
720static void
721GetWindowTitle( const WindowInfo *window, char *title )
722{
723 char *t = GetWindowTitleHelper(window->dpy, window->drawable, GL_TRUE);
724 if (t) {
725 crStrcpy(title, t);
726 XFree(t);
727 }
728 else {
729 title[0] = 0;
730 }
731}
732
733
734/**
735 *Return current cursor position in local window coords.
736 */
737static void
738GetCursorPosition(WindowInfo *window, int pos[2] )
739{
740 int rootX, rootY;
741 Window root, child;
742 unsigned int mask;
743 int x, y;
744
745 XLOCK(window->dpy);
746
747 Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
748 &rootX, &rootY, &pos[0], &pos[1], &mask);
749 if (q) {
750 unsigned int w, h;
751 stubGetWindowGeometry( window, &x, &y, &w, &h );
752 /* invert Y */
753 pos[1] = (int) h - pos[1] - 1;
754 }
755 else {
756 pos[0] = pos[1] = 0;
757 }
758
759 XUNLOCK(window->dpy);
760}
761
762#endif
763
764
765/**
766 * This function is called by MakeCurrent() and determines whether or
767 * not a new rendering context should be bound to Chromium or the native
768 * OpenGL.
769 * \return GL_FALSE if native OpenGL should be used, or GL_TRUE if Chromium
770 * should be used.
771 */
772static GLboolean
773stubCheckUseChromium( WindowInfo *window )
774{
775 int x, y;
776 unsigned int w, h;
777
778 /* If the provided window is CHROMIUM, we're clearly intended
779 * to create a CHROMIUM context.
780 */
781 if (window->type == CHROMIUM)
782 return GL_TRUE;
783
784 if (stub.ignoreFreeglutMenus) {
785 const char *glutMenuTitle = "freeglut menu";
786 char title[1000];
787 GetWindowTitle(window, title);
788 if (crStrcmp(title, glutMenuTitle) == 0) {
789 crDebug("GL faker: Ignoring freeglut menu window");
790 return GL_FALSE;
791 }
792 }
793
794 /* If the user's specified a window count for Chromium, see if
795 * this window satisfies that criterium.
796 */
797 stub.matchChromiumWindowCounter++;
798 if (stub.matchChromiumWindowCount > 0) {
799 if (stub.matchChromiumWindowCounter != stub.matchChromiumWindowCount) {
800 crDebug("Using native GL, app window doesn't meet match_window_count");
801 return GL_FALSE;
802 }
803 }
804
805 /* If the user's specified a window list to ignore, see if this
806 * window satisfies that criterium.
807 */
808 if (stub.matchChromiumWindowID) {
809 GLuint i;
810
811 for (i = 0; i <= stub.numIgnoreWindowID; i++) {
812 if (stub.matchChromiumWindowID[i] == stub.matchChromiumWindowCounter) {
813 crDebug("Ignore window ID %d, using native GL", stub.matchChromiumWindowID[i]);
814 return GL_FALSE;
815 }
816 }
817 }
818
819 /* If the user's specified a minimum window size for Chromium, see if
820 * this window satisfies that criterium.
821 */
822 if (stub.minChromiumWindowWidth > 0 &&
823 stub.minChromiumWindowHeight > 0) {
824 stubGetWindowGeometry( window, &x, &y, &w, &h );
825 if (w >= stub.minChromiumWindowWidth &&
826 h >= stub.minChromiumWindowHeight) {
827
828 /* Check for maximum sized window now too */
829 if (stub.maxChromiumWindowWidth &&
830 stub.maxChromiumWindowHeight) {
831 if (w < stub.maxChromiumWindowWidth &&
832 h < stub.maxChromiumWindowHeight)
833 return GL_TRUE;
834 else
835 return GL_FALSE;
836 }
837
838 return GL_TRUE;
839 }
840 crDebug("Using native GL, app window doesn't meet minimum_window_size");
841 return GL_FALSE;
842 }
843 else if (stub.matchWindowTitle) {
844 /* If the user's specified a window title for Chromium, see if this
845 * window satisfies that criterium.
846 */
847 GLboolean wildcard = GL_FALSE;
848 char title[1000];
849 char *titlePattern;
850 int len;
851 /* check for leading '*' wildcard */
852 if (stub.matchWindowTitle[0] == '*') {
853 titlePattern = crStrdup( stub.matchWindowTitle + 1 );
854 wildcard = GL_TRUE;
855 }
856 else {
857 titlePattern = crStrdup( stub.matchWindowTitle );
858 }
859 /* check for trailing '*' wildcard */
860 len = crStrlen(titlePattern);
861 if (len > 0 && titlePattern[len - 1] == '*') {
862 titlePattern[len - 1] = '\0'; /* terminate here */
863 wildcard = GL_TRUE;
864 }
865
866 GetWindowTitle( window, title );
867 if (title[0]) {
868 if (wildcard) {
869 if (crStrstr(title, titlePattern)) {
870 crFree(titlePattern);
871 return GL_TRUE;
872 }
873 }
874 else if (crStrcmp(title, titlePattern) == 0) {
875 crFree(titlePattern);
876 return GL_TRUE;
877 }
878 }
879 crFree(titlePattern);
880 crDebug("Using native GL, app window title doesn't match match_window_title string (\"%s\" != \"%s\")", title, stub.matchWindowTitle);
881 return GL_FALSE;
882 }
883
884 /* Window title and size don't matter */
885 CRASSERT(stub.minChromiumWindowWidth == 0);
886 CRASSERT(stub.minChromiumWindowHeight == 0);
887 CRASSERT(stub.matchWindowTitle == NULL);
888
889 /* User hasn't specified a width/height or window title.
890 * We'll use chromium for this window (and context) if no other is.
891 */
892
893 return GL_TRUE; /* use Chromium! */
894}
895
896static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2)
897{
898 WindowInfo *pWindow = (WindowInfo *) data1;
899 ContextInfo *pCtx = (ContextInfo *) data2;
900
901 if (pWindow->pOwner == pCtx)
902 {
903#ifdef WINDOWS
904 /* Note: can't use WindowFromDC(context->pOwnWindow->drawable) here
905 because GL context is already released from DC and actual guest window
906 could be destroyed.
907 */
908 crWindowDestroy((GLint)pWindow->hWnd);
909#else
910 crWindowDestroy((GLint)pWindow->drawable);
911#endif
912 }
913}
914
915GLboolean
916stubMakeCurrent( WindowInfo *window, ContextInfo *context )
917{
918 GLboolean retVal;
919
920 /*
921 * Get WindowInfo and ContextInfo pointers.
922 */
923
924 if (!context || !window) {
925 if (stub.currentContext)
926 stub.currentContext->currentDrawable = NULL;
927 if (context)
928 context->currentDrawable = NULL;
929 stub.currentContext = NULL;
930 return GL_TRUE; /* OK */
931 }
932
933#ifdef CHROMIUM_THREADSAFE
934 stubCheckMultithread();
935#endif
936
937 if (context->type == UNDECIDED) {
938 /* Here's where we really create contexts */
939#ifdef CHROMIUM_THREADSAFE
940 crLockMutex(&stub.mutex);
941#endif
942
943 if (stubCheckUseChromium(window)) {
944 /*
945 * Create a Chromium context.
946 */
947#if defined(GLX) || defined(DARWIN)
948 GLint spuShareCtx = context->share ? context->share->spuContext : 0;
949#else
950 GLint spuShareCtx = 0;
951#endif
952
953 CRASSERT(stub.spu);
954 CRASSERT(stub.spu->dispatch_table.CreateContext);
955 context->type = CHROMIUM;
956
957 context->spuContext
958 = stub.spu->dispatch_table.CreateContext( context->dpyName,
959 context->visBits,
960 spuShareCtx );
961 if (window->spuWindow == -1)
962 {
963 /*crDebug("(1)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
964 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
965#ifdef CR_NEWWINTRACK
966 window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
967#endif
968 }
969 }
970 else {
971 /*
972 * Create a native OpenGL context.
973 */
974 if (!InstantiateNativeContext(window, context))
975 {
976#ifdef CHROMIUM_THREADSAFE
977 crUnlockMutex(&stub.mutex);
978#endif
979 return 0; /* false */
980 }
981 context->type = NATIVE;
982 }
983
984#ifdef CHROMIUM_THREADSAFE
985 crUnlockMutex(&stub.mutex);
986#endif
987 }
988
989
990 if (context->type == NATIVE) {
991 /*
992 * Native OpenGL MakeCurrent().
993 */
994#ifdef WINDOWS
995 retVal = (GLboolean) stub.wsInterface.wglMakeCurrent( window->drawable, context->hglrc );
996#elif defined(Darwin)
997 // XXX \todo We need to differentiate between these two..
998 retVal = ( stub.wsInterface.CGLSetSurface(context->cglc, window->connection, window->drawable, window->surface) == noErr );
999 retVal = ( stub.wsInterface.CGLSetCurrentContext(context->cglc) == noErr );
1000#elif defined(GLX)
1001 retVal = (GLboolean) stub.wsInterface.glXMakeCurrent( window->dpy, window->drawable, context->glxContext );
1002#endif
1003 }
1004 else {
1005 /*
1006 * SPU chain MakeCurrent().
1007 */
1008 CRASSERT(context->type == CHROMIUM);
1009 CRASSERT(context->spuContext >= 0);
1010
1011 /*if (context->currentDrawable && context->currentDrawable != window)
1012 crDebug("Rebinding context %p to a different window", context);*/
1013
1014 if (window->type == NATIVE) {
1015 crWarning("Can't rebind a chromium context to a native window\n");
1016 retVal = 0;
1017 }
1018 else {
1019 if (window->spuWindow == -1)
1020 {
1021 /*crDebug("(2)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
1022 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
1023#ifdef CR_NEWWINTRACK
1024 window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
1025#endif
1026 if (context->currentDrawable && context->currentDrawable->type==CHROMIUM
1027 && context->currentDrawable->pOwner==context)
1028 {
1029#ifdef WINDOWS
1030 if (!WindowFromDC(context->currentDrawable->drawable))
1031 {
1032 crWindowDestroy((GLint)context->currentDrawable->hWnd);
1033 }
1034#else
1035 Window root;
1036 int x, y;
1037 unsigned int border, depth, w, h;
1038
1039 XLOCK(context->currentDrawable->dpy);
1040 if (!XGetGeometry(context->currentDrawable->dpy, context->currentDrawable->drawable, &root, &x, &y, &w, &h, &border, &depth))
1041 {
1042 crWindowDestroy((GLint)context->currentDrawable->drawable);
1043 }
1044 XUNLOCK(context->currentDrawable->dpy);
1045#endif
1046
1047 }
1048 }
1049
1050 if (window->spuWindow != (GLint)window->drawable)
1051 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, (GLint) window->drawable, context->spuContext );
1052 else
1053 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, 0, /* native window handle */ context->spuContext );
1054
1055 retVal = 1;
1056 }
1057 }
1058
1059 window->type = context->type;
1060 window->pOwner = context;
1061 context->currentDrawable = window;
1062 stub.currentContext = context;
1063
1064 if (retVal) {
1065 /* Now, if we've transitions from Chromium to native rendering, or
1066 * vice versa, we have to change all the OpenGL entrypoint pointers.
1067 */
1068 if (context->type == NATIVE) {
1069 /* Switch to native API */
1070 /*printf(" Switching to native API\n");*/
1071 stubSetDispatch(&stub.nativeDispatch);
1072 }
1073 else if (context->type == CHROMIUM) {
1074 /* Switch to stub (SPU) API */
1075 /*printf(" Switching to spu API\n");*/
1076 stubSetDispatch(&stub.spuDispatch);
1077 }
1078 else {
1079 /* no API switch needed */
1080 }
1081 }
1082
1083 if (!window->width && window->type == CHROMIUM) {
1084 /* One time window setup */
1085 int x, y;
1086 unsigned int winW, winH;
1087
1088 stubGetWindowGeometry( window, &x, &y, &winW, &winH );
1089
1090 /* If we're not using GLX/WGL (no app window) we'll always get
1091 * a width and height of zero here. In that case, skip the viewport
1092 * call since we're probably using a tilesort SPU with fake_window_dims
1093 * which the tilesort SPU will use for the viewport.
1094 */
1095 window->width = winW;
1096 window->height = winH;
1097 if (stub.trackWindowSize)
1098 stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
1099 if (stub.trackWindowPos)
1100 stub.spuDispatch.WindowPosition(window->spuWindow, x, y);
1101 if (winW > 0 && winH > 0)
1102 stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
1103#ifdef VBOX_WITH_WDDM
1104 stub.spu->dispatch_table.WindowVisibleRegion(window->spuWindow, 0, NULL);
1105#endif
1106 }
1107
1108 /* Update window mapping state.
1109 * Basically, this lets us hide render SPU windows which correspond
1110 * to unmapped application windows. Without this, "pertly" (for example)
1111 * opens *lots* of temporary windows which otherwise clutter the screen.
1112 */
1113 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
1114 const int mapped = stubIsWindowVisible(window);
1115 if (mapped != window->mapped) {
1116 crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
1117 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
1118 window->mapped = mapped;
1119 }
1120 }
1121
1122 return retVal;
1123}
1124
1125void
1126stubDestroyContext( unsigned long contextId )
1127{
1128 ContextInfo *context;
1129
1130 if (!stub.contextTable) {
1131 return;
1132 }
1133 context = (ContextInfo *) crHashtableSearch(stub.contextTable, contextId);
1134
1135 CRASSERT(context);
1136
1137 if (context->type == NATIVE) {
1138#ifdef WINDOWS
1139 stub.wsInterface.wglDeleteContext( context->hglrc );
1140#elif defined(Darwin)
1141 stub.wsInterface.CGLDestroyContext( context->cglc );
1142#elif defined(GLX)
1143 stub.wsInterface.glXDestroyContext( context->dpy, context->glxContext );
1144#endif
1145 }
1146 else if (context->type == CHROMIUM) {
1147 /* Have pack SPU or tilesort SPU, etc. destroy the context */
1148 CRASSERT(context->spuContext >= 0);
1149 stub.spu->dispatch_table.DestroyContext( context->spuContext );
1150 crHashtableWalk(stub.windowTable, stubWindowCheckOwnerCB, context);
1151 }
1152
1153 if (stub.currentContext == context) {
1154 stub.currentContext = NULL;
1155 }
1156
1157#ifdef GLX
1158 crFreeHashtable(context->pGLXPixmapsHash, crFree);
1159 if (context->damageDpy)
1160 {
1161 XCloseDisplay(context->damageDpy);
1162 }
1163#endif
1164
1165 crMemZero(context, sizeof(ContextInfo)); /* just to be safe */
1166 crHashtableDelete(stub.contextTable, contextId, crFree);
1167}
1168
1169
1170void
1171stubSwapBuffers(WindowInfo *window, GLint flags)
1172{
1173 if (!window)
1174 return;
1175
1176 /* Determine if this window is being rendered natively or through
1177 * Chromium.
1178 */
1179
1180 if (window->type == NATIVE) {
1181 /*printf("*** Swapping native window %d\n", (int) drawable);*/
1182#ifdef WINDOWS
1183 (void) stub.wsInterface.wglSwapBuffers( window->drawable );
1184#elif defined(Darwin)
1185 /* ...is this ok? */
1186/* stub.wsInterface.CGLFlushDrawable( context->cglc ); */
1187 crDebug("stubSwapBuffers: unable to swap (no context!)");
1188#elif defined(GLX)
1189 stub.wsInterface.glXSwapBuffers( window->dpy, window->drawable );
1190#endif
1191 }
1192 else if (window->type == CHROMIUM) {
1193 /* Let the SPU do the buffer swap */
1194 /*printf("*** Swapping chromium window %d\n", (int) drawable);*/
1195 if (stub.appDrawCursor) {
1196 int pos[2];
1197 GetCursorPosition(window, pos);
1198 stub.spu->dispatch_table.ChromiumParametervCR(GL_CURSOR_POSITION_CR, GL_INT, 2, pos);
1199 }
1200 stub.spu->dispatch_table.SwapBuffers( window->spuWindow, flags );
1201 }
1202 else {
1203 crDebug("Calling SwapBuffers on a window we haven't seen before (no-op).");
1204 }
1205}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use