BsLinuxRenderWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "CoreThread/BsCoreThread.h"
  4. #include "Linux/BsLinuxPlatform.h"
  5. #include "Linux/BsLinuxRenderWindow.h"
  6. #include "Linux/BsLinuxWindow.h"
  7. #include "Linux/BsLinuxVideoModeInfo.h"
  8. #include "Linux/BsLinuxGLSupport.h"
  9. #include "Linux/BsLinuxContext.h"
  10. #include "BsGLPixelFormat.h"
  11. #include "BsGLRenderWindowManager.h"
  12. #include "Math/BsMath.h"
  13. #define XRANDR_ROTATION_LEFT (1 << 1)
  14. #define XRANDR_ROTATION_RIGHT (1 << 3)
  15. namespace bs
  16. {
  17. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, ct::LinuxGLSupport& glSupport)
  18. :RenderWindow(desc, windowId), mGLSupport(glSupport), mProperties(desc)
  19. { }
  20. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  21. {
  22. if (name == "WINDOW" || name == "LINUX_WINDOW")
  23. {
  24. blockUntilCoreInitialized();
  25. getCore()->getCustomAttribute(name, data);
  26. return;
  27. }
  28. }
  29. Vector2I LinuxRenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  30. {
  31. blockUntilCoreInitialized();
  32. LinuxPlatform::lockX();
  33. Vector2I pos = getCore()->_getInternal()->screenToWindowPos(screenPos);
  34. LinuxPlatform::unlockX();
  35. return pos;
  36. }
  37. Vector2I LinuxRenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  38. {
  39. blockUntilCoreInitialized();
  40. LinuxPlatform::lockX();
  41. Vector2I pos = getCore()->_getInternal()->windowToScreenPos(windowPos);
  42. LinuxPlatform::unlockX();
  43. return pos;
  44. }
  45. SPtr<ct::LinuxRenderWindow> LinuxRenderWindow::getCore() const
  46. {
  47. return std::static_pointer_cast<ct::LinuxRenderWindow>(mCoreSpecific);
  48. }
  49. void LinuxRenderWindow::syncProperties()
  50. {
  51. ScopedSpinLock lock(getCore()->_getPropertiesLock());
  52. mProperties = getCore()->mSyncedProperties;
  53. }
  54. namespace ct
  55. {
  56. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, LinuxGLSupport& glsupport)
  57. : RenderWindow(desc, windowId), mWindow(nullptr), mGLSupport(glsupport), mContext(nullptr), mProperties(desc)
  58. , mSyncedProperties(desc), mIsChild(false), mShowOnSwap(false)
  59. { }
  60. LinuxRenderWindow::~LinuxRenderWindow()
  61. {
  62. // Make sure to set the original desktop video mode before we exit
  63. if(mProperties.isFullScreen)
  64. setWindowed(50, 50);
  65. if (mWindow != nullptr)
  66. {
  67. LinuxPlatform::lockX();
  68. mWindow->close();
  69. bs_delete(mWindow);
  70. mWindow = nullptr;
  71. LinuxPlatform::unlockX();
  72. }
  73. }
  74. void LinuxRenderWindow::initialize()
  75. {
  76. LinuxPlatform::lockX();
  77. RenderWindowProperties& props = mProperties;
  78. props.isFullScreen = mDesc.fullscreen;
  79. mIsChild = false;
  80. GLVisualConfig visualConfig = mGLSupport.findBestVisual(LinuxPlatform::getXDisplay(), mDesc.depthBuffer,
  81. mDesc.multisampleCount, mDesc.gamma);
  82. WINDOW_DESC windowDesc;
  83. windowDesc.x = mDesc.left;
  84. windowDesc.y = mDesc.top;
  85. windowDesc.width = mDesc.videoMode.getWidth();
  86. windowDesc.height = mDesc.videoMode.getHeight();
  87. windowDesc.title = mDesc.title;
  88. windowDesc.showDecorations = mDesc.showTitleBar;
  89. windowDesc.allowResize = mDesc.allowResize;
  90. windowDesc.showOnTaskBar = !mDesc.toolWindow;
  91. windowDesc.modal = mDesc.modal;
  92. windowDesc.visualInfo = visualConfig.visualInfo;
  93. windowDesc.screen = mDesc.videoMode.getOutputIdx();
  94. NameValuePairList::const_iterator opt;
  95. opt = mDesc.platformSpecific.find("parentWindowHandle");
  96. if (opt != mDesc.platformSpecific.end())
  97. windowDesc.parent = (::Window)parseUINT64(opt->second);
  98. else
  99. windowDesc.parent = 0;
  100. mIsChild = windowDesc.parent != 0;
  101. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  102. mShowOnSwap = mDesc.hideUntilSwap;
  103. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  104. mWindow = bs_new<LinuxWindow>(windowDesc);
  105. mWindow->_setUserData(this);
  106. props.width = mWindow->getWidth();
  107. props.height = mWindow->getHeight();
  108. props.top = mWindow->getTop();
  109. props.left = mWindow->getLeft();
  110. props.hwGamma = visualConfig.caps.srgb;
  111. props.multisampleCount = visualConfig.caps.numSamples;
  112. XWindowAttributes windowAttributes;
  113. XGetWindowAttributes(LinuxPlatform::getXDisplay(), mWindow->_getXWindow(), &windowAttributes);
  114. XVisualInfo requestVI;
  115. requestVI.screen = windowDesc.screen;
  116. requestVI.visualid = XVisualIDFromVisual(windowAttributes.visual);
  117. LinuxPlatform::unlockX(); // Calls below have their own locking mechanisms
  118. mContext = mGLSupport.createContext(LinuxPlatform::getXDisplay(), requestVI);
  119. if(mDesc.fullscreen && !mIsChild)
  120. setFullscreen(mDesc.videoMode);
  121. if(mDesc.hideUntilSwap || mDesc.hidden)
  122. setHidden(true);
  123. {
  124. ScopedSpinLock lock(mLock);
  125. mSyncedProperties = props;
  126. }
  127. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  128. RenderWindow::initialize();
  129. }
  130. void LinuxRenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  131. {
  132. THROW_IF_NOT_CORE_THREAD;
  133. VideoMode videoMode(width, height, refreshRate, monitorIdx);
  134. setFullscreen(videoMode);
  135. }
  136. void LinuxRenderWindow::setVideoMode(INT32 screen, RROutput output, RRMode mode)
  137. {
  138. ::Display* display = LinuxPlatform::getXDisplay();
  139. ::Window rootWindow = RootWindow(display, screen);
  140. XRRScreenResources* screenRes = XRRGetScreenResources (display, rootWindow);
  141. if(screenRes == nullptr)
  142. {
  143. LOGERR("XRR: Failed to retrieve screen resources. ");
  144. return;
  145. }
  146. XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screenRes, output);
  147. if(outputInfo == nullptr)
  148. {
  149. XRRFreeScreenResources(screenRes);
  150. LOGERR("XRR: Failed to retrieve output info for output: " + toString((UINT32)output));
  151. return;
  152. }
  153. XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screenRes, outputInfo->crtc);
  154. if(crtcInfo == nullptr)
  155. {
  156. XRRFreeScreenResources(screenRes);
  157. XRRFreeOutputInfo(outputInfo);
  158. LOGERR("XRR: Failed to retrieve CRTC info for output: " + toString((UINT32)output));
  159. return;
  160. }
  161. // Note: This changes the user's desktop resolution permanently, even when the app exists, make sure to revert
  162. // (Sadly there doesn't appear to be a better way)
  163. Status status = XRRSetCrtcConfig (display, screenRes, outputInfo->crtc, CurrentTime,
  164. crtcInfo->x, crtcInfo->y, mode, crtcInfo->rotation, &output, 1);
  165. if(status != Success)
  166. LOGERR("XRR: XRRSetCrtcConfig failed.");
  167. XRRFreeCrtcInfo(crtcInfo);
  168. XRRFreeOutputInfo(outputInfo);
  169. XRRFreeScreenResources(screenRes);
  170. }
  171. void LinuxRenderWindow::setFullscreen(const VideoMode& mode)
  172. {
  173. THROW_IF_NOT_CORE_THREAD;
  174. if (mIsChild)
  175. return;
  176. const LinuxVideoModeInfo& videoModeInfo =
  177. static_cast<const LinuxVideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  178. UINT32 outputIdx = mode.getOutputIdx();
  179. if(outputIdx >= videoModeInfo.getNumOutputs())
  180. {
  181. LOGERR("Invalid output device index.")
  182. return;
  183. }
  184. const LinuxVideoOutputInfo& outputInfo =
  185. static_cast<const LinuxVideoOutputInfo&>(videoModeInfo.getOutputInfo (outputIdx));
  186. INT32 screen = outputInfo._getScreen();
  187. RROutput outputID = outputInfo._getOutputID();
  188. RRMode modeID = 0;
  189. if(!mode.isCustom())
  190. {
  191. const LinuxVideoMode& videoMode = static_cast<const LinuxVideoMode&>(mode);
  192. modeID = videoMode._getModeID();
  193. }
  194. else
  195. {
  196. LinuxPlatform::lockX();
  197. // Look for mode matching the requested resolution
  198. ::Display* display = LinuxPlatform::getXDisplay();
  199. ::Window rootWindow = RootWindow(display, screen);
  200. XRRScreenResources* screenRes = XRRGetScreenResources(display, rootWindow);
  201. if (screenRes == nullptr)
  202. {
  203. LOGERR("XRR: Failed to retrieve screen resources. ");
  204. return;
  205. }
  206. XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screenRes, outputID);
  207. if (outputInfo == nullptr)
  208. {
  209. XRRFreeScreenResources(screenRes);
  210. LOGERR("XRR: Failed to retrieve output info for output: " + toString((UINT32)outputID));
  211. return;
  212. }
  213. XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screenRes, outputInfo->crtc);
  214. if (crtcInfo == nullptr)
  215. {
  216. XRRFreeScreenResources(screenRes);
  217. XRRFreeOutputInfo(outputInfo);
  218. LOGERR("XRR: Failed to retrieve CRTC info for output: " + toString((UINT32)outputID));
  219. return;
  220. }
  221. bool foundMode = false;
  222. for (INT32 i = 0; i < screenRes->nmode; i++)
  223. {
  224. const XRRModeInfo& modeInfo = screenRes->modes[i];
  225. UINT32 width, height;
  226. if (crtcInfo->rotation & (XRANDR_ROTATION_LEFT | XRANDR_ROTATION_RIGHT))
  227. {
  228. width = modeInfo.height;
  229. height = modeInfo.width;
  230. }
  231. else
  232. {
  233. width = modeInfo.width;
  234. height = modeInfo.height;
  235. }
  236. float refreshRate;
  237. if (modeInfo.hTotal != 0 && modeInfo.vTotal != 0)
  238. refreshRate = (float) (modeInfo.dotClock / (double) (modeInfo.hTotal * modeInfo.vTotal));
  239. else
  240. refreshRate = 0.0f;
  241. if (width == mode.getWidth() && height == mode.getHeight())
  242. {
  243. modeID = modeInfo.id;
  244. foundMode = true;
  245. if (Math::approxEquals(refreshRate, mode.getRefreshRate()))
  246. break;
  247. }
  248. }
  249. if (!foundMode)
  250. {
  251. LinuxPlatform::unlockX();
  252. LOGERR("Unable to enter fullscreen, unsupported video mode requested.");
  253. return;
  254. }
  255. LinuxPlatform::unlockX();
  256. }
  257. LinuxPlatform::lockX();
  258. setVideoMode(screen, outputID, modeID);
  259. mWindow->_setFullscreen(true);
  260. LinuxPlatform::unlockX();
  261. RenderWindowProperties& props = mProperties;
  262. props.isFullScreen = true;
  263. props.top = 0;
  264. props.left = 0;
  265. props.width = mode.getWidth();
  266. props.height = mode.getHeight();
  267. _windowMovedOrResized();
  268. }
  269. void LinuxRenderWindow::setWindowed(UINT32 width, UINT32 height)
  270. {
  271. THROW_IF_NOT_CORE_THREAD;
  272. RenderWindowProperties& props = mProperties;
  273. if (!props.isFullScreen)
  274. return;
  275. // Restore old screen config
  276. const LinuxVideoModeInfo& videoModeInfo =
  277. static_cast<const LinuxVideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  278. UINT32 outputIdx = 0; // 0 is always primary
  279. if(outputIdx >= videoModeInfo.getNumOutputs())
  280. {
  281. LOGERR("Invalid output device index.")
  282. return;
  283. }
  284. const LinuxVideoOutputInfo& outputInfo =
  285. static_cast<const LinuxVideoOutputInfo&>(videoModeInfo.getOutputInfo (outputIdx));
  286. const LinuxVideoMode& desktopVideoMode = static_cast<const LinuxVideoMode&>(outputInfo.getDesktopVideoMode());
  287. LinuxPlatform::lockX();
  288. setVideoMode(outputInfo._getScreen(), outputInfo._getOutputID(), desktopVideoMode._getModeID());
  289. mWindow->_setFullscreen(false);
  290. LinuxPlatform::unlockX();
  291. props.isFullScreen = false;
  292. props.width = width;
  293. props.height = height;
  294. {
  295. ScopedSpinLock lock(mLock);
  296. mSyncedProperties.width = props.width;
  297. mSyncedProperties.height = props.height;
  298. }
  299. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  300. _windowMovedOrResized();
  301. }
  302. void LinuxRenderWindow::move(INT32 left, INT32 top)
  303. {
  304. THROW_IF_NOT_CORE_THREAD;
  305. RenderWindowProperties& props = mProperties;
  306. if (!props.isFullScreen)
  307. {
  308. LinuxPlatform::lockX();
  309. mWindow->move(left, top);
  310. LinuxPlatform::unlockX();
  311. props.top = mWindow->getTop();
  312. props.left = mWindow->getLeft();
  313. {
  314. ScopedSpinLock lock(mLock);
  315. mSyncedProperties.top = props.top;
  316. mSyncedProperties.left = props.left;
  317. }
  318. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  319. }
  320. }
  321. void LinuxRenderWindow::resize(UINT32 width, UINT32 height)
  322. {
  323. THROW_IF_NOT_CORE_THREAD;
  324. RenderWindowProperties& props = mProperties;
  325. if (!props.isFullScreen)
  326. {
  327. LinuxPlatform::lockX();
  328. mWindow->resize(width, height);
  329. LinuxPlatform::unlockX();
  330. props.width = mWindow->getWidth();
  331. props.height = mWindow->getHeight();
  332. {
  333. ScopedSpinLock lock(mLock);
  334. mSyncedProperties.width = props.width;
  335. mSyncedProperties.height = props.height;
  336. }
  337. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  338. }
  339. }
  340. void LinuxRenderWindow::minimize()
  341. {
  342. THROW_IF_NOT_CORE_THREAD;
  343. LinuxPlatform::lockX();
  344. mWindow->minimize();
  345. LinuxPlatform::unlockX();
  346. }
  347. void LinuxRenderWindow::maximize()
  348. {
  349. THROW_IF_NOT_CORE_THREAD;
  350. LinuxPlatform::lockX();
  351. mWindow->maximize();
  352. LinuxPlatform::unlockX();
  353. }
  354. void LinuxRenderWindow::restore()
  355. {
  356. THROW_IF_NOT_CORE_THREAD;
  357. LinuxPlatform::lockX();
  358. mWindow->restore();
  359. LinuxPlatform::unlockX();
  360. }
  361. void LinuxRenderWindow::swapBuffers(UINT32 syncMask)
  362. {
  363. THROW_IF_NOT_CORE_THREAD;
  364. if (mShowOnSwap)
  365. setHidden(false);
  366. LinuxPlatform::lockX();
  367. glXSwapBuffers(LinuxPlatform::getXDisplay(), mWindow->_getXWindow());
  368. LinuxPlatform::unlockX();
  369. }
  370. void LinuxRenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  371. {
  372. THROW_IF_NOT_CORE_THREAD;
  373. if ((dst.getRight() > getProperties().width) ||
  374. (dst.getBottom() > getProperties().height) ||
  375. (dst.getFront() != 0) || (dst.getBack() != 1))
  376. {
  377. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  378. }
  379. if (buffer == FB_AUTO)
  380. {
  381. buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
  382. }
  383. GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
  384. GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
  385. if ((format == GL_NONE) || (type == 0))
  386. {
  387. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  388. }
  389. // Must change the packing to ensure no overruns!
  390. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  391. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  392. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  393. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  394. format, type, dst.getData());
  395. // restore default alignment
  396. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  397. //vertical flip
  398. {
  399. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  400. size_t height = dst.getHeight();
  401. UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
  402. UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  403. while (tmpRow >= tmpData)
  404. {
  405. memcpy(tmpRow, srcRow, rowSpan);
  406. srcRow += rowSpan;
  407. tmpRow -= rowSpan;
  408. }
  409. memcpy(dst.getData(), tmpData, rowSpan * height);
  410. bs_free(tmpData);
  411. }
  412. }
  413. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  414. {
  415. if(name == "GLCONTEXT")
  416. {
  417. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(data);
  418. *contextPtr = mContext;
  419. return;
  420. }
  421. else if(name == "LINUX_WINDOW")
  422. {
  423. LinuxWindow** window = (LinuxWindow**)data;
  424. *window = mWindow;
  425. return;
  426. }
  427. else if(name == "WINDOW")
  428. {
  429. ::Window* window = (::Window*)data;
  430. *window = mWindow->_getXWindow();
  431. return;
  432. }
  433. }
  434. void LinuxRenderWindow::setActive(bool state)
  435. {
  436. THROW_IF_NOT_CORE_THREAD;
  437. LinuxPlatform::lockX();
  438. if(state)
  439. mWindow->restore();
  440. else
  441. mWindow->minimize();
  442. LinuxPlatform::unlockX();
  443. RenderWindow::setActive(state);
  444. }
  445. void LinuxRenderWindow::setHidden(bool hidden)
  446. {
  447. THROW_IF_NOT_CORE_THREAD;
  448. mShowOnSwap = false;
  449. LinuxPlatform::lockX();
  450. if(hidden)
  451. mWindow->hide();
  452. else
  453. mWindow->show();
  454. LinuxPlatform::unlockX();
  455. RenderWindow::setHidden(hidden);
  456. }
  457. void LinuxRenderWindow::_windowMovedOrResized()
  458. {
  459. if (!mWindow)
  460. return;
  461. RenderWindowProperties& props = mProperties;
  462. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  463. {
  464. props.top = mWindow->getTop();
  465. props.left = mWindow->getLeft();
  466. props.width = mWindow->getWidth();
  467. props.height = mWindow->getHeight();
  468. }
  469. RenderWindow::_windowMovedOrResized();
  470. }
  471. void LinuxRenderWindow::syncProperties()
  472. {
  473. ScopedSpinLock lock(mLock);
  474. mProperties = mSyncedProperties;
  475. }
  476. }}