summaryrefslogtreecommitdiff
path: root/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c
blob: f9492a2d0b4c3b5ebc726ccbb8ab51fa8cc45db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/** @file
 * VirtualBox OpenGL Cocoa Window System implementation
 */

/*
 * Copyright (C) 2009-2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#include <OpenGL/OpenGL.h>

#include "renderspu.h"
#include <iprt/process.h>
#include <iprt/string.h>
#include <iprt/path.h>

#include <cr_string.h>
#include <cr_mem.h>

GLboolean renderspu_SystemInitVisual(VisualInfo *pVisInfo)
{
    CRASSERT(pVisInfo);

/*    cocoaGLVisualCreate(&pCtxInfo->context);*/

    return GL_TRUE;
}

GLboolean renderspu_SystemCreateContext(VisualInfo *pVisInfo, ContextInfo *pCtxInfo, ContextInfo *pSharedCtxInfo)
{
    CRASSERT(pVisInfo);
    CRASSERT(pCtxInfo);

    pCtxInfo->currentWindow = NULL;

    cocoaGLCtxCreate(&pCtxInfo->context, pVisInfo->visAttribs, pSharedCtxInfo ? pSharedCtxInfo->context : NULL);

    return GL_TRUE;
}

void renderspu_SystemDestroyContext(ContextInfo *pCtxInfo)
{
    if(!pCtxInfo)
        return;

    if(pCtxInfo->context)
    {
        cocoaGLCtxDestroy(pCtxInfo->context);
        pCtxInfo->context = NULL;
    }
}

void renderspuFullscreen(WindowInfo *pWinInfo, GLboolean fFullscreen)
{
    /* Real fullscreen isn't supported by VirtualBox */
}

GLboolean renderspu_SystemVBoxCreateWindow(VisualInfo *pVisInfo, GLboolean fShowIt, WindowInfo *pWinInfo)
{
    CRASSERT(pVisInfo);
    CRASSERT(pWinInfo);

    /* VirtualBox is the only frontend which support 3D right now. */
    char pszName[256];
    if (RTProcGetExecutablePath(pszName, sizeof(pszName)))
        /* Check for VirtualBox and VirtualBoxVM */
        if (RTStrNICmp(RTPathFilename(pszName), "VirtualBox", 10) != 0)
            return GL_FALSE;

    pWinInfo->visual = pVisInfo;
    pWinInfo->window = NULL;
    pWinInfo->nativeWindow = NULL;
    pWinInfo->currentCtx = NULL;

#ifdef __LP64__
    NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
#else /* __LP64__ */
    NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
#endif /* __LP64__ */

    cocoaViewCreate(&pWinInfo->window, pWinInfo, pParentWin, pVisInfo->visAttribs);

    if (fShowIt)
        renderspu_SystemShowWindow(pWinInfo, fShowIt);

    return GL_TRUE;
}

void renderspu_SystemReparentWindow(WindowInfo *pWinInfo)
{
#ifdef __LP64__
    NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
#else /* __LP64__ */
    NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
#endif /* __LP64__ */
    cocoaViewReparent(pWinInfo->window, pParentWin);
}

void renderspu_SystemDestroyWindow(WindowInfo *pWinInfo)
{
    CRASSERT(pWinInfo);

    cocoaViewDestroy(pWinInfo->window);
}

void renderspu_SystemWindowPosition(WindowInfo *pWinInfo, GLint x, GLint y)
{
    CRASSERT(pWinInfo);

#ifdef __LP64__
    NativeNSViewRef pParentWin = (NativeNSViewRef)render_spu_parent_window_id;
#else /* __LP64__ */
    NativeNSViewRef pParentWin = (NativeNSViewRef)(uint32_t)render_spu_parent_window_id;
#endif /* __LP64__ */

    /*pParentWin is unused in the call, otherwise it might hold incorrect value if for ex. last reparent call was for
      a different screen*/
    cocoaViewSetPosition(pWinInfo->window, pParentWin, x, y);
}

void renderspu_SystemWindowSize(WindowInfo *pWinInfo, GLint w, GLint h)
{
    CRASSERT(pWinInfo);

    cocoaViewSetSize(pWinInfo->window, w, h);
}

void renderspu_SystemGetWindowGeometry(WindowInfo *pWinInfo, GLint *pX, GLint *pY, GLint *pW, GLint *pH)
{
    CRASSERT(pWinInfo);

    cocoaViewGetGeometry(pWinInfo->window, pX, pY, pW, pH);
}

void renderspu_SystemGetMaxWindowSize(WindowInfo *pWinInfo, GLint *pW, GLint *pH)
{
    CRASSERT(pWinInfo);

    *pW = 10000;
    *pH = 10000;
}

void renderspu_SystemShowWindow(WindowInfo *pWinInfo, GLboolean fShowIt)
{
    CRASSERT(pWinInfo);

    cocoaViewShow(pWinInfo->window, fShowIt);
}

void renderspu_SystemVBoxPresentComposition( WindowInfo *window, const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry )
{
    cocoaViewPresentComposition(window->window, pChangedEntry);
}

void renderspu_SystemMakeCurrent(WindowInfo *pWinInfo, GLint nativeWindow, ContextInfo *pCtxInfo)
{
/*    if(pWinInfo->visual != pCtxInfo->visual)*/
/*        printf ("visual mismatch .....................\n");*/

    nativeWindow = 0;

    if (pWinInfo && pCtxInfo)
        cocoaViewMakeCurrentContext(pWinInfo->window, pCtxInfo->context);
    else
        cocoaViewMakeCurrentContext(NULL, NULL);
}

void renderspu_SystemSwapBuffers(WindowInfo *pWinInfo, GLint flags)
{
    CRASSERT(pWinInfo);

    cocoaViewDisplay(pWinInfo->window);
}

void renderspu_SystemWindowVisibleRegion(WindowInfo *pWinInfo, GLint cRects, const GLint* paRects)
{
    CRASSERT(pWinInfo);

    cocoaViewSetVisibleRegion(pWinInfo->window, cRects, paRects);
}

void renderspu_SystemWindowApplyVisibleRegion(WindowInfo *pWinInfo)
{
}

int renderspu_SystemInit()
{
    return VINF_SUCCESS;
}

int renderspu_SystemTerm()
{
    CrGlslTerm(&render_spu.GlobalShaders);
    return VINF_SUCCESS;
}

static SPUNamedFunctionTable * renderspuFindEntry(SPUNamedFunctionTable *aFunctions, const char *pcszName)
{
    SPUNamedFunctionTable *pCur;

    for (pCur = aFunctions ; pCur->name != NULL ; pCur++)
    {
        if (!crStrcmp( pcszName, pCur->name ) )
        {
            return pCur;
        }
    }

    AssertFailed();

    return NULL;
}

typedef struct CR_RENDER_CTX_INFO
{
    ContextInfo * pContext;
    WindowInfo * pWindow;
} CR_RENDER_CTX_INFO;

void renderspuCtxInfoInitCurrent(CR_RENDER_CTX_INFO *pInfo)
{
    GET_CONTEXT(pCurCtx);
    pInfo->pContext = pCurCtx;
    pInfo->pWindow = pCurCtx->currentWindow;
}

void renderspuCtxInfoRestoreCurrent(CR_RENDER_CTX_INFO *pInfo)
{
    GET_CONTEXT(pCurCtx);
    if (pCurCtx == pInfo->pContext && (!pCurCtx || pCurCtx->currentWindow == pInfo->pWindow))
        return;
    renderspuPerformMakeCurrent(pInfo->pWindow, 0, pInfo->pContext);
}

GLboolean renderspuCtxSetCurrentWithAnyWindow(ContextInfo * pContext, CR_RENDER_CTX_INFO *pInfo)
{
    WindowInfo * window;
    renderspuCtxInfoInitCurrent(pInfo);

    if (pInfo->pContext == pContext)
        return GL_TRUE;

    window = pContext->currentWindow;
    if (!window)
    {
        window = renderspuGetDummyWindow(pContext->BltInfo.Base.visualBits);
        if (!window)
        {
            crWarning("renderspuGetDummyWindow failed");
            return GL_FALSE;
        }
    }

    Assert(window);

    renderspuPerformMakeCurrent(window, 0, pContext);
    return GL_TRUE;
}

void renderspu_SystemDefaultSharedContextChanged(ContextInfo *fromContext, ContextInfo *toContext)
{
    CRASSERT(fromContext != toContext);

    if (!CrGlslIsInited(&render_spu.GlobalShaders))
    {
        CrGlslInit(&render_spu.GlobalShaders, &render_spu.blitterDispatch);
    }

    if (fromContext)
    {
        if (CrGlslNeedsCleanup(&render_spu.GlobalShaders))
        {
            CR_RENDER_CTX_INFO Info;
            if (renderspuCtxSetCurrentWithAnyWindow(fromContext, &Info))
            {
                CrGlslCleanup(&render_spu.GlobalShaders);
                renderspuCtxInfoRestoreCurrent(&Info);
            }
            else
                crWarning("renderspuCtxSetCurrentWithAnyWindow failed!");
        }
    }
    else
    {
        CRASSERT(!CrGlslNeedsCleanup(&render_spu.GlobalShaders));
    }

    CRASSERT(!CrGlslNeedsCleanup(&render_spu.GlobalShaders));

    if (toContext)
    {
        CR_RENDER_CTX_INFO Info;
        if (renderspuCtxSetCurrentWithAnyWindow(toContext, &Info))
        {
            int rc = CrGlslProgGenAllNoAlpha(&render_spu.GlobalShaders);
            if (!RT_SUCCESS(rc))
                crWarning("CrGlslProgGenAllNoAlpha failed, rc %d", rc);

            renderspuCtxInfoRestoreCurrent(&Info);
        }
        else
            crWarning("renderspuCtxSetCurrentWithAnyWindow failed!");
    }
}

AssertCompile(sizeof (GLhandleARB) == sizeof (void*));

static VBoxGLhandleARB crHndlSearchVBox(GLhandleARB hNative)
{
    CRASSERT(!(((uintptr_t)hNative) >> 32));
    return (VBoxGLhandleARB)((uintptr_t)hNative);
}

static GLhandleARB crHndlSearchNative(VBoxGLhandleARB hVBox)
{
    return (GLhandleARB)((uintptr_t)hVBox);
}

static VBoxGLhandleARB crHndlAcquireVBox(GLhandleARB hNative)
{
    CRASSERT(!(((uintptr_t)hNative) >> 32));
    return (VBoxGLhandleARB)((uintptr_t)hNative);
}

static GLhandleARB crHndlReleaseVBox(VBoxGLhandleARB hVBox)
{
    return (GLhandleARB)((uintptr_t)hVBox);
}

static void SPU_APIENTRY renderspu_SystemDeleteObjectARB(VBoxGLhandleARB obj)
{
    GLhandleARB hNative = crHndlReleaseVBox(obj);
    if (!hNative)
    {
        crWarning("no native for %d", obj);
        return;
    }

    render_spu.pfnDeleteObject(hNative);
}

static void SPU_APIENTRY renderspu_SystemGetAttachedObjectsARB( VBoxGLhandleARB containerObj, GLsizei maxCount, GLsizei * pCount, VBoxGLhandleARB * obj )
{
    GLhandleARB *paAttachments;
    GLhandleARB hNative = crHndlSearchNative(containerObj);
    GLsizei count, i;

    if (pCount)
        *pCount = 0;

    if (!hNative)
    {
        crWarning("no native for %d", obj);
        return;
    }

    paAttachments = crCalloc(maxCount * sizeof (*paAttachments));
    if (!paAttachments)
    {
        crWarning("crCalloc failed");
        return;
    }

    render_spu.pfnGetAttachedObjects(hNative, maxCount, &count, paAttachments);
    if (pCount)
        *pCount = count;
    if (count > maxCount)
    {
        crWarning("count too big");
        count = maxCount;
    }

    for (i = 0; i < count; ++i)
    {
        obj[i] = crHndlSearchVBox(paAttachments[i]);
        CRASSERT(obj[i]);
    }

    crFree(paAttachments);
}

static VBoxGLhandleARB SPU_APIENTRY renderspu_SystemGetHandleARB(GLenum pname)
{
    GLhandleARB hNative = render_spu.pfnGetHandle(pname);
    VBoxGLhandleARB hVBox;
    if (!hNative)
    {
        crWarning("pfnGetHandle failed");
        return 0;
    }
    hVBox = crHndlAcquireVBox(hNative);
    CRASSERT(hVBox);
    return hVBox;
}

static void SPU_APIENTRY renderspu_SystemGetInfoLogARB( VBoxGLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * infoLog )
{
    GLhandleARB hNative = crHndlSearchNative(obj);
    if (!hNative)
    {
        crWarning("invalid handle!");
        return;
    }

    render_spu.pfnGetInfoLog(hNative, maxLength, length, infoLog);
}

static void SPU_APIENTRY renderspu_SystemGetObjectParameterfvARB( VBoxGLhandleARB obj, GLenum pname, GLfloat * params )
{
    GLhandleARB hNative = crHndlSearchNative(obj);
    if (!hNative)
    {
        crWarning("invalid handle!");
        return;
    }

    render_spu.pfnGetObjectParameterfv(hNative, pname, params);
}

static void SPU_APIENTRY renderspu_SystemGetObjectParameterivARB( VBoxGLhandleARB obj, GLenum pname, GLint * params )
{
    GLhandleARB hNative = crHndlSearchNative(obj);
    if (!hNative)
    {
        crWarning("invalid handle!");
        return;
    }

    render_spu.pfnGetObjectParameteriv(hNative, pname, params);
}

uint32_t renderspu_SystemPostprocessFunctions(SPUNamedFunctionTable *aFunctions, uint32_t cFunctions, uint32_t cTable)
{
    SPUNamedFunctionTable * pEntry;

    pEntry = renderspuFindEntry(aFunctions, "DeleteObjectARB");
    if (pEntry)
    {
        render_spu.pfnDeleteObject = (PFNDELETE_OBJECT)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemDeleteObjectARB;
    }

    pEntry = renderspuFindEntry(aFunctions, "GetAttachedObjectsARB");
    if (pEntry)
    {
        render_spu.pfnGetAttachedObjects = (PFNGET_ATTACHED_OBJECTS)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemGetAttachedObjectsARB;
    }

    pEntry = renderspuFindEntry(aFunctions, "GetHandleARB");
    if (pEntry)
    {
        render_spu.pfnGetHandle = (PFNGET_HANDLE)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemGetHandleARB;
    }

    pEntry = renderspuFindEntry(aFunctions, "GetInfoLogARB");
    if (pEntry)
    {
        render_spu.pfnGetInfoLog = (PFNGET_INFO_LOG)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemGetInfoLogARB;
    }

    pEntry = renderspuFindEntry(aFunctions, "GetObjectParameterfvARB");
    if (pEntry)
    {
        render_spu.pfnGetObjectParameterfv = (PFNGET_OBJECT_PARAMETERFV)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemGetObjectParameterfvARB;
    }

    pEntry = renderspuFindEntry(aFunctions, "GetObjectParameterivARB");
    if (pEntry)
    {
        render_spu.pfnGetObjectParameteriv = (PFNGET_OBJECT_PARAMETERIV)pEntry->fn;
        pEntry->fn = (SPUGenericFunction)renderspu_SystemGetObjectParameterivARB;
    }

    return cFunctions;
}