Skip to content

Commit

Permalink
[dxgi] Leave fullscreen mode when window looses focus
Browse files Browse the repository at this point in the history
  • Loading branch information
gofman committed Mar 24, 2023
1 parent 1a5afc7 commit a454ac2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dxgi/dxgi_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace dxvk {
return hr;
}

frontendSwapChain = new DxgiSwapChain(this, presenter.ptr(), hWnd, &desc, &fsDesc);
frontendSwapChain = new DxgiSwapChain(this, presenter.ptr(), hWnd, &desc, &fsDesc, pDevice);
} else {
Logger::err("DXGI: CreateSwapChainForHwnd: Unsupported device type");
return DXGI_ERROR_UNSUPPORTED;
Expand Down
23 changes: 20 additions & 3 deletions src/dxgi/dxgi_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

#include "../util/util_misc.h"

#include <d3d12.h>

namespace dxvk {

DxgiSwapChain::DxgiSwapChain(
DxgiFactory* pFactory,
IDXGIVkSwapChain* pPresenter,
HWND hWnd,
const DXGI_SWAP_CHAIN_DESC1* pDesc,
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC* pFullscreenDesc)
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC* pFullscreenDesc,
IUnknown* pDevice)
: m_factory (pFactory),
m_window (hWnd),
m_desc (*pDesc),
m_descFs (*pFullscreenDesc),
m_presentCount(0u),
m_presenter (pPresenter),
m_monitor (wsi::getWindowMonitor(m_window)) {
m_monitor (wsi::getWindowMonitor(m_window)),
m_is_d3d12(SUCCEEDED(pDevice->QueryInterface(__uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&Com<ID3D12CommandQueue>())))) {

if (FAILED(m_presenter->GetAdapter(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&m_adapter))))
throw DxvkError("DXGI: Failed to get adapter for present device");

Expand Down Expand Up @@ -214,7 +219,9 @@ namespace dxvk {
BOOL* pFullscreen,
IDXGIOutput** ppTarget) {
HRESULT hr = S_OK;


if (!m_is_d3d12 && !m_descFs.Windowed && wsi::isOccluded(m_window))
SetFullscreenState(FALSE, nullptr);
if (pFullscreen != nullptr)
*pFullscreen = !m_descFs.Windowed;

Expand Down Expand Up @@ -287,6 +294,16 @@ namespace dxvk {
if (SyncInterval > 4)
return DXGI_ERROR_INVALID_CALL;

if (!m_is_d3d12 && wsi::isMinimized(m_window))
return DXGI_STATUS_OCCLUDED;

if (!m_descFs.Windowed && wsi::isOccluded(m_window))
{
if (!(PresentFlags & DXGI_PRESENT_TEST))
SetFullscreenState(FALSE, nullptr);
return DXGI_STATUS_OCCLUDED;
}

std::lock_guard<dxvk::recursive_mutex> lockWin(m_lockWindow);
std::lock_guard<dxvk::mutex> lockBuf(m_lockBuffer);

Expand Down
7 changes: 5 additions & 2 deletions src/dxgi/dxgi_swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace dxvk {
IDXGIVkSwapChain* pPresenter,
HWND hWnd,
const DXGI_SWAP_CHAIN_DESC1* pDesc,
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC* pFullscreenDesc);
const DXGI_SWAP_CHAIN_FULLSCREEN_DESC* pFullscreenDesc,
IUnknown* pDevice);

~DxgiSwapChain();

Expand Down Expand Up @@ -189,7 +190,9 @@ namespace dxvk {

HMONITOR m_monitor;
wsi::DxvkWindowState m_windowState;


bool m_is_d3d12;

HRESULT EnterFullscreenMode(
IDXGIOutput1 *pTarget);

Expand Down
14 changes: 13 additions & 1 deletion src/wsi/glfw/wsi_window_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ namespace dxvk::wsi {
return window != nullptr;
}


bool isMinimized(HWND hWindow) {
GLFWwindow* window = fromHwnd(hWindow);
return glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0;
}


bool isOccluded(HWND hWindow) {
return false;
}


void updateFullscreenWindow(
HMONITOR hMonitor,
HWND hWindow,
Expand All @@ -141,4 +153,4 @@ namespace dxvk::wsi {
return glfwCreateWindowSurface(instance, window, nullptr, pSurface);
}

}
}
11 changes: 11 additions & 0 deletions src/wsi/sdl2/wsi_window_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ namespace dxvk::wsi {
}


bool isMinimized(HWND hWindow) {
SDL_Window* window = fromHwnd(hWindow);
return (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
}


bool isOccluded(HWND hWindow) {
return false;
}


void updateFullscreenWindow(
HMONITOR hMonitor,
HWND hWindow,
Expand Down
10 changes: 10 additions & 0 deletions src/wsi/win32/wsi_window_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ namespace dxvk::wsi {
}


bool isMinimized(HWND hWindow) {
return (::GetWindowLongW(hWindow, GWL_STYLE) & WS_MINIMIZE) != 0;
}


bool isOccluded(HWND hWindow) {
return ::GetForegroundWindow() != hWindow;
}


void updateFullscreenWindow(
HMONITOR hMonitor,
HWND hWindow,
Expand Down
16 changes: 16 additions & 0 deletions src/wsi/wsi_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ namespace dxvk::wsi {
*/
bool isWindow(HWND hWindow);

/**
* \brief Is window minimized?
*
* \param [in] hWindow The window
* \returns Is window minimized?
*/
bool isMinimized(HWND hWindow);

/**
* \brief Is window occluded?
*
* \param [in] hWindow The window
* \returns Is window occluded?
*/
bool isOccluded(HWND hWindow);

/**
* \brief Update a fullscreen window's position/size
*
Expand Down

0 comments on commit a454ac2

Please sign in to comment.