Set CALayer OpenGL Context to be shared with the current LWJGL context.
Make code OS X 10.5 compatible (change from NSOpenGLLayer to CAOpenGLLayer)
This commit is contained in:
parent
f13f35d250
commit
790f895220
|
@ -46,6 +46,7 @@
|
|||
#include <OpenGL/gl.h>
|
||||
#include <OpenGL/glext.h>
|
||||
#include "common_tools.h"
|
||||
#include <jawt_md.h>
|
||||
|
||||
@class NSOpenGLContext, NSOpenGLPixelFormat, MacOSXOpenGLView, MacOSXKeyableWindow;
|
||||
|
||||
|
@ -94,15 +95,29 @@ typedef struct {
|
|||
- (BOOL)acceptsFirstResponder;
|
||||
@end
|
||||
|
||||
@interface GLLayer : CAOpenGLLayer {
|
||||
@public
|
||||
JAWT_MacOSXDrawingSurfaceInfo *macosx_dsi;
|
||||
MacOSXWindowInfo *window_info;
|
||||
|
||||
@private
|
||||
CGLContextObj contextObject;
|
||||
}
|
||||
|
||||
- (void) attachLayer;
|
||||
- (void) removeLayer;
|
||||
|
||||
@end
|
||||
|
||||
typedef struct {
|
||||
bool isCALayer;
|
||||
bool isWindowed;
|
||||
bool canDrawGL;
|
||||
MacOSXWindowInfo *window_info;
|
||||
NSOpenGLPixelFormat *pixel_format;
|
||||
NSOpenGLPixelBuffer *pbuffer;
|
||||
NSView *parent;
|
||||
GLLayer *glLayer;
|
||||
} MacOSXPeerInfo;
|
||||
|
||||
|
||||
NSOpenGLPixelFormat *choosePixelFormat(JNIEnv *env, jobject pixel_format, bool gl32, bool use_display_bpp, bool support_window, bool support_pbuffer, bool double_buffered);
|
||||
#endif
|
||||
|
|
|
@ -46,17 +46,6 @@
|
|||
#include "context.h"
|
||||
#include "common_tools.h"
|
||||
|
||||
|
||||
@interface GLLayer : NSOpenGLLayer {
|
||||
MacOSXPeerInfo *peer_info;
|
||||
}
|
||||
- (void) attachLayer: (id<JAWT_SurfaceLayers>)surfaceLayers;
|
||||
- (void) removeLayer: (id<JAWT_SurfaceLayers>)surfaceLayers;
|
||||
|
||||
- (MacOSXPeerInfo*) peer_info;
|
||||
- (void) setPeer_info: (MacOSXPeerInfo*)input;
|
||||
@end
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
||||
(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject peer_info_handle) {
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
@ -69,13 +58,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
|||
if(surface->awt.version & 0x80000000) { //JAWT_MACOSX_USE_CALAYER) {
|
||||
|
||||
if (macosx_dsi != NULL) {
|
||||
// get the root layer of the AWT Canvas
|
||||
id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)macosx_dsi;
|
||||
GLLayer *glLayer = [[GLLayer new] autorelease];
|
||||
[glLayer performSelectorOnMainThread:@selector(attachLayer:) withObject:surfaceLayers waitUntilDone:YES];
|
||||
[glLayer setPeer_info:peer_info];
|
||||
peer_info->glLayer = [GLLayer new];
|
||||
peer_info->glLayer->macosx_dsi = macosx_dsi;
|
||||
peer_info->glLayer->window_info = peer_info->window_info;
|
||||
}
|
||||
|
||||
peer_info->isCALayer = true;
|
||||
peer_info->isWindowed = true;
|
||||
peer_info->parent = nil;
|
||||
|
||||
|
@ -85,6 +73,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
|||
|
||||
// no CALayer support, fallback to using legacy method of getting the NSView of an AWT Canvas
|
||||
peer_info->parent = macosx_dsi->cocoaViewRef;
|
||||
peer_info->isCALayer = false;
|
||||
peer_info->isWindowed = true;
|
||||
|
||||
[pool release];
|
||||
|
@ -92,35 +81,28 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
|||
|
||||
@implementation GLLayer
|
||||
|
||||
- (void) attachLayer:(id<JAWT_SurfaceLayers>)surfaceLayers {
|
||||
- (void) attachLayer {
|
||||
self.asynchronous = YES;
|
||||
self.needsDisplayOnBoundsChange = YES;
|
||||
self.opaque = NO;
|
||||
self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
|
||||
|
||||
// get root layer of the AWT Canvas and add self to it
|
||||
id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)macosx_dsi;
|
||||
surfaceLayers.layer = self;
|
||||
}
|
||||
|
||||
- (void) removeLayer:(id<JAWT_SurfaceLayers>)surfaceLayers {
|
||||
- (void) removeLayer {
|
||||
// remove self from root layer
|
||||
id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)macosx_dsi;
|
||||
surfaceLayers.layer = nil;
|
||||
}
|
||||
|
||||
- (MacOSXPeerInfo*) peer_info {
|
||||
return peer_info;
|
||||
}
|
||||
|
||||
- (void) setPeer_info: (MacOSXPeerInfo*)input {
|
||||
peer_info = input;
|
||||
}
|
||||
|
||||
-(void)drawInCGLContext:(CGLContextObj)glContext
|
||||
pixelFormat:(CGLPixelFormatObj)pixelFormat
|
||||
forLayerTime:(CFTimeInterval)timeInterval
|
||||
displayTime:(const CVTimeStamp *)timeStamp {
|
||||
|
||||
if(!peer_info) {
|
||||
return;
|
||||
}
|
||||
|
||||
// set the current context
|
||||
CGLSetCurrentContext(glContext);
|
||||
|
||||
|
@ -133,16 +115,18 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
|||
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &originalReadFBO);
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &originalDrawFBO);
|
||||
|
||||
/*glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// copy/blit the LWJGL FBO to this CALayers FBO
|
||||
// TODO
|
||||
/*glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, lwjglFBO);
|
||||
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 1);//lwjglFBO);
|
||||
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, originalFBO);
|
||||
|
||||
glBlitFramebufferEXT(0, 0, width, height,
|
||||
0, 0, width, height,
|
||||
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
|
||||
GL_NEAREST);*/
|
||||
|
||||
|
||||
glBlitFramebufferEXT(0, 0, 640, 480,//width, height,
|
||||
0, 0, 640, 480,//width, height,
|
||||
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
|
||||
GL_NEAREST);*/
|
||||
|
||||
// for testing, draw a single yellow quad spinning around based on the current time
|
||||
GLfloat rotate = timeInterval * 60.0; // 60 degrees per second
|
||||
|
@ -173,7 +157,24 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXCanvasPeerInfo_nInitHandle
|
|||
pixelFormat:(CGLPixelFormatObj)pixelFormat
|
||||
forLayerTime:(CFTimeInterval)timeInterval
|
||||
displayTime:(const CVTimeStamp *)timeStamp {
|
||||
return YES;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
|
||||
CGLCreateContext(pixelFormat, [window_info->context CGLContextObj], &contextObject);
|
||||
return contextObject;
|
||||
}
|
||||
|
||||
- (void)releaseCGLContext:(CGLContextObj)glContext {
|
||||
CGLDestroyContext(contextObject);
|
||||
}
|
||||
|
||||
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
|
||||
return CGLGetPixelFormat([window_info->context CGLContextObj]);
|
||||
}
|
||||
|
||||
- (void)releaseCGLPixelFormat:(CGLPixelFormatObj)pixelFormat {
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -71,10 +71,10 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre
|
|||
shared_context = shared_context_info->context;
|
||||
}
|
||||
context = [[NSOpenGLContext alloc] initWithFormat:peer_info->pixel_format shareContext:shared_context];
|
||||
if (context == NULL) {
|
||||
throwException(env, "Could not create context");
|
||||
return NULL;
|
||||
}
|
||||
if (context == NULL) {
|
||||
throwException(env, "Could not create context");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (peer_info->window_info->fullscreen) {
|
||||
// set a fixed backbuffer size for fullscreen
|
||||
|
@ -90,12 +90,12 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nCre
|
|||
CGLDisable(cgcontext, kCGLCESurfaceBackingSize);
|
||||
}
|
||||
|
||||
[peer_info->window_info->view setOpenGLContext:context];
|
||||
peer_info->window_info->context = context;
|
||||
[peer_info->window_info->view setOpenGLContext:context];
|
||||
peer_info->window_info->context = context;
|
||||
context_info = (MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle);
|
||||
context_info->context = context;
|
||||
context_info->peer_info = peer_info;
|
||||
|
||||
context_info->context = context;
|
||||
context_info->peer_info = peer_info;
|
||||
|
||||
[pool release];
|
||||
return context_handle;
|
||||
}
|
||||
|
@ -115,8 +115,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nSwapBu
|
|||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
|
||||
[[peer_info->window_info->view openGLContext] flushBuffer];
|
||||
peer_info->canDrawGL = true;
|
||||
[pool release];
|
||||
[pool release];
|
||||
}
|
||||
|
||||
|
||||
|
@ -125,8 +124,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nUpdate
|
|||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
MacOSXPeerInfo *peer_info = ((MacOSXContext *)(*env)->GetDirectBufferAddress(env, context_handle))->peer_info;
|
||||
[[peer_info->window_info->view openGLContext] update];
|
||||
peer_info->canDrawGL = true;
|
||||
[pool release];
|
||||
[pool release];
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_clearDrawable
|
||||
|
@ -147,9 +145,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_nReleas
|
|||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXContextImplementation_setView
|
||||
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
|
||||
[[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view];
|
||||
peer_info->canDrawGL = true;
|
||||
MacOSXPeerInfo *peer_info = (MacOSXPeerInfo *)(*env)->GetDirectBufferAddress(env, peer_info_handle);
|
||||
[[peer_info->window_info->view openGLContext] setView: peer_info->window_info->view];
|
||||
|
||||
if (peer_info->isCALayer) {
|
||||
// if using a CALayer, attach it to AWT Canvas and create a shared opengl context with current context
|
||||
[peer_info->glLayer performSelectorOnMainThread:@selector(attachLayer) withObject:nil waitUntilDone:NO];
|
||||
}
|
||||
|
||||
[pool release];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue