VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c@ 51082

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

crOpenGL/OSX: bugfixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1/** @file
2 * VirtualBox OpenGL Cocoa Window System implementation
3 */
4
5/*
6 * Copyright (C) 2009-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include <OpenGL/OpenGL.h>
18
19#include "renderspu.h"
20#include <iprt/process.h>
21#include <iprt/string.h>
22#include <iprt/path.h>
23
24#include <cr_string.h>
25#include <cr_mem.h>
26
27GLboolean renderspu_SystemInitVisual(VisualInfo *pVisInfo)
28{
29 CRASSERT(pVisInfo);
30
31/* cocoaGLVisualCreate(&pCtxInfo->context);*/
32
33 return GL_TRUE;
34}
35
36GLboolean renderspu_SystemCreateContext(VisualInfo *pVisInfo, ContextInfo *pCtxInfo, ContextInfo *pSharedCtxInfo)
37{
38 CRASSERT(pVisInfo);
39 CRASSERT(pCtxInfo);
40
41 pCtxInfo->currentWindow = NULL;
42
43 cocoaGLCtxCreate(&pCtxInfo->context, pVisInfo->visAttribs, pSharedCtxInfo ? pSharedCtxInfo->context : NULL);
44
45 return GL_TRUE;
46}
47
48void renderspu_SystemDestroyContext(ContextInfo *pCtxInfo)
49{
50 if(!pCtxInfo)
51 return;
52
53 if(pCtxInfo->context)
54 {
55 cocoaGLCtxDestroy(pCtxInfo->context);
56 pCtxInfo->context = NULL;
57 }
58}
59
60void renderspuFullscreen(WindowInfo *pWinInfo, GLboolean fFullscreen)
61{
62 /* Real fullscreen isn't supported by VirtualBox */
63}
64
65GLboolean renderspu_SystemVBoxCreateWindow(VisualInfo *pVisInfo, GLboolean fShowIt, WindowInfo *pWinInfo)
66{
67 CRASSERT(pVisInfo);
68 CRASSERT(pWinInfo);
69
70 /* VirtualBox is the only frontend which support 3D right now. */
71 char pszName[256];
72 if (RTProcGetExecutablePath(pszName, sizeof(pszName)))
73 /* Check for VirtualBox and VirtualBoxVM */
74 if (RTStrNICmp(RTPathFilename(pszName), "VirtualBox", 10) != 0)
75 return GL_FALSE;
76
77 pWinInfo->visual = pVisInfo;
78 pWinInfo->window = NULL;
79 pWinInfo->nativeWindow = NULL;
80 pWinInfo->currentCtx = NULL;
81
82#ifdef __LP64__
83 NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
84#else /* __LP64__ */
85 NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
86#endif /* __LP64__ */
87
88 cocoaViewCreate(&pWinInfo->window, pWinInfo, pParentWin, pVisInfo->visAttribs);
89
90 if (fShowIt)
91 renderspu_SystemShowWindow(pWinInfo, fShowIt);
92
93 return GL_TRUE;
94}
95
96void renderspu_SystemReparentWindow(WindowInfo *pWinInfo)
97{
98#ifdef __LP64__
99 NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
100#else /* __LP64__ */
101 NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
102#endif /* __LP64__ */
103 cocoaViewReparent(pWinInfo->window, pParentWin);
104}
105
106void renderspu_SystemDestroyWindow(WindowInfo *pWinInfo)
107{
108 CRASSERT(pWinInfo);
109
110 cocoaViewDestroy(pWinInfo->window);
111}
112
113void renderspu_SystemWindowPosition(WindowInfo *pWinInfo, GLint x, GLint y)
114{
115 CRASSERT(pWinInfo);
116
117#ifdef __LP64__
118 NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
119#else /* __LP64__ */
120 NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
121#endif /* __LP64__ */
122
123 /*pParentWin is unused in the call, otherwise it might hold incorrect value if for ex. last reparent call was for
124 a different screen*/
125 cocoaViewSetPosition(pWinInfo->window, pParentWin, x, y);
126}
127
128void renderspu_SystemWindowSize(WindowInfo *pWinInfo, GLint w, GLint h)
129{
130 CRASSERT(pWinInfo);
131
132 cocoaViewSetSize(pWinInfo->window, w, h);
133}
134
135void renderspu_SystemGetWindowGeometry(WindowInfo *pWinInfo, GLint *pX, GLint *pY, GLint *pW, GLint *pH)
136{
137 CRASSERT(pWinInfo);
138
139 cocoaViewGetGeometry(pWinInfo->window, pX, pY, pW, pH);
140}
141
142void renderspu_SystemGetMaxWindowSize(WindowInfo *pWinInfo, GLint *pW, GLint *pH)
143{
144 CRASSERT(pWinInfo);
145
146 *pW = 10000;
147 *pH = 10000;
148}
149
150void renderspu_SystemShowWindow(WindowInfo *pWinInfo, GLboolean fShowIt)
151{
152 CRASSERT(pWinInfo);
153
154 cocoaViewShow(pWinInfo->window, fShowIt);
155}
156
157void renderspu_SystemVBoxPresentComposition( WindowInfo *window, const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry )
158{
159 cocoaViewPresentComposition(window->window, pChangedEntry);
160}
161
162void renderspu_SystemMakeCurrent(WindowInfo *pWinInfo, GLint nativeWindow, ContextInfo *pCtxInfo)
163{
164/* if(pWinInfo->visual != pCtxInfo->visual)*/
165/* printf ("visual mismatch .....................\n");*/
166
167 nativeWindow = 0;
168
169 if (pWinInfo && pCtxInfo)
170 cocoaViewMakeCurrentContext(pWinInfo->window, pCtxInfo->context);
171 else
172 cocoaViewMakeCurrentContext(NULL, NULL);
173}
174
175void renderspu_SystemSwapBuffers(WindowInfo *pWinInfo, GLint flags)
176{
177 CRASSERT(pWinInfo);
178
179 cocoaViewDisplay(pWinInfo->window);
180}
181
182void renderspu_SystemWindowVisibleRegion(WindowInfo *pWinInfo, GLint cRects, const GLint* paRects)
183{
184 CRASSERT(pWinInfo);
185
186 cocoaViewSetVisibleRegion(pWinInfo->window, cRects, paRects);
187}
188
189void renderspu_SystemWindowApplyVisibleRegion(WindowInfo *pWinInfo)
190{
191}
192
193int renderspu_SystemInit()
194{
195 return VINF_SUCCESS;
196}
197
198int renderspu_SystemTerm()
199{
200 CrGlslTerm(&render_spu.GlobalShaders);
201 return VINF_SUCCESS;
202}
203
204static SPUNamedFunctionTable * renderspuFindEntry(SPUNamedFunctionTable *aFunctions, const char *pcszName)
205{
206 SPUNamedFunctionTable *pCur;
207
208 for (pCur = aFunctions ; pCur->name != NULL ; pCur++)
209 {
210 if (!crStrcmp( pcszName, pCur->name ) )
211 {
212 return pCur;
213 }
214 }
215
216 AssertFailed();
217
218 return NULL;
219}
220
221typedef struct CR_RENDER_CTX_INFO
222{
223 ContextInfo * pContext;
224 WindowInfo * pWindow;
225} CR_RENDER_CTX_INFO;
226
227void renderspuCtxInfoInitCurrent(CR_RENDER_CTX_INFO *pInfo)
228{
229 GET_CONTEXT(pCurCtx);
230 pInfo->pContext = pCurCtx;
231 pInfo->pWindow = pCurCtx ? pCurCtx->currentWindow : NULL;
232}
233
234void renderspuCtxInfoRestoreCurrent(CR_RENDER_CTX_INFO *pInfo)
235{
236 GET_CONTEXT(pCurCtx);
237 if (pCurCtx == pInfo->pContext && (!pCurCtx || pCurCtx->currentWindow == pInfo->pWindow))
238 return;
239 renderspuPerformMakeCurrent(pInfo->pWindow, 0, pInfo->pContext);
240}
241
242GLboolean renderspuCtxSetCurrentWithAnyWindow(ContextInfo * pContext, CR_RENDER_CTX_INFO *pInfo)
243{
244 WindowInfo * window;
245 renderspuCtxInfoInitCurrent(pInfo);
246
247 if (pInfo->pContext == pContext)
248 return GL_TRUE;
249
250 window = pContext->currentWindow;
251 if (!window)
252 {
253 window = renderspuGetDummyWindow(pContext->BltInfo.Base.visualBits);
254 if (!window)
255 {
256 WARN(("renderspuGetDummyWindow failed"));
257 return GL_FALSE;
258 }
259 }
260
261 Assert(window);
262
263 renderspuPerformMakeCurrent(window, 0, pContext);
264 return GL_TRUE;
265}
266
267void renderspu_SystemDefaultSharedContextChanged(ContextInfo *fromContext, ContextInfo *toContext)
268{
269 CRASSERT(fromContext != toContext);
270
271 if (!CrGlslIsInited(&render_spu.GlobalShaders))
272 {
273 CrGlslInit(&render_spu.GlobalShaders, &render_spu.blitterDispatch);
274 }
275
276 if (fromContext)
277 {
278 if (CrGlslNeedsCleanup(&render_spu.GlobalShaders))
279 {
280 CR_RENDER_CTX_INFO Info;
281 if (renderspuCtxSetCurrentWithAnyWindow(fromContext, &Info))
282 {
283 CrGlslCleanup(&render_spu.GlobalShaders);
284 renderspuCtxInfoRestoreCurrent(&Info);
285 }
286 else
287 WARN(("renderspuCtxSetCurrentWithAnyWindow failed!"));
288 }
289 }
290 else
291 {
292 CRASSERT(!CrGlslNeedsCleanup(&render_spu.GlobalShaders));
293 }
294
295 CRASSERT(!CrGlslNeedsCleanup(&render_spu.GlobalShaders));
296
297 if (toContext)
298 {
299 CR_RENDER_CTX_INFO Info;
300 if (renderspuCtxSetCurrentWithAnyWindow(toContext, &Info))
301 {
302 int rc = CrGlslProgGenAllNoAlpha(&render_spu.GlobalShaders);
303 if (!RT_SUCCESS(rc))
304 WARN(("CrGlslProgGenAllNoAlpha failed, rc %d", rc));
305
306 renderspuCtxInfoRestoreCurrent(&Info);
307 }
308 else
309 crWarning("renderspuCtxSetCurrentWithAnyWindow failed!");
310 }
311}
312
313AssertCompile(sizeof (GLhandleARB) == sizeof (void*));
314
315static VBoxGLhandleARB crHndlSearchVBox(GLhandleARB hNative)
316{
317 CRASSERT(!(((uintptr_t)hNative) >> 32));
318 return (VBoxGLhandleARB)((uintptr_t)hNative);
319}
320
321static GLhandleARB crHndlSearchNative(VBoxGLhandleARB hVBox)
322{
323 return (GLhandleARB)((uintptr_t)hVBox);
324}
325
326static VBoxGLhandleARB crHndlAcquireVBox(GLhandleARB hNative)
327{
328 CRASSERT(!(((uintptr_t)hNative) >> 32));
329 return (VBoxGLhandleARB)((uintptr_t)hNative);
330}
331
332static GLhandleARB crHndlReleaseVBox(VBoxGLhandleARB hVBox)
333{
334 return (GLhandleARB)((uintptr_t)hVBox);
335}
336
337static void SPU_APIENTRY renderspu_SystemDeleteObjectARB(VBoxGLhandleARB obj)
338{
339 GLhandleARB hNative = crHndlReleaseVBox(obj);
340 if (!hNative)
341 {
342 crWarning("no native for %d", obj);
343 return;
344 }
345
346 render_spu.pfnDeleteObject(hNative);
347}
348
349static void SPU_APIENTRY renderspu_SystemGetAttachedObjectsARB( VBoxGLhandleARB containerObj, GLsizei maxCount, GLsizei * pCount, VBoxGLhandleARB * obj )
350{
351 GLhandleARB *paAttachments;
352 GLhandleARB hNative = crHndlSearchNative(containerObj);
353 GLsizei count, i;
354
355 if (pCount)
356 *pCount = 0;
357
358 if (!hNative)
359 {
360 crWarning("no native for %d", obj);
361 return;
362 }
363
364 paAttachments = crCalloc(maxCount * sizeof (*paAttachments));
365 if (!paAttachments)
366 {
367 crWarning("crCalloc failed");
368 return;
369 }
370
371 render_spu.pfnGetAttachedObjects(hNative, maxCount, &count, paAttachments);
372 if (pCount)
373 *pCount = count;
374 if (count > maxCount)
375 {
376 crWarning("count too big");
377 count = maxCount;
378 }
379
380 for (i = 0; i < count; ++i)
381 {
382 obj[i] = crHndlSearchVBox(paAttachments[i]);
383 CRASSERT(obj[i]);
384 }
385
386 crFree(paAttachments);
387}
388
389static VBoxGLhandleARB SPU_APIENTRY renderspu_SystemGetHandleARB(GLenum pname)
390{
391 GLhandleARB hNative = render_spu.pfnGetHandle(pname);
392 VBoxGLhandleARB hVBox;
393 if (!hNative)
394 {
395 crWarning("pfnGetHandle failed");
396 return 0;
397 }
398 hVBox = crHndlAcquireVBox(hNative);
399 CRASSERT(hVBox);
400 return hVBox;
401}
402
403static void SPU_APIENTRY renderspu_SystemGetInfoLogARB( VBoxGLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * infoLog )
404{
405 GLhandleARB hNative = crHndlSearchNative(obj);
406 if (!hNative)
407 {
408 crWarning("invalid handle!");
409 return;
410 }
411
412 render_spu.pfnGetInfoLog(hNative, maxLength, length, infoLog);
413}
414
415static void SPU_APIENTRY renderspu_SystemGetObjectParameterfvARB( VBoxGLhandleARB obj, GLenum pname, GLfloat * params )
416{
417 GLhandleARB hNative = crHndlSearchNative(obj);
418 if (!hNative)
419 {
420 crWarning("invalid handle!");
421 return;
422 }
423
424 render_spu.pfnGetObjectParameterfv(hNative, pname, params);
425}
426
427static void SPU_APIENTRY renderspu_SystemGetObjectParameterivARB( VBoxGLhandleARB obj, GLenum pname, GLint * params )
428{
429 GLhandleARB hNative = crHndlSearchNative(obj);
430 if (!hNative)
431 {
432 crWarning("invalid handle!");
433 return;
434 }
435
436 render_spu.pfnGetObjectParameteriv(hNative, pname, params);
437}
438
439uint32_t renderspu_SystemPostprocessFunctions(SPUNamedFunctionTable *aFunctions, uint32_t cFunctions, uint32_t cTable)
440{
441 SPUNamedFunctionTable * pEntry;
442
443 pEntry = renderspuFindEntry(aFunctions, "DeleteObjectARB");
444 if (pEntry)
445 {
446 render_spu.pfnDeleteObject = (PFNDELETE_OBJECT)pEntry->fn;
447 pEntry->fn = (SPUGenericFunction)renderspu_SystemDeleteObjectARB;
448 }
449
450 pEntry = renderspuFindEntry(aFunctions, "GetAttachedObjectsARB");
451 if (pEntry)
452 {
453 render_spu.pfnGetAttachedObjects = (PFNGET_ATTACHED_OBJECTS)pEntry->fn;
454 pEntry->fn = (SPUGenericFunction)renderspu_SystemGetAttachedObjectsARB;
455 }
456
457 pEntry = renderspuFindEntry(aFunctions, "GetHandleARB");
458 if (pEntry)
459 {
460 render_spu.pfnGetHandle = (PFNGET_HANDLE)pEntry->fn;
461 pEntry->fn = (SPUGenericFunction)renderspu_SystemGetHandleARB;
462 }
463
464 pEntry = renderspuFindEntry(aFunctions, "GetInfoLogARB");
465 if (pEntry)
466 {
467 render_spu.pfnGetInfoLog = (PFNGET_INFO_LOG)pEntry->fn;
468 pEntry->fn = (SPUGenericFunction)renderspu_SystemGetInfoLogARB;
469 }
470
471 pEntry = renderspuFindEntry(aFunctions, "GetObjectParameterfvARB");
472 if (pEntry)
473 {
474 render_spu.pfnGetObjectParameterfv = (PFNGET_OBJECT_PARAMETERFV)pEntry->fn;
475 pEntry->fn = (SPUGenericFunction)renderspu_SystemGetObjectParameterfvARB;
476 }
477
478 pEntry = renderspuFindEntry(aFunctions, "GetObjectParameterivARB");
479 if (pEntry)
480 {
481 render_spu.pfnGetObjectParameteriv = (PFNGET_OBJECT_PARAMETERIV)pEntry->fn;
482 pEntry->fn = (SPUGenericFunction)renderspu_SystemGetObjectParameterivARB;
483 }
484
485 return cFunctions;
486}
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