VirtualBox

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

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

crOpenGL: export to OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.7 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 if (!window
560 || !window->dpy
561 || !window->drawable
562 || !XGetGeometry(window->dpy, window->drawable, &root,
563 x, y, w, h, &border, &depth)
564 || !XTranslateCoordinates(window->dpy, window->drawable, root,
565 *x, *y, x, y, &child)) {
566 *x = *y = 0;
567 *w = *h = 0;
568 }
569}
570
571static char *
572GetWindowTitleHelper( Display *dpy, Window window, GLboolean recurseUp )
573{
574 while (1) {
575 char *name;
576 if (!XFetchName(dpy, window, &name))
577 return NULL;
578 if (name[0]) {
579 return name;
580 }
581 else if (recurseUp) {
582 /* This window has no name, try the parent */
583 Status stat;
584 Window root, parent, *children;
585 unsigned int numChildren;
586 stat = XQueryTree( dpy, window, &root, &parent,
587 &children, &numChildren );
588 if (!stat || window == root)
589 return NULL;
590 if (children)
591 XFree(children);
592 window = parent;
593 }
594 else {
595 XFree(name);
596 return NULL;
597 }
598 }
599}
600
601static void
602GetWindowTitle( const WindowInfo *window, char *title )
603{
604 char *t = GetWindowTitleHelper(window->dpy, window->drawable, GL_TRUE);
605 if (t) {
606 crStrcpy(title, t);
607 XFree(t);
608 }
609 else {
610 title[0] = 0;
611 }
612}
613
614
615/**
616 *Return current cursor position in local window coords.
617 */
618static void
619GetCursorPosition( const WindowInfo *window, int pos[2] )
620{
621 int rootX, rootY;
622 Window root, child;
623 unsigned int mask;
624 int x, y;
625 Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
626 &rootX, &rootY, &pos[0], &pos[1], &mask);
627 if (q) {
628 unsigned int w, h;
629 stubGetWindowGeometry( window, &x, &y, &w, &h );
630 /* invert Y */
631 pos[1] = (int) h - pos[1] - 1;
632 }
633 else {
634 pos[0] = pos[1] = 0;
635 }
636}
637
638#endif
639
640
641/**
642 * This function is called by MakeCurrent() and determines whether or
643 * not a new rendering context should be bound to Chromium or the native
644 * OpenGL.
645 * \return GL_FALSE if native OpenGL should be used, or GL_TRUE if Chromium
646 * should be used.
647 */
648static GLboolean
649stubCheckUseChromium( WindowInfo *window )
650{
651 int x, y;
652 unsigned int w, h;
653
654 /* If the provided window is CHROMIUM, we're clearly intended
655 * to create a CHROMIUM context.
656 */
657 if (window->type == CHROMIUM)
658 return GL_TRUE;
659
660 if (stub.ignoreFreeglutMenus) {
661 const char *glutMenuTitle = "freeglut menu";
662 char title[1000];
663 GetWindowTitle(window, title);
664 if (crStrcmp(title, glutMenuTitle) == 0) {
665 crDebug("GL faker: Ignoring freeglut menu window");
666 return GL_FALSE;
667 }
668 }
669
670 /* If the user's specified a window count for Chromium, see if
671 * this window satisfies that criterium.
672 */
673 stub.matchChromiumWindowCounter++;
674 if (stub.matchChromiumWindowCount > 0) {
675 if (stub.matchChromiumWindowCounter != stub.matchChromiumWindowCount) {
676 crDebug("Using native GL, app window doesn't meet match_window_count");
677 return GL_FALSE;
678 }
679 }
680
681 /* If the user's specified a window list to ignore, see if this
682 * window satisfies that criterium.
683 */
684 if (stub.matchChromiumWindowID) {
685 GLuint i;
686
687 for (i = 0; i <= stub.numIgnoreWindowID; i++) {
688 if (stub.matchChromiumWindowID[i] == stub.matchChromiumWindowCounter) {
689 crDebug("Ignore window ID %d, using native GL", stub.matchChromiumWindowID[i]);
690 return GL_FALSE;
691 }
692 }
693 }
694
695 /* If the user's specified a minimum window size for Chromium, see if
696 * this window satisfies that criterium.
697 */
698 if (stub.minChromiumWindowWidth > 0 &&
699 stub.minChromiumWindowHeight > 0) {
700 stubGetWindowGeometry( window, &x, &y, &w, &h );
701 if (w >= stub.minChromiumWindowWidth &&
702 h >= stub.minChromiumWindowHeight) {
703
704 /* Check for maximum sized window now too */
705 if (stub.maxChromiumWindowWidth &&
706 stub.maxChromiumWindowHeight) {
707 if (w < stub.maxChromiumWindowWidth &&
708 h < stub.maxChromiumWindowHeight)
709 return GL_TRUE;
710 else
711 return GL_FALSE;
712 }
713
714 return GL_TRUE;
715 }
716 crDebug("Using native GL, app window doesn't meet minimum_window_size");
717 return GL_FALSE;
718 }
719 else if (stub.matchWindowTitle) {
720 /* If the user's specified a window title for Chromium, see if this
721 * window satisfies that criterium.
722 */
723 GLboolean wildcard = GL_FALSE;
724 char title[1000];
725 char *titlePattern;
726 int len;
727 /* check for leading '*' wildcard */
728 if (stub.matchWindowTitle[0] == '*') {
729 titlePattern = crStrdup( stub.matchWindowTitle + 1 );
730 wildcard = GL_TRUE;
731 }
732 else {
733 titlePattern = crStrdup( stub.matchWindowTitle );
734 }
735 /* check for trailing '*' wildcard */
736 len = crStrlen(titlePattern);
737 if (len > 0 && titlePattern[len - 1] == '*') {
738 titlePattern[len - 1] = '\0'; /* terminate here */
739 wildcard = GL_TRUE;
740 }
741
742 GetWindowTitle( window, title );
743 if (title[0]) {
744 if (wildcard) {
745 if (crStrstr(title, titlePattern)) {
746 crFree(titlePattern);
747 return GL_TRUE;
748 }
749 }
750 else if (crStrcmp(title, titlePattern) == 0) {
751 crFree(titlePattern);
752 return GL_TRUE;
753 }
754 }
755 crFree(titlePattern);
756 crDebug("Using native GL, app window title doesn't match match_window_title string (\"%s\" != \"%s\")", title, stub.matchWindowTitle);
757 return GL_FALSE;
758 }
759
760 /* Window title and size don't matter */
761 CRASSERT(stub.minChromiumWindowWidth == 0);
762 CRASSERT(stub.minChromiumWindowHeight == 0);
763 CRASSERT(stub.matchWindowTitle == NULL);
764
765 /* User hasn't specified a width/height or window title.
766 * We'll use chromium for this window (and context) if no other is.
767 */
768
769 return GL_TRUE; /* use Chromium! */
770}
771
772
773GLboolean
774stubMakeCurrent( WindowInfo *window, ContextInfo *context )
775{
776 GLboolean retVal;
777
778 /*
779 * Get WindowInfo and ContextInfo pointers.
780 */
781
782 if (!context || !window) {
783 if (stub.currentContext)
784 stub.currentContext->currentDrawable = NULL;
785 if (context)
786 context->currentDrawable = NULL;
787 stub.currentContext = NULL;
788 return GL_TRUE; /* OK */
789 }
790
791#ifdef CHROMIUM_THREADSAFE
792 stubCheckMultithread();
793#endif
794
795 if (context->type == UNDECIDED) {
796 /* Here's where we really create contexts */
797#ifdef CHROMIUM_THREADSAFE
798 crLockMutex(&stub.mutex);
799#endif
800
801 if (stubCheckUseChromium(window)) {
802 /*
803 * Create a Chromium context.
804 */
805#if defined(GLX) || defined(DARWIN)
806 GLint spuShareCtx = context->share ? context->share->spuContext : 0;
807#else
808 GLint spuShareCtx = 0;
809#endif
810
811 CRASSERT(stub.spu);
812 CRASSERT(stub.spu->dispatch_table.CreateContext);
813 context->type = CHROMIUM;
814
815 context->spuContext
816 = stub.spu->dispatch_table.CreateContext( context->dpyName,
817 context->visBits,
818 spuShareCtx );
819 if (window->spuWindow == -1)
820 {
821 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
822 CRASSERT(!context->pOwnWindow);
823 context->pOwnWindow = window;
824 }
825 }
826 else {
827 /*
828 * Create a native OpenGL context.
829 */
830 if (!InstantiateNativeContext(window, context))
831 {
832#ifdef CHROMIUM_THREADSAFE
833 crUnlockMutex(&stub.mutex);
834#endif
835 return 0; /* false */
836 }
837 context->type = NATIVE;
838 }
839
840#ifdef CHROMIUM_THREADSAFE
841 crUnlockMutex(&stub.mutex);
842#endif
843 }
844
845
846 if (context->type == NATIVE) {
847 /*
848 * Native OpenGL MakeCurrent().
849 */
850#ifdef WINDOWS
851 retVal = (GLboolean) stub.wsInterface.wglMakeCurrent( window->drawable, context->hglrc );
852#elif defined(Darwin)
853 // XXX \todo We need to differentiate between these two..
854 retVal = ( stub.wsInterface.CGLSetSurface(context->cglc, window->connection, window->drawable, window->surface) == noErr );
855 retVal = ( stub.wsInterface.CGLSetCurrentContext(context->cglc) == noErr );
856#elif defined(GLX)
857 retVal = (GLboolean) stub.wsInterface.glXMakeCurrent( window->dpy, window->drawable, context->glxContext );
858#endif
859 }
860 else {
861 /*
862 * SPU chain MakeCurrent().
863 */
864 CRASSERT(context->type == CHROMIUM);
865 CRASSERT(context->spuContext >= 0);
866
867 if (context->currentDrawable && context->currentDrawable != window)
868 crWarning("Rebinding context %p to a different window", context);
869
870 if (window->type == NATIVE) {
871 crWarning("Can't rebind a chromium context to a native window\n");
872 retVal = 0;
873 }
874 else {
875 if (window->spuWindow == -1)
876 {
877 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
878 CRASSERT(!context->pOwnWindow);
879 context->pOwnWindow = window;
880 }
881
882 if (window->spuWindow != (GLint)window->drawable)
883 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, (GLint) window->drawable, context->spuContext );
884 else
885 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, 0, /* native window handle */ context->spuContext );
886
887 retVal = 1;
888 }
889 }
890
891 window->type = context->type;
892 context->currentDrawable = window;
893 stub.currentContext = context;
894
895 if (retVal) {
896 /* Now, if we've transitions from Chromium to native rendering, or
897 * vice versa, we have to change all the OpenGL entrypoint pointers.
898 */
899 if (context->type == NATIVE) {
900 /* Switch to native API */
901 /*printf(" Switching to native API\n");*/
902 stubSetDispatch(&stub.nativeDispatch);
903 }
904 else if (context->type == CHROMIUM) {
905 /* Switch to stub (SPU) API */
906 /*printf(" Switching to spu API\n");*/
907 stubSetDispatch(&stub.spuDispatch);
908 }
909 else {
910 /* no API switch needed */
911 }
912 }
913
914 if (!window->width && window->type == CHROMIUM) {
915 /* One time window setup */
916 int x, y;
917 unsigned int winW, winH;
918
919 stubGetWindowGeometry( window, &x, &y, &winW, &winH );
920
921 /* If we're not using GLX/WGL (no app window) we'll always get
922 * a width and height of zero here. In that case, skip the viewport
923 * call since we're probably using a tilesort SPU with fake_window_dims
924 * which the tilesort SPU will use for the viewport.
925 */
926 window->width = winW;
927 window->height = winH;
928 if (stub.trackWindowSize)
929 stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
930 if (winW > 0 && winH > 0)
931 stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
932 }
933
934 /* Update window mapping state.
935 * Basically, this lets us hide render SPU windows which correspond
936 * to unmapped application windows. Without this, perfly (for example)
937 * opens *lots* of temporary windows which otherwise clutter the screen.
938 */
939 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
940 const int mapped = stubIsWindowVisible(window);
941 if (mapped != window->mapped) {
942 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
943 window->mapped = mapped;
944 }
945 }
946
947 return retVal;
948}
949
950
951
952void
953stubDestroyContext( unsigned long contextId )
954{
955 ContextInfo *context;
956
957 if (!stub.contextTable) {
958 return;
959 }
960 context = (ContextInfo *) crHashtableSearch(stub.contextTable, contextId);
961
962 CRASSERT(context);
963
964 if (context->type == NATIVE) {
965#ifdef WINDOWS
966 stub.wsInterface.wglDeleteContext( context->hglrc );
967#elif defined(Darwin)
968 stub.wsInterface.CGLDestroyContext( context->cglc );
969#elif defined(GLX)
970 stub.wsInterface.glXDestroyContext( context->dpy, context->glxContext );
971#endif
972 }
973 else if (context->type == CHROMIUM) {
974 /* Have pack SPU or tilesort SPU, etc. destroy the context */
975 CRASSERT(context->spuContext >= 0);
976 stub.spu->dispatch_table.DestroyContext( context->spuContext );
977 if (context->pOwnWindow)
978 {
979 /* Note: can't use WindowFromDC(context->pOwnWindow->drawable) here
980 because GL context is already released from DC and actual guest window
981 could be destroyed.
982 */
983 crWindowDestroy((GLint)context->pOwnWindow->hWnd);
984 }
985 }
986
987 if (stub.currentContext == context) {
988 stub.currentContext = NULL;
989 }
990
991 crMemZero(context, sizeof(ContextInfo)); /* just to be safe */
992 crHashtableDelete(stub.contextTable, contextId, crFree);
993}
994
995
996void
997stubSwapBuffers( const WindowInfo *window, GLint flags )
998{
999 if (!window)
1000 return;
1001
1002 /* Determine if this window is being rendered natively or through
1003 * Chromium.
1004 */
1005
1006 if (window->type == NATIVE) {
1007 /*printf("*** Swapping native window %d\n", (int) drawable);*/
1008#ifdef WINDOWS
1009 (void) stub.wsInterface.wglSwapBuffers( window->drawable );
1010#elif defined(Darwin)
1011 /* ...is this ok? */
1012/* stub.wsInterface.CGLFlushDrawable( context->cglc ); */
1013 crDebug("stubSwapBuffers: unable to swap (no context!)");
1014#elif defined(GLX)
1015 stub.wsInterface.glXSwapBuffers( window->dpy, window->drawable );
1016#endif
1017 }
1018 else if (window->type == CHROMIUM) {
1019 /* Let the SPU do the buffer swap */
1020 /*printf("*** Swapping chromium window %d\n", (int) drawable);*/
1021 if (stub.appDrawCursor) {
1022 int pos[2];
1023 GetCursorPosition(window, pos);
1024 stub.spu->dispatch_table.ChromiumParametervCR(GL_CURSOR_POSITION_CR, GL_INT, 2, pos);
1025 }
1026 stub.spu->dispatch_table.SwapBuffers( window->spuWindow, flags );
1027 }
1028 else {
1029 crDebug("Calling SwapBuffers on a window we haven't seen before (no-op).");
1030 }
1031}
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