2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2015-03-31 08:21:15 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WMF.h"
|
|
|
|
#include "D3D11ShareHandleImage.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#include "mozilla/layers/TextureClient.h"
|
|
|
|
#include "mozilla/layers/TextureD3D11.h"
|
|
|
|
#include "mozilla/layers/CompositableClient.h"
|
|
|
|
#include "mozilla/layers/CompositableForwarder.h"
|
2017-03-13 15:29:22 +03:00
|
|
|
#include "mozilla/gfx/DeviceManagerDx.h"
|
2015-03-31 08:21:15 +03:00
|
|
|
#include "d3d11.h"
|
2017-03-07 23:55:20 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "DXVA2Manager.h"
|
|
|
|
#include <memory>
|
2015-03-31 08:21:15 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2017-03-13 15:29:22 +03:00
|
|
|
using namespace gfx;
|
|
|
|
|
2015-11-17 11:09:00 +03:00
|
|
|
D3D11ShareHandleImage::D3D11ShareHandleImage(const gfx::IntSize& aSize,
|
2018-10-08 14:26:00 +03:00
|
|
|
const gfx::IntRect& aRect,
|
|
|
|
const GUID& aSourceFormat)
|
|
|
|
: Image(nullptr, ImageFormat::D3D11_SHARE_HANDLE_TEXTURE)
|
|
|
|
, mSize(aSize)
|
|
|
|
, mPictureRect(aRect)
|
|
|
|
, mSourceFormat(aSourceFormat)
|
|
|
|
|
2015-03-31 08:21:15 +03:00
|
|
|
{
|
2015-11-17 11:09:00 +03:00
|
|
|
}
|
2015-03-31 08:21:15 +03:00
|
|
|
|
2015-11-17 11:09:00 +03:00
|
|
|
bool
|
2018-10-04 12:39:50 +03:00
|
|
|
D3D11ShareHandleImage::AllocateTexture(D3D11RecycleAllocator* aAllocator,
|
2018-10-08 14:26:00 +03:00
|
|
|
ID3D11Device* aDevice)
|
2015-11-17 11:09:00 +03:00
|
|
|
{
|
2016-09-21 12:24:43 +03:00
|
|
|
if (aAllocator) {
|
2018-10-08 14:26:00 +03:00
|
|
|
if (mSourceFormat == MFVideoFormat_NV12 &&
|
|
|
|
gfxPrefs::PDMWMFUseNV12Format() &&
|
2017-09-10 18:54:31 +03:00
|
|
|
gfx::DeviceManagerDx::Get()->CanUseNV12()) {
|
2018-10-04 12:39:50 +03:00
|
|
|
mTextureClient =
|
|
|
|
aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::NV12, mSize);
|
2018-10-11 01:21:53 +03:00
|
|
|
} else if (((mSourceFormat == MFVideoFormat_P010 &&
|
|
|
|
gfx::DeviceManagerDx::Get()->CanUseP010()) ||
|
|
|
|
(mSourceFormat == MFVideoFormat_P016 &&
|
|
|
|
gfx::DeviceManagerDx::Get()->CanUseP016())) &&
|
|
|
|
gfxPrefs::PDMWMFUseNV12Format()) {
|
|
|
|
mTextureClient = aAllocator->CreateOrRecycleClient(
|
|
|
|
mSourceFormat == MFVideoFormat_P010 ? gfx::SurfaceFormat::P010
|
|
|
|
: gfx::SurfaceFormat::P016,
|
|
|
|
mSize);
|
2017-03-07 23:55:20 +03:00
|
|
|
} else {
|
2018-10-04 12:39:50 +03:00
|
|
|
mTextureClient =
|
|
|
|
aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::B8G8R8A8, mSize);
|
2017-03-07 23:55:20 +03:00
|
|
|
}
|
2016-09-21 12:24:43 +03:00
|
|
|
if (mTextureClient) {
|
2018-10-11 01:21:53 +03:00
|
|
|
mTexture =
|
|
|
|
static_cast<D3D11TextureData*>(mTextureClient->GetInternalData())
|
|
|
|
->GetD3D11Texture();
|
2016-09-21 12:24:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aDevice);
|
|
|
|
CD3D11_TEXTURE2D_DESC newDesc(DXGI_FORMAT_B8G8R8A8_UNORM,
|
2018-10-11 01:21:53 +03:00
|
|
|
mSize.width,
|
|
|
|
mSize.height,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
D3D11_BIND_RENDER_TARGET |
|
|
|
|
D3D11_BIND_SHADER_RESOURCE);
|
2016-09-21 12:24:43 +03:00
|
|
|
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
|
|
|
|
|
2018-10-11 01:21:53 +03:00
|
|
|
HRESULT hr =
|
|
|
|
aDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(mTexture));
|
2016-09-21 12:24:43 +03:00
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
2015-03-31 08:21:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::IntSize
|
2017-11-30 21:03:02 +03:00
|
|
|
D3D11ShareHandleImage::GetSize() const
|
2015-03-31 08:21:15 +03:00
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureClient*
|
2016-09-27 06:22:20 +03:00
|
|
|
D3D11ShareHandleImage::GetTextureClient(KnowsCompositor* aForwarder)
|
2015-03-31 08:21:15 +03:00
|
|
|
{
|
|
|
|
return mTextureClient;
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface>
|
2015-03-31 08:21:15 +03:00
|
|
|
D3D11ShareHandleImage::GetAsSourceSurface()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Texture2D> texture = GetTexture();
|
2015-08-19 04:04:14 +03:00
|
|
|
if (!texture) {
|
2017-02-21 08:16:31 +03:00
|
|
|
gfxWarning() << "Cannot readback from shared texture because no texture is available.";
|
2015-03-31 08:21:15 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Device> device;
|
2015-10-18 07:40:10 +03:00
|
|
|
texture->GetDevice(getter_AddRefs(device));
|
2015-03-31 08:21:15 +03:00
|
|
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
2015-08-19 04:04:14 +03:00
|
|
|
texture->GetDesc(&desc);
|
2015-03-31 08:21:15 +03:00
|
|
|
|
2017-03-07 23:55:20 +03:00
|
|
|
HRESULT hr;
|
|
|
|
|
2018-10-04 12:39:50 +03:00
|
|
|
if (desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) {
|
2017-03-07 23:55:20 +03:00
|
|
|
nsAutoCString error;
|
|
|
|
std::unique_ptr<DXVA2Manager> manager(DXVA2Manager::CreateD3D11DXVA(nullptr, error, device));
|
|
|
|
|
|
|
|
if (!manager) {
|
|
|
|
gfxWarning() << "Failed to create DXVA2 manager!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ID3D11Texture2D> outTexture;
|
|
|
|
|
2018-10-08 14:26:00 +03:00
|
|
|
hr = manager->CopyToBGRATexture(
|
|
|
|
texture, mSourceFormat, getter_AddRefs(outTexture));
|
2017-03-07 23:55:20 +03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2018-10-08 14:26:00 +03:00
|
|
|
gfxWarning() << "Failed to copy to BGRA texture.";
|
2017-03-07 23:55:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = outTexture;
|
|
|
|
texture->GetDesc(&desc);
|
|
|
|
}
|
|
|
|
|
2015-03-31 08:21:15 +03:00
|
|
|
CD3D11_TEXTURE2D_DESC softDesc(desc.Format, desc.Width, desc.Height);
|
|
|
|
softDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
|
|
|
softDesc.BindFlags = 0;
|
|
|
|
softDesc.MiscFlags = 0;
|
|
|
|
softDesc.MipLevels = 1;
|
|
|
|
softDesc.Usage = D3D11_USAGE_STAGING;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Texture2D> softTexture;
|
2017-03-07 23:55:20 +03:00
|
|
|
hr = device->CreateTexture2D(&softDesc,
|
2015-03-31 08:21:15 +03:00
|
|
|
NULL,
|
2015-10-18 07:40:10 +03:00
|
|
|
static_cast<ID3D11Texture2D**>(getter_AddRefs(softTexture)));
|
2015-03-31 08:21:15 +03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to create 2D staging texture.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11DeviceContext> context;
|
2015-10-18 07:40:10 +03:00
|
|
|
device->GetImmediateContext(getter_AddRefs(context));
|
2015-03-31 08:21:15 +03:00
|
|
|
if (!context) {
|
2017-10-10 13:09:37 +03:00
|
|
|
gfxCriticalError() << "Failed to get immediate context.";
|
2015-03-31 08:21:15 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:16:31 +03:00
|
|
|
RefPtr<IDXGIKeyedMutex> mutex;
|
|
|
|
hr = texture->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(mutex));
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr) && mutex) {
|
|
|
|
hr = mutex->AcquireSync(0, 2000);
|
|
|
|
if (hr != S_OK) {
|
|
|
|
NS_WARNING("Acquire sync didn't manage to return within 2 seconds.");
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 04:04:14 +03:00
|
|
|
context->CopyResource(softTexture, texture);
|
2017-02-21 08:16:31 +03:00
|
|
|
if (SUCCEEDED(hr) && mutex) {
|
|
|
|
mutex->ReleaseSync(0);
|
|
|
|
}
|
2015-03-31 08:21:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> surface =
|
2016-07-26 02:36:36 +03:00
|
|
|
gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8A8);
|
2015-03-31 08:21:15 +03:00
|
|
|
if (NS_WARN_IF(!surface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::DataSourceSurface::MappedSurface mappedSurface;
|
|
|
|
if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
hr = context->Map(softTexture, 0, D3D11_MAP_READ, 0, &map);
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
surface->Unmap();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < mSize.height; y++) {
|
|
|
|
memcpy(mappedSurface.mData + mappedSurface.mStride * y,
|
|
|
|
(unsigned char*)(map.pData) + map.RowPitch * y,
|
|
|
|
mSize.width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->Unmap(softTexture, 0);
|
|
|
|
surface->Unmap();
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11Texture2D*
|
2018-10-11 01:21:53 +03:00
|
|
|
D3D11ShareHandleImage::GetTexture() const
|
|
|
|
{
|
2016-09-21 12:24:43 +03:00
|
|
|
return mTexture;
|
2015-03-31 08:21:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-19 04:04:46 +03:00
|
|
|
already_AddRefed<TextureClient>
|
|
|
|
D3D11RecycleAllocator::Allocate(gfx::SurfaceFormat aFormat,
|
|
|
|
gfx::IntSize aSize,
|
|
|
|
BackendSelector aSelector,
|
|
|
|
TextureFlags aTextureFlags,
|
|
|
|
TextureAllocationFlags aAllocFlags)
|
|
|
|
{
|
2015-10-15 18:53:37 +03:00
|
|
|
return CreateD3D11TextureClientWithDevice(aSize, aFormat,
|
|
|
|
aTextureFlags, aAllocFlags,
|
2016-09-27 06:22:20 +03:00
|
|
|
mDevice, mSurfaceAllocator->GetTextureForwarder());
|
2015-08-19 04:04:46 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:53:37 +03:00
|
|
|
already_AddRefed<TextureClient>
|
2015-08-19 04:04:46 +03:00
|
|
|
D3D11RecycleAllocator::CreateOrRecycleClient(gfx::SurfaceFormat aFormat,
|
|
|
|
const gfx::IntSize& aSize)
|
|
|
|
{
|
2018-06-13 16:45:52 +03:00
|
|
|
// When CompositorDevice or ContentDevice is updated,
|
|
|
|
// we could not reuse old D3D11Textures. It could cause video flickering.
|
|
|
|
RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetImageDevice();
|
|
|
|
if (!!mImageDevice && mImageDevice != device) {
|
|
|
|
ShrinkToMinimumSize();
|
|
|
|
}
|
|
|
|
mImageDevice = device;
|
|
|
|
|
2017-03-07 23:55:20 +03:00
|
|
|
TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT;
|
2017-03-13 15:29:22 +03:00
|
|
|
if (gfxPrefs::PDMWMFUseSyncTexture() || mDevice == DeviceManagerDx::Get()->GetCompositorDevice()) {
|
|
|
|
// If our device is the compositor device, we don't need any synchronization in practice.
|
2017-03-07 23:55:20 +03:00
|
|
|
allocFlags = TextureAllocationFlags::ALLOC_MANUAL_SYNCHRONIZATION;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TextureClient> textureClient =
|
2015-08-19 04:04:46 +03:00
|
|
|
CreateOrRecycle(aFormat,
|
|
|
|
aSize,
|
|
|
|
BackendSelector::Content,
|
2016-03-21 08:49:59 +03:00
|
|
|
layers::TextureFlags::DEFAULT,
|
2017-03-07 23:55:20 +03:00
|
|
|
allocFlags);
|
2015-10-15 18:53:37 +03:00
|
|
|
return textureClient.forget();
|
2015-08-19 04:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|