VirtualBox

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

Last change on this file since 30457 was 30440, checked in by vboxsync, 15 years ago

crOpenGL: wddm friendly windows info tracking + more consistent updates (disabled except windows yet)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 32.5 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7/**
8 * \mainpage OpenGL_stub
9 *
10 * \section OpenGL_stubIntroduction Introduction
11 *
12 * Chromium consists of all the top-level files in the cr
13 * directory. The OpenGL_stub module basically takes care of API dispatch,
14 * and OpenGL state management.
15 *
16 */
17
18/**
19 * This file manages OpenGL rendering contexts in the faker library.
20 * The big issue is switching between Chromium and native GL context
21 * management. This is where we support multiple client OpenGL
22 * windows. Typically, one window is handled by Chromium while any
23 * other windows are handled by the native OpenGL library.
24 */
25
26#include "chromium.h"
27#include "cr_error.h"
28#include "cr_spu.h"
29#include "cr_mem.h"
30#include "cr_string.h"
31#include "cr_environment.h"
32#include "stub.h"
33
34/**
35 * This function should be called from MakeCurrent(). It'll detect if
36 * we're in a multi-thread situation, and do the right thing for dispatch.
37 */
38#ifdef CHROMIUM_THREADSAFE
39 static void
40stubCheckMultithread( void )
41{
42 static unsigned long knownID;
43 static GLboolean firstCall = GL_TRUE;
44
45 if (stub.threadSafe)
46 return; /* nothing new, nothing to do */
47
48 if (firstCall) {
49 knownID = crThreadID();
50 firstCall = GL_FALSE;
51 }
52 else if (knownID != crThreadID()) {
53 /* going thread-safe now! */
54 stub.threadSafe = GL_TRUE;
55 crSPUCopyDispatchTable(&glim, &stubThreadsafeDispatch);
56 }
57}
58#endif
59
60
61/**
62 * Install the given dispatch table as the table used for all gl* calls.
63 */
64 static void
65stubSetDispatch( SPUDispatchTable *table )
66{
67 CRASSERT(table);
68
69#ifdef CHROMIUM_THREADSAFE
70 /* always set the per-thread dispatch pointer */
71 crSetTSD(&stub.dispatchTSD, (void *) table);
72 if (stub.threadSafe) {
73 /* Do nothing - the thread-safe dispatch functions will call GetTSD()
74 * to get a pointer to the dispatch table, and jump through it.
75 */
76 }
77 else
78#endif
79 {
80 /* Single thread mode - just install the caller's dispatch table */
81 /* This conditional is an optimization to try to avoid unnecessary
82 * copying. It seems to work with atlantis, multiwin, etc. but
83 * _could_ be a problem. (Brian)
84 */
85 if (glim.copy_of != table->copy_of)
86 crSPUCopyDispatchTable(&glim, table);
87 }
88}
89
90
91/**
92 * Create a new _Chromium_ window, not GLX, WGL or CGL.
93 * Called by crWindowCreate() only.
94 */
95 GLint
96stubNewWindow( const char *dpyName, GLint visBits )
97{
98 WindowInfo *winInfo;
99 GLint spuWin, size[2];
100
101 spuWin = stub.spu->dispatch_table.WindowCreate( dpyName, visBits );
102 if (spuWin < 0) {
103 return -1;
104 }
105
106 winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
107 if (!winInfo) {
108 stub.spu->dispatch_table.WindowDestroy(spuWin);
109 return -1;
110 }
111
112 winInfo->type = CHROMIUM;
113
114 /* Ask the head SPU for the initial window size */
115 size[0] = size[1] = 0;
116 stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, 0, GL_INT, 2, size);
117 if (size[0] == 0 && size[1] == 0) {
118 /* use some reasonable defaults */
119 size[0] = size[1] = 512;
120 }
121 winInfo->width = size[0];
122 winInfo->height = size[1];
123 winInfo->mapped = 1;
124
125 if (!dpyName)
126 dpyName = "";
127
128 crStrncpy(winInfo->dpyName, dpyName, MAX_DPY_NAME);
129 winInfo->dpyName[MAX_DPY_NAME-1] = 0;
130
131 /* Use spuWin as the hash table index and GLX/WGL handle */
132#ifdef WINDOWS
133 winInfo->drawable = (HDC) spuWin;
134 winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
135#elif defined(Darwin)
136 winInfo->drawable = (CGSWindowID) spuWin;
137#elif defined(GLX)
138 winInfo->drawable = (GLXDrawable) spuWin;
139 winInfo->pVisibleRegions = NULL;
140 winInfo->cVisibleRegions = 0;
141#endif
142#ifdef CR_NEWWINTRACK
143 winInfo->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
144#endif
145 winInfo->spuWindow = spuWin;
146
147 crHashtableAdd(stub.windowTable, (unsigned int) spuWin, winInfo);
148
149 return spuWin;
150}
151
152
153GLboolean
154stubIsWindowVisible( const WindowInfo *win )
155{
156#if defined(WINDOWS)
157 return GL_TRUE;
158#elif defined(Darwin)
159 return GL_TRUE;
160#elif defined(GLX)
161 if (win->dpy) {
162 XWindowAttributes attr;
163 XLOCK(win->dpy);
164 XGetWindowAttributes(win->dpy, win->drawable, &attr);
165 XUNLOCK(win->dpy);
166 return (attr.map_state != IsUnmapped);
167 }
168 else {
169 /* probably created by crWindowCreate() */
170 return win->mapped;
171 }
172#endif
173}
174
175
176/**
177 * Given a Windows HDC or GLX Drawable, return the corresponding
178 * WindowInfo structure. Create a new one if needed.
179 */
180WindowInfo *
181#ifdef WINDOWS
182 stubGetWindowInfo( HDC drawable )
183#elif defined(Darwin)
184 stubGetWindowInfo( CGSWindowID drawable )
185#elif defined(GLX)
186stubGetWindowInfo( Display *dpy, GLXDrawable drawable )
187#endif
188{
189#ifndef WINDOWS
190 WindowInfo *winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) drawable);
191#else
192 WindowInfo *winInfo;
193 HWND hwnd;
194 hwnd = WindowFromDC(drawable);
195
196 if (!hwnd)
197 {
198 return NULL;
199 }
200
201 winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) hwnd);
202#endif
203 if (!winInfo) {
204 winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
205 if (!winInfo)
206 return NULL;
207#ifdef GLX
208 crStrncpy(winInfo->dpyName, DisplayString(dpy), MAX_DPY_NAME);
209 winInfo->dpyName[MAX_DPY_NAME-1] = 0;
210 winInfo->dpy = dpy;
211 winInfo->pVisibleRegions = NULL;
212#elif defined(Darwin)
213 winInfo->connection = _CGSDefaultConnection(); // store our connection as default
214#elif defined(WINDOWS)
215 winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
216 winInfo->hWnd = hwnd;
217#endif
218 winInfo->drawable = drawable;
219 winInfo->type = UNDECIDED;
220 winInfo->spuWindow = -1;
221 winInfo->mapped = -1; /* don't know */
222 winInfo->pOwner = NULL;
223#ifdef CR_NEWWINTRACK
224 winInfo->u32ClientID = -1;
225#endif
226#ifndef WINDOWS
227 crHashtableAdd(stub.windowTable, (unsigned int) drawable, winInfo);
228#else
229 crHashtableAdd(stub.windowTable, (unsigned int) hwnd, winInfo);
230#endif
231 }
232 return winInfo;
233}
234
235
236/**
237 * Allocate a new ContextInfo object, initialize it, put it into the
238 * context hash table. If type==CHROMIUM, call the head SPU's
239 * CreateContext() function too.
240 */
241 ContextInfo *
242stubNewContext( const char *dpyName, GLint visBits, ContextType type,
243 unsigned long shareCtx )
244{
245 GLint spuContext = -1, spuShareCtx = 0;
246 ContextInfo *context;
247
248 if (shareCtx > 0) {
249 /* translate shareCtx to a SPU context ID */
250 context = (ContextInfo *)
251 crHashtableSearch(stub.contextTable, shareCtx);
252 if (context)
253 spuShareCtx = context->spuContext;
254 }
255
256 if (type == CHROMIUM) {
257 spuContext
258 = stub.spu->dispatch_table.CreateContext(dpyName, visBits, spuShareCtx);
259 if (spuContext < 0)
260 return NULL;
261 }
262
263 context = crCalloc(sizeof(ContextInfo));
264 if (!context) {
265 stub.spu->dispatch_table.DestroyContext(spuContext);
266 return NULL;
267 }
268
269 if (!dpyName)
270 dpyName = "";
271
272 context->id = stub.freeContextNumber++;
273 context->type = type;
274 context->spuContext = spuContext;
275 context->visBits = visBits;
276 context->currentDrawable = NULL;
277 crStrncpy(context->dpyName, dpyName, MAX_DPY_NAME);
278 context->dpyName[MAX_DPY_NAME-1] = 0;
279
280#if defined(GLX) || defined(DARWIN)
281 context->share = (ContextInfo *)
282 crHashtableSearch(stub.contextTable, (unsigned long) shareCtx);
283#endif
284
285#ifdef GLX
286 context->pGLXPixmapsHash = crAllocHashtable();
287 context->damageInitFailed = GL_FALSE;
288 context->damageDpy = NULL;
289 context->damageEventsBase = 0;
290#endif
291
292 crHashtableAdd(stub.contextTable, context->id, (void *) context);
293
294 return context;
295}
296
297
298#ifdef Darwin
299
300#define SET_ATTR(l,i,a) ( (l)[(i)++] = (a) )
301#define SET_ATTR_V(l,i,a,v) ( SET_ATTR(l,i,a), SET_ATTR(l,i,v) )
302
303void stubSetPFA( ContextInfo *ctx, CGLPixelFormatAttribute *attribs, int size, GLint *num ) {
304 GLuint visual = ctx->visBits;
305 int i = 0;
306
307 CRASSERT(visual & CR_RGB_BIT);
308
309 SET_ATTR_V(attribs, i, kCGLPFAColorSize, 8);
310
311 if( visual & CR_DEPTH_BIT )
312 SET_ATTR_V(attribs, i, kCGLPFADepthSize, 16);
313
314 if( visual & CR_ACCUM_BIT )
315 SET_ATTR_V(attribs, i, kCGLPFAAccumSize, 1);
316
317 if( visual & CR_STENCIL_BIT )
318 SET_ATTR_V(attribs, i, kCGLPFAStencilSize, 1);
319
320 if( visual & CR_ALPHA_BIT )
321 SET_ATTR_V(attribs, i, kCGLPFAAlphaSize, 1);
322
323 if( visual & CR_DOUBLE_BIT )
324 SET_ATTR(attribs, i, kCGLPFADoubleBuffer);
325
326 if( visual & CR_STEREO_BIT )
327 SET_ATTR(attribs, i, kCGLPFAStereo);
328
329/* SET_ATTR_V(attribs, i, kCGLPFASampleBuffers, 1);
330 SET_ATTR_V(attribs, i, kCGLPFASamples, 0);
331 SET_ATTR_V(attribs, i, kCGLPFADisplayMask, 0); */
332 SET_ATTR(attribs, i, kCGLPFABackingStore);
333 SET_ATTR(attribs, i, kCGLPFAWindow);
334 SET_ATTR_V(attribs, i, kCGLPFADisplayMask, ctx->disp_mask);
335
336 SET_ATTR(attribs, i, 0);
337
338 *num = i;
339}
340
341#endif
342
343/**
344 * This creates a native GLX/WGL context.
345 */
346static GLboolean
347InstantiateNativeContext( WindowInfo *window, ContextInfo *context )
348{
349#ifdef WINDOWS
350 context->hglrc = stub.wsInterface.wglCreateContext( window->drawable );
351 return context->hglrc ? GL_TRUE : GL_FALSE;
352#elif defined(Darwin)
353 CGLContextObj shareCtx = NULL;
354 CGLPixelFormatObj pix;
355 long npix;
356
357 CGLPixelFormatAttribute attribs[16];
358 GLint ind = 0;
359
360 if( context->share ) {
361 if( context->cglc != context->share->cglc ) {
362 crWarning("CGLCreateContext() is trying to share a non-existant "
363 "CGL context. Setting share context to zero.");
364 shareCtx = 0;
365 }
366 else
367 shareCtx = context->cglc;
368 }
369
370 stubSetPFA( context, attribs, 16, &ind );
371
372 stub.wsInterface.CGLChoosePixelFormat( attribs, &pix, &npix );
373 stub.wsInterface.CGLCreateContext( pix, shareCtx, &context->cglc );
374 if( !context->cglc )
375 crError("InstantiateNativeContext: Couldn't Create the context!");
376
377 stub.wsInterface.CGLDestroyPixelFormat( pix );
378
379 if( context->parambits ) {
380 /* Set the delayed parameters */
381 if( context->parambits & VISBIT_SWAP_RECT )
382 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapRectangle, context->swap_rect );
383
384 if( context->parambits & VISBIT_SWAP_INTERVAL )
385 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapInterval, &(context->swap_interval) );
386
387 if( context->parambits & VISBIT_CLIENT_STORAGE )
388 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPClientStorage, (long*)&(context->client_storage) );
389
390 context->parambits = 0;
391 }
392
393 return context->cglc ? GL_TRUE : GL_FALSE;
394#elif defined(GLX)
395 GLXContext shareCtx = 0;
396
397 /* sort out context sharing here */
398 if (context->share) {
399 if (context->glxContext != context->share->glxContext) {
400 crWarning("glXCreateContext() is trying to share a non-existant "
401 "GLX context. Setting share context to zero.");
402 shareCtx = 0;
403 }
404 else {
405 shareCtx = context->glxContext;
406 }
407 }
408
409 context->glxContext = stub.wsInterface.glXCreateContext( window->dpy,
410 context->visual, shareCtx, context->direct );
411
412 return context->glxContext ? GL_TRUE : GL_FALSE;
413#endif
414}
415
416
417/**
418 * Utility functions to get window size and titlebar text.
419 */
420#ifdef WINDOWS
421
422void
423stubGetWindowGeometry( const WindowInfo *window, int *x, int *y,
424 unsigned int *w, unsigned int *h )
425{
426 RECT rect;
427 HWND hwnd;
428
429 if (!window->drawable) {
430 *w = *h = 0;
431 return;
432 }
433
434 hwnd = WindowFromDC( window->drawable );
435
436 if (!hwnd) {
437 *w = 0;
438 *h = 0;
439 }
440 else {
441 GetClientRect( hwnd, &rect );
442 *w = rect.right - rect.left;
443 *h = rect.bottom - rect.top;
444 ClientToScreen( hwnd, (LPPOINT) &rect );
445 *x = rect.left;
446 *y = rect.top;
447 }
448}
449
450static void
451GetWindowTitle( const WindowInfo *window, char *title )
452{
453 HWND hwnd;
454 /* XXX - we don't handle recurseUp */
455 hwnd = WindowFromDC( window->drawable );
456 if (hwnd)
457 GetWindowText(hwnd, title, 100);
458 else
459 title[0] = 0;
460}
461
462static void
463GetCursorPosition( const WindowInfo *window, int pos[2] )
464{
465 RECT rect;
466 POINT point;
467 GLint size[2], x, y;
468 unsigned int NativeHeight, NativeWidth, ChromiumHeight, ChromiumWidth;
469 float WidthRatio, HeightRatio;
470 static int DebugFlag = 0;
471
472 // apparently the "window" parameter passed to this
473 // function contains the native window information
474 HWND NATIVEhwnd = WindowFromDC( window->drawable );
475
476 // get the native window's height and width
477 stubGetWindowGeometry(window, &x, &y, &NativeWidth, &NativeHeight);
478
479 // get the spu window's height and width
480 stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, window->spuWindow, GL_INT, 2, size);
481 ChromiumWidth = size[0];
482 ChromiumHeight = size[1];
483
484 // get the ratio of the size of the native window to the cr window
485 WidthRatio = (float)ChromiumWidth / (float)NativeWidth;
486 HeightRatio = (float)ChromiumHeight / (float)NativeHeight;
487
488 // output some debug information at the beginning
489 if(DebugFlag)
490 {
491 DebugFlag = 0;
492 crDebug("Native Window Handle = %d", NATIVEhwnd);
493 crDebug("Native Width = %i", NativeWidth);
494 crDebug("Native Height = %i", NativeHeight);
495 crDebug("Chromium Width = %i", ChromiumWidth);
496 crDebug("Chromium Height = %i", ChromiumHeight);
497 }
498
499 if (NATIVEhwnd)
500 {
501 GetClientRect( NATIVEhwnd, &rect );
502 GetCursorPos (&point);
503
504 // make sure these coordinates are relative to the native window,
505 // not the whole desktop
506 ScreenToClient(NATIVEhwnd, &point);
507
508 // calculate the new position of the virtual cursor
509 pos[0] = (int)(point.x * WidthRatio);
510 pos[1] = (int)((NativeHeight - point.y) * HeightRatio);
511 }
512 else
513 {
514 pos[0] = 0;
515 pos[1] = 0;
516 }
517}
518
519#elif defined(Darwin)
520
521extern OSStatus CGSGetScreenRectForWindow( CGSConnectionID cid, CGSWindowID wid, float *outRect );
522extern OSStatus CGSGetWindowBounds( CGSConnectionID cid, CGSWindowID wid, float *bounds );
523
524void
525stubGetWindowGeometry( const WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h )
526{
527 float rect[4];
528
529 if( !window ||
530 !window->connection ||
531 !window->drawable ||
532 CGSGetWindowBounds( window->connection, window->drawable, rect ) != noErr )
533 {
534 *x = *y = 0;
535 *w = *h = 0;
536 } else {
537 *x = (int) rect[0];
538 *y = (int) rect[1];
539 *w = (int) rect[2];
540 *h = (int) rect[3];
541 }
542}
543
544
545static void
546GetWindowTitle( const WindowInfo *window, char *title )
547{
548 /* XXX \todo Darwin window Title */
549 title[0] = '\0';
550}
551
552
553static void
554GetCursorPosition( const WindowInfo *window, int pos[2] )
555{
556 Point mouse_pos;
557 float window_rect[4];
558
559 GetMouse( &mouse_pos );
560 CGSGetScreenRectForWindow( window->connection, window->drawable, window_rect );
561
562 pos[0] = mouse_pos.h - (int) window_rect[0];
563 pos[1] = (int) window_rect[3] - (mouse_pos.v - (int) window_rect[1]);
564
565 /*crDebug( "%i %i", pos[0], pos[1] );*/
566}
567
568#elif defined(GLX)
569
570void
571stubGetWindowGeometry( const WindowInfo *window, int *x, int *y,
572 unsigned int *w, unsigned int *h )
573{
574 Window root, child;
575 unsigned int border, depth;
576
577 //@todo: Performing those checks is expensive operation, especially for simple apps with high FPS.
578 // Disabling those tripples glxgears fps, thus using xevens instead of per frame polling is much more preffered.
579 //@todo: Check similiar on windows guests, though doubtfull as there're no XSync like calls on windows.
580 if (window && window->dpy)
581 {
582 XLOCK(window->dpy);
583 }
584
585 if (!window
586 || !window->dpy
587 || !window->drawable
588 || !XGetGeometry(window->dpy, window->drawable, &root,
589 x, y, w, h, &border, &depth)
590 || !XTranslateCoordinates(window->dpy, window->drawable, root,
591 0, 0, x, y, &child))
592 {
593 crWarning("Failed to get windows geometry for %p, try xwininfo", window);
594 *x = *y = 0;
595 *w = *h = 0;
596 }
597
598 if (window && window->dpy)
599 {
600 XUNLOCK(window->dpy);
601 }
602}
603
604static char *
605GetWindowTitleHelper( Display *dpy, Window window, GLboolean recurseUp )
606{
607 while (1) {
608 char *name;
609 if (!XFetchName(dpy, window, &name))
610 return NULL;
611 if (name[0]) {
612 return name;
613 }
614 else if (recurseUp) {
615 /* This window has no name, try the parent */
616 Status stat;
617 Window root, parent, *children;
618 unsigned int numChildren;
619 stat = XQueryTree( dpy, window, &root, &parent,
620 &children, &numChildren );
621 if (!stat || window == root)
622 return NULL;
623 if (children)
624 XFree(children);
625 window = parent;
626 }
627 else {
628 XFree(name);
629 return NULL;
630 }
631 }
632}
633
634static void
635GetWindowTitle( const WindowInfo *window, char *title )
636{
637 char *t = GetWindowTitleHelper(window->dpy, window->drawable, GL_TRUE);
638 if (t) {
639 crStrcpy(title, t);
640 XFree(t);
641 }
642 else {
643 title[0] = 0;
644 }
645}
646
647
648/**
649 *Return current cursor position in local window coords.
650 */
651static void
652GetCursorPosition( const WindowInfo *window, int pos[2] )
653{
654 int rootX, rootY;
655 Window root, child;
656 unsigned int mask;
657 int x, y;
658
659 XLOCK(window->dpy);
660
661 Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
662 &rootX, &rootY, &pos[0], &pos[1], &mask);
663 if (q) {
664 unsigned int w, h;
665 stubGetWindowGeometry( window, &x, &y, &w, &h );
666 /* invert Y */
667 pos[1] = (int) h - pos[1] - 1;
668 }
669 else {
670 pos[0] = pos[1] = 0;
671 }
672
673 XUNLOCK(window->dpy);
674}
675
676#endif
677
678
679/**
680 * This function is called by MakeCurrent() and determines whether or
681 * not a new rendering context should be bound to Chromium or the native
682 * OpenGL.
683 * \return GL_FALSE if native OpenGL should be used, or GL_TRUE if Chromium
684 * should be used.
685 */
686static GLboolean
687stubCheckUseChromium( WindowInfo *window )
688{
689 int x, y;
690 unsigned int w, h;
691
692 /* If the provided window is CHROMIUM, we're clearly intended
693 * to create a CHROMIUM context.
694 */
695 if (window->type == CHROMIUM)
696 return GL_TRUE;
697
698 if (stub.ignoreFreeglutMenus) {
699 const char *glutMenuTitle = "freeglut menu";
700 char title[1000];
701 GetWindowTitle(window, title);
702 if (crStrcmp(title, glutMenuTitle) == 0) {
703 crDebug("GL faker: Ignoring freeglut menu window");
704 return GL_FALSE;
705 }
706 }
707
708 /* If the user's specified a window count for Chromium, see if
709 * this window satisfies that criterium.
710 */
711 stub.matchChromiumWindowCounter++;
712 if (stub.matchChromiumWindowCount > 0) {
713 if (stub.matchChromiumWindowCounter != stub.matchChromiumWindowCount) {
714 crDebug("Using native GL, app window doesn't meet match_window_count");
715 return GL_FALSE;
716 }
717 }
718
719 /* If the user's specified a window list to ignore, see if this
720 * window satisfies that criterium.
721 */
722 if (stub.matchChromiumWindowID) {
723 GLuint i;
724
725 for (i = 0; i <= stub.numIgnoreWindowID; i++) {
726 if (stub.matchChromiumWindowID[i] == stub.matchChromiumWindowCounter) {
727 crDebug("Ignore window ID %d, using native GL", stub.matchChromiumWindowID[i]);
728 return GL_FALSE;
729 }
730 }
731 }
732
733 /* If the user's specified a minimum window size for Chromium, see if
734 * this window satisfies that criterium.
735 */
736 if (stub.minChromiumWindowWidth > 0 &&
737 stub.minChromiumWindowHeight > 0) {
738 stubGetWindowGeometry( window, &x, &y, &w, &h );
739 if (w >= stub.minChromiumWindowWidth &&
740 h >= stub.minChromiumWindowHeight) {
741
742 /* Check for maximum sized window now too */
743 if (stub.maxChromiumWindowWidth &&
744 stub.maxChromiumWindowHeight) {
745 if (w < stub.maxChromiumWindowWidth &&
746 h < stub.maxChromiumWindowHeight)
747 return GL_TRUE;
748 else
749 return GL_FALSE;
750 }
751
752 return GL_TRUE;
753 }
754 crDebug("Using native GL, app window doesn't meet minimum_window_size");
755 return GL_FALSE;
756 }
757 else if (stub.matchWindowTitle) {
758 /* If the user's specified a window title for Chromium, see if this
759 * window satisfies that criterium.
760 */
761 GLboolean wildcard = GL_FALSE;
762 char title[1000];
763 char *titlePattern;
764 int len;
765 /* check for leading '*' wildcard */
766 if (stub.matchWindowTitle[0] == '*') {
767 titlePattern = crStrdup( stub.matchWindowTitle + 1 );
768 wildcard = GL_TRUE;
769 }
770 else {
771 titlePattern = crStrdup( stub.matchWindowTitle );
772 }
773 /* check for trailing '*' wildcard */
774 len = crStrlen(titlePattern);
775 if (len > 0 && titlePattern[len - 1] == '*') {
776 titlePattern[len - 1] = '\0'; /* terminate here */
777 wildcard = GL_TRUE;
778 }
779
780 GetWindowTitle( window, title );
781 if (title[0]) {
782 if (wildcard) {
783 if (crStrstr(title, titlePattern)) {
784 crFree(titlePattern);
785 return GL_TRUE;
786 }
787 }
788 else if (crStrcmp(title, titlePattern) == 0) {
789 crFree(titlePattern);
790 return GL_TRUE;
791 }
792 }
793 crFree(titlePattern);
794 crDebug("Using native GL, app window title doesn't match match_window_title string (\"%s\" != \"%s\")", title, stub.matchWindowTitle);
795 return GL_FALSE;
796 }
797
798 /* Window title and size don't matter */
799 CRASSERT(stub.minChromiumWindowWidth == 0);
800 CRASSERT(stub.minChromiumWindowHeight == 0);
801 CRASSERT(stub.matchWindowTitle == NULL);
802
803 /* User hasn't specified a width/height or window title.
804 * We'll use chromium for this window (and context) if no other is.
805 */
806
807 return GL_TRUE; /* use Chromium! */
808}
809
810static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2)
811{
812 WindowInfo *pWindow = (WindowInfo *) data1;
813 ContextInfo *pCtx = (ContextInfo *) data2;
814
815 if (pWindow->pOwner == pCtx)
816 {
817#ifdef WINDOWS
818 /* Note: can't use WindowFromDC(context->pOwnWindow->drawable) here
819 because GL context is already released from DC and actual guest window
820 could be destroyed.
821 */
822 crWindowDestroy((GLint)pWindow->hWnd);
823#else
824 crWindowDestroy((GLint)pWindow->drawable);
825#endif
826 }
827}
828
829GLboolean
830stubMakeCurrent( WindowInfo *window, ContextInfo *context )
831{
832 GLboolean retVal;
833
834 /*
835 * Get WindowInfo and ContextInfo pointers.
836 */
837
838 if (!context || !window) {
839 if (stub.currentContext)
840 stub.currentContext->currentDrawable = NULL;
841 if (context)
842 context->currentDrawable = NULL;
843 stub.currentContext = NULL;
844 return GL_TRUE; /* OK */
845 }
846
847#ifdef CHROMIUM_THREADSAFE
848 stubCheckMultithread();
849#endif
850
851 if (context->type == UNDECIDED) {
852 /* Here's where we really create contexts */
853#ifdef CHROMIUM_THREADSAFE
854 crLockMutex(&stub.mutex);
855#endif
856
857 if (stubCheckUseChromium(window)) {
858 /*
859 * Create a Chromium context.
860 */
861#if defined(GLX) || defined(DARWIN)
862 GLint spuShareCtx = context->share ? context->share->spuContext : 0;
863#else
864 GLint spuShareCtx = 0;
865#endif
866
867 CRASSERT(stub.spu);
868 CRASSERT(stub.spu->dispatch_table.CreateContext);
869 context->type = CHROMIUM;
870
871 context->spuContext
872 = stub.spu->dispatch_table.CreateContext( context->dpyName,
873 context->visBits,
874 spuShareCtx );
875 if (window->spuWindow == -1)
876 {
877 /*crDebug("(1)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
878 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
879#ifdef CR_NEWWINTRACK
880 window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
881#endif
882
883 }
884 }
885 else {
886 /*
887 * Create a native OpenGL context.
888 */
889 if (!InstantiateNativeContext(window, context))
890 {
891#ifdef CHROMIUM_THREADSAFE
892 crUnlockMutex(&stub.mutex);
893#endif
894 return 0; /* false */
895 }
896 context->type = NATIVE;
897 }
898
899#ifdef CHROMIUM_THREADSAFE
900 crUnlockMutex(&stub.mutex);
901#endif
902 }
903
904
905 if (context->type == NATIVE) {
906 /*
907 * Native OpenGL MakeCurrent().
908 */
909#ifdef WINDOWS
910 retVal = (GLboolean) stub.wsInterface.wglMakeCurrent( window->drawable, context->hglrc );
911#elif defined(Darwin)
912 // XXX \todo We need to differentiate between these two..
913 retVal = ( stub.wsInterface.CGLSetSurface(context->cglc, window->connection, window->drawable, window->surface) == noErr );
914 retVal = ( stub.wsInterface.CGLSetCurrentContext(context->cglc) == noErr );
915#elif defined(GLX)
916 retVal = (GLboolean) stub.wsInterface.glXMakeCurrent( window->dpy, window->drawable, context->glxContext );
917#endif
918 }
919 else {
920 /*
921 * SPU chain MakeCurrent().
922 */
923 CRASSERT(context->type == CHROMIUM);
924 CRASSERT(context->spuContext >= 0);
925
926 /*if (context->currentDrawable && context->currentDrawable != window)
927 crDebug("Rebinding context %p to a different window", context);*/
928
929 if (window->type == NATIVE) {
930 crWarning("Can't rebind a chromium context to a native window\n");
931 retVal = 0;
932 }
933 else {
934 if (window->spuWindow == -1)
935 {
936 /*crDebug("(2)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
937 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
938 if (context->currentDrawable && context->currentDrawable->type==CHROMIUM
939 && context->currentDrawable->pOwner==context)
940 {
941#ifdef WINDOWS
942 if (!WindowFromDC(context->currentDrawable->drawable))
943 {
944 crWindowDestroy((GLint)context->currentDrawable->hWnd);
945 }
946#else
947 Window root;
948 int x, y;
949 unsigned int border, depth, w, h;
950
951 XLOCK(context->currentDrawable->dpy);
952 if (!XGetGeometry(context->currentDrawable->dpy, context->currentDrawable->drawable, &root, &x, &y, &w, &h, &border, &depth))
953 {
954 crWindowDestroy((GLint)context->currentDrawable->drawable);
955 }
956 XUNLOCK(context->currentDrawable->dpy);
957#endif
958
959 }
960 }
961
962 if (window->spuWindow != (GLint)window->drawable)
963 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, (GLint) window->drawable, context->spuContext );
964 else
965 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, 0, /* native window handle */ context->spuContext );
966
967 retVal = 1;
968 }
969 }
970
971 window->type = context->type;
972 window->pOwner = context;
973 context->currentDrawable = window;
974 stub.currentContext = context;
975
976 if (retVal) {
977 /* Now, if we've transitions from Chromium to native rendering, or
978 * vice versa, we have to change all the OpenGL entrypoint pointers.
979 */
980 if (context->type == NATIVE) {
981 /* Switch to native API */
982 /*printf(" Switching to native API\n");*/
983 stubSetDispatch(&stub.nativeDispatch);
984 }
985 else if (context->type == CHROMIUM) {
986 /* Switch to stub (SPU) API */
987 /*printf(" Switching to spu API\n");*/
988 stubSetDispatch(&stub.spuDispatch);
989 }
990 else {
991 /* no API switch needed */
992 }
993 }
994
995 if (!window->width && window->type == CHROMIUM) {
996 /* One time window setup */
997 int x, y;
998 unsigned int winW, winH;
999
1000 stubGetWindowGeometry( window, &x, &y, &winW, &winH );
1001
1002 /* If we're not using GLX/WGL (no app window) we'll always get
1003 * a width and height of zero here. In that case, skip the viewport
1004 * call since we're probably using a tilesort SPU with fake_window_dims
1005 * which the tilesort SPU will use for the viewport.
1006 */
1007 window->width = winW;
1008 window->height = winH;
1009 if (stub.trackWindowSize)
1010 stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
1011 if (stub.trackWindowPos)
1012 stub.spuDispatch.WindowPosition(window->spuWindow, x, y);
1013 if (winW > 0 && winH > 0)
1014 stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
1015 }
1016
1017 /* Update window mapping state.
1018 * Basically, this lets us hide render SPU windows which correspond
1019 * to unmapped application windows. Without this, perfly (for example)
1020 * opens *lots* of temporary windows which otherwise clutter the screen.
1021 */
1022 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
1023 const int mapped = stubIsWindowVisible(window);
1024 if (mapped != window->mapped) {
1025 crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
1026 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
1027 window->mapped = mapped;
1028 }
1029 }
1030
1031 return retVal;
1032}
1033
1034void
1035stubDestroyContext( unsigned long contextId )
1036{
1037 ContextInfo *context;
1038
1039 if (!stub.contextTable) {
1040 return;
1041 }
1042 context = (ContextInfo *) crHashtableSearch(stub.contextTable, contextId);
1043
1044 CRASSERT(context);
1045
1046 if (context->type == NATIVE) {
1047#ifdef WINDOWS
1048 stub.wsInterface.wglDeleteContext( context->hglrc );
1049#elif defined(Darwin)
1050 stub.wsInterface.CGLDestroyContext( context->cglc );
1051#elif defined(GLX)
1052 stub.wsInterface.glXDestroyContext( context->dpy, context->glxContext );
1053#endif
1054 }
1055 else if (context->type == CHROMIUM) {
1056 /* Have pack SPU or tilesort SPU, etc. destroy the context */
1057 CRASSERT(context->spuContext >= 0);
1058 stub.spu->dispatch_table.DestroyContext( context->spuContext );
1059 crHashtableWalk(stub.windowTable, stubWindowCheckOwnerCB, context);
1060 }
1061
1062 if (stub.currentContext == context) {
1063 stub.currentContext = NULL;
1064 }
1065
1066#ifdef GLX
1067 crFreeHashtable(context->pGLXPixmapsHash, crFree);
1068 if (context->damageDpy)
1069 {
1070 XCloseDisplay(context->damageDpy);
1071 }
1072#endif
1073
1074 crMemZero(context, sizeof(ContextInfo)); /* just to be safe */
1075 crHashtableDelete(stub.contextTable, contextId, crFree);
1076}
1077
1078
1079void
1080stubSwapBuffers( const WindowInfo *window, GLint flags )
1081{
1082 if (!window)
1083 return;
1084
1085 /* Determine if this window is being rendered natively or through
1086 * Chromium.
1087 */
1088
1089 if (window->type == NATIVE) {
1090 /*printf("*** Swapping native window %d\n", (int) drawable);*/
1091#ifdef WINDOWS
1092 (void) stub.wsInterface.wglSwapBuffers( window->drawable );
1093#elif defined(Darwin)
1094 /* ...is this ok? */
1095/* stub.wsInterface.CGLFlushDrawable( context->cglc ); */
1096 crDebug("stubSwapBuffers: unable to swap (no context!)");
1097#elif defined(GLX)
1098 stub.wsInterface.glXSwapBuffers( window->dpy, window->drawable );
1099#endif
1100 }
1101 else if (window->type == CHROMIUM) {
1102 /* Let the SPU do the buffer swap */
1103 /*printf("*** Swapping chromium window %d\n", (int) drawable);*/
1104 if (stub.appDrawCursor) {
1105 int pos[2];
1106 GetCursorPosition(window, pos);
1107 stub.spu->dispatch_table.ChromiumParametervCR(GL_CURSOR_POSITION_CR, GL_INT, 2, pos);
1108 }
1109 stub.spu->dispatch_table.SwapBuffers( window->spuWindow, flags );
1110 }
1111 else {
1112 crDebug("Calling SwapBuffers on a window we haven't seen before (no-op).");
1113 }
1114}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette