2022-03-30 19:10:27 +02:00
|
|
|
#include "winrt-capture.h"
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
extern "C" {
|
|
|
|
HRESULT __stdcall CreateDirect3D11DeviceFromDXGIDevice(
|
|
|
|
::IDXGIDevice *dxgiDevice, ::IInspectable **graphicsDevice);
|
|
|
|
|
|
|
|
HRESULT __stdcall CreateDirect3D11SurfaceFromDXGISurface(
|
|
|
|
::IDXGISurface *dgxiSurface, ::IInspectable **graphicsSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct __declspec(uuid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1"))
|
|
|
|
IDirect3DDxgiInterfaceAccess : ::IUnknown {
|
|
|
|
virtual HRESULT __stdcall GetInterface(GUID const &id,
|
|
|
|
void **object) = 0;
|
|
|
|
};
|
|
|
|
|
2020-03-07 18:27:58 +01:00
|
|
|
extern "C" EXPORT BOOL winrt_capture_supported()
|
2020-03-18 17:21:40 +01:00
|
|
|
try {
|
2020-03-18 08:33:12 +01:00
|
|
|
/* no contract for IGraphicsCaptureItemInterop, verify 10.0.18362.0 */
|
|
|
|
return winrt::Windows::Foundation::Metadata::ApiInformation::
|
|
|
|
IsApiContractPresent(L"Windows.Foundation.UniversalApiContract",
|
|
|
|
8);
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (const winrt::hresult_error &err) {
|
2021-08-19 02:07:00 +02:00
|
|
|
blog(LOG_ERROR, "winrt_capture_supported (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2020-03-18 17:21:40 +01:00
|
|
|
return false;
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "winrt_capture_supported (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2020-03-25 16:36:15 +01:00
|
|
|
return false;
|
2020-03-07 18:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" EXPORT BOOL winrt_capture_cursor_toggle_supported()
|
2020-03-18 17:21:40 +01:00
|
|
|
try {
|
2019-11-28 01:38:10 +01:00
|
|
|
return winrt::Windows::Foundation::Metadata::ApiInformation::
|
2020-03-07 18:27:58 +01:00
|
|
|
IsPropertyPresent(
|
|
|
|
L"Windows.Graphics.Capture.GraphicsCaptureSession",
|
|
|
|
L"IsCursorCaptureEnabled");
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (const winrt::hresult_error &err) {
|
2020-03-18 17:21:40 +01:00
|
|
|
blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2020-03-18 17:21:40 +01:00
|
|
|
return false;
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2020-03-25 16:36:15 +01:00
|
|
|
return false;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static winrt::com_ptr<T> GetDXGIInterfaceFromObject(
|
|
|
|
winrt::Windows::Foundation::IInspectable const &object)
|
|
|
|
{
|
|
|
|
auto access = object.as<IDirect3DDxgiInterfaceAccess>();
|
|
|
|
winrt::com_ptr<T> result;
|
|
|
|
winrt::check_hresult(
|
|
|
|
access->GetInterface(winrt::guid_of<T>(), result.put_void()));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-25 06:21:52 +01:00
|
|
|
static bool get_client_box(HWND window, uint32_t width, uint32_t height,
|
|
|
|
D3D11_BOX *client_box)
|
|
|
|
{
|
2020-05-10 18:39:06 +02:00
|
|
|
RECT client_rect{}, window_rect{};
|
2020-02-25 06:21:52 +01:00
|
|
|
POINT upper_left{};
|
|
|
|
|
2020-05-10 18:39:06 +02:00
|
|
|
/* check iconic (minimized) twice, ABA is very unlikely */
|
|
|
|
bool client_box_available =
|
|
|
|
!IsIconic(window) && GetClientRect(window, &client_rect) &&
|
|
|
|
!IsIconic(window) && (client_rect.right > 0) &&
|
|
|
|
(client_rect.bottom > 0) &&
|
2020-02-25 06:21:52 +01:00
|
|
|
(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS,
|
|
|
|
&window_rect,
|
|
|
|
sizeof(window_rect)) == S_OK) &&
|
|
|
|
ClientToScreen(window, &upper_left);
|
|
|
|
if (client_box_available) {
|
|
|
|
const uint32_t left =
|
|
|
|
(upper_left.x > window_rect.left)
|
|
|
|
? (upper_left.x - window_rect.left)
|
|
|
|
: 0;
|
|
|
|
client_box->left = left;
|
|
|
|
|
|
|
|
const uint32_t top = (upper_left.y > window_rect.top)
|
|
|
|
? (upper_left.y - window_rect.top)
|
|
|
|
: 0;
|
|
|
|
client_box->top = top;
|
|
|
|
|
|
|
|
uint32_t texture_width = 1;
|
|
|
|
if (width > left) {
|
|
|
|
texture_width =
|
|
|
|
min(width - left, (uint32_t)client_rect.right);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t texture_height = 1;
|
|
|
|
if (height > top) {
|
|
|
|
texture_height =
|
|
|
|
min(height - top, (uint32_t)client_rect.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
client_box->right = left + texture_width;
|
|
|
|
client_box->bottom = top + texture_height;
|
|
|
|
|
|
|
|
client_box->front = 0;
|
|
|
|
client_box->back = 1;
|
2020-05-10 18:39:06 +02:00
|
|
|
|
|
|
|
client_box_available = (client_box->right <= width) &&
|
|
|
|
(client_box->bottom <= height);
|
2020-02-25 06:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return client_box_available;
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:16:44 +02:00
|
|
|
static DXGI_FORMAT get_pixel_format(HWND window, HMONITOR monitor,
|
|
|
|
BOOL force_sdr)
|
2022-03-10 06:42:17 +01:00
|
|
|
{
|
2022-09-04 21:16:44 +02:00
|
|
|
static constexpr DXGI_FORMAT sdr_format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
|
|
|
|
|
|
|
if (force_sdr)
|
|
|
|
return sdr_format;
|
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
if (window)
|
|
|
|
monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
|
|
|
|
|
|
|
|
return (monitor && gs_is_monitor_hdr(monitor))
|
|
|
|
? DXGI_FORMAT_R16G16B16A16_FLOAT
|
2022-09-04 21:16:44 +02:00
|
|
|
: sdr_format;
|
2022-03-10 06:42:17 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
struct winrt_capture {
|
2020-02-25 06:21:52 +01:00
|
|
|
HWND window;
|
2022-09-04 21:16:44 +02:00
|
|
|
BOOL client_area;
|
|
|
|
BOOL force_sdr;
|
2021-01-26 03:53:35 +01:00
|
|
|
HMONITOR monitor;
|
2022-03-10 06:42:17 +01:00
|
|
|
DXGI_FORMAT format;
|
2019-11-28 01:38:10 +01:00
|
|
|
|
2020-03-07 18:27:58 +01:00
|
|
|
bool capture_cursor;
|
2020-11-03 07:08:46 +01:00
|
|
|
BOOL cursor_visible;
|
2020-03-07 18:27:58 +01:00
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
gs_texture_t *texture;
|
|
|
|
bool texture_written;
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item{nullptr};
|
|
|
|
winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device{
|
|
|
|
nullptr};
|
|
|
|
ComPtr<ID3D11DeviceContext> context;
|
|
|
|
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool frame_pool{
|
|
|
|
nullptr};
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureSession session{
|
|
|
|
nullptr};
|
|
|
|
winrt::Windows::Graphics::SizeInt32 last_size;
|
2020-05-07 06:32:57 +02:00
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem::Closed_revoker
|
|
|
|
closed;
|
2019-11-28 01:38:10 +01:00
|
|
|
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::
|
|
|
|
FrameArrived_revoker frame_arrived;
|
|
|
|
|
2020-02-25 06:21:52 +01:00
|
|
|
uint32_t texture_width;
|
|
|
|
uint32_t texture_height;
|
|
|
|
D3D11_BOX client_box;
|
|
|
|
|
2020-08-21 07:12:39 +02:00
|
|
|
BOOL active;
|
2019-11-28 01:38:10 +01:00
|
|
|
struct winrt_capture *next;
|
|
|
|
|
2020-05-10 18:39:06 +02:00
|
|
|
void on_closed(
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem const &,
|
|
|
|
winrt::Windows::Foundation::IInspectable const &)
|
2020-05-07 06:32:57 +02:00
|
|
|
{
|
|
|
|
active = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
void on_frame_arrived(winrt::Windows::Graphics::Capture::
|
|
|
|
Direct3D11CaptureFramePool const &sender,
|
|
|
|
winrt::Windows::Foundation::IInspectable const &)
|
|
|
|
{
|
|
|
|
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame
|
|
|
|
frame = sender.TryGetNextFrame();
|
|
|
|
const winrt::Windows::Graphics::SizeInt32 frame_content_size =
|
|
|
|
frame.ContentSize();
|
|
|
|
|
|
|
|
winrt::com_ptr<ID3D11Texture2D> frame_surface =
|
|
|
|
GetDXGIInterfaceFromObject<ID3D11Texture2D>(
|
|
|
|
frame.Surface());
|
|
|
|
|
|
|
|
/* need GetDesc because ContentSize is not reliable */
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
frame_surface->GetDesc(&desc);
|
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
obs_enter_graphics();
|
2020-02-25 06:21:52 +01:00
|
|
|
|
2022-09-04 21:16:44 +02:00
|
|
|
if (desc.Format ==
|
|
|
|
get_pixel_format(window, monitor, force_sdr)) {
|
2022-03-10 06:42:17 +01:00
|
|
|
if (!client_area ||
|
|
|
|
get_client_box(window, desc.Width, desc.Height,
|
|
|
|
&client_box)) {
|
|
|
|
if (client_area) {
|
|
|
|
texture_width = client_box.right -
|
|
|
|
client_box.left;
|
|
|
|
texture_height = client_box.bottom -
|
|
|
|
client_box.top;
|
|
|
|
} else {
|
|
|
|
texture_width = desc.Width;
|
|
|
|
texture_height = desc.Height;
|
2020-05-10 18:39:06 +02:00
|
|
|
}
|
2020-02-25 06:21:52 +01:00
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
if (texture) {
|
|
|
|
if (texture_width !=
|
|
|
|
gs_texture_get_width(
|
|
|
|
texture) ||
|
|
|
|
texture_height !=
|
|
|
|
gs_texture_get_height(
|
|
|
|
texture)) {
|
|
|
|
gs_texture_destroy(texture);
|
|
|
|
texture = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
if (!texture) {
|
|
|
|
const gs_color_format color_format =
|
|
|
|
desc.Format == DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
? GS_RGBA16F
|
|
|
|
: GS_BGRA;
|
|
|
|
texture = gs_texture_create(
|
|
|
|
texture_width, texture_height,
|
|
|
|
color_format, 1, NULL, 0);
|
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
if (client_area) {
|
|
|
|
context->CopySubresourceRegion(
|
|
|
|
(ID3D11Texture2D *)
|
|
|
|
gs_texture_get_obj(
|
|
|
|
texture),
|
|
|
|
0, 0, 0, 0, frame_surface.get(),
|
|
|
|
0, &client_box);
|
|
|
|
} else {
|
|
|
|
/* if they gave an SRV, we could avoid this copy */
|
|
|
|
context->CopyResource(
|
|
|
|
(ID3D11Texture2D *)
|
|
|
|
gs_texture_get_obj(
|
|
|
|
texture),
|
|
|
|
frame_surface.get());
|
|
|
|
}
|
2020-03-07 18:27:58 +01:00
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
texture_written = true;
|
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
if (frame_content_size.Width != last_size.Width ||
|
|
|
|
frame_content_size.Height != last_size.Height) {
|
|
|
|
format = desc.Format;
|
|
|
|
frame_pool.Recreate(
|
|
|
|
device,
|
|
|
|
static_cast<
|
|
|
|
winrt::Windows::Graphics::DirectX::
|
|
|
|
DirectXPixelFormat>(
|
|
|
|
format),
|
|
|
|
2, frame_content_size);
|
|
|
|
|
|
|
|
last_size = frame_content_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
active = FALSE;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-21 09:23:14 +02:00
|
|
|
static struct winrt_capture *capture_list;
|
2019-11-28 01:38:10 +01:00
|
|
|
|
|
|
|
static void winrt_capture_device_loss_release(void *data)
|
|
|
|
{
|
|
|
|
winrt_capture *capture = static_cast<winrt_capture *>(data);
|
2020-08-21 07:12:39 +02:00
|
|
|
capture->active = FALSE;
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
capture->frame_arrived.revoke();
|
2021-02-15 07:12:13 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
capture->frame_pool.Close();
|
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Direct3D11CaptureFramePool::Close (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "Direct3D11CaptureFramePool::Close (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-02-15 07:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
capture->session.Close();
|
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-02-15 07:12:13 +01:00
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
|
|
|
|
capture->session = nullptr;
|
|
|
|
capture->frame_pool = nullptr;
|
|
|
|
capture->context = nullptr;
|
|
|
|
capture->device = nullptr;
|
2020-05-21 09:23:14 +02:00
|
|
|
capture->item = nullptr;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 04:09:08 +01:00
|
|
|
static bool winrt_capture_border_toggle_supported()
|
|
|
|
try {
|
|
|
|
return winrt::Windows::Foundation::Metadata::ApiInformation::
|
|
|
|
IsPropertyPresent(
|
|
|
|
L"Windows.Graphics.Capture.GraphicsCaptureSession",
|
|
|
|
L"IsBorderRequired");
|
|
|
|
} catch (const winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-01-15 04:09:08 +01:00
|
|
|
return false;
|
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-01-15 04:09:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:53:35 +01:00
|
|
|
static winrt::Windows::Graphics::Capture::GraphicsCaptureItem
|
|
|
|
winrt_capture_create_item(IGraphicsCaptureItemInterop *const interop_factory,
|
|
|
|
HWND window, HMONITOR monitor)
|
|
|
|
{
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr};
|
|
|
|
if (window) {
|
|
|
|
try {
|
|
|
|
const HRESULT hr = interop_factory->CreateForWindow(
|
|
|
|
window,
|
|
|
|
winrt::guid_of<ABI::Windows::Graphics::Capture::
|
|
|
|
IGraphicsCaptureItem>(),
|
|
|
|
reinterpret_cast<void **>(
|
|
|
|
winrt::put_abi(item)));
|
|
|
|
if (FAILED(hr))
|
|
|
|
blog(LOG_ERROR, "CreateForWindow (0x%08X)", hr);
|
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR, "CreateForWindow (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-01-26 03:53:35 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "CreateForWindow (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-01-26 03:53:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(monitor);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const HRESULT hr = interop_factory->CreateForMonitor(
|
|
|
|
monitor,
|
|
|
|
winrt::guid_of<ABI::Windows::Graphics::Capture::
|
|
|
|
IGraphicsCaptureItem>(),
|
|
|
|
reinterpret_cast<void **>(
|
|
|
|
winrt::put_abi(item)));
|
|
|
|
if (FAILED(hr))
|
|
|
|
blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
|
|
|
|
hr);
|
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR, "CreateForMonitor (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-01-26 03:53:35 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-01-26 03:53:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
|
|
|
|
{
|
|
|
|
winrt_capture *capture = static_cast<winrt_capture *>(data);
|
|
|
|
|
2020-05-21 09:23:14 +02:00
|
|
|
auto activation_factory = winrt::get_activation_factory<
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
|
|
|
|
auto interop_factory =
|
|
|
|
activation_factory.as<IGraphicsCaptureItemInterop>();
|
2021-01-26 03:53:35 +01:00
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
|
|
|
|
winrt_capture_create_item(interop_factory.get(),
|
|
|
|
capture->window, capture->monitor);
|
2022-03-30 19:10:27 +02:00
|
|
|
if (!item)
|
|
|
|
return;
|
2020-05-21 09:23:14 +02:00
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
ID3D11Device *const d3d_device = (ID3D11Device *)device_void;
|
|
|
|
ComPtr<IDXGIDevice> dxgi_device;
|
|
|
|
if (FAILED(d3d_device->QueryInterface(&dxgi_device)))
|
|
|
|
blog(LOG_ERROR, "Failed to get DXGI device");
|
|
|
|
|
|
|
|
winrt::com_ptr<IInspectable> inspectable;
|
|
|
|
if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
|
|
|
|
inspectable.put())))
|
|
|
|
blog(LOG_ERROR, "Failed to get WinRT device");
|
|
|
|
|
|
|
|
const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
|
|
|
|
device = inspectable.as<winrt::Windows::Graphics::DirectX::
|
|
|
|
Direct3D11::IDirect3DDevice>();
|
|
|
|
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
|
|
|
|
frame_pool = winrt::Windows::Graphics::Capture::
|
|
|
|
Direct3D11CaptureFramePool::Create(
|
|
|
|
device,
|
2022-03-10 06:42:17 +01:00
|
|
|
static_cast<winrt::Windows::Graphics::DirectX::
|
|
|
|
DirectXPixelFormat>(
|
|
|
|
capture->format),
|
2019-11-28 01:38:10 +01:00
|
|
|
2, capture->last_size);
|
|
|
|
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
2020-05-21 09:23:14 +02:00
|
|
|
frame_pool.CreateCaptureSession(item);
|
2019-11-28 01:38:10 +01:00
|
|
|
|
2021-01-15 04:09:08 +01:00
|
|
|
if (winrt_capture_border_toggle_supported()) {
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
|
|
|
|
RequestAccessAsync(
|
|
|
|
winrt::Windows::Graphics::Capture::
|
|
|
|
GraphicsCaptureAccessKind::Borderless)
|
|
|
|
.get();
|
|
|
|
session.IsBorderRequired(false);
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:27:58 +01:00
|
|
|
if (winrt_capture_cursor_toggle_supported())
|
2020-10-04 01:53:21 +02:00
|
|
|
session.IsCursorCaptureEnabled(capture->capture_cursor &&
|
|
|
|
capture->cursor_visible);
|
2020-03-07 18:27:58 +01:00
|
|
|
|
2020-05-21 09:23:14 +02:00
|
|
|
capture->item = item;
|
2019-11-28 01:38:10 +01:00
|
|
|
capture->device = device;
|
|
|
|
d3d_device->GetImmediateContext(&capture->context);
|
|
|
|
capture->frame_pool = frame_pool;
|
|
|
|
capture->session = session;
|
|
|
|
capture->frame_arrived = frame_pool.FrameArrived(
|
|
|
|
winrt::auto_revoke,
|
|
|
|
{capture, &winrt_capture::on_frame_arrived});
|
|
|
|
|
2020-08-21 07:12:39 +02:00
|
|
|
try {
|
|
|
|
session.StartCapture();
|
|
|
|
capture->active = TRUE;
|
|
|
|
} catch (winrt::hresult_error &err) {
|
2021-08-19 09:56:34 +02:00
|
|
|
blog(LOG_ERROR, "StartCapture (0x%08X): %ls", err.code().value,
|
|
|
|
err.message().c_str());
|
2020-08-21 07:12:39 +02:00
|
|
|
} catch (...) {
|
2021-08-19 02:07:00 +02:00
|
|
|
blog(LOG_ERROR, "StartCapture (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2020-08-21 07:12:39 +02:00
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
2022-09-04 21:16:44 +02:00
|
|
|
static struct winrt_capture *
|
|
|
|
winrt_capture_init_internal(BOOL cursor, HWND window, BOOL client_area,
|
|
|
|
BOOL force_sdr, HMONITOR monitor)
|
2020-03-17 20:22:20 +01:00
|
|
|
try {
|
2019-11-28 01:38:10 +01:00
|
|
|
ID3D11Device *const d3d_device = (ID3D11Device *)gs_get_device_obj();
|
|
|
|
ComPtr<IDXGIDevice> dxgi_device;
|
2020-03-17 20:22:20 +01:00
|
|
|
|
|
|
|
HRESULT hr = d3d_device->QueryInterface(&dxgi_device);
|
|
|
|
if (FAILED(hr)) {
|
2020-03-18 08:33:12 +01:00
|
|
|
blog(LOG_ERROR, "Failed to get DXGI device");
|
2019-11-28 01:38:10 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
winrt::com_ptr<IInspectable> inspectable;
|
2020-03-17 20:22:20 +01:00
|
|
|
hr = CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(),
|
|
|
|
inspectable.put());
|
2019-11-28 01:38:10 +01:00
|
|
|
if (FAILED(hr)) {
|
2020-03-18 08:33:12 +01:00
|
|
|
blog(LOG_ERROR, "Failed to get WinRT device");
|
2019-11-28 01:38:10 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto activation_factory = winrt::get_activation_factory<
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
|
|
|
|
auto interop_factory =
|
|
|
|
activation_factory.as<IGraphicsCaptureItemInterop>();
|
2021-01-26 03:53:35 +01:00
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item =
|
|
|
|
winrt_capture_create_item(interop_factory.get(), window,
|
|
|
|
monitor);
|
|
|
|
if (!item)
|
2019-11-28 01:38:10 +01:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice
|
|
|
|
device = inspectable.as<winrt::Windows::Graphics::DirectX::
|
|
|
|
Direct3D11::IDirect3DDevice>();
|
|
|
|
const winrt::Windows::Graphics::SizeInt32 size = item.Size();
|
2022-09-04 21:16:44 +02:00
|
|
|
const DXGI_FORMAT format = get_pixel_format(window, monitor, force_sdr);
|
2019-11-28 01:38:10 +01:00
|
|
|
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
|
|
|
|
frame_pool = winrt::Windows::Graphics::Capture::
|
|
|
|
Direct3D11CaptureFramePool::Create(
|
|
|
|
device,
|
2022-03-10 06:42:17 +01:00
|
|
|
static_cast<winrt::Windows::Graphics::DirectX::
|
|
|
|
DirectXPixelFormat>(format),
|
2019-11-28 01:38:10 +01:00
|
|
|
2, size);
|
|
|
|
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
|
|
|
frame_pool.CreateCaptureSession(item);
|
|
|
|
|
2021-01-15 04:09:08 +01:00
|
|
|
if (winrt_capture_border_toggle_supported()) {
|
|
|
|
winrt::Windows::Graphics::Capture::GraphicsCaptureAccess::
|
|
|
|
RequestAccessAsync(
|
|
|
|
winrt::Windows::Graphics::Capture::
|
|
|
|
GraphicsCaptureAccessKind::Borderless)
|
|
|
|
.get();
|
|
|
|
session.IsBorderRequired(false);
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:27:58 +01:00
|
|
|
/* disable cursor capture if possible since ours performs better */
|
|
|
|
const BOOL cursor_toggle_supported =
|
|
|
|
winrt_capture_cursor_toggle_supported();
|
|
|
|
if (cursor_toggle_supported)
|
2020-10-04 01:53:21 +02:00
|
|
|
session.IsCursorCaptureEnabled(cursor);
|
2020-03-07 18:27:58 +01:00
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
struct winrt_capture *capture = new winrt_capture{};
|
2020-02-25 06:21:52 +01:00
|
|
|
capture->window = window;
|
|
|
|
capture->client_area = client_area;
|
2022-09-04 21:16:44 +02:00
|
|
|
capture->force_sdr = force_sdr;
|
2021-01-26 03:53:35 +01:00
|
|
|
capture->monitor = monitor;
|
2022-03-10 06:42:17 +01:00
|
|
|
capture->format = format;
|
2020-03-07 18:27:58 +01:00
|
|
|
capture->capture_cursor = cursor && cursor_toggle_supported;
|
2020-10-04 01:53:21 +02:00
|
|
|
capture->cursor_visible = cursor;
|
2019-11-28 01:38:10 +01:00
|
|
|
capture->item = item;
|
|
|
|
capture->device = device;
|
|
|
|
d3d_device->GetImmediateContext(&capture->context);
|
|
|
|
capture->frame_pool = frame_pool;
|
|
|
|
capture->session = session;
|
|
|
|
capture->last_size = size;
|
2020-05-07 06:32:57 +02:00
|
|
|
capture->closed = item.Closed(winrt::auto_revoke,
|
|
|
|
{capture, &winrt_capture::on_closed});
|
2019-11-28 01:38:10 +01:00
|
|
|
capture->frame_arrived = frame_pool.FrameArrived(
|
|
|
|
winrt::auto_revoke,
|
|
|
|
{capture, &winrt_capture::on_frame_arrived});
|
|
|
|
capture->next = capture_list;
|
|
|
|
capture_list = capture;
|
|
|
|
|
|
|
|
session.StartCapture();
|
2020-08-21 07:12:39 +02:00
|
|
|
capture->active = TRUE;
|
2019-11-28 01:38:10 +01:00
|
|
|
|
|
|
|
gs_device_loss callbacks;
|
|
|
|
callbacks.device_loss_release = winrt_capture_device_loss_release;
|
|
|
|
callbacks.device_loss_rebuild = winrt_capture_device_loss_rebuild;
|
|
|
|
callbacks.data = capture;
|
|
|
|
gs_register_loss_callbacks(&callbacks);
|
|
|
|
|
|
|
|
return capture;
|
2020-03-17 20:22:20 +01:00
|
|
|
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (const winrt::hresult_error &err) {
|
2021-08-19 09:56:34 +02:00
|
|
|
blog(LOG_ERROR, "winrt_capture_init (0x%08X): %ls", err.code().value,
|
2020-03-18 08:33:12 +01:00
|
|
|
err.message().c_str());
|
2020-03-17 20:22:20 +01:00
|
|
|
return nullptr;
|
2020-03-25 16:36:15 +01:00
|
|
|
} catch (...) {
|
2021-08-19 02:07:00 +02:00
|
|
|
blog(LOG_ERROR, "winrt_capture_init (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2020-03-25 16:36:15 +01:00
|
|
|
return nullptr;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 03:53:35 +01:00
|
|
|
extern "C" EXPORT struct winrt_capture *
|
2022-09-04 21:16:44 +02:00
|
|
|
winrt_capture_init_window(BOOL cursor, HWND window, BOOL client_area,
|
|
|
|
BOOL force_sdr)
|
2021-01-26 03:53:35 +01:00
|
|
|
{
|
2022-09-04 21:16:44 +02:00
|
|
|
return winrt_capture_init_internal(cursor, window, client_area,
|
|
|
|
force_sdr, NULL);
|
2021-01-26 03:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" EXPORT struct winrt_capture *
|
2022-12-28 23:22:24 +01:00
|
|
|
winrt_capture_init_monitor(BOOL cursor, HMONITOR monitor, BOOL force_sdr)
|
2021-01-26 03:53:35 +01:00
|
|
|
{
|
2022-12-28 23:22:24 +01:00
|
|
|
return winrt_capture_init_internal(cursor, NULL, false, force_sdr,
|
|
|
|
monitor);
|
2021-01-26 03:53:35 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 01:38:10 +01:00
|
|
|
extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
|
|
|
|
{
|
|
|
|
if (capture) {
|
|
|
|
struct winrt_capture *current = capture_list;
|
|
|
|
if (current == capture) {
|
|
|
|
capture_list = capture->next;
|
|
|
|
} else {
|
|
|
|
struct winrt_capture *previous;
|
|
|
|
do {
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
} while (current != capture);
|
|
|
|
|
|
|
|
previous->next = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
|
|
|
gs_unregister_loss_callbacks(capture);
|
|
|
|
gs_texture_destroy(capture->texture);
|
|
|
|
obs_leave_graphics();
|
|
|
|
|
|
|
|
capture->frame_arrived.revoke();
|
2020-05-07 06:32:57 +02:00
|
|
|
capture->closed.revoke();
|
2021-02-15 07:12:13 +01:00
|
|
|
|
|
|
|
try {
|
2022-03-30 19:10:27 +02:00
|
|
|
if (capture->frame_pool)
|
|
|
|
capture->frame_pool.Close();
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Direct3D11CaptureFramePool::Close (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Direct3D11CaptureFramePool::Close (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-02-15 07:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-03-30 19:10:27 +02:00
|
|
|
if (capture->session)
|
|
|
|
capture->session.Close();
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"GraphicsCaptureSession::Close (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-02-15 07:12:13 +01:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"GraphicsCaptureSession::Close (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2021-02-15 07:12:13 +01:00
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
|
|
|
|
delete capture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:32:57 +02:00
|
|
|
extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
|
|
|
|
{
|
|
|
|
return capture->active;
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:17:22 +02:00
|
|
|
extern "C" EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
|
2020-03-07 18:27:58 +01:00
|
|
|
BOOL visible)
|
|
|
|
{
|
2021-05-08 06:17:22 +02:00
|
|
|
BOOL success = FALSE;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (capture->capture_cursor) {
|
|
|
|
if (capture->cursor_visible != visible) {
|
|
|
|
capture->session.IsCursorCaptureEnabled(
|
|
|
|
visible);
|
|
|
|
capture->cursor_visible = visible;
|
|
|
|
}
|
2020-10-04 01:53:21 +02:00
|
|
|
}
|
2021-05-08 06:17:22 +02:00
|
|
|
|
|
|
|
success = TRUE;
|
|
|
|
} catch (winrt::hresult_error &err) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X): %ls",
|
2021-08-19 09:56:34 +02:00
|
|
|
err.code().value, err.message().c_str());
|
2021-05-08 06:17:22 +02:00
|
|
|
} catch (...) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X)",
|
2021-08-19 09:56:34 +02:00
|
|
|
winrt::to_hresult().value);
|
2020-10-04 01:53:21 +02:00
|
|
|
}
|
2021-05-08 06:17:22 +02:00
|
|
|
|
|
|
|
return success;
|
2020-03-07 18:27:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
extern "C" EXPORT enum gs_color_space
|
|
|
|
winrt_capture_get_color_space(const struct winrt_capture *capture)
|
|
|
|
{
|
|
|
|
return (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT)
|
|
|
|
? GS_CS_709_EXTENDED
|
|
|
|
: GS_CS_SRGB;
|
|
|
|
}
|
|
|
|
|
2021-07-07 07:10:57 +02:00
|
|
|
extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture)
|
2019-11-28 01:38:10 +01:00
|
|
|
{
|
2022-03-10 06:42:17 +01:00
|
|
|
if (capture->texture_written) {
|
|
|
|
const char *tech_name = "Draw";
|
|
|
|
float multiplier = 1.f;
|
|
|
|
const gs_color_space current_space = gs_get_color_space();
|
|
|
|
if (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT) {
|
|
|
|
switch (current_space) {
|
|
|
|
case GS_CS_SRGB:
|
|
|
|
case GS_CS_SRGB_16F:
|
|
|
|
tech_name = "DrawMultiplyTonemap";
|
|
|
|
multiplier =
|
|
|
|
80.f / obs_get_video_sdr_white_level();
|
|
|
|
break;
|
|
|
|
case GS_CS_709_EXTENDED:
|
|
|
|
tech_name = "DrawMultiply";
|
|
|
|
multiplier =
|
|
|
|
80.f / obs_get_video_sdr_white_level();
|
|
|
|
}
|
|
|
|
} else if (current_space == GS_CS_709_SCRGB) {
|
|
|
|
tech_name = "DrawMultiply";
|
|
|
|
multiplier = obs_get_video_sdr_white_level() / 80.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_effect_t *const effect =
|
2022-05-03 08:18:55 +02:00
|
|
|
obs_get_base_effect(OBS_EFFECT_DEFAULT);
|
2022-03-10 06:42:17 +01:00
|
|
|
gs_technique_t *tech =
|
|
|
|
gs_effect_get_technique(effect, tech_name);
|
|
|
|
|
|
|
|
const bool previous = gs_framebuffer_srgb_enabled();
|
|
|
|
gs_enable_framebuffer_srgb(true);
|
2022-05-03 08:18:55 +02:00
|
|
|
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
|
2022-03-10 06:42:17 +01:00
|
|
|
|
|
|
|
gs_texture_t *const texture = capture->texture;
|
|
|
|
gs_effect_set_texture_srgb(
|
|
|
|
gs_effect_get_param_by_name(effect, "image"), texture);
|
|
|
|
gs_effect_set_float(gs_effect_get_param_by_name(effect,
|
|
|
|
"multiplier"),
|
|
|
|
multiplier);
|
|
|
|
|
|
|
|
const size_t passes = gs_technique_begin(tech);
|
|
|
|
for (size_t i = 0; i < passes; i++) {
|
|
|
|
if (gs_technique_begin_pass(tech, i)) {
|
|
|
|
gs_draw_sprite(texture, 0, 0, 0);
|
|
|
|
|
|
|
|
gs_technique_end_pass(tech);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gs_technique_end(tech);
|
|
|
|
|
2022-05-03 08:18:55 +02:00
|
|
|
gs_blend_state_pop();
|
|
|
|
|
2022-03-10 06:42:17 +01:00
|
|
|
gs_enable_framebuffer_srgb(previous);
|
|
|
|
}
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 06:21:52 +01:00
|
|
|
extern "C" EXPORT uint32_t
|
2019-11-28 01:38:10 +01:00
|
|
|
winrt_capture_width(const struct winrt_capture *capture)
|
|
|
|
{
|
2020-02-25 06:21:52 +01:00
|
|
|
return capture ? capture->texture_width : 0;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 06:21:52 +01:00
|
|
|
extern "C" EXPORT uint32_t
|
2019-11-28 01:38:10 +01:00
|
|
|
winrt_capture_height(const struct winrt_capture *capture)
|
|
|
|
{
|
2020-02-25 06:21:52 +01:00
|
|
|
return capture ? capture->texture_height : 0;
|
2019-11-28 01:38:10 +01:00
|
|
|
}
|
2020-05-21 09:23:14 +02:00
|
|
|
|
|
|
|
extern "C" EXPORT void winrt_capture_thread_start()
|
|
|
|
{
|
|
|
|
struct winrt_capture *capture = capture_list;
|
|
|
|
void *const device = gs_get_device_obj();
|
|
|
|
while (capture) {
|
|
|
|
winrt_capture_device_loss_rebuild(device, capture);
|
|
|
|
capture = capture->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" EXPORT void winrt_capture_thread_stop()
|
|
|
|
{
|
|
|
|
struct winrt_capture *capture = capture_list;
|
|
|
|
while (capture) {
|
|
|
|
winrt_capture_device_loss_release(capture);
|
|
|
|
capture = capture->next;
|
|
|
|
}
|
|
|
|
}
|