|
|
|
|
|
|
#include "Unity/IUnityGraphicsMetal.h" |
|
|
|
|
|
|
|
namespace uiwidgets { |
|
|
|
|
|
|
|
NSOpenGLContext* UnitySurfaceManager::gl_context_ = nullptr; |
|
|
|
NSOpenGLContext* UnitySurfaceManager::gl_resource_context_ = nullptr; |
|
|
|
|
|
|
|
UnitySurfaceManager::UnitySurfaceManager(IUnityInterfaces* unity_interfaces) |
|
|
|
{ |
|
|
|
FML_DCHECK(metal_device_ == nullptr); |
|
|
|
|
|
|
metal_device_ = metalGraphics->MetalDevice(); |
|
|
|
|
|
|
|
//create opengl context |
|
|
|
FML_DCHECK(!gl_context_); |
|
|
|
FML_DCHECK(!gl_resource_context_); |
|
|
|
|
|
|
|
NSOpenGLPixelFormatAttribute attrs[] = |
|
|
|
if (gl_context_ == nullptr && gl_resource_context_ == nullptr) { |
|
|
|
NSOpenGLPixelFormatAttribute attrs[] = |
|
|
|
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; |
|
|
|
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; |
|
|
|
//gl_context_ may be created unproperly, we can check this using the relevant gl_resource_context_ |
|
|
|
//loop until the created gl_context_ is ok, otherwise the program will crash anyway |
|
|
|
while(gl_resource_context_ == nil) { |
|
|
|
gl_context_ = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil]; |
|
|
|
gl_resource_context_ = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:gl_context_]; |
|
|
|
//gl_context_ may be created unproperly, we can check this using the relevant gl_resource_context_ |
|
|
|
//loop until the created gl_context_ is ok, otherwise the program will crash anyway |
|
|
|
while(gl_resource_context_ == nil) { |
|
|
|
gl_context_ = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil]; |
|
|
|
gl_resource_context_ = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:gl_context_]; |
|
|
|
if (gl_resource_context_ == nil) { |
|
|
|
if (gl_resource_context_ == nil) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
void UnitySurfaceManager::ReleaseNativeRenderContext() |
|
|
|
{ |
|
|
|
FML_DCHECK(gl_resource_context_); |
|
|
|
CGLReleaseContext(gl_resource_context_.CGLContextObj); |
|
|
|
gl_resource_context_ = nullptr; |
|
|
|
CGLReleaseContext(gl_context_.CGLContextObj); |
|
|
|
gl_context_ = nullptr; |
|
|
|
|
|
|
|
FML_DCHECK(metal_device_ != nullptr); |
|
|
|
metal_device_ = nullptr; |
|
|
|