-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrlawt_mac.m
331 lines (281 loc) · 9.52 KB
/
rlawt_mac.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2022 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __APPLE__
#include "rlawt.h"
#include <jawt_md.h>
#include <OpenGL/gl3.h>
#include <QuartzCore/QuartzCore.h>
@protocol CanSetContentsChanged
-(void)setContentsChanged;
@end
@interface RLLayer : CALayer {
@public int offsetY;
CGFloat newScale;
}
- (void)setFrame:(CGRect)newValue;
- (void)fixFrame;
- (void)displayIOSurface:(id)ioSurface;
@end
@implementation RLLayer
- (void)setFrame:(CGRect)newValue {
[super setFrame: newValue];
if (self.superlayer) {
offsetY = self.superlayer.frame.size.height - newValue.origin.y - newValue.size.height;
}
}
// JAWT does not update our bounds when the y offset from the bottom of our
// superlayer changes, causing the layer to become displaced from the correct
// vertical position
- (void)fixFrame {
if (!self.superlayer) {
return;
}
super.frame = CGRectMake(
self.frame.origin.x,
self.superlayer.frame.size.height - offsetY - self.frame.size.height,
self.frame.size.width,
self.frame.size.height);
}
- (void)displayIOSurface:(id)ioSurface {
[CATransaction begin];
[CATransaction setDisableActions: true];
self.contentsScale = self->newScale;
self.contents = ioSurface;
[(id<CanSetContentsChanged>)self setContentsChanged];
[self fixFrame];
[CATransaction commit];
}
@end
void rlawtThrow(JNIEnv *env, const char *msg) {
if ((*env)->ExceptionCheck(env)) {
return;
}
jclass clazz = (*env)->FindClass(env, "java/lang/RuntimeException");
(*env)->ThrowNew(env, clazz, msg);
}
static void rlawtThrowCGLError(JNIEnv *env, const char *msg, CGLError err) {
char buf[256] = {0};
snprintf(buf, sizeof(buf), "%s (cgl: %s)", msg, CGLErrorString(err));
rlawtThrow(env, buf);
}
static bool makeCurrent(JNIEnv *env, CGLContextObj ctx) {
CGLError err = CGLSetCurrentContext(ctx);
if (err != kCGLNoError) {
rlawtThrowCGLError(env, "unable to make current", err);
return false;
}
return true;
}
static void propsPutInt(CFMutableDictionaryRef props, const CFStringRef key, int value) {
CFNumberRef boxedValue = CFNumberCreate(NULL, kCFNumberIntType, &value);
CFDictionaryAddValue(props, key, boxedValue);
CFRelease(boxedValue);
}
static bool rlawtCreateIOSurface(JNIEnv *env, AWTContext *ctx) {
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);
propsPutInt(props, kIOSurfacePixelFormat, (int)'BGRA');
IOSurfaceRef buf = IOSurfaceCreate(props);
CFRelease(props);
if (!buf) {
rlawtThrow(env, "unable to create io surface");
return false;
}
const GLuint target = GL_TEXTURE_RECTANGLE;
glActiveTexture(GL_TEXTURE0);
glBindTexture(target, ctx->tex[ctx->back]);
CGLError err = CGLTexImageIOSurface2D(
ctx->context,
target, GL_RGBA,
size.width, size.height,
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
buf,
0);
glBindTexture(target, 0);
glBindFramebuffer(GL_FRAMEBUFFER, ctx->fbo[ctx->back]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, ctx->tex[ctx->back], 0);
if (err != kCGLNoError) {
rlawtThrowCGLError(env, "unable to bind io surface to texture", err);
goto freeSurface;
}
int fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fbStatus != GL_FRAMEBUFFER_COMPLETE) {
char buf[256] = {0};
snprintf(buf, sizeof(buf), "unable to create fb (%d)", fbStatus);
rlawtThrow(env, buf);
goto freeSurface;
}
if (ctx->buffer[ctx->back]) {
CFRelease(ctx->buffer[ctx->back]);
}
ctx->buffer[ctx->back] = buf;
ctx->bufferScale[ctx->back] = scale;
return true;
freeSurface:
CFRelease(buf);
return false;
}
JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_createGLContext(JNIEnv *env, jobject self) {
AWTContext *ctx = rlawtGetContext(env, self);
if (!ctx || !rlawtContextState(env, ctx, false)) {
return;
}
JAWT_DrawingSurfaceInfo *dsi = ctx->ds->GetDrawingSurfaceInfo(ctx->ds);
if (!dsi) {
rlawtThrow(env, "unable to get dsi");
return;
}
id<JAWT_SurfaceLayers> dspi = (id<JAWT_SurfaceLayers>) dsi->platformInfo;
if (!dspi) {
rlawtThrow(env, "unable to get platform dsi");
goto freeDSI;
}
CGLPixelFormatAttribute attribs[] = {
kCGLPFAColorSize, 24,
kCGLPFAAlphaSize, ctx->alphaDepth,
kCGLPFADepthSize, ctx->depthDepth,
kCGLPFAStencilSize, ctx->stencilDepth,
kCGLPFADoubleBuffer, true,
kCGLPFASampleBuffers, ctx->multisamples > 0,
kCGLPFASamples, ctx->multisamples,
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_GL4_Core,
0
};
CGLPixelFormatObj pxFmt = NULL;
int numPxFmt = 1;
CGLError err = CGLChoosePixelFormat(attribs, &pxFmt, &numPxFmt);
if (!pxFmt || err != kCGLNoError) {
rlawtThrowCGLError(env, "unable to choose format", err);
goto freeDSI;
}
err = CGLCreateContext(pxFmt, NULL, &ctx->context);
CGLReleasePixelFormat(pxFmt);
if (!ctx->context || err != kCGLNoError) {
rlawtThrowCGLError(env, "unable to create context", err);
goto freeDSI;
}
if (!makeCurrent(env, ctx->context)) {
goto freeContext;
}
glGenTextures(2, &ctx->tex[0]);
glGenFramebuffers(2, &ctx->fbo[0]);
dispatch_sync(dispatch_get_main_queue(), ^{
RLLayer *layer = [[RLLayer alloc] init];
layer.opaque = true;
layer.needsDisplayOnBoundsChange = false;
layer.magnificationFilter = kCAFilterNearest;
layer.contentsGravity = kCAGravityCenter;
layer.affineTransform = CGAffineTransformMakeScale(1, -1);
ctx->layer = layer;
dspi.layer = layer;
// must be after we give jawt the layer so our frame fix works
layer.frame = CGRectMake(
dsi->bounds.x + ctx->offsetX,
dspi.windowLayer.bounds.size.height - (dsi->bounds.y + ctx->offsetY) - dsi->bounds.height, // as per AWTSurfaceLayers::setBounds
dsi->bounds.width,
dsi->bounds.height);
});
if (!rlawtCreateIOSurface(env, ctx)) {
goto freeContext;
}
ctx->ds->FreeDrawingSurfaceInfo(dsi);
ctx->contextCreated = true;
return;
freeContext:
CGLDestroyContext(ctx->context);
freeDSI:
ctx->ds->FreeDrawingSurfaceInfo(dsi);
}
void rlawtContextFreePlatform(JNIEnv *env, AWTContext *ctx) {
CGLSetCurrentContext(NULL);
if (ctx->context) {
CGLDestroyContext(ctx->context);
}
if (ctx->buffer[0]) {
CFRelease(ctx->buffer[0]);
}
if (ctx->buffer[1]) {
CFRelease(ctx->buffer[1]);
}
if (ctx->layer) {
dispatch_sync(dispatch_get_main_queue(), ^{
[ctx->layer removeFromSuperlayer];
[ctx->layer release];
});
}
}
JNIEXPORT int JNICALL Java_net_runelite_rlawt_AWTContext_setSwapInterval(JNIEnv *env, jobject self, jint interval) {
return 0;
}
JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_makeCurrent(JNIEnv *env, jobject self) {
AWTContext *ctx = rlawtGetContext(env, self);
if (!ctx || !rlawtContextState(env, ctx, true)) {
return;
}
makeCurrent(env, ctx->context);
}
JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_detachCurrent(JNIEnv *env, jobject self) {
AWTContext *ctx = rlawtGetContext(env, self);
if (!ctx || !rlawtContextState(env, ctx, true)) {
return;
}
makeCurrent(env, NULL);
}
JNIEXPORT void JNICALL Java_net_runelite_rlawt_AWTContext_swapBuffers(JNIEnv *env, jobject self) {
AWTContext *ctx = rlawtGetContext(env, self);
if (!ctx || !rlawtContextState(env, ctx, true)) {
return;
}
glFlush();
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]) != (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;
}
}
}
JNIEXPORT jint JNICALL Java_net_runelite_rlawt_AWTContext_getFramebuffer(JNIEnv *env, jobject self, jboolean front) {
AWTContext *ctx = rlawtGetContext(env, self);
if (!ctx || !rlawtContextState(env, ctx, true)) {
return 0;
}
return ctx->fbo[ctx->back ^ front];
}
#endif