dolphin/Source/Core/Common/GL/GLInterface/AGL.mm

150 lines
3.7 KiB
Plaintext
Raw Normal View History

// Copyright 2012 Dolphin Emulator Project
2015-05-17 23:08:10 +00:00
// Licensed under GPLv2+
// Refer to the license.txt file included.
2012-12-17 21:01:52 +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;
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
if (*width == new_width && *height == new_height)
2017-04-16 03:43:22 +00:00
return false;
*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)
{
ERROR_LOG(VIDEO, "failed to get NSWindow");
return false;
}
(void)UpdateCachedDimensions(view, width, height);
[window makeFirstResponder:view];
[context setView:view];
[window makeKeyAndOrderFront:nil];
return true;
}
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];
}
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()
bool GLContextAGL::Initialize(void* display_handle, void* window_handle, bool stereo, bool core)
2012-12-17 21:01:52 +00:00
{
NSOpenGLPixelFormatAttribute attr[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAOpenGLProfile,
core ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy,
NSOpenGLPFAAccelerated,
stereo ? NSOpenGLPFAStereo : static_cast<NSOpenGLPixelFormatAttribute>(0),
0};
m_pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
if (m_pixel_format == nil)
{
ERROR_LOG(VIDEO, "failed to create pixel format");
return false;
}
m_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format shareContext:nil];
2017-04-16 03:49:00 +00:00
if (m_context == nil)
{
ERROR_LOG(VIDEO, "failed to create context");
return false;
}
2017-04-16 03:43:22 +00:00
if (!window_handle)
return true;
2017-04-16 02:23:19 +00:00
2017-04-16 03:49:00 +00:00
m_view = static_cast<NSView*>(window_handle);
m_opengl_mode = Mode::OpenGL;
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
}
std::unique_ptr<GLContext> GLContextAGL::CreateSharedContext()
{
NSOpenGLContext* new_agl_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format
shareContext:m_context];
if (new_agl_context == nil)
{
ERROR_LOG(VIDEO, "failed to create shared context");
return nullptr;
}
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];
new_context->m_is_shared = true;
return new_context;
}
bool GLContextAGL::MakeCurrent()
2012-12-17 21:01:52 +00:00
{
2017-04-16 03:49:00 +00:00
[m_context makeCurrentContext];
return true;
2012-12-17 21:01:52 +00:00
}
bool GLContextAGL::ClearCurrent()
{
[NSOpenGLContext clearCurrentContext];
return true;
}
void GLContextAGL::Update()
{
2017-04-16 03:49:00 +00:00
if (!m_view)
2017-04-16 03:43:22 +00:00
return;
if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height))
2017-04-16 03:49:00 +00:00
[m_context update];
}
void GLContextAGL::SwapInterval(int interval)
{
2017-04-16 03:49:00 +00:00
[m_context setValues:static_cast<GLint*>(&interval) forParameter:NSOpenGLCPSwapInterval];
}