VirtualBox

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

Last change on this file since 42903 was 42520, checked in by vboxsync, 13 years ago

crOpenGL: debug assertion fixes

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