|
|
|
|
|
|
|
|
|
|
UnitySurfaceManager::~UnitySurfaceManager() { CleanUp(); } |
|
|
|
|
|
|
|
GLuint UnitySurfaceManager::CreateRenderSurface(void* native_texture_ptr) { |
|
|
|
ID3D11Texture2D* d3d11_texture = |
|
|
|
static_cast<ID3D11Texture2D*>(native_texture_ptr); |
|
|
|
void UnitySurfaceManager::CreateRenderTexture(size_t width, size_t height) { |
|
|
|
D3D11_TEXTURE2D_DESC desc = {0}; |
|
|
|
desc.Width = width; |
|
|
|
desc.Height = height; |
|
|
|
desc.MipLevels = 1; |
|
|
|
desc.ArraySize = 1; |
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
|
|
|
desc.SampleDesc.Count = 1; |
|
|
|
desc.SampleDesc.Quality = 0; |
|
|
|
desc.Usage = D3D11_USAGE_DEFAULT; |
|
|
|
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; |
|
|
|
desc.CPUAccessFlags = 0; |
|
|
|
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; |
|
|
|
|
|
|
|
HRESULT hr = d3d11_device_->CreateTexture2D(&desc, nullptr, &d3d11_texture); |
|
|
|
FML_CHECK(SUCCEEDED(hr)) << "UnitySurfaceManager: Create Native d3d11Texture2D failed"; |
|
|
|
} |
|
|
|
|
|
|
|
GLuint UnitySurfaceManager::CreateRenderSurface(size_t width, size_t height) { |
|
|
|
CreateRenderTexture(width, height); |
|
|
|
|
|
|
|
IDXGIResource* image_resource; |
|
|
|
HRESULT hr = d3d11_texture->QueryInterface( |
|
|
|
__uuidof(IDXGIResource), reinterpret_cast<void**>(&image_resource)); |
|
|
|
|
|
|
"D3D11_RESOURCE_MISC_SHARED is needed"; |
|
|
|
|
|
|
|
IDXGIResource* dxgi_resource; |
|
|
|
hr = d3d11_device_->OpenSharedResource( |
|
|
|
hr = d3d11_angle_device_->OpenSharedResource( |
|
|
|
shared_image_handle, __uuidof(ID3D11Resource), |
|
|
|
reinterpret_cast<void**>(&dxgi_resource)); |
|
|
|
FML_CHECK(SUCCEEDED(hr)) |
|
|
|
|
|
|
|
|
|
|
dxgi_resource->Release(); |
|
|
|
|
|
|
|
MakeCurrent(EGL_NO_DISPLAY); |
|
|
|
|
|
|
|
const EGLint attribs[] = {EGL_NONE}; |
|
|
|
FML_DCHECK(fbo_egl_image_ == nullptr); |
|
|
|
fbo_egl_image_ = |
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_); |
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
|
|
|
fbo_texture_, 0); |
|
|
|
|
|
|
|
FML_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) == |
|
|
|
GL_FRAMEBUFFER_COMPLETE); |
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, old_framebuffer_binding); |
|
|
|
|
|
|
|
|
|
|
void UnitySurfaceManager::DestroyRenderSurface() { |
|
|
|
FML_DCHECK(fbo_ != 0); |
|
|
|
glDeleteFramebuffers(1, &fbo_); |
|
|
|
fbo_ = 0; |
|
|
|
|
|
|
|
FML_DCHECK(fbo_texture_ != 0); |
|
|
|
glDeleteTextures(1, &fbo_texture_); |
|
|
|
fbo_texture_ = 0; |
|
|
|
|
|
|
|
FML_DCHECK(fbo_egl_image_ != nullptr); |
|
|
|
eglDestroyImageKHR(egl_display_, fbo_egl_image_); |
|
|
|
fbo_egl_image_ = nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
bool UnitySurfaceManager::ClearCurrent() { |
|
|
|
return eglMakeCurrent(egl_display_, EGL_NO_SURFACE, EGL_NO_SURFACE, |
|
|
|
EGL_NO_CONTEXT) == EGL_TRUE; |
|
|
|
|
|
|
|
|
|
|
IUnityGraphicsD3D11* d3d11 = unity_interfaces->Get<IUnityGraphicsD3D11>(); |
|
|
|
ID3D11Device* d3d11_device = d3d11->GetDevice(); |
|
|
|
|
|
|
|
d3d11_device_ = d3d11_device; |
|
|
|
|
|
|
|
IDXGIDevice* dxgi_device; |
|
|
|
HRESULT hr = d3d11_device->QueryInterface( |
|
|
|
|
|
|
EGLAttrib angle_device = 0; |
|
|
|
eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(egl_device), |
|
|
|
EGL_D3D11_DEVICE_ANGLE, &angle_device); |
|
|
|
d3d11_device_ = reinterpret_cast<ID3D11Device*>(angle_device); |
|
|
|
d3d11_angle_device_ = reinterpret_cast<ID3D11Device*>(angle_device); |
|
|
|
|
|
|
|
const EGLint configAttributes[] = {EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, |
|
|
|
EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
d3d11_device_ = nullptr; |
|
|
|
d3d11_angle_device_ = nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
bool UnitySurfaceManager::ReleaseNativeRenderTexture() { |
|
|
|
if (d3d11_texture == nullptr) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
FML_DCHECK(fbo_ != 0); |
|
|
|
glDeleteFramebuffers(1, &fbo_); |
|
|
|
fbo_ = 0; |
|
|
|
|
|
|
|
FML_DCHECK(fbo_texture_ != 0); |
|
|
|
glDeleteTextures(1, &fbo_texture_); |
|
|
|
fbo_texture_ = 0; |
|
|
|
|
|
|
|
FML_DCHECK(fbo_egl_image_ != nullptr); |
|
|
|
eglDestroyImageKHR(egl_display_, fbo_egl_image_); |
|
|
|
fbo_egl_image_ = nullptr; |
|
|
|
|
|
|
|
d3d11_texture->Release(); |
|
|
|
d3d11_texture = nullptr; |
|
|
|
|
|
|
|
return true; |
|
|
|
|