VirtualBox

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

Last change on this file since 78251 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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