CmGLXGLSupport.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 "CmException.h"
  25. #include "CmStringConverter.h"
  26. #include "CmGLXGLSupport.h"
  27. #include "CmGLXUtils.h"
  28. #include "CmGLXWindow.h"
  29. #include "CmGLXRenderTexture.h"
  30. #include <X11/extensions/Xrandr.h>
  31. GLenum glxewContextInit(Ogre::GLSupport *glSupport);
  32. static Display *_currentDisplay;
  33. static Display *_getCurrentDisplay(void) { return _currentDisplay; }
  34. namespace CamelotEngine
  35. {
  36. //-------------------------------------------------------------------------------------------------//
  37. template<class C> void remove_duplicates(C& c)
  38. {
  39. std::sort(c.begin(), c.end());
  40. typename C::iterator p = std::unique(c.begin(), c.end());
  41. c.erase(p, c.end());
  42. }
  43. //-------------------------------------------------------------------------------------------------//
  44. GLXGLSupport::GLXGLSupport() : mGLDisplay(0), mXDisplay(0)
  45. {
  46. // A connection that might be shared with the application for GL rendering:
  47. mGLDisplay = getGLDisplay();
  48. // A connection that is NOT shared to enable independent event processing:
  49. mXDisplay = getXDisplay();
  50. int dummy;
  51. if (XQueryExtension(mXDisplay, "RANDR", &dummy, &dummy, &dummy))
  52. {
  53. XRRScreenConfiguration *screenConfig;
  54. screenConfig = XRRGetScreenInfo(mXDisplay, DefaultRootWindow(mXDisplay));
  55. if (screenConfig)
  56. {
  57. XRRScreenSize *screenSizes;
  58. int nSizes = 0;
  59. Rotation currentRotation;
  60. int currentSizeID = XRRConfigCurrentConfiguration(screenConfig, &currentRotation);
  61. screenSizes = XRRConfigSizes(screenConfig, &nSizes);
  62. mCurrentMode.first.first = screenSizes[currentSizeID].width;
  63. mCurrentMode.first.second = screenSizes[currentSizeID].height;
  64. mCurrentMode.second = XRRConfigCurrentRate(screenConfig);
  65. mOriginalMode = mCurrentMode;
  66. for(int sizeID = 0; sizeID < nSizes; sizeID++)
  67. {
  68. short *rates;
  69. int nRates = 0;
  70. rates = XRRConfigRates(screenConfig, sizeID, &nRates);
  71. for (int rate = 0; rate < nRates; rate++)
  72. {
  73. VideoMode mode;
  74. mode.first.first = screenSizes[sizeID].width;
  75. mode.first.second = screenSizes[sizeID].height;
  76. mode.second = rates[rate];
  77. mVideoModes.push_back(mode);
  78. }
  79. }
  80. XRRFreeScreenConfigInfo(screenConfig);
  81. }
  82. }
  83. else
  84. {
  85. mCurrentMode.first.first = DisplayWidth(mXDisplay, DefaultScreen(mXDisplay));
  86. mCurrentMode.first.second = DisplayHeight(mXDisplay, DefaultScreen(mXDisplay));
  87. mCurrentMode.second = 0;
  88. mOriginalMode = mCurrentMode;
  89. mVideoModes.push_back(mCurrentMode);
  90. }
  91. GLXFBConfig *fbConfigs;
  92. int config, nConfigs = 0;
  93. fbConfigs = chooseFBConfig(NULL, &nConfigs);
  94. for (config = 0; config < nConfigs; config++)
  95. {
  96. int caveat, samples;
  97. getFBConfigAttrib (fbConfigs[config], GLX_CONFIG_CAVEAT, &caveat);
  98. if (caveat != GLX_SLOW_CONFIG)
  99. {
  100. getFBConfigAttrib (fbConfigs[config], GLX_SAMPLES, &samples);
  101. mSampleLevels.push_back(StringConverter::toString(samples));
  102. }
  103. }
  104. XFree (fbConfigs);
  105. remove_duplicates(mSampleLevels);
  106. }
  107. //-------------------------------------------------------------------------------------------------//
  108. GLXGLSupport::~GLXGLSupport()
  109. {
  110. if (mXDisplay)
  111. XCloseDisplay(mXDisplay);
  112. if (! mIsExternalDisplay && mGLDisplay)
  113. XCloseDisplay(mGLDisplay);
  114. }
  115. //-------------------------------------------------------------------------------------------------//
  116. void GLXGLSupport::addConfig(void)
  117. {
  118. ConfigOption optFullScreen;
  119. ConfigOption optVideoMode;
  120. ConfigOption optDisplayFrequency;
  121. ConfigOption optVSync;
  122. ConfigOption optFSAA;
  123. ConfigOption optRTTMode;
  124. ConfigOption optSRGB;
  125. optFullScreen.name = "Full Screen";
  126. optFullScreen.immutable = false;
  127. optVideoMode.name = "Video Mode";
  128. optVideoMode.immutable = false;
  129. optDisplayFrequency.name = "Display Frequency";
  130. optDisplayFrequency.immutable = false;
  131. optVSync.name = "VSync";
  132. optVSync.immutable = false;
  133. optFSAA.name = "FSAA";
  134. optFSAA.immutable = false;
  135. optRTTMode.name = "RTT Preferred Mode";
  136. optRTTMode.immutable = false;
  137. optSRGB.name = "sRGB Gamma Conversion";
  138. optSRGB.immutable = false;
  139. optFullScreen.possibleValues.push_back("No");
  140. optFullScreen.possibleValues.push_back("Yes");
  141. optFullScreen.currentValue = optFullScreen.possibleValues[1];
  142. VideoModes::const_iterator value = mVideoModes.begin();
  143. VideoModes::const_iterator end = mVideoModes.end();
  144. for (; value != end; value++)
  145. {
  146. String mode = StringConverter::toString(value->first.first,4) + " x " + StringConverter::toString(value->first.second,4);
  147. optVideoMode.possibleValues.push_back(mode);
  148. }
  149. remove_duplicates(optVideoMode.possibleValues);
  150. optVideoMode.currentValue = StringConverter::toString(mCurrentMode.first.first,4) + " x " + StringConverter::toString(mCurrentMode.first.second,4);
  151. refreshConfig();
  152. if (GLXEW_SGI_swap_control)
  153. {
  154. optVSync.possibleValues.push_back("No");
  155. optVSync.possibleValues.push_back("Yes");
  156. optVSync.currentValue = optVSync.possibleValues[0];
  157. }
  158. // Is it worth checking for GL_EXT_framebuffer_object ?
  159. optRTTMode.possibleValues.push_back("FBO");
  160. if (GLXEW_VERSION_1_3)
  161. {
  162. optRTTMode.possibleValues.push_back("PBuffer");
  163. }
  164. optRTTMode.possibleValues.push_back("Copy");
  165. optRTTMode.currentValue = optRTTMode.possibleValues[0];
  166. if (! mSampleLevels.empty())
  167. {
  168. StringVector::const_iterator value = mSampleLevels.begin();
  169. StringVector::const_iterator end = mSampleLevels.end();
  170. for (; value != end; value++)
  171. {
  172. optFSAA.possibleValues.push_back(*value);
  173. }
  174. optFSAA.currentValue = optFSAA.possibleValues[0];
  175. }
  176. if (GLXEW_EXT_framebuffer_sRGB)
  177. {
  178. optSRGB.possibleValues.push_back("No");
  179. optSRGB.possibleValues.push_back("Yes");
  180. optSRGB.currentValue = optSRGB.possibleValues[0];
  181. }
  182. mOptions[optFullScreen.name] = optFullScreen;
  183. mOptions[optVideoMode.name] = optVideoMode;
  184. mOptions[optDisplayFrequency.name] = optDisplayFrequency;
  185. mOptions[optVSync.name] = optVSync;
  186. mOptions[optRTTMode.name] = optRTTMode;
  187. mOptions[optFSAA.name] = optFSAA;
  188. mOptions[optSRGB.name] = optSRGB;
  189. refreshConfig();
  190. }
  191. //-------------------------------------------------------------------------------------------------//
  192. void GLXGLSupport::refreshConfig(void)
  193. {
  194. ConfigOptionMap::iterator optVideoMode = mOptions.find("Video Mode");
  195. ConfigOptionMap::iterator optDisplayFrequency = mOptions.find("Display Frequency");
  196. if (optVideoMode != mOptions.end() && optDisplayFrequency != mOptions.end())
  197. {
  198. optDisplayFrequency->second.possibleValues.clear();
  199. VideoModes::const_iterator value = mVideoModes.begin();
  200. VideoModes::const_iterator end = mVideoModes.end();
  201. for (; value != end; value++)
  202. {
  203. String mode = StringConverter::toString(value->first.first,4) + " x " + StringConverter::toString(value->first.second,4);
  204. if (mode == optVideoMode->second.currentValue)
  205. {
  206. String frequency = StringConverter::toString(value->second) + " MHz";
  207. optDisplayFrequency->second.possibleValues.push_back(frequency);
  208. }
  209. }
  210. if (! optDisplayFrequency->second.possibleValues.empty())
  211. {
  212. optDisplayFrequency->second.currentValue = optDisplayFrequency->second.possibleValues[0];
  213. }
  214. else
  215. {
  216. optVideoMode->second.currentValue = StringConverter::toString(mVideoModes[0].first.first,4) + " x " + StringConverter::toString(mVideoModes[0].first.second,4);
  217. optDisplayFrequency->second.currentValue = StringConverter::toString(mVideoModes[0].second) + " MHz";
  218. }
  219. }
  220. }
  221. //-------------------------------------------------------------------------------------------------//
  222. void GLXGLSupport::setConfigOption(const String &name, const String &value)
  223. {
  224. ConfigOptionMap::iterator option = mOptions.find(name);
  225. if(option == mOptions.end())
  226. {
  227. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Option named " + name + " does not exist.", "GLXGLSupport::setConfigOption" );
  228. }
  229. else
  230. {
  231. option->second.currentValue = value;
  232. }
  233. if (name == "Video Mode")
  234. {
  235. ConfigOptionMap::iterator opt;
  236. if((opt = mOptions.find("Full Screen")) != mOptions.end())
  237. {
  238. if (opt->second.currentValue == "Yes")
  239. refreshConfig();
  240. }
  241. }
  242. }
  243. //-------------------------------------------------------------------------------------------------//
  244. String GLXGLSupport::validateConfig(void)
  245. {
  246. // TO DO
  247. return StringUtil::BLANK;
  248. }
  249. //-------------------------------------------------------------------------------------------------//
  250. RenderWindow* GLXGLSupport::createWindow(bool autoCreateWindow, GLRenderSystem* renderSystem, const String& windowTitle)
  251. {
  252. RenderWindow *window = 0;
  253. if (autoCreateWindow)
  254. {
  255. ConfigOptionMap::iterator opt;
  256. ConfigOptionMap::iterator end = mOptions.end();
  257. NameValuePairList miscParams;
  258. bool fullscreen = false;
  259. uint w = 800, h = 600;
  260. if((opt = mOptions.find("Full Screen")) != end)
  261. fullscreen = (opt->second.currentValue == "Yes");
  262. if((opt = mOptions.find("Display Frequency")) != end)
  263. miscParams["displayFrequency"] = opt->second.currentValue;
  264. if((opt = mOptions.find("Video Mode")) != end)
  265. {
  266. String val = opt->second.currentValue;
  267. String::size_type pos = val.find('x');
  268. if (pos != String::npos)
  269. {
  270. w = StringConverter::parseUnsignedInt(val.substr(0, pos));
  271. h = StringConverter::parseUnsignedInt(val.substr(pos + 1));
  272. }
  273. }
  274. if((opt = mOptions.find("FSAA")) != end)
  275. miscParams["FSAA"] = opt->second.currentValue;
  276. if((opt = mOptions.find("VSync")) != end)
  277. miscParams["vsync"] = opt->second.currentValue;
  278. if((opt = mOptions.find("sRGB Gamma Conversion")) != end)
  279. miscParams["gamma"] = opt->second.currentValue;
  280. window = renderSystem->_createRenderWindow(windowTitle, w, h, fullscreen, &miscParams);
  281. }
  282. return window;
  283. }
  284. //-------------------------------------------------------------------------------------------------//
  285. RenderWindow* GLXGLSupport::newWindow(const String &name, unsigned int width, unsigned int height, bool fullScreen, const NameValuePairList *miscParams)
  286. {
  287. GLXWindow* window = new GLXWindow(this);
  288. window->create(name, width, height, fullScreen, miscParams);
  289. return window;
  290. }
  291. //-------------------------------------------------------------------------------------------------//
  292. GLPBuffer *GLXGLSupport::createPBuffer(PixelComponentType format, size_t width, size_t height)
  293. {
  294. return new GLXPBuffer(this, format, width, height);
  295. }
  296. //-------------------------------------------------------------------------------------------------//
  297. void GLXGLSupport::start()
  298. {
  299. LogManager::getSingleton().logMessage(
  300. "******************************\n"
  301. "*** Starting GLX Subsystem ***\n"
  302. "******************************");
  303. }
  304. //-------------------------------------------------------------------------------------------------//
  305. void GLXGLSupport::stop()
  306. {
  307. LogManager::getSingleton().logMessage(
  308. "******************************\n"
  309. "*** Stopping GLX Subsystem ***\n"
  310. "******************************");
  311. }
  312. //-------------------------------------------------------------------------------------------------//
  313. void* GLXGLSupport::getProcAddress(const String& procname) {
  314. return (void*)glXGetProcAddressARB((const GLubyte*)procname.c_str());
  315. }
  316. //-------------------------------------------------------------------------------------------------//
  317. void GLXGLSupport::initialiseExtensions(void)
  318. {
  319. assert (mGLDisplay);
  320. GLSupport::initialiseExtensions();
  321. const char* extensionsString;
  322. // This is more realistic than using glXGetClientString:
  323. extensionsString = glXQueryExtensionsString(mGLDisplay, DefaultScreen(mGLDisplay));
  324. LogManager::getSingleton().stream() << "Supported GLX extensions: " << extensionsString;
  325. std::stringstream ext;
  326. String instr;
  327. ext << extensionsString;
  328. while(ext >> instr)
  329. {
  330. extensionList.insert(instr);
  331. }
  332. }
  333. //-------------------------------------------------------------------------------------------------//
  334. // Returns the FBConfig behind a GLXContext
  335. GLXFBConfig GLXGLSupport::getFBConfigFromContext(::GLXContext context)
  336. {
  337. GLXFBConfig fbConfig = 0;
  338. if (GLXEW_VERSION_1_3)
  339. {
  340. int fbConfigAttrib[] = {
  341. GLX_FBCONFIG_ID, 0,
  342. None
  343. };
  344. GLXFBConfig *fbConfigs;
  345. int nElements = 0;
  346. glXQueryContext(mGLDisplay, context, GLX_FBCONFIG_ID, &fbConfigAttrib[1]);
  347. fbConfigs = glXChooseFBConfig(mGLDisplay, DefaultScreen(mGLDisplay), fbConfigAttrib, &nElements);
  348. if (nElements)
  349. {
  350. fbConfig = fbConfigs[0];
  351. XFree(fbConfigs);
  352. }
  353. }
  354. else if (GLXEW_EXT_import_context && GLXEW_SGIX_fbconfig)
  355. {
  356. VisualID visualid;
  357. if (glXQueryContextInfoEXT(mGLDisplay, context, GLX_VISUAL_ID, (int*)&visualid))
  358. {
  359. fbConfig = getFBConfigFromVisualID(visualid);
  360. }
  361. }
  362. return fbConfig;
  363. }
  364. //-------------------------------------------------------------------------------------------------//
  365. // Returns the FBConfig behind a GLXDrawable, or returns 0 when
  366. // missing GLX_SGIX_fbconfig and drawable is Window (unlikely), OR
  367. // missing GLX_VERSION_1_3 and drawable is a GLXPixmap (possible).
  368. GLXFBConfig GLXGLSupport::getFBConfigFromDrawable(GLXDrawable drawable, unsigned int *width, unsigned int *height)
  369. {
  370. GLXFBConfig fbConfig = 0;
  371. if (GLXEW_VERSION_1_3)
  372. {
  373. int fbConfigAttrib[] = {
  374. GLX_FBCONFIG_ID, 0,
  375. None
  376. };
  377. GLXFBConfig *fbConfigs;
  378. int nElements = 0;
  379. glXQueryDrawable (mGLDisplay, drawable, GLX_FBCONFIG_ID, (unsigned int*)&fbConfigAttrib[1]);
  380. fbConfigs = glXChooseFBConfig(mGLDisplay, DefaultScreen(mGLDisplay), fbConfigAttrib, &nElements);
  381. if (nElements)
  382. {
  383. fbConfig = fbConfigs[0];
  384. XFree (fbConfigs);
  385. glXQueryDrawable(mGLDisplay, drawable, GLX_WIDTH, width);
  386. glXQueryDrawable(mGLDisplay, drawable, GLX_HEIGHT, height);
  387. }
  388. }
  389. if (! fbConfig && GLXEW_SGIX_fbconfig)
  390. {
  391. XWindowAttributes windowAttrib;
  392. if (XGetWindowAttributes(mGLDisplay, drawable, &windowAttrib))
  393. {
  394. VisualID visualid = XVisualIDFromVisual(windowAttrib.visual);
  395. fbConfig = getFBConfigFromVisualID(visualid);
  396. *width = windowAttrib.width;
  397. *height = windowAttrib.height;
  398. }
  399. }
  400. return fbConfig;
  401. }
  402. //-------------------------------------------------------------------------------------------------//
  403. // Finds a GLXFBConfig compatible with a given VisualID.
  404. GLXFBConfig GLXGLSupport::getFBConfigFromVisualID(VisualID visualid)
  405. {
  406. GLXFBConfig fbConfig = 0;
  407. if (GLXEW_SGIX_fbconfig && glXGetFBConfigFromVisualSGIX)
  408. {
  409. XVisualInfo visualInfo;
  410. visualInfo.screen = DefaultScreen(mGLDisplay);
  411. visualInfo.depth = DefaultDepth(mGLDisplay, DefaultScreen(mGLDisplay));
  412. visualInfo.visualid = visualid;
  413. fbConfig = glXGetFBConfigFromVisualSGIX(mGLDisplay, &visualInfo);
  414. }
  415. if (! fbConfig)
  416. {
  417. int minAttribs[] = {
  418. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT || GLX_PIXMAP_BIT,
  419. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  420. GLX_RED_SIZE, 1,
  421. GLX_BLUE_SIZE, 1,
  422. GLX_GREEN_SIZE, 1,
  423. None
  424. };
  425. int nConfigs = 0;
  426. GLXFBConfig *fbConfigs = chooseFBConfig(minAttribs, &nConfigs);
  427. for (int i = 0; i < nConfigs && ! fbConfig; i++)
  428. {
  429. XVisualInfo *visualInfo = getVisualFromFBConfig(fbConfigs[i]);
  430. if (visualInfo->visualid == visualid)
  431. fbConfig = fbConfigs[i];
  432. XFree(visualInfo);
  433. }
  434. XFree(fbConfigs);
  435. }
  436. return fbConfig;
  437. }
  438. //-------------------------------------------------------------------------------------------------//
  439. // A helper class for the implementation of selectFBConfig
  440. class FBConfigAttribs
  441. {
  442. public:
  443. FBConfigAttribs(const int* attribs)
  444. {
  445. fields[GLX_CONFIG_CAVEAT] = GLX_NONE;
  446. for (int i = 0; attribs[2*i]; i++)
  447. {
  448. fields[attribs[2*i]] = attribs[2*i+1];
  449. }
  450. }
  451. void load(GLXGLSupport* const glSupport, GLXFBConfig fbConfig)
  452. {
  453. std::map<int,int>::iterator it;
  454. for (it = fields.begin(); it != fields.end(); it++)
  455. {
  456. it->second = 0;
  457. glSupport->getFBConfigAttrib(fbConfig, it->first, &it->second);
  458. }
  459. }
  460. bool operator>(FBConfigAttribs& alternative)
  461. {
  462. // Caveats are best avoided, but might be needed for anti-aliasing
  463. if (fields[GLX_CONFIG_CAVEAT] != alternative.fields[GLX_CONFIG_CAVEAT])
  464. {
  465. if (fields[GLX_CONFIG_CAVEAT] == GLX_SLOW_CONFIG)
  466. return false;
  467. if (fields.find(GLX_SAMPLES) != fields.end() &&
  468. fields[GLX_SAMPLES] < alternative.fields[GLX_SAMPLES])
  469. return false;
  470. }
  471. std::map<int,int>::iterator it;
  472. for (it = fields.begin(); it != fields.end(); it++)
  473. {
  474. if (it->first != GLX_CONFIG_CAVEAT && fields[it->first] > alternative.fields[it->first])
  475. return true;
  476. }
  477. return false;
  478. }
  479. std::map<int,int> fields;
  480. };
  481. //-------------------------------------------------------------------------------------------------//
  482. // Finds an FBConfig that possesses each of minAttribs and gets as close
  483. // as possible to each of the maxAttribs without exceeding them.
  484. // Resembles glXChooseFBConfig, but is forgiving to platforms
  485. // that do not support the attributes listed in the maxAttribs.
  486. GLXFBConfig GLXGLSupport::selectFBConfig (const int* minAttribs, const int *maxAttribs)
  487. {
  488. GLXFBConfig *fbConfigs;
  489. GLXFBConfig fbConfig = 0;
  490. int config, nConfigs = 0;
  491. fbConfigs = chooseFBConfig(minAttribs, &nConfigs);
  492. // this is a fix for cases where chooseFBConfig is not supported.
  493. // On the 10/2010 chooseFBConfig was not supported on VirtualBox
  494. // http://www.virtualbox.org/ticket/7195
  495. if (!nConfigs)
  496. {
  497. fbConfigs = glXGetFBConfigs(mGLDisplay, DefaultScreen(mGLDisplay), &nConfigs);
  498. }
  499. if (! nConfigs)
  500. return 0;
  501. fbConfig = fbConfigs[0];
  502. if (maxAttribs)
  503. {
  504. FBConfigAttribs maximum(maxAttribs);
  505. FBConfigAttribs best(maxAttribs);
  506. FBConfigAttribs candidate(maxAttribs);
  507. best.load(this, fbConfig);
  508. for (config = 1; config < nConfigs; config++)
  509. {
  510. candidate.load(this, fbConfigs[config]);
  511. if (candidate > maximum)
  512. continue;
  513. if (candidate > best)
  514. {
  515. fbConfig = fbConfigs[config];
  516. best.load(this, fbConfig);
  517. }
  518. }
  519. }
  520. XFree (fbConfigs);
  521. return fbConfig;
  522. }
  523. //-------------------------------------------------------------------------------------------------//
  524. bool GLXGLSupport::loadIcon(const std::string &name, Pixmap *pixmap, Pixmap *bitmap)
  525. {
  526. Image image;
  527. int width, height;
  528. char* imageData;
  529. try
  530. {
  531. // Try to load image
  532. image.load(name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  533. if(image.getFormat() != PF_A8R8G8B8)
  534. {
  535. // Image format must be RGBA
  536. return false;
  537. }
  538. width = image.getWidth();
  539. height = image.getHeight();
  540. imageData = (char*)image.getData();
  541. }
  542. catch(Exception &e)
  543. {
  544. // Could not find image; never mind
  545. return false;
  546. }
  547. int bitmapLineLength = (width + 7) / 8;
  548. int pixmapLineLength = 4 * width;
  549. char* bitmapData = (char*)malloc(bitmapLineLength * height);
  550. char* pixmapData = (char*)malloc(pixmapLineLength * height);
  551. int sptr = 0, dptr = 0;
  552. for(int y = 0; y < height; y++)
  553. {
  554. for(int x = 0; x < width; x++)
  555. {
  556. if (ImageByteOrder(mXDisplay) == MSBFirst)
  557. {
  558. pixmapData[dptr + 0] = 0;
  559. pixmapData[dptr + 1] = imageData[sptr + 0];
  560. pixmapData[dptr + 2] = imageData[sptr + 1];
  561. pixmapData[dptr + 3] = imageData[sptr + 2];
  562. }
  563. else
  564. {
  565. pixmapData[dptr + 3] = 0;
  566. pixmapData[dptr + 2] = imageData[sptr + 0];
  567. pixmapData[dptr + 1] = imageData[sptr + 1];
  568. pixmapData[dptr + 0] = imageData[sptr + 2];
  569. }
  570. if(((unsigned char)imageData[sptr + 3])<128)
  571. {
  572. bitmapData[y*bitmapLineLength+(x>>3)] &= ~(1<<(x&7));
  573. }
  574. else
  575. {
  576. bitmapData[y*bitmapLineLength+(x>>3)] |= 1<<(x&7);
  577. }
  578. sptr += 4;
  579. dptr += 4;
  580. }
  581. }
  582. // Create bitmap on server and copy over bitmapData
  583. *bitmap = XCreateBitmapFromData(mXDisplay, DefaultRootWindow(mXDisplay), bitmapData, width, height);
  584. free(bitmapData);
  585. // Create pixmap on server and copy over pixmapData (via pixmapXImage)
  586. *pixmap = XCreatePixmap(mXDisplay, DefaultRootWindow(mXDisplay), width, height, 24);
  587. GC gc = XCreateGC (mXDisplay, DefaultRootWindow(mXDisplay), 0, NULL);
  588. XImage *pixmapXImage = XCreateImage(mXDisplay, NULL, 24, ZPixmap, 0, pixmapData, width, height, 8, width*4);
  589. XPutImage(mXDisplay, *pixmap, gc, pixmapXImage, 0, 0, 0, 0, width, height);
  590. XDestroyImage(pixmapXImage);
  591. XFreeGC(mXDisplay, gc);
  592. return true;
  593. }
  594. //-------------------------------------------------------------------------------------------------//
  595. Display* GLXGLSupport::getGLDisplay(void)
  596. {
  597. if (! mGLDisplay)
  598. {
  599. glXGetCurrentDisplay = (PFNGLXGETCURRENTDISPLAYPROC)getProcAddress("glXGetCurrentDisplay");
  600. mGLDisplay = glXGetCurrentDisplay();
  601. mIsExternalDisplay = true;
  602. if (! mGLDisplay)
  603. {
  604. mGLDisplay = XOpenDisplay(0);
  605. mIsExternalDisplay = false;
  606. }
  607. if(! mGLDisplay)
  608. {
  609. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn`t open X display " + String((const char*)XDisplayName (0)), "GLXGLSupport::getGLDisplay");
  610. }
  611. initialiseGLXEW();
  612. if (! GLXEW_VERSION_1_3 && ! (GLXEW_SGIX_fbconfig && GLXEW_EXT_import_context))
  613. {
  614. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "No GLX FBConfig support on your display", "GLXGLSupport::GLXGLSupport");
  615. }
  616. }
  617. return mGLDisplay;
  618. }
  619. //-------------------------------------------------------------------------------------------------//
  620. Display* GLXGLSupport::getXDisplay(void)
  621. {
  622. if (! mXDisplay)
  623. {
  624. char* displayString = mGLDisplay ? DisplayString(mGLDisplay) : 0;
  625. mXDisplay = XOpenDisplay(displayString);
  626. if (! mXDisplay)
  627. {
  628. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Couldn`t open X display " + String((const char*)displayString), "GLXGLSupport::getXDisplay");
  629. }
  630. mAtomDeleteWindow = XInternAtom(mXDisplay, "WM_DELETE_WINDOW", True);
  631. mAtomFullScreen = XInternAtom(mXDisplay, "_NET_WM_STATE_FULLSCREEN", True);
  632. mAtomState = XInternAtom(mXDisplay, "_NET_WM_STATE", True);
  633. }
  634. return mXDisplay;
  635. }
  636. //-------------------------------------------------------------------------------------------------//
  637. String GLXGLSupport::getDisplayName(void)
  638. {
  639. return String((const char*)XDisplayName(DisplayString(mGLDisplay)));
  640. }
  641. //-------------------------------------------------------------------------------------------------//
  642. GLXFBConfig* GLXGLSupport::chooseFBConfig(const GLint *attribList, GLint *nElements)
  643. {
  644. GLXFBConfig *fbConfigs;
  645. if (GLXEW_VERSION_1_3)
  646. fbConfigs = glXChooseFBConfig(mGLDisplay, DefaultScreen(mGLDisplay), attribList, nElements);
  647. else
  648. fbConfigs = glXChooseFBConfigSGIX(mGLDisplay, DefaultScreen(mGLDisplay), attribList, nElements);
  649. return fbConfigs;
  650. }
  651. //-------------------------------------------------------------------------------------------------//
  652. ::GLXContext GLXGLSupport::createNewContext(GLXFBConfig fbConfig, GLint renderType, ::GLXContext shareList, GLboolean direct) const
  653. {
  654. ::GLXContext glxContext;
  655. if (GLXEW_VERSION_1_3)
  656. glxContext = glXCreateNewContext(mGLDisplay, fbConfig, renderType, shareList, direct);
  657. else
  658. glxContext = glXCreateContextWithConfigSGIX(mGLDisplay, fbConfig, renderType, shareList, direct);
  659. return glxContext;
  660. }
  661. //-------------------------------------------------------------------------------------------------//
  662. GLint GLXGLSupport::getFBConfigAttrib(GLXFBConfig fbConfig, GLint attribute, GLint *value)
  663. {
  664. GLint status;
  665. if (GLXEW_VERSION_1_3)
  666. status = glXGetFBConfigAttrib(mGLDisplay, fbConfig, attribute, value);
  667. else
  668. status = glXGetFBConfigAttribSGIX(mGLDisplay, fbConfig, attribute, value);
  669. return status;
  670. }
  671. //-------------------------------------------------------------------------------------------------//
  672. XVisualInfo* GLXGLSupport::getVisualFromFBConfig(GLXFBConfig fbConfig)
  673. {
  674. XVisualInfo *visualInfo;
  675. if (GLXEW_VERSION_1_3)
  676. visualInfo = glXGetVisualFromFBConfig(mGLDisplay, fbConfig);
  677. else
  678. visualInfo = glXGetVisualFromFBConfigSGIX(mGLDisplay, fbConfig);
  679. return visualInfo;
  680. }
  681. //-------------------------------------------------------------------------------------------------//
  682. void GLXGLSupport::switchMode(uint& width, uint& height, short& frequency)
  683. {
  684. int size = 0;
  685. int newSize = -1;
  686. VideoModes::iterator mode;
  687. VideoModes::iterator end = mVideoModes.end();
  688. VideoMode *newMode = 0;
  689. for(mode = mVideoModes.begin(); mode != end; size++)
  690. {
  691. if (mode->first.first >= static_cast<int>(width) &&
  692. mode->first.second >= static_cast<int>(height))
  693. {
  694. if (! newMode ||
  695. mode->first.first < newMode->first.first ||
  696. mode->first.second < newMode->first.second)
  697. {
  698. newSize = size;
  699. newMode = &(*mode);
  700. }
  701. }
  702. VideoMode *lastMode = &(*mode);
  703. while (++mode != end && mode->first == lastMode->first)
  704. {
  705. if (lastMode == newMode && mode->second == frequency)
  706. {
  707. newMode = &(*mode);
  708. }
  709. }
  710. }
  711. if (newMode && *newMode != mCurrentMode)
  712. {
  713. XRRScreenConfiguration *screenConfig = XRRGetScreenInfo (mXDisplay, DefaultRootWindow(mXDisplay));
  714. if (screenConfig)
  715. {
  716. Rotation currentRotation;
  717. XRRConfigCurrentConfiguration (screenConfig, &currentRotation);
  718. XRRSetScreenConfigAndRate(mXDisplay, screenConfig, DefaultRootWindow(mXDisplay), newSize, currentRotation, newMode->second, CurrentTime);
  719. XRRFreeScreenConfigInfo(screenConfig);
  720. mCurrentMode = *newMode;
  721. LogManager::getSingleton().logMessage("Entered video mode " + StringConverter::toString(mCurrentMode.first.first) + "x" + StringConverter::toString(mCurrentMode.first.second) + " @ " + StringConverter::toString(mCurrentMode.second) + "MHz");
  722. }
  723. }
  724. }
  725. //-------------------------------------------------------------------------------------------------//
  726. void GLXGLSupport::switchMode(void)
  727. {
  728. return switchMode(mOriginalMode.first.first, mOriginalMode.first.second, mOriginalMode.second);
  729. }
  730. //-------------------------------------------------------------------------------------------------//
  731. // Initialise GLXEW
  732. //
  733. // Overloading glXGetCurrentDisplay allows us to call glxewContextInit
  734. // before establishing a GL context. This approach is a bit of a hack,
  735. // but it minimises the patches required between glew.c and glew.cpp.
  736. void GLXGLSupport::initialiseGLXEW(void)
  737. {
  738. _currentDisplay = mGLDisplay;
  739. glXGetCurrentDisplay = (PFNGLXGETCURRENTDISPLAYPROC)_getCurrentDisplay;
  740. if (glxewContextInit(this) != GLEW_OK)
  741. {
  742. XCloseDisplay (mGLDisplay);
  743. XCloseDisplay (mXDisplay);
  744. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "No GLX 1.1 support on your platform", "GLXGLSupport::initialiseGLXEW");
  745. }
  746. glXGetCurrentDisplay = (PFNGLXGETCURRENTDISPLAYPROC)getProcAddress("glXGetCurrentDisplay");
  747. }
  748. }