VirtualBox

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

Last change on this file since 26547 was 26545, checked in by vboxsync, 15 years ago

crOpenGL: fix assertion when application uses several windows with single opengl context (#4598)

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