VirtualBox

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

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

crOpenGL/wddm: basics for miniport-based visible rects processing (guest part); export flushToHost command

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