Skip to content

Commit

Permalink
macos: add retina support
Browse files Browse the repository at this point in the history
Co-authored-by: khogeland <[email protected]>
Co-authored-by: Max Weber <[email protected]>
  • Loading branch information
3 people committed Apr 6, 2024
1 parent be1037c commit d5229d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions rlawt.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
#ifdef __APPLE__
CALayer *layer;
IOSurfaceRef buffer[2];
CGFloat bufferScale[2];
CGLContextObj context;

GLuint tex[2];
Expand Down
20 changes: 15 additions & 5 deletions rlawt_mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ -(void)setContentsChanged;
@end

@interface RLLayer : CALayer {
int offsetY;
@public int offsetY;
CGFloat newScale;
}
- (void)setFrame:(CGRect)newValue;
- (void)fixFrame;
Expand Down Expand Up @@ -69,6 +70,7 @@ - (void)fixFrame {
- (void)displayIOSurface:(id)ioSurface {
[CATransaction begin];
[CATransaction setDisableActions: true];
self.contentsScale = self->newScale;
self.contents = ioSurface;
[(id<CanSetContentsChanged>)self setContentsChanged];
[self fixFrame];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -293,16 +300,19 @@ 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];

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

0 comments on commit d5229d0

Please sign in to comment.