1 | /* $Id: DevVGA-SVGA3d-cocoa.m 54638 2015-03-05 00:33:47Z 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-2014 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 | #include "DevVGA-SVGA3d-cocoa.h"
|
---|
21 | #import <Cocoa/Cocoa.h>
|
---|
22 | #import <OpenGL/gl.h>
|
---|
23 |
|
---|
24 | #include <iprt/thread.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 |
|
---|
27 | /* Debug macros */
|
---|
28 | #if 0 /*def DEBUG_VERBOSE*/
|
---|
29 | /*# error "should be disabled!"*/
|
---|
30 | # define DEBUG_INFO(text) do { \
|
---|
31 | crWarning text ; \
|
---|
32 | Assert(0); \
|
---|
33 | } while (0)
|
---|
34 |
|
---|
35 | # define DEBUG_MSG(text) \
|
---|
36 | printf text
|
---|
37 |
|
---|
38 | # define DEBUG_WARN(text) do { \
|
---|
39 | crWarning text ; \
|
---|
40 | Assert(0); \
|
---|
41 | } while (0)
|
---|
42 |
|
---|
43 | # define DEBUG_MSG_1(text) \
|
---|
44 | DEBUG_MSG(text)
|
---|
45 |
|
---|
46 | # define DEBUG_FUNC_ENTER() \
|
---|
47 | int cchDebugFuncEnter = printf("==>%s\n", __PRETTY_FUNCTION__)
|
---|
48 |
|
---|
49 | #define DEBUG_FUNC_LEAVE() do { \
|
---|
50 | DEBUG_MSG(("<==%s\n", __PRETTY_FUNCTION__)); \
|
---|
51 | } while (0)
|
---|
52 |
|
---|
53 | #define DEBUG_FUNC_RET(valuefmtnl) do { \
|
---|
54 | DEBUG_MSG(("<==%s returns", __PRETTY_FUNCTION__)); \
|
---|
55 | DEBUG_MSG(valuefmtnl); \
|
---|
56 | } while (0)
|
---|
57 |
|
---|
58 | #else
|
---|
59 |
|
---|
60 | # define DEBUG_INFO(text) do { \
|
---|
61 | crInfo text ; \
|
---|
62 | } while (0)
|
---|
63 |
|
---|
64 | # define DEBUG_MSG(text) \
|
---|
65 | do {} while (0)
|
---|
66 |
|
---|
67 | # define DEBUG_WARN(text) do { \
|
---|
68 | crWarning text ; \
|
---|
69 | } while (0)
|
---|
70 |
|
---|
71 | # define DEBUG_MSG_1(text) \
|
---|
72 | do {} while (0)
|
---|
73 |
|
---|
74 | # define DEBUG_FUNC_ENTER() int cchDebugFuncEnter = 0
|
---|
75 | # define DEBUG_FUNC_LEAVE() NOREF(cchDebugFuncEnter)
|
---|
76 | # define DEBUG_FUNC_RET(valuefmtnl) DEBUG_FUNC_LEAVE()
|
---|
77 |
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | # define CHECK_GL_ERROR()\
|
---|
81 | do {} while (0)
|
---|
82 |
|
---|
83 |
|
---|
84 | /** Custom OpenGL context class.
|
---|
85 | *
|
---|
86 | * This implementation doesn't allow to set a view to the
|
---|
87 | * context, but save the view for later use. Also it saves a copy of the
|
---|
88 | * pixel format used to create that context for later use. */
|
---|
89 | @interface VMSVGA3DOpenGLContext: NSOpenGLContext
|
---|
90 | {
|
---|
91 | @private
|
---|
92 | NSOpenGLPixelFormat *m_pPixelFormat;
|
---|
93 | NSView *m_pView;
|
---|
94 | }
|
---|
95 | - (NSOpenGLPixelFormat*)openGLPixelFormat;
|
---|
96 | @end
|
---|
97 |
|
---|
98 | @interface VMSVGA3DOverlayView: NSView
|
---|
99 | {
|
---|
100 | @private
|
---|
101 | NSView *m_pParentView;
|
---|
102 | NSWindow *m_pOverlayWin;
|
---|
103 |
|
---|
104 | NSOpenGLContext *m_pGLCtx;
|
---|
105 |
|
---|
106 | /* Position/Size tracking */
|
---|
107 | NSPoint m_Pos;
|
---|
108 | NSSize m_Size;
|
---|
109 |
|
---|
110 | /** This is necessary for clipping on the root window */
|
---|
111 | NSRect m_RootRect;
|
---|
112 | float m_yInvRootOffset;
|
---|
113 | }
|
---|
114 | - (id)initWithFrame:(NSRect)frame parentView:(NSView*)pparentView;
|
---|
115 | - (void)setGLCtx:(NSOpenGLContext*)pCtx;
|
---|
116 | - (NSOpenGLContext*)glCtx;
|
---|
117 | - (void)setOverlayWin: (NSWindow*)win;
|
---|
118 | - (NSWindow*)overlayWin;
|
---|
119 | - (void)setPos:(NSPoint)pos;
|
---|
120 | - (NSPoint)pos;
|
---|
121 | - (void)setSize:(NSSize)size;
|
---|
122 | - (NSSize) size;
|
---|
123 | - (void)updateViewportCS;
|
---|
124 | - (void)reshape;
|
---|
125 | @end
|
---|
126 |
|
---|
127 | /** Helper view.
|
---|
128 | *
|
---|
129 | * This view is added as a sub view of the parent view to track
|
---|
130 | * main window changes. Whenever the main window is changed
|
---|
131 | * (which happens on fullscreen/seamless entry/exit) the overlay
|
---|
132 | * window is informed & can add them self as a child window
|
---|
133 | * again. */
|
---|
134 | @class VMSVGA3DOverlayWindow;
|
---|
135 | @interface VMSVGA3DOverlayHelperView: NSView
|
---|
136 | {
|
---|
137 | @private
|
---|
138 | VMSVGA3DOverlayWindow *m_pOverlayWindow;
|
---|
139 | }
|
---|
140 | -(id)initWithOverlayWindow:(VMSVGA3DOverlayWindow*)pOverlayWindow;
|
---|
141 | @end
|
---|
142 |
|
---|
143 | /** Custom window class.
|
---|
144 | *
|
---|
145 | * This is the overlay window which contains our custom NSView.
|
---|
146 | * Its a direct child of the Qt Main window. It marks its background
|
---|
147 | * transparent & non opaque to make clipping possible. It also disable mouse
|
---|
148 | * events and handle frame change events of the parent view. */
|
---|
149 | @interface VMSVGA3DOverlayWindow: NSWindow
|
---|
150 | {
|
---|
151 | @private
|
---|
152 | NSView *m_pParentView;
|
---|
153 | VMSVGA3DOverlayView *m_pOverlayView;
|
---|
154 | VMSVGA3DOverlayHelperView *m_pOverlayHelperView;
|
---|
155 | NSThread *m_Thread;
|
---|
156 | }
|
---|
157 | - (id)initWithParentView:(NSView*)pParentView overlayView:(VMSVGA3DOverlayView*)pOverlayView;
|
---|
158 | - (void)parentWindowFrameChanged:(NSNotification *)note;
|
---|
159 | - (void)parentWindowChanged:(NSWindow*)pWindow;
|
---|
160 | @end
|
---|
161 |
|
---|
162 |
|
---|
163 | /********************************************************************************
|
---|
164 | *
|
---|
165 | * VMSVGA3DOpenGLContext class implementation
|
---|
166 | *
|
---|
167 | ********************************************************************************/
|
---|
168 | @implementation VMSVGA3DOpenGLContext
|
---|
169 |
|
---|
170 | -(id)initWithFormat:(NSOpenGLPixelFormat*)format shareContext:(NSOpenGLContext*)share
|
---|
171 | {
|
---|
172 | DEBUG_FUNC_ENTER();
|
---|
173 |
|
---|
174 | m_pPixelFormat = NULL;
|
---|
175 | m_pView = NULL;
|
---|
176 |
|
---|
177 | self = [super initWithFormat:format shareContext:share];
|
---|
178 | if (self)
|
---|
179 | m_pPixelFormat = format;
|
---|
180 |
|
---|
181 | DEBUG_MSG(("OCTX(%p): init VMSVGA3DOpenGLContext\n", (void*)self));
|
---|
182 | DEBUG_FUNC_RET(("%p\n", (void *)self));
|
---|
183 | return self;
|
---|
184 | }
|
---|
185 |
|
---|
186 | - (void)dealloc
|
---|
187 | {
|
---|
188 | DEBUG_FUNC_ENTER();
|
---|
189 | DEBUG_MSG(("OCTX(%p): dealloc VMSVGA3DOpenGLContext\n", (void*)self));
|
---|
190 |
|
---|
191 | [m_pPixelFormat release];
|
---|
192 |
|
---|
193 | m_pPixelFormat = NULL;
|
---|
194 | m_pView = NULL;
|
---|
195 |
|
---|
196 | [super dealloc];
|
---|
197 | DEBUG_FUNC_LEAVE();
|
---|
198 | }
|
---|
199 |
|
---|
200 | -(bool)isDoubleBuffer
|
---|
201 | {
|
---|
202 | DEBUG_FUNC_ENTER();
|
---|
203 | GLint val;
|
---|
204 | [m_pPixelFormat getValues:&val forAttribute:NSOpenGLPFADoubleBuffer forVirtualScreen:0];
|
---|
205 | DEBUG_FUNC_RET(("%d\n", val == 1 ? YES : NO));
|
---|
206 | return val == 1 ? YES : NO;
|
---|
207 | }
|
---|
208 |
|
---|
209 | -(void)setView:(NSView*)view
|
---|
210 | {
|
---|
211 | DEBUG_FUNC_ENTER();
|
---|
212 | DEBUG_MSG(("OCTX(%p): setView: new view: %p\n", (void*)self, (void*)view));
|
---|
213 |
|
---|
214 | m_pView = view;
|
---|
215 |
|
---|
216 | DEBUG_FUNC_LEAVE();
|
---|
217 | }
|
---|
218 |
|
---|
219 | -(NSView*)view
|
---|
220 | {
|
---|
221 | DEBUG_FUNC_ENTER();
|
---|
222 | DEBUG_FUNC_RET(("%p\n", (void *)m_pView));
|
---|
223 | return m_pView;
|
---|
224 | }
|
---|
225 |
|
---|
226 | -(void)clearDrawable
|
---|
227 | {
|
---|
228 | DEBUG_FUNC_ENTER();
|
---|
229 | DEBUG_MSG(("OCTX(%p): clearDrawable\n", (void*)self));
|
---|
230 |
|
---|
231 | m_pView = NULL;
|
---|
232 | [super clearDrawable];
|
---|
233 |
|
---|
234 | DEBUG_FUNC_LEAVE();
|
---|
235 | }
|
---|
236 |
|
---|
237 | -(NSOpenGLPixelFormat*)openGLPixelFormat
|
---|
238 | {
|
---|
239 | DEBUG_FUNC_ENTER();
|
---|
240 | DEBUG_FUNC_RET(("%p\n", (void *)m_pPixelFormat));
|
---|
241 | return m_pPixelFormat;
|
---|
242 | }
|
---|
243 |
|
---|
244 | @end
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | /********************************************************************************
|
---|
249 | *
|
---|
250 | * VMSVGA3DOverlayHelperView class implementation
|
---|
251 | *
|
---|
252 | ********************************************************************************/
|
---|
253 | @implementation VMSVGA3DOverlayHelperView
|
---|
254 |
|
---|
255 | -(id)initWithOverlayWindow:(VMSVGA3DOverlayWindow*)pOverlayWindow
|
---|
256 | {
|
---|
257 | DEBUG_FUNC_ENTER();
|
---|
258 |
|
---|
259 | self = [super initWithFrame:NSZeroRect];
|
---|
260 |
|
---|
261 | m_pOverlayWindow = pOverlayWindow;
|
---|
262 |
|
---|
263 | DEBUG_MSG(("OHVW(%p): init OverlayHelperView\n", (void*)self));
|
---|
264 | DEBUG_FUNC_RET(("%p\n", (void *)self));
|
---|
265 | return self;
|
---|
266 | }
|
---|
267 |
|
---|
268 | -(void)viewDidMoveToWindow
|
---|
269 | {
|
---|
270 | DEBUG_FUNC_ENTER();
|
---|
271 | DEBUG_MSG(("OHVW(%p): viewDidMoveToWindow: new win: %p\n", (void*)self, (void*)[self window]));
|
---|
272 |
|
---|
273 | [m_pOverlayWindow parentWindowChanged:[self window]];
|
---|
274 |
|
---|
275 | DEBUG_FUNC_LEAVE();
|
---|
276 | }
|
---|
277 |
|
---|
278 | @end
|
---|
279 |
|
---|
280 | /********************************************************************************
|
---|
281 | *
|
---|
282 | * VMSVGA3DOverlayWindow class implementation
|
---|
283 | *
|
---|
284 | ********************************************************************************/
|
---|
285 | @implementation VMSVGA3DOverlayWindow
|
---|
286 |
|
---|
287 | - (id)initWithParentView:(NSView*)pParentView overlayView:(VMSVGA3DOverlayView*)pOverlayView
|
---|
288 | {
|
---|
289 | DEBUG_FUNC_ENTER();
|
---|
290 | NSWindow *pParentWin = nil;
|
---|
291 |
|
---|
292 | if((self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]))
|
---|
293 | {
|
---|
294 | m_pParentView = pParentView;
|
---|
295 | m_pOverlayView = pOverlayView;
|
---|
296 | m_Thread = [NSThread currentThread];
|
---|
297 |
|
---|
298 | [m_pOverlayView setOverlayWin: self];
|
---|
299 |
|
---|
300 | m_pOverlayHelperView = [[VMSVGA3DOverlayHelperView alloc] initWithOverlayWindow:self];
|
---|
301 | /* Add the helper view as a child of the parent view to get notifications */
|
---|
302 | [pParentView addSubview:m_pOverlayHelperView];
|
---|
303 |
|
---|
304 | /* Make sure this window is transparent */
|
---|
305 | #ifdef SHOW_WINDOW_BACKGROUND
|
---|
306 | /* For debugging */
|
---|
307 | [self setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7]];
|
---|
308 | #else
|
---|
309 | [self setBackgroundColor:[NSColor clearColor]];
|
---|
310 | #endif
|
---|
311 | [self setOpaque:NO];
|
---|
312 | [self setAlphaValue:.999];
|
---|
313 | /* Disable mouse events for this window */
|
---|
314 | [self setIgnoresMouseEvents:YES];
|
---|
315 |
|
---|
316 | pParentWin = [m_pParentView window];
|
---|
317 |
|
---|
318 | /* Initial set the position to the parents view top/left (Compiz fix). */
|
---|
319 | [self setFrameOrigin:
|
---|
320 | [pParentWin convertBaseToScreen:
|
---|
321 | [m_pParentView convertPoint:NSZeroPoint toView:nil]]];
|
---|
322 |
|
---|
323 | /* Set the overlay view as our content view */
|
---|
324 | [self setContentView:m_pOverlayView];
|
---|
325 |
|
---|
326 | /* Add ourself as a child to the parent views window. Note: this has to
|
---|
327 | * be done last so that everything else is setup in
|
---|
328 | * parentWindowChanged. */
|
---|
329 | [pParentWin addChildWindow:self ordered:NSWindowAbove];
|
---|
330 | }
|
---|
331 | DEBUG_MSG(("OWIN(%p): init OverlayWindow\n", (void*)self));
|
---|
332 | DEBUG_FUNC_RET(("%p\n", (void *)self));
|
---|
333 | return self;
|
---|
334 | }
|
---|
335 |
|
---|
336 | - (void)dealloc
|
---|
337 | {
|
---|
338 | DEBUG_FUNC_ENTER();
|
---|
339 | DEBUG_MSG(("OWIN(%p): dealloc OverlayWindow\n", (void*)self));
|
---|
340 |
|
---|
341 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
---|
342 |
|
---|
343 | [m_pOverlayHelperView removeFromSuperview];
|
---|
344 | [m_pOverlayHelperView release];
|
---|
345 |
|
---|
346 | [super dealloc];
|
---|
347 | DEBUG_FUNC_LEAVE();
|
---|
348 | }
|
---|
349 |
|
---|
350 | - (void)parentWindowFrameChanged:(NSNotification*)pNote
|
---|
351 | {
|
---|
352 | DEBUG_FUNC_ENTER();
|
---|
353 | DEBUG_MSG(("OWIN(%p): parentWindowFrameChanged\n", (void*)self));
|
---|
354 |
|
---|
355 | [m_pOverlayView reshape];
|
---|
356 |
|
---|
357 | DEBUG_FUNC_LEAVE();
|
---|
358 | }
|
---|
359 |
|
---|
360 | - (void)parentWindowChanged:(NSWindow*)pWindow
|
---|
361 | {
|
---|
362 | DEBUG_FUNC_ENTER();
|
---|
363 | DEBUG_MSG(("OWIN(%p): parentWindowChanged\n", (void*)self));
|
---|
364 |
|
---|
365 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
---|
366 |
|
---|
367 | if(pWindow != nil)
|
---|
368 | {
|
---|
369 | /* Ask to get notifications when our parent window frame changes. */
|
---|
370 | [[NSNotificationCenter defaultCenter]
|
---|
371 | addObserver:self
|
---|
372 | selector:@selector(parentWindowFrameChanged:)
|
---|
373 | name:NSWindowDidResizeNotification
|
---|
374 | object:pWindow];
|
---|
375 | /* Add us self as child window */
|
---|
376 | [pWindow addChildWindow:self ordered:NSWindowAbove];
|
---|
377 | [m_pOverlayView reshape];
|
---|
378 | }
|
---|
379 |
|
---|
380 | DEBUG_FUNC_LEAVE();
|
---|
381 | }
|
---|
382 |
|
---|
383 | @end
|
---|
384 |
|
---|
385 | /********************************************************************************
|
---|
386 | *
|
---|
387 | * VMSVGA3DOverlayView class implementation
|
---|
388 | *
|
---|
389 | ********************************************************************************/
|
---|
390 | @implementation VMSVGA3DOverlayView
|
---|
391 |
|
---|
392 | - (id)initWithFrame:(NSRect) frame parentView:(NSView*)pParentView
|
---|
393 | {
|
---|
394 | DEBUG_FUNC_ENTER();
|
---|
395 |
|
---|
396 | m_pParentView = pParentView;
|
---|
397 | /* Make some reasonable defaults */
|
---|
398 | m_pGLCtx = nil;
|
---|
399 | m_Pos = NSZeroPoint;
|
---|
400 | m_Size = NSMakeSize(1, 1);
|
---|
401 | m_RootRect = NSMakeRect(0, 0, m_Size.width, m_Size.height);
|
---|
402 | m_yInvRootOffset = 0;
|
---|
403 |
|
---|
404 | self = [super initWithFrame: frame];
|
---|
405 |
|
---|
406 | DEBUG_MSG(("OVIW(%p): init VMSVGA3DOverlayView\n", (void*)self));
|
---|
407 | DEBUG_FUNC_RET(("%p\n", (void *)self));
|
---|
408 | return self;
|
---|
409 | }
|
---|
410 |
|
---|
411 | - (void)cleanupData
|
---|
412 | {
|
---|
413 | DEBUG_FUNC_ENTER();
|
---|
414 |
|
---|
415 | /*[self deleteDockTile];*/
|
---|
416 |
|
---|
417 | [self setGLCtx:nil];
|
---|
418 |
|
---|
419 | #if 0
|
---|
420 | if (m_pSharedGLCtx)
|
---|
421 | {
|
---|
422 | if ([m_pSharedGLCtx view] == self)
|
---|
423 | [m_pSharedGLCtx clearDrawable];
|
---|
424 |
|
---|
425 | [m_pSharedGLCtx release];
|
---|
426 |
|
---|
427 | m_pSharedGLCtx = nil;
|
---|
428 |
|
---|
429 | CrBltTerm(m_pBlitter);
|
---|
430 |
|
---|
431 | RTMemFree(m_pBlitter);
|
---|
432 |
|
---|
433 | m_pBlitter = nil;
|
---|
434 | }
|
---|
435 | #endif
|
---|
436 |
|
---|
437 | /*[self clearVisibleRegions];*/
|
---|
438 |
|
---|
439 | DEBUG_FUNC_LEAVE();
|
---|
440 | }
|
---|
441 |
|
---|
442 | - (void)dealloc
|
---|
443 | {
|
---|
444 | DEBUG_FUNC_ENTER();
|
---|
445 | DEBUG_MSG(("OVIW(%p): dealloc OverlayView\n", (void*)self));
|
---|
446 |
|
---|
447 | [self cleanupData];
|
---|
448 |
|
---|
449 | [super dealloc];
|
---|
450 |
|
---|
451 | DEBUG_FUNC_LEAVE();
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | - (void)setGLCtx:(NSOpenGLContext*)pCtx
|
---|
456 | {
|
---|
457 | DEBUG_FUNC_ENTER();
|
---|
458 |
|
---|
459 | DEBUG_MSG(("OVIW(%p): setGLCtx: new ctx: %p (old: %p)\n", (void*)self, (void*)pCtx, (void *)m_pGLCtx));
|
---|
460 | if (m_pGLCtx == pCtx)
|
---|
461 | {
|
---|
462 | DEBUG_FUNC_LEAVE();
|
---|
463 | return;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /* ensure the context drawable is cleared to avoid holding a reference to inexistent view */
|
---|
467 | if (m_pGLCtx)
|
---|
468 | {
|
---|
469 | [m_pGLCtx clearDrawable];
|
---|
470 | [m_pGLCtx release];
|
---|
471 | /*[m_pGLCtx performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];*/
|
---|
472 | }
|
---|
473 | m_pGLCtx = pCtx;
|
---|
474 | if (pCtx)
|
---|
475 | [pCtx retain];
|
---|
476 |
|
---|
477 | DEBUG_FUNC_LEAVE();
|
---|
478 | }
|
---|
479 |
|
---|
480 | - (NSOpenGLContext*)glCtx
|
---|
481 | {
|
---|
482 | DEBUG_FUNC_ENTER();
|
---|
483 | DEBUG_FUNC_RET(("%p\n", (void *)m_pGLCtx));
|
---|
484 | return m_pGLCtx;
|
---|
485 | }
|
---|
486 |
|
---|
487 | - (void)setOverlayWin:(NSWindow*)pWin
|
---|
488 | {
|
---|
489 | DEBUG_FUNC_ENTER();
|
---|
490 | DEBUG_MSG(("OVIW(%p): setOverlayWin: new win: %p\n", (void*)self, (void*)pWin));
|
---|
491 | m_pOverlayWin = pWin;
|
---|
492 | DEBUG_FUNC_LEAVE();
|
---|
493 | }
|
---|
494 |
|
---|
495 | - (NSWindow*)overlayWin
|
---|
496 | {
|
---|
497 | DEBUG_FUNC_ENTER();
|
---|
498 | DEBUG_FUNC_RET(("%p\n", (void *)m_pOverlayWin));
|
---|
499 | return m_pOverlayWin;
|
---|
500 | }
|
---|
501 |
|
---|
502 | - (void)setPos:(NSPoint)pos
|
---|
503 | {
|
---|
504 | DEBUG_FUNC_ENTER();
|
---|
505 |
|
---|
506 | m_Pos = pos;
|
---|
507 | [self reshape];
|
---|
508 | DEBUG_FUNC_LEAVE();
|
---|
509 | }
|
---|
510 |
|
---|
511 | - (NSPoint)pos
|
---|
512 | {
|
---|
513 | DEBUG_FUNC_ENTER();
|
---|
514 | DEBUG_FUNC_RET(("%f,%f\n", m_Pos.x, m_Pos.y));
|
---|
515 | return m_Pos;
|
---|
516 | }
|
---|
517 |
|
---|
518 | - (void)setSize:(NSSize)size
|
---|
519 | {
|
---|
520 | DEBUG_FUNC_ENTER();
|
---|
521 | m_Size = size;
|
---|
522 | [self reshape];
|
---|
523 | DEBUG_FUNC_LEAVE();
|
---|
524 | }
|
---|
525 |
|
---|
526 | - (NSSize)size
|
---|
527 | {
|
---|
528 | DEBUG_FUNC_ENTER();
|
---|
529 | DEBUG_FUNC_RET(("%f,%f\n", m_Size.width, m_Size.height));
|
---|
530 | return m_Size;
|
---|
531 | }
|
---|
532 |
|
---|
533 | - (void)updateViewportCS
|
---|
534 | {
|
---|
535 | DEBUG_FUNC_ENTER();
|
---|
536 | DEBUG_MSG(("OVIW(%p): updateViewport\n", (void*)self));
|
---|
537 |
|
---|
538 | /* Update the viewport for our OpenGL view */
|
---|
539 | /* [m_pSharedGLCtx update]; */
|
---|
540 |
|
---|
541 | /* Clear background to transparent */
|
---|
542 | /* glClearColor(0.0f, 0.0f, 0.0f, 0.0f);*/
|
---|
543 | DEBUG_FUNC_LEAVE();
|
---|
544 | }
|
---|
545 |
|
---|
546 | - (void)reshape
|
---|
547 | {
|
---|
548 | DEBUG_FUNC_ENTER();
|
---|
549 | NSRect parentFrame = NSZeroRect;
|
---|
550 | NSPoint parentPos = NSZeroPoint;
|
---|
551 | NSPoint childPos = NSZeroPoint;
|
---|
552 | NSRect childFrame = NSZeroRect;
|
---|
553 | NSRect newFrame = NSZeroRect;
|
---|
554 |
|
---|
555 | DEBUG_MSG(("OVIW(%p): reshape\n", (void*)self));
|
---|
556 |
|
---|
557 | /* Getting the right screen coordinates of the parents frame is a little bit
|
---|
558 | * complicated. */
|
---|
559 | parentFrame = [m_pParentView frame];
|
---|
560 | DEBUG_MSG(("FIXED parentFrame [%f:%f], [%f:%f]\n", parentFrame.origin.x, parentFrame.origin.y, parentFrame.size.width, parentFrame.size.height));
|
---|
561 | parentPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:NSMakePoint(parentFrame.origin.x, parentFrame.origin.y + parentFrame.size.height)]];
|
---|
562 | parentFrame.origin.x = parentPos.x;
|
---|
563 | parentFrame.origin.y = parentPos.y;
|
---|
564 |
|
---|
565 | /* Calculate the new screen coordinates of the overlay window. */
|
---|
566 | childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
|
---|
567 | childPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:childPos]];
|
---|
568 | DEBUG_MSG(("FIXED childPos(screen) [%f:%f]\n", childPos.x, childPos.y));
|
---|
569 |
|
---|
570 | /* Make a frame out of it. */
|
---|
571 | childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
|
---|
572 | DEBUG_MSG(("FIXED childFrame [%f:%f], [%f:%f]\n", childFrame.origin.x, childFrame.origin.y, childFrame.size.width, childFrame.size.height));
|
---|
573 |
|
---|
574 | /* We have to make sure that the overlay window will not be displayed out
|
---|
575 | * of the parent window. So intersect both frames & use the result as the new
|
---|
576 | * frame for the window. */
|
---|
577 | newFrame = NSIntersectionRect(parentFrame, childFrame);
|
---|
578 |
|
---|
579 | DEBUG_MSG(("[%p]: parentFrame pos[%f : %f] size[%f : %f]\n",
|
---|
580 | (void*)self,
|
---|
581 | parentFrame.origin.x, parentFrame.origin.y,
|
---|
582 | parentFrame.size.width, parentFrame.size.height));
|
---|
583 | DEBUG_MSG(("[%p]: childFrame pos[%f : %f] size[%f : %f]\n",
|
---|
584 | (void*)self,
|
---|
585 | childFrame.origin.x, childFrame.origin.y,
|
---|
586 | childFrame.size.width, childFrame.size.height));
|
---|
587 |
|
---|
588 | DEBUG_MSG(("[%p]: newFrame pos[%f : %f] size[%f : %f]\n",
|
---|
589 | (void*)self,
|
---|
590 | newFrame.origin.x, newFrame.origin.y,
|
---|
591 | newFrame.size.width, newFrame.size.height));
|
---|
592 |
|
---|
593 | /* Later we have to correct the texture position in the case the window is
|
---|
594 | * out of the parents window frame. So save the shift values for later use. */
|
---|
595 | m_RootRect.origin.x = newFrame.origin.x - childFrame.origin.x;
|
---|
596 | m_RootRect.origin.y = childFrame.size.height + childFrame.origin.y - (newFrame.size.height + newFrame.origin.y);
|
---|
597 | m_RootRect.size = newFrame.size;
|
---|
598 | m_yInvRootOffset = newFrame.origin.y - childFrame.origin.y;
|
---|
599 |
|
---|
600 | DEBUG_MSG(("[%p]: m_RootRect pos[%f : %f] size[%f : %f]\n",
|
---|
601 | (void*)self,
|
---|
602 | m_RootRect.origin.x, m_RootRect.origin.y,
|
---|
603 | m_RootRect.size.width, m_RootRect.size.height));
|
---|
604 |
|
---|
605 | /* Set the new frame. */
|
---|
606 | [[self window] setFrame:newFrame display:YES];
|
---|
607 |
|
---|
608 | #if 0
|
---|
609 | /* Make sure the context is updated according */
|
---|
610 | /* [self updateViewport]; */
|
---|
611 | if (m_pSharedGLCtx)
|
---|
612 | {
|
---|
613 | VBOX_CR_RENDER_CTX_INFO CtxInfo;
|
---|
614 | vboxCtxEnter(m_pSharedGLCtx, &CtxInfo);
|
---|
615 |
|
---|
616 | [self updateViewportCS];
|
---|
617 |
|
---|
618 | vboxCtxLeave(&CtxInfo);
|
---|
619 | }
|
---|
620 | #endif
|
---|
621 | DEBUG_FUNC_LEAVE();
|
---|
622 | }
|
---|
623 |
|
---|
624 | @end
|
---|
625 |
|
---|
626 |
|
---|
627 | void vmsvga3dCocoaCreateContext(NativeNSOpenGLContextRef *ppCtx, NativeNSOpenGLContextRef pShareCtx, bool fOtherProfile)
|
---|
628 | {
|
---|
629 | DEBUG_FUNC_ENTER();
|
---|
630 | NSOpenGLPixelFormat *pFmt = nil;
|
---|
631 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
632 |
|
---|
633 | // Consider to remove it and check if it's harmless.
|
---|
634 | NSOpenGLPixelFormatAttribute attribs[] =
|
---|
635 | {
|
---|
636 | NSOpenGLPFAOpenGLProfile, (NSOpenGLPixelFormatAttribute)0,
|
---|
637 | //NSOpenGLPFAWindow, - obsolete/deprecated, try work without it...
|
---|
638 | NSOpenGLPFAAccelerated,
|
---|
639 | NSOpenGLPFADoubleBuffer,
|
---|
640 | NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
|
---|
641 | NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8,
|
---|
642 | NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
|
---|
643 | 0
|
---|
644 | };
|
---|
645 | attribs[1] = fOtherProfile ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy;
|
---|
646 |
|
---|
647 | /* Choose a pixel format */
|
---|
648 | pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
---|
649 | if (pFmt)
|
---|
650 | {
|
---|
651 | *ppCtx = [[VMSVGA3DOpenGLContext alloc] initWithFormat:pFmt shareContext:pShareCtx];
|
---|
652 | DEBUG_MSG(("New context %p\n", (void *)*ppCtx));
|
---|
653 | }
|
---|
654 | else
|
---|
655 | {
|
---|
656 | AssertFailed();
|
---|
657 | *ppCtx = NULL;
|
---|
658 | }
|
---|
659 |
|
---|
660 | [pPool release];
|
---|
661 |
|
---|
662 | DEBUG_FUNC_LEAVE();
|
---|
663 | }
|
---|
664 |
|
---|
665 | void vmsvga3dCocoaDestroyContext(NativeNSOpenGLContextRef pCtx)
|
---|
666 | {
|
---|
667 | DEBUG_FUNC_ENTER();
|
---|
668 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
669 | [pCtx release];
|
---|
670 | [pPool release];
|
---|
671 | DEBUG_FUNC_LEAVE();
|
---|
672 | }
|
---|
673 |
|
---|
674 | void vmsvga3dCocoaCreateView(NativeNSViewRef *ppView, NativeNSViewRef pParentView)
|
---|
675 | {
|
---|
676 | DEBUG_FUNC_ENTER();
|
---|
677 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
678 |
|
---|
679 | /* Create our worker view */
|
---|
680 | VMSVGA3DOverlayView* pView = [[VMSVGA3DOverlayView alloc] initWithFrame:NSZeroRect parentView:pParentView];
|
---|
681 |
|
---|
682 | if (pView)
|
---|
683 | {
|
---|
684 | /* We need a real window as container for the view */
|
---|
685 | [[VMSVGA3DOverlayWindow alloc] initWithParentView:pParentView overlayView:pView];
|
---|
686 | /* Return the freshly created overlay view */
|
---|
687 | *ppView = pView;
|
---|
688 | }
|
---|
689 |
|
---|
690 | [pPool release];
|
---|
691 | DEBUG_FUNC_LEAVE();
|
---|
692 | }
|
---|
693 |
|
---|
694 | void vmsvga3dCocoaDestroyView(NativeNSViewRef pView)
|
---|
695 | {
|
---|
696 | DEBUG_FUNC_ENTER();
|
---|
697 | NSWindow *pWin = nil;
|
---|
698 |
|
---|
699 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
700 | [pView setHidden: YES];
|
---|
701 |
|
---|
702 | [pWin release];
|
---|
703 | [pView release];
|
---|
704 |
|
---|
705 | [pPool release];
|
---|
706 | DEBUG_FUNC_LEAVE();
|
---|
707 | }
|
---|
708 |
|
---|
709 | void vmsvga3dCocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
|
---|
710 | {
|
---|
711 | DEBUG_FUNC_ENTER();
|
---|
712 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
713 |
|
---|
714 | [(VMSVGA3DOverlayView*)pView setPos:NSMakePoint(x, y)];
|
---|
715 |
|
---|
716 | [pPool release];
|
---|
717 | }
|
---|
718 |
|
---|
719 | void vmsvga3dCocoaViewSetSize(NativeNSViewRef pView, int w, int h)
|
---|
720 | {
|
---|
721 | DEBUG_FUNC_ENTER();
|
---|
722 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
723 |
|
---|
724 | [(VMSVGA3DOverlayView*)pView setSize:NSMakeSize(w, h)];
|
---|
725 |
|
---|
726 | [pPool release];
|
---|
727 | DEBUG_FUNC_LEAVE();
|
---|
728 | }
|
---|
729 |
|
---|
730 | void vmsvga3dCocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
|
---|
731 | {
|
---|
732 | DEBUG_FUNC_ENTER();
|
---|
733 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
734 |
|
---|
735 | DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", (void*)pView, (void*)pCtx));
|
---|
736 |
|
---|
737 | /* Always flush before flush. glXMakeCurrent and wglMakeCurrent does this
|
---|
738 | implicitly, seemingly NSOpenGLContext::makeCurrentContext doesn't. */
|
---|
739 | if ([NSOpenGLContext currentContext] != 0)
|
---|
740 | glFlush();
|
---|
741 |
|
---|
742 | if (pView)
|
---|
743 | {
|
---|
744 | [(VMSVGA3DOverlayView*)pView setGLCtx:pCtx];
|
---|
745 | /* [(VMSVGA3DOverlayView*)pView makeCurrentFBO];*/
|
---|
746 | [pCtx makeCurrentContext];
|
---|
747 | }
|
---|
748 | else
|
---|
749 | {
|
---|
750 | [NSOpenGLContext clearCurrentContext];
|
---|
751 | }
|
---|
752 |
|
---|
753 | [pPool release];
|
---|
754 | DEBUG_FUNC_LEAVE();
|
---|
755 | }
|
---|
756 |
|
---|
757 | void vmsvga3dCocoaSwapBuffers(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
|
---|
758 | {
|
---|
759 | DEBUG_FUNC_ENTER();
|
---|
760 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
761 |
|
---|
762 | [pCtx flushBuffer];
|
---|
763 |
|
---|
764 | [pPool release];
|
---|
765 | DEBUG_FUNC_LEAVE();
|
---|
766 | }
|
---|
767 |
|
---|