VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use