2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-12-17 21:01:52 +00:00
|
|
|
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLInterface/AGL.h"
|
2015-09-18 17:43:34 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2012-12-17 21:01:52 +00:00
|
|
|
|
2017-04-16 03:43:22 +00:00
|
|
|
static bool UpdateCachedDimensions(NSView* view, u32* width, u32* height)
|
|
|
|
{
|
|
|
|
NSWindow* window = [view window];
|
|
|
|
NSSize size = [view frame].size;
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
const CGFloat scale = [window backingScaleFactor];
|
|
|
|
u32 new_width = static_cast<u32>(size.width * scale);
|
|
|
|
u32 new_height = static_cast<u32>(size.height * scale);
|
2017-04-16 03:43:22 +00:00
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
if (*width == new_width && *height == new_height)
|
2017-04-16 03:43:22 +00:00
|
|
|
return false;
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
*width = new_width;
|
|
|
|
*height = new_height;
|
2017-04-16 03:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AttachContextToView(NSOpenGLContext* context, NSView* view, u32* width, u32* height)
|
|
|
|
{
|
|
|
|
// Enable high-resolution display support.
|
|
|
|
[view setWantsBestResolutionOpenGLSurface:YES];
|
|
|
|
|
|
|
|
NSWindow* window = [view window];
|
|
|
|
if (window == nil)
|
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "failed to get NSWindow");
|
2017-04-16 03:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)UpdateCachedDimensions(view, width, height);
|
|
|
|
|
2020-01-02 02:47:55 +00:00
|
|
|
// the following calls can crash if not called from the main thread on macOS 10.15
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
[window makeFirstResponder:view];
|
|
|
|
[context setView:view];
|
|
|
|
[window makeKeyAndOrderFront:nil];
|
|
|
|
});
|
2017-04-16 03:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-03 13:03:33 +00:00
|
|
|
GLContextAGL::~GLContextAGL()
|
|
|
|
{
|
|
|
|
if ([NSOpenGLContext currentContext] == m_context)
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
|
|
|
|
|
|
|
if (m_context)
|
|
|
|
{
|
|
|
|
[m_context clearDrawable];
|
|
|
|
[m_context release];
|
|
|
|
}
|
|
|
|
if (m_pixel_format)
|
|
|
|
[m_pixel_format release];
|
|
|
|
}
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
bool GLContextAGL::IsHeadless() const
|
|
|
|
{
|
|
|
|
return !m_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLContextAGL::Swap()
|
2012-12-17 21:01:52 +00:00
|
|
|
{
|
2017-04-16 03:49:00 +00:00
|
|
|
[m_context flushBuffer];
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create rendering window.
|
|
|
|
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
|
2019-04-10 14:40:19 +00:00
|
|
|
bool GLContextAGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool core)
|
2012-12-17 21:01:52 +00:00
|
|
|
{
|
2017-06-26 10:32:09 +00:00
|
|
|
NSOpenGLPixelFormatAttribute attr[] = {
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
NSOpenGLPFAOpenGLProfile,
|
|
|
|
core ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy,
|
|
|
|
NSOpenGLPFAAccelerated,
|
|
|
|
stereo ? NSOpenGLPFAStereo : static_cast<NSOpenGLPixelFormatAttribute>(0),
|
|
|
|
0};
|
2017-07-20 01:38:25 +00:00
|
|
|
m_pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
|
|
|
|
if (m_pixel_format == nil)
|
2014-08-30 21:01:19 +00:00
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "failed to create pixel format");
|
2013-08-29 05:33:24 +00:00
|
|
|
return false;
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 01:38:25 +00:00
|
|
|
m_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format shareContext:nil];
|
2017-04-16 03:49:00 +00:00
|
|
|
if (m_context == nil)
|
2014-08-30 21:01:19 +00:00
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "failed to create context");
|
2013-08-29 05:33:24 +00:00
|
|
|
return false;
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-04-10 14:40:19 +00:00
|
|
|
if (!wsi.render_surface)
|
2017-04-16 03:43:22 +00:00
|
|
|
return true;
|
2017-04-16 02:23:19 +00:00
|
|
|
|
2019-04-10 14:40:19 +00:00
|
|
|
m_view = static_cast<NSView*>(wsi.render_surface);
|
2018-10-03 13:02:45 +00:00
|
|
|
m_opengl_mode = Mode::OpenGL;
|
2018-10-03 13:03:33 +00:00
|
|
|
if (!AttachContextToView(m_context, m_view, &m_backbuffer_width, &m_backbuffer_height))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
[m_context makeCurrentContext];
|
|
|
|
return true;
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:03:30 +00:00
|
|
|
std::unique_ptr<GLContext> GLContextAGL::CreateSharedContext()
|
2017-07-20 01:38:25 +00:00
|
|
|
{
|
2019-05-05 23:48:12 +00:00
|
|
|
NSOpenGLContext* new_agl_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format
|
|
|
|
shareContext:m_context];
|
2018-10-03 13:03:30 +00:00
|
|
|
if (new_agl_context == nil)
|
2017-07-20 01:38:25 +00:00
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "failed to create shared context");
|
2018-10-03 13:03:30 +00:00
|
|
|
return nullptr;
|
2017-07-20 01:38:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:03:30 +00:00
|
|
|
std::unique_ptr<GLContextAGL> new_context = std::make_unique<GLContextAGL>();
|
|
|
|
new_context->m_context = new_agl_context;
|
|
|
|
new_context->m_pixel_format = m_pixel_format;
|
|
|
|
[new_context->m_pixel_format retain];
|
2018-10-03 13:03:33 +00:00
|
|
|
new_context->m_is_shared = true;
|
2018-10-03 13:03:30 +00:00
|
|
|
return new_context;
|
2017-07-20 01:38:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
bool GLContextAGL::MakeCurrent()
|
2012-12-17 21:01:52 +00:00
|
|
|
{
|
2017-04-16 03:49:00 +00:00
|
|
|
[m_context makeCurrentContext];
|
2013-03-18 22:15:59 +00:00
|
|
|
return true;
|
2012-12-17 21:01:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
bool GLContextAGL::ClearCurrent()
|
2013-04-11 01:32:07 +00:00
|
|
|
{
|
2014-10-23 14:56:42 +00:00
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2013-04-11 01:32:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
void GLContextAGL::Update()
|
2013-03-18 01:58:43 +00:00
|
|
|
{
|
2017-04-16 03:49:00 +00:00
|
|
|
if (!m_view)
|
2017-04-16 03:43:22 +00:00
|
|
|
return;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height))
|
2020-01-02 02:47:55 +00:00
|
|
|
// the following calls can crash if not called from the main thread on macOS 10.15
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
[m_context update];
|
|
|
|
});
|
2013-03-18 01:58:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
void GLContextAGL::SwapInterval(int interval)
|
2014-01-27 12:11:03 +00:00
|
|
|
{
|
2017-04-16 03:49:00 +00:00
|
|
|
[m_context setValues:static_cast<GLint*>(&interval) forParameter:NSOpenGLCPSwapInterval];
|
2014-01-27 12:11:03 +00:00
|
|
|
}
|