VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-cocoa.m@ 57372

Last change on this file since 57372 was 57372, checked in by vboxsync, 9 years ago

scm: fixes in previous cleanup run.

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