VirtualBox

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

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

vmsvga3d: Darwin view info.

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