CmOSXGLSupport.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreException.h"
  25. #include "OgreLogManager.h"
  26. #include "OgreStringConverter.h"
  27. #include "OgreRoot.h"
  28. #include "OgreOSXGLSupport.h"
  29. #include "OgreOSXCarbonWindow.h"
  30. #include "OgreOSXCocoaWindow.h"
  31. #include "OgreGLTexture.h"
  32. #include "OgreOSXRenderTexture.h"
  33. #include "macUtils.h"
  34. #include <dlfcn.h>
  35. #include <OpenGL/OpenGL.h>
  36. namespace Ogre {
  37. OSXGLSupport::OSXGLSupport() : mAPI(""), mContextType("")
  38. {
  39. }
  40. OSXGLSupport::~OSXGLSupport()
  41. {
  42. }
  43. void OSXGLSupport::addConfig( void )
  44. {
  45. ConfigOption optFullScreen;
  46. ConfigOption optVideoMode;
  47. ConfigOption optBitDepth;
  48. ConfigOption optFSAA;
  49. ConfigOption optRTTMode;
  50. ConfigOption optMacAPI;
  51. // FS setting possiblities
  52. optFullScreen.name = "Full Screen";
  53. optFullScreen.possibleValues.push_back( "Yes" );
  54. optFullScreen.possibleValues.push_back( "No" );
  55. optFullScreen.currentValue = "No";
  56. optFullScreen.immutable = false;
  57. optBitDepth.name = "Colour Depth";
  58. optBitDepth.possibleValues.push_back( "32" );
  59. optBitDepth.possibleValues.push_back( "16" );
  60. optBitDepth.currentValue = "32";
  61. optBitDepth.immutable = false;
  62. mOptions[ optFullScreen.name ] = optFullScreen;
  63. mOptions[ optBitDepth.name ] = optBitDepth;
  64. CGLRendererInfoObj rend;
  65. #if defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
  66. long nrend;
  67. CGLQueryRendererInfo(CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), &rend, &nrend);
  68. #else
  69. GLint nrend;
  70. CGLQueryRendererInfo(CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), &rend, &nrend);
  71. #endif
  72. #if defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
  73. long maxSamples;
  74. CGLDescribeRenderer(rend, 0, kCGLRPMaxSamples, &maxSamples);
  75. #else
  76. GLint maxSamples;
  77. CGLDescribeRenderer(rend, 0, kCGLRPMaxSamples, &maxSamples);
  78. #endif
  79. CGLDestroyRendererInfo(rend);
  80. // FSAA possibilities
  81. optFSAA.name = "FSAA";
  82. optFSAA.possibleValues.push_back( "0" );
  83. switch( maxSamples )
  84. {
  85. case 8:
  86. optFSAA.possibleValues.push_back( "2" );
  87. optFSAA.possibleValues.push_back( "4" );
  88. optFSAA.possibleValues.push_back( "6" );
  89. optFSAA.possibleValues.push_back( "8" );
  90. break;
  91. case 6:
  92. optFSAA.possibleValues.push_back( "2" );
  93. optFSAA.possibleValues.push_back( "4" );
  94. optFSAA.possibleValues.push_back( "6" );
  95. break;
  96. case 4:
  97. optFSAA.possibleValues.push_back( "2" );
  98. optFSAA.possibleValues.push_back( "4" );
  99. break;
  100. case 2:
  101. optFSAA.possibleValues.push_back( "2" );
  102. break;
  103. default: break;
  104. }
  105. optFSAA.currentValue = "0";
  106. optFSAA.immutable = false;
  107. mOptions[ optFSAA.name ] = optFSAA;
  108. // Video mode possiblities
  109. optVideoMode.name = "Video Mode";
  110. optVideoMode.immutable = false;
  111. #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  112. CFArrayRef displayModes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL);
  113. #else
  114. CFArrayRef displayModes = CGDisplayAvailableModes(CGMainDisplayID());
  115. #endif
  116. CFIndex numModes = CFArrayGetCount(displayModes);
  117. CFMutableArrayRef goodModes = NULL;
  118. goodModes = CFArrayCreateMutable(kCFAllocatorDefault, numModes, NULL);
  119. // Grab all the available display modes, then weed out duplicates...
  120. for(int i = 0; i < numModes; ++i)
  121. {
  122. #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  123. CGDisplayModeRef modeInfo = (CGDisplayModeRef)CFArrayGetValueAtIndex(displayModes, i);
  124. // Get IOKit flags for the display mode
  125. uint32_t ioFlags = CGDisplayModeGetIOFlags(modeInfo);
  126. bool safeForHardware = ioFlags & kDisplayModeSafetyFlags ? true : false;
  127. bool stretched = ioFlags & kDisplayModeStretchedFlag ? true : false;
  128. bool skipped = false;
  129. if((safeForHardware) || (!stretched))
  130. {
  131. size_t width = CGDisplayModeGetWidth(modeInfo);
  132. size_t height = CGDisplayModeGetHeight(modeInfo);
  133. for(CFIndex j = 0; j < CFArrayGetCount(goodModes); ++j)
  134. {
  135. CGDisplayModeRef otherMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(goodModes, j);
  136. size_t otherWidth = CGDisplayModeGetWidth(otherMode);
  137. size_t otherHeight = CGDisplayModeGetHeight(otherMode);
  138. // If we find a duplicate then skip this mode
  139. if((otherWidth == width) && (otherHeight == height))
  140. skipped = true;
  141. }
  142. // This is a new mode, so add it to our goodModes array
  143. if(!skipped)
  144. CFArrayAppendValue(goodModes, modeInfo);
  145. #else
  146. CFDictionaryRef modeInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(displayModes, i);
  147. Boolean safeForHardware = _getDictionaryBoolean(modeInfo, kCGDisplayModeIsSafeForHardware);
  148. Boolean stretched = _getDictionaryBoolean(modeInfo, kCGDisplayModeIsStretched);
  149. Boolean skipped = false;
  150. if((safeForHardware) || (!stretched))
  151. {
  152. long width = _getDictionaryLong(modeInfo, kCGDisplayWidth);
  153. long height = _getDictionaryLong(modeInfo, kCGDisplayHeight);
  154. for(int j = 0; j < CFArrayGetCount(goodModes); ++j)
  155. {
  156. CFDictionaryRef otherMode = (CFDictionaryRef)CFArrayGetValueAtIndex(goodModes, j);
  157. long otherWidth = _getDictionaryLong(otherMode, kCGDisplayWidth);
  158. long otherHeight = _getDictionaryLong(otherMode, kCGDisplayHeight);
  159. // If we find a duplicate then skip this mode
  160. if((otherWidth == width) && (otherHeight == height))
  161. skipped = true;
  162. }
  163. // This is a new mode, so add it to our goodModes array
  164. if(!skipped)
  165. CFArrayAppendValue(goodModes, modeInfo);
  166. #endif
  167. }
  168. }
  169. // Sort the modes...
  170. CFArraySortValues(goodModes, CFRangeMake(0, CFArrayGetCount(goodModes)),
  171. (CFComparatorFunction)_compareModes, NULL);
  172. // Now pull the modes out and put them into optVideoModes
  173. for(int i = 0; i < CFArrayGetCount(goodModes); ++i)
  174. {
  175. #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  176. CGDisplayModeRef resolution = (CGDisplayModeRef)CFArrayGetValueAtIndex(goodModes, i);
  177. size_t fWidth = CGDisplayModeGetWidth(resolution);
  178. size_t fHeight = CGDisplayModeGetHeight(resolution);
  179. #else
  180. CFDictionaryRef resolution = (CFDictionaryRef)CFArrayGetValueAtIndex(goodModes, i);
  181. long fWidth = _getDictionaryLong(resolution, kCGDisplayWidth);
  182. long fHeight = _getDictionaryLong(resolution, kCGDisplayHeight);
  183. #endif
  184. String resoString = StringConverter::toString(fWidth) + " x " + StringConverter::toString(fHeight);
  185. optVideoMode.possibleValues.push_back(resoString);
  186. }
  187. // Release memory
  188. CFRelease(goodModes);
  189. optRTTMode.name = "RTT Preferred Mode";
  190. optRTTMode.possibleValues.push_back( "FBO" );
  191. optRTTMode.possibleValues.push_back( "PBuffer" );
  192. optRTTMode.possibleValues.push_back( "Copy" );
  193. optRTTMode.currentValue = "FBO";
  194. optRTTMode.immutable = false;
  195. optMacAPI.name = "macAPI";
  196. optMacAPI.possibleValues.push_back( "cocoa" );
  197. #ifndef __LP64__
  198. optMacAPI.possibleValues.push_back( "carbon" );
  199. optMacAPI.currentValue = "carbon";
  200. #else
  201. optMacAPI.currentValue = "cocoa";
  202. #endif
  203. optMacAPI.immutable = false;
  204. mOptions[optMacAPI.name] = optMacAPI;
  205. mOptions[optFullScreen.name] = optFullScreen;
  206. mOptions[optVideoMode.name] = optVideoMode;
  207. mOptions[optFSAA.name] = optFSAA;
  208. mOptions[optRTTMode.name] = optRTTMode;
  209. }
  210. String OSXGLSupport::validateConfig( void )
  211. {
  212. return String( "" );
  213. }
  214. RenderWindow* OSXGLSupport::createWindow( bool autoCreateWindow, GLRenderSystem* renderSystem, const String& windowTitle )
  215. {
  216. if( autoCreateWindow )
  217. {
  218. ConfigOptionMap::iterator opt = mOptions.find( "Full Screen" );
  219. if( opt == mOptions.end() )
  220. OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, "Can't find full screen options!", "OSXGLSupport::createWindow" );
  221. bool fullscreen = ( opt->second.currentValue == "Yes" );
  222. opt = mOptions.find( "Video Mode" );
  223. if( opt == mOptions.end() )
  224. OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, "Can't find video mode options!", "OSXGLSupport::createWindow" );
  225. String val = opt->second.currentValue;
  226. String::size_type pos = val.find( 'x' );
  227. if( pos == String::npos )
  228. OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, "Invalid Video Mode provided", "OSXGLSupport::createWindow" );
  229. unsigned int w = StringConverter::parseUnsignedInt( val.substr( 0, pos ) );
  230. unsigned int h = StringConverter::parseUnsignedInt( val.substr( pos + 1 ) );
  231. // Parse FSAA config
  232. NameValuePairList winOptions;
  233. winOptions[ "title" ] = windowTitle;
  234. opt = mOptions.find( "FSAA" );
  235. if( opt != mOptions.end() )
  236. {
  237. winOptions[ "FSAA" ] = opt->second.currentValue;
  238. }
  239. opt = mOptions.find( "macAPI" );
  240. if( opt != mOptions.end() )
  241. {
  242. winOptions[ "macAPI" ] = opt->second.currentValue;
  243. }
  244. return renderSystem->_createRenderWindow( windowTitle, w, h, fullscreen, &winOptions );
  245. }
  246. else
  247. {
  248. // XXX What is the else?
  249. return NULL;
  250. }
  251. }
  252. RenderWindow* OSXGLSupport::newWindow( const String &name, unsigned int width, unsigned int height,
  253. bool fullScreen, const NameValuePairList *miscParams )
  254. {
  255. // Does the user want Cocoa or Carbon, default to Carbon...
  256. mAPI = "carbon";
  257. mContextType = "CGL";
  258. if(miscParams)
  259. {
  260. NameValuePairList::const_iterator opt(NULL);
  261. // First we must determine if this is a Carbon or a Cocoa window
  262. // that we wish to create
  263. opt = miscParams->find("macAPI");
  264. if(opt != miscParams->end() && opt->second == "cocoa")
  265. {
  266. // Our user wants a Cocoa compatible system
  267. mAPI = "cocoa";
  268. mContextType = "NSOpenGL";
  269. }
  270. }
  271. // Create the window, if Cocoa return a Cocoa window
  272. if(mAPI == "cocoa")
  273. {
  274. LogManager::getSingleton().logMessage("Creating a Cocoa Compatible Render System");
  275. OSXCocoaWindow *window = OGRE_NEW OSXCocoaWindow();
  276. window->create(name, width, height, fullScreen, miscParams);
  277. return window;
  278. }
  279. #ifndef __LP64__
  280. else
  281. {
  282. // Otherwise default to Carbon
  283. LogManager::getSingleton().logMessage("Creating a Carbon Compatible Render System");
  284. OSXCarbonWindow *window = OGRE_NEW OSXCarbonWindow();
  285. window->create(name, width, height, fullScreen, miscParams);
  286. return window;
  287. }
  288. #endif
  289. return NULL;
  290. }
  291. void OSXGLSupport::start()
  292. {
  293. LogManager::getSingleton().logMessage(
  294. "********************************************\n"
  295. "*** Starting Mac OS X OpenGL Subsystem ***\n"
  296. "********************************************");
  297. }
  298. void OSXGLSupport::stop()
  299. {
  300. LogManager::getSingleton().logMessage(
  301. "********************************************\n"
  302. "*** Stopping Mac OS X OpenGL Subsystem ***\n"
  303. "********************************************");
  304. }
  305. void* OSXGLSupport::getProcAddress( const char* name )
  306. {
  307. void *symbol;
  308. symbol = NULL;
  309. std::string fullPath = macPluginPath() + "RenderSystem_GL.dylib";
  310. void *handle = dlopen(fullPath.c_str(), RTLD_LAZY | RTLD_GLOBAL);
  311. if(handle) {
  312. symbol = dlsym (handle, name);
  313. }
  314. dlclose(handle);
  315. return symbol;
  316. }
  317. void* OSXGLSupport::getProcAddress( const String& procname )
  318. {
  319. return getProcAddress( procname.c_str() );
  320. }
  321. bool OSXGLSupport::supportsPBuffers()
  322. {
  323. return true;
  324. }
  325. GLPBuffer* OSXGLSupport::createPBuffer(PixelComponentType format, size_t width, size_t height)
  326. {
  327. // if(mContextType == "NSOpenGL")
  328. // return OGRE_NEW OSXCocoaPBuffer(format, width, height);
  329. // if(mContextType == "CGL")
  330. // return OGRE_NEW OSXCGLPBuffer(format, width, height);
  331. // else
  332. return OGRE_NEW OSXPBuffer(format, width, height);
  333. }
  334. #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  335. CFComparisonResult OSXGLSupport::_compareModes (const void *val1, const void *val2, void *context)
  336. {
  337. // These are the values we will be interested in...
  338. /*
  339. CGDisplayModeGetWidth
  340. CGDisplayModeGetHeight
  341. CGDisplayModeGetRefreshRate
  342. _getDictionaryLong((mode), kCGDisplayBitsPerPixel)
  343. CGDisplayModeGetIOFlags((mode), kDisplayModeStretchedFlag)
  344. CGDisplayModeGetIOFlags((mode), kDisplayModeSafetyFlags)
  345. */
  346. // CFArray comparison callback for sorting display modes.
  347. #pragma unused(context)
  348. CGDisplayModeRef thisMode = (CGDisplayModeRef)val1;
  349. CGDisplayModeRef otherMode = (CGDisplayModeRef)val2;
  350. size_t width = CGDisplayModeGetWidth(thisMode);
  351. size_t otherWidth = CGDisplayModeGetWidth(otherMode);
  352. size_t height = CGDisplayModeGetHeight(thisMode);
  353. size_t otherHeight = CGDisplayModeGetHeight(otherMode);
  354. // Sort modes in screen size order
  355. if (width * height < otherWidth * otherHeight)
  356. {
  357. return kCFCompareLessThan;
  358. }
  359. else if (width * height > otherWidth * otherHeight)
  360. {
  361. return kCFCompareGreaterThan;
  362. }
  363. // Sort modes by refresh rate.
  364. double refreshRate = CGDisplayModeGetRefreshRate(thisMode);
  365. double otherRefreshRate = CGDisplayModeGetRefreshRate(otherMode);
  366. if (refreshRate < otherRefreshRate)
  367. {
  368. return kCFCompareLessThan;
  369. }
  370. else if (refreshRate > otherRefreshRate)
  371. {
  372. return kCFCompareGreaterThan;
  373. }
  374. return kCFCompareEqualTo;
  375. }
  376. #else
  377. CFComparisonResult OSXGLSupport::_compareModes (const void *val1, const void *val2, void *context)
  378. {
  379. // These are the values we will be interested in...
  380. /*
  381. _getDictionaryLong((mode), kCGDisplayWidth)
  382. _getDictionaryLong((mode), kCGDisplayHeight)
  383. _getDictionaryLong((mode), kCGDisplayRefreshRate)
  384. _getDictionaryLong((mode), kCGDisplayBitsPerPixel)
  385. _getDictionaryBoolean((mode), kCGDisplayModeIsSafeForHardware)
  386. _getDictionaryBoolean((mode), kCGDisplayModeIsStretched)
  387. */
  388. // CFArray comparison callback for sorting display modes.
  389. #pragma unused(context)
  390. CFDictionaryRef thisMode = (CFDictionaryRef)val1;
  391. CFDictionaryRef otherMode = (CFDictionaryRef)val2;
  392. long width = _getDictionaryLong(thisMode, kCGDisplayWidth);
  393. long otherWidth = _getDictionaryLong(otherMode, kCGDisplayWidth);
  394. long height = _getDictionaryLong(thisMode, kCGDisplayHeight);
  395. long otherHeight = _getDictionaryLong(otherMode, kCGDisplayHeight);
  396. // Sort modes in screen size order
  397. if (width * height < otherWidth * otherHeight)
  398. {
  399. return kCFCompareLessThan;
  400. }
  401. else if (width * height > otherWidth * otherHeight)
  402. {
  403. return kCFCompareGreaterThan;
  404. }
  405. // Sort modes by bits per pixel
  406. long bitsPerPixel = _getDictionaryLong(thisMode, kCGDisplayBitsPerPixel);
  407. long otherBitsPerPixel = _getDictionaryLong(otherMode, kCGDisplayBitsPerPixel);
  408. if (bitsPerPixel < otherBitsPerPixel)
  409. {
  410. return kCFCompareLessThan;
  411. }
  412. else if (bitsPerPixel > otherBitsPerPixel)
  413. {
  414. return kCFCompareGreaterThan;
  415. }
  416. // Sort modes by refresh rate.
  417. long refreshRate = _getDictionaryLong(thisMode, kCGDisplayRefreshRate);
  418. long otherRefreshRate = _getDictionaryLong(otherMode, kCGDisplayRefreshRate);
  419. if (refreshRate < otherRefreshRate)
  420. {
  421. return kCFCompareLessThan;
  422. }
  423. else if (refreshRate > otherRefreshRate)
  424. {
  425. return kCFCompareGreaterThan;
  426. }
  427. return kCFCompareEqualTo;
  428. }
  429. #endif
  430. Boolean OSXGLSupport::_getDictionaryBoolean(CFDictionaryRef dict, const void* key)
  431. {
  432. Boolean value = false;
  433. CFBooleanRef boolRef;
  434. boolRef = (CFBooleanRef)CFDictionaryGetValue(dict, key);
  435. if (boolRef != NULL)
  436. value = CFBooleanGetValue(boolRef);
  437. return value;
  438. }
  439. long OSXGLSupport::_getDictionaryLong(CFDictionaryRef dict, const void* key)
  440. {
  441. long value = 0;
  442. CFNumberRef numRef;
  443. numRef = (CFNumberRef)CFDictionaryGetValue(dict, key);
  444. if (numRef != NULL)
  445. CFNumberGetValue(numRef, kCFNumberLongType, &value);
  446. return value;
  447. }
  448. }