Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MacOS retina support #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AWTContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,4 +178,5 @@ public int getBufferMode()
public native long getGLXDisplay();

public native long getWGLHDC();

}
2 changes: 2 additions & 0 deletions rlawt.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef struct {

int offsetX;
int offsetY;

GLfloat backingScaleFactor;
khogeland marked this conversation as resolved.
Show resolved Hide resolved
#endif

#ifdef __unix__
Expand Down
34 changes: 24 additions & 10 deletions rlawt_mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the scales are tied to the buffers I would probably make a CGfloat scale[2] in AWTContext to avoid this double-swap

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;
}
}
}

Expand Down