BsWin32RenderWindow.cpp 16 KB

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