VirtualBox

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

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

crOpenGL: use shared memory to access xserver pixmaps data

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