mirror of
https://github.com/Blinue/Magpie.git
synced 2026-06-24 02:04:10 +00:00
chore: 修正部分格式
This commit is contained in:
parent
2de06c5246
commit
89ae4695e5
4 changed files with 96 additions and 96 deletions
|
|
@ -26,71 +26,71 @@ bool GraphicsCaptureFrameSource::Initialize() {
|
|||
}
|
||||
|
||||
_d3dDC = App::GetInstance().GetRenderer().GetD3DDC();
|
||||
HWND hwndSrc = App::GetInstance().GetHwndSrc();
|
||||
HWND hwndSrc = App::GetInstance().GetHwndSrc();
|
||||
|
||||
// 包含边框的窗口尺寸
|
||||
RECT srcRect{};
|
||||
HRESULT hr = DwmGetWindowAttribute(hwndSrc, DWMWA_EXTENDED_FRAME_BOUNDS, &srcRect, sizeof(srcRect));
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("DwmGetWindowAttribute 失败", hr));
|
||||
return false;
|
||||
}
|
||||
// 包含边框的窗口尺寸
|
||||
RECT srcRect{};
|
||||
HRESULT hr = DwmGetWindowAttribute(hwndSrc, DWMWA_EXTENDED_FRAME_BOUNDS, &srcRect, sizeof(srcRect));
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("DwmGetWindowAttribute 失败", hr));
|
||||
return false;
|
||||
}
|
||||
|
||||
const RECT& srcClient = App::GetInstance().GetSrcClientRect();
|
||||
const RECT& srcClient = App::GetInstance().GetSrcClientRect();
|
||||
|
||||
// 在源窗口存在 DPI 缩放时有时会有一像素的偏移(取决于窗口在屏幕上的位置)
|
||||
// 可能是 DwmGetWindowAttribute 的 bug
|
||||
_frameInWnd = {
|
||||
UINT(srcClient.left - srcRect.left),
|
||||
_frameInWnd = {
|
||||
UINT(srcClient.left - srcRect.left),
|
||||
UINT(srcClient.top - srcRect.top),
|
||||
0,
|
||||
UINT(srcClient.right - srcRect.left),
|
||||
UINT(srcClient.bottom - srcRect.top),
|
||||
1
|
||||
};
|
||||
};
|
||||
|
||||
SIZE frameSize = { LONG(_frameInWnd.right - _frameInWnd.left), LONG(_frameInWnd.bottom - _frameInWnd.top) };
|
||||
|
||||
try {
|
||||
// Windows.Graphics.Capture API 似乎只能运行于 MTA,造成诸多麻烦
|
||||
winrt::init_apartment(winrt::apartment_type::multi_threaded);
|
||||
try {
|
||||
// Windows.Graphics.Capture API 似乎只能运行于 MTA,造成诸多麻烦
|
||||
winrt::init_apartment(winrt::apartment_type::multi_threaded);
|
||||
|
||||
if (!winrt::ApiInformation::IsTypePresent(L"Windows.Graphics.Capture.GraphicsCaptureSession")) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "不存在 GraphicsCaptureSession API");
|
||||
return false;
|
||||
}
|
||||
if (!winrt::GraphicsCaptureSession::IsSupported()) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "当前不支持 WinRT 捕获");
|
||||
return false;
|
||||
}
|
||||
if (!winrt::ApiInformation::IsTypePresent(L"Windows.Graphics.Capture.GraphicsCaptureSession")) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "不存在 GraphicsCaptureSession API");
|
||||
return false;
|
||||
}
|
||||
if (!winrt::GraphicsCaptureSession::IsSupported()) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "当前不支持 WinRT 捕获");
|
||||
return false;
|
||||
}
|
||||
|
||||
hr = CreateDirect3D11DeviceFromDXGIDevice(
|
||||
App::GetInstance().GetRenderer().GetDXGIDevice().Get(),
|
||||
reinterpret_cast<::IInspectable**>(winrt::put_abi(_wrappedD3DDevice))
|
||||
);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 IDirect3DDevice 失败", hr));
|
||||
return false;
|
||||
}
|
||||
hr = CreateDirect3D11DeviceFromDXGIDevice(
|
||||
App::GetInstance().GetRenderer().GetDXGIDevice().Get(),
|
||||
reinterpret_cast<::IInspectable**>(winrt::put_abi(_wrappedD3DDevice))
|
||||
);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 IDirect3DDevice 失败", hr));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 从窗口句柄获取 GraphicsCaptureItem
|
||||
auto interop = winrt::get_activation_factory<winrt::GraphicsCaptureItem, IGraphicsCaptureItemInterop>();
|
||||
if (!interop) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "获取 IGraphicsCaptureItemInterop 失败");
|
||||
return false;
|
||||
}
|
||||
// 从窗口句柄获取 GraphicsCaptureItem
|
||||
auto interop = winrt::get_activation_factory<winrt::GraphicsCaptureItem, IGraphicsCaptureItemInterop>();
|
||||
if (!interop) {
|
||||
SPDLOG_LOGGER_ERROR(logger, "获取 IGraphicsCaptureItemInterop 失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
hr = interop->CreateForWindow(
|
||||
hwndSrc,
|
||||
winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
|
||||
winrt::put_abi(_captureItem)
|
||||
);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 GraphicsCaptureItem 失败", hr));
|
||||
return false;
|
||||
}
|
||||
hr = interop->CreateForWindow(
|
||||
hwndSrc,
|
||||
winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
|
||||
winrt::put_abi(_captureItem)
|
||||
);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 GraphicsCaptureItem 失败", hr));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 创建帧缓冲池
|
||||
// 创建帧缓冲池
|
||||
// 帧的尺寸为不含阴影的窗口尺寸,和 _captureItem.Size() 不同
|
||||
_captureFramePool = winrt::Direct3D11CaptureFramePool::CreateFreeThreaded(
|
||||
_wrappedD3DDevice,
|
||||
|
|
@ -99,44 +99,44 @@ bool GraphicsCaptureFrameSource::Initialize() {
|
|||
{ srcRect.right - srcRect.left, srcRect.bottom - srcRect.top } // 帧的尺寸
|
||||
);
|
||||
|
||||
// 开始捕获
|
||||
_captureSession = _captureFramePool.CreateCaptureSession(_captureItem);
|
||||
// 开始捕获
|
||||
_captureSession = _captureFramePool.CreateCaptureSession(_captureItem);
|
||||
|
||||
if (winrt::ApiInformation::IsPropertyPresent(
|
||||
L"Windows.Graphics.Capture.GraphicsCaptureSession",
|
||||
L"IsCursorCaptureEnabled"
|
||||
)) {
|
||||
// 从 v2004 开始提供
|
||||
_captureSession.IsCursorCaptureEnabled(false);
|
||||
if (winrt::ApiInformation::IsPropertyPresent(
|
||||
L"Windows.Graphics.Capture.GraphicsCaptureSession",
|
||||
L"IsCursorCaptureEnabled"
|
||||
)) {
|
||||
// 从 v2004 开始提供
|
||||
_captureSession.IsCursorCaptureEnabled(false);
|
||||
} else {
|
||||
SPDLOG_LOGGER_INFO(logger, "当前系统无 IsCursorCaptureEnabled API");
|
||||
}
|
||||
|
||||
_captureSession.StartCapture();
|
||||
} catch (const winrt::hresult_error& e) {
|
||||
SPDLOG_LOGGER_ERROR(logger, fmt::format("初始化 WinRT 失败:{}", StrUtils::UTF16ToUTF8(e.message())));
|
||||
return false;
|
||||
}
|
||||
_captureSession.StartCapture();
|
||||
} catch (const winrt::hresult_error& e) {
|
||||
SPDLOG_LOGGER_ERROR(logger, fmt::format("初始化 WinRT 失败:{}", StrUtils::UTF16ToUTF8(e.message())));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.Width = frameSize.cx;
|
||||
desc.Height = frameSize.cy;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
|
||||
hr = App::GetInstance().GetRenderer().GetD3DDevice()->CreateTexture2D(&desc, nullptr, &_output);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 Texture2D 失败", hr));
|
||||
return false;
|
||||
}
|
||||
desc.Height = frameSize.cy;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
|
||||
hr = App::GetInstance().GetRenderer().GetD3DDevice()->CreateTexture2D(&desc, nullptr, &_output);
|
||||
if (FAILED(hr)) {
|
||||
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("创建 Texture2D 失败", hr));
|
||||
return false;
|
||||
}
|
||||
|
||||
App::GetInstance().SetErrorMsg(ErrorMessages::GENERIC);
|
||||
SPDLOG_LOGGER_INFO(logger, "GraphicsCaptureFrameSource 初始化完成");
|
||||
return true;
|
||||
SPDLOG_LOGGER_INFO(logger, "GraphicsCaptureFrameSource 初始化完成");
|
||||
return true;
|
||||
}
|
||||
|
||||
ComPtr<ID3D11Texture2D> GraphicsCaptureFrameSource::GetOutput() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue