VirtualBox

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

Last change on this file since 57064 was 56419, checked in by vboxsync, 10 years ago

logging fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 KB
Line 
1/* $Id: DevVGA-SVGA3d-cocoa.m 56419 2015-06-14 18:27:41Z vboxsync $ */
2/** @file
3 * VirtualBox OpenGL Cocoa Window System Helper Implementation.
4 *
5 * @remarks Inspired by HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m.
6 */
7
8/*
9 * Copyright (C) 2009-2015 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
24#include "DevVGA-SVGA3d-cocoa.h"
25#import <Cocoa/Cocoa.h>
26#import <OpenGL/gl.h>
27
28#include <iprt/thread.h>
29#include <iprt/assert.h>
30#include <iprt/string.h>
31#include <VBox/log.h>
32
33
34/*******************************************************************************
35* Defined Constants And Macros *
36*******************************************************************************/
37/** @def USE_NSOPENGLVIEW
38 * Define this to experiment with using NSOpenGLView instead
39 * of NSView. There are transparency issues with the former,
40 * so for the time being we're using the latter. */
41#if 0
42#define USE_NSOPENGLVIEW
43#endif
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Argument package for doing this on the main thread.
51 */
52@interface VMSVGA3DCreateViewAndContext : NSObject
53{
54@public
55 /* in */
56 NativeNSViewRef pParentView;
57 uint32_t cx;
58 uint32_t cy;
59 NativeNSOpenGLContextRef pSharedCtx;
60 bool fOtherProfile;
61
62 /* out */
63 NativeNSViewRef pView;
64 NativeNSOpenGLContextRef pCtx;
65}
66@end
67
68
69/**
70 * The overlay view.
71 */
72@interface VMSVGA3DOverlayView
73#ifdef USE_NSOPENGLVIEW
74 : NSOpenGLView
75#else
76 : NSView
77#endif
78{
79@private
80 NSView *m_pParentView;
81 /** Set if the buffers needs clearing. */
82 bool m_fClear;
83
84#ifndef USE_NSOPENGLVIEW
85 /** The OpenGL context associated with this view. */
86 NSOpenGLContext *m_pCtx;
87#endif
88
89 /* Position/Size tracking */
90 NSPoint m_Pos;
91 NSSize m_Size;
92
93 /** This is necessary for clipping on the root window */
94 NSRect m_RootRect;
95}
96+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams;
97- (id)initWithFrameAndFormat:(NSRect)frame parentView:(NSView*)pparentView pixelFormat:(NSOpenGLPixelFormat *)pFmt;
98- (void)setPos:(NSPoint)pos;
99- (void)setSize:(NSSize)size;
100- (void)vboxReshape;
101- (void)vboxClearBuffers;
102- (NSOpenGLContext *)makeCurrentGLContext;
103- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx;
104
105#ifndef USE_NSOPENGLVIEW
106/* NSOpenGLView fakes: */
107- (void)setOpenGLContext:(NSOpenGLContext *)pCtx;
108- (NSOpenGLContext *)openGLContext;
109- (void)prepareOpenGL;
110
111#endif
112/* Overridden: */
113- (void)viewDidMoveToWindow;
114- (void)viewDidMoveToSuperview;
115- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize;
116- (void)drawRect:(NSRect)rect;
117
118@end
119
120
121/********************************************************************************
122*
123* VMSVGA3DOverlayView class implementation
124*
125********************************************************************************/
126@implementation VMSVGA3DOverlayView
127
128
129+ (void)createViewAndContext:(VMSVGA3DCreateViewAndContext *)pParams
130{
131 LogFlow(("OvlView createViewAndContext:\n"));
132
133 /*
134 * Create a pixel format.
135 */
136 NSOpenGLPixelFormat *pFmt = nil;
137
138 // Consider to remove it and check if it's harmless.
139 NSOpenGLPixelFormatAttribute attribs[] =
140 {
141 NSOpenGLPFAOpenGLProfile, (NSOpenGLPixelFormatAttribute)0,
142 //NSOpenGLPFAWindow, - obsolete/deprecated, try work without it...
143 NSOpenGLPFAAccelerated,
144 NSOpenGLPFADoubleBuffer,
145 NSOpenGLPFABackingStore,
146 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
147 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8,
148 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
149 0
150 };
151 attribs[1] = pParams->fOtherProfile ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy;
152 pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
153 if (pFmt)
154 {
155 /*
156 * Create a new view.
157 */
158 NSRect Frame;
159 Frame.origin.x = 0;
160 Frame.origin.y = 0;
161 Frame.size.width = pParams->cx < _1M ? pParams->cx : 0;
162 Frame.size.height = pParams->cy < _1M ? pParams->cy : 0;
163 VMSVGA3DOverlayView *pView = [[VMSVGA3DOverlayView alloc] initWithFrameAndFormat:Frame
164 parentView:pParams->pParentView
165 pixelFormat:pFmt];
166 if (pView)
167 {
168 /*
169 * If we have no shared GL context, we use the one that NSOpenGLView create. Otherwise,
170 * we replace it. (If we don't call openGLContext, it won't yet have been instantiated,
171 * so there is no unecessary contexts created here when pSharedCtx != NULL.)
172 */
173 NSOpenGLContext *pCtx;
174#ifdef USE_NSOPENGLVIEW
175 if (!pParams->pSharedCtx)
176 pCtx = [pView openGLContext];
177 else
178#endif
179 {
180 pCtx = [[NSOpenGLContext alloc] initWithFormat:pFmt shareContext: pParams->pSharedCtx];
181 if (pCtx)
182 {
183 [pView setOpenGLContext:pCtx];
184 [pCtx setView:pView];
185#ifdef USE_NSOPENGLVIEW
186 Assert([pCtx view] == pView);
187#endif
188 }
189 }
190 if (pCtx)
191 {
192 /*
193 * Attach the view to the parent.
194 */
195 [pParams->pParentView addSubview:pView];
196
197 /*
198 * Resize and return.
199 */
200 //[pView setSize:Frame.size];
201
202 NSOpenGLContext *pSavedCtx = [pView makeCurrentGLContext];
203
204 [pView prepareOpenGL];
205 GLint x;
206 //x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSwapInterval];
207 //x = 1; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOrder];
208 x = 0; [pCtx setValues:&x forParameter:NSOpenGLCPSurfaceOpacity];
209
210 [pView setHidden:NO];
211
212 [pView restoreSavedGLContext:pSavedCtx];
213
214 pParams->pView = pView;
215 pParams->pCtx = pCtx;
216 [pCtx retain]; //??
217
218 [pFmt release];
219 LogFlow(("OvlView createViewAndContext: returns successfully\n"));
220 return;
221 }
222 [pView release];
223 }
224 [pFmt release];
225 }
226 else
227 AssertFailed();
228
229 LogFlow(("OvlView createViewAndContext: returns failure\n"));
230 return;
231}
232
233- (id)initWithFrameAndFormat:(NSRect) frame parentView:(NSView*)pParentView pixelFormat:(NSOpenGLPixelFormat *)pFmt
234{
235 LogFlow(("OvlView(%p) initWithFrameAndFormat:\n", (void *)self));
236
237 m_pParentView = pParentView;
238 /* Make some reasonable defaults */
239 m_Pos = NSZeroPoint;
240 m_Size = frame.size;
241 m_RootRect = NSMakeRect(0, 0, m_Size.width, m_Size.height);
242
243#ifdef USE_NSOPENGLVIEW
244 self = [super initWithFrame:frame pixelFormat:pFmt];
245#else
246 self = [super initWithFrame:frame];
247#endif
248 if (self)
249 {
250 self.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;
251 }
252 LogFlow(("OvlView(%p) initWithFrameAndFormat: returns %p\n", (void *)self, (void *)self));
253 return self;
254}
255
256- (void)dealloc
257{
258 LogFlow(("OvlView(%p) dealloc:\n", (void *)self));
259
260#ifdef USE_NSOPENGLVIEW
261 [[self openGLContext] clearDrawable];
262#else
263 if (m_pCtx)
264 {
265 [m_pCtx clearDrawable];
266 [m_pCtx release];
267 m_pCtx = nil;
268 }
269#endif
270
271 [super dealloc];
272
273 LogFlow(("OvlView(%p) dealloc: returns\n", (void *)self));
274}
275
276
277- (void)setPos:(NSPoint)pos
278{
279 Log(("OvlView(%p) setPos: (%d,%d)\n", (void *)self, (int)pos.x, (int)pos.y));
280
281 m_Pos = pos;
282 [self vboxReshape];
283
284 LogFlow(("OvlView(%p) setPos: returns\n", (void *)self));
285}
286
287
288- (void)setSize:(NSSize)size
289{
290 Log(("OvlView(%p) setSize: (%d,%d):\n", (void *)self, (int)size.width, (int)size.height));
291 m_Size = size;
292 [self vboxReshape];
293 LogFlow(("OvlView(%p) setSize: returns\n", (void *)self));
294}
295
296
297- (void)vboxClearBuffers
298{
299#if 1 /* experiment */
300 if ([NSThread isMainThread])
301 Log(("OvlView(%p) vboxClearBuffers: skip, main thread\n", (void *)self));
302 else
303 {
304 Log(("OvlView(%p) vboxClearBuffers: clears\n", (void *)self));
305 NSOpenGLContext *pSavedCtx = [self makeCurrentGLContext];
306
307 GLint iOldDrawBuf = GL_BACK;
308 glGetIntegerv(GL_DRAW_BUFFER, &iOldDrawBuf);
309 glDrawBuffer(GL_FRONT_AND_BACK);
310 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
311 glClear(GL_COLOR_BUFFER_BIT /*|GL_DEPTH_BUFFER_BIT*/ );
312 [[self openGLContext] flushBuffer];
313 glDrawBuffer(iOldDrawBuf);
314
315 [self restoreSavedGLContext:pSavedCtx];
316 }
317#endif
318}
319
320
321- (void)vboxReshape
322{
323 LogFlow(("OvlView(%p) vboxReshape:\n", (void *)self));
324
325 /*
326 * Not doing any complicate stuff here yet, hoping that we'll get correctly
327 * resized when the parent view changes...
328 */
329
330 /*
331 * Tell the GL context.
332 */
333 //[[self openGLContext] setView:self];
334 [[self openGLContext] update];
335
336 [self vboxClearBuffers];
337
338 LogFlow(("OvlView(%p) vboxReshape: returns\n", (void *)self));
339}
340
341
342/**
343 * Changes to the OpenGL context associated with the view.
344 * @returns Previous OpenGL context.
345 */
346- (NSOpenGLContext *)makeCurrentGLContext
347{
348 NSOpenGLContext *pSavedCtx = [NSOpenGLContext currentContext];
349
350 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
351 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
352 if (pSavedCtx != nil)
353 glFlush();
354
355 [[self openGLContext] makeCurrentContext];
356 return pSavedCtx;
357}
358
359
360/**
361 * Restores the previous OpenGL context after
362 * makeCurrentGLContext.
363 *
364 * @param pSavedCtx The makeCurrentGLContext return value.
365 */
366- (void)restoreSavedGLContext:(NSOpenGLContext *)pSavedCtx
367{
368 /* Always flush before changing. glXMakeCurrent and wglMakeCurrent does this
369 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
370 glFlush();
371
372 if (pSavedCtx)
373 [pSavedCtx makeCurrentContext];
374 else
375 [NSOpenGLContext clearCurrentContext];
376}
377
378#ifndef USE_NSOPENGLVIEW
379/*
380 * Faking NSOpenGLView interface.
381 */
382- (void)setOpenGLContext:(NSOpenGLContext *)pCtx
383{
384 if (pCtx != m_pCtx)
385 {
386 if (pCtx)
387 {
388 [pCtx retain];
389 [pCtx setView:self];
390 /*Assert([pCtx view] == self); - setView fails early on, works later... */
391 }
392
393 if (m_pCtx)
394 [m_pCtx release];
395
396 m_pCtx = pCtx;
397
398 if (pCtx)
399 [pCtx update];
400 }
401}
402
403- (NSOpenGLContext *)openGLContext
404{
405 /* Stupid hack to work around setView failing early. */
406 if (m_pCtx && [m_pCtx view] != self)
407 [m_pCtx setView:self];
408 return m_pCtx;
409}
410
411- (void)prepareOpenGL
412{
413 //[m_pCtx prepareOpenGL];
414}
415#endif /* USE_NSOPENGLVIEW */
416
417/*
418 * Overridden NSOpenGLView / NSView methods:
419 */
420
421-(void)viewDidMoveToWindow
422{
423 LogFlow(("OvlView(%p) viewDidMoveToWindow: new win: %p\n", (void *)self, (void *)[self window]));
424 [super viewDidMoveToWindow];
425 [self vboxReshape];
426}
427
428-(void)viewDidMoveToSuperview
429{
430 LogFlow(("OvlView(%p) viewDidMoveToSuperview: new view: %p\n", (void *)self, (void *)[self superview]));
431 [super viewDidMoveToSuperview];
432 [self vboxReshape];
433}
434
435-(void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize
436{
437 LogFlow(("OvlView(%p) resizeWithOldSuperviewSize: %d,%d -> %d,%d\n", (void *)self,
438 (int)oldBoundsSize.width, (int)oldBoundsSize.height, (int)[self bounds].size.width, (int)[self bounds].size.height));
439 [super resizeWithOldSuperviewSize:oldBoundsSize];
440 [self vboxReshape];
441}
442
443- (void)drawRect:(NSRect)rect
444{
445 if (m_fClear)
446 {
447 m_fClear = false;
448 [self vboxClearBuffers];
449 }
450}
451
452@end /* VMSVGA3DOverlayView */
453
454@implementation VMSVGA3DCreateViewAndContext
455@end
456
457
458VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaServiceRunLoop(void)
459{
460 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
461 NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
462
463 if ([NSRunLoop mainRunLoop] != pRunLoop)
464 {
465 [pRunLoop runUntilDate:[NSDate distantPast]];
466 }
467
468 [pPool release];
469}
470
471
472VMSVGA3DCOCOA_DECL(bool) vmsvga3dCocoaCreateViewAndContext(NativeNSViewRef *ppView, NativeNSOpenGLContextRef *ppCtx,
473 NativeNSViewRef pParentView, uint32_t cx, uint32_t cy,
474 NativeNSOpenGLContextRef pSharedCtx, bool fOtherProfile)
475{
476 LogFlow(("vmsvga3dCocoaCreateViewAndContext: pParentView=%d size=%d,%d pSharedCtx=%p fOtherProfile=%RTbool\n",
477 (void *)pParentView, cx, cy, (void *)pSharedCtx, fOtherProfile));
478 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
479 vmsvga3dCocoaServiceRunLoop();
480
481
482 VMSVGA3DCreateViewAndContext *pParams = [VMSVGA3DCreateViewAndContext alloc];
483 pParams->pParentView = pParentView;
484 pParams->cx = cx;
485 pParams->cy = cy;
486 pParams->pSharedCtx = pSharedCtx;
487 pParams->fOtherProfile = fOtherProfile;
488 pParams->pView = NULL;
489 pParams->pCtx = NULL;
490
491 [VMSVGA3DOverlayView performSelectorOnMainThread:@selector(createViewAndContext:)
492 withObject:pParams
493 waitUntilDone:YES];
494
495 vmsvga3dCocoaServiceRunLoop();
496
497 *ppCtx = pParams->pCtx;
498 *ppView = pParams->pView;
499 bool fRet = *ppCtx != NULL && *ppView != NULL;
500
501 [pParams release];
502
503 [pPool release];
504 LogFlow(("vmsvga3dCocoaDestroyContext: returns %RTbool\n", fRet));
505 return fRet;
506}
507
508
509VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaDestroyViewAndContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
510{
511 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: pView=%p pCtx=%p\n", (void *)pView, (void *)pCtx));
512 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
513
514 /* The view */
515 [pView removeFromSuperview];
516 [pView setHidden: YES];
517 Log(("vmsvga3dCocoaDestroyViewAndContext: view %p ref count=%d\n", (void *)pView, [pView retainCount]));
518 [pView release];
519
520 /* The OpenGL context. */
521 Log(("vmsvga3dCocoaDestroyViewAndContext: ctx %p ref count=%d\n", (void *)pCtx, [pCtx retainCount]));
522 [pCtx release];
523
524 [pPool release];
525 LogFlow(("vmsvga3dCocoaDestroyViewAndContext: returns\n"));
526}
527
528
529VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
530{
531 LogFlow(("vmsvga3dCocoaViewSetPosition: pView=%p pParentView=%p (%d,%d)\n", (void *)pView, (void *)pParentView, x, y));
532 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
533
534 [(VMSVGA3DOverlayView *)pView setPos:NSMakePoint(x, y)];
535
536 [pPool release];
537 LogFlow(("vmsvga3dCocoaViewSetPosition: returns\n"));
538}
539
540
541VMSVGA3DCOCOA_DECL(void) vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int cx, int cy)
542{
543 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p (%d,%d)\n", (void *)pView, cx, cy));
544 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
545 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
546
547 [pOverlayView setSize:NSMakeSize(cx, cy)];
548
549 [pPool release];
550 LogFlow(("vmsvga3dCocoaViewSetSize: returns\n"));
551}
552
553
554void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
555{
556 LogFlow(("vmsvga3dCocoaViewSetSize: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
557 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
558 VMSVGA3DOverlayView *pOverlayView = (VMSVGA3DOverlayView *)pView;
559
560 /* Always flush before flush. glXMakeCurrent and wglMakeCurrent does this
561 implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
562 if ([NSOpenGLContext currentContext] != 0)
563 glFlush();
564
565 if (pOverlayView)
566 {
567 /* This must be a release assertion as we depend on the setView
568 sideeffect of the openGLContext method call. (hack alert!) */
569 AssertRelease([pOverlayView openGLContext] == pCtx);
570 [pCtx makeCurrentContext];
571 }
572 else
573 [NSOpenGLContext clearCurrentContext];
574
575 [pPool release];
576 LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
577}
578
579
580void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
581{
582 LogFlow(("vmsvga3dCocoaSwapBuffers: pView=%p, pCtx=%p\n", (void*)pView, (void*)pCtx));
583 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
584
585#ifndef USE_NSOPENGLVIEW
586 /* Hack alert! setView fails early on so call openGLContext to try again. */
587 VMSVGA3DOverlayView *pMyView = (VMSVGA3DOverlayView *)pView;
588 if ([pCtx view] == NULL)
589 [pMyView openGLContext];
590#elif defined(RT_STRICT)
591 NSOpenGLView *pMyView = (NSOpenGLView *)pView;
592#endif
593
594 Assert(pCtx == [NSOpenGLContext currentContext]);
595 Assert(pCtx == [pMyView openGLContext]);
596 AssertMsg([pCtx view] == pMyView, ("%p != %p\n", (void *)[pCtx view], (void *)pMyView));
597
598 [pCtx flushBuffer];
599 //[pView setNeedsDisplay:YES];
600 vmsvga3dCocoaServiceRunLoop();
601
602 [pPool release];
603 LogFlow(("vmsvga3dCocoaSwapBuffers: returns\n"));
604}
605
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