VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/crOpenGL/context.c@ 16480

Last change on this file since 16480 was 16480, checked in by vboxsync, 16 years ago

crOpenGL: track visible regions on linux guests + some more exports

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