From 1b01cfb3dc74d2c0ab204f008805f5b97a4dbed5 Mon Sep 17 00:00:00 2001 From: khogeland Date: Wed, 13 Mar 2024 20:49:20 -0700 Subject: [PATCH 1/4] MacOS retina support --- AWTContext.java | 3 ++- rlawt.h | 2 ++ rlawt_mac.m | 34 ++++++++++++++++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/AWTContext.java b/AWTContext.java index 48ce48d..20b9288 100644 --- a/AWTContext.java +++ b/AWTContext.java @@ -117,7 +117,7 @@ public AWTContext(Component component) { if (c instanceof Window) { - Insets insets = ((Window) c).getInsets(); + Insets insets = ((Window)c).getInsets(); x -= insets.left; y -= insets.top; break; @@ -178,4 +178,5 @@ public int getBufferMode() public native long getGLXDisplay(); public native long getWGLHDC(); + } diff --git a/rlawt.h b/rlawt.h index 202c246..af11c13 100644 --- a/rlawt.h +++ b/rlawt.h @@ -62,6 +62,8 @@ typedef struct { int offsetX; int offsetY; + + GLfloat backingScaleFactor; #endif #ifdef __unix__ diff --git a/rlawt_mac.m b/rlawt_mac.m index 61ff9c8..18041c6 100644 --- a/rlawt_mac.m +++ b/rlawt_mac.m @@ -110,8 +110,8 @@ static void propsPutInt(CFMutableDictionaryRef props, const CFStringRef key, int static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { CFMutableDictionaryRef props = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CGSize size = ctx->layer.frame.size; - propsPutInt(props, kIOSurfaceHeight, size.height); - propsPutInt(props, kIOSurfaceWidth, size.width); + propsPutInt(props, kIOSurfaceHeight, size.height*ctx->backingScaleFactor); + propsPutInt(props, kIOSurfaceWidth, size.width*ctx->backingScaleFactor); propsPutInt(props, kIOSurfaceBytesPerElement, 4); propsPutInt(props, kIOSurfacePixelFormat, (int)'BGRA'); @@ -128,7 +128,7 @@ static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { CGLError err = CGLTexImageIOSurface2D( ctx->context, target, GL_RGBA, - size.width, size.height, + size.width*ctx->backingScaleFactor, size.height*ctx->backingScaleFactor, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf, 0); @@ -211,12 +211,15 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv glGenTextures(2, &ctx->tex[0]); glGenFramebuffers(2, &ctx->fbo[0]); + ctx->backingScaleFactor = dspi.windowLayer.contentsScale; + dispatch_sync(dispatch_get_main_queue(), ^{ RLLayer *layer = [[RLLayer alloc] init]; layer.opaque = true; layer.needsDisplayOnBoundsChange = false; layer.magnificationFilter = kCAFilterNearest; - layer.contentsGravity = kCAGravityCenter; + layer.contentsGravity = kCAGravityTopLeft; + layer.contentsScale = ctx->backingScaleFactor; layer.affineTransform = CGAffineTransformMakeScale(1, -1); ctx->layer = layer; @@ -293,19 +296,30 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *en } glFlush(); + [(RLLayer*)ctx->layer performSelectorOnMainThread: @selector(displayIOSurface:) withObject: (id)(ctx->buffer[ctx->back]) waitUntilDone: true]; - + 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) { - if (!rlawtCreateIOSurface(env, ctx)) { - return; - } + || IOSurfaceGetWidth(ctx->buffer[ctx->back]) != ctx->layer.frame.size.width + || IOSurfaceGetHeight(ctx->buffer[ctx->back]) != ctx->layer.frame.size.height + || ctx->backingScaleFactor != ctx->layer.superlayer.contentsScale) { + ctx->backingScaleFactor = ctx->layer.superlayer.contentsScale; + dispatch_sync(dispatch_get_main_queue(), ^{ + RLLayer *layer = (RLLayer*)ctx->layer; + layer.contentsScale = ctx->backingScaleFactor; + }); + if (!rlawtCreateIOSurface(env, ctx)) { + return; + } + ctx->back ^= 1; // remake both buffers at the same time to prevent flickering + if (!rlawtCreateIOSurface(env, ctx)) { + return; + } } } From 6a6b753d7e43671e381bdaf032df8522ad4ea87a Mon Sep 17 00:00:00 2001 From: khogeland Date: Fri, 15 Mar 2024 16:54:51 -0700 Subject: [PATCH 2/4] cleanup --- AWTContext.java | 3 +-- rlawt_mac.m | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/AWTContext.java b/AWTContext.java index 20b9288..48ce48d 100644 --- a/AWTContext.java +++ b/AWTContext.java @@ -117,7 +117,7 @@ public AWTContext(Component component) { if (c instanceof Window) { - Insets insets = ((Window)c).getInsets(); + Insets insets = ((Window) c).getInsets(); x -= insets.left; y -= insets.top; break; @@ -178,5 +178,4 @@ public int getBufferMode() public native long getGLXDisplay(); public native long getWGLHDC(); - } diff --git a/rlawt_mac.m b/rlawt_mac.m index 18041c6..7c5d76b 100644 --- a/rlawt_mac.m +++ b/rlawt_mac.m @@ -218,7 +218,7 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv layer.opaque = true; layer.needsDisplayOnBoundsChange = false; layer.magnificationFilter = kCAFilterNearest; - layer.contentsGravity = kCAGravityTopLeft; + layer.contentsGravity = kCAGravityCenter; layer.contentsScale = ctx->backingScaleFactor; layer.affineTransform = CGAffineTransformMakeScale(1, -1); From cb2058cd9b35d3678ea726b0f0e45d4d80419570 Mon Sep 17 00:00:00 2001 From: khogeland Date: Fri, 15 Mar 2024 16:59:50 -0700 Subject: [PATCH 3/4] cleanup --- rlawt.h | 1 - rlawt_mac.m | 15 ++++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/rlawt.h b/rlawt.h index af11c13..c1023c7 100644 --- a/rlawt.h +++ b/rlawt.h @@ -63,7 +63,6 @@ typedef struct { int offsetX; int offsetY; - GLfloat backingScaleFactor; #endif #ifdef __unix__ diff --git a/rlawt_mac.m b/rlawt_mac.m index 7c5d76b..be5d7df 100644 --- a/rlawt_mac.m +++ b/rlawt_mac.m @@ -110,8 +110,8 @@ static void propsPutInt(CFMutableDictionaryRef props, const CFStringRef key, int static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { CFMutableDictionaryRef props = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CGSize size = ctx->layer.frame.size; - propsPutInt(props, kIOSurfaceHeight, size.height*ctx->backingScaleFactor); - propsPutInt(props, kIOSurfaceWidth, size.width*ctx->backingScaleFactor); + propsPutInt(props, kIOSurfaceHeight, size.height*ctx->layer.superlayer.contentsScale); + propsPutInt(props, kIOSurfaceWidth, size.width*ctx->layer.superlayer.contentsScale); propsPutInt(props, kIOSurfaceBytesPerElement, 4); propsPutInt(props, kIOSurfacePixelFormat, (int)'BGRA'); @@ -128,7 +128,7 @@ static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) { CGLError err = CGLTexImageIOSurface2D( ctx->context, target, GL_RGBA, - size.width*ctx->backingScaleFactor, size.height*ctx->backingScaleFactor, + size.width*ctx->layer.superlayer.contentsScale, size.height*ctx->layer.superlayer.contentsScale, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf, 0); @@ -211,15 +211,13 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv glGenTextures(2, &ctx->tex[0]); glGenFramebuffers(2, &ctx->fbo[0]); - ctx->backingScaleFactor = dspi.windowLayer.contentsScale; - dispatch_sync(dispatch_get_main_queue(), ^{ RLLayer *layer = [[RLLayer alloc] init]; layer.opaque = true; layer.needsDisplayOnBoundsChange = false; layer.magnificationFilter = kCAFilterNearest; layer.contentsGravity = kCAGravityCenter; - layer.contentsScale = ctx->backingScaleFactor; + layer.contentsScale = dspi.windowLayer.contentsScale; layer.affineTransform = CGAffineTransformMakeScale(1, -1); ctx->layer = layer; @@ -307,11 +305,10 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *en 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 - || ctx->backingScaleFactor != ctx->layer.superlayer.contentsScale) { - ctx->backingScaleFactor = ctx->layer.superlayer.contentsScale; + || ctx->layer.contentsScale != ctx->layer.superlayer.contentsScale) { dispatch_sync(dispatch_get_main_queue(), ^{ RLLayer *layer = (RLLayer*)ctx->layer; - layer.contentsScale = ctx->backingScaleFactor; + layer.contentsScale = ctx->layer.superlayer.contentsScale; }); if (!rlawtCreateIOSurface(env, ctx)) { return; From 6c72d1d66f049dd4c4b4cf57b4098310855a6874 Mon Sep 17 00:00:00 2001 From: khogeland Date: Fri, 15 Mar 2024 19:51:06 -0700 Subject: [PATCH 4/4] whitespace --- rlawt.h | 1 - rlawt_mac.m | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/rlawt.h b/rlawt.h index c1023c7..202c246 100644 --- a/rlawt.h +++ b/rlawt.h @@ -62,7 +62,6 @@ typedef struct { int offsetX; int offsetY; - #endif #ifdef __unix__ diff --git a/rlawt_mac.m b/rlawt_mac.m index be5d7df..2198485 100644 --- a/rlawt_mac.m +++ b/rlawt_mac.m @@ -306,17 +306,17 @@ JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *en || IOSurfaceGetWidth(ctx->buffer[ctx->back]) != ctx->layer.frame.size.width || IOSurfaceGetHeight(ctx->buffer[ctx->back]) != ctx->layer.frame.size.height || ctx->layer.contentsScale != ctx->layer.superlayer.contentsScale) { - dispatch_sync(dispatch_get_main_queue(), ^{ - RLLayer *layer = (RLLayer*)ctx->layer; - layer.contentsScale = ctx->layer.superlayer.contentsScale; - }); - if (!rlawtCreateIOSurface(env, ctx)) { - return; - } - ctx->back ^= 1; // remake both buffers at the same time to prevent flickering - if (!rlawtCreateIOSurface(env, ctx)) { - return; - } + dispatch_sync(dispatch_get_main_queue(), ^{ + RLLayer *layer = (RLLayer*)ctx->layer; + layer.contentsScale = ctx->layer.superlayer.contentsScale; + }); + if (!rlawtCreateIOSurface(env, ctx)) { + return; + } + ctx->back ^= 1; // remake both buffers at the same time to prevent flickering + if (!rlawtCreateIOSurface(env, ctx)) { + return; + } } }