VirtualBox

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

Last change on this file since 47161 was 47161, checked in by vboxsync, 11 years ago

crOpenGL: fix wddm init & win state handling

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