BsLinuxRenderWindow.cpp 15 KB

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