VirtualBox

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

Last change on this file since 97697 was 97380, checked in by vboxsync, 2 years ago

Devices/Graphics: Fix crash on Catalina if the old 3D code is enabled

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.6 KB
Line 
1/* $Id: DevVGA-SVGA3d-cocoa.m 97380 2022-11-03 10:57:23Z 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-2022 Oracle and/or its affiliates.
10 *
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
28 */
29
30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
34#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
35#include "DevVGA-SVGA3d-cocoa.h"
36#import <Cocoa/Cocoa.h>
37#undef PVM /* Stupid namespace pollution from outdated sys/param.h header file. */
38#import <OpenGL/gl.h>
39
40#include <iprt/thread.h>
41#include <iprt/assert.h>
42#include <iprt/string.h>
43#include <VBox/log.h>
44#include <VBox/vmm/dbgf.h>
45
46
47/*********************************************************************************************************************************
48* Defined Constants And Macros *
49*********************************************************************************************************************************/
50/** @def USE_NSOPENGLVIEW
51 * Define this to experiment with using NSOpenGLView instead
52 * of NSView. There are transparency issues with the former,
53 * so for the time being we're using the latter. */
54#if 0 || DOXYGEN_RUNNING
55# define USE_NSOPENGLVIEW
56#endif
57
58/**@def FLOAT_FMT_STR
59 * Format string bits to go with FLOAT_FMT_ARGS. */
60#define FLOAT_FMT_STR "%d.%06u"
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. */
64#define FLOAT_FMT_ARGS(r) (int)(r), ((unsigned)(RT_ABS(r) * 1000000) % 1000000U)
65
66
67
68/*********************************************************************************************************************************
69* Structures and Typedefs *
70*********************************************************************************************************************************/
71/**
72 * Argument package for doing this on the main thread.
73 */
74@interface VMSVGA3DCreateViewAndContext : NSObject
75{
76@public
77 /* in */
78 NativeNSViewRef pParentView;
79 uint32_t cx;
80 uint32_t cy;
81 NativeNSOpenGLContextRef pSharedCtx;
82 bool fOtherProfile;
83
84 /* out */
85 NativeNSViewRef pView;
86 NativeNSOpenGLContextRef pCtx;
87}
88@end
89
90
91/**
92 * The overlay view.
93 */
94@interface VMSVGA3DOverlayView
95#ifdef USE_NSOPENGLVIEW
96 : NSOpenGLView
97#else
98 : NSView
99#endif
100{
101@private
102 /** This points to the parent view, if there is one. If there isn't a parent
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. */
106 NSView *m_pParentView;
107 /** Indicates that buffers (back+front) needs clearing before use because
108 * the view changed size. There are two buffers, so this is set to two
109 * each time when the view area increases. */
110 uint32_t m_cClears;
111 /** Set if the OpenGL context needs updating after a resize. */
112 bool m_fUpdateCtx;
113
114#ifndef USE_NSOPENGLVIEW
115 /** The OpenGL context associated with this view. */
116 NSOpenGLContext *m_pCtx;
117 /** Number of times we've tried to set the view (shut up noisy NSLog). */
118 uint32_t m_cSetViewAttempts;
119#endif
120
121 /** The desired view position relative to super. */
122 NSPoint m_Pos;
123 /** The desired view size. */
124 NSSize m_Size;
125}
126+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams;
127- (id)initWithFrameAndFormat:(NSRect)frame parentView:(NSView*)pparentView pixelFormat:(NSOpenGLPixelFormat *)pFmt;
128- (void)vboxSetPos:(NSPoint)pos;
129- (void)vboxSetSize:(NSSize)size;
130- (void)vboxScheduleCtxUpdate;
131- (void)vboxReshapePerform;
132- (void)vboxReshape;
133- (void)vboxBoundsDidChange:(NSNotification *)pNotification;
134- (void)vboxFrameDidChange:(NSNotification *)pNotification;
135- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification;
136- (BOOL)postsFrameChangedNotifications;
137- (void)vboxRemoveFromSuperviewAndHide;
138- (void)vboxUpdateCtxIfNecessary;
139- (void)vboxClearBackBufferIfNecessary;
140- (NSOpenGLContext *)makeCurrentGLContext;
141- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx;
142
143#ifndef USE_NSOPENGLVIEW
144/* NSOpenGLView fakes: */
145- (void)setOpenGLContext:(NSOpenGLContext *)pCtx;
146- (NSOpenGLContext *)openGLContext;
147- (void)prepareOpenGL;
148
149#endif
150/* Overridden: */
151- (void)viewDidMoveToWindow;
152- (void)viewDidMoveToSuperview;
153- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize;
154- (void)drawRect:(NSRect)rect;
155
156@end
157
158
159/********************************************************************************
160*
161* VMSVGA3DOverlayView class implementation
162*
163********************************************************************************/
164@implementation VMSVGA3DOverlayView
165
166
167+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams
168{
169 LogFlow(("OvlView createViewAndContext:\n"));
170
171 /*
172 * Create a pixel format.
173 */
174 NSOpenGLPixelFormat *pFmt = nil;
175
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;
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;
201 VMSVGA3DOverlayView *pView = [[VMSVGA3DOverlayView alloc] initWithFrameAndFormat:Frame
202 parentView:pParams->pParentView
203 pixelFormat:pFmt];
204 if (pView)
205 {
206 /*
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,
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 {
230 /*
231 * Attach the view to the parent if we have one. Otherwise make sure its invisible.
232 */
233 if (pParams->pParentView)
234 [pParams->pParentView addSubview:pView];
235 else
236 [pView setHidden:YES];
237
238 /*
239 * Resize and return.
240 */
241 //[pView vboxSetSize:Frame.size];
242
243 NSOpenGLContext *pSavedCtx = [pView makeCurrentGLContext];
244
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];
250
251 if (pParams->pParentView)
252 [pView setHidden:NO];
253 else
254 [pView setHidden:YES];
255
256 [pView restoreSavedGLContext:pSavedCtx];
257
258 pParams->pView = pView;
259 pParams->pCtx = pCtx;
260 [pCtx retain]; //??
261
262 [pFmt release];
263
264 LogFlow(("OvlView createViewAndContext: returns successfully\n"));
265 return;
266 }
267 [pView release];
268 }
269 [pFmt release];
270 }
271 else
272 AssertFailed();
273
274 LogFlow(("OvlView createViewAndContext: returns failure\n"));
275 return;
276}
277
278- (id)initWithFrameAndFormat:(NSRect) frame parentView:(NSView *)pParentView pixelFormat:(NSOpenGLPixelFormat *)pFmt
279{
280 LogFlow(("OvlView(%p) initWithFrameAndFormat:\n", (void *)self));
281
282 m_pParentView = pParentView;
283 /* Make some reasonable defaults */
284 m_Pos = NSZeroPoint;
285 m_Size = frame.size;
286 m_cClears = 2;
287 m_fUpdateCtx = true;
288
289#ifdef USE_NSOPENGLVIEW
290 self = [super initWithFrame:frame pixelFormat:pFmt];
291#else
292 RT_NOREF(pFmt);
293 m_cSetViewAttempts = 0;
294 m_pCtx = NULL;
295 self = [super initWithFrame:frame];
296#endif
297 if (self)
298 {
299 //self.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;
300 self.autoresizingMask = NSViewNotSizable;
301
302 /*
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.
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 }
325 }
326 LogFlow(("OvlView(%p) initWithFrameAndFormat: returns %p\n", (void *)self, (void *)self));
327 return self;
328}
329
330- (void)dealloc
331{
332 LogFlow(("OvlView(%p) dealloc:\n", (void *)self));
333
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
344
345 [super dealloc];
346
347 LogFlow(("OvlView(%p) dealloc: returns\n", (void *)self));
348}
349
350
351- (void)vboxSetPos:(NSPoint)pos
352{
353 Log(("OvlView(%p) vboxSetPos: (%d,%d)\n", (void *)self, (int)pos.x, (int)pos.y));
354
355 m_Pos = pos;
356 [self vboxReshape];
357
358 LogFlow(("OvlView(%p) vboxSetPos: returns\n", (void *)self));
359}
360
361
362- (void)vboxSetSize:(NSSize)size
363{
364 Log(("OvlView(%p) vboxSetSize: (%d,%d):\n", (void *)self, (int)size.width, (int)size.height));
365 m_Size = size;
366 [self vboxReshape];
367 LogFlow(("OvlView(%p) vboxSetSize: returns\n", (void *)self));
368}
369
370- (void)vboxScheduleCtxUpdate
371{
372 m_fUpdateCtx = true;
373}
374
375- (void)vboxUpdateCtxIfNecessary
376{
377 if (m_fUpdateCtx)
378 {
379 Log(("OvlView(%p) vboxUpdateCtxIfNecessary: m_fUpdateCtx\n", (void *)self));
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];
382 m_fUpdateCtx = false;
383 }
384}
385
386
387- (void)vboxClearBackBufferIfNecessary
388{
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--;
396
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{
430 /*
431 * Change the size and position if necessary.
432 */
433 NSRect CurFrameRect = [self frame];
434 /** @todo conversions? */
435 if ( m_Pos.x != CurFrameRect.origin.x
436 || m_Pos.y != CurFrameRect.origin.y)
437 {
438 LogFlow(("OvlView(%p) vboxReshapePerform: moving (%d,%d) -> (%d,%d)\n",
439 (void *)self, CurFrameRect.origin.x, CurFrameRect.origin.y, m_Pos.x, m_Pos.y));
440 [self setFrameOrigin:m_Pos];
441 }
442
443 if ( CurFrameRect.size.width != m_Size.width
444 || CurFrameRect.size.height != m_Size.height)
445 {
446 LogFlow(("OvlView(%p) vboxReshapePerform: resizing (%d,%d) -> (%d,%d)\n",
447 (void *)self, CurFrameRect.size.width, CurFrameRect.size.height, m_Size.width, m_Size.height));
448 [self setFrameSize:m_Size];
449
450 /*
451 * Schedule two clears and a context update for now.
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
465 /*
466 * Resize the view.
467 */
468 if ([NSThread isMainThread])
469 [self vboxReshapePerform];
470 else
471 {
472 [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
473 vmsvga3dCocoaServiceRunLoop();
474
475 /*
476 * Try update the opengl context.
477 */
478 [[self openGLContext] update];
479 }
480
481 LogFlow(("OvlView(%p) vboxReshape: returns\n", (void *)self));
482}
483
484/**
485 * This is called when the bounds change.
486 *
487 * We indicate that the FIFO thread must update the GL context.
488 */
489- (void)vboxBoundsDidChange:(NSNotification *)pNotification
490{
491 RT_NOREF(pNotification);
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.
498 *
499 * We indicate that the FIFO thread must update the GL context.
500 */
501- (void)vboxFrameDidChange:(NSNotification *)pNotification
502{
503 RT_NOREF(pNotification);
504 LogFlow(("OvlView(%p) vboxFrameDidChange:\n", (void *)self));
505 self->m_fUpdateCtx = true;
506}
507
508/**
509 * This is called when moved to different screen/GPU or/and when the GL context
510 * needs updating.
511 *
512 * We indicate that the FIFO thread must update the GL context.
513 */
514- (void)vboxFrameDidChangeGlobal:(NSNotification *)pNotification
515{
516 RT_NOREF(pNotification);
517 LogFlow(("OvlView(%p) vboxFrameDidChangeGlobal:\n", (void *)self));
518 self->m_fUpdateCtx = true;
519}
520
521/** This enables the vboxFrameDidChange notification. */
522- (BOOL)postsFrameChangedNotifications
523{
524 LogFlow(("OvlView(%p) postsFrameChangedNotifications:\n", (void *)self));
525 return YES;
526}
527
528/**
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.
532 */
533- (void)vboxRemoveFromSuperviewAndHide
534{
535 LogFlow(("OvlView(%p) vboxRemoveFromSuperviewAndHide:\n", (void *)self));
536 if (m_pParentView)
537 {
538 /*
539 * The removeFromSuperview has been frequently seen to deadlock thing like this:
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 }
565 }
566}
567
568
569/**
570 * Changes to the OpenGL context associated with the view.
571 * @returns Previous OpenGL context.
572 */
573- (NSOpenGLContext *)makeCurrentGLContext
574{
575 NSOpenGLContext *pSavedCtx = [NSOpenGLContext currentContext];
576
577 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
578 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
579 if (pSavedCtx != nil)
580 glFlush();
581
582 [[self openGLContext] makeCurrentContext];
583 return pSavedCtx;
584}
585
586
587/**
588 * Restores the previous OpenGL context after
589 * makeCurrentGLContext.
590 *
591 * @param pSavedCtx The makeCurrentGLContext return value.
592 */
593- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx
594{
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];
603}
604
605#ifndef USE_NSOPENGLVIEW
606/*
607 * Faking NSOpenGLView interface.
608 */
609- (void)setOpenGLContext:(NSOpenGLContext *)pCtx
610{
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 }
619
620 if (m_pCtx)
621 [m_pCtx release];
622
623 m_pCtx = pCtx;
624
625 if (pCtx)
626 [pCtx update];
627 }
628}
629
630- (NSOpenGLContext *)openGLContext
631{
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. */
635 if (m_pCtx && [m_pCtx view] != self)
636 {
637 m_cSetViewAttempts++;
638 if ( m_pParentView
639 || m_cSetViewAttempts < 64
640 || (m_cSetViewAttempts & (m_cSetViewAttempts < _64K ? 0xfff : 0x7fff)) == 0 )
641 [m_pCtx setView:self];
642 }
643 return m_pCtx;
644}
645
646- (void)prepareOpenGL
647{
648 //[m_pCtx prepareOpenGL];
649}
650#endif /* USE_NSOPENGLVIEW */
651
652/*
653 * Overridden NSOpenGLView / NSView methods:
654 */
655
656/** @todo do we need this? */
657-(void)viewDidMoveToWindow
658{
659 LogFlow(("OvlView(%p) viewDidMoveToWindow: new win: %p\n", (void *)self, (void *)[self window]));
660 [super viewDidMoveToWindow];
661 [self vboxReshape];
662}
663
664-(void)viewDidMoveToSuperview
665{
666 LogFlow(("OvlView(%p) viewDidMoveToSuperview: new view: %p\n", (void *)self, (void *)[self superview]));
667 [super viewDidMoveToSuperview];
668 [self vboxReshape];
669}
670
671-(void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize
672{
673 LogFlow(("OvlView(%p) resizeWithOldSuperviewSize: %d,%d -> %d,%d\n", (void *)self,
674 (int)oldBoundsSize.width, (int)oldBoundsSize.height, (int)[self bounds].size.width, (int)[self bounds].size.height));
675 [super resizeWithOldSuperviewSize:oldBoundsSize];
676 [self vboxReshape];
677}
678
679- (void)drawRect:(NSRect)rect
680{
681 RT_NOREF(rect);
682// if (m_fClear)
683// {
684// m_fClear = false;
685// [self vboxClearBuffers];
686// }
687}
688
689@end /* VMSVGA3DOverlayView */
690
691@implementation VMSVGA3DCreateViewAndContext
692@end
693
694
695VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaServiceRunLoop(void)
696{
697 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
698 NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
699
700 if ([NSRunLoop mainRunLoop] != pRunLoop)
701 {
702 [pRunLoop runUntilDate:[NSDate distantPast]];
703 }
704
705 [pPool release];
706}
707
708
709/**
710 * Document me later.
711 *
712 * @param ppView
713 * @param ppCtx
714 * @param pParentView The parent view if this is a context we'll be
715 * presenting to.
716 * @param cx
717 * @param cy
718 * @param pSharedCtx
719 * @param fOtherProfile
720 */
721VMSVGA3DCOCOA_DECL(bool) vmsvga3dCocoaCreateViewAndContext(NativeNSViewRef *ppView, NativeNSOpenGLContextRef *ppCtx,
722 NativeNSViewRef pParentView, uint32_t cx, uint32_t cy,
723 NativeNSOpenGLContextRef pSharedCtx, bool fOtherProfile)
724{
725 LogFlow(("vmsvga3dCocoaCreateViewAndContext: pParentView=%d size=%d,%d pSharedCtx=%p fOtherProfile=%RTbool\n",
726 (void *)pParentView, cx, cy, (void *)pSharedCtx, fOtherProfile));
727 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
728 vmsvga3dCocoaServiceRunLoop();
729
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
752 [pPool release];
753 LogFlow(("vmsvga3dCocoaDestroyContext: returns %RTbool\n", fRet));
754 return fRet;
755}
756
757
758VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaDestroyViewAndContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
759{
760 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: pView=%p pCtx=%p\n", (void *)pView, (void *)pCtx));
761 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
762
763 /* The view */
764 VMSVGA3DOverlayView *pOvlView = (VMSVGA3DOverlayView *)pView;
765 [pOvlView vboxRemoveFromSuperviewAndHide];
766
767 Log(("vmsvga3dCocoaDestroyViewAndContext: view %p ref count=%d\n", (void *)pOvlView, [pOvlView retainCount]));
768 [pOvlView release];
769
770 /* The OpenGL context. */
771 Log(("vmsvga3dCocoaDestroyViewAndContext: ctx %p ref count=%d\n", (void *)pCtx, [pCtx retainCount]));
772 [pCtx release];
773
774 [pPool release];
775 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: returns\n"));
776}
777
778
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;
785
786 NSRect FrameRect = [pOvlView frame];
787 pHlp->pfnPrintf(pHlp, " Frame rect: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
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];
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),
793 FLOAT_FMT_ARGS(BoundsRect.size.width), FLOAT_FMT_ARGS(BoundsRect.size.height));
794 NSRect VisibleRect = [pOvlView visibleRect];
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),
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);
801 pHlp->pfnPrintf(pHlp, " wantsLayer: %RTbool\n", [pOvlView wantsLayer] != NO);
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);
805 pHlp->pfnPrintf(pHlp, " autoresizingMask: %#x\n", [pOvlView autoresizingMask]);
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];
816 pHlp->pfnPrintf(pHlp, " Superview#%u frame: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
817 iLevel,
818 FLOAT_FMT_ARGS(FrameRect.origin.x), FLOAT_FMT_ARGS(FrameRect.origin.y),
819 FLOAT_FMT_ARGS(FrameRect.size.width), FLOAT_FMT_ARGS(FrameRect.size.height));
820 BoundsRect = [pCurView bounds];
821 pHlp->pfnPrintf(pHlp, " Superview#%u bounds: x=" FLOAT_FMT_STR ", y=" FLOAT_FMT_STR " cx=" FLOAT_FMT_STR ", cy=" FLOAT_FMT_STR "\n",
822 iLevel,
823 FLOAT_FMT_ARGS(BoundsRect.origin.x), FLOAT_FMT_ARGS(BoundsRect.origin.y),
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];
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),
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
851/** @note Not currently used. */
852VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
853{
854 RT_NOREF(pParentView);
855 LogFlow(("vmsvga3dCocoaViewSetPosition: pView=%p pParentView=%p (%d,%d)\n", (void *)pView, (void *)pParentView, x, y));
856 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
857
858 [(VMSVGA3DOverlayView *)pView vboxSetPos:NSMakePoint(x, y)];
859
860 [pPool release];
861 LogFlow(("vmsvga3dCocoaViewSetPosition: returns\n"));
862}
863
864
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
880VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int cx, int cy)
881{
882 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p (%d,%d)\n", (void *)pView, cx, cy));
883 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
884 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
885
886 [pOverlayView vboxSetSize:NSMakeSize(cx, cy)];
887
888 [pPool release];
889 LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
890}
891
892
893void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
894{
895 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
896 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
897 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
898
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
904 if (pOverlayView)
905 {
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);
909 [pCtx makeCurrentContext];
910 [pOverlayView vboxUpdateCtxIfNecessary];
911 }
912 else
913 [NSOpenGLContext clearCurrentContext];
914
915 [pPool release];
916 LogFlow(("vmsvga3dCocoaViewMakeCurrentContext: returns\n"));
917}
918
919
920void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
921{
922 LogFlow(("vmsvga3dCocoaSwapBuffers: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
923 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
924 VMSVGA3DOverlayView *pMyView = (VMSVGA3DOverlayView *)pView;
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
936 [pCtx flushBuffer];
937 //[pView setNeedsDisplay:YES];
938 vmsvga3dCocoaServiceRunLoop();
939
940 /* If buffer clearing or/and context updates are pending, execute that now. */
941 [pMyView vboxUpdateCtxIfNecessary];
942 [pMyView vboxClearBackBufferIfNecessary];
943
944 [pPool release];
945 LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
946}
947
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