BsWin32RenderWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #ifndef _WIN32_WINNT
  4. #define _WIN32_WINNT 0x0500
  5. #endif
  6. #include "Win32/BsWin32RenderWindow.h"
  7. #include "Input/BsInput.h"
  8. #include "Renderapi/BsRenderAPI.h"
  9. #include "Corethread/BsCoreThread.h"
  10. #include "Error/BsException.h"
  11. #include "Win32/BsWin32GLSupport.h"
  12. #include "Win32/BsWin32Context.h"
  13. #include "Win32/BsWin32Platform.h"
  14. #include "Win32/BsWin32VideoModeInfo.h"
  15. #include "BsGLPixelFormat.h"
  16. #include "Managers/BsRenderWindowManager.h"
  17. #include "Win32/BsWin32Window.h"
  18. #include "Math/BsMath.h"
  19. GLenum GLEWAPIENTRY wglewContextInit(bs::ct::GLSupport* glSupport);
  20. namespace bs
  21. {
  22. #define _MAX_CLASS_NAME_ 128
  23. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, ct::Win32GLSupport &glsupport)
  24. :RenderWindow(desc, windowId), mGLSupport(glsupport), mProperties(desc)
  25. {
  26. }
  27. void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
  28. {
  29. if (name == "WINDOW")
  30. {
  31. UINT64 *pHwnd = (UINT64*)pData;
  32. *pHwnd = (UINT64)getHWnd();
  33. return;
  34. }
  35. }
  36. Vector2I Win32RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  37. {
  38. POINT pos;
  39. pos.x = screenPos.x;
  40. pos.y = screenPos.y;
  41. ScreenToClient(getHWnd(), &pos);
  42. return Vector2I(pos.x, pos.y);
  43. }
  44. Vector2I Win32RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  45. {
  46. POINT pos;
  47. pos.x = windowPos.x;
  48. pos.y = windowPos.y;
  49. ClientToScreen(getHWnd(), &pos);
  50. return Vector2I(pos.x, pos.y);
  51. }
  52. SPtr<ct::Win32RenderWindow> Win32RenderWindow::getCore() const
  53. {
  54. return std::static_pointer_cast<ct::Win32RenderWindow>(mCoreSpecific);
  55. }
  56. void Win32RenderWindow::syncProperties()
  57. {
  58. ScopedSpinLock lock(getCore()->mLock);
  59. mProperties = getCore()->mSyncedProperties;
  60. }
  61. HWND Win32RenderWindow::getHWnd() const
  62. {
  63. blockUntilCoreInitialized();
  64. return getCore()->_getHWnd();
  65. }
  66. namespace ct
  67. {
  68. Win32RenderWindow::Win32RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, Win32GLSupport& glsupport)
  69. : RenderWindow(desc, windowId), mWindow(nullptr), mGLSupport(glsupport), mHDC(nullptr), mIsChild(false)
  70. , mDeviceName(nullptr), mDisplayFrequency(0), mShowOnSwap(false), mContext(nullptr), mProperties(desc)
  71. , mSyncedProperties(desc)
  72. { }
  73. Win32RenderWindow::~Win32RenderWindow()
  74. {
  75. RenderWindowProperties& props = mProperties;
  76. if (mWindow != nullptr)
  77. {
  78. ReleaseDC(mWindow->getHWnd(), mHDC);
  79. bs_delete(mWindow);
  80. mWindow = nullptr;
  81. }
  82. mHDC = nullptr;
  83. if (mDeviceName != nullptr)
  84. {
  85. bs_free(mDeviceName);
  86. mDeviceName = nullptr;
  87. }
  88. }
  89. void Win32RenderWindow::initialize()
  90. {
  91. RenderWindowProperties& props = mProperties;
  92. props.isFullScreen = mDesc.fullscreen;
  93. mIsChild = false;
  94. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  95. WINDOW_DESC windowDesc;
  96. windowDesc.showTitleBar = mDesc.showTitleBar;
  97. windowDesc.showBorder = mDesc.showBorder;
  98. windowDesc.allowResize = mDesc.allowResize;
  99. windowDesc.enableDoubleClick = true;
  100. windowDesc.fullscreen = mDesc.fullscreen;
  101. windowDesc.width = mDesc.videoMode.getWidth();
  102. windowDesc.height = mDesc.videoMode.getHeight();
  103. windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
  104. windowDesc.left = mDesc.left;
  105. windowDesc.top = mDesc.top;
  106. windowDesc.outerDimensions = false;
  107. windowDesc.title = mDesc.title;
  108. windowDesc.toolWindow = mDesc.toolWindow;
  109. windowDesc.creationParams = this;
  110. windowDesc.modal = mDesc.modal;
  111. windowDesc.wndProc = &Win32Platform::_win32WndProc;
  112. #ifdef BS_STATIC_LIB
  113. windowDesc.module = GetModuleHandle(NULL);
  114. #else
  115. windowDesc.module = GetModuleHandle(MODULE_NAME);
  116. #endif
  117. NameValuePairList::const_iterator opt;
  118. opt = mDesc.platformSpecific.find("parentWindowHandle");
  119. if (opt != mDesc.platformSpecific.end())
  120. windowDesc.parent = (HWND)parseUINT64(opt->second);
  121. opt = mDesc.platformSpecific.find("externalWindowHandle");
  122. if (opt != mDesc.platformSpecific.end())
  123. windowDesc.external = (HWND)parseUINT64(opt->second);
  124. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  125. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  126. if (numOutputs > 0)
  127. {
  128. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  129. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  130. windowDesc.monitor = outputInfo.getMonitorHandle();
  131. }
  132. mIsChild = windowDesc.parent != nullptr;
  133. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  134. if (!windowDesc.external)
  135. {
  136. mShowOnSwap = mDesc.hideUntilSwap;
  137. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  138. }
  139. mWindow = bs_new<Win32Window>(windowDesc);
  140. props.width = mWindow->getWidth();
  141. props.height = mWindow->getHeight();
  142. props.top = mWindow->getTop();
  143. props.left = mWindow->getLeft();
  144. if (!windowDesc.external)
  145. {
  146. if (props.isFullScreen)
  147. {
  148. DEVMODE displayDeviceMode;
  149. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  150. displayDeviceMode.dmSize = sizeof(DEVMODE);
  151. displayDeviceMode.dmBitsPerPel = 32;
  152. displayDeviceMode.dmPelsWidth = props.width;
  153. displayDeviceMode.dmPelsHeight = props.height;
  154. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  155. if (mDisplayFrequency)
  156. {
  157. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  158. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  159. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  160. {
  161. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
  162. }
  163. }
  164. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  165. {
  166. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
  167. }
  168. }
  169. }
  170. mHDC = GetDC(mWindow->getHWnd());
  171. int testMultisample = props.multisampleCount;
  172. bool testHwGamma = mDesc.gamma;
  173. bool formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  174. if (!formatOk)
  175. {
  176. if (props.multisampleCount > 0)
  177. {
  178. // Try without multisampling
  179. testMultisample = 0;
  180. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  181. }
  182. if (!formatOk && mDesc.gamma)
  183. {
  184. // Try without sRGB
  185. testHwGamma = false;
  186. testMultisample = props.multisampleCount;
  187. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  188. }
  189. if (!formatOk && mDesc.gamma && (props.multisampleCount > 0))
  190. {
  191. // Try without both
  192. testHwGamma = false;
  193. testMultisample = 0;
  194. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  195. }
  196. if (!formatOk)
  197. BS_EXCEPT(RenderingAPIException, "Failed selecting pixel format.");
  198. }
  199. // Record what gamma option we used in the end
  200. // this will control enabling of sRGB state flags when used
  201. props.hwGamma = testHwGamma;
  202. props.multisampleCount = testMultisample;
  203. mContext = mGLSupport.createContext(mHDC, nullptr);
  204. {
  205. ScopedSpinLock lock(mLock);
  206. mSyncedProperties = props;
  207. }
  208. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  209. RenderWindow::initialize();
  210. }
  211. void Win32RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  212. {
  213. THROW_IF_NOT_CORE_THREAD;
  214. if (mIsChild)
  215. return;
  216. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  217. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  218. if (numOutputs == 0)
  219. return;
  220. RenderWindowProperties& props = mProperties;
  221. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  222. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  223. mDisplayFrequency = Math::roundToInt(refreshRate);
  224. props.isFullScreen = true;
  225. DEVMODE displayDeviceMode;
  226. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  227. displayDeviceMode.dmSize = sizeof(DEVMODE);
  228. displayDeviceMode.dmBitsPerPel = 32;
  229. displayDeviceMode.dmPelsWidth = width;
  230. displayDeviceMode.dmPelsHeight = height;
  231. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  232. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  233. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  234. MONITORINFOEX monitorInfo;
  235. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  236. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  237. GetMonitorInfo(hMonitor, &monitorInfo);
  238. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  239. {
  240. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  241. }
  242. props.top = monitorInfo.rcMonitor.top;
  243. props.left = monitorInfo.rcMonitor.left;
  244. props.width = width;
  245. props.height = height;
  246. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  247. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, 0);
  248. SetWindowPos(mWindow->getHWnd(), HWND_TOP, props.left, props.top, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  249. _windowMovedOrResized();
  250. }
  251. void Win32RenderWindow::setFullscreen(const VideoMode& mode)
  252. {
  253. THROW_IF_NOT_CORE_THREAD;
  254. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  255. }
  256. void Win32RenderWindow::setWindowed(UINT32 width, UINT32 height)
  257. {
  258. THROW_IF_NOT_CORE_THREAD;
  259. RenderWindowProperties& props = mProperties;
  260. if (!props.isFullScreen)
  261. return;
  262. props.isFullScreen = false;
  263. props.width = width;
  264. props.height = height;
  265. // Drop out of fullscreen
  266. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  267. UINT32 winWidth = width;
  268. UINT32 winHeight = height;
  269. RECT rect;
  270. SetRect(&rect, 0, 0, winWidth, winHeight);
  271. AdjustWindowRect(&rect, mWindow->getStyle(), false);
  272. winWidth = rect.right - rect.left;
  273. winHeight = rect.bottom - rect.top;
  274. // Deal with centering when switching down to smaller resolution
  275. HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
  276. MONITORINFO monitorInfo;
  277. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  278. monitorInfo.cbSize = sizeof(MONITORINFO);
  279. GetMonitorInfo(hMonitor, &monitorInfo);
  280. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  281. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  282. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  283. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  284. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle() | WS_VISIBLE);
  285. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
  286. SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
  287. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  288. {
  289. ScopedSpinLock lock(mLock);
  290. mSyncedProperties.width = props.width;
  291. mSyncedProperties.height = props.height;
  292. }
  293. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  294. _windowMovedOrResized();
  295. }
  296. void Win32RenderWindow::move(INT32 left, INT32 top)
  297. {
  298. THROW_IF_NOT_CORE_THREAD;
  299. RenderWindowProperties& props = mProperties;
  300. if (!props.isFullScreen)
  301. {
  302. mWindow->move(left, top);
  303. props.top = mWindow->getTop();
  304. props.left = mWindow->getLeft();
  305. {
  306. ScopedSpinLock lock(mLock);
  307. mSyncedProperties.top = props.top;
  308. mSyncedProperties.left = props.left;
  309. }
  310. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  311. }
  312. }
  313. void Win32RenderWindow::resize(UINT32 width, UINT32 height)
  314. {
  315. THROW_IF_NOT_CORE_THREAD;
  316. RenderWindowProperties& props = mProperties;
  317. if (!props.isFullScreen)
  318. {
  319. mWindow->resize(width, height);
  320. props.width = mWindow->getWidth();
  321. props.height = mWindow->getHeight();
  322. {
  323. ScopedSpinLock lock(mLock);
  324. mSyncedProperties.width = props.width;
  325. mSyncedProperties.height = props.height;
  326. }
  327. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  328. }
  329. }
  330. void Win32RenderWindow::minimize()
  331. {
  332. THROW_IF_NOT_CORE_THREAD;
  333. mWindow->minimize();
  334. }
  335. void Win32RenderWindow::maximize()
  336. {
  337. THROW_IF_NOT_CORE_THREAD;
  338. mWindow->maximize();
  339. }
  340. void Win32RenderWindow::restore()
  341. {
  342. THROW_IF_NOT_CORE_THREAD;
  343. mWindow->restore();
  344. }
  345. void Win32RenderWindow::swapBuffers(UINT32 syncMask)
  346. {
  347. THROW_IF_NOT_CORE_THREAD;
  348. if (mShowOnSwap)
  349. setHidden(false);
  350. SwapBuffers(mHDC);
  351. }
  352. void Win32RenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  353. {
  354. THROW_IF_NOT_CORE_THREAD;
  355. if ((dst.getRight() > getProperties().width) ||
  356. (dst.getBottom() > getProperties().height) ||
  357. (dst.getFront() != 0) || (dst.getBack() != 1))
  358. {
  359. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  360. }
  361. if (buffer == FB_AUTO)
  362. {
  363. buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
  364. }
  365. GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
  366. GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
  367. if ((format == GL_NONE) || (type == 0))
  368. {
  369. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  370. }
  371. // Must change the packing to ensure no overruns!
  372. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  373. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  374. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  375. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  376. format, type, dst.getData());
  377. // restore default alignment
  378. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  379. //vertical flip
  380. {
  381. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  382. size_t height = dst.getHeight();
  383. UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
  384. UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  385. while (tmpRow >= tmpData)
  386. {
  387. memcpy(tmpRow, srcRow, rowSpan);
  388. srcRow += rowSpan;
  389. tmpRow -= rowSpan;
  390. }
  391. memcpy(dst.getData(), tmpData, rowSpan * height);
  392. bs_free(tmpData);
  393. }
  394. }
  395. void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
  396. {
  397. if(name == "GLCONTEXT")
  398. {
  399. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
  400. *contextPtr = mContext;
  401. return;
  402. }
  403. else if(name == "WINDOW")
  404. {
  405. UINT64 *pHwnd = (UINT64*)pData;
  406. *pHwnd = (UINT64)_getHWnd();
  407. return;
  408. }
  409. }
  410. void Win32RenderWindow::setActive(bool state)
  411. {
  412. THROW_IF_NOT_CORE_THREAD;
  413. mWindow->setActive(state);
  414. RenderWindow::setActive(state);
  415. }
  416. void Win32RenderWindow::setHidden(bool hidden)
  417. {
  418. THROW_IF_NOT_CORE_THREAD;
  419. mShowOnSwap = false;
  420. mWindow->setHidden(hidden);
  421. RenderWindow::setHidden(hidden);
  422. }
  423. void Win32RenderWindow::_windowMovedOrResized()
  424. {
  425. if (!mWindow)
  426. return;
  427. mWindow->_windowMovedOrResized();
  428. RenderWindowProperties& props = mProperties;
  429. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  430. {
  431. props.top = mWindow->getTop();
  432. props.left = mWindow->getLeft();
  433. props.width = mWindow->getWidth();
  434. props.height = mWindow->getHeight();
  435. }
  436. RenderWindow::_windowMovedOrResized();
  437. }
  438. HWND Win32RenderWindow::_getHWnd() const
  439. {
  440. return mWindow->getHWnd();
  441. }
  442. void Win32RenderWindow::syncProperties()
  443. {
  444. ScopedSpinLock lock(mLock);
  445. mProperties = mSyncedProperties;
  446. }
  447. }
  448. }