1 | /* $Id: OpenGLTestDarwin.cpp 53815 2015-01-15 11:15:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host opengl support test
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2014 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 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <VBox/VBoxOGL.h>
|
---|
23 |
|
---|
24 | #include <IOKit/IOKitLib.h>
|
---|
25 | #include <OpenGL/OpenGL.h>
|
---|
26 | #include <ApplicationServices/ApplicationServices.h>
|
---|
27 | #include <OpenGL/gl.h>
|
---|
28 | #ifdef VBOX_WITH_COCOA_QT
|
---|
29 | # include <OpenGL/glu.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/env.h>
|
---|
33 | #include <iprt/log.h>
|
---|
34 | #include <iprt/once.h>
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @callback_method_impl{FNRTONCE,
|
---|
40 | * For determining the cached VBoxOglIsOfflineRenderingAppropriate result.}
|
---|
41 | */
|
---|
42 | static DECLCALLBACK(int32_t) vboxOglIsOfflineRenderingAppropriateOnce(void *pvUser)
|
---|
43 | {
|
---|
44 | bool *pfAppropriate = (bool *)pvUser;
|
---|
45 |
|
---|
46 | /* It is assumed that it is makes sense to enable offline rendering
|
---|
47 | only in case if host has more than one GPU installed. This routine
|
---|
48 | counts all the PCI devices in IORegistry which have IOName property
|
---|
49 | set to "display". If the number of such devices is greater than one,
|
---|
50 | it sets pfAppropriate to TRUE, otherwise to FALSE. */
|
---|
51 |
|
---|
52 | CFStringRef apKeyStrings[] = { CFSTR(kIOProviderClassKey), CFSTR(kIONameMatchKey) };
|
---|
53 | CFStringRef apValueStrings[] = { CFSTR("IOPCIDevice"), CFSTR("display") };
|
---|
54 | Assert(RT_ELEMENTS(apKeyStrings) == RT_ELEMENTS(apValueStrings));
|
---|
55 |
|
---|
56 | CFDictionaryRef pMatchingDictionary = CFDictionaryCreate(kCFAllocatorDefault,
|
---|
57 | (const void **)apKeyStrings,
|
---|
58 | (const void **)apValueStrings,
|
---|
59 | RT_ELEMENTS(apKeyStrings),
|
---|
60 | &kCFTypeDictionaryKeyCallBacks,
|
---|
61 | &kCFTypeDictionaryValueCallBacks);
|
---|
62 | if (pMatchingDictionary)
|
---|
63 | {
|
---|
64 | /* The reference to pMatchingDictionary is consumed by the function below => no IORelease(pMatchingDictionary)! */
|
---|
65 | io_iterator_t matchingServices;
|
---|
66 | kern_return_t krc = IOServiceGetMatchingServices(kIOMasterPortDefault, pMatchingDictionary, &matchingServices);
|
---|
67 | if (krc == kIOReturnSuccess)
|
---|
68 | {
|
---|
69 | io_object_t matchingService;
|
---|
70 | int cMatchingServices = 0;
|
---|
71 |
|
---|
72 | while ((matchingService = IOIteratorNext(matchingServices)) != 0)
|
---|
73 | {
|
---|
74 | cMatchingServices++;
|
---|
75 | IOObjectRelease(matchingService);
|
---|
76 | }
|
---|
77 |
|
---|
78 | *pfAppropriate = cMatchingServices > 1;
|
---|
79 |
|
---|
80 | IOObjectRelease(matchingServices);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | LogRel(("OpenGL: Offline rendering support is %s (pid=%d)\n", *pfAppropriate ? "ON" : "OFF", (int)getpid()));
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | bool RTCALL VBoxOglIsOfflineRenderingAppropriate(void)
|
---|
90 | {
|
---|
91 | /* In order to do not slowdown 3D engine which can ask about offline rendering several times,
|
---|
92 | let's cache the result and assume that renderers amount value is constant. Use the IPRT
|
---|
93 | execute once construct to make sure there aren't any threading issues. */
|
---|
94 | static RTONCE s_Once = RTONCE_INITIALIZER;
|
---|
95 | static bool s_fCached = false;
|
---|
96 | int rc = RTOnce(&s_Once, vboxOglIsOfflineRenderingAppropriateOnce, &s_fCached);
|
---|
97 | AssertRC(rc);
|
---|
98 | return s_fCached;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | bool RTCALL VBoxOglIs3DAccelerationSupported(void)
|
---|
103 | {
|
---|
104 | if (RTEnvExist("VBOX_CROGL_FORCE_SUPPORTED"))
|
---|
105 | {
|
---|
106 | LogRel(("VBOX_CROGL_FORCE_SUPPORTED is specified, skipping 3D test, and treating as supported\n"));
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 |
|
---|
110 | CGOpenGLDisplayMask cglDisplayMask = CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID());
|
---|
111 | CGLPixelFormatAttribute aAttribs[] =
|
---|
112 | {
|
---|
113 | kCGLPFADisplayMask,
|
---|
114 | (CGLPixelFormatAttribute)cglDisplayMask,
|
---|
115 | kCGLPFAAccelerated,
|
---|
116 | kCGLPFADoubleBuffer,
|
---|
117 | VBoxOglIsOfflineRenderingAppropriate() ? kCGLPFAAllowOfflineRenderers : (CGLPixelFormatAttribute)NULL,
|
---|
118 | (CGLPixelFormatAttribute)NULL
|
---|
119 | };
|
---|
120 | CGLPixelFormatObj pPixelFormat = NULL;
|
---|
121 | GLint cPixelFormatsIgnored = 0;
|
---|
122 | CGLError rcCgl = CGLChoosePixelFormat(aAttribs, &pPixelFormat, &cPixelFormatsIgnored);
|
---|
123 | if (rcCgl != kCGLNoError)
|
---|
124 | {
|
---|
125 | LogRel(("OpenGL Info: 3D test unable to choose pixel format (rcCgl=0x%X)\n", rcCgl));
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (pPixelFormat)
|
---|
130 | {
|
---|
131 | CGLContextObj pCglContext = 0;
|
---|
132 | rcCgl = CGLCreateContext(pPixelFormat, NULL, &pCglContext);
|
---|
133 | CGLDestroyPixelFormat(pPixelFormat);
|
---|
134 |
|
---|
135 | if (rcCgl != kCGLNoError)
|
---|
136 | {
|
---|
137 | LogRel(("OpenGL Info: 3D test unable to create context (rcCgl=0x%X)\n", rcCgl));
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (pCglContext)
|
---|
142 | {
|
---|
143 | GLboolean isSupported = GL_TRUE;
|
---|
144 |
|
---|
145 | #ifdef VBOX_WITH_COCOA_QT
|
---|
146 | /*
|
---|
147 | * In the Cocoa port we depend on the GL_EXT_framebuffer_object &
|
---|
148 | * the GL_EXT_texture_rectangle extension. If they are not
|
---|
149 | * available, disable 3D support.
|
---|
150 | */
|
---|
151 | CGLSetCurrentContext(pCglContext);
|
---|
152 | const GLubyte *pszExts = glGetString(GL_EXTENSIONS);
|
---|
153 | isSupported = gluCheckExtension((const GLubyte *)"GL_EXT_framebuffer_object", pszExts);
|
---|
154 | if (isSupported)
|
---|
155 | {
|
---|
156 | isSupported = gluCheckExtension((const GLubyte *)"GL_EXT_texture_rectangle", pszExts);
|
---|
157 | if (!isSupported)
|
---|
158 | LogRel(("OpenGL Info: 3D test found that GL_EXT_texture_rectangle extension not supported.\n"));
|
---|
159 | }
|
---|
160 | else
|
---|
161 | LogRel(("OpenGL Info: 3D test found that GL_EXT_framebuffer_object extension not supported.\n"));
|
---|
162 | #endif /* VBOX_WITH_COCOA_QT */
|
---|
163 |
|
---|
164 | CGLDestroyContext(pCglContext);
|
---|
165 | LogRel(("OpenGL Info: 3D test %spassed\n", isSupported == GL_TRUE ? "" : "not "));
|
---|
166 | return isSupported == GL_TRUE;
|
---|
167 | }
|
---|
168 |
|
---|
169 | LogRel(("OpenGL Info: 3D test unable to create context (internal error).\n"));
|
---|
170 | }
|
---|
171 | else
|
---|
172 | LogRel(("OpenGL Info: 3D test unable to choose pixel format (internal error).\n"));
|
---|
173 |
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 |
|
---|