New upstream version 26.0.0+dfsg1

This commit is contained in:
Sebastian Ramacher 2020-10-01 22:15:25 +02:00
parent 8e020cdacb
commit 240080891f
837 changed files with 41275 additions and 9196 deletions

View file

@ -30,14 +30,10 @@ try {
extern "C" EXPORT BOOL winrt_capture_cursor_toggle_supported()
try {
#ifdef NTDDI_WIN10_VB
return winrt::Windows::Foundation::Metadata::ApiInformation::
IsPropertyPresent(
L"Windows.Graphics.Capture.GraphicsCaptureSession",
L"IsCursorCaptureEnabled");
#else
return false;
#endif
} catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X): %ls",
err.to_abi(), err.message().c_str());
@ -62,11 +58,14 @@ static winrt::com_ptr<T> GetDXGIInterfaceFromObject(
static bool get_client_box(HWND window, uint32_t width, uint32_t height,
D3D11_BOX *client_box)
{
RECT client_rect, window_rect{};
RECT client_rect{}, window_rect{};
POINT upper_left{};
const bool client_box_available =
GetClientRect(window, &client_rect) &&
/* 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) &&
(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS,
&window_rect,
sizeof(window_rect)) == S_OK) &&
@ -100,6 +99,9 @@ static bool get_client_box(HWND window, uint32_t width, uint32_t height,
client_box->front = 0;
client_box->back = 1;
client_box_available = (client_box->right <= width) &&
(client_box->bottom <= height);
}
return client_box_available;
@ -123,15 +125,16 @@ struct winrt_capture {
winrt::Windows::Graphics::Capture::GraphicsCaptureSession session{
nullptr};
winrt::Windows::Graphics::SizeInt32 last_size;
winrt::Windows::Graphics::Capture::GraphicsCaptureItem::Closed_revoker
closed;
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool::
FrameArrived_revoker frame_arrived;
uint32_t texture_width;
uint32_t texture_height;
D3D11_BOX client_box;
bool client_box_available;
bool thread_changed;
BOOL active;
struct winrt_capture *next;
void draw_cursor()
@ -186,6 +189,13 @@ struct winrt_capture {
DestroyIcon(icon);
}
void on_closed(
winrt::Windows::Graphics::Capture::GraphicsCaptureItem const &,
winrt::Windows::Foundation::IInspectable const &)
{
active = FALSE;
}
void on_frame_arrived(winrt::Windows::Graphics::Capture::
Direct3D11CaptureFramePool const &sender,
winrt::Windows::Foundation::IInspectable const &)
@ -205,50 +215,53 @@ struct winrt_capture {
D3D11_TEXTURE2D_DESC desc;
frame_surface->GetDesc(&desc);
client_box_available = false;
if (client_area) {
client_box_available = get_client_box(
window, desc.Width, desc.Height, &client_box);
}
if (client_box_available) {
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;
}
if (texture) {
if (texture_width != gs_texture_get_width(texture) ||
texture_height != gs_texture_get_height(texture)) {
gs_texture_destroy(texture);
texture = nullptr;
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;
}
}
if (!texture) {
texture = gs_texture_create_gdi(texture_width,
texture_height);
}
if (texture) {
if (texture_width !=
gs_texture_get_width(texture) ||
texture_height !=
gs_texture_get_height(texture)) {
gs_texture_destroy(texture);
texture = nullptr;
}
}
if (client_box_available) {
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());
}
if (!texture) {
texture = gs_texture_create_gdi(texture_width,
texture_height);
}
if (capture_cursor && cursor_visible) {
draw_cursor();
}
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());
}
texture_written = true;
if (capture_cursor && cursor_visible) {
draw_cursor();
}
texture_written = true;
}
if (frame_content_size.Width != last_size.Width ||
frame_content_size.Height != last_size.Height) {
@ -265,11 +278,13 @@ struct winrt_capture {
}
};
struct winrt_capture *capture_list;
static struct winrt_capture *capture_list;
static void winrt_capture_device_loss_release(void *data)
{
winrt_capture *capture = static_cast<winrt_capture *>(data);
capture->active = FALSE;
capture->frame_arrived.revoke();
capture->frame_pool.Close();
capture->session.Close();
@ -278,12 +293,32 @@ static void winrt_capture_device_loss_release(void *data)
capture->frame_pool = nullptr;
capture->context = nullptr;
capture->device = nullptr;
capture->item = nullptr;
}
static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
{
winrt_capture *capture = static_cast<winrt_capture *>(data);
auto activation_factory = winrt::get_activation_factory<
winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
auto interop_factory =
activation_factory.as<IGraphicsCaptureItemInterop>();
winrt::Windows::Graphics::Capture::GraphicsCaptureItem item = {nullptr};
try {
interop_factory->CreateForWindow(
capture->window,
winrt::guid_of<ABI::Windows::Graphics::Capture::
IGraphicsCaptureItem>(),
reinterpret_cast<void **>(winrt::put_abi(item)));
} catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "CreateForWindow (0x%08X): %ls", err.to_abi(),
err.message().c_str());
} catch (...) {
blog(LOG_ERROR, "CreateForWindow (0x%08X)",
winrt::to_hresult());
}
ID3D11Device *const d3d_device = (ID3D11Device *)device_void;
ComPtr<IDXGIDevice> dxgi_device;
if (FAILED(d3d_device->QueryInterface(&dxgi_device)))
@ -305,14 +340,13 @@ static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
DirectXPixelFormat::B8G8R8A8UIntNormalized,
2, capture->last_size);
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
frame_pool.CreateCaptureSession(capture->item);
frame_pool.CreateCaptureSession(item);
/* disable cursor capture if possible since ours performs better */
#ifdef NTDDI_WIN10_VB
if (winrt_capture_cursor_toggle_supported())
session.IsCursorCaptureEnabled(false);
#endif
capture->item = item;
capture->device = device;
d3d_device->GetImmediateContext(&capture->context);
capture->frame_pool = frame_pool;
@ -321,11 +355,17 @@ static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
winrt::auto_revoke,
{capture, &winrt_capture::on_frame_arrived});
session.StartCapture();
try {
session.StartCapture();
capture->active = TRUE;
} catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "StartCapture (0x%08X): %ls", err.to_abi(),
err.message().c_str());
} catch (...) {
blog(LOG_ERROR, "StartCapture (0x%08X)", winrt::to_hresult());
}
}
thread_local bool initialized_tls;
extern "C" EXPORT struct winrt_capture *
winrt_capture_init(BOOL cursor, HWND window, BOOL client_area)
try {
@ -384,13 +424,8 @@ try {
/* disable cursor capture if possible since ours performs better */
const BOOL cursor_toggle_supported =
winrt_capture_cursor_toggle_supported();
#ifdef NTDDI_WIN10_VB
if (cursor_toggle_supported)
session.IsCursorCaptureEnabled(false);
#endif
if (capture_list == nullptr)
initialized_tls = true;
struct winrt_capture *capture = new winrt_capture{};
capture->window = window;
@ -402,6 +437,8 @@ try {
capture->frame_pool = frame_pool;
capture->session = session;
capture->last_size = size;
capture->closed = item.Closed(winrt::auto_revoke,
{capture, &winrt_capture::on_closed});
capture->frame_arrived = frame_pool.FrameArrived(
winrt::auto_revoke,
{capture, &winrt_capture::on_frame_arrived});
@ -409,6 +446,7 @@ try {
capture_list = capture;
session.StartCapture();
capture->active = TRUE;
gs_device_loss callbacks;
callbacks.device_loss_release = winrt_capture_device_loss_release;
@ -449,6 +487,7 @@ extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
obs_leave_graphics();
capture->frame_arrived.revoke();
capture->closed.revoke();
capture->frame_pool.Close();
capture->session.Close();
@ -476,6 +515,11 @@ static void draw_texture(struct winrt_capture *capture, gs_effect_t *effect)
gs_technique_end(tech);
}
extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
{
return capture->active;
}
extern "C" EXPORT void winrt_capture_show_cursor(struct winrt_capture *capture,
BOOL visible)
{
@ -485,28 +529,8 @@ extern "C" EXPORT void winrt_capture_show_cursor(struct winrt_capture *capture,
extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture,
gs_effect_t *effect)
{
if (capture && capture->texture_written) {
if (!initialized_tls) {
struct winrt_capture *current = capture_list;
while (current) {
current->thread_changed = true;
current = current->next;
}
initialized_tls = true;
}
if (capture->thread_changed) {
/* new graphics thread. treat like device loss. */
winrt_capture_device_loss_release(capture);
winrt_capture_device_loss_rebuild(gs_get_device_obj(),
capture);
capture->thread_changed = false;
}
if (capture->texture_written)
draw_texture(capture, effect);
}
}
extern "C" EXPORT uint32_t
@ -520,3 +544,22 @@ winrt_capture_height(const struct winrt_capture *capture)
{
return capture ? capture->texture_height : 0;
}
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;
}
}