VirtualBox

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

Last change on this file since 72300 was 70938, checked in by vboxsync, 7 years ago

DevVGA-SVGA3d: FLOAT_FMT_ARGS fix for negative values. Fixed formatting bug in vmsvga3dInfoContextWorkerOne.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.2 KB
Line 
1/* $Id: DevVGA-SVGA3d-cocoa.m 70938 2018-02-09 17:32:07Z 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-2017 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 || DOXYGEN_RUNNING
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.%06u"
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)(RT_ABS(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)vboxScheduleCtxUpdate;
121- (void)vboxReshapePerform;
122- (void)vboxReshape;
123- (void)vboxBoundsDidChange:(NSNotification *)pNotification;
124- (void)vboxFrameDidChange:(NSNotification *)pNotification;
125- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification;
126- (BOOL)postsFrameChangedNotifications;
127- (void)vboxRemoveFromSuperviewAndHide;
128- (void)vboxUpdateCtxIfNecessary;
129- (void)vboxClearBackBufferIfNecessary;
130- (NSOpenGLContext *)makeCurrentGLContext;
131- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx;
132
133#ifndef USE_NSOPENGLVIEW
134/* NSOpenGLView fakes: */
135- (void)setOpenGLContext:(NSOpenGLContext *)pCtx;
136- (NSOpenGLContext *)openGLContext;
137- (void)prepareOpenGL;
138
139#endif
140/* Overridden: */
141- (void)viewDidMoveToWindow;
142- (void)viewDidMoveToSuperview;
143- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize;
144- (void)drawRect:(NSRect)rect;
145
146@end
147
148
149/********************************************************************************
150*
151* VMSVGA3DOverlayView class implementation
152*
153********************************************************************************/
154@implementation VMSVGA3DOverlayView
155
156
157+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams
158{
159 LogFlow(("OvlView createViewAndContext:\n"));
160
161 /*
162 * Create a pixel format.
163 */
164 NSOpenGLPixelFormat *pFmt = nil;
165
166 // Consider to remove it and check if it's harmless.
167 NSOpenGLPixelFormatAttribute attribs[] =
168 {
169 NSOpenGLPFAOpenGLProfile, (NSOpenGLPixelFormatAttribute)0,
170 //NSOpenGLPFAWindow, - obsolete/deprecated, try work without it...
171 NSOpenGLPFAAccelerated,
172 NSOpenGLPFADoubleBuffer,
173 NSOpenGLPFABackingStore,
174 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
175 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8,
176 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
177 0
178 };
179 attribs[1] = pParams->fOtherProfile ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy;
180 pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
181 if (pFmt)
182 {
183 /*
184 * Create a new view.
185 */
186 NSRect Frame;
187 Frame.origin.x = 0;
188 Frame.origin.y = 0;
189 Frame.size.width = pParams->cx < _1M && pParams->cx > 0 ? pParams->cx : 1; /* 'invalid drawable' if 0,0 size? */
190 Frame.size.height = pParams->cy < _1M && pParams->cy > 0 ? pParams->cy : 1;
191 VMSVGA3DOverlayView *pView = [[VMSVGA3DOverlayView alloc] initWithFrameAndFormat:Frame
192 parentView:pParams->pParentView
193 pixelFormat:pFmt];
194 if (pView)
195 {
196 /*
197 * If we have no shared GL context, we use the one that NSOpenGLView create. Otherwise,
198 * we replace it. (If we don't call openGLContext, it won't yet have been instantiated,
199 * so there is no unecessary contexts created here when pSharedCtx != NULL.)
200 */
201 NSOpenGLContext *pCtx;
202#ifdef USE_NSOPENGLVIEW
203 if (!pParams->pSharedCtx)
204 pCtx = [pView openGLContext];
205 else
206#endif
207 {
208 pCtx = [[NSOpenGLContext alloc] initWithFormat:pFmt shareContext: pParams->pSharedCtx];
209 if (pCtx)
210 {
211 [pView setOpenGLContext:pCtx];
212 [pCtx setView:pView];
213#ifdef USE_NSOPENGLVIEW
214 Assert([pCtx view] == pView);
215#endif
216 }
217 }
218 if (pCtx)
219 {
220 /*
221 * Attach the view to the parent if we have one. Otherwise make sure its invisible.
222 */
223 if (pParams->pParentView)
224 [pParams->pParentView addSubview:pView];
225 else
226 [pView setHidden:YES];
227
228 /*
229 * Resize and return.
230 */
231 //[pView vboxSetSize:Frame.size];
232
233 NSOpenGLContext *pSavedCtx = [pView makeCurrentGLContext];
234
235 [pView prepareOpenGL];
236 GLint x;
237 //x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSwapInterval];
238 //x = 1; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOrder];
239 x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOpacity];
240
241 if (pParams->pParentView)
242 [pView setHidden:NO];
243 else
244 [pView setHidden:YES];
245
246 [pView restoreSavedGLContext:pSavedCtx];
247
248 pParams->pView = pView;
249 pParams->pCtx = pCtx;
250 [pCtx retain]; //??
251
252 [pFmt release];
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 RT_NOREF(pFmt);
283 m_cSetViewAttempts = 0;
284 m_pCtx = NULL;
285 self = [super initWithFrame:frame];
286#endif
287 if (self)
288 {
289 //self.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;
290 self.autoresizingMask = NSViewNotSizable;
291
292 /*
293 * Get notifications when we're moved or resized and when we're moved
294 * to a different screen or GPU or when the GL context simply needs updating.
295 */
296 if (pParentView)
297 {
298 [[NSNotificationCenter defaultCenter] addObserver:self
299 selector:@selector(vboxBoundsDidChange:)
300 name:NSViewBoundsDidChangeNotification
301 object:self];
302 [[NSNotificationCenter defaultCenter] addObserver:self
303 selector:@selector(vboxFrameDidChange:)
304 name:NSViewFrameDidChangeNotification
305 object:self];
306 //[[NSNotificationCenter defaultCenter] addObserver:self
307 // selector:@selector(vboxFrameDidChange:)
308 // name:NSViewDidUpdateTrackingAreasNotification
309 // object:self];
310 [[NSNotificationCenter defaultCenter] addObserver:self
311 selector:@selector(vboxFrameDidChangeGlobal:)
312 name:NSViewGlobalFrameDidChangeNotification
313 object:self];
314 }
315 }
316 LogFlow(("OvlView(%p) initWithFrameAndFormat: returns %p\n", (void *)self, (void *)self));
317 return self;
318}
319
320- (void)dealloc
321{
322 LogFlow(("OvlView(%p) dealloc:\n", (void *)self));
323
324#ifdef USE_NSOPENGLVIEW
325 [[self openGLContext] clearDrawable];
326#else
327 if (m_pCtx)
328 {
329 [m_pCtx clearDrawable];
330 [m_pCtx release];
331 m_pCtx = nil;
332 }
333#endif
334
335 [super dealloc];
336
337 LogFlow(("OvlView(%p) dealloc: returns\n", (void *)self));
338}
339
340
341- (void)vboxSetPos:(NSPoint)pos
342{
343 Log(("OvlView(%p) vboxSetPos: (%d,%d)\n", (void *)self, (int)pos.x, (int)pos.y));
344
345 m_Pos = pos;
346 [self vboxReshape];
347
348 LogFlow(("OvlView(%p) vboxSetPos: returns\n", (void *)self));
349}
350
351
352- (void)vboxSetSize:(NSSize)size
353{
354 Log(("OvlView(%p) vboxSetSize: (%d,%d):\n", (void *)self, (int)size.width, (int)size.height));
355 m_Size = size;
356 [self vboxReshape];
357 LogFlow(("OvlView(%p) vboxSetSize: returns\n", (void *)self));
358}
359
360- (void)vboxScheduleCtxUpdate
361{
362 m_fUpdateCtx = true;
363}
364
365- (void)vboxUpdateCtxIfNecessary
366{
367 if (m_fUpdateCtx)
368 {
369 Log(("OvlView(%p) vboxUpdateCtxIfNecessary: m_fUpdateCtx\n", (void *)self));
370 [[self openGLContext] update];
371 m_fUpdateCtx = false;
372 }
373}
374
375
376- (void)vboxClearBackBufferIfNecessary
377{
378#if 1 /* experiment */
379 if (m_cClears > 0)
380 {
381 Assert(![NSThread isMainThread]);
382 Assert([self openGLContext] == [NSOpenGLContext currentContext]);
383 Log(("OvlView(%p) vboxClearBackBufferIfNecessary: m_cClears=%d\n", (void *)self, m_cClears));
384 m_cClears--;
385
386 /* Clear errors. */
387 GLenum rc;
388 while ((rc = glGetError()) != GL_NO_ERROR)
389 continue;
390
391 /* Save the old buffer setting and make it GL_BACK (shall be GL_BACK already actually). */
392 GLint iOldDrawBuf = GL_BACK;
393 glGetIntegerv(GL_DRAW_BUFFER, &iOldDrawBuf);
394 if (iOldDrawBuf != GL_BACK)
395 glDrawBuffer(GL_BACK);
396 while ((rc = glGetError()) != GL_NO_ERROR)
397 AssertMsgFailed(("rc=%x\n", rc));
398
399 /* Clear the current GL_BACK. */
400 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
401 glClear(GL_COLOR_BUFFER_BIT /*|GL_DEPTH_BUFFER_BIT*/ );
402 while ((rc = glGetError()) != GL_NO_ERROR)
403 AssertMsgFailed(("rc=%x\n", rc));
404
405 /* We're back to the orignal back buffer now. Just restore GL_DRAW_BUFFER. */
406 if (iOldDrawBuf != GL_BACK)
407 glDrawBuffer(iOldDrawBuf);
408
409 while ((rc = glGetError()) != GL_NO_ERROR)
410 AssertMsgFailed(("rc=%x\n", rc));
411 }
412#endif
413}
414
415
416
417- (void)vboxReshapePerform
418{
419 /*
420 * Change the size and position if necessary.
421 */
422 NSRect CurFrameRect = [self frame];
423 /** @todo conversions? */
424 if ( m_Pos.x != CurFrameRect.origin.x
425 || m_Pos.y != CurFrameRect.origin.y)
426 {
427 LogFlow(("OvlView(%p) vboxReshapePerform: moving (%d,%d) -> (%d,%d)\n",
428 (void *)self, CurFrameRect.origin.x, CurFrameRect.origin.y, m_Pos.x, m_Pos.y));
429 [self setFrameOrigin:m_Pos];
430 }
431
432 if ( CurFrameRect.size.width != m_Size.width
433 || CurFrameRect.size.height != m_Size.height)
434 {
435 LogFlow(("OvlView(%p) vboxReshapePerform: resizing (%d,%d) -> (%d,%d)\n",
436 (void *)self, CurFrameRect.size.width, CurFrameRect.size.height, m_Size.width, m_Size.height));
437 [self setFrameSize:m_Size];
438
439 /*
440 * Schedule two clears and a context update for now.
441 * Really though, we should just clear any new surface area.
442 */
443 m_cClears = 2;
444 }
445 m_fUpdateCtx = true;
446 LogFlow(("OvlView(%p) vboxReshapePerform: returns\n", self));
447}
448
449
450- (void)vboxReshape
451{
452 LogFlow(("OvlView(%p) vboxReshape:\n", (void *)self));
453
454 /*
455 * Resize the view.
456 */
457 if ([NSThread isMainThread])
458 [self vboxReshapePerform];
459 else
460 {
461 [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
462 vmsvga3dCocoaServiceRunLoop();
463
464 /*
465 * Try update the opengl context.
466 */
467 [[self openGLContext] update];
468 }
469
470 LogFlow(("OvlView(%p) vboxReshape: returns\n", (void *)self));
471}
472
473/**
474 * This is called when the bounds change.
475 *
476 * We indicate that the FIFO thread must update the GL context.
477 */
478- (void)vboxBoundsDidChange:(NSNotification *)pNotification
479{
480 RT_NOREF(pNotification);
481 LogFlow(("OvlView(%p) vboxBoundsDidChange:\n", (void *)self));
482 self->m_fUpdateCtx = true;
483}
484
485/**
486 * This is called when the frame changes size or position.
487 *
488 * We indicate that the FIFO thread must update the GL context.
489 */
490- (void)vboxFrameDidChange:(NSNotification *)pNotification
491{
492 RT_NOREF(pNotification);
493 LogFlow(("OvlView(%p) vboxFrameDidChange:\n", (void *)self));
494 self->m_fUpdateCtx = true;
495}
496
497/**
498 * This is called when moved to different screen/GPU or/and when the GL context
499 * needs updating.
500 *
501 * We indicate that the FIFO thread must update the GL context.
502 */
503- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification
504{
505 RT_NOREF(pNotification);
506 LogFlow(("OvlView(%p) vboxFrameDidChangeGlobal:\n", (void *)self));
507 self->m_fUpdateCtx = true;
508}
509
510/** This enables the vboxFrameDidChange notification. */
511- (BOOL)postsFrameChangedNotifications
512{
513 LogFlow(("OvlView(%p) postsFrameChangedNotifications:\n", (void *)self));
514 return YES;
515}
516
517/**
518 * Removes the view from the parent, if it has one, and makes sure it's hidden.
519 *
520 * This is callbed before destroying it.
521 */
522- (void)vboxRemoveFromSuperviewAndHide
523{
524 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide:\n", (void *)self));
525 if (m_pParentView)
526 {
527 /*
528 * The removeFromSuperview has been frequently seen to deadlock thing like this:
529 * #0 0x00007fff8db440fa in __psynch_cvwait ()
530 * #1 0x00007fff8d0acfb9 in _pthread_cond_wait ()
531 * #2 0x00007fff8a1bc8f0 in -[NSViewHierarchyLock _lockForWriting:handler:] ()
532 * #3 0x00007fff8a1bc171 in -[NSView removeFromSuperview] ()
533 * #4 0x000000010cffb2bb in -[VMSVGA3DOverlayView vboxRemoveFromSuperviewAndHide] (self=0x10a1da550, _cmd=0x10cffd734) at DevVGA-SVGA3d-cocoa.m:467
534 * #5 0x000000010cffbed3 in vmsvga3dCocoaDestroyViewAndContext (pView=0x10a1da550, pCtx=0x10a1da630) at DevVGA-SVGA3d-cocoa.m:662
535 * (This is from OS X 10.8.5.)
536 */
537 if ([NSThread isMainThread])
538 {
539 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide: calling removeFromSuperview\n", (void *)self));
540 [self removeFromSuperview];
541 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide: calling setHidden\n", (void *)self));
542 [self setHidden:YES];
543 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide: calling setHidden\n", (void *)self));
544 [[NSNotificationCenter defaultCenter] removeObserver:self];
545 }
546 else
547 {
548 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide: defering to main thread\n", (void *)self));
549 vmsvga3dCocoaServiceRunLoop();
550 [self performSelectorOnMainThread:@selector(vboxRemoveFromSuperviewAndHide) withObject:nil waitUntilDone:YES];
551 vmsvga3dCocoaServiceRunLoop();
552 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide: main thread done\n", (void *)self));
553 }
554 }
555}
556
557
558/**
559 * Changes to the OpenGL context associated with the view.
560 * @returns Previous OpenGL context.
561 */
562- (NSOpenGLContext *)makeCurrentGLContext
563{
564 NSOpenGLContext *pSavedCtx = [NSOpenGLContext currentContext];
565
566 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
567 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
568 if (pSavedCtx != nil)
569 glFlush();
570
571 [[self openGLContext] makeCurrentContext];
572 return pSavedCtx;
573}
574
575
576/**
577 * Restores the previous OpenGL context after
578 * makeCurrentGLContext.
579 *
580 * @param pSavedCtx The makeCurrentGLContext return value.
581 */
582- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx
583{
584 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
585 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
586 glFlush();
587
588 if (pSavedCtx)
589 [pSavedCtx makeCurrentContext];
590 else
591 [NSOpenGLContext clearCurrentContext];
592}
593
594#ifndef USE_NSOPENGLVIEW
595/*
596 * Faking NSOpenGLView interface.
597 */
598- (void)setOpenGLContext:(NSOpenGLContext *)pCtx
599{
600 if (pCtx != m_pCtx)
601 {
602 if (pCtx)
603 {
604 [pCtx retain];
605 [pCtx setView:self];
606 /*Assert([pCtx view] == self); - setView fails early on, works later... */
607 }
608
609 if (m_pCtx)
610 [m_pCtx release];
611
612 m_pCtx = pCtx;
613
614 if (pCtx)
615 [pCtx update];
616 }
617}
618
619- (NSOpenGLContext *)openGLContext
620{
621 /* Stupid hacks to work around setView failing early. This can get kind of
622 noisy on some OS versions, so shut it up a little bit. */
623 /** @todo use NSOpenGLView for the non-visible contexts. */
624 if (m_pCtx && [m_pCtx view] != self)
625 {
626 m_cSetViewAttempts++;
627 if ( m_pParentView
628 || m_cSetViewAttempts < 64
629 || (m_cSetViewAttempts & (m_cSetViewAttempts < _64K ? 0xfff : 0x7fff)) == 0 )
630 [m_pCtx setView:self];
631 }
632 return m_pCtx;
633}
634
635- (void)prepareOpenGL
636{
637 //[m_pCtx prepareOpenGL];
638}
639#endif /* USE_NSOPENGLVIEW */
640
641/*
642 * Overridden NSOpenGLView / NSView methods:
643 */
644
645/** @todo do we need this? */
646-(void)viewDidMoveToWindow
647{
648 LogFlow(("OvlView(%p) viewDidMoveToWindow: new win: %p\n", (void *)self, (void *)[self window]));
649 [super viewDidMoveToWindow];
650 [self vboxReshape];
651}
652
653-(void)viewDidMoveToSuperview
654{
655 LogFlow(("OvlView(%p) viewDidMoveToSuperview: new view: %p\n", (void *)self, (void *)[self superview]));
656 [super viewDidMoveToSuperview];
657 [self vboxReshape];
658}
659
660-(void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize
661{
662 LogFlow(("OvlView(%p) resizeWithOldSuperviewSize: %d,%d -> %d,%d\n", (void *)self,
663 (int)oldBoundsSize.width, (int)oldBoundsSize.height, (int)[self bounds].size.width, (int)[self bounds].size.height));
664 [super resizeWithOldSuperviewSize:oldBoundsSize];
665 [self vboxReshape];
666}
667
668- (void)drawRect:(NSRect)rect
669{
670 RT_NOREF(rect);
671// if (m_fClear)
672// {
673// m_fClear = false;
674// [self vboxClearBuffers];
675// }
676}
677
678@end /* VMSVGA3DOverlayView */
679
680@implementation VMSVGA3DCreateViewAndContext
681@end
682
683
684VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaServiceRunLoop(void)
685{
686 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
687 NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
688
689 if ([NSRunLoop mainRunLoop] != pRunLoop)
690 {
691 [pRunLoop runUntilDate:[NSDate distantPast]];
692 }
693
694 [pPool release];
695}
696
697
698/**
699 * Document me later.
700 *
701 * @param ppView
702 * @param ppCtx
703 * @param pParentView The parent view if this is a context we'll be
704 * presenting to.
705 * @param cx
706 * @param cy
707 * @param pSharedCtx
708 * @param fOtherProfile
709 */
710VMSVGA3DCOCOA_DECL(bool) vmsvga3dCocoaCreateViewAndContext(NativeNSViewRef *ppView, NativeNSOpenGLContextRef *ppCtx,
711 NativeNSViewRef pParentView, uint32_t cx, uint32_t cy,
712 NativeNSOpenGLContextRef pSharedCtx, bool fOtherProfile)
713{
714 LogFlow(("vmsvga3dCocoaCreateViewAndContext: pParentView=%d size=%d,%d pSharedCtx=%p fOtherProfile=%RTbool\n",
715 (void *)pParentView, cx, cy, (void *)pSharedCtx, fOtherProfile));
716 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
717 vmsvga3dCocoaServiceRunLoop();
718
719
720 VMSVGA3DCreateViewAndContext *pParams = [VMSVGA3DCreateViewAndContext alloc];
721 pParams->pParentView = pParentView;
722 pParams->cx = cx;
723 pParams->cy = cy;
724 pParams->pSharedCtx = pSharedCtx;
725 pParams->fOtherProfile = fOtherProfile;
726 pParams->pView = NULL;
727 pParams->pCtx = NULL;
728
729 [VMSVGA3DOverlayView performSelectorOnMainThread:@selector(createViewAndContext:)
730 withObject:pParams
731 waitUntilDone:YES];
732
733 vmsvga3dCocoaServiceRunLoop();
734
735 *ppCtx = pParams->pCtx;
736 *ppView = pParams->pView;
737 bool fRet = *ppCtx != NULL && *ppView != NULL;
738
739 [pParams release];
740
741 [pPool release];
742 LogFlow(("vmsvga3dCocoaDestroyContext: returns %RTbool\n", fRet));
743 return fRet;
744}
745
746
747VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaDestroyViewAndContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
748{
749 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: pView=%p pCtx=%p\n", (void *)pView, (void *)pCtx));
750 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
751
752 /* The view */
753 VMSVGA3DOverlayView *pOvlView = (VMSVGA3DOverlayView *)pView;
754 [pOvlView vboxRemoveFromSuperviewAndHide];
755
756 Log(("vmsvga3dCocoaDestroyViewAndContext: view %p ref count=%d\n", (void *)pOvlView, [pOvlView retainCount]));
757 [pOvlView release];
758
759 /* The OpenGL context. */
760 Log(("vmsvga3dCocoaDestroyViewAndContext: ctx %p ref count=%d\n", (void *)pCtx, [pCtx retainCount]));
761 [pCtx release];
762
763 [pPool release];
764 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: returns\n"));
765}
766
767
768VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewInfo(PCDBGFINFOHLP pHlp, NativeNSViewRef pView)
769{
770 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
771 if (pView != nil)
772 {
773 VMSVGA3DOverlayView *pOvlView = (VMSVGA3DOverlayView *)pView;
774
775 NSRect FrameRect = [pOvlView frame];
776 pHlp->pfnPrintf(pHlp, " Frame rect: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
777 FLOAT_FMT_ARGS(FrameRect.origin.x), FLOAT_FMT_ARGS(FrameRect.origin.y),
778 FLOAT_FMT_ARGS(FrameRect.size.width), FLOAT_FMT_ARGS(FrameRect.size.height));
779 NSRect BoundsRect = [pOvlView bounds];
780 pHlp->pfnPrintf(pHlp, " Bounds rect: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
781 FLOAT_FMT_ARGS(BoundsRect.origin.x), FLOAT_FMT_ARGS(BoundsRect.origin.y),
782 FLOAT_FMT_ARGS(BoundsRect.size.width), FLOAT_FMT_ARGS(BoundsRect.size.height));
783 NSRect VisibleRect = [pOvlView visibleRect];
784 pHlp->pfnPrintf(pHlp, " Visible rect: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
785 FLOAT_FMT_ARGS(VisibleRect.origin.x), FLOAT_FMT_ARGS(VisibleRect.origin.y),
786 FLOAT_FMT_ARGS(VisibleRect.size.width), FLOAT_FMT_ARGS(VisibleRect.size.height));
787 pHlp->pfnPrintf(pHlp, " isHidden: %RTbool\n", [pOvlView isHidden] != NO);
788 pHlp->pfnPrintf(pHlp, " canDraw: %RTbool\n", [pOvlView canDraw] != NO);
789 pHlp->pfnPrintf(pHlp, " wantsDefaultClipping: %RTbool\n", [pOvlView wantsDefaultClipping] != NO);
790 pHlp->pfnPrintf(pHlp, " wantsLayer: %RTbool\n", [pOvlView wantsLayer] != NO);
791 if ([pOvlView layer] != nil)
792 pHlp->pfnPrintf(pHlp, " Layer: %p\n", [pOvlView layer] != nil);
793 pHlp->pfnPrintf(pHlp, " isOpaque: %RTbool\n", [pOvlView isOpaque] != NO);
794 pHlp->pfnPrintf(pHlp, " autoresizingMask: %#x\n", [pOvlView autoresizingMask]);
795 pHlp->pfnPrintf(pHlp, " isRotatedOrScaledFromBase: %RTbool\n", [pOvlView isRotatedOrScaledFromBase] != NO);
796
797 NSView *pEnclosingScrollView = [pOvlView enclosingScrollView];
798 NSView *pCurView = [pOvlView superview];
799 uint32_t iLevel;
800 for (iLevel = 1; pCurView && iLevel < 7; iLevel++)
801 {
802 NSView *pNextView = [pCurView superview];
803 pHlp->pfnPrintf(pHlp, " Superview#%u: %p, super=%p\n", iLevel, pCurView, pNextView);
804 FrameRect = [pCurView frame];
805 pHlp->pfnPrintf(pHlp, " Superview#%u frame: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
806 iLevel,
807 FLOAT_FMT_ARGS(FrameRect.origin.x), FLOAT_FMT_ARGS(FrameRect.origin.y),
808 FLOAT_FMT_ARGS(FrameRect.size.width), FLOAT_FMT_ARGS(FrameRect.size.height));
809 BoundsRect = [pCurView bounds];
810 pHlp->pfnPrintf(pHlp, " Superview#%u bounds: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
811 iLevel,
812 FLOAT_FMT_ARGS(BoundsRect.origin.x), FLOAT_FMT_ARGS(BoundsRect.origin.y),
813 FLOAT_FMT_ARGS(BoundsRect.size.width), FLOAT_FMT_ARGS(BoundsRect.size.height));
814 if (pEnclosingScrollView == pCurView)
815 pHlp->pfnPrintf(pHlp, " Superview#%u is enclosing scroll view\n", iLevel);
816 if ([pCurView enclosingScrollView])
817 pHlp->pfnPrintf(pHlp, " Superview#%u has an enclosing scroll view: %p\n", [pCurView enclosingScrollView]);
818 pCurView = pNextView;
819 }
820 if (pCurView)
821 pHlp->pfnPrintf(pHlp, " (There are more super views)\n");
822
823 NSWindow *pWindow = [pOvlView window];
824 if (pWindow != nil)
825 {
826 pHlp->pfnPrintf(pHlp, " Window: %p\n", pWindow);
827 FrameRect = [pWindow frame];
828 pHlp->pfnPrintf(pHlp, " Window frame: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
829 FLOAT_FMT_ARGS(FrameRect.origin.x), FLOAT_FMT_ARGS(FrameRect.origin.y),
830 FLOAT_FMT_ARGS(FrameRect.size.width), FLOAT_FMT_ARGS(FrameRect.size.height));
831 CGFloat rFactor = [pWindow backingScaleFactor];
832 pHlp->pfnPrintf(pHlp, " W.backingScaleFactor: " FLOAT_FMT_STR "\n", FLOAT_FMT_ARGS(rFactor));
833 }
834
835 }
836 [pPool release];
837}
838
839
840/** @note Not currently used. */
841VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
842{
843 RT_NOREF(pParentView);
844 LogFlow(("vmsvga3dCocoaViewSetPosition: pView=%p pParentView=%p (%d,%d)\n", (void *)pView, (void *)pParentView, x, y));
845 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
846
847 [(VMSVGA3DOverlayView *)pView vboxSetPos:NSMakePoint(x, y)];
848
849 [pPool release];
850 LogFlow(("vmsvga3dCocoaViewSetPosition: returns\n"));
851}
852
853
854VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewUpdateViewport(NativeNSViewRef pView)
855{
856 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p\n", (void *)pView));
857 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
858 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
859
860 /* Possible that we don't actually need to do this (i.e. this API), but right now I'm
861 leaving it to be sure things actually work right when scrolling. */
862 [pOverlayView vboxScheduleCtxUpdate];
863
864 [pPool release];
865 LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
866}
867
868
869VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int cx, int cy)
870{
871 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p (%d,%d)\n", (void *)pView, cx, cy));
872 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
873 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
874
875 [pOverlayView vboxSetSize:NSMakeSize(cx, cy)];
876
877 [pPool release];
878 LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
879}
880
881
882void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
883{
884 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
885 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
886 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
887
888 /* Always flush before flush. glXMakeCurrent and wglMakeCurrent does this
889 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
890 if ([NSOpenGLContext currentContext] != 0)
891 glFlush();
892
893 if (pOverlayView)
894 {
895 /* This must be a release assertion as we depend on the setView
896 sideeffect of the openGLContext method call. (hack alert!) */
897 AssertRelease([pOverlayView openGLContext] == pCtx);
898 [pCtx makeCurrentContext];
899 [pOverlayView vboxUpdateCtxIfNecessary];
900 }
901 else
902 [NSOpenGLContext clearCurrentContext];
903
904 [pPool release];
905 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: returns\n"));
906}
907
908
909void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
910{
911 LogFlow(("vmsvga3dCocoaSwapBuffers: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
912 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
913 VMSVGA3DOverlayView *pMyView = (VMSVGA3DOverlayView *)pView;
914
915#ifndef USE_NSOPENGLVIEW
916 /* Hack alert! setView fails early on so call openGLContext to try again. */
917 if ([pCtx view] == NULL)
918 [pMyView openGLContext];
919#endif
920
921 Assert(pCtx == [NSOpenGLContext currentContext]);
922 Assert(pCtx == [pMyView openGLContext]);
923 AssertMsg([pCtx view] == pMyView, ("%p != %p\n", (void *)[pCtx view], (void *)pMyView));
924
925 [pCtx flushBuffer];
926 //[pView setNeedsDisplay:YES];
927 vmsvga3dCocoaServiceRunLoop();
928
929 /* If buffer clearing or/and context updates are pending, execute that now. */
930 [pMyView vboxUpdateCtxIfNecessary];
931 [pMyView vboxClearBackBufferIfNecessary];
932
933 [pPool release];
934 LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
935}
936
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