VirtualBox

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

Last change on this file since 82781 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.2 KB
RevLine 
[54167]1/* $Id: DevVGA-SVGA3d-cocoa.m 76553 2019-01-01 01:45:53Z vboxsync $ */
[53206]2/** @file
[53946]3 * VirtualBox OpenGL Cocoa Window System Helper Implementation.
4 *
[53599]5 * @remarks Inspired by HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m.
[53206]6 */
7
8/*
[76553]9 * Copyright (C) 2009-2019 Oracle Corporation
[53206]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
[57358]20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
[55840]24#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
[53206]25#include "DevVGA-SVGA3d-cocoa.h"
26#import <Cocoa/Cocoa.h>
[57154]27#undef PVM /* Stupid namespace pollution from outdated sys/param.h header file. */
[53206]28#import <OpenGL/gl.h>
29
30#include <iprt/thread.h>
[54167]31#include <iprt/assert.h>
[55840]32#include <iprt/string.h>
33#include <VBox/log.h>
[57154]34#include <VBox/vmm/dbgf.h>
[53206]35
[53599]36
[57358]37/*********************************************************************************************************************************
38* Defined Constants And Macros *
39*********************************************************************************************************************************/
[55840]40/** @def USE_NSOPENGLVIEW
[56333]41 * Define this to experiment with using NSOpenGLView instead
42 * of NSView. There are transparency issues with the former,
[55840]43 * so for the time being we're using the latter. */
[65159]44#if 0 || DOXYGEN_RUNNING
45# define USE_NSOPENGLVIEW
[55840]46#endif
[53599]47
[57154]48/**@def FLOAT_FMT_STR
49 * Format string bits to go with FLOAT_FMT_ARGS. */
[70938]50#define FLOAT_FMT_STR "%d.%06u"
[57154]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. */
[70938]54#define FLOAT_FMT_ARGS(r) (int)(r), ((unsigned)(RT_ABS(r) * 1000000) % 1000000U)
[53599]55
[57154]56
57
[57358]58/*********************************************************************************************************************************
59* Structures and Typedefs *
60*********************************************************************************************************************************/
[55840]61/**
62 * Argument package for doing this on the main thread.
63 */
64@interface VMSVGA3DCreateViewAndContext : NSObject
65{
66@public
67 /* in */
[56333]68 NativeNSViewRef pParentView;
[55840]69 uint32_t cx;
70 uint32_t cy;
71 NativeNSOpenGLContextRef pSharedCtx;
72 bool fOtherProfile;
[53599]73
[55840]74 /* out */
75 NativeNSViewRef pView;
76 NativeNSOpenGLContextRef pCtx;
77}
78@end
[53599]79
80
[55840]81/**
82 * The overlay view.
83 */
[56333]84@interface VMSVGA3DOverlayView
[55840]85#ifdef USE_NSOPENGLVIEW
86 : NSOpenGLView
[53206]87#else
[55840]88 : NSView
[53206]89#endif
90{
91@private
[57131]92 /** This points to the parent view, if there is one. If there isn't a parent
[57358]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. */
[57131]96 NSView *m_pParentView;
97 /** Indicates that buffers (back+front) needs clearing before use because
[57358]98 * the view changed size. There are two buffers, so this is set to two
99 * each time when the view area increases. */
[57131]100 uint32_t m_cClears;
101 /** Set if the OpenGL context needs updating after a resize. */
102 bool m_fUpdateCtx;
[53206]103
[55840]104#ifndef USE_NSOPENGLVIEW
105 /** The OpenGL context associated with this view. */
106 NSOpenGLContext *m_pCtx;
[57135]107 /** Number of times we've tried to set the view (shut up noisy NSLog). */
108 uint32_t m_cSetViewAttempts;
[55840]109#endif
110
[57131]111 /** The desired view position relative to super. */
112 NSPoint m_Pos;
113 /** The desired view size. */
114 NSSize m_Size;
[53206]115}
[55840]116+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams;
117- (id)initWithFrameAndFormat:(NSRect)frame parentView:(NSView*)pparentView pixelFormat:(NSOpenGLPixelFormat *)pFmt;
[57131]118- (void)vboxSetPos:(NSPoint)pos;
119- (void)vboxSetSize:(NSSize)size;
[57790]120- (void)vboxScheduleCtxUpdate;
[57131]121- (void)vboxReshapePerform;
[55840]122- (void)vboxReshape;
[57790]123- (void)vboxBoundsDidChange:(NSNotification *)pNotification;
124- (void)vboxFrameDidChange:(NSNotification *)pNotification;
125- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification;
[57131]126- (BOOL)postsFrameChangedNotifications;
127- (void)vboxRemoveFromSuperviewAndHide;
128- (void)vboxUpdateCtxIfNecessary;
129- (void)vboxClearBackBufferIfNecessary;
[55840]130- (NSOpenGLContext *)makeCurrentGLContext;
131- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx;
[53206]132
[55840]133#ifndef USE_NSOPENGLVIEW
134/* NSOpenGLView fakes: */
135- (void)setOpenGLContext:(NSOpenGLContext *)pCtx;
136- (NSOpenGLContext *)openGLContext;
137- (void)prepareOpenGL;
[53206]138
[55840]139#endif
[56333]140/* Overridden: */
[55840]141- (void)viewDidMoveToWindow;
142- (void)viewDidMoveToSuperview;
143- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize;
144- (void)drawRect:(NSRect)rect;
145
[53206]146@end
147
148
149/********************************************************************************
[57372]150*
[55840]151* VMSVGA3DOverlayView class implementation
[53206]152*
153********************************************************************************/
[55840]154@implementation VMSVGA3DOverlayView
[53206]155
[53599]156
[55840]157+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams
[53206]158{
[56419]159 LogFlow(("OvlView createViewAndContext:\n"));
[53206]160
[55840]161 /*
162 * Create a pixel format.
163 */
164 NSOpenGLPixelFormat *pFmt = nil;
[53206]165
[55840]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;
[57135]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;
[55840]191 VMSVGA3DOverlayView *pView = [[VMSVGA3DOverlayView alloc] initWithFrameAndFormat:Frame
192 parentView:pParams->pParentView
193 pixelFormat:pFmt];
194 if (pView)
195 {
196 /*
[56333]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,
[55840]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 {
[56333]220 /*
[57131]221 * Attach the view to the parent if we have one. Otherwise make sure its invisible.
[55840]222 */
[57131]223 if (pParams->pParentView)
224 [pParams->pParentView addSubview:pView];
225 else
226 [pView setHidden:YES];
[53599]227
[55840]228 /*
229 * Resize and return.
230 */
[57131]231 //[pView vboxSetSize:Frame.size];
[53206]232
[55840]233 NSOpenGLContext *pSavedCtx = [pView makeCurrentGLContext];
[53206]234
[55840]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];
[53206]240
[57131]241 if (pParams->pParentView)
242 [pView setHidden:NO];
243 else
244 [pView setHidden:YES];
[53599]245
[55840]246 [pView restoreSavedGLContext:pSavedCtx];
[53206]247
[55840]248 pParams->pView = pView;
249 pParams->pCtx = pCtx;
250 [pCtx retain]; //??
[53206]251
[55840]252 [pFmt release];
[57131]253
[56419]254 LogFlow(("OvlView createViewAndContext: returns successfully\n"));
[55840]255 return;
256 }
257 [pView release];
258 }
259 [pFmt release];
260 }
261 else
262 AssertFailed();
[53206]263
[56419]264 LogFlow(("OvlView createViewAndContext: returns failure\n"));
[55840]265 return;
[53206]266}
267
[57135]268- (id)initWithFrameAndFormat:(NSRect) frame parentView:(NSView *)pParentView pixelFormat:(NSOpenGLPixelFormat *)pFmt
[53206]269{
[56419]270 LogFlow(("OvlView(%p) initWithFrameAndFormat:\n", (void *)self));
[53206]271
[55840]272 m_pParentView = pParentView;
273 /* Make some reasonable defaults */
274 m_Pos = NSZeroPoint;
275 m_Size = frame.size;
[57131]276 m_cClears = 2;
277 m_fUpdateCtx = true;
[56333]278
[55840]279#ifdef USE_NSOPENGLVIEW
280 self = [super initWithFrame:frame pixelFormat:pFmt];
[53599]281#else
[63480]282 RT_NOREF(pFmt);
[57135]283 m_cSetViewAttempts = 0;
284 m_pCtx = NULL;
[55840]285 self = [super initWithFrame:frame];
[53599]286#endif
[55840]287 if (self)
288 {
[57131]289 //self.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;
290 self.autoresizingMask = NSViewNotSizable;
[57790]291
292 /*
[62463]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.
[57790]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 }
[53599]315 }
[56419]316 LogFlow(("OvlView(%p) initWithFrameAndFormat: returns %p\n", (void *)self, (void *)self));
[53599]317 return self;
318}
319
320- (void)dealloc
321{
[56419]322 LogFlow(("OvlView(%p) dealloc:\n", (void *)self));
[53599]323
[55840]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
[53599]334
[55840]335 [super dealloc];
[53599]336
[56419]337 LogFlow(("OvlView(%p) dealloc: returns\n", (void *)self));
[53599]338}
339
340
[57131]341- (void)vboxSetPos:(NSPoint)pos
[53599]342{
[57131]343 Log(("OvlView(%p) vboxSetPos: (%d,%d)\n", (void *)self, (int)pos.x, (int)pos.y));
[53599]344
[55840]345 m_Pos = pos;
346 [self vboxReshape];
[53599]347
[57131]348 LogFlow(("OvlView(%p) vboxSetPos: returns\n", (void *)self));
[53599]349}
350
351
[57131]352- (void)vboxSetSize:(NSSize)size
[53206]353{
[57131]354 Log(("OvlView(%p) vboxSetSize: (%d,%d):\n", (void *)self, (int)size.width, (int)size.height));
[55840]355 m_Size = size;
356 [self vboxReshape];
[57131]357 LogFlow(("OvlView(%p) vboxSetSize: returns\n", (void *)self));
[53206]358}
359
[57790]360- (void)vboxScheduleCtxUpdate
361{
362 m_fUpdateCtx = true;
363}
[55840]364
[57131]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}
[55840]374
[57131]375
376- (void)vboxClearBackBufferIfNecessary
[53599]377{
[57131]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--;
[53599]385
[57131]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{
[55840]419 /*
[57131]420 * Change the size and position if necessary.
[55840]421 */
[57131]422 NSRect CurFrameRect = [self frame];
423 /** @todo conversions? */
424 if ( m_Pos.x != CurFrameRect.origin.x
425 || m_Pos.y != CurFrameRect.origin.y)
426 {
[57358]427 LogFlow(("OvlView(%p) vboxReshapePerform: moving (%d,%d) -> (%d,%d)\n",
[57131]428 (void *)self, CurFrameRect.origin.x, CurFrameRect.origin.y, m_Pos.x, m_Pos.y));
429 [self setFrameOrigin:m_Pos];
430 }
[53599]431
[57131]432 if ( CurFrameRect.size.width != m_Size.width
433 || CurFrameRect.size.height != m_Size.height)
[57358]434 {
435 LogFlow(("OvlView(%p) vboxReshapePerform: resizing (%d,%d) -> (%d,%d)\n",
[57131]436 (void *)self, CurFrameRect.size.width, CurFrameRect.size.height, m_Size.width, m_Size.height));
437 [self setFrameSize:m_Size];
438
[57358]439 /*
440 * Schedule two clears and a context update for now.
[57131]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
[55840]454 /*
[57131]455 * Resize the view.
[55840]456 */
[57131]457 if ([NSThread isMainThread])
458 [self vboxReshapePerform];
459 else
460 {
461 [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
462 vmsvga3dCocoaServiceRunLoop();
[53599]463
[57131]464 /*
465 * Try update the opengl context.
466 */
467 [[self openGLContext] update];
468 }
[55840]469
[56419]470 LogFlow(("OvlView(%p) vboxReshape: returns\n", (void *)self));
[53599]471}
472
[57790]473/**
474 * This is called when the bounds change.
[62463]475 *
[57790]476 * We indicate that the FIFO thread must update the GL context.
477 */
478- (void)vboxBoundsDidChange:(NSNotification *)pNotification
[57131]479{
[63480]480 RT_NOREF(pNotification);
[57790]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.
[62463]487 *
[57790]488 * We indicate that the FIFO thread must update the GL context.
489 */
490- (void)vboxFrameDidChange:(NSNotification *)pNotification
491{
[63480]492 RT_NOREF(pNotification);
[57131]493 LogFlow(("OvlView(%p) vboxFrameDidChange:\n", (void *)self));
[57790]494 self->m_fUpdateCtx = true;
[57131]495}
[53599]496
[62463]497/**
498 * This is called when moved to different screen/GPU or/and when the GL context
[57790]499 * needs updating.
[62463]500 *
[57790]501 * We indicate that the FIFO thread must update the GL context.
502 */
503- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification
504{
[63480]505 RT_NOREF(pNotification);
[57790]506 LogFlow(("OvlView(%p) vboxFrameDidChangeGlobal:\n", (void *)self));
507 self->m_fUpdateCtx = true;
508}
509
510/** This enables the vboxFrameDidChange notification. */
[57131]511- (BOOL)postsFrameChangedNotifications
512{
513 LogFlow(("OvlView(%p) postsFrameChangedNotifications:\n", (void *)self));
514 return YES;
515}
516
[56333]517/**
[57358]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.
[57131]521 */
522- (void)vboxRemoveFromSuperviewAndHide
523{
524 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide:\n", (void *)self));
525 if (m_pParentView)
526 {
[57137]527 /*
[57358]528 * The removeFromSuperview has been frequently seen to deadlock thing like this:
[57137]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 }
[57131]554 }
555}
556
557
558/**
[56333]559 * Changes to the OpenGL context associated with the view.
560 * @returns Previous OpenGL context.
561 */
[55840]562- (NSOpenGLContext *)makeCurrentGLContext
[53206]563{
[55840]564 NSOpenGLContext *pSavedCtx = [NSOpenGLContext currentContext];
[53599]565
[55840]566 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
567 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
568 if (pSavedCtx != nil)
569 glFlush();
[53599]570
[55840]571 [[self openGLContext] makeCurrentContext];
572 return pSavedCtx;
[53206]573}
574
[55840]575
576/**
[56333]577 * Restores the previous OpenGL context after
578 * makeCurrentGLContext.
579 *
[55840]580 * @param pSavedCtx The makeCurrentGLContext return value.
581 */
582- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx
[53206]583{
[55840]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];
[53206]592}
593
[55840]594#ifndef USE_NSOPENGLVIEW
595/*
596 * Faking NSOpenGLView interface.
597 */
598- (void)setOpenGLContext:(NSOpenGLContext *)pCtx
[53206]599{
[55840]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 }
[53206]608
[55840]609 if (m_pCtx)
610 [m_pCtx release];
611
612 m_pCtx = pCtx;
613
614 if (pCtx)
615 [pCtx update];
616 }
[53206]617}
618
[55840]619- (NSOpenGLContext *)openGLContext
[53206]620{
[57135]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. */
[55840]624 if (m_pCtx && [m_pCtx view] != self)
[57135]625 {
626 m_cSetViewAttempts++;
[57358]627 if ( m_pParentView
628 || m_cSetViewAttempts < 64
[57135]629 || (m_cSetViewAttempts & (m_cSetViewAttempts < _64K ? 0xfff : 0x7fff)) == 0 )
630 [m_pCtx setView:self];
631 }
[55840]632 return m_pCtx;
[53206]633}
634
[55840]635- (void)prepareOpenGL
[53206]636{
[55840]637 //[m_pCtx prepareOpenGL];
[53206]638}
[55840]639#endif /* USE_NSOPENGLVIEW */
[53206]640
[56333]641/*
[55840]642 * Overridden NSOpenGLView / NSView methods:
643 */
644
[57131]645/** @todo do we need this? */
[55840]646-(void)viewDidMoveToWindow
[53206]647{
[55840]648 LogFlow(("OvlView(%p) viewDidMoveToWindow: new win: %p\n", (void *)self, (void *)[self window]));
649 [super viewDidMoveToWindow];
650 [self vboxReshape];
[53206]651}
652
[55840]653-(void)viewDidMoveToSuperview
[53206]654{
[55840]655 LogFlow(("OvlView(%p) viewDidMoveToSuperview: new view: %p\n", (void *)self, (void *)[self superview]));
656 [super viewDidMoveToSuperview];
657 [self vboxReshape];
[53206]658}
659
[55840]660-(void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize
[53206]661{
[56333]662 LogFlow(("OvlView(%p) resizeWithOldSuperviewSize: %d,%d -> %d,%d\n", (void *)self,
[55840]663 (int)oldBoundsSize.width, (int)oldBoundsSize.height, (int)[self bounds].size.width, (int)[self bounds].size.height));
664 [super resizeWithOldSuperviewSize:oldBoundsSize];
665 [self vboxReshape];
[53206]666}
667
[55840]668- (void)drawRect:(NSRect)rect
[53206]669{
[63480]670 RT_NOREF(rect);
[57131]671// if (m_fClear)
672// {
673// m_fClear = false;
674// [self vboxClearBuffers];
675// }
[53206]676}
677
[55840]678@end /* VMSVGA3DOverlayView */
679
680@implementation VMSVGA3DCreateViewAndContext
[53206]681@end
682
683
[55840]684VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaServiceRunLoop(void)
[53206]685{
686 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[55840]687 NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
[53206]688
[55840]689 if ([NSRunLoop mainRunLoop] != pRunLoop)
[53206]690 {
[55840]691 [pRunLoop runUntilDate:[NSDate distantPast]];
[53206]692 }
693
694 [pPool release];
695}
696
697
[57131]698/**
699 * Document me later.
[57358]700 *
[65159]701 * @param ppView
702 * @param ppCtx
[57358]703 * @param pParentView The parent view if this is a context we'll be
[57131]704 * presenting to.
[65159]705 * @param cx
706 * @param cy
707 * @param pSharedCtx
708 * @param fOtherProfile
[57131]709 */
[55840]710VMSVGA3DCOCOA_DECL(bool) vmsvga3dCocoaCreateViewAndContext(NativeNSViewRef *ppView, NativeNSOpenGLContextRef *ppCtx,
711 NativeNSViewRef pParentView, uint32_t cx, uint32_t cy,
712 NativeNSOpenGLContextRef pSharedCtx, bool fOtherProfile)
[53206]713{
[56333]714 LogFlow(("vmsvga3dCocoaCreateViewAndContext: pParentView=%d size=%d,%d pSharedCtx=%p fOtherProfile=%RTbool\n",
[55840]715 (void *)pParentView, cx, cy, (void *)pSharedCtx, fOtherProfile));
[53206]716 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[55840]717 vmsvga3dCocoaServiceRunLoop();
[53206]718
[55840]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
[53206]741 [pPool release];
[55840]742 LogFlow(("vmsvga3dCocoaDestroyContext: returns %RTbool\n", fRet));
743 return fRet;
[53206]744}
745
[55840]746
747VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaDestroyViewAndContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
[53206]748{
[55840]749 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: pView=%p pCtx=%p\n", (void *)pView, (void *)pCtx));
[53206]750 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[55840]751
752 /* The view */
[57137]753 VMSVGA3DOverlayView *pOvlView = (VMSVGA3DOverlayView *)pView;
754 [pOvlView vboxRemoveFromSuperviewAndHide];
[57131]755
[57137]756 Log(("vmsvga3dCocoaDestroyViewAndContext: view %p ref count=%d\n", (void *)pOvlView, [pOvlView retainCount]));
757 [pOvlView release];
[53599]758
[55840]759 /* The OpenGL context. */
[56419]760 Log(("vmsvga3dCocoaDestroyViewAndContext: ctx %p ref count=%d\n", (void *)pCtx, [pCtx retainCount]));
[55840]761 [pCtx release];
762
[53206]763 [pPool release];
[55840]764 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: returns\n"));
[53206]765}
766
[55840]767
[57154]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;
[57358]774
[57154]775 NSRect FrameRect = [pOvlView frame];
[57358]776 pHlp->pfnPrintf(pHlp, " Frame rect: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
[57154]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];
[57358]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),
[57154]782 FLOAT_FMT_ARGS(BoundsRect.size.width), FLOAT_FMT_ARGS(BoundsRect.size.height));
783 NSRect VisibleRect = [pOvlView visibleRect];
[57358]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),
[57154]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);
[57358]790 pHlp->pfnPrintf(pHlp, " wantsLayer: %RTbool\n", [pOvlView wantsLayer] != NO);
[57154]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);
[57358]794 pHlp->pfnPrintf(pHlp, " autoresizingMask: %#x\n", [pOvlView autoresizingMask]);
[57154]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];
[57358]805 pHlp->pfnPrintf(pHlp, " Superview#%u frame: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
[57154]806 iLevel,
[57358]807 FLOAT_FMT_ARGS(FrameRect.origin.x), FLOAT_FMT_ARGS(FrameRect.origin.y),
[57154]808 FLOAT_FMT_ARGS(FrameRect.size.width), FLOAT_FMT_ARGS(FrameRect.size.height));
809 BoundsRect = [pCurView bounds];
[57358]810 pHlp->pfnPrintf(pHlp, " Superview#%u bounds: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
[57154]811 iLevel,
[57358]812 FLOAT_FMT_ARGS(BoundsRect.origin.x), FLOAT_FMT_ARGS(BoundsRect.origin.y),
[57154]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];
[57358]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),
[57154]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
[57131]840/** @note Not currently used. */
[55840]841VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
[53206]842{
[63482]843 RT_NOREF(pParentView);
[55840]844 LogFlow(("vmsvga3dCocoaViewSetPosition: pView=%p pParentView=%p (%d,%d)\n", (void *)pView, (void *)pParentView, x, y));
[53206]845 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
846
[57131]847 [(VMSVGA3DOverlayView *)pView vboxSetPos:NSMakePoint(x, y)];
[53206]848
849 [pPool release];
[55840]850 LogFlow(("vmsvga3dCocoaViewSetPosition: returns\n"));
[53206]851}
852
[55840]853
[57790]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
[55840]869VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int cx, int cy)
[53206]870{
[55840]871 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p (%d,%d)\n", (void *)pView, cx, cy));
[53206]872 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[55840]873 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
[53206]874
[57131]875 [pOverlayView vboxSetSize:NSMakeSize(cx, cy)];
[53206]876
877 [pPool release];
[55840]878 LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
[53206]879}
880
[55840]881
[53206]882void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
883{
[57131]884 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
[53206]885 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[55840]886 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
[53206]887
[54638]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
[55840]893 if (pOverlayView)
[53206]894 {
[55840]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);
[53206]898 [pCtx makeCurrentContext];
[57131]899 [pOverlayView vboxUpdateCtxIfNecessary];
[53206]900 }
901 else
[56333]902 [NSOpenGLContext clearCurrentContext];
[53206]903
904 [pPool release];
[57135]905 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: returns\n"));
[53206]906}
907
[55840]908
[53599]909void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
[53206]910{
[55840]911 LogFlow(("vmsvga3dCocoaSwapBuffers: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
[53206]912 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[57131]913 VMSVGA3DOverlayView *pMyView = (VMSVGA3DOverlayView *)pView;
[55840]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
[53206]925 [pCtx flushBuffer];
[55840]926 //[pView setNeedsDisplay:YES];
927 vmsvga3dCocoaServiceRunLoop();
[56333]928
[57131]929 /* If buffer clearing or/and context updates are pending, execute that now. */
930 [pMyView vboxUpdateCtxIfNecessary];
931 [pMyView vboxClearBackBufferIfNecessary];
932
[53206]933 [pPool release];
[55840]934 LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
[53206]935}
[54167]936
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use