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 | #include <Carbon/Carbon.h>
|
---|
8 | #include <AGL/agl.h>
|
---|
9 | #include <OpenGL/OpenGL.h>
|
---|
10 |
|
---|
11 | #include <iprt/time.h>
|
---|
12 |
|
---|
13 | #include <stdio.h>
|
---|
14 |
|
---|
15 | #include "cr_environment.h"
|
---|
16 | #include "cr_error.h"
|
---|
17 | #include "cr_string.h"
|
---|
18 | #include "cr_mem.h"
|
---|
19 | #include "renderspu.h"
|
---|
20 |
|
---|
21 | /* Some necessary global defines */
|
---|
22 | WindowGroupRef gParentGroup = NULL;
|
---|
23 | WindowGroupRef gMasterGroup = NULL;
|
---|
24 | GLint gCurrentBufferName = 1;
|
---|
25 | uint64_t gDockUpdateTS = 0;
|
---|
26 |
|
---|
27 | enum
|
---|
28 | {
|
---|
29 | /* Event classes */
|
---|
30 | kEventClassVBox = 'vbox',
|
---|
31 | /* Event kinds */
|
---|
32 | kEventVBoxShowWindow = 'swin',
|
---|
33 | kEventVBoxMoveWindow = 'mwin',
|
---|
34 | kEventVBoxResizeWindow = 'rwin',
|
---|
35 | kEventVBoxUpdateDock = 'udck'
|
---|
36 | };
|
---|
37 |
|
---|
38 | #define renderspuSetWindowContext(w, c) \
|
---|
39 | ( SetWRefCon( (w), (unsigned long) (c) ) )
|
---|
40 | #define renderspuGetWindowContext(w) \
|
---|
41 | ( (ContextInfo *) GetWRefCon( ((w)->nativeWindow ? (w)->nativeWindow : (w)->window) ) )
|
---|
42 |
|
---|
43 | /* Debug macros */
|
---|
44 | #define DEBUG_MSG_RESULT(result, text) \
|
---|
45 | crDebug(text" (%d; %s:%d)", (int)(result), __FILE__, __LINE__)
|
---|
46 |
|
---|
47 | #define CHECK_CARBON_RC(result, text) \
|
---|
48 | if((result) != noErr) \
|
---|
49 | DEBUG_MSG_RESULT(result, text);
|
---|
50 |
|
---|
51 | #define CHECK_CARBON_RC_RETURN(result, text, ret) \
|
---|
52 | if((result) != noErr) \
|
---|
53 | { \
|
---|
54 | DEBUG_MSG_RESULT(result, text); \
|
---|
55 | return ret; \
|
---|
56 | }
|
---|
57 |
|
---|
58 | #define CHECK_CARBON_RC_RETURN_VOID(result, text) \
|
---|
59 | CHECK_CARBON_RC_RETURN(result, text,)
|
---|
60 |
|
---|
61 | #define CHECK_AGL_RC(result, text) \
|
---|
62 | if(!(result)) \
|
---|
63 | { \
|
---|
64 | GLenum error = render_spu.ws.aglGetError(); \
|
---|
65 | DEBUG_MSG_RESULT(result, text); \
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* Window event handler */
|
---|
69 | static pascal OSStatus
|
---|
70 | windowEvtHndlr(EventHandlerCallRef myHandler, EventRef event, void* userData)
|
---|
71 | {
|
---|
72 | /* Currently this is *NOT* used */
|
---|
73 |
|
---|
74 | #pragma unused (userData)
|
---|
75 | WindowRef window = NULL;
|
---|
76 | Rect rectPort = { 0, 0, 0, 0 };
|
---|
77 | OSStatus result = eventNotHandledErr;
|
---|
78 | UInt32 class = GetEventClass( event );
|
---|
79 | UInt32 kind = GetEventKind( event );
|
---|
80 |
|
---|
81 | GetEventParameter(event, kEventParamDirectObject, typeWindowRef,
|
---|
82 | NULL, sizeof(WindowRef), NULL, &window);
|
---|
83 | if( window )
|
---|
84 | GetWindowPortBounds( window, &rectPort );
|
---|
85 |
|
---|
86 | switch (class) {
|
---|
87 | case kEventClassWindow:
|
---|
88 | switch (kind) {
|
---|
89 | case kEventWindowActivated:
|
---|
90 | case kEventWindowDrawContent:
|
---|
91 | break;
|
---|
92 |
|
---|
93 | case kEventWindowClose:
|
---|
94 | HideWindow( window );
|
---|
95 | SetWRefCon( window, (int)NULL );
|
---|
96 |
|
---|
97 | crWarning( "Render SPU: caught kEventWindowClose -- quitting." );
|
---|
98 | break;
|
---|
99 |
|
---|
100 | case kEventWindowShown:
|
---|
101 | /* build gl */
|
---|
102 | if( window == FrontWindow() )
|
---|
103 | SetUserFocusWindow( window );
|
---|
104 | InvalWindowRect( window, &rectPort );
|
---|
105 | break;
|
---|
106 |
|
---|
107 | case kEventWindowBoundsChanged:
|
---|
108 | /* resize
|
---|
109 | update */
|
---|
110 | break;
|
---|
111 |
|
---|
112 | case kEventWindowZoomed:
|
---|
113 | /* zoom button */
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | break;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return result;
|
---|
120 | }
|
---|
121 |
|
---|
122 | GLboolean
|
---|
123 | renderspu_SystemInitVisual(VisualInfo *visual)
|
---|
124 | {
|
---|
125 | if(visual->visAttribs & CR_PBUFFER_BIT)
|
---|
126 | crWarning("Render SPU: PBuffers not support on Darwin/AGL yet.");
|
---|
127 |
|
---|
128 | return GL_TRUE;
|
---|
129 | }
|
---|
130 |
|
---|
131 | GLboolean
|
---|
132 | renderspuChoosePixelFormat(ContextInfo *context, AGLPixelFormat *pix)
|
---|
133 | {
|
---|
134 | GLbitfield visAttribs = context->visual->visAttribs;
|
---|
135 | GLint attribs[32];
|
---|
136 | GLint ind = 0;
|
---|
137 |
|
---|
138 | #define ATTR_ADD(s) ( attribs[ind++] = (s) )
|
---|
139 | #define ATTR_ADDV(s,v) ( ATTR_ADD((s)), ATTR_ADD((v)) )
|
---|
140 |
|
---|
141 | CRASSERT(render_spu.ws.aglChoosePixelFormat);
|
---|
142 |
|
---|
143 | ATTR_ADD(AGL_RGBA);
|
---|
144 | /* ATTR_ADDV(AGL_RED_SIZE, 1);
|
---|
145 | ATTR_ADDV(AGL_GREEN_SIZE, 1);
|
---|
146 | ATTR_ADDV(AGL_BLUE_SIZE, 1); */
|
---|
147 |
|
---|
148 | /* if( render_spu.fullscreen )*/
|
---|
149 | /* ATTR_ADD(AGL_FULLSCREEN);*/
|
---|
150 |
|
---|
151 | if( visAttribs & CR_ALPHA_BIT )
|
---|
152 | ATTR_ADDV(AGL_ALPHA_SIZE, 1);
|
---|
153 |
|
---|
154 | if( visAttribs & CR_DOUBLE_BIT )
|
---|
155 | ATTR_ADD(AGL_DOUBLEBUFFER);
|
---|
156 |
|
---|
157 | if( visAttribs & CR_STEREO_BIT )
|
---|
158 | ATTR_ADD(AGL_STEREO);
|
---|
159 |
|
---|
160 | if( visAttribs & CR_DEPTH_BIT )
|
---|
161 | ATTR_ADDV(AGL_DEPTH_SIZE, 1);
|
---|
162 |
|
---|
163 | if( visAttribs & CR_STENCIL_BIT )
|
---|
164 | ATTR_ADDV(AGL_STENCIL_SIZE, 1);
|
---|
165 |
|
---|
166 | if( visAttribs & CR_ACCUM_BIT ) {
|
---|
167 | ATTR_ADDV(AGL_ACCUM_RED_SIZE, 1);
|
---|
168 | ATTR_ADDV(AGL_ACCUM_GREEN_SIZE, 1);
|
---|
169 | ATTR_ADDV(AGL_ACCUM_BLUE_SIZE, 1);
|
---|
170 | if( visAttribs & CR_ALPHA_BIT )
|
---|
171 | ATTR_ADDV(AGL_ACCUM_ALPHA_SIZE, 1);
|
---|
172 | }
|
---|
173 |
|
---|
174 | if( visAttribs & CR_MULTISAMPLE_BIT ) {
|
---|
175 | ATTR_ADDV(AGL_SAMPLE_BUFFERS_ARB, 1);
|
---|
176 | ATTR_ADDV(AGL_SAMPLES_ARB, 4);
|
---|
177 | }
|
---|
178 |
|
---|
179 | if( visAttribs & CR_OVERLAY_BIT )
|
---|
180 | ATTR_ADDV(AGL_LEVEL, 1);
|
---|
181 |
|
---|
182 | ATTR_ADD(AGL_NONE);
|
---|
183 |
|
---|
184 | *pix = render_spu.ws.aglChoosePixelFormat( NULL, 0, attribs );
|
---|
185 |
|
---|
186 | return (*pix != NULL);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void
|
---|
190 | renderspuDestroyPixelFormat(ContextInfo *context, AGLPixelFormat *pix)
|
---|
191 | {
|
---|
192 | render_spu.ws.aglDestroyPixelFormat( *pix );
|
---|
193 | *pix = NULL;
|
---|
194 | }
|
---|
195 |
|
---|
196 | GLboolean
|
---|
197 | renderspu_SystemCreateContext(VisualInfo *visual, ContextInfo *context, ContextInfo *sharedContext)
|
---|
198 | {
|
---|
199 | AGLPixelFormat pix;
|
---|
200 |
|
---|
201 | (void) sharedContext;
|
---|
202 | CRASSERT(visual);
|
---|
203 | CRASSERT(context);
|
---|
204 |
|
---|
205 | context->visual = visual;
|
---|
206 |
|
---|
207 | if( !renderspuChoosePixelFormat(context, &pix) ) {
|
---|
208 | crError( "Render SPU: Unable to create pixel format" );
|
---|
209 | return GL_FALSE;
|
---|
210 | }
|
---|
211 |
|
---|
212 | context->context = render_spu.ws.aglCreateContext( pix, NULL );
|
---|
213 | renderspuDestroyPixelFormat( context, &pix );
|
---|
214 |
|
---|
215 | if( !context->context ) {
|
---|
216 | crError( "Render SPU: Could not create rendering context" );
|
---|
217 | return GL_FALSE;
|
---|
218 | }
|
---|
219 |
|
---|
220 | return GL_TRUE;
|
---|
221 | }
|
---|
222 |
|
---|
223 | void
|
---|
224 | renderspu_SystemDestroyContext(ContextInfo *context)
|
---|
225 | {
|
---|
226 | if(!context)
|
---|
227 | return;
|
---|
228 |
|
---|
229 | render_spu.ws.aglSetDrawable(context->context, NULL);
|
---|
230 | render_spu.ws.aglSetCurrentContext(NULL);
|
---|
231 | if(context->context)
|
---|
232 | {
|
---|
233 | render_spu.ws.aglDestroyContext(context->context);
|
---|
234 | context->context = NULL;
|
---|
235 | }
|
---|
236 |
|
---|
237 | context->visual = NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void
|
---|
241 | renderspuFullscreen(WindowInfo *window, GLboolean fullscreen)
|
---|
242 | {
|
---|
243 | /* Real fullscreen isn't supported by VirtualBox */
|
---|
244 | }
|
---|
245 |
|
---|
246 | GLboolean
|
---|
247 | renderspuWindowAttachContext(WindowInfo *wi, WindowRef window,
|
---|
248 | ContextInfo *context)
|
---|
249 | {
|
---|
250 | GLboolean result;
|
---|
251 |
|
---|
252 | if(!context || !wi)
|
---|
253 | return render_spu.ws.aglSetCurrentContext( NULL );
|
---|
254 |
|
---|
255 | /* Flush old context first */
|
---|
256 | if (context->currentWindow->window != window)
|
---|
257 | render_spu.self.Flush();
|
---|
258 | /* If the window buffer name is uninitialized we have to create a new
|
---|
259 | * dummy context. */
|
---|
260 | if (wi->bufferName == -1)
|
---|
261 | {
|
---|
262 | /* Use the same visual bits as those in the context structure */
|
---|
263 | AGLPixelFormat pix;
|
---|
264 | if( !renderspuChoosePixelFormat(context, &pix) )
|
---|
265 | {
|
---|
266 | crError( "Render SPU: Unable to create pixel format" );
|
---|
267 | return GL_FALSE;
|
---|
268 | }
|
---|
269 | /* Create the dummy context */
|
---|
270 | wi->dummyContext = render_spu.ws.aglCreateContext( pix, NULL );
|
---|
271 | renderspuDestroyPixelFormat( context, &pix );
|
---|
272 | if( !wi->dummyContext )
|
---|
273 | {
|
---|
274 | crError( "Render SPU: Could not create rendering context" );
|
---|
275 | return GL_FALSE;
|
---|
276 | }
|
---|
277 | AGLDrawable drawable;
|
---|
278 | drawable = (AGLDrawable) GetWindowPort(window);
|
---|
279 | /* New global buffer name */
|
---|
280 | wi->bufferName = gCurrentBufferName++;
|
---|
281 | /* Set the new buffer name to the dummy context. This enable the
|
---|
282 | * sharing of the same hardware buffer afterwards. */
|
---|
283 | result = render_spu.ws.aglSetInteger(wi->dummyContext, AGL_BUFFER_NAME, &wi->bufferName);
|
---|
284 | CHECK_AGL_RC (result, "Render SPU: SetInteger Failed");
|
---|
285 | /* Assign the dummy context to the window */
|
---|
286 | result = render_spu.ws.aglSetDrawable(wi->dummyContext, drawable);
|
---|
287 | CHECK_AGL_RC (result, "Render SPU: SetDrawable Failed");
|
---|
288 | }
|
---|
289 |
|
---|
290 | AGLDrawable oldDrawable;
|
---|
291 | AGLDrawable newDrawable;
|
---|
292 |
|
---|
293 | oldDrawable = render_spu.ws.aglGetDrawable(context->context);
|
---|
294 | newDrawable = (AGLDrawable) GetWindowPort(window);
|
---|
295 | /* Only switch the context if the drawable has changed */
|
---|
296 | if (oldDrawable != newDrawable)
|
---|
297 | {
|
---|
298 | /* Reset the current context */
|
---|
299 | result = render_spu.ws.aglSetDrawable(context->context, NULL);
|
---|
300 | CHECK_AGL_RC (result, "Render SPU: SetDrawable Failed");
|
---|
301 | /* Set the buffer name of the dummy context to the current context
|
---|
302 | * also. After that both share the same hardware buffer. */
|
---|
303 | render_spu.ws.aglSetInteger (context->context, AGL_BUFFER_NAME, &wi->bufferName);
|
---|
304 | CHECK_AGL_RC (result, "Render SPU: SetInteger Failed");
|
---|
305 | /* Set the new drawable */
|
---|
306 | result = render_spu.ws.aglSetDrawable(context->context, newDrawable);
|
---|
307 | CHECK_AGL_RC (result, "Render SPU: SetDrawable Failed");
|
---|
308 | renderspuSetWindowContext( window, context );
|
---|
309 | }
|
---|
310 | result = render_spu.ws.aglSetCurrentContext(context->context);
|
---|
311 | CHECK_AGL_RC (result, "Render SPU: SetCurrentContext Failed");
|
---|
312 | result = render_spu.ws.aglUpdateContext(context->context);
|
---|
313 | CHECK_AGL_RC (result, "Render SPU: UpdateContext Failed");
|
---|
314 |
|
---|
315 | return result;
|
---|
316 | }
|
---|
317 |
|
---|
318 | GLboolean
|
---|
319 | renderspu_SystemCreateWindow(VisualInfo *visual, GLboolean showIt,
|
---|
320 | WindowInfo *window)
|
---|
321 | {
|
---|
322 | return GL_TRUE;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void
|
---|
326 | renderspu_SystemDestroyWindow(WindowInfo *window)
|
---|
327 | {
|
---|
328 | CRASSERT(window);
|
---|
329 | CRASSERT(window->visual);
|
---|
330 |
|
---|
331 | if(!window->nativeWindow)
|
---|
332 | DisposeWindow(window->window);
|
---|
333 |
|
---|
334 | /* Delete the dummy context */
|
---|
335 | if(window->dummyContext)
|
---|
336 | {
|
---|
337 | render_spu.ws.aglSetDrawable(window->dummyContext, NULL);
|
---|
338 | render_spu.ws.aglDestroyContext(window->dummyContext);
|
---|
339 | window->dummyContext = NULL;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* Reset some values */
|
---|
343 | window->bufferName = -1;
|
---|
344 | window->visual = NULL;
|
---|
345 | window->window = NULL;
|
---|
346 | }
|
---|
347 |
|
---|
348 | void
|
---|
349 | renderspu_SystemWindowSize(WindowInfo *window, GLint w, GLint h)
|
---|
350 | {
|
---|
351 | CRASSERT(window);
|
---|
352 | CRASSERT(window->window);
|
---|
353 |
|
---|
354 | OSStatus status = noErr;
|
---|
355 | /* Send a event to the main thread, cause some function of Carbon aren't
|
---|
356 | * thread safe */
|
---|
357 | EventRef evt;
|
---|
358 | status = CreateEvent(NULL, kEventClassVBox, kEventVBoxResizeWindow, 0, kEventAttributeNone, &evt);
|
---|
359 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: CreateEvent Failed");
|
---|
360 | status = SetEventParameter(evt, kEventParamWindowRef, typeWindowRef, sizeof(window->window), &window->window);
|
---|
361 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: SetEventParameter Failed");
|
---|
362 | HISize s = CGSizeMake (w, h);
|
---|
363 | status = SetEventParameter(evt, kEventParamDimensions, typeHISize, sizeof (s), &s);
|
---|
364 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: SetEventParameter Failed");
|
---|
365 | status = PostEventToQueue(GetMainEventQueue(), evt, kEventPriorityStandard);
|
---|
366 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: PostEventToQueue Failed");
|
---|
367 |
|
---|
368 | /* Update the context */
|
---|
369 | GLboolean result = true;
|
---|
370 | ContextInfo *context = renderspuGetWindowContext(window);
|
---|
371 | if (context &&
|
---|
372 | context->context)
|
---|
373 | {
|
---|
374 | result = render_spu.ws.aglUpdateContext(context->context);
|
---|
375 | CHECK_AGL_RC (result, "Render SPU: UpdateContext Failed");
|
---|
376 | }
|
---|
377 | /* save the new size */
|
---|
378 | window->width = w;
|
---|
379 | window->height = h;
|
---|
380 | }
|
---|
381 |
|
---|
382 | void
|
---|
383 | renderspu_SystemGetWindowGeometry(WindowInfo *window,
|
---|
384 | GLint *x, GLint *y,
|
---|
385 | GLint *w, GLint *h)
|
---|
386 | {
|
---|
387 | CRASSERT(window);
|
---|
388 | CRASSERT(window->window);
|
---|
389 |
|
---|
390 | OSStatus status = noErr;
|
---|
391 | Rect r;
|
---|
392 | status = GetWindowBounds(window->window, kWindowStructureRgn, &r);
|
---|
393 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: GetWindowBounds Failed");
|
---|
394 |
|
---|
395 | *x = (int) r.left;
|
---|
396 | *y = (int) r.top;
|
---|
397 | *w = (int) (r.right - r.left);
|
---|
398 | *h = (int) (r.bottom - r.top);
|
---|
399 | }
|
---|
400 |
|
---|
401 | void
|
---|
402 | renderspu_SystemGetMaxWindowSize(WindowInfo *window,
|
---|
403 | GLint *w, GLint *h)
|
---|
404 | {
|
---|
405 | CRASSERT(window);
|
---|
406 | CRASSERT(window->window);
|
---|
407 |
|
---|
408 | OSStatus status = noErr;
|
---|
409 | HISize s;
|
---|
410 | status = GetWindowResizeLimits (window->window, NULL, &s);
|
---|
411 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: GetWindowResizeLimits Failed");
|
---|
412 |
|
---|
413 | *w = s.width;
|
---|
414 | *h = s.height;
|
---|
415 | }
|
---|
416 |
|
---|
417 | void
|
---|
418 | renderspu_SystemWindowPosition(WindowInfo *window,
|
---|
419 | GLint x, GLint y)
|
---|
420 | {
|
---|
421 | CRASSERT(window);
|
---|
422 | CRASSERT(window->window);
|
---|
423 |
|
---|
424 | OSStatus status = noErr;
|
---|
425 | /* Send a event to the main thread, cause some function of Carbon aren't
|
---|
426 | * thread safe */
|
---|
427 | EventRef evt;
|
---|
428 | status = CreateEvent(NULL, kEventClassVBox, kEventVBoxMoveWindow, 0, kEventAttributeNone, &evt);
|
---|
429 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: CreateEvent Failed");
|
---|
430 | status = SetEventParameter(evt, kEventParamWindowRef, typeWindowRef, sizeof(window->window), &window->window);
|
---|
431 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: SetEventParameter Failed");
|
---|
432 | HIPoint p = CGPointMake (x, y);
|
---|
433 | status = SetEventParameter(evt, kEventParamOrigin, typeHIPoint, sizeof (p), &p);
|
---|
434 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: SetEventParameter Failed");
|
---|
435 | status = PostEventToQueue(GetMainEventQueue(), evt, kEventPriorityStandard);
|
---|
436 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: PostEventToQueue Failed");
|
---|
437 |
|
---|
438 | /* Update the context */
|
---|
439 | GLboolean result = true;
|
---|
440 | ContextInfo *context = renderspuGetWindowContext(window);
|
---|
441 | if (context &&
|
---|
442 | context->context)
|
---|
443 | {
|
---|
444 | result = render_spu.ws.aglUpdateContext(context->context);
|
---|
445 | CHECK_AGL_RC (result, "Render SPU: UpdateContext Failed");
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | /* Either show or hide the render SPU's window. */
|
---|
450 | void
|
---|
451 | renderspu_SystemShowWindow(WindowInfo *window, GLboolean showIt)
|
---|
452 | {
|
---|
453 | CRASSERT(window);
|
---|
454 | CRASSERT(window->window);
|
---|
455 |
|
---|
456 | if (!IsValidWindowPtr(window->window))
|
---|
457 | return;
|
---|
458 |
|
---|
459 | if(showIt)
|
---|
460 | {
|
---|
461 | /* Force moving the win to the right position before we show it */
|
---|
462 | renderspu_SystemWindowPosition (window, window->x, window->y);
|
---|
463 | OSStatus status = noErr;
|
---|
464 | /* Send a event to the main thread, cause some function of Carbon
|
---|
465 | * aren't thread safe */
|
---|
466 | EventRef evt;
|
---|
467 | status = CreateEvent(NULL, kEventClassVBox, kEventVBoxShowWindow, 0, kEventAttributeNone, &evt);
|
---|
468 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: CreateEvent Failed");
|
---|
469 | status = SetEventParameter(evt, kEventParamWindowRef, typeWindowRef, sizeof (window->window), &window->window);
|
---|
470 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: SetEventParameter Failed");
|
---|
471 | status = PostEventToQueue(GetMainEventQueue(), evt, kEventPriorityStandard);
|
---|
472 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: PostEventToQueue Failed");
|
---|
473 | }
|
---|
474 | else
|
---|
475 | HideWindow(window->window);
|
---|
476 |
|
---|
477 | /* Update the context */
|
---|
478 | GLboolean result = true;
|
---|
479 | ContextInfo *context = renderspuGetWindowContext(window);
|
---|
480 | if (context &&
|
---|
481 | context->context)
|
---|
482 | {
|
---|
483 | result = render_spu.ws.aglUpdateContext(context->context);
|
---|
484 | CHECK_AGL_RC (result, "Render SPU: UpdateContext Failed");
|
---|
485 | }
|
---|
486 |
|
---|
487 | window->visible = showIt;
|
---|
488 | }
|
---|
489 |
|
---|
490 | void
|
---|
491 | renderspu_SystemMakeCurrent(WindowInfo *window, GLint nativeWindow,
|
---|
492 | ContextInfo *context)
|
---|
493 | {
|
---|
494 | Boolean result;
|
---|
495 |
|
---|
496 | CRASSERT(render_spu.ws.aglSetCurrentContext);
|
---|
497 | /* crDebug( "renderspu_SystemMakeCurrent( %x, %i, %x )", window, nativeWindow, context );*/
|
---|
498 |
|
---|
499 | if(window && context)
|
---|
500 | {
|
---|
501 | CRASSERT(window->window);
|
---|
502 | CRASSERT(context->context);
|
---|
503 |
|
---|
504 | if(window->visual != context->visual)
|
---|
505 | {
|
---|
506 | crDebug("Render SPU: MakeCurrent visual mismatch (0x%x != 0x%x); remaking window.",
|
---|
507 | (uint)window->visual->visAttribs, (uint)context->visual->visAttribs);
|
---|
508 | /*
|
---|
509 | * XXX have to revisit this issue!!!
|
---|
510 | *
|
---|
511 | * But for now we destroy the current window
|
---|
512 | * and re-create it with the context's visual abilities
|
---|
513 | */
|
---|
514 | renderspu_SystemDestroyWindow(window);
|
---|
515 | renderspu_SystemCreateWindow(context->visual, window->visible,
|
---|
516 | window);
|
---|
517 | }
|
---|
518 |
|
---|
519 | /* This is the normal case: rendering to the render SPU's own window */
|
---|
520 | result = renderspuWindowAttachContext(window, window->window,
|
---|
521 | context);
|
---|
522 | /* XXX this is a total hack to work around an NVIDIA driver bug */
|
---|
523 | if(render_spu.self.GetFloatv && context->haveWindowPosARB)
|
---|
524 | {
|
---|
525 | GLfloat f[4];
|
---|
526 | render_spu.self.GetFloatv(GL_CURRENT_RASTER_POSITION, f);
|
---|
527 | if (!window->everCurrent || f[1] < 0.0)
|
---|
528 | {
|
---|
529 | crDebug("Render SPU: Resetting raster pos");
|
---|
530 | render_spu.self.WindowPos2iARB(0, 0);
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | else
|
---|
535 | renderspuWindowAttachContext (0, 0, 0);
|
---|
536 | }
|
---|
537 |
|
---|
538 | void
|
---|
539 | renderspu_SystemSwapBuffers(WindowInfo *window, GLint flags)
|
---|
540 | {
|
---|
541 | CRASSERT(window);
|
---|
542 | CRASSERT(window->window);
|
---|
543 |
|
---|
544 | ContextInfo *context = renderspuGetWindowContext(window);
|
---|
545 |
|
---|
546 | if(!context)
|
---|
547 | crError("Render SPU: SwapBuffers got a null context from the window");
|
---|
548 |
|
---|
549 | render_spu.ws.aglSwapBuffers(context->context);
|
---|
550 |
|
---|
551 |
|
---|
552 | /* This method seems called very often. To prevent the dock using all free
|
---|
553 | * resources we update the dock only two times per second. */
|
---|
554 | uint64_t curTS = RTTimeMilliTS();
|
---|
555 | if ((curTS - gDockUpdateTS) > 500)
|
---|
556 | {
|
---|
557 | OSStatus status = noErr;
|
---|
558 | /* Send a event to the main thread, cause some function of Carbon aren't
|
---|
559 | * thread safe */
|
---|
560 | EventRef evt;
|
---|
561 | status = CreateEvent(NULL, kEventClassVBox, kEventVBoxUpdateDock, 0, kEventAttributeNone, &evt);
|
---|
562 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: CreateEvent Failed");
|
---|
563 | status = PostEventToQueue(GetMainEventQueue(), evt, kEventPriorityStandard);
|
---|
564 | CHECK_CARBON_RC_RETURN_VOID (status, "Render SPU: PostEventToQueue Failed");
|
---|
565 |
|
---|
566 | gDockUpdateTS = curTS;
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | void renderspu_SystemWindowVisibleRegion(WindowInfo *window, GLint cRects, GLint* pRects)
|
---|
571 | {
|
---|
572 | CRASSERT(window);
|
---|
573 | CRASSERT(window->window);
|
---|
574 |
|
---|
575 | ContextInfo *c;
|
---|
576 | c = renderspuGetWindowContext (window);
|
---|
577 | if (c &&
|
---|
578 | c->context)
|
---|
579 | {
|
---|
580 | int i;
|
---|
581 | /* Create some temporary regions */
|
---|
582 | RgnHandle rgn = NewRgn();
|
---|
583 | SetEmptyRgn (rgn);
|
---|
584 | RgnHandle tmpRgn = NewRgn();
|
---|
585 | for (i=0; i<cRects; ++i)
|
---|
586 | {
|
---|
587 | SetRectRgn (tmpRgn,
|
---|
588 | pRects[4*i] , pRects[4*i+1],
|
---|
589 | pRects[4*i+2], pRects[4*i+3]);
|
---|
590 | UnionRgn (rgn, tmpRgn, rgn);
|
---|
591 | }
|
---|
592 | DisposeRgn (tmpRgn);
|
---|
593 |
|
---|
594 | GLboolean result = true;
|
---|
595 | /* Set the clip region to the context */
|
---|
596 | result = render_spu.ws.aglSetInteger(c->context, AGL_CLIP_REGION, (const GLint*)rgn);
|
---|
597 | CHECK_AGL_RC (result, "Render SPU: SetInteger Failed");
|
---|
598 | result = render_spu.ws.aglEnable(c->context, AGL_CLIP_REGION);
|
---|
599 | CHECK_AGL_RC (result, "Render SPU: Enable Failed");
|
---|
600 | /* Clear the region structure */
|
---|
601 | DisposeRgn (rgn);
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | GLboolean
|
---|
606 | renderspu_SystemVBoxCreateWindow(VisualInfo *visual, GLboolean showIt,
|
---|
607 | WindowInfo *window)
|
---|
608 | {
|
---|
609 | CRASSERT(visual);
|
---|
610 | CRASSERT(window);
|
---|
611 |
|
---|
612 | WindowAttributes winAttr = kWindowNoShadowAttribute | kWindowCompositingAttribute | kWindowIgnoreClicksAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute;
|
---|
613 | WindowClass winClass = kOverlayWindowClass;
|
---|
614 | Rect windowRect;
|
---|
615 | OSStatus status = noErr;
|
---|
616 |
|
---|
617 | window->visual = visual;
|
---|
618 | window->nativeWindow = NULL;
|
---|
619 |
|
---|
620 | if(window->window && IsValidWindowPtr(window->window))
|
---|
621 | /* Destroy the old one */
|
---|
622 | DisposeWindow(window->window);
|
---|
623 |
|
---|
624 | windowRect.left = window->x;
|
---|
625 | windowRect.top = window->y;
|
---|
626 | windowRect.right = window->x + window->width;
|
---|
627 | windowRect.bottom = window->y + window->height;
|
---|
628 |
|
---|
629 | status = CreateNewWindow(winClass, winAttr, &windowRect, &window->window);
|
---|
630 | CHECK_CARBON_RC_RETURN (status, "Render SPU: CreateNewWindow Failed", GL_FALSE);
|
---|
631 |
|
---|
632 | /* We set a title for debugging purposes */
|
---|
633 | CFStringRef title_string;
|
---|
634 | title_string = CFStringCreateWithCStringNoCopy(NULL, window->title,
|
---|
635 | kCFStringEncodingMacRoman, NULL);
|
---|
636 | SetWindowTitleWithCFString(window->window, title_string);
|
---|
637 | CFRelease(title_string);
|
---|
638 |
|
---|
639 | /* We need grouping so create a master group for this & all following
|
---|
640 | * windows & one group for the parent. */
|
---|
641 | if(!gMasterGroup || !gParentGroup)
|
---|
642 | {
|
---|
643 | status = CreateWindowGroup(kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrSharedActivation | kWindowGroupAttrHideOnCollapse | kWindowGroupAttrFixedLevel, &gMasterGroup);
|
---|
644 | CHECK_CARBON_RC_RETURN (status, "Render SPU: CreateWindowGroup Failed", GL_FALSE);
|
---|
645 | status = CreateWindowGroup(kWindowGroupAttrMoveTogether | kWindowGroupAttrLayerTogether | kWindowGroupAttrSharedActivation | kWindowGroupAttrHideOnCollapse | kWindowGroupAttrFixedLevel, &gParentGroup);
|
---|
646 | CHECK_CARBON_RC_RETURN (status, "Render SPU: CreateWindowGroup Failed", GL_FALSE);
|
---|
647 | /* Make the correct z-layering */
|
---|
648 | SendWindowGroupBehind (gParentGroup, gMasterGroup);
|
---|
649 | /* and set the gParentGroup as parent for gMasterGroup. */
|
---|
650 | SetWindowGroupParent (gMasterGroup, gParentGroup);
|
---|
651 | }
|
---|
652 |
|
---|
653 | /* The parent has to be in its own group */
|
---|
654 | WindowRef parent = NULL;
|
---|
655 | if (render_spu_parent_window_id)
|
---|
656 | {
|
---|
657 | parent = HIViewGetWindow ((HIViewRef)render_spu_parent_window_id);
|
---|
658 | SetWindowGroup (parent, gParentGroup);
|
---|
659 | }
|
---|
660 | /* Add the new window to the master group */
|
---|
661 | SetWindowGroup(window->window, gMasterGroup);
|
---|
662 |
|
---|
663 | /* Own handler needed? */
|
---|
664 | {
|
---|
665 | /* Even though there are still issues with the windows themselves,
|
---|
666 | install the event handlers */
|
---|
667 | EventTypeSpec event_list[] = { {kEventClassWindow, kEventWindowClose} };
|
---|
668 |
|
---|
669 | window->event_handler = NewEventHandlerUPP( windowEvtHndlr );
|
---|
670 |
|
---|
671 | /*InstallWindowEventHandler(window->window, window->event_handler,
|
---|
672 | GetEventTypeCount(event_list), event_list,
|
---|
673 | NULL, NULL);*/
|
---|
674 | }
|
---|
675 |
|
---|
676 | /* This will be initialized on the first attempt to attach the global
|
---|
677 | * context to this new window */
|
---|
678 | window->bufferName = -1;
|
---|
679 | window->dummyContext = NULL;
|
---|
680 |
|
---|
681 | if(showIt)
|
---|
682 | renderspu_SystemShowWindow(window, GL_TRUE);
|
---|
683 |
|
---|
684 | crDebug("Render SPU: actual window (x, y, width, height): %d, %d, %d, %d",
|
---|
685 | window->x, window->y, window->width, window->height);
|
---|
686 |
|
---|
687 | return GL_TRUE;
|
---|
688 | }
|
---|
689 |
|
---|