1 | /* $Id: DevVGA-SVGA3d-cocoa.m 57135 2015-07-30 19:41:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox OpenGL Cocoa Window System Helper Implementation.
|
---|
4 | *
|
---|
5 | * @remarks Inspired by HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2009-2015 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #define LOG_GROUP LOG_GROUP_DEV_VMSVGA
|
---|
24 | #include "DevVGA-SVGA3d-cocoa.h"
|
---|
25 | #import <Cocoa/Cocoa.h>
|
---|
26 | #import <OpenGL/gl.h>
|
---|
27 |
|
---|
28 | #include <iprt/thread.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <VBox/log.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Defined Constants And Macros *
|
---|
36 | *******************************************************************************/
|
---|
37 | /** @def USE_NSOPENGLVIEW
|
---|
38 | * Define this to experiment with using NSOpenGLView instead
|
---|
39 | * of NSView. There are transparency issues with the former,
|
---|
40 | * so for the time being we're using the latter. */
|
---|
41 | #if 0
|
---|
42 | #define USE_NSOPENGLVIEW
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 | /**
|
---|
50 | * Argument package for doing this on the main thread.
|
---|
51 | */
|
---|
52 | @interface VMSVGA3DCreateViewAndContext : NSObject
|
---|
53 | {
|
---|
54 | @public
|
---|
55 | /* in */
|
---|
56 | NativeNSViewRef pParentView;
|
---|
57 | uint32_t cx;
|
---|
58 | uint32_t cy;
|
---|
59 | NativeNSOpenGLContextRef pSharedCtx;
|
---|
60 | bool fOtherProfile;
|
---|
61 |
|
---|
62 | /* out */
|
---|
63 | NativeNSViewRef pView;
|
---|
64 | NativeNSOpenGLContextRef pCtx;
|
---|
65 | }
|
---|
66 | @end
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * The overlay view.
|
---|
71 | */
|
---|
72 | @interface VMSVGA3DOverlayView
|
---|
73 | #ifdef USE_NSOPENGLVIEW
|
---|
74 | : NSOpenGLView
|
---|
75 | #else
|
---|
76 | : NSView
|
---|
77 | #endif
|
---|
78 | {
|
---|
79 | @private
|
---|
80 | /** This points to the parent view, if there is one. If there isn't a parent
|
---|
81 | * the view will be hidden and never used for displaying stuff. We only have
|
---|
82 | * one visible context per guest screen that is visible to the user and
|
---|
83 | * subject to buffer swapping. */
|
---|
84 | NSView *m_pParentView;
|
---|
85 | /** Indicates that buffers (back+front) needs clearing before use because
|
---|
86 | * the view changed size. There are two buffers, so this is set to two
|
---|
87 | * each time when the view area increases. */
|
---|
88 | uint32_t m_cClears;
|
---|
89 | /** Set if the OpenGL context needs updating after a resize. */
|
---|
90 | bool m_fUpdateCtx;
|
---|
91 |
|
---|
92 | #ifndef USE_NSOPENGLVIEW
|
---|
93 | /** The OpenGL context associated with this view. */
|
---|
94 | NSOpenGLContext *m_pCtx;
|
---|
95 | /** Number of times we've tried to set the view (shut up noisy NSLog). */
|
---|
96 | uint32_t m_cSetViewAttempts;
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /** The desired view position relative to super. */
|
---|
100 | NSPoint m_Pos;
|
---|
101 | /** The desired view size. */
|
---|
102 | NSSize m_Size;
|
---|
103 | }
|
---|
104 | + (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams;
|
---|
105 | - (id)initWithFrameAndFormat:(NSRect)frame parentView:(NSView*)pparentView pixelFormat:(NSOpenGLPixelFormat *)pFmt;
|
---|
106 | - (void)vboxSetPos:(NSPoint)pos;
|
---|
107 | - (void)vboxSetSize:(NSSize)size;
|
---|
108 | - (void)vboxReshapePerform;
|
---|
109 | - (void)vboxReshape;
|
---|
110 | #if 0 // doesn't work or isn't needed :/
|
---|
111 | - (void)vboxFrameDidChange;
|
---|
112 | - (BOOL)postsFrameChangedNotifications;
|
---|
113 | #endif
|
---|
114 | - (void)vboxRemoveFromSuperviewAndHide;
|
---|
115 | - (void)vboxUpdateCtxIfNecessary;
|
---|
116 | - (void)vboxClearBackBufferIfNecessary;
|
---|
117 | - (NSOpenGLContext *)makeCurrentGLContext;
|
---|
118 | - (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx;
|
---|
119 |
|
---|
120 | #ifndef USE_NSOPENGLVIEW
|
---|
121 | /* NSOpenGLView fakes: */
|
---|
122 | - (void)setOpenGLContext:(NSOpenGLContext *)pCtx;
|
---|
123 | - (NSOpenGLContext *)openGLContext;
|
---|
124 | - (void)prepareOpenGL;
|
---|
125 |
|
---|
126 | #endif
|
---|
127 | /* Overridden: */
|
---|
128 | - (void)viewDidMoveToWindow;
|
---|
129 | - (void)viewDidMoveToSuperview;
|
---|
130 | - (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize;
|
---|
131 | - (void)drawRect:(NSRect)rect;
|
---|
132 |
|
---|
133 | @end
|
---|
134 |
|
---|
135 |
|
---|
136 | /********************************************************************************
|
---|
137 | *
|
---|
138 | * VMSVGA3DOverlayView class implementation
|
---|
139 | *
|
---|
140 | ********************************************************************************/
|
---|
141 | @implementation VMSVGA3DOverlayView
|
---|
142 |
|
---|
143 |
|
---|
144 | + (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams
|
---|
145 | {
|
---|
146 | LogFlow(("OvlView createViewAndContext:\n"));
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Create a pixel format.
|
---|
150 | */
|
---|
151 | NSOpenGLPixelFormat *pFmt = nil;
|
---|
152 |
|
---|
153 | // Consider to remove it and check if it's harmless.
|
---|
154 | NSOpenGLPixelFormatAttribute attribs[] =
|
---|
155 | {
|
---|
156 | NSOpenGLPFAOpenGLProfile, (NSOpenGLPixelFormatAttribute)0,
|
---|
157 | //NSOpenGLPFAWindow, - obsolete/deprecated, try work without it...
|
---|
158 | NSOpenGLPFAAccelerated,
|
---|
159 | NSOpenGLPFADoubleBuffer,
|
---|
160 | NSOpenGLPFABackingStore,
|
---|
161 | NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
|
---|
162 | NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8,
|
---|
163 | NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
|
---|
164 | 0
|
---|
165 | };
|
---|
166 | attribs[1] = pParams->fOtherProfile ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy;
|
---|
167 | pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
---|
168 | if (pFmt)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Create a new view.
|
---|
172 | */
|
---|
173 | NSRect Frame;
|
---|
174 | Frame.origin.x = 0;
|
---|
175 | Frame.origin.y = 0;
|
---|
176 | Frame.size.width = pParams->cx < _1M && pParams->cx > 0 ? pParams->cx : 1; /* 'invalid drawable' if 0,0 size? */
|
---|
177 | Frame.size.height = pParams->cy < _1M && pParams->cy > 0 ? pParams->cy : 1;
|
---|
178 | VMSVGA3DOverlayView *pView = [[VMSVGA3DOverlayView alloc] initWithFrameAndFormat:Frame
|
---|
179 | parentView:pParams->pParentView
|
---|
180 | pixelFormat:pFmt];
|
---|
181 | if (pView)
|
---|
182 | {
|
---|
183 | /*
|
---|
184 | * If we have no shared GL context, we use the one that NSOpenGLView create. Otherwise,
|
---|
185 | * we replace it. (If we don't call openGLContext, it won't yet have been instantiated,
|
---|
186 | * so there is no unecessary contexts created here when pSharedCtx != NULL.)
|
---|
187 | */
|
---|
188 | NSOpenGLContext *pCtx;
|
---|
189 | #ifdef USE_NSOPENGLVIEW
|
---|
190 | if (!pParams->pSharedCtx)
|
---|
191 | pCtx = [pView openGLContext];
|
---|
192 | else
|
---|
193 | #endif
|
---|
194 | {
|
---|
195 | pCtx = [[NSOpenGLContext alloc] initWithFormat:pFmt shareContext: pParams->pSharedCtx];
|
---|
196 | if (pCtx)
|
---|
197 | {
|
---|
198 | [pView setOpenGLContext:pCtx];
|
---|
199 | [pCtx setView:pView];
|
---|
200 | #ifdef USE_NSOPENGLVIEW
|
---|
201 | Assert([pCtx view] == pView);
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 | }
|
---|
205 | if (pCtx)
|
---|
206 | {
|
---|
207 | /*
|
---|
208 | * Attach the view to the parent if we have one. Otherwise make sure its invisible.
|
---|
209 | */
|
---|
210 | if (pParams->pParentView)
|
---|
211 | [pParams->pParentView addSubview:pView];
|
---|
212 | else
|
---|
213 | [pView setHidden:YES];
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Resize and return.
|
---|
217 | */
|
---|
218 | //[pView vboxSetSize:Frame.size];
|
---|
219 |
|
---|
220 | NSOpenGLContext *pSavedCtx = [pView makeCurrentGLContext];
|
---|
221 |
|
---|
222 | [pView prepareOpenGL];
|
---|
223 | GLint x;
|
---|
224 | //x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSwapInterval];
|
---|
225 | //x = 1; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOrder];
|
---|
226 | x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOpacity];
|
---|
227 |
|
---|
228 | if (pParams->pParentView)
|
---|
229 | [pView setHidden:NO];
|
---|
230 | else
|
---|
231 | [pView setHidden:YES];
|
---|
232 |
|
---|
233 | [pView restoreSavedGLContext:pSavedCtx];
|
---|
234 |
|
---|
235 | pParams->pView = pView;
|
---|
236 | pParams->pCtx = pCtx;
|
---|
237 | [pCtx retain]; //??
|
---|
238 |
|
---|
239 | [pFmt release];
|
---|
240 |
|
---|
241 | #if 0 // doesn't work or isn't needed :/
|
---|
242 | /*
|
---|
243 | * Get notifications when we're moved...
|
---|
244 | */
|
---|
245 | if (pParams->pParentView)
|
---|
246 | {
|
---|
247 | [[NSNotificationCenter defaultCenter] addObserver:self
|
---|
248 | selector:@selector(vboxFrameDidChange)
|
---|
249 | name:NSViewFrameDidChangeNotification
|
---|
250 | object:self];
|
---|
251 | }
|
---|
252 | #endif
|
---|
253 |
|
---|
254 | LogFlow(("OvlView createViewAndContext: returns successfully\n"));
|
---|
255 | return;
|
---|
256 | }
|
---|
257 | [pView release];
|
---|
258 | }
|
---|
259 | [pFmt release];
|
---|
260 | }
|
---|
261 | else
|
---|
262 | AssertFailed();
|
---|
263 |
|
---|
264 | LogFlow(("OvlView createViewAndContext: returns failure\n"));
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | - (id)initWithFrameAndFormat:(NSRect) frame parentView:(NSView *)pParentView pixelFormat:(NSOpenGLPixelFormat *)pFmt
|
---|
269 | {
|
---|
270 | LogFlow(("OvlView(%p) initWithFrameAndFormat:\n", (void *)self));
|
---|
271 |
|
---|
272 | m_pParentView = pParentView;
|
---|
273 | /* Make some reasonable defaults */
|
---|
274 | m_Pos = NSZeroPoint;
|
---|
275 | m_Size = frame.size;
|
---|
276 | m_cClears = 2;
|
---|
277 | m_fUpdateCtx = true;
|
---|
278 |
|
---|
279 | #ifdef USE_NSOPENGLVIEW
|
---|
280 | self = [super initWithFrame:frame pixelFormat:pFmt];
|
---|
281 | #else
|
---|
282 | m_cSetViewAttempts = 0;
|
---|
283 | m_pCtx = NULL;
|
---|
284 | self = [super initWithFrame:frame];
|
---|
285 | #endif
|
---|
286 | if (self)
|
---|
287 | {
|
---|
288 | //self.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;
|
---|
289 | self.autoresizingMask = NSViewNotSizable;
|
---|
290 | }
|
---|
291 | LogFlow(("OvlView(%p) initWithFrameAndFormat: returns %p\n", (void *)self, (void *)self));
|
---|
292 | return self;
|
---|
293 | }
|
---|
294 |
|
---|
295 | - (void)dealloc
|
---|
296 | {
|
---|
297 | LogFlow(("OvlView(%p) dealloc:\n", (void *)self));
|
---|
298 |
|
---|
299 | #ifdef USE_NSOPENGLVIEW
|
---|
300 | [[self openGLContext] clearDrawable];
|
---|
301 | #else
|
---|
302 | if (m_pCtx)
|
---|
303 | {
|
---|
304 | [m_pCtx clearDrawable];
|
---|
305 | [m_pCtx release];
|
---|
306 | m_pCtx = nil;
|
---|
307 | }
|
---|
308 | #endif
|
---|
309 |
|
---|
310 | [super dealloc];
|
---|
311 |
|
---|
312 | LogFlow(("OvlView(%p) dealloc: returns\n", (void *)self));
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | - (void)vboxSetPos:(NSPoint)pos
|
---|
317 | {
|
---|
318 | Log(("OvlView(%p) vboxSetPos: (%d,%d)\n", (void *)self, (int)pos.x, (int)pos.y));
|
---|
319 |
|
---|
320 | m_Pos = pos;
|
---|
321 | [self vboxReshape];
|
---|
322 |
|
---|
323 | LogFlow(("OvlView(%p) vboxSetPos: returns\n", (void *)self));
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | - (void)vboxSetSize:(NSSize)size
|
---|
328 | {
|
---|
329 | Log(("OvlView(%p) vboxSetSize: (%d,%d):\n", (void *)self, (int)size.width, (int)size.height));
|
---|
330 | m_Size = size;
|
---|
331 | [self vboxReshape];
|
---|
332 | LogFlow(("OvlView(%p) vboxSetSize: returns\n", (void *)self));
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | - (void)vboxUpdateCtxIfNecessary
|
---|
337 | {
|
---|
338 | if (m_fUpdateCtx)
|
---|
339 | {
|
---|
340 | Log(("OvlView(%p) vboxUpdateCtxIfNecessary: m_fUpdateCtx\n", (void *)self));
|
---|
341 | [[self openGLContext] update];
|
---|
342 | m_fUpdateCtx = false;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | - (void)vboxClearBackBufferIfNecessary
|
---|
348 | {
|
---|
349 | #if 1 /* experiment */
|
---|
350 | if (m_cClears > 0)
|
---|
351 | {
|
---|
352 | Assert(![NSThread isMainThread]);
|
---|
353 | Assert([self openGLContext] == [NSOpenGLContext currentContext]);
|
---|
354 | Log(("OvlView(%p) vboxClearBackBufferIfNecessary: m_cClears=%d\n", (void *)self, m_cClears));
|
---|
355 | m_cClears--;
|
---|
356 |
|
---|
357 | /* Clear errors. */
|
---|
358 | GLenum rc;
|
---|
359 | while ((rc = glGetError()) != GL_NO_ERROR)
|
---|
360 | continue;
|
---|
361 |
|
---|
362 | /* Save the old buffer setting and make it GL_BACK (shall be GL_BACK already actually). */
|
---|
363 | GLint iOldDrawBuf = GL_BACK;
|
---|
364 | glGetIntegerv(GL_DRAW_BUFFER, &iOldDrawBuf);
|
---|
365 | if (iOldDrawBuf != GL_BACK)
|
---|
366 | glDrawBuffer(GL_BACK);
|
---|
367 | while ((rc = glGetError()) != GL_NO_ERROR)
|
---|
368 | AssertMsgFailed(("rc=%x\n", rc));
|
---|
369 |
|
---|
370 | /* Clear the current GL_BACK. */
|
---|
371 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
---|
372 | glClear(GL_COLOR_BUFFER_BIT /*|GL_DEPTH_BUFFER_BIT*/ );
|
---|
373 | while ((rc = glGetError()) != GL_NO_ERROR)
|
---|
374 | AssertMsgFailed(("rc=%x\n", rc));
|
---|
375 |
|
---|
376 | /* We're back to the orignal back buffer now. Just restore GL_DRAW_BUFFER. */
|
---|
377 | if (iOldDrawBuf != GL_BACK)
|
---|
378 | glDrawBuffer(iOldDrawBuf);
|
---|
379 |
|
---|
380 | while ((rc = glGetError()) != GL_NO_ERROR)
|
---|
381 | AssertMsgFailed(("rc=%x\n", rc));
|
---|
382 | }
|
---|
383 | #endif
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 |
|
---|
388 | - (void)vboxReshapePerform
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * Change the size and position if necessary.
|
---|
392 | */
|
---|
393 | NSRect CurFrameRect = [self frame];
|
---|
394 | /** @todo conversions? */
|
---|
395 | if ( m_Pos.x != CurFrameRect.origin.x
|
---|
396 | || m_Pos.y != CurFrameRect.origin.y)
|
---|
397 | {
|
---|
398 | LogFlow(("OvlView(%p) vboxReshapePerform: moving (%d,%d) -> (%d,%d)\n",
|
---|
399 | (void *)self, CurFrameRect.origin.x, CurFrameRect.origin.y, m_Pos.x, m_Pos.y));
|
---|
400 | [self setFrameOrigin:m_Pos];
|
---|
401 | }
|
---|
402 |
|
---|
403 | if ( CurFrameRect.size.width != m_Size.width
|
---|
404 | || CurFrameRect.size.height != m_Size.height)
|
---|
405 | {
|
---|
406 | LogFlow(("OvlView(%p) vboxReshapePerform: resizing (%d,%d) -> (%d,%d)\n",
|
---|
407 | (void *)self, CurFrameRect.size.width, CurFrameRect.size.height, m_Size.width, m_Size.height));
|
---|
408 | [self setFrameSize:m_Size];
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Schedule two clears and a context update for now.
|
---|
412 | * Really though, we should just clear any new surface area.
|
---|
413 | */
|
---|
414 | m_cClears = 2;
|
---|
415 | }
|
---|
416 | m_fUpdateCtx = true;
|
---|
417 | LogFlow(("OvlView(%p) vboxReshapePerform: returns\n", self));
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | - (void)vboxReshape
|
---|
422 | {
|
---|
423 | LogFlow(("OvlView(%p) vboxReshape:\n", (void *)self));
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Resize the view.
|
---|
427 | */
|
---|
428 | if ([NSThread isMainThread])
|
---|
429 | [self vboxReshapePerform];
|
---|
430 | else
|
---|
431 | {
|
---|
432 | [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
|
---|
433 | vmsvga3dCocoaServiceRunLoop();
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Try update the opengl context.
|
---|
437 | */
|
---|
438 | [[self openGLContext] update];
|
---|
439 | }
|
---|
440 |
|
---|
441 | LogFlow(("OvlView(%p) vboxReshape: returns\n", (void *)self));
|
---|
442 | }
|
---|
443 |
|
---|
444 | #if 0 // doesn't work or isn't needed :/
|
---|
445 | - (void)vboxFrameDidChange
|
---|
446 | {
|
---|
447 | LogFlow(("OvlView(%p) vboxFrameDidChange:\n", (void *)self));
|
---|
448 | }
|
---|
449 |
|
---|
450 | - (BOOL)postsFrameChangedNotifications
|
---|
451 | {
|
---|
452 | LogFlow(("OvlView(%p) postsFrameChangedNotifications:\n", (void *)self));
|
---|
453 | return YES;
|
---|
454 | }
|
---|
455 | #endif
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Removes the view from the parent, if it has one, and makes sure it's hidden.
|
---|
459 | *
|
---|
460 | * This is callbed before destroying it.
|
---|
461 | */
|
---|
462 | - (void)vboxRemoveFromSuperviewAndHide
|
---|
463 | {
|
---|
464 | LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide:\n", (void *)self));
|
---|
465 | if (m_pParentView)
|
---|
466 | {
|
---|
467 | [self removeFromSuperview];
|
---|
468 | [self setHidden:YES];
|
---|
469 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Changes to the OpenGL context associated with the view.
|
---|
476 | * @returns Previous OpenGL context.
|
---|
477 | */
|
---|
478 | - (NSOpenGLContext *)makeCurrentGLContext
|
---|
479 | {
|
---|
480 | NSOpenGLContext *pSavedCtx = [NSOpenGLContext currentContext];
|
---|
481 |
|
---|
482 | /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
|
---|
483 | implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
|
---|
484 | if (pSavedCtx != nil)
|
---|
485 | glFlush();
|
---|
486 |
|
---|
487 | [[self openGLContext] makeCurrentContext];
|
---|
488 | return pSavedCtx;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Restores the previous OpenGL context after
|
---|
494 | * makeCurrentGLContext.
|
---|
495 | *
|
---|
496 | * @param pSavedCtx The makeCurrentGLContext return value.
|
---|
497 | */
|
---|
498 | - (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx
|
---|
499 | {
|
---|
500 | /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
|
---|
501 | implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
|
---|
502 | glFlush();
|
---|
503 |
|
---|
504 | if (pSavedCtx)
|
---|
505 | [pSavedCtx makeCurrentContext];
|
---|
506 | else
|
---|
507 | [NSOpenGLContext clearCurrentContext];
|
---|
508 | }
|
---|
509 |
|
---|
510 | #ifndef USE_NSOPENGLVIEW
|
---|
511 | /*
|
---|
512 | * Faking NSOpenGLView interface.
|
---|
513 | */
|
---|
514 | - (void)setOpenGLContext:(NSOpenGLContext *)pCtx
|
---|
515 | {
|
---|
516 | if (pCtx != m_pCtx)
|
---|
517 | {
|
---|
518 | if (pCtx)
|
---|
519 | {
|
---|
520 | [pCtx retain];
|
---|
521 | [pCtx setView:self];
|
---|
522 | /*Assert([pCtx view] == self); - setView fails early on, works later... */
|
---|
523 | }
|
---|
524 |
|
---|
525 | if (m_pCtx)
|
---|
526 | [m_pCtx release];
|
---|
527 |
|
---|
528 | m_pCtx = pCtx;
|
---|
529 |
|
---|
530 | if (pCtx)
|
---|
531 | [pCtx update];
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | - (NSOpenGLContext *)openGLContext
|
---|
536 | {
|
---|
537 | /* Stupid hacks to work around setView failing early. This can get kind of
|
---|
538 | noisy on some OS versions, so shut it up a little bit. */
|
---|
539 | /** @todo use NSOpenGLView for the non-visible contexts. */
|
---|
540 | if (m_pCtx && [m_pCtx view] != self)
|
---|
541 | {
|
---|
542 | m_cSetViewAttempts++;
|
---|
543 | if ( m_pParentView
|
---|
544 | || m_cSetViewAttempts < 64
|
---|
545 | || (m_cSetViewAttempts & (m_cSetViewAttempts < _64K ? 0xfff : 0x7fff)) == 0 )
|
---|
546 | [m_pCtx setView:self];
|
---|
547 | }
|
---|
548 | return m_pCtx;
|
---|
549 | }
|
---|
550 |
|
---|
551 | - (void)prepareOpenGL
|
---|
552 | {
|
---|
553 | //[m_pCtx prepareOpenGL];
|
---|
554 | }
|
---|
555 | #endif /* USE_NSOPENGLVIEW */
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Overridden NSOpenGLView / NSView methods:
|
---|
559 | */
|
---|
560 |
|
---|
561 | /** @todo do we need this? */
|
---|
562 | -(void)viewDidMoveToWindow
|
---|
563 | {
|
---|
564 | LogFlow(("OvlView(%p) viewDidMoveToWindow: new win: %p\n", (void *)self, (void *)[self window]));
|
---|
565 | [super viewDidMoveToWindow];
|
---|
566 | [self vboxReshape];
|
---|
567 | }
|
---|
568 |
|
---|
569 | -(void)viewDidMoveToSuperview
|
---|
570 | {
|
---|
571 | LogFlow(("OvlView(%p) viewDidMoveToSuperview: new view: %p\n", (void *)self, (void *)[self superview]));
|
---|
572 | [super viewDidMoveToSuperview];
|
---|
573 | [self vboxReshape];
|
---|
574 | }
|
---|
575 |
|
---|
576 | -(void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize
|
---|
577 | {
|
---|
578 | LogFlow(("OvlView(%p) resizeWithOldSuperviewSize: %d,%d -> %d,%d\n", (void *)self,
|
---|
579 | (int)oldBoundsSize.width, (int)oldBoundsSize.height, (int)[self bounds].size.width, (int)[self bounds].size.height));
|
---|
580 | [super resizeWithOldSuperviewSize:oldBoundsSize];
|
---|
581 | [self vboxReshape];
|
---|
582 | }
|
---|
583 |
|
---|
584 | - (void)drawRect:(NSRect)rect
|
---|
585 | {
|
---|
586 | // if (m_fClear)
|
---|
587 | // {
|
---|
588 | // m_fClear = false;
|
---|
589 | // [self vboxClearBuffers];
|
---|
590 | // }
|
---|
591 | }
|
---|
592 |
|
---|
593 | @end /* VMSVGA3DOverlayView */
|
---|
594 |
|
---|
595 | @implementation VMSVGA3DCreateViewAndContext
|
---|
596 | @end
|
---|
597 |
|
---|
598 |
|
---|
599 | VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaServiceRunLoop(void)
|
---|
600 | {
|
---|
601 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
602 | NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
|
---|
603 |
|
---|
604 | if ([NSRunLoop mainRunLoop] != pRunLoop)
|
---|
605 | {
|
---|
606 | [pRunLoop runUntilDate:[NSDate distantPast]];
|
---|
607 | }
|
---|
608 |
|
---|
609 | [pPool release];
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Document me later.
|
---|
615 | *
|
---|
616 | * @param pParentView The parent view if this is a context we'll be
|
---|
617 | * presenting to.
|
---|
618 | */
|
---|
619 | VMSVGA3DCOCOA_DECL(bool) vmsvga3dCocoaCreateViewAndContext(NativeNSViewRef *ppView, NativeNSOpenGLContextRef *ppCtx,
|
---|
620 | NativeNSViewRef pParentView, uint32_t cx, uint32_t cy,
|
---|
621 | NativeNSOpenGLContextRef pSharedCtx, bool fOtherProfile)
|
---|
622 | {
|
---|
623 | LogFlow(("vmsvga3dCocoaCreateViewAndContext: pParentView=%d size=%d,%d pSharedCtx=%p fOtherProfile=%RTbool\n",
|
---|
624 | (void *)pParentView, cx, cy, (void *)pSharedCtx, fOtherProfile));
|
---|
625 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
626 | vmsvga3dCocoaServiceRunLoop();
|
---|
627 |
|
---|
628 |
|
---|
629 | VMSVGA3DCreateViewAndContext *pParams = [VMSVGA3DCreateViewAndContext alloc];
|
---|
630 | pParams->pParentView = pParentView;
|
---|
631 | pParams->cx = cx;
|
---|
632 | pParams->cy = cy;
|
---|
633 | pParams->pSharedCtx = pSharedCtx;
|
---|
634 | pParams->fOtherProfile = fOtherProfile;
|
---|
635 | pParams->pView = NULL;
|
---|
636 | pParams->pCtx = NULL;
|
---|
637 |
|
---|
638 | [VMSVGA3DOverlayView performSelectorOnMainThread:@selector(createViewAndContext:)
|
---|
639 | withObject:pParams
|
---|
640 | waitUntilDone:YES];
|
---|
641 |
|
---|
642 | vmsvga3dCocoaServiceRunLoop();
|
---|
643 |
|
---|
644 | *ppCtx = pParams->pCtx;
|
---|
645 | *ppView = pParams->pView;
|
---|
646 | bool fRet = *ppCtx != NULL && *ppView != NULL;
|
---|
647 |
|
---|
648 | [pParams release];
|
---|
649 |
|
---|
650 | [pPool release];
|
---|
651 | LogFlow(("vmsvga3dCocoaDestroyContext: returns %RTbool\n", fRet));
|
---|
652 | return fRet;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaDestroyViewAndContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
|
---|
657 | {
|
---|
658 | LogFlow(("vmsvga3dCocoaDestroyViewAndContext: pView=%p pCtx=%p\n", (void *)pView, (void *)pCtx));
|
---|
659 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
660 |
|
---|
661 | /* The view */
|
---|
662 | [(VMSVGA3DOverlayView *)pView vboxRemoveFromSuperviewAndHide];
|
---|
663 |
|
---|
664 | Log(("vmsvga3dCocoaDestroyViewAndContext: view %p ref count=%d\n", (void *)pView, [pView retainCount]));
|
---|
665 | [pView release];
|
---|
666 |
|
---|
667 | /* The OpenGL context. */
|
---|
668 | Log(("vmsvga3dCocoaDestroyViewAndContext: ctx %p ref count=%d\n", (void *)pCtx, [pCtx retainCount]));
|
---|
669 | [pCtx release];
|
---|
670 |
|
---|
671 | [pPool release];
|
---|
672 | LogFlow(("vmsvga3dCocoaDestroyViewAndContext: returns\n"));
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 | /** @note Not currently used. */
|
---|
677 | VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
|
---|
678 | {
|
---|
679 | LogFlow(("vmsvga3dCocoaViewSetPosition: pView=%p pParentView=%p (%d,%d)\n", (void *)pView, (void *)pParentView, x, y));
|
---|
680 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
681 |
|
---|
682 | [(VMSVGA3DOverlayView *)pView vboxSetPos:NSMakePoint(x, y)];
|
---|
683 |
|
---|
684 | [pPool release];
|
---|
685 | LogFlow(("vmsvga3dCocoaViewSetPosition: returns\n"));
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int cx, int cy)
|
---|
690 | {
|
---|
691 | LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p (%d,%d)\n", (void *)pView, cx, cy));
|
---|
692 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
693 | VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
|
---|
694 |
|
---|
695 | [pOverlayView vboxSetSize:NSMakeSize(cx, cy)];
|
---|
696 |
|
---|
697 | [pPool release];
|
---|
698 | LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
|
---|
703 | {
|
---|
704 | LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
|
---|
705 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
706 | VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
|
---|
707 |
|
---|
708 | /* Always flush before flush. glXMakeCurrent and wglMakeCurrent does this
|
---|
709 | implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
|
---|
710 | if ([NSOpenGLContext currentContext] != 0)
|
---|
711 | glFlush();
|
---|
712 |
|
---|
713 | if (pOverlayView)
|
---|
714 | {
|
---|
715 | /* This must be a release assertion as we depend on the setView
|
---|
716 | sideeffect of the openGLContext method call. (hack alert!) */
|
---|
717 | AssertRelease([pOverlayView openGLContext] == pCtx);
|
---|
718 | [pCtx makeCurrentContext];
|
---|
719 | [pOverlayView vboxUpdateCtxIfNecessary];
|
---|
720 | }
|
---|
721 | else
|
---|
722 | [NSOpenGLContext clearCurrentContext];
|
---|
723 |
|
---|
724 | [pPool release];
|
---|
725 | LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: returns\n"));
|
---|
726 | }
|
---|
727 |
|
---|
728 |
|
---|
729 | void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
|
---|
730 | {
|
---|
731 | LogFlow(("vmsvga3dCocoaSwapBuffers: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
|
---|
732 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
733 | VMSVGA3DOverlayView *pMyView = (VMSVGA3DOverlayView *)pView;
|
---|
734 |
|
---|
735 | #ifndef USE_NSOPENGLVIEW
|
---|
736 | /* Hack alert! setView fails early on so call openGLContext to try again. */
|
---|
737 | if ([pCtx view] == NULL)
|
---|
738 | [pMyView openGLContext];
|
---|
739 | #endif
|
---|
740 |
|
---|
741 | Assert(pCtx == [NSOpenGLContext currentContext]);
|
---|
742 | Assert(pCtx == [pMyView openGLContext]);
|
---|
743 | AssertMsg([pCtx view] == pMyView, ("%p != %p\n", (void *)[pCtx view], (void *)pMyView));
|
---|
744 |
|
---|
745 | [pCtx flushBuffer];
|
---|
746 | //[pView setNeedsDisplay:YES];
|
---|
747 | vmsvga3dCocoaServiceRunLoop();
|
---|
748 |
|
---|
749 | /* If buffer clearing or/and context updates are pending, execute that now. */
|
---|
750 | [pMyView vboxUpdateCtxIfNecessary];
|
---|
751 | [pMyView vboxClearBackBufferIfNecessary];
|
---|
752 |
|
---|
753 | [pPool release];
|
---|
754 | LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
|
---|
755 | }
|
---|
756 |
|
---|