yolobs-studio/libobs-d3d11/d3d11-duplicator.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

240 lines
5.5 KiB
C++
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "d3d11-subsystem.hpp"
2019-10-13 19:58:08 +00:00
#include <unordered_map>
2016-02-23 23:16:51 +00:00
static inline bool get_monitor(gs_device_t *device, int monitor_idx,
2019-09-22 21:19:10 +00:00
IDXGIOutput **dxgiOutput)
2016-02-23 23:16:51 +00:00
{
HRESULT hr;
2017-04-19 19:54:15 +00:00
hr = device->adapter->EnumOutputs(monitor_idx, dxgiOutput);
2016-02-23 23:16:51 +00:00
if (FAILED(hr)) {
if (hr == DXGI_ERROR_NOT_FOUND)
return false;
throw HRError("Failed to get output", hr);
}
return true;
}
2017-04-19 19:54:15 +00:00
void gs_duplicator::Start()
2016-02-23 23:16:51 +00:00
{
ComPtr<IDXGIOutput1> output1;
ComPtr<IDXGIOutput> output;
HRESULT hr;
2017-04-19 19:54:15 +00:00
if (!get_monitor(device, idx, output.Assign()))
2016-02-23 23:16:51 +00:00
throw "Invalid monitor index";
hr = output->QueryInterface(__uuidof(IDXGIOutput1),
2019-09-22 21:19:10 +00:00
(void **)output1.Assign());
2016-02-23 23:16:51 +00:00
if (FAILED(hr))
throw HRError("Failed to query IDXGIOutput1", hr);
hr = output1->DuplicateOutput(device->device, duplicator.Assign());
if (FAILED(hr))
throw HRError("Failed to duplicate output", hr);
}
2017-04-19 19:54:15 +00:00
gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
2019-09-22 21:19:10 +00:00
: gs_obj(device_, gs_type::gs_duplicator),
texture(nullptr),
idx(monitor_idx),
refs(1),
updated(false)
2017-04-19 19:54:15 +00:00
{
Start();
}
2016-02-23 23:16:51 +00:00
gs_duplicator::~gs_duplicator()
{
delete texture;
}
extern "C" {
EXPORT bool device_get_duplicator_monitor_info(gs_device_t *device,
2019-09-22 21:19:10 +00:00
int monitor_idx,
struct gs_monitor_info *info)
2016-02-23 23:16:51 +00:00
{
DXGI_OUTPUT_DESC desc;
HRESULT hr;
try {
ComPtr<IDXGIOutput> output;
if (!get_monitor(device, monitor_idx, output.Assign()))
return false;
hr = output->GetDesc(&desc);
if (FAILED(hr))
throw HRError("GetDesc failed", hr);
2019-09-22 21:19:10 +00:00
} catch (const HRError &error) {
blog(LOG_ERROR,
"device_get_duplicator_monitor_info: "
"%s (%08lX)",
error.str, error.hr);
2016-02-23 23:16:51 +00:00
return false;
}
switch (desc.Rotation) {
case DXGI_MODE_ROTATION_UNSPECIFIED:
case DXGI_MODE_ROTATION_IDENTITY:
info->rotation_degrees = 0;
break;
case DXGI_MODE_ROTATION_ROTATE90:
info->rotation_degrees = 90;
break;
case DXGI_MODE_ROTATION_ROTATE180:
info->rotation_degrees = 180;
break;
case DXGI_MODE_ROTATION_ROTATE270:
info->rotation_degrees = 270;
break;
}
info->x = desc.DesktopCoordinates.left;
info->y = desc.DesktopCoordinates.top;
info->cx = desc.DesktopCoordinates.right - info->x;
info->cy = desc.DesktopCoordinates.bottom - info->y;
return true;
}
2019-10-13 19:58:08 +00:00
static std::unordered_map<int, gs_duplicator *> instances;
2018-02-19 19:54:37 +00:00
void reset_duplicators(void)
{
2019-10-13 19:58:08 +00:00
for (std::pair<const int, gs_duplicator *> &pair : instances) {
2018-02-19 19:54:37 +00:00
pair.second->updated = false;
}
}
2016-02-23 23:16:51 +00:00
EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
2019-09-22 21:19:10 +00:00
int monitor_idx)
2016-02-23 23:16:51 +00:00
{
gs_duplicator *duplicator = nullptr;
2019-10-13 19:58:08 +00:00
const auto it = instances.find(monitor_idx);
2018-02-19 19:54:37 +00:00
if (it != instances.end()) {
duplicator = it->second;
duplicator->refs++;
return duplicator;
}
2016-02-23 23:16:51 +00:00
try {
duplicator = new gs_duplicator(device, monitor_idx);
2018-02-19 19:54:37 +00:00
instances[monitor_idx] = duplicator;
2016-02-23 23:16:51 +00:00
} catch (const char *error) {
2019-09-22 21:19:10 +00:00
blog(LOG_DEBUG, "device_duplicator_create: %s", error);
2016-02-23 23:16:51 +00:00
return nullptr;
2019-09-22 21:19:10 +00:00
} catch (const HRError &error) {
2016-02-23 23:16:51 +00:00
blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
2019-09-22 21:19:10 +00:00
error.str, error.hr);
2016-02-23 23:16:51 +00:00
return nullptr;
}
return duplicator;
}
EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
{
2018-02-19 19:54:37 +00:00
if (--duplicator->refs == 0) {
instances.erase(duplicator->idx);
delete duplicator;
}
2016-02-23 23:16:51 +00:00
}
static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
{
D3D11_TEXTURE2D_DESC desc;
tex->GetDesc(&desc);
2019-09-22 21:19:10 +00:00
if (!d->texture || d->texture->width != desc.Width ||
2016-02-23 23:16:51 +00:00
d->texture->height != desc.Height) {
delete d->texture;
2019-09-22 21:19:10 +00:00
d->texture = (gs_texture_2d *)gs_texture_create(
desc.Width, desc.Height,
ConvertDXGITextureFormat(desc.Format), 1, nullptr, 0);
2016-02-23 23:16:51 +00:00
}
if (!!d->texture)
2019-09-22 21:19:10 +00:00
d->device->context->CopyResource(d->texture->texture, tex);
2016-02-23 23:16:51 +00:00
}
EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
{
DXGI_OUTDUPL_FRAME_INFO info;
ComPtr<ID3D11Texture2D> tex;
ComPtr<IDXGIResource> res;
HRESULT hr;
2018-02-19 19:54:37 +00:00
if (!d->duplicator) {
return false;
}
if (d->updated) {
return true;
}
2016-02-23 23:16:51 +00:00
hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
if (hr == DXGI_ERROR_ACCESS_LOST) {
return false;
} else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
return true;
} else if (FAILED(hr)) {
2019-09-22 21:19:10 +00:00
blog(LOG_ERROR,
"gs_duplicator_update_frame: Failed to update "
"frame (%08lX)",
hr);
2016-02-23 23:16:51 +00:00
return true;
}
hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
2019-09-22 21:19:10 +00:00
(void **)tex.Assign());
2016-02-23 23:16:51 +00:00
if (FAILED(hr)) {
2019-09-22 21:19:10 +00:00
blog(LOG_ERROR,
"gs_duplicator_update_frame: Failed to query "
"ID3D11Texture2D (%08lX)",
hr);
2016-02-23 23:16:51 +00:00
d->duplicator->ReleaseFrame();
return true;
}
copy_texture(d, tex);
d->duplicator->ReleaseFrame();
2018-02-19 19:54:37 +00:00
d->updated = true;
2016-02-23 23:16:51 +00:00
return true;
}
EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
{
return duplicator->texture;
}
}