Bug 1865991 - Allocate ID3D11Texture2D in SharedSurface_ANGLEShareHandle::Create() r=gfx-reviewers,lsalzman

In current gecko, SharedSurface_ANGLEShareHandle requests ANGLE to allocate ID3D11Texture2D. But Bug 1865984 and Bug 1861605 requests to change D3D11_TEXTURE2D_DESC::MiscFlags. To do it, the ID3D11Texture2D needs to be allocated by SharedSurface_ANGLEShareHandle.

Differential Revision: https://phabricator.services.mozilla.com/D194332
This commit is contained in:
sotaro 2023-11-23 00:28:09 +00:00
parent 720d42893d
commit 916658f69e

View File

@ -14,13 +14,37 @@
namespace mozilla {
namespace gl {
static ID3D11Device* GetD3D11DeviceOfEGLDisplay(GLContextEGL* gle) {
const auto& egl = gle->mEgl;
MOZ_ASSERT(egl);
if (!egl ||
!egl->mLib->IsExtensionSupported(gl::EGLLibExtension::EXT_device_query)) {
return nullptr;
}
// Fetch the D3D11 device.
EGLDeviceEXT eglDevice = nullptr;
egl->fQueryDisplayAttribEXT(LOCAL_EGL_DEVICE_EXT, (EGLAttrib*)&eglDevice);
MOZ_ASSERT(eglDevice);
ID3D11Device* device = nullptr;
egl->mLib->fQueryDeviceAttribEXT(eglDevice, LOCAL_EGL_D3D11_DEVICE_ANGLE,
(EGLAttrib*)&device);
if (!device) {
return nullptr;
}
return device;
}
// Returns `EGL_NO_SURFACE` (`0`) on error.
static EGLSurface CreatePBufferSurface(EglDisplay* egl, EGLConfig config,
const gfx::IntSize& size) {
const gfx::IntSize& size,
RefPtr<ID3D11Texture2D> texture2D) {
const EGLint attribs[] = {LOCAL_EGL_WIDTH, size.width, LOCAL_EGL_HEIGHT,
size.height, LOCAL_EGL_NONE};
const auto buffer = reinterpret_cast<EGLClientBuffer>(texture2D.get());
EGLSurface surface = egl->fCreatePbufferSurface(config, attribs);
EGLSurface surface = egl->fCreatePbufferFromClientBuffer(
LOCAL_EGL_D3D_TEXTURE_ANGLE, buffer, config, attribs);
if (!surface) {
EGLint err = egl->mLib->fGetError();
gfxCriticalError() << "Failed to create Pbuffer surface error: "
@ -40,38 +64,51 @@ SharedSurface_ANGLEShareHandle::Create(const SharedSurfaceDesc& desc) {
MOZ_ASSERT(egl->IsExtensionSupported(
EGLExtension::ANGLE_surface_d3d_texture_2d_share_handle));
auto* device = GetD3D11DeviceOfEGLDisplay(gle);
if (!device) {
return nullptr;
}
// Create a texture in case we need to readback.
const DXGI_FORMAT format = DXGI_FORMAT_B8G8R8A8_UNORM;
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;
RefPtr<ID3D11Texture2D> texture2D;
auto hr =
device->CreateTexture2D(&texDesc, nullptr, getter_AddRefs(texture2D));
if (FAILED(hr)) {
return nullptr;
}
HANDLE shareHandle = nullptr;
RefPtr<IDXGIResource> texDXGI;
hr = texture2D->QueryInterface(__uuidof(IDXGIResource),
getter_AddRefs(texDXGI));
if (FAILED(hr)) {
return nullptr;
}
hr = texDXGI->GetSharedHandle(&shareHandle);
if (FAILED(hr)) {
return nullptr;
}
RefPtr<IDXGIKeyedMutex> keyedMutex;
texture2D->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(keyedMutex));
if (!keyedMutex) {
return nullptr;
}
const auto& config = gle->mSurfaceConfig;
MOZ_ASSERT(config);
EGLSurface pbuffer = CreatePBufferSurface(egl.get(), config, desc.size);
EGLSurface pbuffer =
CreatePBufferSurface(egl.get(), config, desc.size, texture2D);
if (!pbuffer) return nullptr;
// Declare everything before 'goto's.
HANDLE shareHandle = nullptr;
bool ok = egl->fQuerySurfacePointerANGLE(
pbuffer, LOCAL_EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, &shareHandle);
if (!ok) {
egl->fDestroySurface(pbuffer);
return nullptr;
}
void* opaqueKeyedMutex = nullptr;
egl->fQuerySurfacePointerANGLE(pbuffer, LOCAL_EGL_DXGI_KEYED_MUTEX_ANGLE,
&opaqueKeyedMutex);
RefPtr<IDXGIKeyedMutex> keyedMutex =
static_cast<IDXGIKeyedMutex*>(opaqueKeyedMutex);
#ifdef DEBUG
if (!keyedMutex) {
std::string envStr("1");
static auto env = PR_GetEnv("MOZ_REQUIRE_KEYED_MUTEX");
if (env) {
envStr = env;
}
if (envStr != "0") {
MOZ_ASSERT(keyedMutex, "set MOZ_REQUIRE_KEYED_MUTEX=0 to allow");
}
}
#endif
return AsUnique(new SharedSurface_ANGLEShareHandle(desc, egl, pbuffer,
shareHandle, keyedMutex));
}
@ -106,25 +143,19 @@ void SharedSurface_ANGLEShareHandle::LockProdImpl() {
void SharedSurface_ANGLEShareHandle::UnlockProdImpl() {}
void SharedSurface_ANGLEShareHandle::ProducerAcquireImpl() {
if (mKeyedMutex) {
HRESULT hr = mKeyedMutex->AcquireSync(0, 10000);
if (hr == WAIT_TIMEOUT) {
MOZ_CRASH("GFX: ANGLE share handle timeout");
}
HRESULT hr = mKeyedMutex->AcquireSync(0, 10000);
if (hr == WAIT_TIMEOUT) {
MOZ_CRASH("GFX: ANGLE share handle timeout");
}
}
void SharedSurface_ANGLEShareHandle::ProducerReleaseImpl() {
const auto& gl = mDesc.gl;
if (mKeyedMutex) {
// XXX: ReleaseSync() has an implicit flush of the D3D commands
// whether we need Flush() or not depends on the ANGLE semantics.
// For now, we'll just do it
gl->fFlush();
mKeyedMutex->ReleaseSync(0);
return;
}
gl->fFinish();
// XXX: ReleaseSync() has an implicit flush of the D3D commands
// whether we need Flush() or not depends on the ANGLE semantics.
// For now, we'll just do it
gl->fFlush();
mKeyedMutex->ReleaseSync(0);
}
void SharedSurface_ANGLEShareHandle::ProducerReadAcquireImpl() {
@ -132,10 +163,7 @@ void SharedSurface_ANGLEShareHandle::ProducerReadAcquireImpl() {
}
void SharedSurface_ANGLEShareHandle::ProducerReadReleaseImpl() {
if (mKeyedMutex) {
mKeyedMutex->ReleaseSync(0);
return;
}
mKeyedMutex->ReleaseSync(0);
}
Maybe<layers::SurfaceDescriptor>
@ -144,7 +172,7 @@ SharedSurface_ANGLEShareHandle::ToSurfaceDescriptor() {
return Some(layers::SurfaceDescriptorD3D10(
(WindowsHandle)mShareHandle, /* gpuProcessTextureId */ Nothing(),
/* arrayIndex */ 0, format, mDesc.size, mDesc.colorSpace,
gfx::ColorRange::FULL, /* hasKeyedMutex */ !!mKeyedMutex));
gfx::ColorRange::FULL, /* hasKeyedMutex */ true));
}
////////////////////////////////////////////////////////////////////////////////