Bug 1861605 - Use IDXGIResource1::CreateSharedHandle() instead of IDXGIResource::GetSharedHandle() r=gfx-reviewers,lsalzman

gecko uses IDXGIResource::GetSharedHandle(). But it is recommend not to use anymore to retrieve the handle to a shared resource.
IDXGIResource1::CreateSharedHandle() with D3D11_RESOURCE_MISC_SHARED_NTHANDLE flag should be used instead of the GetSharedHandle().

The CreateSharedHandle() could be called only once for a shared resource. Later calls fail.

HANDLEs of ID3D11Texture2D are replaced by gfx::FileHandleWrappers.

Differential Revision: https://phabricator.services.mozilla.com/D192173
This commit is contained in:
sotaro 2023-12-19 09:23:21 +00:00
parent f2f1573ae0
commit 7d06300567
28 changed files with 348 additions and 184 deletions

View File

@ -38,7 +38,8 @@ UniquePtr<ExternalTextureD3D11> ExternalTextureD3D11::Create(
desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
desc.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
RefPtr<ID3D11Texture2D> texture;
HRESULT hr =
@ -47,41 +48,55 @@ UniquePtr<ExternalTextureD3D11> ExternalTextureD3D11::Create(
gfxCriticalNoteOnce << "CreateTexture2D failed: " << gfx::hexa(hr);
return nullptr;
}
return MakeUnique<ExternalTextureD3D11>(aWidth, aHeight, aFormat, aUsage,
texture);
}
ExternalTextureD3D11::ExternalTextureD3D11(
const uint32_t aWidth, const uint32_t aHeight,
const struct ffi::WGPUTextureFormat aFormat,
const ffi::WGPUTextureUsages aUsage, RefPtr<ID3D11Texture2D> aTexture)
: ExternalTexture(aWidth, aHeight, aFormat, aUsage), mTexture(aTexture) {
MOZ_ASSERT(mTexture);
}
ExternalTextureD3D11::~ExternalTextureD3D11() {}
void* ExternalTextureD3D11::GetExternalTextureHandle() {
RefPtr<IDXGIResource> resource;
mTexture->QueryInterface((IDXGIResource**)getter_AddRefs(resource));
RefPtr<IDXGIResource1> resource;
texture->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
if (!resource) {
gfxCriticalNoteOnce << "Failed to get IDXGIResource";
return 0;
}
HANDLE sharedHandle;
HRESULT hr = resource->GetSharedHandle(&sharedHandle);
hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
if (FAILED(hr)) {
gfxCriticalNoteOnce << "GetSharedHandle failed: " << gfx::hexa(hr);
return 0;
}
return sharedHandle;
RefPtr<gfx::FileHandleWrapper> handle =
new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
return MakeUnique<ExternalTextureD3D11>(aWidth, aHeight, aFormat, aUsage,
texture, std::move(handle));
}
ExternalTextureD3D11::ExternalTextureD3D11(
const uint32_t aWidth, const uint32_t aHeight,
const struct ffi::WGPUTextureFormat aFormat,
const ffi::WGPUTextureUsages aUsage, const RefPtr<ID3D11Texture2D> aTexture,
RefPtr<gfx::FileHandleWrapper>&& aSharedHandle)
: ExternalTexture(aWidth, aHeight, aFormat, aUsage),
mTexture(aTexture),
mSharedHandle(std::move(aSharedHandle)) {
MOZ_ASSERT(mTexture);
}
ExternalTextureD3D11::~ExternalTextureD3D11() {}
void* ExternalTextureD3D11::GetExternalTextureHandle() {
if (!mSharedHandle) {
return nullptr;
}
return mSharedHandle->GetHandle();
}
Maybe<layers::SurfaceDescriptor> ExternalTextureD3D11::ToSurfaceDescriptor() {
const auto format = gfx::SurfaceFormat::B8G8R8A8;
return Some(layers::SurfaceDescriptorD3D10(
(WindowsHandle)GetExternalTextureHandle(),
mSharedHandle,
/* gpuProcessTextureId */ Nothing(),
/* arrayIndex */ 0, format, gfx::IntSize(mWidth, mHeight),
gfx::ColorSpace2::SRGB, gfx::ColorRange::FULL,

View File

@ -6,6 +6,7 @@
#ifndef GPU_ExternalTextureD3D11_H_
#define GPU_ExternalTextureD3D11_H_
#include "mozilla/gfx/FileHandleWrapper.h"
#include "mozilla/webgpu/ExternalTexture.h"
struct ID3D11Texture2D;
@ -24,7 +25,8 @@ class ExternalTextureD3D11 final : public ExternalTexture {
ExternalTextureD3D11(const uint32_t aWidth, const uint32_t aHeight,
const struct ffi::WGPUTextureFormat aFormat,
const ffi::WGPUTextureUsages aUsage,
RefPtr<ID3D11Texture2D> aTexture);
const RefPtr<ID3D11Texture2D> aTexture,
RefPtr<gfx::FileHandleWrapper>&& aSharedHandle);
virtual ~ExternalTextureD3D11();
void* GetExternalTextureHandle() override;
@ -35,7 +37,8 @@ class ExternalTextureD3D11 final : public ExternalTexture {
const gfx::IntSize& aSize) override;
protected:
RefPtr<ID3D11Texture2D> mTexture;
const RefPtr<ID3D11Texture2D> mTexture;
const RefPtr<gfx::FileHandleWrapper> mSharedHandle;
};
} // namespace webgpu

View File

@ -50,12 +50,24 @@ extern void* wgpu_server_get_external_texture_handle(void* aParam,
WGPUTextureId aId) {
auto* parent = static_cast<WebGPUParent*>(aParam);
auto externalTexture = parent->GetExternalTexture(aId);
if (!externalTexture) {
auto texture = parent->GetExternalTexture(aId);
if (!texture) {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
return nullptr;
}
return externalTexture->GetExternalTextureHandle();
void* sharedHandle = nullptr;
#ifdef XP_WIN
sharedHandle = texture->GetExternalTextureHandle();
if (!sharedHandle) {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
gfxCriticalNoteOnce << "Failed to get shared handle";
return nullptr;
}
#else
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
#endif
return sharedHandle;
}
} // namespace ffi
@ -1462,13 +1474,6 @@ std::shared_ptr<ExternalTexture> WebGPUParent::CreateExternalTexture(
return nullptr;
}
auto* sharedHandle = texture->GetExternalTextureHandle();
if (!sharedHandle) {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
gfxCriticalNoteOnce << "Failed to get shared handle";
return nullptr;
}
std::shared_ptr<ExternalTexture> shared(texture.release());
mExternalTextures.emplace(aTextureId, shared);

View File

@ -7,6 +7,7 @@
#include "GLBlitHelper.h"
#include <d3d11.h>
#include <d3d11_1.h>
#include "GLContextEGL.h"
#include "GLLibraryEGL.h"
@ -59,12 +60,19 @@ static EGLStreamKHR StreamFromD3DTexture(EglDisplay* const egl,
static RefPtr<ID3D11Texture2D> OpenSharedTexture(ID3D11Device* const d3d,
const WindowsHandle handle) {
RefPtr<ID3D11Device1> device1;
d3d->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return nullptr;
}
RefPtr<ID3D11Texture2D> tex;
auto hr =
d3d->OpenSharedResource((HANDLE)handle, __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(tex));
auto hr = device1->OpenSharedResource1(
(HANDLE)handle, __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(tex));
if (FAILED(hr)) {
gfxCriticalError() << "Error code from OpenSharedResource: "
gfxCriticalError() << "Error code from OpenSharedResource1: "
<< gfx::hexa(hr);
return nullptr;
}
@ -231,9 +239,13 @@ bool GLBlitHelper::BlitImage(layers::D3D11YCbCrImage* const srcImage,
const auto& data = srcImage->GetData();
if (!data) return false;
const WindowsHandle handles[3] = {(WindowsHandle)data->mHandles[0],
(WindowsHandle)data->mHandles[1],
(WindowsHandle)data->mHandles[2]};
const WindowsHandle handles[3] = {
(WindowsHandle)(data->mHandles[0] ? data->mHandles[0]->GetHandle()
: nullptr),
(WindowsHandle)(data->mHandles[1] ? data->mHandles[1]->GetHandle()
: nullptr),
(WindowsHandle)(data->mHandles[2] ? data->mHandles[2]->GetHandle()
: nullptr)};
return BlitAngleYCbCr(handles, srcImage->mPictureRect, srcImage->GetYSize(),
srcImage->GetCbCrSize(), srcImage->mColorSpace,
destSize, destOrigin);
@ -247,7 +259,6 @@ bool GLBlitHelper::BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc,
const auto& d3d = GetD3D11();
if (!d3d) return false;
const auto& handle = desc.handle();
const auto& gpuProcessTextureId = desc.gpuProcessTextureId();
const auto& arrayIndex = desc.arrayIndex();
const auto& format = desc.format();
@ -275,11 +286,11 @@ bool GLBlitHelper::BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc,
tex = OpenSharedTexture(d3d, (WindowsHandle)handle.ref());
}
}
} else {
tex = OpenSharedTexture(d3d, handle);
} else if (desc.handle()) {
tex = OpenSharedTexture(d3d, (WindowsHandle)desc.handle()->GetHandle());
}
if (!tex) {
MOZ_GL_ASSERT(mGL, false); // Get a nullptr from OpenSharedResource.
MOZ_GL_ASSERT(mGL, false); // Get a nullptr from OpenSharedResource1.
return false;
}
const RefPtr<ID3D11Texture2D> texList[2] = {tex, tex};
@ -349,8 +360,12 @@ bool GLBlitHelper::BlitDescriptor(
const gfx::IntRect clipRect(0, 0, clipSize.width, clipSize.height);
const WindowsHandle handles[3] = {desc.handleY(), desc.handleCb(),
desc.handleCr()};
auto handleY = desc.handleY() ? desc.handleY()->GetHandle() : nullptr;
auto handleCb = desc.handleCb() ? desc.handleCb()->GetHandle() : nullptr;
auto handleCr = desc.handleCr() ? desc.handleCr()->GetHandle() : nullptr;
const WindowsHandle handles[3] = {
(WindowsHandle)handleY, (WindowsHandle)handleCb, (WindowsHandle)handleCr};
return BlitAngleYCbCr(handles, clipRect, ySize, uvSize, colorSpace, destSize,
destOrigin);
}

View File

@ -10,6 +10,7 @@
#include "GLLibraryEGL.h"
#include "mozilla/gfx/D3D11Checks.h"
#include "mozilla/gfx/DeviceManagerDx.h"
#include "mozilla/gfx/FileHandleWrapper.h"
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
namespace mozilla {
@ -75,7 +76,8 @@ SharedSurface_ANGLEShareHandle::Create(const SharedSurfaceDesc& desc) {
CD3D11_TEXTURE2D_DESC texDesc(
format, desc.size.width, desc.size.height, 1, 1,
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
RefPtr<ID3D11Texture2D> texture2D;
auto hr =
@ -84,18 +86,20 @@ SharedSurface_ANGLEShareHandle::Create(const SharedSurfaceDesc& desc) {
return nullptr;
}
HANDLE shareHandle = nullptr;
RefPtr<IDXGIResource> texDXGI;
hr = texture2D->QueryInterface(__uuidof(IDXGIResource),
RefPtr<IDXGIResource1> texDXGI;
hr = texture2D->QueryInterface(__uuidof(IDXGIResource1),
getter_AddRefs(texDXGI));
if (FAILED(hr)) {
return nullptr;
}
hr = texDXGI->GetSharedHandle(&shareHandle);
if (FAILED(hr)) {
return nullptr;
}
HANDLE sharedHandle = nullptr;
texDXGI->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
RefPtr<gfx::FileHandleWrapper> handle =
new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
RefPtr<IDXGIKeyedMutex> keyedMutex;
texture2D->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(keyedMutex));
@ -110,18 +114,18 @@ SharedSurface_ANGLEShareHandle::Create(const SharedSurfaceDesc& desc) {
CreatePBufferSurface(egl.get(), config, desc.size, texture2D);
if (!pbuffer) return nullptr;
return AsUnique(new SharedSurface_ANGLEShareHandle(desc, egl, pbuffer,
shareHandle, keyedMutex));
return AsUnique(new SharedSurface_ANGLEShareHandle(
desc, egl, pbuffer, std::move(handle), keyedMutex));
}
SharedSurface_ANGLEShareHandle::SharedSurface_ANGLEShareHandle(
const SharedSurfaceDesc& desc, const std::weak_ptr<EglDisplay>& egl,
EGLSurface pbuffer, HANDLE shareHandle,
EGLSurface pbuffer, RefPtr<gfx::FileHandleWrapper>&& aSharedHandle,
const RefPtr<IDXGIKeyedMutex>& keyedMutex)
: SharedSurface(desc, nullptr),
mEGL(egl),
mPBuffer(pbuffer),
mShareHandle(shareHandle),
mSharedHandle(std::move(aSharedHandle)),
mKeyedMutex(keyedMutex) {}
SharedSurface_ANGLEShareHandle::~SharedSurface_ANGLEShareHandle() {
@ -169,7 +173,7 @@ Maybe<layers::SurfaceDescriptor>
SharedSurface_ANGLEShareHandle::ToSurfaceDescriptor() {
const auto format = gfx::SurfaceFormat::B8G8R8A8;
return Some(layers::SurfaceDescriptorD3D10(
(WindowsHandle)mShareHandle, /* gpuProcessTextureId */ Nothing(),
mSharedHandle, /* gpuProcessTextureId */ Nothing(),
/* arrayIndex */ 0, format, mDesc.size, mDesc.colorSpace,
gfx::ColorRange::FULL, /* hasKeyedMutex */ true,
/* fenceInfo */ Nothing()));

View File

@ -14,6 +14,11 @@ struct IDXGIKeyedMutex;
struct ID3D11Texture2D;
namespace mozilla {
namespace gfx {
class FileHandleWrapper;
} // namespace gfx
namespace gl {
class GLContext;
@ -23,7 +28,7 @@ class SharedSurface_ANGLEShareHandle final : public SharedSurface {
public:
const std::weak_ptr<EglDisplay> mEGL;
const EGLSurface mPBuffer;
const HANDLE mShareHandle;
const RefPtr<gfx::FileHandleWrapper> mSharedHandle;
const RefPtr<IDXGIKeyedMutex> mKeyedMutex;
static UniquePtr<SharedSurface_ANGLEShareHandle> Create(
@ -32,7 +37,8 @@ class SharedSurface_ANGLEShareHandle final : public SharedSurface {
private:
SharedSurface_ANGLEShareHandle(const SharedSurfaceDesc&,
const std::weak_ptr<EglDisplay>& egl,
EGLSurface pbuffer, HANDLE shareHandle,
EGLSurface pbuffer,
RefPtr<gfx::FileHandleWrapper>&& aSharedHandle,
const RefPtr<IDXGIKeyedMutex>& keyedMutex);
public:

View File

@ -14,6 +14,7 @@
#include "WGLLibrary.h"
#include "nsPrintfCString.h"
#include "mozilla/gfx/DeviceManagerDx.h"
#include "mozilla/gfx/FileHandleWrapper.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/layers/LayersSurfaces.h"
#include "mozilla/StaticPrefs_webgl.h"
@ -324,7 +325,8 @@ UniquePtr<SharedSurface_D3D11Interop> SharedSurface_D3D11Interop::Create(
// Create a texture in case we need to readback.
const DXGI_FORMAT format = DXGI_FORMAT_B8G8R8A8_UNORM;
CD3D11_TEXTURE2D_DESC texDesc(format, size.width, size.height, 1, 1);
texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
auto hr =
d3d->CreateTexture2D(&texDesc, nullptr, getter_AddRefs(data.texD3D));
@ -333,15 +335,20 @@ UniquePtr<SharedSurface_D3D11Interop> SharedSurface_D3D11Interop::Create(
return nullptr;
}
RefPtr<IDXGIResource> texDXGI;
hr = data.texD3D->QueryInterface(__uuidof(IDXGIResource),
RefPtr<IDXGIResource1> texDXGI;
hr = data.texD3D->QueryInterface(__uuidof(IDXGIResource1),
getter_AddRefs(texDXGI));
if (FAILED(hr)) {
NS_WARNING("Failed to open texture for sharing!");
return nullptr;
}
texDXGI->GetSharedHandle(&data.dxgiHandle);
HANDLE sharedHandle = nullptr;
texDXGI->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
data.dxgiHandle = new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
////
@ -441,7 +448,7 @@ Maybe<layers::SurfaceDescriptor>
SharedSurface_D3D11Interop::ToSurfaceDescriptor() {
const auto format = gfx::SurfaceFormat::B8G8R8A8;
return Some(layers::SurfaceDescriptorD3D10(
WindowsHandle(mData.dxgiHandle), /* gpuProcessTextureId */ Nothing(),
mData.dxgiHandle, /* gpuProcessTextureId */ Nothing(),
/* arrayIndex */ 0, format, mDesc.size, mDesc.colorSpace,
gfx::ColorRange::FULL, /* hasKeyedMutex */ true,
/* fenceInfo */ Nothing()));

View File

@ -11,6 +11,11 @@
#include "SharedSurface.h"
namespace mozilla {
namespace gfx {
class FileHandleWrapper;
} // namespace gfx
namespace gl {
class DXInterop2Device;
@ -23,7 +28,7 @@ class SharedSurface_D3D11Interop final : public SharedSurface {
const RefPtr<DXInterop2Device> interop;
HANDLE lockHandle;
RefPtr<ID3D11Texture2D> texD3D;
HANDLE dxgiHandle;
RefPtr<gfx::FileHandleWrapper> dxgiHandle;
UniquePtr<Renderbuffer> interopRb;
UniquePtr<MozFramebuffer> interopFbIfNeedsIndirect;
};

View File

@ -6,6 +6,8 @@
#include "FileHandleWrapper.h"
#include "mozilla/ipc/FileDescriptor.h"
namespace mozilla::gfx {
FileHandleWrapper::FileHandleWrapper(mozilla::UniqueFileHandle&& aHandle)
@ -17,4 +19,9 @@ mozilla::detail::FileHandleType FileHandleWrapper::GetHandle() {
return mHandle.get();
}
mozilla::UniqueFileHandle FileHandleWrapper::ClonePlatformHandle() {
auto handle = ipc::FileDescriptor(GetHandle());
return handle.TakePlatformHandle();
}
} // namespace mozilla::gfx

View File

@ -35,6 +35,8 @@ class FileHandleWrapper {
mozilla::detail::FileHandleType GetHandle();
mozilla::UniqueFileHandle ClonePlatformHandle();
protected:
~FileHandleWrapper();

View File

@ -13,6 +13,7 @@
#include "LayersTypes.h" // for LayersBackend, etc
#include "nsXULAppAPI.h" // for GeckoProcessType, etc
#include "mozilla/gfx/Types.h"
#include "mozilla/layers/SyncObject.h"
#include "mozilla/EnumSet.h"
#include "mozilla/TypedEnumBits.h"
@ -147,12 +148,6 @@ enum class CompositableType : uint8_t {
COUNT
};
#ifdef XP_WIN
typedef void* SyncHandle;
#else
typedef uintptr_t SyncHandle;
#endif // XP_WIN
/**
* Sent from the compositor to the content-side LayerManager, includes
* properties of the compositor and should (in the future) include information
@ -180,7 +175,7 @@ struct TextureFactoryIdentifier {
bool aCompositorUseDComp = false, bool aUseCompositorWnd = false,
bool aSupportsTextureBlitting = false,
bool aSupportsPartialUploads = false, bool aSupportsComponentAlpha = true,
bool aSupportsD3D11NV12 = false, SyncHandle aSyncHandle = 0)
bool aSupportsD3D11NV12 = false, SyncHandle aSyncHandle = {})
: mParentBackend(aLayersBackend),
mWebRenderBackend(WebRenderBackend::HARDWARE),
mWebRenderCompositor(WebRenderCompositor::DRAW),
@ -203,7 +198,7 @@ struct TextureFactoryIdentifier {
bool aCompositorUseDComp = false, bool aUseCompositorWnd = false,
bool aSupportsTextureBlitting = false,
bool aSupportsPartialUploads = false, bool aSupportsComponentAlpha = true,
bool aSupportsD3D11NV12 = false, SyncHandle aSyncHandle = 0)
bool aSupportsD3D11NV12 = false, SyncHandle aSyncHandle = {})
: mParentBackend(LayersBackend::LAYERS_WR),
mWebRenderBackend(aWebRenderBackend),
mWebRenderCompositor(aWebRenderCompositor),

View File

@ -184,7 +184,8 @@ bool D3D11ShareHandleImage::AllocateTexture(D3D11RecycleAllocator* aAllocator,
CD3D11_TEXTURE2D_DESC newDesc(
DXGI_FORMAT_B8G8R8A8_UNORM, mSize.width, mSize.height, 1, 1,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
newDesc.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
HRESULT hr =
aDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(mTexture));
@ -346,7 +347,7 @@ RefPtr<ID3D11Texture2D> D3D11RecycleAllocator::GetStagingTextureNV12(
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
desc.MiscFlags = 0;
desc.SampleDesc.Count = 1;
HRESULT hr = mDevice->CreateTexture2D(&desc, nullptr,

View File

@ -418,7 +418,8 @@ already_AddRefed<TextureClient> DXGIYCbCrTextureAllocationHelper::Allocate(
? DXGI_FORMAT_R8_UNORM
: DXGI_FORMAT_R16_UNORM,
ySize.width, ySize.height, 1, 1);
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
RefPtr<ID3D10Multithread> mt;
HRESULT hr = mDevice->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));

View File

@ -7,6 +7,7 @@
#ifndef MOZILLA_GFX_LAYERS_SYNCOBJECT_H
#define MOZILLA_GFX_LAYERS_SYNCOBJECT_H
#include "mozilla/gfx/FileHandleWrapper.h"
#include "mozilla/RefCounted.h"
struct ID3D11Device;
@ -15,7 +16,7 @@ namespace mozilla {
namespace layers {
#ifdef XP_WIN
typedef void* SyncHandle;
typedef RefPtr<gfx::FileHandleWrapper> SyncHandle;
#else
typedef uintptr_t SyncHandle;
#endif // XP_WIN

View File

@ -99,8 +99,8 @@ Maybe<HANDLE> GpuProcessD3D11TextureMap::GetSharedHandleOfCopiedTexture(
return Nothing();
}
if (it->second.mCopiedTextureSharedHandle.isSome()) {
return it->second.mCopiedTextureSharedHandle;
if (it->second.mCopiedTextureSharedHandle) {
return Some(it->second.mCopiedTextureSharedHandle->GetHandle());
}
holder = it->second;
@ -121,7 +121,8 @@ Maybe<HANDLE> GpuProcessD3D11TextureMap::GetSharedHandleOfCopiedTexture(
CD3D11_TEXTURE2D_DESC newDesc(
DXGI_FORMAT_NV12, holder.mSize.width, holder.mSize.height, 1, 1,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
newDesc.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
RefPtr<ID3D11Texture2D> copiedTexture;
HRESULT hr =
@ -143,18 +144,23 @@ Maybe<HANDLE> GpuProcessD3D11TextureMap::GetSharedHandleOfCopiedTexture(
context->CopySubresourceRegion(copiedTexture, 0, 0, 0, 0, holder.mTexture,
holder.mArrayIndex, &srcBox);
RefPtr<IDXGIResource> resource;
copiedTexture->QueryInterface((IDXGIResource**)getter_AddRefs(resource));
RefPtr<IDXGIResource1> resource;
copiedTexture->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
if (!resource) {
return Nothing();
}
HANDLE sharedHandle;
hr = resource->GetSharedHandle(&sharedHandle);
hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
if (FAILED(hr)) {
return Nothing();
}
RefPtr<gfx::FileHandleWrapper> handle =
new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
RefPtr<ID3D11Query> query;
CD3D11_QUERY_DESC desc(D3D11_QUERY_EVENT);
hr = device->CreateQuery(&desc, getter_AddRefs(query));
@ -187,10 +193,10 @@ Maybe<HANDLE> GpuProcessD3D11TextureMap::GetSharedHandleOfCopiedTexture(
}
it->second.mCopiedTexture = copiedTexture;
it->second.mCopiedTextureSharedHandle = Some(sharedHandle);
it->second.mCopiedTextureSharedHandle = handle;
}
return Some(sharedHandle);
return Some(handle->GetHandle());
}
size_t GpuProcessD3D11TextureMap::GetWaitingTextureCount() const {

View File

@ -75,7 +75,7 @@ class GpuProcessD3D11TextureMap {
gfx::IntSize mSize;
RefPtr<IMFSampleUsageInfo> mIMFSampleUsageInfo;
RefPtr<ID3D11Texture2D> mCopiedTexture;
Maybe<HANDLE> mCopiedTextureSharedHandle;
RefPtr<gfx::FileHandleWrapper> mCopiedTextureSharedHandle;
};
struct UpdatingTextureHolder {

View File

@ -16,8 +16,10 @@
#include "mozilla/Telemetry.h"
#include "mozilla/gfx/DataSurfaceHelpers.h"
#include "mozilla/gfx/DeviceManagerDx.h"
#include "mozilla/gfx/FileHandleWrapper.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/ipc/FileDescriptor.h"
#include "mozilla/layers/CompositorBridgeChild.h"
#include "mozilla/layers/D3D11TextureIMFSampleImage.h"
#include "mozilla/layers/GpuProcessD3D11TextureMap.h"
@ -289,7 +291,9 @@ static void UnlockD3DTexture(
}
D3D11TextureData::D3D11TextureData(ID3D11Texture2D* aTexture,
uint32_t aArrayIndex, gfx::IntSize aSize,
uint32_t aArrayIndex,
RefPtr<gfx::FileHandleWrapper> aSharedHandle,
gfx::IntSize aSize,
gfx::SurfaceFormat aFormat,
TextureAllocationFlags aFlags)
: mSize(aSize),
@ -297,6 +301,7 @@ D3D11TextureData::D3D11TextureData(ID3D11Texture2D* aTexture,
mNeedsClear(aFlags & ALLOC_CLEAR_BUFFER),
mHasKeyedMutex(HasKeyedMutex(aTexture)),
mTexture(aTexture),
mSharedHandle(std::move(aSharedHandle)),
mArrayIndex(aArrayIndex),
mAllocationFlags(aFlags) {
MOZ_ASSERT(aTexture);
@ -392,22 +397,11 @@ void D3D11TextureData::SyncWithObject(RefPtr<SyncObjectClient> aSyncObject) {
bool D3D11TextureData::SerializeSpecific(
SurfaceDescriptorD3D10* const aOutDesc) {
RefPtr<IDXGIResource> resource;
GetDXGIResource((IDXGIResource**)getter_AddRefs(resource));
if (!resource) {
return false;
}
HANDLE sharedHandle = 0;
if (mGpuProcessTextureId.isNothing()) {
HRESULT hr = resource->GetSharedHandle(&sharedHandle);
if (FAILED(hr)) {
LOGD3D11("Error getting shared handle for texture.");
return false;
}
}
*aOutDesc = SurfaceDescriptorD3D10(
(WindowsHandle)sharedHandle, mGpuProcessTextureId, mArrayIndex, mFormat,
mSize, mColorSpace, mColorRange, /* hasKeyedMutex */ mHasKeyedMutex,
mSharedHandle, mGpuProcessTextureId, mArrayIndex, mFormat, mSize,
mColorSpace, mColorRange, /* hasKeyedMutex */ mHasKeyedMutex,
/* fenceInfo */ Nothing());
return true;
}
@ -435,7 +429,7 @@ already_AddRefed<TextureClient> D3D11TextureData::CreateTextureClient(
gfx::ColorRange aColorRange, KnowsCompositor* aKnowsCompositor,
RefPtr<IMFSampleUsageInfo> aUsageInfo) {
D3D11TextureData* data = new D3D11TextureData(
aTexture, aIndex, aSize, aFormat,
aTexture, aIndex, nullptr, aSize, aFormat,
TextureAllocationFlags::ALLOC_MANUAL_SYNCHRONIZATION);
data->mColorSpace = aColorSpace;
data->SetColorRange(aColorRange);
@ -500,11 +494,13 @@ D3D11TextureData* D3D11TextureData::Create(IntSize aSize, SurfaceFormat aFormat,
newDesc.Format = DXGI_FORMAT_P016;
}
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
newDesc.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
if (!NS_IsMainThread()) {
// On the main thread we use the syncobject to handle synchronization.
if (!(aFlags & ALLOC_MANUAL_SYNCHRONIZATION)) {
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
}
}
@ -575,10 +571,33 @@ D3D11TextureData* D3D11TextureData::Create(IntSize aSize, SurfaceFormat aFormat,
}
UnlockD3DTexture(texture11.get(), SerializeWithMoz2D::Yes);
}
RefPtr<IDXGIResource1> resource;
texture11->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
if (!resource) {
gfxCriticalNoteOnce << "Failed to get IDXGIResource";
return nullptr;
}
HANDLE sharedHandle;
HRESULT hr = resource->GetSharedHandle(&sharedHandle);
hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
if (FAILED(hr)) {
gfxCriticalNoteOnce << "GetSharedHandle failed: " << gfx::hexa(hr);
return nullptr;
}
texture11->SetPrivateDataInterface(
sD3D11TextureUsage,
new TextureMemoryMeasurer(newDesc.Width * newDesc.Height * 4));
return new D3D11TextureData(texture11, 0, aSize, aFormat, aFlags);
RefPtr<gfx::FileHandleWrapper> handle =
new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
return new D3D11TextureData(texture11, 0, std::move(handle), aSize, aFormat,
aFlags);
}
void D3D11TextureData::Deallocate(LayersIPCChannel* aAllocator) {
@ -592,10 +611,6 @@ TextureData* D3D11TextureData::CreateSimilar(
return D3D11TextureData::Create(mSize, mFormat, aAllocFlags);
}
void D3D11TextureData::GetDXGIResource(IDXGIResource** aOutResource) {
mTexture->QueryInterface(aOutResource);
}
TextureFlags D3D11TextureData::GetTextureFlags() const {
// With WebRender, resource open happens asynchronously on RenderThread.
// During opening the resource on host side, TextureClient needs to be alive.
@ -623,35 +638,47 @@ DXGIYCbCrTextureData* DXGIYCbCrTextureData::Create(
sD3D11TextureUsage,
new TextureMemoryMeasurer(aSizeCbCr.width * aSizeCbCr.height));
RefPtr<IDXGIResource> resource;
RefPtr<IDXGIResource1> resource;
aTextureY->QueryInterface((IDXGIResource**)getter_AddRefs(resource));
aTextureY->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
HANDLE handleY;
HRESULT hr = resource->GetSharedHandle(&handleY);
HRESULT hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&handleY);
if (FAILED(hr)) {
return nullptr;
}
const RefPtr<gfx::FileHandleWrapper> sharedHandleY =
new gfx::FileHandleWrapper(UniqueFileHandle(handleY));
aTextureCb->QueryInterface((IDXGIResource**)getter_AddRefs(resource));
aTextureCb->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
HANDLE handleCb;
hr = resource->GetSharedHandle(&handleCb);
hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&handleCb);
if (FAILED(hr)) {
return nullptr;
}
const RefPtr<gfx::FileHandleWrapper> sharedHandleCb =
new gfx::FileHandleWrapper(UniqueFileHandle(handleCb));
aTextureCr->QueryInterface((IDXGIResource**)getter_AddRefs(resource));
aTextureCr->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
HANDLE handleCr;
hr = resource->GetSharedHandle(&handleCr);
hr = resource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&handleCr);
if (FAILED(hr)) {
return nullptr;
}
const RefPtr<gfx::FileHandleWrapper> sharedHandleCr =
new gfx::FileHandleWrapper(UniqueFileHandle(handleCr));
DXGIYCbCrTextureData* texture = new DXGIYCbCrTextureData();
texture->mHandles[0] = handleY;
texture->mHandles[1] = handleCb;
texture->mHandles[2] = handleCr;
texture->mHandles[0] = sharedHandleY;
texture->mHandles[1] = sharedHandleCb;
texture->mHandles[2] = sharedHandleCr;
texture->mD3D11Textures[0] = aTextureY;
texture->mD3D11Textures[1] = aTextureCb;
texture->mD3D11Textures[2] = aTextureCr;
@ -674,10 +701,9 @@ void DXGIYCbCrTextureData::FillInfo(TextureData::Info& aInfo) const {
void DXGIYCbCrTextureData::SerializeSpecific(
SurfaceDescriptorDXGIYCbCr* const aOutDesc) {
*aOutDesc = SurfaceDescriptorDXGIYCbCr(
(WindowsHandle)mHandles[0], (WindowsHandle)mHandles[1],
(WindowsHandle)mHandles[2], mSize, mSizeY, mSizeCbCr, mColorDepth,
mYUVColorSpace, mColorRange);
*aOutDesc = SurfaceDescriptorDXGIYCbCr(mHandles[0], mHandles[1], mHandles[2],
mSize, mSizeY, mSizeCbCr, mColorDepth,
mYUVColorSpace, mColorRange);
}
bool DXGIYCbCrTextureData::Serialize(SurfaceDescriptor& aOutDescriptor) {
@ -763,7 +789,7 @@ DXGITextureHostD3D11::DXGITextureHostD3D11(
mGpuProcessTextureId(aDescriptor.gpuProcessTextureId()),
mArrayIndex(aDescriptor.arrayIndex()),
mSize(aDescriptor.size()),
mHandle((HANDLE)aDescriptor.handle()),
mHandle(aDescriptor.handle()),
mFormat(aDescriptor.format()),
mHasKeyedMutex(aDescriptor.hasKeyedMutex()),
mAcquireFenceInfo(aDescriptor.fenceInfo().isSome()
@ -794,8 +820,15 @@ bool DXGITextureHostD3D11::EnsureTexture() {
return false;
}
HRESULT hr = device->OpenSharedResource(
(HANDLE)mHandle, __uuidof(ID3D11Texture2D),
RefPtr<ID3D11Device1> device1;
device->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return false;
}
HRESULT hr = device1->OpenSharedResource1(
(HANDLE)mHandle.get(), __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(mTexture));
if (FAILED(hr)) {
MOZ_ASSERT(false, "Failed to open shared texture");
@ -1093,9 +1126,9 @@ DXGIYCbCrTextureHostD3D11::DXGIYCbCrTextureHostD3D11(
mColorDepth(aDescriptor.colorDepth()),
mYUVColorSpace(aDescriptor.yUVColorSpace()),
mColorRange(aDescriptor.colorRange()) {
mHandles[0] = (HANDLE)aDescriptor.handleY();
mHandles[1] = (HANDLE)aDescriptor.handleCb();
mHandles[2] = (HANDLE)aDescriptor.handleCr();
mHandles[0] = aDescriptor.handleY();
mHandles[1] = aDescriptor.handleCb();
mHandles[2] = aDescriptor.handleCr();
}
void DXGIYCbCrTextureHostD3D11::CreateRenderTexture(
@ -1423,7 +1456,7 @@ static inline bool ShouldDevCrashOnSyncInitFailure() {
}
SyncObjectD3D11Host::SyncObjectD3D11Host(ID3D11Device* aDevice)
: mSyncHandle(0), mDevice(aDevice) {
: mSyncHandle(nullptr), mDevice(aDevice) {
MOZ_ASSERT(aDevice);
}
@ -1431,7 +1464,8 @@ bool SyncObjectD3D11Host::Init() {
CD3D11_TEXTURE2D_DESC desc(
DXGI_FORMAT_B8G8R8A8_UNORM, 1, 1, 1, 1,
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
RefPtr<ID3D11Texture2D> texture;
HRESULT hr =
@ -1441,7 +1475,7 @@ bool SyncObjectD3D11Host::Init() {
return false;
}
hr = texture->QueryInterface((IDXGIResource**)getter_AddRefs(mSyncTexture));
hr = texture->QueryInterface((IDXGIResource1**)getter_AddRefs(mSyncTexture));
if (FAILED(hr) || !mSyncTexture) {
gfxWarning() << "Could not QI sync texture: " << gfx::hexa(hr);
return false;
@ -1454,8 +1488,11 @@ bool SyncObjectD3D11Host::Init() {
return false;
}
hr = mSyncTexture->GetSharedHandle(&mSyncHandle);
if (FAILED(hr) || !mSyncHandle) {
HANDLE sharedHandle;
hr = mSyncTexture->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
if (FAILED(hr)) {
NS_DispatchToMainThread(NS_NewRunnableFunction(
"layers::SyncObjectD3D11Renderer::Init",
[]() -> void { Accumulate(Telemetry::D3D11_SYNC_HANDLE_FAILURE, 1); }));
@ -1463,6 +1500,7 @@ bool SyncObjectD3D11Host::Init() {
<< gfx::hexa(hr);
return false;
}
mSyncHandle = new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
return true;
}
@ -1511,11 +1549,22 @@ bool SyncObjectD3D11Client::Init(ID3D11Device* aDevice, bool aFallible) {
return true;
}
HRESULT hr = aDevice->OpenSharedResource(
mSyncHandle, __uuidof(ID3D11Texture2D),
if (!mSyncHandle) {
return false;
}
RefPtr<ID3D11Device1> device1;
aDevice->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return 0;
}
HRESULT hr = device1->OpenSharedResource1(
mSyncHandle->GetHandle(), __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(mSyncTexture));
if (FAILED(hr) || !mSyncTexture) {
gfxCriticalNote << "Failed to OpenSharedResource for SyncObjectD3D11: "
gfxCriticalNote << "Failed to OpenSharedResource1 for SyncObjectD3D11: "
<< hexa(hr);
if (!aFallible && ShouldDevCrashOnSyncInitFailure()) {
gfxDevCrash(LogReason::D3D11FinalizeFrame)

View File

@ -8,7 +8,7 @@
#define MOZILLA_GFX_TEXTURED3D11_H
#include <d3d11.h>
#include <d3d11_1.h>
#include <vector>
#include "d3d9.h"
@ -25,8 +25,9 @@
namespace mozilla {
namespace gfx {
class FileHandleWrapper;
struct FenceInfo;
}
} // namespace gfx
namespace gl {
class GLBlitHelper;
@ -121,11 +122,10 @@ class D3D11TextureData final : public TextureData {
private:
D3D11TextureData(ID3D11Texture2D* aTexture, uint32_t aArrayIndex,
RefPtr<gfx::FileHandleWrapper> aSharedHandle,
gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
TextureAllocationFlags aFlags);
void GetDXGIResource(IDXGIResource** aOutResource);
bool PrepareDrawTargetInLock(OpenMode aMode);
friend class gl::GLBlitHelper;
@ -152,6 +152,7 @@ class D3D11TextureData final : public TextureData {
const bool mHasKeyedMutex;
RefPtr<ID3D11Texture2D> mTexture;
const RefPtr<gfx::FileHandleWrapper> mSharedHandle;
Maybe<GpuProcessTextureId> mGpuProcessTextureId;
uint32_t mArrayIndex = 0;
const TextureAllocationFlags mAllocationFlags;
@ -204,7 +205,7 @@ class DXGIYCbCrTextureData : public TextureData {
protected:
RefPtr<ID3D11Texture2D> mD3D11Textures[3];
HANDLE mHandles[3];
RefPtr<gfx::FileHandleWrapper> mHandles[3];
gfx::IntSize mSize;
gfx::IntSize mSizeY;
gfx::IntSize mSizeCbCr;
@ -389,7 +390,7 @@ class DXGITextureHostD3D11 : public TextureHost {
uint32_t mArrayIndex = 0;
RefPtr<DataTextureSourceD3D11> mTextureSource;
gfx::IntSize mSize;
HANDLE mHandle;
const RefPtr<gfx::FileHandleWrapper> mHandle;
gfx::SurfaceFormat mFormat;
bool mHasKeyedMutex;
gfx::FenceInfo mAcquireFenceInfo;
@ -449,7 +450,9 @@ class DXGIYCbCrTextureHostD3D11 : public TextureHost {
gfx::IntSize mSize;
gfx::IntSize mSizeY;
gfx::IntSize mSizeCbCr;
HANDLE mHandles[3];
// Handles will be closed automatically when `UniqueFileHandle` gets
// destroyed.
RefPtr<gfx::FileHandleWrapper> mHandles[3];
bool mIsLocked;
gfx::ColorDepth mColorDepth;
gfx::YUVColorSpace mYUVColorSpace;
@ -495,7 +498,7 @@ class SyncObjectD3D11Host : public SyncObjectHost {
SyncHandle mSyncHandle;
RefPtr<ID3D11Device> mDevice;
RefPtr<IDXGIResource> mSyncTexture;
RefPtr<IDXGIResource1> mSyncTexture;
RefPtr<IDXGIKeyedMutex> mKeyedMutex;
};
@ -522,7 +525,7 @@ class SyncObjectD3D11Client : public SyncObjectClient {
std::vector<ID3D11Texture2D*> mSyncedTextures;
private:
const SyncHandle mSyncHandle;
SyncHandle mSyncHandle;
RefPtr<IDXGIKeyedMutex> mKeyedMutex;
const RefPtr<ID3D11Device> mDevice;
};

View File

@ -245,7 +245,7 @@ RefPtr<TextureHost> TextureHostWrapperD3D11::CreateFromBufferTexture(
auto colorSpace = ToColorSpace2(bufferTexture->GetYUVColorSpace());
auto descD3D10 = SurfaceDescriptorD3D10(
WindowsHandle(nullptr), Some(id),
nullptr, Some(id),
/* arrayIndex */ 0, gfx::SurfaceFormat::NV12, size, colorSpace,
colorRange, /* hasKeyedMutex */ false, /* fenceInfo */ Nothing());

View File

@ -19,6 +19,7 @@
#include "mozilla/ScrollSnapInfo.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ipc/ByteBuf.h"
#include "mozilla/ipc/ProtocolMessageUtils.h"
#include "mozilla/layers/APZInputBridge.h"
#include "mozilla/layers/AsyncDragMetrics.h"
#include "mozilla/layers/CompositorOptions.h"

View File

@ -22,6 +22,7 @@ using mozilla::gfx::IntRect from "mozilla/gfx/Rect.h";
using mozilla::gfx::IntSize from "mozilla/gfx/Point.h";
using mozilla::gfx::Matrix4x4 from "mozilla/gfx/Matrix.h";
using mozilla::gfx::FenceInfo from "mozilla/gfx/FileHandleWrapper.h";
[RefCounted] using mozilla::gfx::FileHandleWrapper from "mozilla/gfx/FileHandleWrapper.h";
[MoveOnly] using mozilla::ipc::SharedMemoryBasic::Handle from "mozilla/ipc/SharedMemoryBasic.h";
using gfxImageFormat from "gfxTypes.h";
using mozilla::layers::MaybeVideoBridgeSource from "mozilla/layers/VideoBridgeUtils.h";
@ -35,7 +36,7 @@ namespace mozilla {
namespace layers {
[Comparable] struct SurfaceDescriptorD3D10 {
WindowsHandle handle;
nullable FileHandleWrapper handle;
GpuProcessTextureId? gpuProcessTextureId;
uint32_t arrayIndex;
SurfaceFormat format;
@ -47,9 +48,9 @@ namespace layers {
};
[Comparable] struct SurfaceDescriptorDXGIYCbCr {
WindowsHandle handleY;
WindowsHandle handleCb;
WindowsHandle handleCr;
nullable FileHandleWrapper handleY;
nullable FileHandleWrapper handleCb;
nullable FileHandleWrapper handleCr;
IntSize size;
IntSize sizeY;
IntSize sizeCbCr;

View File

@ -21,6 +21,7 @@
#include <dxgi1_2.h>
#include <d3d10_1.h>
#include <d3d11.h>
#include <d3d11_1.h>
namespace mozilla {
namespace gfx {
@ -56,7 +57,8 @@ bool D3D11Checks::DoesRenderTargetViewNeedRecreating(ID3D11Device* aDevice) {
offscreenTextureDesc.BindFlags =
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
offscreenTextureDesc.CPUAccessFlags = 0;
offscreenTextureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
offscreenTextureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
HRESULT hr = aDevice->CreateTexture2D(&offscreenTextureDesc, NULL,
getter_AddRefs(offscreenTexture));
@ -242,7 +244,8 @@ static bool DoesTextureSharingWorkInternal(ID3D11Device* device,
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
desc.BindFlags = bindflags;
uint32_t color[texture_size * texture_size];
@ -282,23 +285,35 @@ static bool DoesTextureSharingWorkInternal(ID3D11Device* device,
stride * texture_size);
}
HANDLE shareHandle;
RefPtr<IDXGIResource> otherResource;
if (FAILED(texture->QueryInterface(__uuidof(IDXGIResource),
RefPtr<IDXGIResource1> otherResource;
if (FAILED(texture->QueryInterface(__uuidof(IDXGIResource1),
getter_AddRefs(otherResource)))) {
gfxCriticalError() << "DoesD3D11TextureSharingWork_GetResourceFailure";
return false;
}
if (FAILED(otherResource->GetSharedHandle(&shareHandle))) {
HANDLE sharedHandle;
if (FAILED(otherResource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
nullptr, &sharedHandle))) {
gfxCriticalError() << "DoesD3D11TextureSharingWork_GetSharedTextureFailure";
return false;
}
auto handle = ipc::FileDescriptor(UniqueFileHandle(sharedHandle));
RefPtr<ID3D11Device1> device1;
device->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return false;
}
RefPtr<ID3D11Resource> sharedResource;
RefPtr<ID3D11Texture2D> sharedTexture;
if (FAILED(device->OpenSharedResource(shareHandle, __uuidof(ID3D11Resource),
getter_AddRefs(sharedResource)))) {
auto raw = handle.TakePlatformHandle();
if (FAILED(device1->OpenSharedResource1(raw.get(), __uuidof(ID3D11Resource),
getter_AddRefs(sharedResource)))) {
gfxCriticalError(CriticalLog::DefaultOptions(false))
<< "OpenSharedResource failed for format " << format;
return false;

View File

@ -27,7 +27,8 @@ bool FxROutputHandler::TryInitialize(IDXGISwapChain* aSwapChain,
// Create shareable texture, which will be copied to
D3D11_TEXTURE2D_DESC descOrig = {0};
texOrig->GetDesc(&descOrig);
descOrig.MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
descOrig.MiscFlags |=
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
hr = aDevice->CreateTexture2D(&descOrig, nullptr,
mTexCopy.StartAssignment());
if (hr != S_OK) {
@ -37,14 +38,16 @@ bool FxROutputHandler::TryInitialize(IDXGISwapChain* aSwapChain,
// Now, share the texture to a handle that can be marshaled to another
// process
HANDLE hCopy = nullptr;
RefPtr<IDXGIResource> texResource;
hr = mTexCopy->QueryInterface(IID_IDXGIResource,
RefPtr<IDXGIResource1> texResource;
hr = mTexCopy->QueryInterface(IID_IDXGIResource1,
getter_AddRefs(texResource));
if (hr != S_OK) {
return false;
}
hr = texResource->GetSharedHandle(&hCopy);
hr = texResource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
nullptr, &hCopy);
if (hr != S_OK) {
return false;
}

View File

@ -1389,9 +1389,10 @@ bool VRManager::SubmitFrame(const layers::SurfaceDescriptor& aTexture,
case SurfaceDescriptor::TSurfaceDescriptorD3D10: {
const SurfaceDescriptorD3D10& surf =
aTexture.get_SurfaceDescriptorD3D10();
auto handle = surf.handle()->ClonePlatformHandle();
layer.textureType =
VRLayerTextureType::LayerTextureType_D3D10SurfaceDescriptor;
layer.textureHandle = (void*)surf.handle();
layer.textureHandle = (void*)handle.release();
layer.textureSize.width = surf.size().width;
layer.textureSize.height = surf.size().height;
} break;

View File

@ -8,6 +8,8 @@
#include "moz_external_vr.h"
#include "mozilla/ipc/FileDescriptor.h"
#if defined(XP_WIN)
# include <d3d11.h>
#endif // defined(XP_WIN)
@ -104,8 +106,10 @@ bool VRSession::SubmitFrame(
if (aLayer.textureType ==
VRLayerTextureType::LayerTextureType_D3D10SurfaceDescriptor) {
ID3D11Texture2D* dxTexture = nullptr;
HRESULT hr = mDevice->OpenSharedResource((HANDLE)aLayer.textureHandle,
IID_PPV_ARGS(&dxTexture));
mozilla::ipc::FileDescriptor::UniquePlatformHandle handle(
aLayer.textureHandle);
HRESULT hr =
mDevice->OpenSharedResource1(handle.get(), IID_PPV_ARGS(&dxTexture));
if (SUCCEEDED(hr) && dxTexture != nullptr) {
// Similar to LockD3DTexture in TextureD3D11.cpp
IDXGIKeyedMutex* mutex = nullptr;

View File

@ -24,7 +24,8 @@ namespace mozilla {
namespace wr {
RenderDXGITextureHost::RenderDXGITextureHost(
HANDLE aHandle, Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId,
RefPtr<gfx::FileHandleWrapper> aHandle,
Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId,
uint32_t aArrayIndex, gfx::SurfaceFormat aFormat,
gfx::ColorSpace2 aColorSpace, gfx::ColorRange aColorRange,
gfx::IntSize aSize, bool aHasKeyedMutex, gfx::FenceInfo& aAcquireFenceInfo)
@ -214,9 +215,16 @@ bool RenderDXGITextureHost::EnsureD3D11Texture2D(ID3D11Device* aDevice) {
return false;
}
RefPtr<ID3D11Device1> device1;
aDevice->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return 0;
}
// Get the D3D11 texture from shared handle.
HRESULT hr = aDevice->OpenSharedResource(
(HANDLE)mHandle, __uuidof(ID3D11Texture2D),
HRESULT hr = device1->OpenSharedResource1(
(HANDLE)mHandle->GetHandle(), __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(mTexture));
if (FAILED(hr)) {
MOZ_ASSERT(false,
@ -463,9 +471,9 @@ bool RenderDXGITextureHost::SyncObjectNeeded() {
}
RenderDXGIYCbCrTextureHost::RenderDXGIYCbCrTextureHost(
HANDLE (&aHandles)[3], gfx::YUVColorSpace aYUVColorSpace,
gfx::ColorDepth aColorDepth, gfx::ColorRange aColorRange,
gfx::IntSize aSizeY, gfx::IntSize aSizeCbCr)
RefPtr<gfx::FileHandleWrapper> (&aHandles)[3],
gfx::YUVColorSpace aYUVColorSpace, gfx::ColorDepth aColorDepth,
gfx::ColorRange aColorRange, gfx::IntSize aSizeY, gfx::IntSize aSizeCbCr)
: mHandles{aHandles[0], aHandles[1], aHandles[2]},
mSurfaces{0},
mStreams{0},
@ -586,6 +594,13 @@ bool RenderDXGIYCbCrTextureHost::EnsureLockable() {
}
bool RenderDXGIYCbCrTextureHost::EnsureD3D11Texture2D(ID3D11Device* aDevice) {
RefPtr<ID3D11Device1> device1;
aDevice->QueryInterface((ID3D11Device1**)getter_AddRefs(device1));
if (!device1) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device1";
return false;
}
if (mTextures[0]) {
RefPtr<ID3D11Device> device;
mTextures[0]->GetDevice(getter_AddRefs(device));
@ -601,8 +616,8 @@ bool RenderDXGIYCbCrTextureHost::EnsureD3D11Texture2D(ID3D11Device* aDevice) {
for (int i = 0; i < 3; ++i) {
// Get the R8 D3D11 texture from shared handle.
HRESULT hr = aDevice->OpenSharedResource(
(HANDLE)mHandles[i], __uuidof(ID3D11Texture2D),
HRESULT hr = device1->OpenSharedResource1(
(HANDLE)mHandles[i]->GetHandle(), __uuidof(ID3D11Texture2D),
(void**)(ID3D11Texture2D**)getter_AddRefs(mTextures[i]));
if (FAILED(hr)) {
NS_WARNING(

View File

@ -27,7 +27,8 @@ namespace wr {
class RenderDXGITextureHost final : public RenderTextureHostSWGL {
public:
RenderDXGITextureHost(
HANDLE aHandle, Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId,
RefPtr<gfx::FileHandleWrapper> aHandle,
Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId,
uint32_t aArrayIndex, gfx::SurfaceFormat aFormat, gfx::ColorSpace2,
gfx::ColorRange aColorRange, gfx::IntSize aSize, bool aHasKeyedMutex,
gfx::FenceInfo& aAcquireFenceInfo);
@ -99,7 +100,7 @@ class RenderDXGITextureHost final : public RenderTextureHostSWGL {
RefPtr<gl::GLContext> mGL;
HANDLE mHandle;
RefPtr<gfx::FileHandleWrapper> mHandle;
Maybe<layers::GpuProcessTextureId> mGpuProcessTextureId;
RefPtr<ID3D11Texture2D> mTexture;
uint32_t mArrayIndex = 0;
@ -135,12 +136,10 @@ class RenderDXGITextureHost final : public RenderTextureHostSWGL {
class RenderDXGIYCbCrTextureHost final : public RenderTextureHostSWGL {
public:
explicit RenderDXGIYCbCrTextureHost(HANDLE (&aHandles)[3],
gfx::YUVColorSpace aYUVColorSpace,
gfx::ColorDepth aColorDepth,
gfx::ColorRange aColorRange,
gfx::IntSize aSizeY,
gfx::IntSize aSizeCbCr);
explicit RenderDXGIYCbCrTextureHost(
RefPtr<gfx::FileHandleWrapper> (&aHandles)[3],
gfx::YUVColorSpace aYUVColorSpace, gfx::ColorDepth aColorDepth,
gfx::ColorRange aColorRange, gfx::IntSize aSizeY, gfx::IntSize aSizeCbCr);
RenderDXGIYCbCrTextureHost* AsRenderDXGIYCbCrTextureHost() override {
return this;
@ -198,7 +197,7 @@ class RenderDXGIYCbCrTextureHost final : public RenderTextureHostSWGL {
RefPtr<gl::GLContext> mGL;
HANDLE mHandles[3];
RefPtr<gfx::FileHandleWrapper> mHandles[3];
RefPtr<ID3D11Texture2D> mTextures[3];
RefPtr<IDXGIKeyedMutex> mKeyedMutexs[3];

View File

@ -339,7 +339,7 @@ already_AddRefed<WebRenderAPI> WebRenderAPI::Create(
bool useDComp = false;
bool useTripleBuffering = false;
bool supportsExternalBufferTextures = false;
layers::SyncHandle syncHandle = 0;
layers::SyncHandle syncHandle = {};
// Dispatch a synchronous task because the DocumentHandle object needs to be
// created on the render thread. If need be we could delay waiting on this