From d5229d053af14578e5c9e6c42eab523ff22c1628 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 6 Apr 2024 10:01:49 -0400 Subject: [PATCH] macos: add retina support Co-authored-by: khogeland Co-authored-by: Max Weber --- rlawt.h | 1 + rlawt_mac.m | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/rlawt.h b/rlawt.h index 202c246..c75849d 100644 --- a/rlawt.h +++ b/rlawt.h @@ -54,6 +54,7 @@ typedef struct { #ifdef __APPLE__ CALayer *layer; IOSurfaceRef buffer[2]; + CGFloat bufferScale[2]; CGLContextObj context; GLuint tex[2]; diff --git a/rlawt_mac.m b/rlawt_mac.m index 61ff9c8..384009d 100644 --- a/rlawt_mac.m +++ b/rlawt_mac.m @@ -35,7 +35,8 @@ -(void)setContentsChanged; @end @interface RLLayer : CALayer { - int offsetY; + @public int offsetY; + CGFloat newScale; } - (void)setFrame:(CGRect)newValue; - (void)fixFrame; @@ -69,6 +70,7 @@ - (void)fixFrame { - (void)displayIOSurface:(id)ioSurface { [CATransaction begin]; [CATransaction setDisableActions: true]; + self.contentsScale = self->newScale; self.contents = ioSurface; [(id)self setContentsChanged]; [self fixFrame]; @@ -108,8 +110,12 @@ static void propsPutInt(CFMutableDictionaryRef props, const CFStringRef key, int } static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { - CFMutableDictionaryRef props = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + CGFloat scale = ctx->layer.superlayer.contentsScale; CGSize size = ctx->layer.frame.size; + size.width *= scale; + size.height *= scale; + + CFMutableDictionaryRef props = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); propsPutInt(props, kIOSurfaceHeight, size.height); propsPutInt(props, kIOSurfaceWidth, size.width); propsPutInt(props, kIOSurfaceBytesPerElement, 4); @@ -153,6 +159,7 @@ static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { CFRelease(ctx->buffer[ctx->back]); } ctx->buffer[ctx->back] = buf; + ctx->bufferScale[ctx->back] = scale; return true; freeSurface: CFRelease(buf); @@ -293,7 +300,9 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *en } glFlush(); - [(RLLayer*)ctx->layer performSelectorOnMainThread: + RLLayer *rlLayer = (RLLayer*) ctx->layer; + rlLayer->newScale = ctx->bufferScale[ctx->back]; + [rlLayer performSelectorOnMainThread: @selector(displayIOSurface:) withObject: (id)(ctx->buffer[ctx->back]) waitUntilDone: true]; @@ -301,8 +310,9 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *en ctx->back ^= 1; if (!ctx->buffer[ctx->back] - || IOSurfaceGetWidth(ctx->buffer[ctx->back]) != ctx->layer.frame.size.width - || IOSurfaceGetHeight(ctx->buffer[ctx->back]) != ctx->layer.frame.size.height) { + || IOSurfaceGetWidth(ctx->buffer[ctx->back]) != (size_t) (ctx->layer.frame.size.width * ctx->bufferScale[ctx->back]) + || IOSurfaceGetHeight(ctx->buffer[ctx->back]) != (size_t) (ctx->layer.frame.size.height * ctx->bufferScale[ctx->back]) + || ctx->layer.superlayer.contentsScale != ctx->bufferScale[ctx->back]) { if (!rlawtCreateIOSurface(env, ctx)) { return; }