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

143 lines
3.4 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;
float scale = [window backingScaleFactor];
size.width *= scale;
size.height *= scale;
if (*width == size.width && *height == size.height)
return false;
*width = size.width;
*height = size.height;
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;
}
2012-12-26 06:34:09 +00:00
void cInterfaceAGL::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 cInterfaceAGL::Create(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);
return AttachContextToView(m_context, m_view, &s_backbuffer_width, &s_backbuffer_height);
2012-12-17 21:01:52 +00:00
}
bool cInterfaceAGL::Create(cInterfaceBase* main_context)
{
cInterfaceAGL* agl_context = static_cast<cInterfaceAGL*>(main_context);
NSOpenGLPixelFormat* pixel_format = agl_context->m_pixel_format;
NSOpenGLContext* share_context = agl_context->m_context;
m_context = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:share_context];
if (m_context == nil)
{
ERROR_LOG(VIDEO, "failed to create shared context");
return false;
}
return true;
}
std::unique_ptr<cInterfaceBase> cInterfaceAGL::CreateSharedContext()
{
std::unique_ptr<cInterfaceBase> context = std::make_unique<cInterfaceAGL>();
if (!context->Create(this))
return nullptr;
return context;
}
2012-12-17 21:01:52 +00:00
bool cInterfaceAGL::MakeCurrent()
{
2017-04-16 03:49:00 +00:00
[m_context makeCurrentContext];
return true;
2012-12-17 21:01:52 +00:00
}
bool cInterfaceAGL::ClearCurrent()
{
[NSOpenGLContext clearCurrentContext];
return true;
}
2012-12-17 21:01:52 +00:00
// Close backend
void cInterfaceAGL::Shutdown()
{
2017-04-16 03:49:00 +00:00
[m_context clearDrawable];
[m_context release];
m_context = nil;
[m_pixel_format release];
m_pixel_format = nil;
2012-12-17 21:01:52 +00:00
}
void cInterfaceAGL::Update()
{
2017-04-16 03:49:00 +00:00
if (!m_view)
2017-04-16 03:43:22 +00:00
return;
2017-04-16 03:49:00 +00:00
if (UpdateCachedDimensions(m_view, &s_backbuffer_width, &s_backbuffer_height))
[m_context update];
}
2014-01-27 12:24:35 +00:00
void cInterfaceAGL::SwapInterval(int interval)
{
2017-04-16 03:49:00 +00:00
[m_context setValues:static_cast<GLint*>(&interval) forParameter:NSOpenGLCPSwapInterval];
}