BsLinuxRenderWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 "BsGLPixelFormat.h"
  10. #include "BsGLRenderWindowManager.h"
  11. namespace bs
  12. {
  13. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, ct::LinuxGLSupport& glSupport)
  14. :RenderWindow(desc, windowId), mGLSupport(glSupport), mProperties(desc)
  15. { }
  16. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  17. {
  18. if (name == "WINDOW")
  19. {
  20. blockUntilCoreInitialized();
  21. getCore()->getCustomAttribute(name, data);
  22. return;
  23. }
  24. }
  25. Vector2I LinuxRenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  26. {
  27. blockUntilCoreInitialized();
  28. LinuxPlatform::lockX();
  29. Vector2I pos = getCore()->_getInternal()->screenToWindowPos(screenPos);
  30. LinuxPlatform::unlockX();
  31. return pos;
  32. }
  33. Vector2I LinuxRenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  34. {
  35. blockUntilCoreInitialized();
  36. LinuxPlatform::lockX();
  37. Vector2I pos = getCore()->_getInternal()->windowToScreenPos(windowPos);
  38. LinuxPlatform::unlockX();
  39. return pos;
  40. }
  41. SPtr<ct::LinuxRenderWindow> LinuxRenderWindow::getCore() const
  42. {
  43. return std::static_pointer_cast<ct::LinuxRenderWindow>(mCoreSpecific);
  44. }
  45. void LinuxRenderWindow::syncProperties()
  46. {
  47. ScopedSpinLock lock(getCore()->_getPropertiesLock());
  48. mProperties = getCore()->mSyncedProperties;
  49. }
  50. namespace ct
  51. {
  52. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, LinuxGLSupport& glsupport)
  53. : RenderWindow(desc, windowId), mWindow(nullptr), mGLSupport(glsupport), mContext(nullptr), mProperties(desc)
  54. , mSyncedProperties(desc), mIsChild(false), mShowOnSwap(false), mOldScreenConfig(nullptr)
  55. { }
  56. LinuxRenderWindow::~LinuxRenderWindow()
  57. {
  58. RenderWindowProperties& props = mProperties;
  59. if (mWindow != nullptr)
  60. {
  61. LinuxPlatform::lockX();
  62. if(mOldScreenConfig)
  63. {
  64. XRRFreeScreenConfigInfo(mOldScreenConfig);
  65. mOldScreenConfig = nullptr;
  66. }
  67. mWindow->close();
  68. bs_delete(mWindow);
  69. mWindow = nullptr;
  70. LinuxPlatform::unlockX();
  71. }
  72. }
  73. void LinuxRenderWindow::initialize()
  74. {
  75. LinuxPlatform::lockX();
  76. RenderWindowProperties& props = mProperties;
  77. props.isFullScreen = mDesc.fullscreen;
  78. mIsChild = false;
  79. GLVisualConfig visualConfig = mGLSupport.findBestVisual(LinuxPlatform::getXDisplay(), mDesc.depthBuffer,
  80. mDesc.multisampleCount, mDesc.gamma);
  81. WINDOW_DESC windowDesc;
  82. windowDesc.x = mDesc.left;
  83. windowDesc.y = mDesc.top;
  84. windowDesc.width = mDesc.videoMode.getWidth();
  85. windowDesc.height = mDesc.videoMode.getHeight();
  86. windowDesc.title = mDesc.title;
  87. windowDesc.showDecorations = !mDesc.toolWindow;
  88. windowDesc.modal = mDesc.modal;
  89. windowDesc.visualInfo = visualConfig.visualInfo;
  90. windowDesc.screen = mDesc.videoMode.getOutputIdx();
  91. NameValuePairList::const_iterator opt;
  92. opt = mDesc.platformSpecific.find("parentWindowHandle");
  93. if (opt != mDesc.platformSpecific.end())
  94. windowDesc.parent = (::Window)parseUINT64(opt->second);
  95. mIsChild = windowDesc.parent != nullptr;
  96. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  97. mShowOnSwap = mDesc.hideUntilSwap;
  98. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  99. mWindow = bs_new<LinuxWindow>(windowDesc);
  100. props.width = mWindow->getWidth();
  101. props.height = mWindow->getHeight();
  102. props.top = mWindow->getTop();
  103. props.left = mWindow->getLeft();
  104. props.hwGamma = visualConfig.caps.srgb;
  105. props.multisampleCount = visualConfig.caps.numSamples > 1;
  106. XWindowAttributes windowAttributes;
  107. XGetWindowAttributes(LinuxPlatform::getXDisplay(), mWindow->_getXWindow(), &windowAttributes);
  108. XVisualInfo requestVI;
  109. requestVI.screen = windowDesc.screen;
  110. requestVI.visualid = XVisualIDFromVisual(windowAttributes.visual);
  111. LinuxPlatform::unlockX(); // Calls below have their own locking mechanisms
  112. mContext = mGLSupport.createContext(LinuxPlatform::getXDisplay(), requestVI);
  113. if(mDesc.fullscreen && !mIsChild)
  114. setFullscreen(mDesc.videoMode);
  115. if(mDesc.hideUntilSwap || mDesc.hidden)
  116. setHidden(true);
  117. {
  118. ScopedSpinLock lock(mLock);
  119. mSyncedProperties = props;
  120. }
  121. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  122. RenderWindow::initialize();
  123. }
  124. void LinuxRenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  125. {
  126. THROW_IF_NOT_CORE_THREAD;
  127. if (mIsChild)
  128. return;
  129. const LinuxVideoModeInfo& videoModeInfo = static_cast<const LinuxVideoModeInfo&>(RenderAPI::instance() .getVideoModeInfo());
  130. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  131. if (numOutputs == 0)
  132. return;
  133. RenderWindowProperties& props = mProperties;
  134. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  135. const LinuxVideoOutputInfo& outputInfo = static_cast<const LinuxVideoOutputInfo&>(videoModeInfo.getOutputInfo (actualMonitorIdx));
  136. LinuxPlatform::lockX();
  137. ::Display* display = LinuxPlatform::getXDisplay();
  138. // Change video mode if required
  139. bool changeVideoMode = false;
  140. XRRScreenConfiguration* screenConfig = XRRGetScreenInfo(display, XRootWindow(display, DefaultScreen (display)));
  141. Rotation currentRotation;
  142. SizeID currentSizeID = XRRConfigCurrentConfiguration(screenConfig, &currentRotation);
  143. short currentRate = XRRConfigCurrentRate(screenConfig);
  144. int numSizes;
  145. XRRScreenSize* screenSizes = XRRConfigSizes(screenConfig, &numSizes);
  146. if(width != screenSizes[currentSizeID].width || height != screenSizes[currentSizeID].height ||
  147. currentRate != (short)refreshRate)
  148. changeVideoMode = true;
  149. // If provided mode matches current mode, avoid making the video mode change
  150. if(changeVideoMode)
  151. {
  152. // Remember the old config so we can restore it when exiting fullscreen
  153. if(mOldScreenConfig)
  154. {
  155. XRRFreeScreenConfigInfo(mOldScreenConfig);
  156. mOldScreenConfig = nullptr;
  157. }
  158. mOldScreenConfig = screenConfig;
  159. mOldConfigSizeID = currentSizeID;
  160. mOldConfigRate = currentRate;
  161. // Look for size that best matches our requested video mode
  162. bool foundSize = false;
  163. SizeID foundSizeID = 0;
  164. for(int i = 0; i < numSizes; i++)
  165. {
  166. UINT32 curWidth, curHeight;
  167. if(currentRotation == RR_Rotate_90 || currentRotation == RR_Rotate_270)
  168. {
  169. curWidth = screenSizes[i].height;
  170. curHeight = screenSizes[i].width;
  171. }
  172. else
  173. {
  174. curWidth = screenSizes[i].width;
  175. curHeight = screenSizes[i].height;
  176. }
  177. if(curWidth == width && curHeight == height)
  178. {
  179. foundSizeID = i;
  180. foundSize = true;
  181. break;
  182. }
  183. }
  184. if(!foundSize)
  185. LOGERR("Cannot change video mode, requested resolution not supported.");
  186. // Find refresh rate closest to the requested one, or fall back to 60
  187. if(foundSize)
  188. {
  189. int numRates;
  190. short* rates = XRRConfigRates(screenConfig, foundSizeID, &numRates);
  191. short bestRate = 60;
  192. for(int i = 0; i < numRates; i++)
  193. {
  194. if(rates[i] == (short)refreshRate)
  195. {
  196. bestRate = rates[i];
  197. break;
  198. }
  199. else
  200. {
  201. short diffNew = abs((short)refreshRate - rates[i]);
  202. short diffOld = abs((short)refreshRate - bestRate);
  203. if(diffNew < diffOld)
  204. bestRate = rates[i];
  205. }
  206. }
  207. XRRSetScreenConfigAndRate(display, screenConfig, XRootWindow(display, DefaultScreen(display)),
  208. foundSizeID, currentRotation, bestRate, CurrentTime);
  209. }
  210. }
  211. mWindow->_setFullscreen(true);
  212. LinuxPlatform::unlockX();
  213. props.isFullScreen = true;
  214. props.top = 0;
  215. props.left = 0;
  216. props.width = width;
  217. props.height = height;
  218. _windowMovedOrResized();
  219. }
  220. void LinuxRenderWindow::setFullscreen(const VideoMode& mode)
  221. {
  222. THROW_IF_NOT_CORE_THREAD;
  223. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  224. }
  225. void LinuxRenderWindow::setWindowed(UINT32 width, UINT32 height)
  226. {
  227. THROW_IF_NOT_CORE_THREAD;
  228. RenderWindowProperties& props = mProperties;
  229. if (!props.isFullScreen)
  230. return;
  231. props.isFullScreen = false;
  232. props.width = width;
  233. props.height = height;
  234. LinuxPlatform::lockX();
  235. // Restore old screen config
  236. if(mOldScreenConfig)
  237. {
  238. ::Display* display = LinuxPlatform::getXDisplay();
  239. XRRSetScreenConfigAndRate(display, mOldScreenConfig, XRootWindow(display, DefaultScreen(display)),
  240. mOldConfigSizeID, mOldConfigRotation, mOldConfigRate, CurrentTime);
  241. XRRFreeScreenConfigInfo(mOldScreenConfig);
  242. }
  243. mWindow->_setFullscreen(false);
  244. LinuxPlatform::unlockX();
  245. {
  246. ScopedSpinLock lock(mLock);
  247. mSyncedProperties.width = props.width;
  248. mSyncedProperties.height = props.height;
  249. }
  250. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  251. _windowMovedOrResized();
  252. }
  253. void LinuxRenderWindow::move(INT32 left, INT32 top)
  254. {
  255. THROW_IF_NOT_CORE_THREAD;
  256. RenderWindowProperties& props = mProperties;
  257. if (!props.isFullScreen)
  258. {
  259. LinuxPlatform::lockX();
  260. mWindow->move(left, top);
  261. LinuxPlatform::unlockX();
  262. props.top = mWindow->getTop();
  263. props.left = mWindow->getLeft();
  264. {
  265. ScopedSpinLock lock(mLock);
  266. mSyncedProperties.top = props.top;
  267. mSyncedProperties.left = props.left;
  268. }
  269. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  270. }
  271. }
  272. void LinuxRenderWindow::resize(UINT32 width, UINT32 height)
  273. {
  274. THROW_IF_NOT_CORE_THREAD;
  275. RenderWindowProperties& props = mProperties;
  276. if (!props.isFullScreen)
  277. {
  278. LinuxPlatform::lockX();
  279. mWindow->resize(width, height);
  280. LinuxPlatform::unlockX();
  281. props.width = mWindow->getWidth();
  282. props.height = mWindow->getHeight();
  283. {
  284. ScopedSpinLock lock(mLock);
  285. mSyncedProperties.width = props.width;
  286. mSyncedProperties.height = props.height;
  287. }
  288. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  289. }
  290. }
  291. void LinuxRenderWindow::minimize()
  292. {
  293. THROW_IF_NOT_CORE_THREAD;
  294. LinuxPlatform::lockX();
  295. mWindow->minimize();
  296. LinuxPlatform::unlockX();
  297. }
  298. void LinuxRenderWindow::maximize()
  299. {
  300. THROW_IF_NOT_CORE_THREAD;
  301. LinuxPlatform::lockX();
  302. mWindow->maximize();
  303. LinuxPlatform::unlockX();
  304. }
  305. void LinuxRenderWindow::restore()
  306. {
  307. THROW_IF_NOT_CORE_THREAD;
  308. LinuxPlatform::lockX();
  309. mWindow->restore();
  310. LinuxPlatform::unlockX();
  311. }
  312. void LinuxRenderWindow::swapBuffers(UINT32 syncMask)
  313. {
  314. THROW_IF_NOT_CORE_THREAD;
  315. if (mShowOnSwap)
  316. setHidden(false);
  317. LinuxPlatform::lockX();
  318. glXSwapBuffers(LinuxPlatform::getXDisplay(), mWindow->_getXWindow());
  319. LinuxPlatform::unlockX();
  320. }
  321. void LinuxRenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  322. {
  323. THROW_IF_NOT_CORE_THREAD;
  324. if ((dst.getRight() > getProperties().width) ||
  325. (dst.getBottom() > getProperties().height) ||
  326. (dst.getFront() != 0) || (dst.getBack() != 1))
  327. {
  328. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  329. }
  330. if (buffer == FB_AUTO)
  331. {
  332. buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
  333. }
  334. GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
  335. GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
  336. if ((format == GL_NONE) || (type == 0))
  337. {
  338. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  339. }
  340. // Must change the packing to ensure no overruns!
  341. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  342. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  343. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  344. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  345. format, type, dst.getData());
  346. // restore default alignment
  347. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  348. //vertical flip
  349. {
  350. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  351. size_t height = dst.getHeight();
  352. UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
  353. UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  354. while (tmpRow >= tmpData)
  355. {
  356. memcpy(tmpRow, srcRow, rowSpan);
  357. srcRow += rowSpan;
  358. tmpRow -= rowSpan;
  359. }
  360. memcpy(dst.getData(), tmpData, rowSpan * height);
  361. bs_free(tmpData);
  362. }
  363. }
  364. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  365. {
  366. if(name == "GLCONTEXT")
  367. {
  368. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(data);
  369. *contextPtr = mContext;
  370. return;
  371. }
  372. else if(name == "WINDOW")
  373. {
  374. LinuxWindow** window = (LinuxWindow**)data;
  375. *window = mWindow;
  376. return;
  377. }
  378. }
  379. void LinuxRenderWindow::setActive(bool state)
  380. {
  381. THROW_IF_NOT_CORE_THREAD;
  382. LinuxPlatform::lockX();
  383. if(state)
  384. mWindow->restore();
  385. else
  386. mWindow->minimize();
  387. LinuxPlatform::unlockX();
  388. RenderWindow::setActive(state);
  389. }
  390. void LinuxRenderWindow::setHidden(bool hidden)
  391. {
  392. THROW_IF_NOT_CORE_THREAD;
  393. mShowOnSwap = false;
  394. LinuxPlatform::lockX();
  395. if(hidden)
  396. mWindow->hide();
  397. else
  398. mWindow->show();
  399. LinuxPlatform::unlockX();
  400. RenderWindow::setHidden(hidden);
  401. }
  402. void LinuxRenderWindow::_windowMovedOrResized()
  403. {
  404. if (!mWindow)
  405. return;
  406. RenderWindowProperties& props = mProperties;
  407. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  408. {
  409. props.top = mWindow->getTop();
  410. props.left = mWindow->getLeft();
  411. props.width = mWindow->getWidth();
  412. props.height = mWindow->getHeight();
  413. }
  414. RenderWindow::_windowMovedOrResized();
  415. }
  416. void LinuxRenderWindow::syncProperties()
  417. {
  418. ScopedSpinLock lock(mLock);
  419. mProperties = mSyncedProperties;
  420. }
  421. }}