VirtualBox

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

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

crOpenGL: fix fail on wglMakeCurrent(0,0) call

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