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

115 lines
2.7 KiB
Text
Raw Normal View History

// Copyright 2012 Dolphin Emulator Project
2015-05-18 01:08:10 +02:00
// Licensed under GPLv2+
// Refer to the license.txt file included.
2012-12-17 22:01:52 +01:00
#include "Common/GL/GLInterface/AGL.h"
2015-09-18 19:43:34 +02:00
#include "Common/Logging/Log.h"
2012-12-17 22:01:52 +01:00
2017-04-16 05:43:22 +02: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 07:34:09 +01:00
void cInterfaceAGL::Swap()
2012-12-17 22:01:52 +01:00
{
[cocoaCtx flushBuffer];
2012-12-17 22:01:52 +01:00
}
// Create rendering window.
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
bool cInterfaceAGL::Create(void* window_handle, bool core)
2012-12-17 22:01:52 +01:00
{
NSOpenGLPixelFormatAttribute attr[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAOpenGLProfile,
core ? NSOpenGLProfileVersion3_2Core :
NSOpenGLProfileVersionLegacy,
NSOpenGLPFAAccelerated, 0};
NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
if (fmt == nil)
{
ERROR_LOG(VIDEO, "failed to create pixel format");
return false;
}
cocoaCtx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
[fmt release];
if (cocoaCtx == nil)
{
ERROR_LOG(VIDEO, "failed to create context");
return false;
}
2017-04-16 05:43:22 +02:00
if (!window_handle)
return true;
2017-04-16 04:23:19 +02:00
2017-04-16 05:43:22 +02:00
cocoaWin = static_cast<NSView*>(window_handle);
return AttachContextToView(cocoaCtx, cocoaWin, &s_backbuffer_width, &s_backbuffer_height);
2012-12-17 22:01:52 +01:00
}
bool cInterfaceAGL::MakeCurrent()
{
[cocoaCtx makeCurrentContext];
return true;
2012-12-17 22:01:52 +01:00
}
bool cInterfaceAGL::ClearCurrent()
{
[NSOpenGLContext clearCurrentContext];
return true;
}
2012-12-17 22:01:52 +01:00
// Close backend
void cInterfaceAGL::Shutdown()
{
[cocoaCtx clearDrawable];
[cocoaCtx release];
cocoaCtx = nil;
2012-12-17 22:01:52 +01:00
}
void cInterfaceAGL::Update()
{
2017-04-16 05:43:22 +02:00
if (!cocoaWin)
return;
2017-04-16 05:43:22 +02:00
if (UpdateCachedDimensions(cocoaWin, &s_backbuffer_width, &s_backbuffer_height))
[cocoaCtx update];
}
2014-01-27 13:24:35 +01:00
void cInterfaceAGL::SwapInterval(int interval)
{
[cocoaCtx setValues:(GLint*)&interval forParameter:NSOpenGLCPSwapInterval];
}