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