mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
0e592f8bc6
The NSViewGlobalFrameDidChangeNotification notification is documented to be fired every time [NSOpenGLContext update] needs to be called. However, the notification fails to fire on my setup when a window is moved to a display with a different pixel scale, which leads to incorrectly sized output. This change gets rid of the notification and updates the context before every frame. Signed-off-by: Elias Naur <mail@eliasnaur.com>
70 lines
1.8 KiB
Objective-C
70 lines
1.8 KiB
Objective-C
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
// +build darwin,!ios
|
|
|
|
@import AppKit;
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
#include <OpenGL/OpenGL.h>
|
|
#include "_cgo_export.h"
|
|
|
|
CFTypeRef gio_createGLContext(void) {
|
|
@autoreleasepool {
|
|
NSOpenGLPixelFormatAttribute attr[] = {
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
|
NSOpenGLPFAColorSize, 24,
|
|
NSOpenGLPFADepthSize, 16,
|
|
NSOpenGLPFAAccelerated,
|
|
// Opt-in to automatic GPU switching. CGL-only property.
|
|
kCGLPFASupportsAutomaticGraphicsSwitching,
|
|
NSOpenGLPFAAllowOfflineRenderers,
|
|
0
|
|
};
|
|
NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
|
|
|
|
NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext: nil];
|
|
return CFBridgingRetain(ctx);
|
|
}
|
|
}
|
|
|
|
void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) {
|
|
NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
|
|
NSView *view = (__bridge NSView *)viewRef;
|
|
[view setWantsBestResolutionOpenGLSurface:YES];
|
|
[ctx setView:view];
|
|
}
|
|
|
|
void gio_clearCurrentContext(void) {
|
|
@autoreleasepool {
|
|
[NSOpenGLContext clearCurrentContext];
|
|
}
|
|
}
|
|
|
|
void gio_updateContext(CFTypeRef ctxRef) {
|
|
@autoreleasepool {
|
|
NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
|
|
[ctx update];
|
|
}
|
|
}
|
|
|
|
void gio_makeCurrentContext(CFTypeRef ctxRef) {
|
|
@autoreleasepool {
|
|
NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
|
|
[ctx makeCurrentContext];
|
|
}
|
|
}
|
|
|
|
void gio_lockContext(CFTypeRef ctxRef) {
|
|
@autoreleasepool {
|
|
NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
|
|
CGLLockContext([ctx CGLContextObj]);
|
|
}
|
|
}
|
|
|
|
void gio_unlockContext(CFTypeRef ctxRef) {
|
|
@autoreleasepool {
|
|
NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
|
|
CGLUnlockContext([ctx CGLContextObj]);
|
|
}
|
|
}
|