| 1 |
// Copyright 2012 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "Common/GL/GLInterface/AGL.h" |
| 5 |
|
| 6 |
#include "Common/Logging/Log.h" |
| 7 |
|
| 8 |
// UpdateCachedDimensions and AttachContextToView contain calls to UI APIs, so they must only be |
| 9 |
// called from the main thread or they risk crashing! |
| 10 |
|
| 11 |
static bool UpdateCachedDimensions(NSView* view, u32* width, u32* height) |
| 12 |
{ |
| 13 |
NSWindow* window = [view window]; |
| 14 |
NSSize size = [view frame].size; |
| 15 |
|
| 16 |
const CGFloat scale = [window backingScaleFactor]; |
| 17 |
u32 new_width = static_cast<u32>(size.width * scale); |
| 18 |
u32 new_height = static_cast<u32>(size.height * scale); |
| 19 |
|
| 20 |
if (*width == new_width && *height == new_height) |
| 21 |
return false; |
| 22 |
|
| 23 |
*width = new_width; |
| 24 |
*height = new_height; |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
static bool AttachContextToView(NSOpenGLContext* context, NSView* view, u32* width, u32* height) |
| 29 |
{ |
| 30 |
// Enable high-resolution display support. |
| 31 |
[view setWantsBestResolutionOpenGLSurface:YES]; |
| 32 |
|
| 33 |
NSWindow* window = [view window]; |
| 34 |
if (window == nil) |
| 35 |
{ |
| 36 |
ERROR_LOG_FMT(VIDEO, "failed to get NSWindow"); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
(void)UpdateCachedDimensions(view, width, height); |
| 41 |
|
| 42 |
[context setView:view]; |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
GLContextAGL::~GLContextAGL() |
| 48 |
{ |
| 49 |
if ([NSOpenGLContext currentContext] == m_context) |
| 50 |
[NSOpenGLContext clearCurrentContext]; |
| 51 |
|
| 52 |
if (m_context) |
| 53 |
{ |
| 54 |
[m_context clearDrawable]; |
| 55 |
[m_context release]; |
| 56 |
} |
| 57 |
if (m_pixel_format) |
| 58 |
[m_pixel_format release]; |
| 59 |
} |
| 60 |
|
| 61 |
bool GLContextAGL::IsHeadless() const |
| 62 |
{ |
| 63 |
return !m_view; |
| 64 |
} |
| 65 |
|
| 66 |
void GLContextAGL::Swap() |
| 67 |
{ |
| 68 |
[m_context flushBuffer]; |
| 69 |
} |
| 70 |
|
| 71 |
// Create rendering window. |
| 72 |
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() |
| 73 |
bool GLContextAGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool core) |
| 74 |
{ |
| 75 |
NSOpenGLPixelFormatAttribute attr[] = { |
| 76 |
NSOpenGLPFADoubleBuffer, |
| 77 |
NSOpenGLPFAOpenGLProfile, |
| 78 |
core ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy, |
| 79 |
NSOpenGLPFAAccelerated, |
| 80 |
stereo ? NSOpenGLPFAStereo : static_cast<NSOpenGLPixelFormatAttribute>(0), |
| 81 |
0}; |
| 82 |
m_pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr]; |
| 83 |
if (m_pixel_format == nil) |
| 84 |
{ |
| 85 |
ERROR_LOG_FMT(VIDEO, "failed to create pixel format"); |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
m_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format shareContext:nil]; |
| 90 |
if (m_context == nil) |
| 91 |
{ |
| 92 |
ERROR_LOG_FMT(VIDEO, "failed to create context"); |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if (!wsi.render_surface) |
| 97 |
return true; |
| 98 |
|
| 99 |
m_view = static_cast<NSView*>(wsi.render_surface); |
| 100 |
m_opengl_mode = Mode::OpenGL; |
| 101 |
|
| 102 |
__block bool success; |
| 103 |
if ([NSThread isMainThread]) |
| 104 |
{ |
| 105 |
success = AttachContextToView(m_context, m_view, &m_backbuffer_width, &m_backbuffer_height); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 110 |
success = AttachContextToView(m_context, m_view, &m_backbuffer_width, &m_backbuffer_height); |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
if (!success) |
| 115 |
{ |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
[m_context makeCurrentContext]; |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
std::unique_ptr<GLContext> GLContextAGL::CreateSharedContext() |
| 124 |
{ |
| 125 |
NSOpenGLContext* new_agl_context = [[NSOpenGLContext alloc] initWithFormat:m_pixel_format |
| 126 |
shareContext:m_context]; |
| 127 |
if (new_agl_context == nil) |
| 128 |
{ |
| 129 |
ERROR_LOG_FMT(VIDEO, "failed to create shared context"); |
| 130 |
return nullptr; |
| 131 |
} |
| 132 |
|
| 133 |
std::unique_ptr<GLContextAGL> new_context = std::make_unique<GLContextAGL>(); |
| 134 |
new_context->m_context = new_agl_context; |
| 135 |
new_context->m_pixel_format = m_pixel_format; |
| 136 |
[new_context->m_pixel_format retain]; |
| 137 |
new_context->m_is_shared = true; |
| 138 |
return new_context; |
| 139 |
} |
| 140 |
|
| 141 |
bool GLContextAGL::MakeCurrent() |
| 142 |
{ |
| 143 |
[m_context makeCurrentContext]; |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
bool GLContextAGL::ClearCurrent() |
| 148 |
{ |
| 149 |
[NSOpenGLContext clearCurrentContext]; |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
void GLContextAGL::Update() |
| 154 |
{ |
| 155 |
if (!m_view) |
| 156 |
return; |
| 157 |
|
| 158 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 159 |
if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height)) |
| 160 |
{ |
| 161 |
[m_context update]; |
| 162 |
} |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
void GLContextAGL::SwapInterval(int interval) |
| 167 |
{ |
| 168 |
[m_context setValues:static_cast<GLint*>(&interval) forParameter:NSOpenGLCPSwapInterval]; |
| 169 |
} |