BsLinuxRenderWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "CoreThread/BsCoreThread.h"
  4. #include "Private/Linux/BsLinuxPlatform.h"
  5. #include "Private/Linux/BsLinuxWindow.h"
  6. #include "Linux/BsLinuxRenderWindow.h"
  7. #include "Linux/BsLinuxVideoModeInfo.h"
  8. #include "Math/BsMath.h"
  9. #include "Managers/BsRenderWindowManager.h"
  10. #include "Managers/BsVulkanCommandBufferManager.h"
  11. #include "BsVulkanRenderAPI.h"
  12. #include "BsVulkanDevice.h"
  13. #include "BsVulkanSwapChain.h"
  14. #include "BsVulkanQueue.h"
  15. #include <X11/Xutil.h>
  16. #define XRANDR_ROTATION_LEFT (1 << 1)
  17. #define XRANDR_ROTATION_RIGHT (1 << 3)
  18. namespace bs
  19. {
  20. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId)
  21. :RenderWindow(desc, windowId), mProperties(desc)
  22. { }
  23. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  24. {
  25. if (name == "WINDOW" || name == "LINUX_WINDOW")
  26. {
  27. blockUntilCoreInitialized();
  28. getCore()->getCustomAttribute(name, data);
  29. return;
  30. }
  31. }
  32. Vector2I LinuxRenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  33. {
  34. blockUntilCoreInitialized();
  35. LinuxPlatform::lockX();
  36. Vector2I pos = getCore()->_getInternal()->screenToWindowPos(screenPos);
  37. LinuxPlatform::unlockX();
  38. return pos;
  39. }
  40. Vector2I LinuxRenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  41. {
  42. blockUntilCoreInitialized();
  43. LinuxPlatform::lockX();
  44. Vector2I pos = getCore()->_getInternal()->windowToScreenPos(windowPos);
  45. LinuxPlatform::unlockX();
  46. return pos;
  47. }
  48. SPtr<ct::LinuxRenderWindow> LinuxRenderWindow::getCore() const
  49. {
  50. return std::static_pointer_cast<ct::LinuxRenderWindow>(mCoreSpecific);
  51. }
  52. SPtr<ct::CoreObject> LinuxRenderWindow::createCore() const
  53. {
  54. ct::VulkanRenderAPI& rapi = static_cast<ct::VulkanRenderAPI&>(ct::RenderAPI::instance());
  55. RENDER_WINDOW_DESC desc = mDesc;
  56. SPtr<ct::CoreObject> coreObj = bs_shared_ptr_new<ct::LinuxRenderWindow>(desc, mWindowId, rapi);
  57. coreObj->_setThisPtr(coreObj);
  58. return coreObj;
  59. }
  60. void LinuxRenderWindow::syncProperties()
  61. {
  62. ScopedSpinLock lock(getCore()->_getPropertiesLock());
  63. mProperties = getCore()->mSyncedProperties;
  64. }
  65. namespace ct
  66. {
  67. LinuxRenderWindow::LinuxRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, VulkanRenderAPI& renderAPI)
  68. : RenderWindow(desc, windowId), mRenderAPI(renderAPI), mRequiresNewBackBuffer(true), mWindow(nullptr)
  69. , mProperties(desc), mSyncedProperties(desc), mIsChild(false), mShowOnSwap(false)
  70. { }
  71. LinuxRenderWindow::~LinuxRenderWindow()
  72. {
  73. // Make sure to set the original desktop video mode before we exit
  74. if(mProperties.isFullScreen)
  75. setWindowed(50, 50);
  76. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  77. presentDevice->waitIdle();
  78. if (mWindow != nullptr)
  79. {
  80. Platform::resetNonClientAreas(*this);
  81. LinuxPlatform::lockX();
  82. bs_delete(mWindow);
  83. mWindow = nullptr;
  84. LinuxPlatform::unlockX();
  85. }
  86. mSwapChain = nullptr;
  87. vkDestroySurfaceKHR(mRenderAPI._getInstance(), mSurface, gVulkanAllocator);
  88. }
  89. void LinuxRenderWindow::initialize()
  90. {
  91. LinuxPlatform::lockX();
  92. RenderWindowProperties& props = mProperties;
  93. props.isFullScreen = mDesc.fullscreen;
  94. mIsChild = false;
  95. XVisualInfo visualInfoTempl = {};
  96. visualInfoTempl.screen = XDefaultScreen(LinuxPlatform::getXDisplay());
  97. visualInfoTempl.depth = 24;
  98. visualInfoTempl.c_class = TrueColor;
  99. int32_t numVisuals;
  100. XVisualInfo* visualInfo = XGetVisualInfo(LinuxPlatform::getXDisplay(),
  101. VisualScreenMask | VisualDepthMask | VisualClassMask, &visualInfoTempl, &numVisuals);
  102. WINDOW_DESC windowDesc;
  103. windowDesc.x = mDesc.left;
  104. windowDesc.y = mDesc.top;
  105. windowDesc.width = mDesc.videoMode.getWidth();
  106. windowDesc.height = mDesc.videoMode.getHeight();
  107. windowDesc.title = mDesc.title;
  108. windowDesc.showDecorations = mDesc.showTitleBar;
  109. windowDesc.allowResize = mDesc.allowResize;
  110. windowDesc.showOnTaskBar = !mDesc.toolWindow;
  111. windowDesc.modal = mDesc.modal;
  112. windowDesc.visualInfo = *visualInfo;
  113. windowDesc.screen = mDesc.videoMode.getOutputIdx();
  114. windowDesc.hidden = mDesc.hideUntilSwap || mDesc.hidden;
  115. NameValuePairList::const_iterator opt;
  116. opt = mDesc.platformSpecific.find("parentWindowHandle");
  117. if (opt != mDesc.platformSpecific.end())
  118. windowDesc.parent = (::Window)parseUINT64(opt->second);
  119. else
  120. windowDesc.parent = 0;
  121. mIsChild = windowDesc.parent != 0;
  122. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  123. mShowOnSwap = mDesc.hideUntilSwap && !mDesc.hidden;
  124. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  125. mWindow = bs_new<LinuxWindow>(windowDesc);
  126. mWindow->_setUserData(this);
  127. props.width = mWindow->getWidth();
  128. props.height = mWindow->getHeight();
  129. props.top = mWindow->getTop();
  130. props.left = mWindow->getLeft();
  131. props.hwGamma = mDesc.gamma;
  132. props.multisampleCount = mDesc.multisampleCount;
  133. XWindowAttributes windowAttributes;
  134. XGetWindowAttributes(LinuxPlatform::getXDisplay(), mWindow->_getXWindow(), &windowAttributes);
  135. // Create Vulkan surface
  136. VkXlibSurfaceCreateInfoKHR surfaceCreateInfo;
  137. surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
  138. surfaceCreateInfo.pNext = nullptr;
  139. surfaceCreateInfo.flags = 0;
  140. surfaceCreateInfo.window = mWindow->_getXWindow();
  141. surfaceCreateInfo.dpy = LinuxPlatform::getXDisplay();
  142. // Note: I manually lock Xlib, while Vulkan spec says XInitThreads should be called, since Vulkan
  143. // surely calls Xlib under the hood as well. I've tried to guess which calls use Xlib and lock them
  144. // externally, but XInitThreads might be required if problems occur.
  145. VkInstance instance = mRenderAPI._getInstance();
  146. VkResult result = vkCreateXlibSurfaceKHR(instance, &surfaceCreateInfo, gVulkanAllocator, &mSurface);
  147. assert(result == VK_SUCCESS);
  148. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  149. VkPhysicalDevice physicalDevice = presentDevice->getPhysical();
  150. mPresentQueueFamily = presentDevice->getQueueFamily(GQT_GRAPHICS);
  151. VkBool32 supportsPresent;
  152. vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, mPresentQueueFamily, mSurface, &supportsPresent);
  153. if(!supportsPresent)
  154. {
  155. // Note: Not supporting present only queues at the moment
  156. // Note: Also present device can only return one family of graphics queue, while there could be more (some of
  157. // which support present)
  158. BS_EXCEPT(RenderingAPIException, "Cannot find a graphics queue that also supports present operations.");
  159. }
  160. SurfaceFormat format = presentDevice->getSurfaceFormat(mSurface, mDesc.gamma);
  161. mColorFormat = format.colorFormat;
  162. mColorSpace = format.colorSpace;
  163. mDepthFormat = format.depthFormat;
  164. // Create swap chain
  165. mSwapChain = bs_shared_ptr_new<VulkanSwapChain>();
  166. mSwapChain->rebuild(presentDevice, mSurface, props.width, props.height, props.vsync, mColorFormat, mColorSpace,
  167. mDesc.depthBuffer, mDepthFormat);
  168. LinuxPlatform::unlockX(); // Calls below have their own locking mechanisms
  169. if(mDesc.fullscreen && !mIsChild)
  170. setFullscreen(mDesc.videoMode);
  171. if(mDesc.vsync && mDesc.vsyncInterval > 0)
  172. setVSync(true, mDesc.vsyncInterval);
  173. {
  174. ScopedSpinLock lock(mLock);
  175. mSyncedProperties = props;
  176. }
  177. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  178. RenderWindow::initialize();
  179. }
  180. void LinuxRenderWindow::acquireBackBuffer()
  181. {
  182. // We haven't presented the current back buffer yet, so just use that one
  183. if (!mRequiresNewBackBuffer)
  184. return;
  185. mSwapChain->acquireBackBuffer();
  186. mRequiresNewBackBuffer = false;
  187. }
  188. void LinuxRenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  189. {
  190. THROW_IF_NOT_CORE_THREAD;
  191. VideoMode videoMode(width, height, refreshRate, monitorIdx);
  192. setFullscreen(videoMode);
  193. }
  194. void LinuxRenderWindow::setVideoMode(INT32 screen, RROutput output, RRMode mode)
  195. {
  196. ::Display* display = LinuxPlatform::getXDisplay();
  197. ::Window rootWindow = RootWindow(display, screen);
  198. XRRScreenResources* screenRes = XRRGetScreenResources (display, rootWindow);
  199. if(screenRes == nullptr)
  200. {
  201. LOGERR("XRR: Failed to retrieve screen resources. ");
  202. return;
  203. }
  204. XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screenRes, output);
  205. if(outputInfo == nullptr)
  206. {
  207. XRRFreeScreenResources(screenRes);
  208. LOGERR("XRR: Failed to retrieve output info for output: " + toString((UINT32)output));
  209. return;
  210. }
  211. XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screenRes, outputInfo->crtc);
  212. if(crtcInfo == nullptr)
  213. {
  214. XRRFreeScreenResources(screenRes);
  215. XRRFreeOutputInfo(outputInfo);
  216. LOGERR("XRR: Failed to retrieve CRTC info for output: " + toString((UINT32)output));
  217. return;
  218. }
  219. // Note: This changes the user's desktop resolution permanently, even when the app exists, make sure to revert
  220. // (Sadly there doesn't appear to be a better way)
  221. Status status = XRRSetCrtcConfig (display, screenRes, outputInfo->crtc, CurrentTime,
  222. crtcInfo->x, crtcInfo->y, mode, crtcInfo->rotation, &output, 1);
  223. if(status != Success)
  224. LOGERR("XRR: XRRSetCrtcConfig failed.");
  225. XRRFreeCrtcInfo(crtcInfo);
  226. XRRFreeOutputInfo(outputInfo);
  227. XRRFreeScreenResources(screenRes);
  228. }
  229. void LinuxRenderWindow::setFullscreen(const VideoMode& mode)
  230. {
  231. THROW_IF_NOT_CORE_THREAD;
  232. if (mIsChild)
  233. return;
  234. const LinuxVideoModeInfo& videoModeInfo =
  235. static_cast<const LinuxVideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  236. UINT32 outputIdx = mode.getOutputIdx();
  237. if(outputIdx >= videoModeInfo.getNumOutputs())
  238. {
  239. LOGERR("Invalid output device index.")
  240. return;
  241. }
  242. const LinuxVideoOutputInfo& outputInfo =
  243. static_cast<const LinuxVideoOutputInfo&>(videoModeInfo.getOutputInfo (outputIdx));
  244. INT32 screen = outputInfo._getScreen();
  245. RROutput outputID = outputInfo._getOutputID();
  246. RRMode modeID = 0;
  247. if(!mode.isCustom())
  248. {
  249. const LinuxVideoMode& videoMode = static_cast<const LinuxVideoMode&>(mode);
  250. modeID = videoMode._getModeID();
  251. }
  252. else
  253. {
  254. LinuxPlatform::lockX();
  255. // Look for mode matching the requested resolution
  256. ::Display* display = LinuxPlatform::getXDisplay();
  257. ::Window rootWindow = RootWindow(display, screen);
  258. XRRScreenResources* screenRes = XRRGetScreenResources(display, rootWindow);
  259. if (screenRes == nullptr)
  260. {
  261. LOGERR("XRR: Failed to retrieve screen resources. ");
  262. return;
  263. }
  264. XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screenRes, outputID);
  265. if (outputInfo == nullptr)
  266. {
  267. XRRFreeScreenResources(screenRes);
  268. LOGERR("XRR: Failed to retrieve output info for output: " + toString((UINT32)outputID));
  269. return;
  270. }
  271. XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screenRes, outputInfo->crtc);
  272. if (crtcInfo == nullptr)
  273. {
  274. XRRFreeScreenResources(screenRes);
  275. XRRFreeOutputInfo(outputInfo);
  276. LOGERR("XRR: Failed to retrieve CRTC info for output: " + toString((UINT32)outputID));
  277. return;
  278. }
  279. bool foundMode = false;
  280. for (INT32 i = 0; i < screenRes->nmode; i++)
  281. {
  282. const XRRModeInfo& modeInfo = screenRes->modes[i];
  283. UINT32 width, height;
  284. if (crtcInfo->rotation & (XRANDR_ROTATION_LEFT | XRANDR_ROTATION_RIGHT))
  285. {
  286. width = modeInfo.height;
  287. height = modeInfo.width;
  288. }
  289. else
  290. {
  291. width = modeInfo.width;
  292. height = modeInfo.height;
  293. }
  294. float refreshRate;
  295. if (modeInfo.hTotal != 0 && modeInfo.vTotal != 0)
  296. refreshRate = (float) (modeInfo.dotClock / (double) (modeInfo.hTotal * modeInfo.vTotal));
  297. else
  298. refreshRate = 0.0f;
  299. if (width == mode.getWidth() && height == mode.getHeight())
  300. {
  301. modeID = modeInfo.id;
  302. foundMode = true;
  303. if (Math::approxEquals(refreshRate, mode.getRefreshRate()))
  304. break;
  305. }
  306. }
  307. if (!foundMode)
  308. {
  309. LinuxPlatform::unlockX();
  310. LOGERR("Unable to enter fullscreen, unsupported video mode requested.");
  311. return;
  312. }
  313. LinuxPlatform::unlockX();
  314. }
  315. LinuxPlatform::lockX();
  316. setVideoMode(screen, outputID, modeID);
  317. mWindow->_setFullscreen(true);
  318. LinuxPlatform::unlockX();
  319. RenderWindowProperties& props = mProperties;
  320. props.isFullScreen = true;
  321. props.top = 0;
  322. props.left = 0;
  323. props.width = mode.getWidth();
  324. props.height = mode.getHeight();
  325. _windowMovedOrResized();
  326. {
  327. ScopedSpinLock lock(mLock);
  328. mSyncedProperties.left = props.left;
  329. mSyncedProperties.top = props.top;
  330. mSyncedProperties.width = props.width;
  331. mSyncedProperties.height = props.height;
  332. }
  333. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  334. bs::RenderWindowManager::instance().notifyMovedOrResized(this);
  335. }
  336. void LinuxRenderWindow::setWindowed(UINT32 width, UINT32 height)
  337. {
  338. THROW_IF_NOT_CORE_THREAD;
  339. RenderWindowProperties& props = mProperties;
  340. if (!props.isFullScreen)
  341. return;
  342. // Restore old screen config
  343. const LinuxVideoModeInfo& videoModeInfo =
  344. static_cast<const LinuxVideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  345. UINT32 outputIdx = 0; // 0 is always primary
  346. if(outputIdx >= videoModeInfo.getNumOutputs())
  347. {
  348. LOGERR("Invalid output device index.")
  349. return;
  350. }
  351. const LinuxVideoOutputInfo& outputInfo =
  352. static_cast<const LinuxVideoOutputInfo&>(videoModeInfo.getOutputInfo (outputIdx));
  353. const LinuxVideoMode& desktopVideoMode = static_cast<const LinuxVideoMode&>(outputInfo.getDesktopVideoMode());
  354. LinuxPlatform::lockX();
  355. setVideoMode(outputInfo._getScreen(), outputInfo._getOutputID(), desktopVideoMode._getModeID());
  356. mWindow->_setFullscreen(false);
  357. LinuxPlatform::unlockX();
  358. props.isFullScreen = false;
  359. props.width = width;
  360. props.height = height;
  361. _windowMovedOrResized();
  362. {
  363. ScopedSpinLock lock(mLock);
  364. mSyncedProperties.left = props.left;
  365. mSyncedProperties.top = props.top;
  366. mSyncedProperties.width = props.width;
  367. mSyncedProperties.height = props.height;
  368. }
  369. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  370. bs::RenderWindowManager::instance().notifyMovedOrResized(this);
  371. }
  372. void LinuxRenderWindow::move(INT32 left, INT32 top)
  373. {
  374. THROW_IF_NOT_CORE_THREAD;
  375. RenderWindowProperties& props = mProperties;
  376. if (!props.isFullScreen)
  377. {
  378. LinuxPlatform::lockX();
  379. mWindow->move(left, top);
  380. LinuxPlatform::unlockX();
  381. props.top = mWindow->getTop();
  382. props.left = mWindow->getLeft();
  383. {
  384. ScopedSpinLock lock(mLock);
  385. mSyncedProperties.top = props.top;
  386. mSyncedProperties.left = props.left;
  387. }
  388. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  389. }
  390. }
  391. void LinuxRenderWindow::resize(UINT32 width, UINT32 height)
  392. {
  393. THROW_IF_NOT_CORE_THREAD;
  394. RenderWindowProperties& props = mProperties;
  395. if (!props.isFullScreen)
  396. {
  397. LinuxPlatform::lockX();
  398. mWindow->resize(width, height);
  399. LinuxPlatform::unlockX();
  400. props.width = mWindow->getWidth();
  401. props.height = mWindow->getHeight();
  402. {
  403. ScopedSpinLock lock(mLock);
  404. mSyncedProperties.width = props.width;
  405. mSyncedProperties.height = props.height;
  406. }
  407. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  408. }
  409. }
  410. void LinuxRenderWindow::minimize()
  411. {
  412. THROW_IF_NOT_CORE_THREAD;
  413. LinuxPlatform::lockX();
  414. mWindow->minimize();
  415. LinuxPlatform::unlockX();
  416. }
  417. void LinuxRenderWindow::maximize()
  418. {
  419. THROW_IF_NOT_CORE_THREAD;
  420. LinuxPlatform::lockX();
  421. mWindow->maximize();
  422. LinuxPlatform::unlockX();
  423. }
  424. void LinuxRenderWindow::restore()
  425. {
  426. THROW_IF_NOT_CORE_THREAD;
  427. LinuxPlatform::lockX();
  428. mWindow->restore();
  429. LinuxPlatform::unlockX();
  430. }
  431. void LinuxRenderWindow::setVSync(bool enabled, UINT32 interval)
  432. {
  433. THROW_IF_NOT_CORE_THREAD;
  434. if(!enabled)
  435. interval = 0;
  436. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  437. presentDevice->waitIdle();
  438. LinuxPlatform::lockX();
  439. mSwapChain->rebuild(presentDevice, mSurface, mProperties.width, mProperties.height, enabled, mColorFormat,
  440. mColorSpace, mDesc.depthBuffer, mDepthFormat);
  441. LinuxPlatform::unlockX();
  442. mProperties.vsync = enabled;
  443. mProperties.vsyncInterval = interval;
  444. {
  445. ScopedSpinLock lock(mLock);
  446. mSyncedProperties.vsync = enabled;
  447. mSyncedProperties.vsyncInterval = interval;
  448. }
  449. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  450. }
  451. void LinuxRenderWindow::swapBuffers(UINT32 syncMask)
  452. {
  453. THROW_IF_NOT_CORE_THREAD;
  454. if (mShowOnSwap)
  455. setHidden(false);
  456. LinuxPlatform::lockX();
  457. // Get a command buffer on which we'll submit
  458. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  459. // Assuming present queue is always graphics
  460. assert(presentDevice->getQueueFamily(GQT_GRAPHICS) == mPresentQueueFamily);
  461. // Find an appropriate queue to execute on
  462. VulkanQueue* queue = presentDevice->getQueue(GQT_GRAPHICS, 0);
  463. UINT32 queueMask = presentDevice->getQueueMask(GQT_GRAPHICS, 0);
  464. // Ignore myself
  465. syncMask &= ~queueMask;
  466. UINT32 deviceIdx = presentDevice->getIndex();
  467. VulkanCommandBufferManager& cbm = static_cast<VulkanCommandBufferManager&>(CommandBufferManager::instance());
  468. UINT32 numSemaphores;
  469. cbm.getSyncSemaphores(deviceIdx, syncMask, mSemaphoresTemp, numSemaphores);
  470. // Wait on present (i.e. until the back buffer becomes available), if we haven't already done so
  471. const SwapChainSurface& surface = mSwapChain->getBackBuffer();
  472. if(surface.needsWait)
  473. {
  474. mSemaphoresTemp[numSemaphores] = mSwapChain->getBackBuffer().sync;
  475. numSemaphores++;
  476. mSwapChain->notifyBackBufferWaitIssued();
  477. }
  478. queue->present(mSwapChain.get(), mSemaphoresTemp, numSemaphores);
  479. mRequiresNewBackBuffer = true;
  480. LinuxPlatform::unlockX();
  481. }
  482. void LinuxRenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  483. {
  484. THROW_IF_NOT_CORE_THREAD;
  485. assert(false && "Not implemented");
  486. }
  487. void LinuxRenderWindow::getCustomAttribute(const String& name, void* data) const
  488. {
  489. if (name == "FB")
  490. {
  491. VulkanFramebuffer** fb = (VulkanFramebuffer**)data;
  492. *fb = mSwapChain->getBackBuffer().framebuffer;
  493. return;
  494. }
  495. if(name == "SC")
  496. {
  497. VulkanSwapChain** sc = (VulkanSwapChain**)data;
  498. *sc = mSwapChain.get();
  499. return;
  500. }
  501. if(name == "LINUX_WINDOW")
  502. {
  503. LinuxWindow** window = (LinuxWindow**)data;
  504. *window = mWindow;
  505. return;
  506. }
  507. if(name == "WINDOW")
  508. {
  509. ::Window* window = (::Window*)data;
  510. *window = mWindow->_getXWindow();
  511. return;
  512. }
  513. }
  514. void LinuxRenderWindow::setActive(bool state)
  515. {
  516. THROW_IF_NOT_CORE_THREAD;
  517. LinuxPlatform::lockX();
  518. if(state)
  519. mWindow->restore();
  520. else
  521. mWindow->minimize();
  522. LinuxPlatform::unlockX();
  523. RenderWindow::setActive(state);
  524. }
  525. void LinuxRenderWindow::setHidden(bool hidden)
  526. {
  527. THROW_IF_NOT_CORE_THREAD;
  528. if(!hidden)
  529. mShowOnSwap = false;
  530. LinuxPlatform::lockX();
  531. if(hidden)
  532. mWindow->hide();
  533. else
  534. mWindow->show();
  535. LinuxPlatform::unlockX();
  536. RenderWindow::setHidden(hidden);
  537. }
  538. void LinuxRenderWindow::_windowMovedOrResized()
  539. {
  540. if (!mWindow)
  541. return;
  542. RenderWindowProperties& props = mProperties;
  543. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  544. {
  545. props.top = mWindow->getTop();
  546. props.left = mWindow->getLeft();
  547. props.width = mWindow->getWidth();
  548. props.height = mWindow->getHeight();
  549. }
  550. // Resize swap chain
  551. //// Need to make sure nothing is using the swap buffer before we re-create it
  552. // Note: Optionally I can detect exactly on which queues (if any) are the swap chain images used on, and only wait
  553. // on those
  554. SPtr<VulkanDevice> presentDevice = mRenderAPI._getPresentDevice();
  555. presentDevice->waitIdle();
  556. // Note: This assumes that this method was called from the main message loop, which already acquires X locks,
  557. // so no need to lock here explicitly
  558. mSwapChain->rebuild(presentDevice, mSurface, props.width, props.height, props.vsync, mColorFormat, mColorSpace,
  559. mDesc.depthBuffer, mDepthFormat);
  560. }
  561. void LinuxRenderWindow::syncProperties()
  562. {
  563. ScopedSpinLock lock(mLock);
  564. mProperties = mSyncedProperties;
  565. }
  566. }}