From 808c8da0729b845d010d3f5e0babf1fc6129c3e9 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 4 Nov 2012 06:42:43 +0100 Subject: MacOSXCGLContext[NSOpenGLLayer/NSView]: Propagate drawable change Propagate drawable change to MacOSXCGLContext where either context/NSView or context/NSOpenGLLayer association needs to get updated. Fixes drawable/context switch. --- .../jogamp/opengl/macosx/cgl/MacOSXCGLContext.java | 75 ++++++++++++++++------ .../opengl/macosx/cgl/MacOSXCGLDrawable.java | 4 +- .../macosx/MacOSXWindowSystemInterface-calayer.m | 1 + .../native/macosx/MacOSXWindowSystemInterface.m | 13 +++- 4 files changed, 68 insertions(+), 25 deletions(-) (limited to 'src/jogl') diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java index 360b7457b..1c59b8d97 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java @@ -84,6 +84,7 @@ public abstract class MacOSXCGLContext extends GLContextImpl // NSOpenGL-based or CGL-based) protected interface GLBackendImpl { boolean isNSContext(); + void drawableChangedNotify(); long create(long share, int ctp, int major, int minor); boolean destroy(long ctx); boolean contextRealized(boolean realized); @@ -336,7 +337,13 @@ public abstract class MacOSXCGLContext extends GLContextImpl super.contextRealized(false); // 2) free drawable stuff } } - + + /* pp */ void drawableChangedNotify() { + if( 0 != contextHandle) { + impl.drawableChangedNotify(); + } + } + /* pp */ void detachPBuffer() { impl.detachPBuffer(); } @@ -475,7 +482,7 @@ public abstract class MacOSXCGLContext extends GLContextImpl class NSOpenGLImpl implements GLBackendImpl { private OffscreenLayerSurface backingLayerHost = null; private long nsOpenGLLayer = 0; - private long nsOpenGLLayerPFmt = 0; + private long nsOpenGLLayerPFmt = 0; // lifecycle: [create - contextRealized] private float screenVSyncTimeout; // microSec private int vsyncTimeout; // microSec - for nsOpenGLLayer mode private int lastWidth=0, lastHeight=0; // allowing to detect size change @@ -486,26 +493,31 @@ public abstract class MacOSXCGLContext extends GLContextImpl public boolean isNSContext() { return true; } @Override - public long create(long share, int ctp, int major, int minor) { - long ctx = 0; - final NativeSurface surface = drawable.getNativeSurface(); - final MacOSXCGLGraphicsConfiguration config = (MacOSXCGLGraphicsConfiguration) surface.getGraphicsConfiguration(); - final GLCapabilitiesImmutable chosenCaps = (GLCapabilitiesImmutable) config.getChosenCapabilities(); + public void drawableChangedNotify() { + backingLayerHost = NativeWindowFactory.getOffscreenLayerSurface(drawable.getNativeSurface(), true); + if( null == backingLayerHost ) { + boolean[] isPBuffer = { false }; + boolean[] isFBO = { false }; + CGL.setContextView(contextHandle, getNSViewHandle(isPBuffer, isFBO)); + } else { + nsOpenGLLayer = backingLayerHost.getAttachedSurfaceLayer(); + } + } + + private long getNSViewHandle(boolean[] isPBuffer, boolean[] isFBO) { final long nsViewHandle; - final boolean isPBuffer; - final boolean isFBO; if(drawable instanceof GLFBODrawableImpl) { nsViewHandle = 0; - isPBuffer = false; - isFBO = true; + isPBuffer[0] = false; + isFBO[0] = true; if(DEBUG) { System.err.println("NS create GLFBODrawableImpl drawable: isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); } } else if(drawable instanceof MacOSXCGLDrawable) { // we allow null here! (special pbuffer case) nsViewHandle = ((MacOSXCGLDrawable)MacOSXCGLContext.this.drawable).getNSViewHandle(); - isPBuffer = CGL.isNSOpenGLPixelBuffer(drawable.getHandle()); - isFBO = false; + isPBuffer[0] = CGL.isNSOpenGLPixelBuffer(drawable.getHandle()); + isFBO[0] = false; if(DEBUG) { System.err.println("NS create MacOSXCGLDrawable drawable handle isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); } @@ -514,8 +526,8 @@ public abstract class MacOSXCGLContext extends GLContextImpl final long drawableHandle = drawable.getHandle(); final boolean isNSView = OSXUtil.isNSView(drawableHandle); final boolean isNSWindow = OSXUtil.isNSWindow(drawableHandle); - isPBuffer = CGL.isNSOpenGLPixelBuffer(drawableHandle); - isFBO = false; + isPBuffer[0] = CGL.isNSOpenGLPixelBuffer(drawableHandle); + isFBO[0] = false; if(DEBUG) { System.err.println("NS create Anonymous drawable handle "+toHexString(drawableHandle)+": isNSView "+isNSView+", isNSWindow "+isNSWindow+", isFBO "+isFBO+", isPBuffer "+isPBuffer+", "+drawable.getClass().getName()+",\n\t"+drawable); @@ -524,13 +536,32 @@ public abstract class MacOSXCGLContext extends GLContextImpl nsViewHandle = drawableHandle; } else if( isNSWindow ) { nsViewHandle = OSXUtil.GetNSView(drawableHandle); - } else if( isPBuffer ) { + } else if( isPBuffer[0] ) { nsViewHandle = 0; } else { throw new RuntimeException("Anonymous drawable instance's handle neither NSView, NSWindow nor PBuffer: "+toHexString(drawableHandle)+", "+drawable.getClass().getName()+",\n\t"+drawable); } } - needsSetContextPBuffer = isPBuffer; + needsSetContextPBuffer = isPBuffer[0]; + return nsViewHandle; + } + + @Override + public long create(long share, int ctp, int major, int minor) { + long ctx = 0; + final NativeSurface surface = drawable.getNativeSurface(); + final MacOSXCGLGraphicsConfiguration config = (MacOSXCGLGraphicsConfiguration) surface.getGraphicsConfiguration(); + final GLCapabilitiesImmutable chosenCaps = (GLCapabilitiesImmutable) config.getChosenCapabilities(); + final long nsViewHandle; + final boolean isPBuffer; + final boolean isFBO; + { + boolean[] _isPBuffer = { false }; + boolean[] _isFBO = { false }; + nsViewHandle = getNSViewHandle(_isPBuffer, _isFBO); + isPBuffer = _isPBuffer[0]; + isFBO = _isFBO[0]; + } backingLayerHost = NativeWindowFactory.getOffscreenLayerSurface(surface, true); boolean incompleteView = null != backingLayerHost; @@ -661,6 +692,7 @@ public abstract class MacOSXCGLContext extends GLContextImpl gl3ShaderProgramName = 0; } nsOpenGLLayer = CGL.createNSOpenGLLayer(ctx, gl3ShaderProgramName, nsOpenGLLayerPFmt, pbufferHandle, texID, chosenCaps.isBackgroundOpaque(), lastWidth, lastHeight); + nsOpenGLLayerPFmt = 0; // NSOpenGLLayer will release pfmt if (DEBUG) { System.err.println("NS create nsOpenGLLayer "+toHexString(nsOpenGLLayer)+" w/ pbuffer "+toHexString(pbufferHandle)+", texID "+texID+", texSize "+lastWidth+"x"+lastHeight+", "+drawable); } @@ -688,10 +720,6 @@ public abstract class MacOSXCGLContext extends GLContextImpl } nsOpenGLLayer = 0; } - if(0 != nsOpenGLLayerPFmt) { - CGL.deletePixelFormat(nsOpenGLLayerPFmt); - nsOpenGLLayerPFmt = 0; - } } backingLayerHost = null; return true; @@ -841,6 +869,11 @@ public abstract class MacOSXCGLContext extends GLContextImpl @Override public boolean isNSContext() { return false; } + @Override + public void drawableChangedNotify() { + // FIXME + } + @Override public long create(long share, int ctp, int major, int minor) { long ctx = 0; diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java index cc727c8e1..ff1772860 100644 --- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java +++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawable.java @@ -117,7 +117,9 @@ public abstract class MacOSXCGLDrawable extends GLDrawableImpl { // OpenGL window interface synchronized (createdContexts) { if(bound) { - createdContexts.add(new WeakReference((MacOSXCGLContext)ctx)); + final MacOSXCGLContext osxCtx = (MacOSXCGLContext)ctx; + createdContexts.add(new WeakReference(osxCtx)); + osxCtx.drawableChangedNotify(); } else { for(int i=0; i ref = createdContexts.get(i); diff --git a/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m b/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m index 6ebf400e2..b37930587 100644 --- a/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m +++ b/src/jogl/native/macosx/MacOSXWindowSystemInterface-calayer.m @@ -448,6 +448,7 @@ static const GLfloat gl_verts[] = { [self disableAnimation]; [self deallocPBuffer]; [[self openGLContext] release]; + [parentPixelFmt release]; [self release]; DBG_PRINT("MyNSOpenGLLayer::releaseLayer.X: %p (refcnt %d)\n", self, (int)[self retainCount]); pthread_mutex_unlock(&renderLock); diff --git a/src/jogl/native/macosx/MacOSXWindowSystemInterface.m b/src/jogl/native/macosx/MacOSXWindowSystemInterface.m index 48807ee29..e8925f8e8 100644 --- a/src/jogl/native/macosx/MacOSXWindowSystemInterface.m +++ b/src/jogl/native/macosx/MacOSXWindowSystemInterface.m @@ -551,8 +551,7 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, NSOpenGLContext* ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:share]; - if (ctx != nil) { - if (view != nil) { + if ( nil != ctx && nil != view ) { if(!opaque) { GLint zeroOpacity = 0; [ctx setValues:&zeroOpacity forParameter:NSOpenGLCPSurfaceOpacity]; @@ -563,7 +562,6 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, DBG_PRINT("createContext.3.X: setView\n"); [view unlockFocus]; } - } } DBG_PRINT("createContext.X: ctx: %p\n", ctx); @@ -571,6 +569,15 @@ NSOpenGLContext* createContext(NSOpenGLContext* share, return ctx; } +void setContextView(NSOpenGLContext* ctx, NSView* view) { + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + if ( nil != ctx && nil != view ) { + [ctx setView:view]; + } + [pool release]; + return ctx; +} + Bool makeCurrentContext(NSOpenGLContext* ctx) { #if 0 // we issue the CGL Lock from Java upfront! -- cgit v1.2.3