VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m@ 52494

Last change on this file since 52494 was 52494, checked in by vboxsync, 11 years ago

crOpenGL/osx: more sync with main thread

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.4 KB
Line 
1/* $Id: renderspu_cocoa_helper.m 52494 2014-08-25 13:23:57Z vboxsync $ */
2/** @file
3 * VirtualBox OpenGL Cocoa Window System Helper Implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "renderspu_cocoa_helper.h"
19
20#import <Cocoa/Cocoa.h>
21#undef PVM
22
23#include "chromium.h" /* For the visual bits of chromium */
24
25#include <iprt/thread.h>
26#include <iprt/string.h>
27#include <iprt/mem.h>
28#include <iprt/time.h>
29#include <iprt/assert.h>
30
31#include <cr_vreg.h>
32#include <cr_error.h>
33#include <cr_blitter.h>
34#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
35# include <cr_pixeldata.h>
36#endif
37
38
39#include "renderspu.h"
40
41/** @page pg_opengl_cocoa OpenGL - Cocoa Window System Helper
42 *
43 * How this works:
44 * In general it is not so easy like on the other platforms, cause Cocoa
45 * doesn't support any clipping of already painted stuff. In Mac OS X there is
46 * the concept of translucent canvas's e.g. windows and there it is just
47 * painted what should be visible to the user. Unfortunately this isn't the
48 * concept of chromium. Therefor I reroute all OpenGL operation from the guest
49 * to a frame buffer object (FBO). This is a OpenGL extension, which is
50 * supported by all OS X versions we support (AFAIC tell). Of course the guest
51 * doesn't know that and we have to make sure that the OpenGL state always is
52 * in the right state to paint into the FBO and not to the front/back buffer.
53 * Several functions below (like cocoaBindFramebufferEXT, cocoaGetIntegerv,
54 * ...) doing this. When a swap or finish is triggered by the guest, the
55 * content (which is already bound to an texture) is painted on the screen
56 * within a separate OpenGL context. This allows the usage of the same
57 * resources (texture ids, buffers ...) but at the same time having an
58 * different internal OpenGL state. Another advantage is that we can paint a
59 * thumbnail of the current output in a much more smaller (GPU accelerated
60 * scale) version on a third context and use glReadPixels to get the actual
61 * data. glReadPixels is a very slow operation, but as we just use a much more
62 * smaller image, we can handle it (anyway this is only done 5 times per
63 * second).
64 *
65 * Other things to know:
66 * - If the guest request double buffering, we have to make sure there are two
67 * buffers. We use the same FBO with 2 color attachments. Also glDrawBuffer
68 * and glReadBuffer is intercepted to make sure it is painted/read to/from
69 * the correct buffers. On swap our buffers are swapped and not the
70 * front/back buffer.
71 * - If the guest request a depth/stencil buffer, a combined render buffer for
72 * this is created.
73 * - If the size of the guest OpenGL window changes, all FBO's, textures, ...
74 * need to be recreated.
75 * - We need to track any changes to the parent window
76 * (create/destroy/move/resize). The various classes like OverlayHelperView,
77 * OverlayWindow, ... are there for.
78 * - The HGCM service runs on a other thread than the Main GUI. Keeps this
79 * always in mind (see e.g. performSelectorOnMainThread in renderFBOToView)
80 * - We make heavy use of late binding. We can not be sure that the GUI (or any
81 * other third party GUI), overwrite our NSOpenGLContext. So we always ask if
82 * this is our own one, before use. Really neat concept of Objective-C/Cocoa
83 * ;)
84 */
85
86/* Debug macros */
87#define FBO 1 /* Disable this to see how the output is without the FBO in the middle of the processing chain. */
88#if 0
89# define CR_RENDER_FORCE_PRESENT_MAIN_THREAD /* force present schedule to main thread */
90# define SHOW_WINDOW_BACKGROUND 1 /* Define this to see the window background even if the window is clipped */
91# define DEBUG_VERBOSE /* Define this to get some debug info about the messages flow. */
92#endif
93
94#ifdef DEBUG_misha
95# define DEBUG_MSG(text) \
96 printf text
97# define DEBUG_WARN(text) do { \
98 crWarning text ; \
99 Assert(0); \
100 } while (0)
101#else
102# define DEBUG_MSG(text) \
103 do {} while (0)
104# define DEBUG_WARN(text) do { \
105 crWarning text ; \
106 } while (0)
107#endif
108
109#ifdef DEBUG_VERBOSE
110# define DEBUG_MSG_1(text) \
111 DEBUG_MSG(text)
112#else
113# define DEBUG_MSG_1(text) \
114 do {} while (0)
115#endif
116
117#define DEBUG_FUNC_ENTER() do { \
118 DEBUG_MSG(("==>%s\n", __PRETTY_FUNCTION__)); \
119 } while (0)
120
121#define DEBUG_FUNC_LEAVE() do { \
122 DEBUG_MSG(("<==%s\n", __PRETTY_FUNCTION__)); \
123 } while (0)
124
125
126#ifdef DEBUG_poetzsch
127# define CHECK_GL_ERROR()\
128 do \
129 { \
130 checkGLError(__FILE__, __LINE__); \
131 }while (0);
132
133 static void checkGLError(char *file, int line)
134 {
135 GLenum g = glGetError();
136 if (g != GL_NO_ERROR)
137 {
138 char *errStr;
139 switch (g)
140 {
141 case GL_INVALID_ENUM: errStr = RTStrDup("GL_INVALID_ENUM"); break;
142 case GL_INVALID_VALUE: errStr = RTStrDup("GL_INVALID_VALUE"); break;
143 case GL_INVALID_OPERATION: errStr = RTStrDup("GL_INVALID_OPERATION"); break;
144 case GL_STACK_OVERFLOW: errStr = RTStrDup("GL_STACK_OVERFLOW"); break;
145 case GL_STACK_UNDERFLOW: errStr = RTStrDup("GL_STACK_UNDERFLOW"); break;
146 case GL_OUT_OF_MEMORY: errStr = RTStrDup("GL_OUT_OF_MEMORY"); break;
147 case GL_TABLE_TOO_LARGE: errStr = RTStrDup("GL_TABLE_TOO_LARGE"); break;
148 default: errStr = RTStrDup("UNKNOWN"); break;
149 }
150 DEBUG_MSG(("%s:%d: glError %d (%s)\n", file, line, g, errStr));
151 RTMemFree(errStr);
152 }
153 }
154#else
155# define CHECK_GL_ERROR()\
156 do {} while (0)
157#endif
158
159#define GL_SAVE_STATE \
160 do \
161 { \
162 glPushAttrib(GL_ALL_ATTRIB_BITS); \
163 glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); \
164 glMatrixMode(GL_PROJECTION); \
165 glPushMatrix(); \
166 glMatrixMode(GL_TEXTURE); \
167 glPushMatrix(); \
168 glMatrixMode(GL_COLOR); \
169 glPushMatrix(); \
170 glMatrixMode(GL_MODELVIEW); \
171 glPushMatrix(); \
172 } \
173 while(0);
174
175#define GL_RESTORE_STATE \
176 do \
177 { \
178 glMatrixMode(GL_MODELVIEW); \
179 glPopMatrix(); \
180 glMatrixMode(GL_COLOR); \
181 glPopMatrix(); \
182 glMatrixMode(GL_TEXTURE); \
183 glPopMatrix(); \
184 glMatrixMode(GL_PROJECTION); \
185 glPopMatrix(); \
186 glPopClientAttrib(); \
187 glPopAttrib(); \
188 } \
189 while(0);
190
191static NSOpenGLContext * vboxCtxGetCurrent()
192{
193 GET_CONTEXT(pCtxInfo);
194 if (pCtxInfo)
195 {
196 Assert(pCtxInfo->context);
197 return pCtxInfo->context;
198 }
199
200 return nil;
201}
202
203static bool vboxCtxSyncCurrentInfo()
204{
205 GET_CONTEXT(pCtxInfo);
206 NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
207 NSView *pView = pCtx ? [pCtx view] : nil;
208 bool fAdjusted = false;
209 if (pCtxInfo)
210 {
211 WindowInfo *pWinInfo = pCtxInfo->currentWindow;
212 Assert(pWinInfo);
213 if (pCtxInfo->context != pCtx
214 || pWinInfo->window != pView)
215 {
216 renderspu_SystemMakeCurrent(pWinInfo, 0, pCtxInfo);
217 fAdjusted = true;
218 }
219 }
220 else
221 {
222 if (pCtx)
223 {
224 [NSOpenGLContext clearCurrentContext];
225 fAdjusted = true;
226 }
227 }
228
229 return fAdjusted;
230}
231
232typedef struct VBOX_CR_RENDER_CTX_INFO
233{
234 bool fIsValid;
235 NSOpenGLContext *pCtx;
236 NSView *pView;
237} VBOX_CR_RENDER_CTX_INFO, *PVBOX_CR_RENDER_CTX_INFO;
238
239static void vboxCtxEnter(NSOpenGLContext*pCtx, PVBOX_CR_RENDER_CTX_INFO pCtxInfo)
240{
241 NSOpenGLContext *pOldCtx = vboxCtxGetCurrent();
242 NSView *pOldView = (pOldCtx ? [pOldCtx view] : nil);
243 NSView *pView = [pCtx view];
244 bool fNeedCtxSwitch = (pOldCtx != pCtx || pOldView != pView);
245 Assert(pCtx);
246 // Assert(pOldCtx == m_pGLCtx);
247 // Assert(pOldView == self);
248 // Assert(fNeedCtxSwitch);
249 if (fNeedCtxSwitch)
250 {
251 if(pOldCtx != nil)
252 glFlush();
253
254 [pCtx makeCurrentContext];
255
256 pCtxInfo->fIsValid = true;
257 pCtxInfo->pCtx = pOldCtx;
258 pCtxInfo->pView = pView;
259 }
260 else
261 {
262 pCtxInfo->fIsValid = false;
263 }
264}
265
266static void vboxCtxLeave(PVBOX_CR_RENDER_CTX_INFO pCtxInfo)
267{
268 if (pCtxInfo->fIsValid)
269 {
270 NSOpenGLContext *pOldCtx = pCtxInfo->pCtx;
271 NSView *pOldView = pCtxInfo->pView;
272
273 glFlush();
274 if (pOldCtx != nil)
275 {
276 if ([pOldCtx view] != pOldView)
277 {
278 [pOldCtx setView: pOldView];
279 }
280
281 [pOldCtx makeCurrentContext];
282
283#ifdef DEBUG
284 {
285 NSOpenGLContext *pTstOldCtx = [NSOpenGLContext currentContext];
286 NSView *pTstOldView = (pTstOldCtx ? [pTstOldCtx view] : nil);
287 Assert(pTstOldCtx == pOldCtx);
288 Assert(pTstOldView == pOldView);
289 }
290#endif
291 }
292 else
293 {
294 [NSOpenGLContext clearCurrentContext];
295 }
296 }
297}
298
299/** Custom OpenGL context class.
300 *
301 * This implementation doesn't allow to set a view to the
302 * context, but save the view for later use. Also it saves a copy of the
303 * pixel format used to create that context for later use. */
304@interface OverlayOpenGLContext: NSOpenGLContext
305{
306@private
307 NSOpenGLPixelFormat *m_pPixelFormat;
308 NSView *m_pView;
309}
310- (NSOpenGLPixelFormat*)openGLPixelFormat;
311@end
312
313@interface VBoxTask : NSObject
314{
315}
316- (void)run;
317@end
318
319@interface VBoxTaskPerformSelector : VBoxTask
320{
321@private
322 id m_Object;
323 SEL m_Selector;
324 id m_Arg;
325}
326- (id)initWithObject:(id)aObject selector:(SEL)aSelector arg:(id)aArg;
327- (void)run;
328- (void)dealloc;
329@end
330
331#if 0
332typedef DECLCALLBACKPTR(void, PFNVBOXTASKCALLBACK)(void *pvCb);
333
334@interface VBoxTaskCallback: VBoxTask
335{
336@private
337 PFNVBOXTASKCALLBACK m_pfnCb;
338 void *m_pvCb;
339}
340- (id)initWithCb:(PFNVBOXTASKCALLBACK)pfnCb arg:(void*)pvCb;
341- (void)run;
342@end
343#endif
344
345@interface VBoxTaskComposite: VBoxTask
346{
347@private
348 NSUInteger m_CurIndex;
349 RTCRITSECT m_Lock;
350 NSMutableArray *m_pArray;
351}
352- (id)init;
353- (void)add:(VBoxTask*)pTask;
354- (void)run;
355- (void)dealloc;
356@end
357
358@implementation VBoxTask
359@end
360
361@implementation VBoxTaskPerformSelector
362- (id)initWithObject:(id)aObject selector:(SEL)aSelector arg:(id)aArg
363{
364 [aObject retain];
365 m_Object = aObject;
366 m_Selector = aSelector;
367 if (aArg != nil)
368 [aArg retain];
369 m_Arg = aArg;
370
371 return self;
372}
373
374- (void)run
375{
376 [m_Object performSelector:m_Selector withObject:m_Arg];
377}
378
379- (void)dealloc
380{
381 [m_Object release];
382 if (m_Arg != nil)
383 [m_Arg release];
384}
385@end
386
387@implementation VBoxTaskComposite
388- (id)init
389{
390 int rc = RTCritSectInit(&m_Lock);
391 if (!RT_SUCCESS(rc))
392 {
393 DEBUG_WARN(("RTCritSectInit failed %d\n", rc));
394 return nil;
395 }
396
397 m_CurIndex = 0;
398
399 m_pArray = [[NSMutableArray alloc] init];
400 return self;
401}
402
403- (void)add:(VBoxTask*)pTask
404{
405 [pTask retain];
406 int rc = RTCritSectEnter(&m_Lock);
407 if (RT_SUCCESS(rc))
408 {
409 [m_pArray addObject:pTask];
410 RTCritSectLeave(&m_Lock);
411 }
412 else
413 {
414 DEBUG_WARN(("RTCritSectEnter failed %d\n", rc));
415 [pTask release];
416 }
417}
418
419- (void)run
420{
421 for(;;)
422 {
423 int rc = RTCritSectEnter(&m_Lock);
424 if (RT_FAILURE(rc))
425 {
426 DEBUG_WARN(("RTCritSectEnter failed %d\n", rc));
427 break;
428 }
429
430 NSUInteger count = [m_pArray count];
431 Assert(m_CurIndex <= count);
432 if (m_CurIndex == count)
433 {
434 [m_pArray removeAllObjects];
435 m_CurIndex = 0;
436 RTCritSectLeave(&m_Lock);
437 break;
438 }
439
440 VBoxTask* pTask = (VBoxTask*)[m_pArray objectAtIndex:m_CurIndex];
441 Assert(pTask != nil);
442
443 ++m_CurIndex;
444
445 if (m_CurIndex > 1024)
446 {
447 NSRange range;
448 range.location = 0;
449 range.length = m_CurIndex;
450 [m_pArray removeObjectsInRange:range];
451 m_CurIndex = 0;
452 }
453 RTCritSectLeave(&m_Lock);
454
455 [pTask run];
456 [pTask release];
457 }
458}
459
460- (void)dealloc
461{
462 NSUInteger count = [m_pArray count];
463 for(;m_CurIndex < count; ++m_CurIndex)
464 {
465 VBoxTask* pTask = (VBoxTask*)[m_pArray objectAtIndex:m_CurIndex];
466 DEBUG_WARN(("dealloc with non-empty tasks! %p\n", pTask));
467 [pTask release];
468 }
469
470 [m_pArray release];
471 RTCritSectDelete(&m_Lock);
472}
473@end
474
475@interface VBoxMainThreadTaskRunner : NSObject
476{
477@private
478 VBoxTaskComposite *m_pTasks;
479}
480- (id)init;
481- (void)add:(VBoxTask*)pTask;
482- (void)addObj:(id)aObject selector:(SEL)aSelector arg:(id)aArg;
483- (void)runTasks;
484- (bool)runTasksSyncIfPossible;
485- (void)dealloc;
486+ (VBoxMainThreadTaskRunner*) globalInstance;
487@end
488
489@implementation VBoxMainThreadTaskRunner
490- (id)init
491{
492 self = [super init];
493 if (self)
494 {
495 m_pTasks = [[VBoxTaskComposite alloc] init];
496 }
497
498 return self;
499}
500
501+ (VBoxMainThreadTaskRunner*) globalInstance
502{
503 static dispatch_once_t dispatchOnce;
504 static VBoxMainThreadTaskRunner *pRunner = nil;
505 dispatch_once(&dispatchOnce, ^{
506 pRunner = [[VBoxMainThreadTaskRunner alloc] init];
507 });
508 return pRunner;
509}
510
511typedef struct CR_RCD_RUN
512{
513 VBoxMainThreadTaskRunner *pRunner;
514} CR_RCD_RUN;
515
516static DECLCALLBACK(void) vboxRcdRun(void *pvCb)
517{
518 DEBUG_FUNC_ENTER();
519 CR_RCD_RUN * pRun = (CR_RCD_RUN*)pvCb;
520 [pRun->pRunner runTasks];
521 DEBUG_FUNC_LEAVE();
522}
523
524- (void)add:(VBoxTask*)pTask
525{
526 DEBUG_FUNC_ENTER();
527 [m_pTasks add:pTask];
528 [self retain];
529
530 if (![self runTasksSyncIfPossible])
531 {
532 DEBUG_MSG(("task will be processed async\n"));
533 [self performSelectorOnMainThread:@selector(runTasks) withObject:nil waitUntilDone:NO];
534 }
535
536 DEBUG_FUNC_LEAVE();
537}
538
539- (void)addObj:(id)aObject selector:(SEL)aSelector arg:(id)aArg
540{
541 VBoxTaskPerformSelector *pSelTask = [[VBoxTaskPerformSelector alloc] initWithObject:aObject selector:aSelector arg:aArg];
542 [self add:pSelTask];
543 [pSelTask release];
544}
545
546- (void)runTasks
547{
548 BOOL fIsMain = [NSThread isMainThread];
549 Assert(fIsMain);
550 if (fIsMain)
551 {
552 [m_pTasks run];
553 [self release];
554 }
555 else
556 {
557 DEBUG_WARN(("run tasks called not on main thread!\n"));
558 [self performSelectorOnMainThread:@selector(runTasks) withObject:nil waitUntilDone:YES];
559 }
560}
561
562- (bool)runTasksSyncIfPossible
563{
564 if (renderspuCalloutAvailable())
565 {
566 CR_RCD_RUN Run;
567 Run.pRunner = self;
568 Assert(![NSThread isMainThread]);
569 renderspuCalloutClient(vboxRcdRun, &Run);
570 return true;
571 }
572
573 if ([NSThread isMainThread])
574 {
575 [self runTasks];
576 return true;
577 }
578
579 return false;
580}
581
582- (void)dealloc
583{
584 [m_pTasks release];
585}
586
587@end
588
589@class DockOverlayView;
590
591/** The custom view class.
592 * This is the main class of the cocoa OpenGL implementation. It
593 * manages an frame buffer object for the rendering of the guest
594 * applications. The guest applications render in this frame buffer which
595 * is bind to an OpenGL texture. To display the guest content, an secondary
596 * shared OpenGL context of the main OpenGL context is created. The secondary
597 * context is marked as non opaque & the texture is displayed on an object
598 * which is composed out of the several visible region rectangles. */
599@interface OverlayView: NSView
600{
601@private
602 NSView *m_pParentView;
603 NSWindow *m_pOverlayWin;
604
605 NSOpenGLContext *m_pGLCtx;
606 NSOpenGLContext *m_pSharedGLCtx;
607 RTTHREAD mThread;
608
609 GLuint m_FBOId;
610
611 /** The corresponding dock tile view of this OpenGL view & all helper
612 * members. */
613 DockOverlayView *m_DockTileView;
614
615 GLfloat m_FBOThumbScaleX;
616 GLfloat m_FBOThumbScaleY;
617 uint64_t m_uiDockUpdateTime;
618
619 /* For clipping */
620 GLint m_cClipRects;
621 GLint *m_paClipRects;
622
623 /* Position/Size tracking */
624 NSPoint m_Pos;
625 NSSize m_Size;
626
627 /** This is necessary for clipping on the root window */
628 NSRect m_RootRect;
629 float m_yInvRootOffset;
630
631 CR_BLITTER *m_pBlitter;
632 WindowInfo *m_pWinInfo;
633 bool m_fNeedViewportUpdate;
634 bool m_fNeedCtxUpdate;
635 bool m_fDataVisible;
636 bool m_fCleanupNeeded;
637 bool m_fEverSized;
638}
639- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView winInfo:(WindowInfo*)pWinInfo;
640- (void)setGLCtx:(NSOpenGLContext*)pCtx;
641- (NSOpenGLContext*)glCtx;
642
643- (void)setParentView: (NSView*)view;
644- (NSView*)parentView;
645- (void)setOverlayWin: (NSWindow*)win;
646- (NSWindow*)overlayWin;
647
648- (void)vboxSetPos:(NSPoint)pos;
649- (void)vboxSetPosUI:(NSPoint)pos;
650- (void)vboxSetPosUIObj:(NSValue*)pPos;
651- (NSPoint)pos;
652- (bool)isEverSized;
653- (void)vboxDestroy;
654- (void)vboxSetSizeUI:(NSSize)size;
655- (void)vboxSetSizeUIObj:(NSValue*)pSize;
656- (void)vboxSetSize:(NSSize)size;
657- (NSSize)size;
658- (void)updateViewportCS;
659- (void)vboxReshapePerform;
660- (void)vboxReshapeOnResizePerform;
661- (void)vboxReshapeOnReparentPerform;
662
663- (void)createDockTile;
664- (void)deleteDockTile;
665
666- (void)makeCurrentFBO;
667- (void)swapFBO;
668- (void)vboxSetVisible:(GLboolean)fVisible;
669- (void)vboxSetVisibleUIObj:(NSNumber*)pVisible;
670- (void)vboxSetVisibleUI:(GLboolean)fVisible;
671- (void)vboxTryDraw;
672- (void)vboxTryDrawUI;
673- (void)vboxReparent:(NSView*)pParentView;
674- (void)vboxReparentUI:(NSView*)pParentView;
675- (void)vboxPresent:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
676- (void)vboxPresentCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
677- (void)vboxPresentToDockTileCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
678- (void)vboxPresentToViewCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
679- (void)presentComposition:(const VBOXVR_SCR_COMPOSITOR_ENTRY*)pChangedEntry;
680- (void)vboxBlitterSyncWindow;
681
682- (void)clearVisibleRegions;
683- (void)setVisibleRegions:(GLint)cRects paRects:(const GLint*)paRects;
684- (GLboolean)vboxNeedsEmptyPresent;
685
686- (NSView*)dockTileScreen;
687- (void)reshapeDockTile;
688- (void)cleanupData;
689@end
690
691/** Helper view.
692 *
693 * This view is added as a sub view of the parent view to track
694 * main window changes. Whenever the main window is changed
695 * (which happens on fullscreen/seamless entry/exit) the overlay
696 * window is informed & can add them self as a child window
697 * again. */
698@class OverlayWindow;
699@interface OverlayHelperView: NSView
700{
701@private
702 OverlayWindow *m_pOverlayWindow;
703}
704-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow;
705@end
706
707/** Custom window class.
708 *
709 * This is the overlay window which contains our custom NSView.
710 * Its a direct child of the Qt Main window. It marks its background
711 * transparent & non opaque to make clipping possible. It also disable mouse
712 * events and handle frame change events of the parent view. */
713@interface OverlayWindow: NSWindow
714{
715@private
716 NSView *m_pParentView;
717 OverlayView *m_pOverlayView;
718 OverlayHelperView *m_pOverlayHelperView;
719 NSThread *m_Thread;
720}
721- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView;
722- (void)parentWindowFrameChanged:(NSNotification *)note;
723- (void)parentWindowChanged:(NSWindow*)pWindow;
724@end
725
726@interface DockOverlayView: NSView
727{
728 NSBitmapImageRep *m_ThumbBitmap;
729 NSImage *m_ThumbImage;
730 NSLock *m_Lock;
731}
732- (void)dealloc;
733- (void)cleanup;
734- (void)lock;
735- (void)unlock;
736- (void)setFrame:(NSRect)frame;
737- (void)drawRect:(NSRect)aRect;
738- (NSBitmapImageRep*)thumbBitmap;
739- (NSImage*)thumbImage;
740@end
741
742@implementation DockOverlayView
743- (id)init
744{
745 DEBUG_FUNC_ENTER();
746 self = [super init];
747
748 if (self)
749 {
750 /* We need a lock cause the thumb image could be accessed from the main
751 * thread when someone is calling display on the dock tile & from the
752 * OpenGL thread when the thumbnail is updated. */
753 m_Lock = [[NSLock alloc] init];
754 }
755
756 DEBUG_FUNC_LEAVE();
757
758 return self;
759}
760
761- (void)dealloc
762{
763 DEBUG_FUNC_ENTER();
764
765 [self cleanup];
766 [m_Lock release];
767
768 [super dealloc];
769
770 DEBUG_FUNC_LEAVE();
771}
772
773- (void)cleanup
774{
775 DEBUG_FUNC_ENTER();
776
777 if (m_ThumbImage != nil)
778 {
779 [m_ThumbImage release];
780 m_ThumbImage = nil;
781 }
782 if (m_ThumbBitmap != nil)
783 {
784 [m_ThumbBitmap release];
785 m_ThumbBitmap = nil;
786 }
787
788 DEBUG_FUNC_LEAVE();
789}
790
791- (void)lock
792{
793 DEBUG_FUNC_ENTER();
794 [m_Lock lock];
795 DEBUG_FUNC_LEAVE();
796}
797
798- (void)unlock
799{
800 DEBUG_FUNC_ENTER();
801 [m_Lock unlock];
802 DEBUG_FUNC_LEAVE();
803}
804
805- (void)setFrame:(NSRect)frame
806{
807 DEBUG_FUNC_ENTER();
808 [super setFrame:frame];
809
810 [self lock];
811 [self cleanup];
812
813 if ( frame.size.width > 0
814 && frame.size.height > 0)
815 {
816 /* Create a buffer for our thumbnail image. Its in the size of this view. */
817 m_ThumbBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
818 pixelsWide:frame.size.width
819 pixelsHigh:frame.size.height
820 bitsPerSample:8
821 samplesPerPixel:4
822 hasAlpha:YES
823 isPlanar:NO
824 colorSpaceName:NSDeviceRGBColorSpace
825 bitmapFormat:NSAlphaFirstBitmapFormat
826 bytesPerRow:frame.size.width * 4
827 bitsPerPixel:8 * 4];
828 m_ThumbImage = [[NSImage alloc] initWithSize:[m_ThumbBitmap size]];
829 [m_ThumbImage addRepresentation:m_ThumbBitmap];
830 }
831 [self unlock];
832 DEBUG_FUNC_LEAVE();
833}
834
835- (BOOL)isFlipped
836{
837 DEBUG_FUNC_ENTER();
838 DEBUG_FUNC_LEAVE();
839 return YES;
840}
841
842- (void)drawRect:(NSRect)aRect
843{
844 DEBUG_FUNC_ENTER();
845 NSRect frame;
846
847 [self lock];
848#ifdef SHOW_WINDOW_BACKGROUND
849 [[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7] set];
850 frame = [self frame];
851 [NSBezierPath fillRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
852#endif /* SHOW_WINDOW_BACKGROUND */
853 if (m_ThumbImage != nil)
854 [m_ThumbImage drawAtPoint:NSMakePoint(0, 0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
855 [self unlock];
856 DEBUG_FUNC_LEAVE();
857}
858
859- (NSBitmapImageRep*)thumbBitmap
860{
861 DEBUG_FUNC_ENTER();
862 DEBUG_FUNC_LEAVE();
863 return m_ThumbBitmap;
864}
865
866- (NSImage*)thumbImage
867{
868 DEBUG_FUNC_ENTER();
869 DEBUG_FUNC_LEAVE();
870
871 return m_ThumbImage;
872}
873@end
874
875/********************************************************************************
876*
877* OverlayOpenGLContext class implementation
878*
879********************************************************************************/
880@implementation OverlayOpenGLContext
881
882-(id)initWithFormat:(NSOpenGLPixelFormat*)format shareContext:(NSOpenGLContext*)share
883{
884 DEBUG_FUNC_ENTER();
885
886 m_pPixelFormat = NULL;
887 m_pView = NULL;
888
889 self = [super initWithFormat:format shareContext:share];
890 if (self)
891 m_pPixelFormat = format;
892
893 DEBUG_MSG(("OCTX(%p): init OverlayOpenGLContext\n", (void*)self));
894
895 DEBUG_FUNC_LEAVE();
896
897 return self;
898}
899
900- (void)dealloc
901{
902 DEBUG_FUNC_ENTER();
903
904 DEBUG_MSG(("OCTX(%p): dealloc OverlayOpenGLContext\n", (void*)self));
905
906 [m_pPixelFormat release];
907
908 [super dealloc];
909
910 DEBUG_FUNC_LEAVE();
911}
912
913-(bool)isDoubleBuffer
914{
915 DEBUG_FUNC_ENTER();
916
917 GLint val;
918 [m_pPixelFormat getValues:&val forAttribute:NSOpenGLPFADoubleBuffer forVirtualScreen:0];
919
920 DEBUG_FUNC_LEAVE();
921
922 return val == GL_TRUE ? YES : NO;
923}
924
925-(void)setView:(NSView*)view
926{
927 DEBUG_FUNC_ENTER();
928
929 DEBUG_MSG(("OCTX(%p): setView: new view: %p\n", (void*)self, (void*)view));
930
931#if 1 /* def FBO */
932 m_pView = view;;
933#else
934 [super setView: view];
935#endif
936
937 DEBUG_FUNC_LEAVE();
938}
939
940-(NSView*)view
941{
942 DEBUG_FUNC_ENTER();
943 DEBUG_FUNC_LEAVE();
944
945#if 1 /* def FBO */
946 return m_pView;
947#else
948 return [super view];
949#endif
950}
951
952-(void)clearDrawable
953{
954 DEBUG_FUNC_ENTER();
955
956 DEBUG_MSG(("OCTX(%p): clearDrawable\n", (void*)self));
957
958 m_pView = NULL;;
959 [super clearDrawable];
960
961 DEBUG_FUNC_LEAVE();
962}
963
964-(NSOpenGLPixelFormat*)openGLPixelFormat
965{
966 DEBUG_FUNC_ENTER();
967 DEBUG_FUNC_LEAVE();
968
969 return m_pPixelFormat;
970}
971
972@end
973
974/********************************************************************************
975*
976* OverlayHelperView class implementation
977*
978********************************************************************************/
979@implementation OverlayHelperView
980
981-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow
982{
983 DEBUG_FUNC_ENTER();
984
985 self = [super initWithFrame:NSZeroRect];
986
987 m_pOverlayWindow = pOverlayWindow;
988
989 DEBUG_MSG(("OHVW(%p): init OverlayHelperView\n", (void*)self));
990
991 DEBUG_FUNC_LEAVE();
992
993 return self;
994}
995
996-(void)viewDidMoveToWindow
997{
998 DEBUG_FUNC_ENTER();
999
1000 DEBUG_MSG(("OHVW(%p): viewDidMoveToWindow: new win: %p\n", (void*)self, (void*)[self window]));
1001
1002 [m_pOverlayWindow parentWindowChanged:[self window]];
1003
1004 DEBUG_FUNC_LEAVE();
1005}
1006
1007@end
1008
1009/********************************************************************************
1010*
1011* OverlayWindow class implementation
1012*
1013********************************************************************************/
1014@implementation OverlayWindow
1015
1016- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView
1017{
1018 DEBUG_FUNC_ENTER();
1019
1020 NSWindow *pParentWin = nil;
1021
1022 if((self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]))
1023 {
1024 m_pParentView = pParentView;
1025 m_pOverlayView = pOverlayView;
1026 m_Thread = [NSThread currentThread];
1027
1028 [m_pOverlayView setOverlayWin: self];
1029
1030 m_pOverlayHelperView = [[OverlayHelperView alloc] initWithOverlayWindow:self];
1031 /* Add the helper view as a child of the parent view to get notifications */
1032 [pParentView addSubview:m_pOverlayHelperView];
1033
1034 /* Make sure this window is transparent */
1035#ifdef SHOW_WINDOW_BACKGROUND
1036 /* For debugging */
1037 [self setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7]];
1038#else
1039 [self setBackgroundColor:[NSColor clearColor]];
1040#endif
1041 [self setOpaque:NO];
1042 [self setAlphaValue:.999];
1043 /* Disable mouse events for this window */
1044 [self setIgnoresMouseEvents:YES];
1045
1046 pParentWin = [m_pParentView window];
1047
1048 /* Initial set the position to the parents view top/left (Compiz fix). */
1049 [self setFrameOrigin:
1050 [pParentWin convertBaseToScreen:
1051 [m_pParentView convertPoint:NSZeroPoint toView:nil]]];
1052
1053 /* Set the overlay view as our content view */
1054 [self setContentView:m_pOverlayView];
1055
1056 /* Add ourself as a child to the parent views window. Note: this has to
1057 * be done last so that everything else is setup in
1058 * parentWindowChanged. */
1059 [pParentWin addChildWindow:self ordered:NSWindowAbove];
1060 }
1061 DEBUG_MSG(("OWIN(%p): init OverlayWindow\n", (void*)self));
1062
1063 DEBUG_FUNC_LEAVE();
1064
1065 return self;
1066}
1067
1068- (void)dealloc
1069{
1070 DEBUG_FUNC_ENTER();
1071
1072 DEBUG_MSG(("OWIN(%p): dealloc OverlayWindow\n", (void*)self));
1073
1074 [[NSNotificationCenter defaultCenter] removeObserver:self];
1075
1076 [m_pOverlayHelperView removeFromSuperview];
1077 [m_pOverlayHelperView release];
1078
1079 [super dealloc];
1080
1081 DEBUG_FUNC_LEAVE();
1082}
1083
1084- (void)parentWindowFrameChanged:(NSNotification*)pNote
1085{
1086 DEBUG_FUNC_ENTER();
1087
1088 DEBUG_MSG(("OWIN(%p): parentWindowFrameChanged\n", (void*)self));
1089
1090 /* Reposition this window with the help of the OverlayView. Perform the
1091 * call in the OpenGL thread. */
1092 /*
1093 [m_pOverlayView performSelector:@selector(vboxReshapePerform) onThread:m_Thread withObject:nil waitUntilDone:YES];
1094 */
1095
1096 if ([m_pOverlayView isEverSized])
1097 {
1098 if([NSThread isMainThread])
1099 [m_pOverlayView vboxReshapePerform];
1100 else
1101 [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
1102 }
1103
1104 DEBUG_FUNC_LEAVE();
1105}
1106
1107- (void)parentWindowChanged:(NSWindow*)pWindow
1108{
1109 DEBUG_FUNC_ENTER();
1110
1111 DEBUG_MSG(("OWIN(%p): parentWindowChanged\n", (void*)self));
1112
1113 [[NSNotificationCenter defaultCenter] removeObserver:self];
1114
1115 if(pWindow != nil)
1116 {
1117 /* Ask to get notifications when our parent window frame changes. */
1118 [[NSNotificationCenter defaultCenter]
1119 addObserver:self
1120 selector:@selector(parentWindowFrameChanged:)
1121 name:NSWindowDidResizeNotification
1122 object:pWindow];
1123 /* Add us self as child window */
1124 [pWindow addChildWindow:self ordered:NSWindowAbove];
1125 /* Reshape the overlay view after a short waiting time to let the main
1126 * window resize itself properly. */
1127 /*
1128 [m_pOverlayView performSelector:@selector(vboxReshapePerform) withObject:nil afterDelay:0.2];
1129 [NSTimer scheduledTimerWithTimeInterval:0.2 target:m_pOverlayView selector:@selector(vboxReshapePerform) userInfo:nil repeats:NO];
1130 */
1131
1132 if ([m_pOverlayView isEverSized])
1133 {
1134 if([NSThread isMainThread])
1135 [m_pOverlayView vboxReshapePerform];
1136 else
1137 [self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
1138 }
1139 }
1140
1141 DEBUG_FUNC_LEAVE();
1142}
1143
1144@end
1145
1146/********************************************************************************
1147*
1148* OverlayView class implementation
1149*
1150********************************************************************************/
1151@implementation OverlayView
1152
1153- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView winInfo:(WindowInfo*)pWinInfo
1154{
1155 DEBUG_FUNC_ENTER();
1156
1157 m_pParentView = pParentView;
1158 /* Make some reasonable defaults */
1159 m_pGLCtx = nil;
1160 m_pSharedGLCtx = nil;
1161 mThread = aThread;
1162 m_FBOId = 0;
1163 m_cClipRects = 0;
1164 m_paClipRects = NULL;
1165 m_Pos = NSZeroPoint;
1166 m_Size = NSMakeSize(1, 1);
1167 m_RootRect = NSMakeRect(0, 0, m_Size.width, m_Size.height);
1168 m_yInvRootOffset = 0;
1169 m_pBlitter = nil;
1170 m_pWinInfo = pWinInfo;
1171 m_fNeedViewportUpdate = true;
1172 m_fNeedCtxUpdate = true;
1173 m_fDataVisible = false;
1174 m_fCleanupNeeded = false;
1175 m_fEverSized = false;
1176
1177 self = [super initWithFrame:frame];
1178
1179 DEBUG_MSG(("OVIW(%p): init OverlayView\n", (void*)self));
1180
1181 DEBUG_FUNC_LEAVE();
1182
1183 return self;
1184}
1185
1186- (void)cleanupData
1187{
1188 DEBUG_FUNC_ENTER();
1189
1190 [self deleteDockTile];
1191
1192 [self setGLCtx:nil];
1193
1194 if (m_pSharedGLCtx)
1195 {
1196 if ([m_pSharedGLCtx view] == self)
1197 [m_pSharedGLCtx clearDrawable];
1198
1199 [m_pSharedGLCtx release];
1200
1201 m_pSharedGLCtx = nil;
1202
1203 CrBltTerm(m_pBlitter);
1204
1205 RTMemFree(m_pBlitter);
1206
1207 m_pBlitter = nil;
1208 }
1209
1210 [self clearVisibleRegions];
1211
1212 DEBUG_FUNC_LEAVE();
1213}
1214
1215- (void)dealloc
1216{
1217 DEBUG_FUNC_ENTER();
1218
1219 DEBUG_MSG(("OVIW(%p): dealloc OverlayView\n", (void*)self));
1220
1221 [self cleanupData];
1222
1223 [super dealloc];
1224
1225 DEBUG_FUNC_LEAVE();
1226}
1227
1228- (void)drawRect:(NSRect)aRect
1229{
1230 [self vboxTryDrawUI];
1231}
1232
1233- (void)setGLCtx:(NSOpenGLContext*)pCtx
1234{
1235 DEBUG_FUNC_ENTER();
1236
1237 DEBUG_MSG(("OVIW(%p): setGLCtx: new ctx: %p\n", (void*)self, (void*)pCtx));
1238 if (m_pGLCtx == pCtx)
1239 {
1240 DEBUG_FUNC_LEAVE();
1241 return;
1242 }
1243
1244 /* ensure the context drawable is cleared to avoid holding a reference to inexistent view */
1245 if (m_pGLCtx)
1246 {
1247 [m_pGLCtx clearDrawable];
1248 [m_pGLCtx release];
1249 /*[m_pGLCtx performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];*/
1250 }
1251
1252 m_pGLCtx = pCtx;
1253 if (pCtx)
1254 [pCtx retain];
1255
1256 DEBUG_FUNC_LEAVE();
1257}
1258
1259- (NSOpenGLContext*)glCtx
1260{
1261 DEBUG_FUNC_ENTER();
1262 DEBUG_FUNC_LEAVE();
1263
1264 return m_pGLCtx;
1265}
1266
1267- (NSView*)parentView
1268{
1269 DEBUG_FUNC_ENTER();
1270 DEBUG_FUNC_LEAVE();
1271
1272 return m_pParentView;
1273}
1274
1275- (void)setParentView:(NSView*)pView
1276{
1277 DEBUG_FUNC_ENTER();
1278
1279 DEBUG_MSG(("OVIW(%p): setParentView: new view: %p\n", (void*)self, (void*)pView));
1280
1281 m_pParentView = pView;
1282
1283 DEBUG_FUNC_LEAVE();
1284}
1285
1286- (void)setOverlayWin:(NSWindow*)pWin
1287{
1288 DEBUG_FUNC_ENTER();
1289
1290 DEBUG_MSG(("OVIW(%p): setOverlayWin: new win: %p\n", (void*)self, (void*)pWin));
1291
1292 m_pOverlayWin = pWin;
1293
1294 DEBUG_FUNC_LEAVE();
1295}
1296
1297- (NSWindow*)overlayWin
1298{
1299 DEBUG_FUNC_ENTER();
1300 DEBUG_FUNC_LEAVE();
1301
1302 return m_pOverlayWin;
1303}
1304
1305- (void)vboxSetPosUI:(NSPoint)pos
1306{
1307 DEBUG_FUNC_ENTER();
1308
1309 m_Pos = pos;
1310
1311 if (m_fEverSized)
1312 [self vboxReshapePerform];
1313
1314 DEBUG_FUNC_LEAVE();
1315}
1316
1317- (void)vboxSetPosUIObj:(NSValue*)pPos
1318{
1319 DEBUG_FUNC_ENTER();
1320
1321 NSPoint pos = [pPos pointValue];
1322 [self vboxSetPosUI:pos];
1323
1324 DEBUG_FUNC_LEAVE();
1325}
1326
1327typedef struct CR_RCD_SETPOS
1328{
1329 OverlayView *pView;
1330 NSPoint pos;
1331} CR_RCD_SETPOS;
1332
1333static DECLCALLBACK(void) vboxRcdSetPos(void *pvCb)
1334{
1335 DEBUG_FUNC_ENTER();
1336
1337 CR_RCD_SETPOS * pReparent = (CR_RCD_SETPOS*)pvCb;
1338 [pReparent->pView vboxSetPosUI:pReparent->pos];
1339
1340 DEBUG_FUNC_LEAVE();
1341}
1342
1343- (void)vboxSetPos:(NSPoint)pos
1344{
1345 DEBUG_FUNC_ENTER();
1346
1347 DEBUG_MSG(("OVIW(%p): vboxSetPos: new pos: %d, %d\n", (void*)self, (int)pos.x, (int)pos.y));
1348 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
1349 NSValue *pPos = [NSValue valueWithPoint:pos];
1350 [pRunner addObj:self selector:@selector(vboxSetPosUIObj:) arg:pPos];
1351
1352 DEBUG_FUNC_LEAVE();
1353}
1354
1355- (NSPoint)pos
1356{
1357 DEBUG_FUNC_ENTER();
1358 DEBUG_FUNC_LEAVE();
1359 return m_Pos;
1360}
1361
1362- (bool)isEverSized
1363{
1364 DEBUG_FUNC_ENTER();
1365 DEBUG_FUNC_LEAVE();
1366 return m_fEverSized;
1367}
1368
1369- (void)vboxDestroy
1370{
1371 DEBUG_FUNC_ENTER();
1372 BOOL fIsMain = [NSThread isMainThread];
1373 NSWindow *pWin = nil;
1374
1375 Assert(fIsMain);
1376
1377 /* Hide the view early */
1378 [self setHidden: YES];
1379
1380 pWin = [self window];
1381 [[NSNotificationCenter defaultCenter] removeObserver:pWin];
1382 [pWin setContentView: nil];
1383 [[pWin parentWindow] removeChildWindow: pWin];
1384
1385 if (fIsMain)
1386 [pWin release];
1387 else
1388 {
1389 /* We can NOT run synchronously with the main thread since this may lead to a deadlock,
1390 caused by main thread waiting xpcom thread, xpcom thread waiting to main hgcm thread,
1391 and main hgcm thread waiting for us, this is why use waitUntilDone:NO,
1392 which should cause no harm */
1393 [pWin performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
1394 }
1395
1396 [self cleanupData];
1397
1398 if (fIsMain)
1399 [self release];
1400 else
1401 {
1402 /* We can NOT run synchronously with the main thread since this may lead to a deadlock,
1403 caused by main thread waiting xpcom thread, xpcom thread waiting to main hgcm thread,
1404 and main hgcm thread waiting for us, this is why use waitUntilDone:NO.
1405 We need to avoid concurrency though, so we cleanup some data right away via a cleanupData call */
1406 [self performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
1407 }
1408 DEBUG_FUNC_LEAVE();
1409}
1410
1411- (void)vboxSetSizeUIObj:(NSValue*)pSize
1412{
1413 DEBUG_FUNC_ENTER();
1414 NSSize size = [pSize sizeValue];
1415 [self vboxSetSizeUI:size];
1416 DEBUG_FUNC_LEAVE();
1417}
1418
1419- (void)vboxSetSizeUI:(NSSize)size
1420{
1421 DEBUG_FUNC_ENTER();
1422 m_Size = size;
1423
1424 m_fEverSized = true;
1425
1426 DEBUG_MSG(("OVIW(%p): vboxSetSize: new size: %dx%d\n", (void*)self, (int)m_Size.width, (int)m_Size.height));
1427 [self vboxReshapeOnResizePerform];
1428
1429 /* ensure window contents is updated after that */
1430 [self vboxTryDrawUI];
1431 DEBUG_FUNC_LEAVE();
1432}
1433
1434typedef struct CR_RCD_SETSIZE
1435{
1436 OverlayView *pView;
1437 NSSize size;
1438} CR_RCD_SETSIZE;
1439
1440static DECLCALLBACK(void) vboxRcdSetSize(void *pvCb)
1441{
1442 DEBUG_FUNC_ENTER();
1443 CR_RCD_SETSIZE * pSetSize = (CR_RCD_SETSIZE*)pvCb;
1444 [pSetSize->pView vboxSetSizeUI:pSetSize->size];
1445 DEBUG_FUNC_LEAVE();
1446}
1447
1448- (void)vboxSetSize:(NSSize)size
1449{
1450 DEBUG_FUNC_ENTER();
1451
1452 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
1453 NSValue *pSize = [NSValue valueWithSize:size];
1454 [pRunner addObj:self selector:@selector(vboxSetSizeUIObj:) arg:pSize];
1455
1456 DEBUG_FUNC_LEAVE();
1457}
1458
1459- (NSSize)size
1460{
1461 DEBUG_FUNC_ENTER();
1462 return m_Size;
1463 DEBUG_FUNC_LEAVE();
1464}
1465
1466- (void)updateViewportCS
1467{
1468 DEBUG_FUNC_ENTER();
1469 DEBUG_MSG(("OVIW(%p): updateViewport\n", (void*)self));
1470
1471 /* Update the viewport for our OpenGL view */
1472 [m_pSharedGLCtx update];
1473
1474 [self vboxBlitterSyncWindow];
1475
1476 /* Clear background to transparent */
1477 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1478 DEBUG_FUNC_LEAVE();
1479}
1480
1481- (void)vboxReshapeOnResizePerform
1482{
1483 DEBUG_FUNC_ENTER();
1484 [self vboxReshapePerform];
1485
1486 [self createDockTile];
1487 /* have to rebind GL_TEXTURE_RECTANGLE_ARB as m_FBOTexId could be changed in updateFBO call */
1488 m_fNeedViewportUpdate = true;
1489#if 0
1490 pCurCtx = [NSOpenGLContext currentContext];
1491 if (pCurCtx && pCurCtx == m_pGLCtx && (pCurView = [pCurCtx view]) == self)
1492 {
1493 [m_pGLCtx update];
1494 m_fNeedCtxUpdate = false;
1495 }
1496 else
1497 {
1498 /* do it in a lazy way */
1499 m_fNeedCtxUpdate = true;
1500 }
1501#endif
1502 DEBUG_FUNC_LEAVE();
1503}
1504
1505- (void)vboxReshapeOnReparentPerform
1506{
1507 DEBUG_FUNC_ENTER();
1508 [self createDockTile];
1509 DEBUG_FUNC_LEAVE();
1510}
1511
1512- (void)vboxReshapePerform
1513{
1514 DEBUG_FUNC_ENTER();
1515 NSRect parentFrame = NSZeroRect;
1516 NSPoint parentPos = NSZeroPoint;
1517 NSPoint childPos = NSZeroPoint;
1518 NSRect childFrame = NSZeroRect;
1519 NSRect newFrame = NSZeroRect;
1520
1521 DEBUG_MSG(("OVIW(%p): vboxReshapePerform\n", (void*)self));
1522
1523 parentFrame = [m_pParentView frame];
1524 DEBUG_MSG(("FIXED parentFrame [%f:%f], [%f:%f]\n", parentFrame.origin.x, parentFrame.origin.y, parentFrame.size.width, parentFrame.size.height));
1525 parentPos = parentFrame.origin;
1526 parentPos.y += parentFrame.size.height;
1527 DEBUG_MSG(("FIXED(view) parentPos [%f:%f]\n", parentPos.x, parentPos.y));
1528 parentPos = [m_pParentView convertPoint:parentPos toView:nil];
1529 DEBUG_MSG(("FIXED parentPos(win) [%f:%f]\n", parentPos.x, parentPos.y));
1530 parentPos = [[m_pParentView window] convertBaseToScreen:parentPos];
1531 DEBUG_MSG(("FIXED parentPos(screen) [%f:%f]\n", parentPos.x, parentPos.y));
1532 parentFrame.origin = parentPos;
1533
1534 childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
1535 DEBUG_MSG(("FIXED(view) childPos [%f:%f]\n", childPos.x, childPos.y));
1536 childPos = [m_pParentView convertPoint:childPos toView:nil];
1537 DEBUG_MSG(("FIXED(win) childPos [%f:%f]\n", childPos.x, childPos.y));
1538 childPos = [[m_pParentView window] convertBaseToScreen:childPos];
1539 DEBUG_MSG(("FIXED childPos(screen) [%f:%f]\n", childPos.x, childPos.y));
1540 childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
1541 DEBUG_MSG(("FIXED childFrame [%f:%f], [%f:%f]\n", childFrame.origin.x, childFrame.origin.y, childFrame.size.width, childFrame.size.height));
1542
1543 /* We have to make sure that the overlay window will not be displayed out
1544 * of the parent window. So intersect both frames & use the result as the new
1545 * frame for the window. */
1546 newFrame = NSIntersectionRect(parentFrame, childFrame);
1547
1548 DEBUG_MSG(("[%#p]: parentFrame pos[%f : %f] size[%f : %f]\n",
1549 (void*)self,
1550 parentFrame.origin.x, parentFrame.origin.y,
1551 parentFrame.size.width, parentFrame.size.height));
1552 DEBUG_MSG(("[%#p]: childFrame pos[%f : %f] size[%f : %f]\n",
1553 (void*)self,
1554 childFrame.origin.x, childFrame.origin.y,
1555 childFrame.size.width, childFrame.size.height));
1556
1557 DEBUG_MSG(("[%#p]: newFrame pos[%f : %f] size[%f : %f]\n",
1558 (void*)self,
1559 newFrame.origin.x, newFrame.origin.y,
1560 newFrame.size.width, newFrame.size.height));
1561
1562 /* Later we have to correct the texture position in the case the window is
1563 * out of the parents window frame. So save the shift values for later use. */
1564 m_RootRect.origin.x = newFrame.origin.x - childFrame.origin.x;
1565 m_RootRect.origin.y = childFrame.size.height + childFrame.origin.y - (newFrame.size.height + newFrame.origin.y);
1566 m_RootRect.size = newFrame.size;
1567 m_yInvRootOffset = newFrame.origin.y - childFrame.origin.y;
1568
1569 DEBUG_MSG(("[%#p]: m_RootRect pos[%f : %f] size[%f : %f]\n",
1570 (void*)self,
1571 m_RootRect.origin.x, m_RootRect.origin.y,
1572 m_RootRect.size.width, m_RootRect.size.height));
1573
1574
1575 /*
1576 NSScrollView *pScrollView = [[[m_pParentView window] contentView] enclosingScrollView];
1577 if (pScrollView)
1578 {
1579 NSRect scrollRect = [pScrollView documentVisibleRect];
1580 NSRect scrollRect = [m_pParentView visibleRect];
1581 printf ("sc rect: %d %d %d %d\n", (int) scrollRect.origin.x,(int) scrollRect.origin.y,(int) scrollRect.size.width,(int) scrollRect.size.height);
1582 NSRect b = [[m_pParentView superview] bounds];
1583 printf ("bound rect: %d %d %d %d\n", (int) b.origin.x,(int) b.origin.y,(int) b.size.width,(int) b.size.height);
1584 newFrame.origin.x += scrollRect.origin.x;
1585 newFrame.origin.y += scrollRect.origin.y;
1586 }
1587 */
1588
1589 /* Set the new frame. */
1590 [[self window] setFrame:newFrame display:YES];
1591
1592 /* Inform the dock tile view as well */
1593 [self reshapeDockTile];
1594
1595 /* Make sure the context is updated according */
1596 /* [self updateViewport]; */
1597 if (m_pSharedGLCtx)
1598 {
1599 VBOX_CR_RENDER_CTX_INFO CtxInfo;
1600 vboxCtxEnter(m_pSharedGLCtx, &CtxInfo);
1601
1602 [self updateViewportCS];
1603
1604 vboxCtxLeave(&CtxInfo);
1605 }
1606 DEBUG_FUNC_LEAVE();
1607}
1608
1609- (void)createDockTile
1610{
1611 DEBUG_FUNC_ENTER();
1612 NSView *pDockScreen = nil;
1613 [self deleteDockTile];
1614
1615 /* Is there a dock tile preview enabled in the GUI? If so setup a
1616 * additional thumbnail view for the dock tile. */
1617 pDockScreen = [self dockTileScreen];
1618 if (pDockScreen)
1619 {
1620 m_DockTileView = [[DockOverlayView alloc] init];
1621 [self reshapeDockTile];
1622 [pDockScreen addSubview:m_DockTileView];
1623 }
1624 DEBUG_FUNC_LEAVE();
1625}
1626
1627- (void)deleteDockTile
1628{
1629 DEBUG_FUNC_ENTER();
1630 if (m_DockTileView != nil)
1631 {
1632 [m_DockTileView removeFromSuperview];
1633 [m_DockTileView release];
1634 m_DockTileView = nil;
1635 }
1636 DEBUG_FUNC_LEAVE();
1637}
1638
1639- (void)makeCurrentFBO
1640{
1641 DEBUG_MSG(("OVIW(%p): makeCurrentFBO\n", (void*)self));
1642
1643 if (m_pGLCtx)
1644 {
1645 if ([m_pGLCtx view] != self)
1646 {
1647 /* We change the active view, so flush first */
1648 if([NSOpenGLContext currentContext] != 0)
1649 glFlush();
1650 [m_pGLCtx setView: self];
1651 CHECK_GL_ERROR();
1652 }
1653 /*
1654 if ([NSOpenGLContext currentContext] != m_pGLCtx)
1655 */
1656 {
1657 [m_pGLCtx makeCurrentContext];
1658 CHECK_GL_ERROR();
1659 if (m_fNeedCtxUpdate == true)
1660 {
1661 [m_pGLCtx update];
1662 m_fNeedCtxUpdate = false;
1663 }
1664 }
1665
1666 if (!m_FBOId)
1667 {
1668 glGenFramebuffersEXT(1, &m_FBOId);
1669 Assert(m_FBOId);
1670 }
1671
1672 }
1673}
1674
1675- (bool)vboxSharedCtxCreate
1676{
1677 DEBUG_FUNC_ENTER();
1678 if (m_pSharedGLCtx)
1679 {
1680 DEBUG_FUNC_LEAVE();
1681 return true;
1682 }
1683
1684 Assert(!m_pBlitter);
1685 m_pBlitter = RTMemAlloc(sizeof (*m_pBlitter));
1686 if (!m_pBlitter)
1687 {
1688 DEBUG_WARN(("m_pBlitter allocation failed"));
1689 DEBUG_FUNC_LEAVE();
1690 return false;
1691 }
1692
1693 int rc = CrBltInit(m_pBlitter, NULL, false, false, &render_spu.GlobalShaders, &render_spu.blitterDispatch);
1694 if (RT_SUCCESS(rc))
1695 {
1696 DEBUG_MSG(("blitter created successfully for view 0x%p\n", (void*)self));
1697 }
1698 else
1699 {
1700 DEBUG_WARN(("CrBltInit failed, rc %d", rc));
1701 RTMemFree(m_pBlitter);
1702 m_pBlitter = NULL;
1703 DEBUG_FUNC_LEAVE();
1704 return false;
1705 }
1706
1707 GLint opaque = 0;
1708 /* Create a shared context out of the main context. Use the same pixel format. */
1709 NSOpenGLContext *pSharedGLCtx = [[NSOpenGLContext alloc] initWithFormat:[(OverlayOpenGLContext*)m_pGLCtx openGLPixelFormat] shareContext:m_pGLCtx];
1710
1711 /* Set the new context as non opaque */
1712 [pSharedGLCtx setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
1713 /* Set this view as the drawable for the new context */
1714 [pSharedGLCtx setView: self];
1715 m_fNeedViewportUpdate = true;
1716
1717 m_pSharedGLCtx = pSharedGLCtx;
1718
1719 DEBUG_FUNC_LEAVE();
1720 return true;
1721}
1722
1723- (void)vboxTryDraw
1724{
1725 glFlush();
1726
1727 DEBUG_MSG(("My[%p]: Draw\n", self));
1728 /* issue to the gui thread */
1729 [self performSelectorOnMainThread:@selector(vboxTryDrawUI) withObject:nil waitUntilDone:NO];
1730}
1731
1732typedef struct CR_RCD_SETVISIBLE
1733{
1734 OverlayView *pView;
1735 BOOL fVisible;
1736} CR_RCD_SETVISIBLE;
1737
1738static DECLCALLBACK(void) vboxRcdSetVisible(void *pvCb)
1739{
1740 DEBUG_FUNC_ENTER();
1741 CR_RCD_SETVISIBLE * pVisible = (CR_RCD_SETVISIBLE*)pvCb;
1742
1743 [pVisible->pView vboxSetVisibleUI:pVisible->fVisible];
1744 DEBUG_FUNC_LEAVE();
1745}
1746
1747- (void)vboxSetVisible:(GLboolean)fVisible
1748{
1749 DEBUG_FUNC_ENTER();
1750
1751 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
1752 NSNumber* pVisObj = [NSNumber numberWithBool:fVisible];
1753 [pRunner addObj:self selector:@selector(vboxSetVisibleUIObj:) arg:pVisObj];
1754
1755 DEBUG_FUNC_LEAVE();
1756}
1757
1758- (void)vboxSetVisibleUI:(GLboolean)fVisible
1759{
1760 DEBUG_FUNC_ENTER();
1761 [self setHidden: !fVisible];
1762 DEBUG_FUNC_LEAVE();
1763}
1764
1765- (void)vboxSetVisibleUIObj:(NSNumber*)pVisible
1766{
1767 DEBUG_FUNC_ENTER();
1768 BOOL fVisible = [pVisible boolValue];
1769 [self vboxSetVisibleUI:fVisible];
1770 DEBUG_FUNC_LEAVE();
1771}
1772
1773typedef struct CR_RCD_REPARENT
1774{
1775 OverlayView *pView;
1776 NSView *pParent;
1777} CR_RCD_REPARENT;
1778
1779static DECLCALLBACK(void) vboxRcdReparent(void *pvCb)
1780{
1781 DEBUG_FUNC_ENTER();
1782 CR_RCD_REPARENT * pReparent = (CR_RCD_REPARENT*)pvCb;
1783 [pReparent->pView vboxReparentUI:pReparent->pParent];
1784 DEBUG_FUNC_LEAVE();
1785}
1786
1787- (void)vboxReparent:(NSView*)pParentView
1788{
1789 DEBUG_FUNC_ENTER();
1790
1791 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
1792 [pRunner addObj:self selector:@selector(vboxReparentUI:) arg:pParentView];
1793
1794 DEBUG_FUNC_LEAVE();
1795}
1796
1797- (void)vboxReparentUI:(NSView*)pParentView
1798{
1799 DEBUG_FUNC_ENTER();
1800 /* Make sure the window is removed from any previous parent window. */
1801 if ([[self overlayWin] parentWindow] != nil)
1802 {
1803 [[[self overlayWin] parentWindow] removeChildWindow:[self overlayWin]];
1804 }
1805
1806 /* Set the new parent view */
1807 [self setParentView: pParentView];
1808
1809 /* Add the overlay window as a child to the new parent window */
1810 if (pParentView != nil)
1811 {
1812 [[pParentView window] addChildWindow:[self overlayWin] ordered:NSWindowAbove];
1813 if ([self isEverSized])
1814 [self vboxReshapeOnReparentPerform];
1815 }
1816
1817 DEBUG_FUNC_LEAVE();
1818}
1819
1820- (void)vboxTryDrawUI
1821{
1822 DEBUG_MSG(("My[%p]: DrawUI\n", self));
1823 const VBOXVR_SCR_COMPOSITOR *pCompositor;
1824 int rc = renderspuVBoxCompositorLock(m_pWinInfo, &pCompositor);
1825 if (RT_FAILURE(rc))
1826 {
1827 DEBUG_WARN(("renderspuVBoxCompositorLock failed\n"));
1828 return;
1829 }
1830
1831 if (!pCompositor && !m_fCleanupNeeded)
1832 {
1833 DEBUG_MSG(("My[%p]: noCompositorUI\n", self));
1834 renderspuVBoxCompositorUnlock(m_pWinInfo);
1835 return;
1836 }
1837
1838 VBOXVR_SCR_COMPOSITOR TmpCompositor;
1839
1840 if (pCompositor)
1841 {
1842 if (!m_pSharedGLCtx)
1843 {
1844 Assert(!m_fDataVisible);
1845 Assert(!m_fCleanupNeeded);
1846 renderspuVBoxCompositorRelease(m_pWinInfo);
1847 if (![self vboxSharedCtxCreate])
1848 {
1849 DEBUG_WARN(("vboxSharedCtxCreate failed\n"));
1850 return;
1851 }
1852
1853 Assert(m_pSharedGLCtx);
1854
1855 pCompositor = renderspuVBoxCompositorAcquire(m_pWinInfo);
1856 Assert(!m_fDataVisible);
1857 Assert(!m_fCleanupNeeded);
1858 if (!pCompositor)
1859 return;
1860 }
1861 }
1862 else
1863 {
1864 DEBUG_MSG(("My[%p]: NeedCleanup\n", self));
1865 Assert(m_fCleanupNeeded);
1866 CrVrScrCompositorInit(&TmpCompositor, NULL);
1867 pCompositor = &TmpCompositor;
1868 }
1869
1870 if ([self lockFocusIfCanDraw])
1871 {
1872 [self vboxPresent:pCompositor];
1873 [self unlockFocus];
1874 }
1875 else if (!m_pWinInfo->visible)
1876 {
1877 DEBUG_MSG(("My[%p]: NotVisible\n", self));
1878 m_fCleanupNeeded = false;
1879 }
1880 else
1881 {
1882 DEBUG_MSG(("My[%p]: Reschedule\n", self));
1883 [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(vboxTryDrawUI) userInfo:nil repeats:NO];
1884 }
1885
1886 renderspuVBoxCompositorUnlock(m_pWinInfo);
1887}
1888
1889- (void)swapFBO
1890{
1891 DEBUG_FUNC_ENTER();
1892 [m_pGLCtx flushBuffer];
1893 DEBUG_FUNC_LEAVE();
1894}
1895
1896- (void)vboxPresent:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
1897{
1898 VBOX_CR_RENDER_CTX_INFO CtxInfo;
1899
1900 DEBUG_MSG(("OVIW(%p): renderFBOToView\n", (void*)self));
1901
1902 Assert(pCompositor);
1903
1904 vboxCtxEnter(m_pSharedGLCtx, &CtxInfo);
1905
1906 [self vboxPresentCS:pCompositor];
1907
1908 vboxCtxLeave(&CtxInfo);
1909}
1910
1911- (void)vboxPresentCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
1912{
1913 {
1914 if ([m_pSharedGLCtx view] != self)
1915 {
1916 DEBUG_MSG(("OVIW(%p): not current view of shared ctx! Switching ...\n", (void*)self));
1917 [m_pSharedGLCtx setView: self];
1918 m_fNeedViewportUpdate = true;
1919 }
1920
1921 if (m_fNeedViewportUpdate)
1922 {
1923 [self updateViewportCS];
1924 m_fNeedViewportUpdate = false;
1925 }
1926
1927 m_fCleanupNeeded = false;
1928
1929 /* Render FBO content to the dock tile when necessary. */
1930 [self vboxPresentToDockTileCS:pCompositor];
1931 /* change to #if 0 to see thumbnail image */
1932#if 1
1933 [self vboxPresentToViewCS:pCompositor];
1934#else
1935 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1936 [m_pSharedGLCtx flushBuffer];
1937#endif
1938
1939 }
1940}
1941
1942DECLINLINE(void) vboxNSRectToRect(const NSRect *pR, RTRECT *pRect)
1943{
1944 pRect->xLeft = (int)pR->origin.x;
1945 pRect->yTop = (int)pR->origin.y;
1946 pRect->xRight = (int)(pR->origin.x + pR->size.width);
1947 pRect->yBottom = (int)(pR->origin.y + pR->size.height);
1948}
1949
1950DECLINLINE(void) vboxNSRectToRectUnstretched(const NSRect *pR, RTRECT *pRect, float xStretch, float yStretch)
1951{
1952 pRect->xLeft = (int)(pR->origin.x / xStretch);
1953 pRect->yTop = (int)(pR->origin.y / yStretch);
1954 pRect->xRight = (int)((pR->origin.x + pR->size.width) / xStretch);
1955 pRect->yBottom = (int)((pR->origin.y + pR->size.height) / yStretch);
1956}
1957
1958DECLINLINE(void) vboxNSRectToRectStretched(const NSRect *pR, RTRECT *pRect, float xStretch, float yStretch)
1959{
1960 pRect->xLeft = (int)(pR->origin.x * xStretch);
1961 pRect->yTop = (int)(pR->origin.y * yStretch);
1962 pRect->xRight = (int)((pR->origin.x + pR->size.width) * xStretch);
1963 pRect->yBottom = (int)((pR->origin.y + pR->size.height) * yStretch);
1964}
1965
1966- (void)vboxPresentToViewCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
1967{
1968 NSRect r = [self frame];
1969 float xStretch, yStretch;
1970 DEBUG_MSG(("OVIW(%p): rF2V frame: [%i, %i, %i, %i]\n", (void*)self, (int)r.origin.x, (int)r.origin.y, (int)r.size.width, (int)r.size.height));
1971
1972#if 1 /* Set to 0 to see the docktile instead of the real output */
1973 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR CIter;
1974 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
1975
1976 CrVrScrCompositorConstIterInit(pCompositor, &CIter);
1977
1978 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1979 glDrawBuffer(GL_BACK);
1980
1981 /* Clear background to transparent */
1982 glClear(GL_COLOR_BUFFER_BIT);
1983
1984 m_fDataVisible = false;
1985
1986 CrVrScrCompositorGetStretching(pCompositor, &xStretch, &yStretch);
1987
1988 while ((pEntry = CrVrScrCompositorConstIterNext(&CIter)) != NULL)
1989 {
1990 uint32_t cRegions;
1991 const RTRECT *paSrcRegions, *paDstRegions;
1992 int rc = CrVrScrCompositorEntryRegionsGet(pCompositor, pEntry, &cRegions, &paSrcRegions, &paDstRegions, NULL);
1993 uint32_t fFlags = CrVrScrCompositorEntryFlagsCombinedGet(pCompositor, pEntry);
1994 if (RT_SUCCESS(rc))
1995 {
1996 uint32_t i;
1997 int rc = CrBltEnter(m_pBlitter);
1998 if (RT_SUCCESS(rc))
1999 {
2000 for (i = 0; i < cRegions; ++i)
2001 {
2002 const RTRECT * pSrcRect = &paSrcRegions[i];
2003 const RTRECT * pDstRect = &paDstRegions[i];
2004 RTRECT DstRect, RestrictDstRect;
2005 RTRECT SrcRect, RestrictSrcRect;
2006
2007 vboxNSRectToRect(&m_RootRect, &RestrictDstRect);
2008 VBoxRectIntersected(&RestrictDstRect, pDstRect, &DstRect);
2009
2010 if (VBoxRectIsZero(&DstRect))
2011 continue;
2012
2013 VBoxRectTranslate(&DstRect, -RestrictDstRect.xLeft, -RestrictDstRect.yTop);
2014
2015 vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch, yStretch);
2016 VBoxRectTranslate(&RestrictSrcRect, -CrVrScrCompositorEntryRectGet(pEntry)->xLeft, -CrVrScrCompositorEntryRectGet(pEntry)->yTop);
2017 VBoxRectIntersected(&RestrictSrcRect, pSrcRect, &SrcRect);
2018
2019 if (VBoxRectIsZero(&SrcRect))
2020 continue;
2021
2022 pSrcRect = &SrcRect;
2023 pDstRect = &DstRect;
2024
2025 const CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
2026
2027 CrBltBlitTexMural(m_pBlitter, true, CrTdTexGet(pTexData), pSrcRect, pDstRect, 1, fFlags | CRBLT_F_NOALPHA);
2028
2029 m_fDataVisible = true;
2030 }
2031 CrBltLeave(m_pBlitter);
2032 }
2033 else
2034 {
2035 DEBUG_WARN(("CrBltEnter failed rc %d", rc));
2036 }
2037 }
2038 else
2039 {
2040 Assert(0);
2041 DEBUG_MSG_1(("BlitStretched: CrVrScrCompositorEntryRegionsGet failed rc %d\n", rc));
2042 }
2043 }
2044#endif
2045 /*
2046 glFinish();
2047 */
2048 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
2049 [m_pSharedGLCtx flushBuffer];
2050}
2051
2052- (void)presentComposition:(const VBOXVR_SCR_COMPOSITOR_ENTRY*)pChangedEntry
2053{
2054 [self vboxTryDraw];
2055}
2056
2057- (void)vboxBlitterSyncWindow
2058{
2059 CR_BLITTER_WINDOW WinInfo;
2060 NSRect r;
2061
2062 if (!m_pBlitter)
2063 return;
2064
2065 memset(&WinInfo, 0, sizeof (WinInfo));
2066
2067 r = [self frame];
2068 WinInfo.width = r.size.width;
2069 WinInfo.height = r.size.height;
2070
2071 Assert(WinInfo.width == m_RootRect.size.width);
2072 Assert(WinInfo.height == m_RootRect.size.height);
2073
2074 /*CrBltMuralSetCurrentInfo(m_pBlitter, NULL);*/
2075
2076 CrBltMuralSetCurrentInfo(m_pBlitter, &WinInfo);
2077 CrBltCheckUpdateViewport(m_pBlitter);
2078}
2079
2080#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
2081static int g_cVBoxTgaCtr = 0;
2082#endif
2083- (void)vboxPresentToDockTileCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
2084{
2085 NSRect r = [self frame];
2086 NSRect rr = NSZeroRect;
2087 GLint i = 0;
2088 NSDockTile *pDT = nil;
2089 float xStretch, yStretch;
2090
2091 if ([m_DockTileView thumbBitmap] != nil)
2092 {
2093 /* Only update after at least 200 ms, cause glReadPixels is
2094 * heavy performance wise. */
2095 uint64_t uiNewTime = RTTimeMilliTS();
2096 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR CIter;
2097 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
2098
2099 if (uiNewTime - m_uiDockUpdateTime > 200)
2100 {
2101 m_uiDockUpdateTime = uiNewTime;
2102#if 0
2103 /* todo: check this for optimization */
2104 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myTextureName);
2105 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_STORAGE_HINT_APPLE,
2106 GL_STORAGE_SHARED_APPLE);
2107 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2108 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
2109 sizex, sizey, 0, GL_BGRA,
2110 GL_UNSIGNED_INT_8_8_8_8_REV, myImagePtr);
2111 glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
2112 0, 0, 0, 0, 0, image_width, image_height);
2113 glFlush();
2114 /* Do other work processing here, using a double or triple buffer */
2115 glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA,
2116 GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
2117#endif
2118 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
2119 glDrawBuffer(GL_BACK);
2120
2121 /* Clear background to transparent */
2122 glClear(GL_COLOR_BUFFER_BIT);
2123
2124 rr = [m_DockTileView frame];
2125
2126 CrVrScrCompositorGetStretching(pCompositor, &xStretch, &yStretch);
2127
2128 CrVrScrCompositorConstIterInit(pCompositor, &CIter);
2129 while ((pEntry = CrVrScrCompositorConstIterNext(&CIter)) != NULL)
2130 {
2131 uint32_t cRegions;
2132 const RTRECT *paSrcRegions, *paDstRegions;
2133 int rc = CrVrScrCompositorEntryRegionsGet(pCompositor, pEntry, &cRegions, &paSrcRegions, &paDstRegions, NULL);
2134 uint32_t fFlags = CrVrScrCompositorEntryFlagsCombinedGet(pCompositor, pEntry);
2135 if (RT_SUCCESS(rc))
2136 {
2137 uint32_t i;
2138 int rc = CrBltEnter(m_pBlitter);
2139 if (RT_SUCCESS(rc))
2140 {
2141 for (i = 0; i < cRegions; ++i)
2142 {
2143 const RTRECT * pSrcRect = &paSrcRegions[i];
2144 const RTRECT * pDstRect = &paDstRegions[i];
2145 RTRECT SrcRect, DstRect, RestrictSrcRect, RestrictDstRect;
2146
2147 vboxNSRectToRect(&m_RootRect, &RestrictDstRect);
2148 VBoxRectIntersected(&RestrictDstRect, pDstRect, &DstRect);
2149
2150 VBoxRectTranslate(&DstRect, -RestrictDstRect.xLeft, -RestrictDstRect.yTop);
2151
2152 VBoxRectScale(&DstRect, m_FBOThumbScaleX, m_FBOThumbScaleY);
2153
2154 if (VBoxRectIsZero(&DstRect))
2155 continue;
2156
2157 vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch, yStretch);
2158 VBoxRectTranslate(&RestrictSrcRect, -CrVrScrCompositorEntryRectGet(pEntry)->xLeft, -CrVrScrCompositorEntryRectGet(pEntry)->yTop);
2159 VBoxRectIntersected(&RestrictSrcRect, pSrcRect, &SrcRect);
2160
2161 if (VBoxRectIsZero(&SrcRect))
2162 continue;
2163
2164 pSrcRect = &SrcRect;
2165 pDstRect = &DstRect;
2166
2167 const CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
2168
2169 CrBltBlitTexMural(m_pBlitter, true, CrTdTexGet(pTexData), pSrcRect, pDstRect, 1, fFlags);
2170 }
2171 CrBltLeave(m_pBlitter);
2172 }
2173 else
2174 {
2175 DEBUG_WARN(("CrBltEnter failed rc %d", rc));
2176 }
2177 }
2178 else
2179 {
2180 Assert(0);
2181 DEBUG_MSG_1(("BlitStretched: CrVrScrCompositorEntryRegionsGet failed rc %d\n", rc));
2182 }
2183 }
2184
2185 glFinish();
2186
2187 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
2188 glReadBuffer(GL_BACK);
2189 /* Here the magic of reading the FBO content in our own buffer
2190 * happens. We have to lock this access, in the case the dock
2191 * is updated currently. */
2192 [m_DockTileView lock];
2193 glReadPixels(0, m_RootRect.size.height - rr.size.height, rr.size.width, rr.size.height,
2194 GL_BGRA,
2195 GL_UNSIGNED_INT_8_8_8_8,
2196 [[m_DockTileView thumbBitmap] bitmapData]);
2197 [m_DockTileView unlock];
2198
2199#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
2200 ++g_cVBoxTgaCtr;
2201 crDumpNamedTGAF((GLint)rr.size.width, (GLint)rr.size.height,
2202 [[m_DockTileView thumbBitmap] bitmapData], "/Users/leo/vboxdumps/dump%d.tga", g_cVBoxTgaCtr);
2203#endif
2204
2205 pDT = [[NSApplication sharedApplication] dockTile];
2206
2207 /* Send a display message to the dock tile in the main thread */
2208 [[[NSApplication sharedApplication] dockTile] performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:NO];
2209 }
2210 }
2211}
2212
2213- (void)clearVisibleRegions
2214{
2215 DEBUG_FUNC_ENTER();
2216 if(m_paClipRects)
2217 {
2218 RTMemFree(m_paClipRects);
2219 m_paClipRects = NULL;
2220 }
2221 m_cClipRects = 0;
2222 DEBUG_FUNC_LEAVE();
2223}
2224
2225- (GLboolean)vboxNeedsEmptyPresent
2226{
2227 if (m_fDataVisible)
2228 {
2229 m_fCleanupNeeded = true;
2230 return GL_TRUE;
2231 }
2232
2233 return GL_FALSE;
2234}
2235
2236- (void)setVisibleRegions:(GLint)cRects paRects:(const GLint*)paRects
2237{
2238 DEBUG_FUNC_ENTER();
2239 GLint cOldRects = m_cClipRects;
2240
2241 DEBUG_MSG_1(("OVIW(%p): setVisibleRegions: cRects=%d\n", (void*)self, cRects));
2242
2243 [self clearVisibleRegions];
2244
2245 if (cRects > 0)
2246 {
2247#ifdef DEBUG_poetzsch
2248 int i =0;
2249 for (i = 0; i < cRects; ++i)
2250 DEBUG_MSG_1(("OVIW(%p): setVisibleRegions: %d - %d %d %d %d\n", (void*)self, i, paRects[i * 4], paRects[i * 4 + 1], paRects[i * 4 + 2], paRects[i * 4 + 3]));
2251#endif
2252
2253 m_paClipRects = (GLint*)RTMemAlloc(sizeof(GLint) * 4 * cRects);
2254 m_cClipRects = cRects;
2255 memcpy(m_paClipRects, paRects, sizeof(GLint) * 4 * cRects);
2256 }
2257
2258 DEBUG_FUNC_LEAVE();
2259}
2260
2261- (NSView*)dockTileScreen
2262{
2263 DEBUG_FUNC_ENTER();
2264 NSView *contentView = [[[NSApplication sharedApplication] dockTile] contentView];
2265 NSView *screenContent = nil;
2266 /* First try the new variant which checks if this window is within the
2267 screen which is previewed in the dock. */
2268 if ([contentView respondsToSelector:@selector(screenContentWithParentView:)])
2269 screenContent = [contentView performSelector:@selector(screenContentWithParentView:) withObject:(id)m_pParentView];
2270 /* If it fails, fall back to the old variant (VBox...) */
2271 else if ([contentView respondsToSelector:@selector(screenContent)])
2272 screenContent = [contentView performSelector:@selector(screenContent)];
2273
2274 DEBUG_FUNC_LEAVE();
2275 return screenContent;
2276}
2277
2278- (void)reshapeDockTile
2279{
2280 DEBUG_FUNC_ENTER();
2281 NSRect newFrame = NSZeroRect;
2282
2283 NSView *pView = [self dockTileScreen];
2284 if (pView != nil)
2285 {
2286 NSRect dockFrame = [pView frame];
2287 /* todo: this is not correct, we should use framebuffer size here, while parent view frame size may differ in case of scrolling */
2288 NSRect parentFrame = [m_pParentView frame];
2289
2290 m_FBOThumbScaleX = (float)dockFrame.size.width / parentFrame.size.width;
2291 m_FBOThumbScaleY = (float)dockFrame.size.height / parentFrame.size.height;
2292 newFrame = NSMakeRect((int)(m_Pos.x * m_FBOThumbScaleX), (int)(dockFrame.size.height - (m_Pos.y + m_Size.height - m_yInvRootOffset) * m_FBOThumbScaleY), (int)(m_Size.width * m_FBOThumbScaleX), (int)(m_Size.height * m_FBOThumbScaleY));
2293 /*
2294 NSRect newFrame = NSMakeRect ((int)roundf(m_Pos.x * m_FBOThumbScaleX), (int)roundf(dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (int)roundf(m_Size.width * m_FBOThumbScaleX), (int)roundf(m_Size.height * m_FBOThumbScaleY));
2295 NSRect newFrame = NSMakeRect ((m_Pos.x * m_FBOThumbScaleX), (dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (m_Size.width * m_FBOThumbScaleX), (m_Size.height * m_FBOThumbScaleY));
2296 printf ("%f %f %f %f - %f %f\n", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height, m_Size.height, m_FBOThumbScaleY);
2297 */
2298 [m_DockTileView setFrame: newFrame];
2299 }
2300 DEBUG_FUNC_LEAVE();
2301}
2302
2303@end
2304
2305/********************************************************************************
2306*
2307* OpenGL context management
2308*
2309********************************************************************************/
2310void cocoaGLCtxCreate(NativeNSOpenGLContextRef *ppCtx, GLbitfield fVisParams, NativeNSOpenGLContextRef pSharedCtx)
2311{
2312 DEBUG_FUNC_ENTER();
2313 NSOpenGLPixelFormat *pFmt = nil;
2314
2315 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2316
2317 NSOpenGLPixelFormatAttribute attribs[24] =
2318 {
2319 NSOpenGLPFAWindow,
2320 NSOpenGLPFAAccelerated,
2321 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24
2322 };
2323
2324 int i = 4;
2325
2326 if (fVisParams & CR_ALPHA_BIT)
2327 {
2328 DEBUG_MSG(("CR_ALPHA_BIT requested\n"));
2329 attribs[i++] = NSOpenGLPFAAlphaSize;
2330 attribs[i++] = 8;
2331 }
2332 if (fVisParams & CR_DEPTH_BIT)
2333 {
2334 DEBUG_MSG(("CR_DEPTH_BIT requested\n"));
2335 attribs[i++] = NSOpenGLPFADepthSize;
2336 attribs[i++] = 24;
2337 }
2338 if (fVisParams & CR_STENCIL_BIT)
2339 {
2340 DEBUG_MSG(("CR_STENCIL_BIT requested\n"));
2341 attribs[i++] = NSOpenGLPFAStencilSize;
2342 attribs[i++] = 8;
2343 }
2344 if (fVisParams & CR_ACCUM_BIT)
2345 {
2346 DEBUG_MSG(("CR_ACCUM_BIT requested\n"));
2347 attribs[i++] = NSOpenGLPFAAccumSize;
2348 if (fVisParams & CR_ALPHA_BIT)
2349 attribs[i++] = 32;
2350 else
2351 attribs[i++] = 24;
2352 }
2353 if (fVisParams & CR_MULTISAMPLE_BIT)
2354 {
2355 DEBUG_MSG(("CR_MULTISAMPLE_BIT requested\n"));
2356 attribs[i++] = NSOpenGLPFASampleBuffers;
2357 attribs[i++] = 1;
2358 attribs[i++] = NSOpenGLPFASamples;
2359 attribs[i++] = 4;
2360 }
2361 if (fVisParams & CR_DOUBLE_BIT)
2362 {
2363 DEBUG_MSG(("CR_DOUBLE_BIT requested\n"));
2364 attribs[i++] = NSOpenGLPFADoubleBuffer;
2365 }
2366 if (fVisParams & CR_STEREO_BIT)
2367 {
2368 /* We don't support that.
2369 DEBUG_MSG(("CR_STEREO_BIT requested\n"));
2370 attribs[i++] = NSOpenGLPFAStereo;
2371 */
2372 }
2373
2374 /* Mark the end */
2375 attribs[i++] = 0;
2376
2377 /* Choose a pixel format */
2378 pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
2379
2380 if (pFmt)
2381 {
2382 *ppCtx = [[OverlayOpenGLContext alloc] initWithFormat:pFmt shareContext:pSharedCtx];
2383
2384 /* Enable multi threaded OpenGL engine */
2385 /*
2386 CGLContextObj cglCtx = [*ppCtx CGLContextObj];
2387 CGLError err = CGLEnable(cglCtx, kCGLCEMPEngine);
2388 if (err != kCGLNoError)
2389 printf ("Couldn't enable MT OpenGL engine!\n");
2390 */
2391
2392 DEBUG_MSG(("New context %X\n", (uint)*ppCtx));
2393 }
2394
2395 [pPool release];
2396
2397 DEBUG_FUNC_LEAVE();
2398}
2399
2400void cocoaGLCtxDestroy(NativeNSOpenGLContextRef pCtx)
2401{
2402 DEBUG_FUNC_ENTER();
2403 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2404
2405 [pCtx release];
2406 /*[pCtx performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];*/
2407
2408 [pPool release];
2409 DEBUG_FUNC_LEAVE();
2410}
2411
2412/********************************************************************************
2413*
2414* View management
2415*
2416********************************************************************************/
2417typedef struct CR_RCD_CREATEVIEW
2418{
2419 WindowInfo *pWinInfo;
2420 NSView *pParentView;
2421 GLbitfield fVisParams;
2422 /* out */
2423 OverlayView *pView;
2424} CR_RCD_CREATEVIEW;
2425
2426static OverlayView * vboxViewCreate(WindowInfo *pWinInfo, NativeNSViewRef pParentView)
2427{
2428 DEBUG_FUNC_ENTER();
2429 /* Create our worker view */
2430 OverlayView* pView = [[OverlayView alloc] initWithFrame:NSZeroRect thread:RTThreadSelf() parentView:pParentView winInfo:pWinInfo];
2431
2432 if (pView)
2433 {
2434 /* We need a real window as container for the view */
2435 [[OverlayWindow alloc] initWithParentView:pParentView overlayView:pView];
2436 /* Return the freshly created overlay view */
2437 DEBUG_FUNC_LEAVE();
2438 return pView;
2439 }
2440
2441 DEBUG_FUNC_LEAVE();
2442 return NULL;
2443}
2444
2445static DECLCALLBACK(void) vboxRcdCreateView(void *pvCb)
2446{
2447 DEBUG_FUNC_ENTER();
2448 CR_RCD_CREATEVIEW * pCreateView = (CR_RCD_CREATEVIEW*)pvCb;
2449 pCreateView->pView = vboxViewCreate(pCreateView->pWinInfo, pCreateView->pParentView);
2450 DEBUG_FUNC_LEAVE();
2451}
2452
2453void cocoaViewCreate(NativeNSViewRef *ppView, WindowInfo *pWinInfo, NativeNSViewRef pParentView, GLbitfield fVisParams)
2454{
2455 DEBUG_FUNC_ENTER();
2456 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2457
2458 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
2459 /* make sure all tasks are run, to preserve the order */
2460 [pRunner runTasksSyncIfPossible];
2461
2462 if (renderspuCalloutAvailable())
2463 {
2464 CR_RCD_CREATEVIEW CreateView;
2465 CreateView.pWinInfo = pWinInfo;
2466 CreateView.pParentView = pParentView;
2467 CreateView.fVisParams = fVisParams;
2468 CreateView.pView = NULL;
2469 renderspuCalloutClient(vboxRcdCreateView, &CreateView);
2470 *ppView = CreateView.pView;
2471 }
2472 else
2473 {
2474 DEBUG_MSG(("no callout available on createWindow\n"));
2475#if 0
2476 dispatch_sync(dispatch_get_main_queue(), ^{
2477#endif
2478 *ppView = vboxViewCreate(pWinInfo, pParentView);
2479#if 0
2480 });
2481#endif
2482 }
2483
2484 [pPool release];
2485
2486 DEBUG_FUNC_LEAVE();
2487}
2488
2489void cocoaViewReparent(NativeNSViewRef pView, NativeNSViewRef pParentView)
2490{
2491 DEBUG_FUNC_ENTER();
2492 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2493
2494 OverlayView* pOView = (OverlayView*)pView;
2495
2496 if (pOView)
2497 {
2498 [pOView vboxReparent:pParentView];
2499 }
2500
2501 [pPool release];
2502
2503 DEBUG_FUNC_LEAVE();
2504}
2505
2506typedef struct CR_RCD_DESTROYVIEW
2507{
2508 OverlayView *pView;
2509} CR_RCD_DESTROYVIEW;
2510
2511static DECLCALLBACK(void) vboxRcdDestroyView(void *pvCb)
2512{
2513 DEBUG_FUNC_ENTER();
2514 CR_RCD_DESTROYVIEW * pDestroyView = (CR_RCD_DESTROYVIEW*)pvCb;
2515 [pDestroyView->pView vboxDestroy];
2516 DEBUG_FUNC_LEAVE();
2517}
2518
2519void cocoaViewDestroy(NativeNSViewRef pView)
2520{
2521 DEBUG_FUNC_ENTER();
2522 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2523
2524 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
2525 [pRunner addObj:pView selector:@selector(vboxDestroy) arg:nil];
2526
2527 [pPool release];
2528
2529 DEBUG_FUNC_LEAVE();
2530}
2531
2532void cocoaViewShow(NativeNSViewRef pView, GLboolean fShowIt)
2533{
2534 DEBUG_FUNC_ENTER();
2535 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2536
2537 [(OverlayView*)pView vboxSetVisible:fShowIt];
2538
2539 [pPool release];
2540 DEBUG_FUNC_LEAVE();
2541}
2542
2543void cocoaViewDisplay(NativeNSViewRef pView)
2544{
2545 DEBUG_FUNC_ENTER();
2546 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2547
2548 DEBUG_WARN(("cocoaViewDisplay should never happen!\n"));
2549 DEBUG_MSG_1(("cocoaViewDisplay %p\n", (void*)pView));
2550 [(OverlayView*)pView swapFBO];
2551
2552 [pPool release];
2553
2554 DEBUG_FUNC_LEAVE();
2555}
2556
2557void cocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
2558{
2559 DEBUG_FUNC_ENTER();
2560 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2561
2562 [(OverlayView*)pView vboxSetPos:NSMakePoint(x, y)];
2563
2564 [pPool release];
2565
2566 DEBUG_FUNC_LEAVE();
2567}
2568
2569void cocoaViewSetSize(NativeNSViewRef pView, int w, int h)
2570{
2571 DEBUG_FUNC_ENTER();
2572 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2573
2574 [(OverlayView*)pView vboxSetSize:NSMakeSize(w, h)];
2575
2576 [pPool release];
2577
2578 DEBUG_FUNC_LEAVE();
2579}
2580
2581typedef struct CR_RCD_GETGEOMETRY
2582{
2583 OverlayView *pView;
2584 NSRect rect;
2585} CR_RCD_GETGEOMETRY;
2586
2587static DECLCALLBACK(void) vboxRcdGetGeomerty(void *pvCb)
2588{
2589 DEBUG_FUNC_ENTER();
2590 CR_RCD_GETGEOMETRY * pGetGeometry = (CR_RCD_GETGEOMETRY*)pvCb;
2591 pGetGeometry->rect = [[pGetGeometry->pView window] frame];
2592 DEBUG_FUNC_LEAVE();
2593}
2594
2595void cocoaViewGetGeometry(NativeNSViewRef pView, int *pX, int *pY, int *pW, int *pH)
2596{
2597 DEBUG_FUNC_ENTER();
2598 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2599
2600 NSRect frame;
2601 VBoxMainThreadTaskRunner *pRunner = [VBoxMainThreadTaskRunner globalInstance];
2602 /* make sure all tasks are run, to preserve the order */
2603 [pRunner runTasksSyncIfPossible];
2604
2605
2606 if (renderspuCalloutAvailable())
2607 {
2608 CR_RCD_GETGEOMETRY GetGeometry;
2609 GetGeometry.pView = (OverlayView*)pView;
2610 renderspuCalloutClient(vboxRcdGetGeomerty, &GetGeometry);
2611 frame = GetGeometry.rect;
2612 }
2613 else
2614 {
2615 DEBUG_MSG(("no callout available on getGeometry\n"));
2616 frame = [[pView window] frame];
2617 }
2618
2619 *pX = frame.origin.x;
2620 *pY = frame.origin.y;
2621 *pW = frame.size.width;
2622 *pH = frame.size.height;
2623
2624 [pPool release];
2625
2626 DEBUG_FUNC_LEAVE();
2627}
2628
2629void cocoaViewPresentComposition(NativeNSViewRef pView, const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry)
2630{
2631 DEBUG_FUNC_ENTER();
2632 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2633 NSOpenGLContext *pCtx;
2634
2635 /* view should not necesserily have a context set */
2636 pCtx = [(OverlayView*)pView glCtx];
2637 if (!pCtx)
2638 {
2639 ContextInfo * pCtxInfo = renderspuDefaultSharedContextAcquire();
2640 if (!pCtxInfo)
2641 {
2642 DEBUG_WARN(("renderspuDefaultSharedContextAcquire returned NULL"));
2643
2644 [pPool release];
2645 DEBUG_FUNC_LEAVE();
2646 return;
2647 }
2648
2649 pCtx = pCtxInfo->context;
2650
2651 [(OverlayView*)pView setGLCtx:pCtx];
2652 }
2653
2654 [(OverlayView*)pView presentComposition:pChangedEntry];
2655
2656 [pPool release];
2657
2658 DEBUG_FUNC_LEAVE();
2659}
2660
2661void cocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
2662{
2663 DEBUG_FUNC_ENTER();
2664 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2665
2666 DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", (void*)pView, (void*)pCtx));
2667
2668 if (pView)
2669 {
2670 [(OverlayView*)pView setGLCtx:pCtx];
2671 [(OverlayView*)pView makeCurrentFBO];
2672 }
2673 else
2674 {
2675 [NSOpenGLContext clearCurrentContext];
2676 }
2677
2678 [pPool release];
2679
2680 DEBUG_FUNC_LEAVE();
2681}
2682
2683GLboolean cocoaViewNeedsEmptyPresent(NativeNSViewRef pView)
2684{
2685 DEBUG_FUNC_ENTER();
2686
2687 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2688
2689 GLboolean fNeedsPresent = [(OverlayView*)pView vboxNeedsEmptyPresent];
2690
2691 [pPool release];
2692
2693 DEBUG_FUNC_LEAVE();
2694
2695 return fNeedsPresent;
2696}
2697
2698void cocoaViewSetVisibleRegion(NativeNSViewRef pView, GLint cRects, const GLint* paRects)
2699{
2700 DEBUG_FUNC_ENTER();
2701 NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
2702
2703 [(OverlayView*)pView setVisibleRegions:cRects paRects:paRects];
2704
2705 [pPool release];
2706
2707 DEBUG_FUNC_LEAVE();
2708}
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