2
0

BsWin32RenderWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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.border = mDesc.border;
  97. windowDesc.enableDoubleClick = mDesc.enableDoubleClick;
  98. windowDesc.fullscreen = mDesc.fullscreen;
  99. windowDesc.width = mDesc.videoMode.getWidth();
  100. windowDesc.height = mDesc.videoMode.getHeight();
  101. windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
  102. windowDesc.left = mDesc.left;
  103. windowDesc.top = mDesc.top;
  104. windowDesc.outerDimensions = mDesc.outerDimensions;
  105. windowDesc.title = mDesc.title;
  106. windowDesc.toolWindow = mDesc.toolWindow;
  107. windowDesc.creationParams = this;
  108. windowDesc.modal = mDesc.modal;
  109. windowDesc.wndProc = &Win32Platform::_win32WndProc;
  110. #ifdef BS_STATIC_LIB
  111. windowDesc.module = GetModuleHandle(NULL);
  112. #else
  113. windowDesc.module = GetModuleHandle(MODULE_NAME);
  114. #endif
  115. NameValuePairList::const_iterator opt;
  116. opt = mDesc.platformSpecific.find("parentWindowHandle");
  117. if (opt != mDesc.platformSpecific.end())
  118. windowDesc.parent = (HWND)parseUINT64(opt->second);
  119. opt = mDesc.platformSpecific.find("externalWindowHandle");
  120. if (opt != mDesc.platformSpecific.end())
  121. windowDesc.external = (HWND)parseUINT64(opt->second);
  122. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  123. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  124. if (numOutputs > 0)
  125. {
  126. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  127. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  128. windowDesc.monitor = outputInfo.getMonitorHandle();
  129. }
  130. mIsChild = windowDesc.parent != nullptr;
  131. props.isFullScreen = mDesc.fullscreen && !mIsChild;
  132. if (!windowDesc.external)
  133. {
  134. mShowOnSwap = mDesc.hideUntilSwap;
  135. props.isHidden = mDesc.hideUntilSwap || mDesc.hidden;
  136. }
  137. mWindow = bs_new<Win32Window>(windowDesc);
  138. props.width = mWindow->getWidth();
  139. props.height = mWindow->getHeight();
  140. props.top = mWindow->getTop();
  141. props.left = mWindow->getLeft();
  142. if (!windowDesc.external)
  143. {
  144. if (props.isFullScreen)
  145. {
  146. DEVMODE displayDeviceMode;
  147. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  148. displayDeviceMode.dmSize = sizeof(DEVMODE);
  149. displayDeviceMode.dmBitsPerPel = 32;
  150. displayDeviceMode.dmPelsWidth = props.width;
  151. displayDeviceMode.dmPelsHeight = props.height;
  152. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  153. if (mDisplayFrequency)
  154. {
  155. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  156. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  157. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  158. {
  159. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings with user display frequency failed.");
  160. }
  161. }
  162. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  163. {
  164. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed.");
  165. }
  166. }
  167. }
  168. mHDC = GetDC(mWindow->getHWnd());
  169. int testMultisample = props.multisampleCount;
  170. bool testHwGamma = mDesc.gamma;
  171. bool formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  172. if (!formatOk)
  173. {
  174. if (props.multisampleCount > 0)
  175. {
  176. // Try without multisampling
  177. testMultisample = 0;
  178. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  179. }
  180. if (!formatOk && mDesc.gamma)
  181. {
  182. // Try without sRGB
  183. testHwGamma = false;
  184. testMultisample = props.multisampleCount;
  185. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  186. }
  187. if (!formatOk && mDesc.gamma && (props.multisampleCount > 0))
  188. {
  189. // Try without both
  190. testHwGamma = false;
  191. testMultisample = 0;
  192. formatOk = mGLSupport.selectPixelFormat(mHDC, 32, testMultisample, testHwGamma, mDesc.depthBuffer);
  193. }
  194. if (!formatOk)
  195. BS_EXCEPT(RenderingAPIException, "Failed selecting pixel format.");
  196. }
  197. // Record what gamma option we used in the end
  198. // this will control enabling of sRGB state flags when used
  199. props.hwGamma = testHwGamma;
  200. props.multisampleCount = testMultisample;
  201. mContext = mGLSupport.createContext(mHDC, nullptr);
  202. {
  203. ScopedSpinLock lock(mLock);
  204. mSyncedProperties = props;
  205. }
  206. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  207. RenderWindow::initialize();
  208. }
  209. void Win32RenderWindow::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  210. {
  211. THROW_IF_NOT_CORE_THREAD;
  212. if (mIsChild)
  213. return;
  214. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderAPI::instance().getVideoModeInfo());
  215. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  216. if (numOutputs == 0)
  217. return;
  218. RenderWindowProperties& props = mProperties;
  219. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  220. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  221. mDisplayFrequency = Math::roundToInt(refreshRate);
  222. props.isFullScreen = true;
  223. DEVMODE displayDeviceMode;
  224. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  225. displayDeviceMode.dmSize = sizeof(DEVMODE);
  226. displayDeviceMode.dmBitsPerPel = 32;
  227. displayDeviceMode.dmPelsWidth = width;
  228. displayDeviceMode.dmPelsHeight = height;
  229. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  230. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  231. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  232. MONITORINFOEX monitorInfo;
  233. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  234. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  235. GetMonitorInfo(hMonitor, &monitorInfo);
  236. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  237. {
  238. BS_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  239. }
  240. props.top = monitorInfo.rcMonitor.top;
  241. props.left = monitorInfo.rcMonitor.left;
  242. props.width = width;
  243. props.height = height;
  244. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  245. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, 0);
  246. SetWindowPos(mWindow->getHWnd(), HWND_TOP, props.left, props.top, width, height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  247. _windowMovedOrResized();
  248. }
  249. void Win32RenderWindow::setFullscreen(const VideoMode& mode)
  250. {
  251. THROW_IF_NOT_CORE_THREAD;
  252. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  253. }
  254. void Win32RenderWindow::setWindowed(UINT32 width, UINT32 height)
  255. {
  256. THROW_IF_NOT_CORE_THREAD;
  257. RenderWindowProperties& props = mProperties;
  258. if (!props.isFullScreen)
  259. return;
  260. props.isFullScreen = false;
  261. props.width = width;
  262. props.height = height;
  263. // Drop out of fullscreen
  264. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  265. UINT32 winWidth = width;
  266. UINT32 winHeight = height;
  267. RECT rect;
  268. SetRect(&rect, 0, 0, winWidth, winHeight);
  269. AdjustWindowRect(&rect, mWindow->getStyle(), false);
  270. winWidth = rect.right - rect.left;
  271. winHeight = rect.bottom - rect.top;
  272. // Deal with centering when switching down to smaller resolution
  273. HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
  274. MONITORINFO monitorInfo;
  275. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  276. monitorInfo.cbSize = sizeof(MONITORINFO);
  277. GetMonitorInfo(hMonitor, &monitorInfo);
  278. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  279. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  280. INT32 left = screenw > INT32(winWidth) ? ((screenw - INT32(winWidth)) / 2) : 0;
  281. INT32 top = screenh > INT32(winHeight) ? ((screenh - INT32(winHeight)) / 2) : 0;
  282. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle() | WS_VISIBLE);
  283. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
  284. SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
  285. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  286. {
  287. ScopedSpinLock lock(mLock);
  288. mSyncedProperties.width = props.width;
  289. mSyncedProperties.height = props.height;
  290. }
  291. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  292. _windowMovedOrResized();
  293. }
  294. void Win32RenderWindow::move(INT32 left, INT32 top)
  295. {
  296. THROW_IF_NOT_CORE_THREAD;
  297. RenderWindowProperties& props = mProperties;
  298. if (!props.isFullScreen)
  299. {
  300. mWindow->move(left, top);
  301. props.top = mWindow->getTop();
  302. props.left = mWindow->getLeft();
  303. {
  304. ScopedSpinLock lock(mLock);
  305. mSyncedProperties.top = props.top;
  306. mSyncedProperties.left = props.left;
  307. }
  308. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  309. }
  310. }
  311. void Win32RenderWindow::resize(UINT32 width, UINT32 height)
  312. {
  313. THROW_IF_NOT_CORE_THREAD;
  314. RenderWindowProperties& props = mProperties;
  315. if (!props.isFullScreen)
  316. {
  317. mWindow->resize(width, height);
  318. props.width = mWindow->getWidth();
  319. props.height = mWindow->getHeight();
  320. {
  321. ScopedSpinLock lock(mLock);
  322. mSyncedProperties.width = props.width;
  323. mSyncedProperties.height = props.height;
  324. }
  325. bs::RenderWindowManager::instance().notifySyncDataDirty(this);
  326. }
  327. }
  328. void Win32RenderWindow::minimize()
  329. {
  330. THROW_IF_NOT_CORE_THREAD;
  331. mWindow->minimize();
  332. }
  333. void Win32RenderWindow::maximize()
  334. {
  335. THROW_IF_NOT_CORE_THREAD;
  336. mWindow->maximize();
  337. }
  338. void Win32RenderWindow::restore()
  339. {
  340. THROW_IF_NOT_CORE_THREAD;
  341. mWindow->restore();
  342. }
  343. void Win32RenderWindow::swapBuffers(UINT32 syncMask)
  344. {
  345. THROW_IF_NOT_CORE_THREAD;
  346. if (mShowOnSwap)
  347. setHidden(false);
  348. SwapBuffers(mHDC);
  349. }
  350. void Win32RenderWindow::copyToMemory(PixelData &dst, FrameBuffer buffer)
  351. {
  352. THROW_IF_NOT_CORE_THREAD;
  353. if ((dst.getRight() > getProperties().width) ||
  354. (dst.getBottom() > getProperties().height) ||
  355. (dst.getFront() != 0) || (dst.getBack() != 1))
  356. {
  357. BS_EXCEPT(InvalidParametersException, "Invalid box.");
  358. }
  359. if (buffer == FB_AUTO)
  360. {
  361. buffer = mProperties.isFullScreen ? FB_FRONT : FB_BACK;
  362. }
  363. GLenum format = GLPixelUtil::getGLOriginFormat(dst.getFormat());
  364. GLenum type = GLPixelUtil::getGLOriginDataType(dst.getFormat());
  365. if ((format == GL_NONE) || (type == 0))
  366. {
  367. BS_EXCEPT(InvalidParametersException, "Unsupported format.");
  368. }
  369. // Must change the packing to ensure no overruns!
  370. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  371. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  372. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  373. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  374. format, type, dst.getData());
  375. // restore default alignment
  376. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  377. //vertical flip
  378. {
  379. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  380. size_t height = dst.getHeight();
  381. UINT8* tmpData = (UINT8*)bs_alloc((UINT32)(rowSpan * height));
  382. UINT8* srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  383. while (tmpRow >= tmpData)
  384. {
  385. memcpy(tmpRow, srcRow, rowSpan);
  386. srcRow += rowSpan;
  387. tmpRow -= rowSpan;
  388. }
  389. memcpy(dst.getData(), tmpData, rowSpan * height);
  390. bs_free(tmpData);
  391. }
  392. }
  393. void Win32RenderWindow::getCustomAttribute(const String& name, void* pData) const
  394. {
  395. if(name == "GLCONTEXT")
  396. {
  397. SPtr<GLContext>* contextPtr = static_cast<SPtr<GLContext>*>(pData);
  398. *contextPtr = mContext;
  399. return;
  400. }
  401. else if(name == "WINDOW")
  402. {
  403. UINT64 *pHwnd = (UINT64*)pData;
  404. *pHwnd = (UINT64)_getHWnd();
  405. return;
  406. }
  407. }
  408. void Win32RenderWindow::setActive(bool state)
  409. {
  410. THROW_IF_NOT_CORE_THREAD;
  411. mWindow->setActive(state);
  412. RenderWindow::setActive(state);
  413. }
  414. void Win32RenderWindow::setHidden(bool hidden)
  415. {
  416. THROW_IF_NOT_CORE_THREAD;
  417. mShowOnSwap = false;
  418. mWindow->setHidden(hidden);
  419. RenderWindow::setHidden(hidden);
  420. }
  421. void Win32RenderWindow::_windowMovedOrResized()
  422. {
  423. if (!mWindow)
  424. return;
  425. mWindow->_windowMovedOrResized();
  426. RenderWindowProperties& props = mProperties;
  427. if (!props.isFullScreen) // Fullscreen is handled directly by this object
  428. {
  429. props.top = mWindow->getTop();
  430. props.left = mWindow->getLeft();
  431. props.width = mWindow->getWidth();
  432. props.height = mWindow->getHeight();
  433. }
  434. RenderWindow::_windowMovedOrResized();
  435. }
  436. HWND Win32RenderWindow::_getHWnd() const
  437. {
  438. return mWindow->getHWnd();
  439. }
  440. void Win32RenderWindow::syncProperties()
  441. {
  442. ScopedSpinLock lock(mLock);
  443. mProperties = mSyncedProperties;
  444. }
  445. }
  446. }