BsMacOSRenderWindow.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #define BS_COCOA_INTERNALS
  4. #include "MacOS/BsMacOSVideoModeInfo.h"
  5. #include "Private/MacOS/BsMacOSWindow.h"
  6. #include "Math/BsMath.h"
  7. #include "CoreThread/BsCoreThread.h"
  8. #include "MacOS/BsMacOSRenderWindow.h"
  9. #include "MacOS/BsMacOSGLSupport.h"
  10. #include "MacOS/BsMacOSContext.h"
  11. #include "BsGLRenderWindowManager.h"
  12. #include "BsGLPixelFormat.h"
  13. namespace bs
  14. {
  15. MacOSRenderWindow::MacOSRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, ct::MacOSGLSupport& glSupport)
  16. :RenderWindow(desc, windowId), mProperties(desc), mGLSupport(glSupport)
  17. { }
  18. void MacOSRenderWindow::initialize()
  19. {
  20. RenderWindowProperties& props = mProperties;
  21. props.isFullScreen = mDesc.fullscreen;
  22. mIsChild = false;
  23. WINDOW_DESC windowDesc;
  24. windowDesc.x = mDesc.left;
  25. windowDesc.y = mDesc.top;
  26. windowDesc.width = mDesc.videoMode.getWidth();
  27. windowDesc.height = mDesc.videoMode.getHeight();
  28. windowDesc.title = mDesc.title;
  29. windowDesc.showDecorations = mDesc.showTitleBar;
  30. windowDesc.allowResize = mDesc.allowResize;
  31. windowDesc.modal = mDesc.modal;
  32. windowDesc.floating = mDesc.toolWindow;
  33. auto iter = mDesc.platformSpecific.find("parentWindowHandle");
  34. mIsChild = iter != mDesc.platformSpecific.end();
  35. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  36. props.isHidden = mDesc.hidden;
  37. mWindow = bs_new<CocoaWindow>(windowDesc);
  38. mWindow->_setUserData(this);
  39. Rect2I area = mWindow->getArea();
  40. props.width = area.width;
  41. props.height = area.height;
  42. props.top = area.y;
  43. props.left = area.x;
  44. props.hwGamma = mDesc.gamma;
  45. props.multisampleCount = mDesc.multisampleCount;
  46. mContext = mGLSupport.createContext(mDesc.depthBuffer, mDesc.multisampleCount);
  47. if(mDesc.fullscreen && !mIsChild)
  48. setFullscreen(mDesc.videoMode);
  49. RenderWindow::initialize();
  50. if(props.isHidden)
  51. mWindow->hide();
  52. {
  53. ScopedSpinLock lock(getCore()->mLock);
  54. getCore()->mSyncedProperties = props;
  55. }
  56. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  57. }
  58. void MacOSRenderWindow::destroy()
  59. {
  60. // Make sure to set the original desktop video mode before we exit
  61. if(mProperties.isFullScreen)
  62. setWindowed(50, 50);
  63. if (mWindow != nullptr)
  64. {
  65. bs_delete(mWindow);
  66. mWindow = nullptr;
  67. }
  68. RenderWindow::destroy();
  69. }
  70. SPtr<ct::CoreObject> MacOSRenderWindow::createCore() const
  71. {
  72. RENDER_WINDOW_DESC desc = mDesc;
  73. SPtr<ct::CoreObject> obj = bs_shared_ptr_new<ct::MacOSRenderWindow>(
  74. desc, mWindowId, mWindow->_getWindowId(), mContext);
  75. obj->_setThisPtr(obj);
  76. return obj;
  77. }
  78. void MacOSRenderWindow::resize(UINT32 width, UINT32 height)
  79. {
  80. RenderWindowProperties& props = mProperties;
  81. if (!props.isFullScreen)
  82. {
  83. mWindow->resize(width, height);
  84. Rect2I area = mWindow->getArea();
  85. props.width = area.width;
  86. props.height = area.height;
  87. {
  88. ScopedSpinLock lock(getCore()->mLock);
  89. getCore()->getSyncedProperties().width = width;
  90. getCore()->getSyncedProperties().height = height;
  91. }
  92. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  93. }
  94. }
  95. void MacOSRenderWindow::move(INT32 left, INT32 top)
  96. {
  97. RenderWindowProperties& props = mProperties;
  98. if (!props.isFullScreen)
  99. {
  100. mWindow->move(left, top);
  101. Rect2I area = mWindow->getArea();
  102. props.top = area.y;
  103. props.left = area.x;
  104. {
  105. ScopedSpinLock lock(getCore()->mLock);
  106. getCore()->getSyncedProperties().top = props.top;
  107. getCore()->getSyncedProperties().left = props.left;
  108. }
  109. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  110. }
  111. }
  112. void MacOSRenderWindow::hide()
  113. {
  114. getMutableProperties().isHidden = true;
  115. {
  116. ScopedSpinLock lock(getCore()->mLock);
  117. getCore()->getSyncedProperties().isHidden = true;
  118. }
  119. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  120. mWindow->hide();
  121. }
  122. void MacOSRenderWindow::show()
  123. {
  124. getMutableProperties().isHidden = false;
  125. {
  126. ScopedSpinLock lock(getCore()->mLock);
  127. getCore()->getSyncedProperties().isHidden = false;
  128. }
  129. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  130. mWindow->show();
  131. }
  132. void MacOSRenderWindow::minimize()
  133. {
  134. RenderWindowProperties& props = mProperties;
  135. props.isMaximized = false;
  136. {
  137. ScopedSpinLock lock(getCore()->mLock);
  138. getCore()->getSyncedProperties().isMaximized = false;
  139. }
  140. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  141. mWindow->minimize();
  142. }
  143. void MacOSRenderWindow::maximize()
  144. {
  145. RenderWindowProperties& props = mProperties;
  146. props.isMaximized = true;
  147. {
  148. ScopedSpinLock lock(getCore()->mLock);
  149. getCore()->getSyncedProperties().isMaximized = true;
  150. }
  151. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  152. mWindow->maximize();
  153. }
  154. void MacOSRenderWindow::restore()
  155. {
  156. RenderWindowProperties& props = mProperties;
  157. props.isMaximized = false;
  158. {
  159. ScopedSpinLock lock(getCore()->mLock);
  160. getCore()->getSyncedProperties().isMaximized = false;
  161. }
  162. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  163. mWindow->restore();
  164. }
  165. void MacOSRenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  166. {
  167. VideoMode videoMode(width, height, refreshRate, monitorIdx);
  168. setFullscreen(videoMode);
  169. }
  170. void MacOSRenderWindow::setFullscreen(const VideoMode& videoMode)
  171. {
  172. if (mIsChild)
  173. return;
  174. const VideoModeInfo& videoModeInfo = ct::RenderAPI::instance().getVideoModeInfo();
  175. UINT32 outputIdx = videoMode.getOutputIdx();
  176. if(outputIdx >= videoModeInfo.getNumOutputs())
  177. {
  178. LOGERR("Invalid output device index.")
  179. return;
  180. }
  181. const VideoOutputInfo& outputInfo = videoModeInfo.getOutputInfo (outputIdx);
  182. if(!videoMode.isCustom())
  183. setDisplayMode(outputInfo, videoMode);
  184. else
  185. {
  186. // Look for mode matching the requested resolution
  187. UINT32 foundMode = (UINT32)-1;
  188. UINT32 numModes = outputInfo.getNumVideoModes();
  189. for (UINT32 i = 0; i < numModes; i++)
  190. {
  191. const VideoMode& currentMode = outputInfo.getVideoMode(i);
  192. if (currentMode.getWidth() == videoMode.getWidth() && currentMode.getHeight() == videoMode.getHeight())
  193. {
  194. foundMode = i;
  195. if (Math::approxEquals(currentMode.getRefreshRate(), videoMode.getRefreshRate()))
  196. break;
  197. }
  198. }
  199. if (foundMode == (UINT32)-1)
  200. {
  201. LOGERR("Unable to enter fullscreen, unsupported video mode requested.");
  202. return;
  203. }
  204. setDisplayMode(outputInfo, outputInfo.getVideoMode(foundMode));
  205. }
  206. mWindow->setFullscreen();
  207. RenderWindowProperties& props = mProperties;
  208. props.isFullScreen = true;
  209. props.top = 0;
  210. props.left = 0;
  211. props.width = videoMode.getWidth();
  212. props.height = videoMode.getHeight();
  213. {
  214. ScopedSpinLock lock(getCore()->mLock);
  215. getCore()->getSyncedProperties().top = props.top;
  216. getCore()->getSyncedProperties().left = props.left;
  217. getCore()->getSyncedProperties().width = props.width;
  218. getCore()->getSyncedProperties().height = props.height;
  219. }
  220. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  221. _windowMovedOrResized();
  222. }
  223. void MacOSRenderWindow::setWindowed(UINT32 width, UINT32 height)
  224. {
  225. RenderWindowProperties& props = mProperties;
  226. if (!props.isFullScreen)
  227. return;
  228. // Restore original display mode
  229. const VideoModeInfo& videoModeInfo = ct::RenderAPI::instance().getVideoModeInfo();
  230. UINT32 outputIdx = 0; // 0 is always primary
  231. if(outputIdx >= videoModeInfo.getNumOutputs())
  232. {
  233. LOGERR("Invalid output device index.")
  234. return;
  235. }
  236. const VideoOutputInfo& outputInfo = videoModeInfo.getOutputInfo(outputIdx);
  237. setDisplayMode(outputInfo, outputInfo.getDesktopVideoMode());
  238. mWindow->setWindowed();
  239. props.isFullScreen = false;
  240. props.width = width;
  241. props.height = height;
  242. {
  243. ScopedSpinLock lock(getCore()->mLock);
  244. getCore()->getSyncedProperties().width = props.width;
  245. getCore()->getSyncedProperties().height = props.height;
  246. }
  247. ct::RenderWindowManager::instance().notifySyncDataDirty(getCore().get());
  248. _windowMovedOrResized();
  249. }
  250. void MacOSRenderWindow::setDisplayMode(const VideoOutputInfo& output, const VideoMode& mode)
  251. {
  252. CGDisplayFadeReservationToken fadeToken = kCGDisplayFadeReservationInvalidToken;
  253. if (CGAcquireDisplayFadeReservation(5.0f, &fadeToken))
  254. CGDisplayFade(fadeToken, 0.3f, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, TRUE);
  255. auto& destOutput = static_cast<const ct::MacOSVideoOutputInfo&>(output);
  256. auto& newMode = static_cast<const ct::MacOSVideoMode&>(mode);
  257. // Note: An alternative to changing display resolution would be to only change the back-buffer size. But that doesn't
  258. // account for refresh rate, so it's questionable how useful it would be.
  259. CGDirectDisplayID displayID = destOutput._getDisplayID();
  260. CGDisplaySetDisplayMode(displayID, newMode._getModeRef(), nullptr);
  261. if (fadeToken != kCGDisplayFadeReservationInvalidToken)
  262. {
  263. CGDisplayFade(fadeToken, 0.3f, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, FALSE);
  264. CGReleaseDisplayFadeReservation(fadeToken);
  265. }
  266. }
  267. void MacOSRenderWindow::getCustomAttribute(const String& name, void* data) const
  268. {
  269. if(name == "COCOA_WINDOW")
  270. {
  271. CocoaWindow** window = (CocoaWindow**)data;
  272. *window = mWindow;
  273. return;
  274. }
  275. else if(name == "WINDOW_ID")
  276. {
  277. UINT32* windowId = (UINT32*)data;
  278. *windowId = mWindow->_getWindowId();
  279. }
  280. }
  281. Vector2I MacOSRenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  282. {
  283. return mWindow->screenToWindowPos(screenPos);
  284. }
  285. Vector2I MacOSRenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  286. {
  287. return mWindow->windowToScreenPos(windowPos);
  288. }
  289. SPtr<ct::MacOSRenderWindow> MacOSRenderWindow::getCore() const
  290. {
  291. return std::static_pointer_cast<ct::MacOSRenderWindow>(mCoreSpecific);
  292. }
  293. void MacOSRenderWindow::_windowMovedOrResized()
  294. {
  295. if (!mWindow)
  296. return;
  297. RenderWindowProperties& props = mProperties;
  298. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  299. {
  300. Rect2I area = mWindow->getArea();
  301. props.top = area.y;
  302. props.left = area.x;
  303. props.width = area.width;
  304. props.height = area.height;
  305. }
  306. {
  307. ScopedSpinLock lock(getCore()->mLock);
  308. getCore()->getSyncedProperties().top = props.top;
  309. getCore()->getSyncedProperties().left = props.left;
  310. getCore()->getSyncedProperties().width = props.width;
  311. getCore()->getSyncedProperties().height = props.height;
  312. }
  313. mContext->markAsDirty();
  314. }
  315. void MacOSRenderWindow::syncProperties()
  316. {
  317. ScopedSpinLock lock(getCore()->_getPropertiesLock());
  318. mProperties = getCore()->mSyncedProperties;
  319. }
  320. namespace ct
  321. {
  322. MacOSRenderWindow::MacOSRenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 renderWindowId, UINT32 cocoaWindowId,
  323. const SPtr<MacOSContext>& context)
  324. : RenderWindow(desc, renderWindowId), mShowOnSwap(false)
  325. , mCocoaWindowId(cocoaWindowId), mProperties(desc), mSyncedProperties(desc)
  326. {
  327. mContext = context;
  328. }
  329. void MacOSRenderWindow::initialize()
  330. {
  331. if(mDesc.vsync && mDesc.vsyncInterval > 0)
  332. setVSync(true, mDesc.vsyncInterval);
  333. }
  334. void MacOSRenderWindow::move(INT32 left, INT32 top)
  335. {
  336. // Do nothing
  337. }
  338. void MacOSRenderWindow::resize(UINT32 width, UINT32 height)
  339. {
  340. // Do nothing
  341. }
  342. void MacOSRenderWindow::setVSync(bool enabled, UINT32 interval)
  343. {
  344. THROW_IF_NOT_CORE_THREAD;
  345. if(!enabled)
  346. interval = 0;
  347. mContext->setVSync(interval);
  348. mProperties.vsync = enabled;
  349. mProperties.vsyncInterval = interval;
  350. {
  351. ScopedSpinLock lock(mLock);
  352. mSyncedProperties.vsync = enabled;
  353. mSyncedProperties.vsyncInterval = interval;
  354. }
  355. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  356. }
  357. void MacOSRenderWindow::swapBuffers(UINT32 syncMask)
  358. {
  359. THROW_IF_NOT_CORE_THREAD;
  360. if (mShowOnSwap)
  361. setHidden(false);
  362. mContext->swapBuffers();
  363. }
  364. void MacOSRenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  365. {
  366. THROW_IF_NOT_CORE_THREAD;
  367. if ((dst.getRight() > getProperties().width) ||
  368. (dst.getBottom() > getProperties().height) ||
  369. (dst.getFront() != 0) || (dst.getBack() != 1))
  370. {
  371. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  372. }
  373. if (buffer == FB_AUTO)
  374. {
  375. buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
  376. }
  377. GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
  378. GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
  379. if ((format == GL_NONE) || (type == 0))
  380. {
  381. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  382. }
  383. // Must change the packing to ensure no overruns!
  384. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  385. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  386. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  387. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  388. format, type, dst.getData());
  389. // restore default alignment
  390. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  391. //vertical flip
  392. {
  393. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  394. size_t height = dst.getHeight();
  395. UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
  396. UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  397. while (tmpRow >= tmpData)
  398. {
  399. memcpy(tmpRow, srcRow, rowSpan);
  400. srcRow += rowSpan;
  401. tmpRow -= rowSpan;
  402. }
  403. memcpy(dst.getData(), tmpData, rowSpan * height);
  404. bs_free(tmpData);
  405. }
  406. }
  407. void MacOSRenderWindow::getCustomAttribute(const String& name, void* data) const
  408. {
  409. if(name == "GLCONTEXT")
  410. {
  411. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(data);
  412. *contextPtr = mContext;
  413. return;
  414. }
  415. else if(name == "WINDOW_ID")
  416. {
  417. UINT32* windowId = (UINT32*)data;
  418. *windowId = mCocoaWindowId;
  419. }
  420. }
  421. void MacOSRenderWindow::syncProperties()
  422. {
  423. ScopedSpinLock lock(mLock);
  424. mProperties = mSyncedProperties;
  425. }
  426. }
  427. }