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