BsMacOSRenderWindow.mm 14 KB

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