VirtualBox

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

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

crOpenGL: some initial functionality on linux

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