BsWin32RenderWindow.cpp 15 KB

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