1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
3  * Copyright (c) 2010 JogAmp Community. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistribution of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistribution in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24  * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27  * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28  * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31  * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32  *
33  * You acknowledge that this software is not designed or intended for use
34  * in the design, construction, operation or maintenance of any nuclear
35  * facility.
36  *
37  * Sun gratefully acknowledges that this software was originally authored
38  * and developed by Kenneth Bradley Russell and Christopher John Kline.
39  */
40 
41 package jogamp.opengl.macosx.cgl;
42 
43 import com.jogamp.nativewindow.AbstractGraphicsScreen;
44 import com.jogamp.nativewindow.DefaultGraphicsScreen;
45 import com.jogamp.nativewindow.NativeSurface;
46 import com.jogamp.nativewindow.NativeWindowFactory;
47 import com.jogamp.opengl.GLCapabilitiesImmutable;
48 import com.jogamp.opengl.GLContext;
49 import com.jogamp.opengl.GLDrawableFactory;
50 import com.jogamp.opengl.GLException;
51 
52 import jogamp.nativewindow.WrappedSurface;
53 import jogamp.opengl.GLContextShareSet;
54 import jogamp.opengl.macosx.cgl.MacOSXCGLDrawable.GLBackendType;
55 
56 
57 public class MacOSXExternalCGLContext extends MacOSXCGLContext {
58 
MacOSXExternalCGLContext(final Drawable drawable, final boolean isNSContext, final long handle)59   private MacOSXExternalCGLContext(final Drawable drawable, final boolean isNSContext, final long handle) {
60     super(drawable, null);
61     setOpenGLMode(isNSContext ? GLBackendType.NSOPENGL : GLBackendType.CGL );
62     this.contextHandle = handle;
63     GLContextShareSet.contextCreated(this);
64     if( !setGLFunctionAvailability(false, 0, 0, CTX_PROFILE_COMPAT, false /* strictMatch */, false /* withinGLVersionsMapping */) ) { // use GL_VERSION
65         throw new InternalError("setGLFunctionAvailability !strictMatch failed");
66     }
67     getGLStateTracker().setEnabled(false); // external context usage can't track state in Java
68   }
69 
create(final GLDrawableFactory factory)70   protected static MacOSXExternalCGLContext create(final GLDrawableFactory factory) {
71     long pixelFormat = 0;
72     long currentDrawable = 0;
73     long contextHandle = CGL.getCurrentContext(); // Check: MacOSX 10.3 ..
74     final boolean isNSContext = 0 != contextHandle;
75     if( isNSContext ) {
76         final long ctx = CGL.getCGLContext(contextHandle);
77         if (ctx == 0) {
78           throw new GLException("Error: NULL Context (CGL) of Context (NS) 0x" +Long.toHexString(contextHandle));
79         }
80         pixelFormat = CGL.CGLGetPixelFormat(ctx);
81         currentDrawable = CGL.getNSView(contextHandle);
82         if(DEBUG) {
83             System.err.println("MacOSXExternalCGLContext Create Context (NS) 0x"+Long.toHexString(contextHandle)+
84                                ", Context (CGL) 0x"+Long.toHexString(ctx)+
85                                ", pixelFormat 0x"+Long.toHexString(pixelFormat)+
86                                ", drawable 0x"+Long.toHexString(currentDrawable));
87         }
88     } else {
89         contextHandle = CGL.CGLGetCurrentContext();
90         if (contextHandle == 0) {
91           throw new GLException("Error: current Context (CGL) null, no Context (NS)");
92         }
93         pixelFormat = CGL.CGLGetPixelFormat(contextHandle);
94         if(DEBUG) {
95             System.err.println("MacOSXExternalCGLContext Create Context (CGL) 0x"+Long.toHexString(contextHandle)+
96                                ", pixelFormat 0x"+Long.toHexString(pixelFormat));
97         }
98     }
99 
100     if (0 == pixelFormat) {
101       throw new GLException("Error: current pixelformat of current Context 0x"+Long.toHexString(contextHandle)+" is null");
102     }
103     final GLCapabilitiesImmutable caps = MacOSXCGLGraphicsConfiguration.CGLPixelFormat2GLCapabilities(pixelFormat);
104     if(DEBUG) {
105         System.err.println("MacOSXExternalCGLContext Create "+caps);
106     }
107 
108     final AbstractGraphicsScreen aScreen = DefaultGraphicsScreen.createDefault(NativeWindowFactory.TYPE_MACOSX);
109     final MacOSXCGLGraphicsConfiguration cfg = new MacOSXCGLGraphicsConfiguration(aScreen, caps, caps);
110 
111     if(0 == currentDrawable) {
112         // set a fake marker stating a valid drawable
113         currentDrawable = 1;
114     }
115     final WrappedSurface ns = new WrappedSurface(cfg, currentDrawable, 64, 64, true);
116     return new MacOSXExternalCGLContext(new Drawable(factory, ns), isNSContext, contextHandle);
117   }
118 
119   @Override
createImpl(final long shareWithHandle)120   protected boolean createImpl(final long shareWithHandle) throws GLException {
121       return true;
122   }
123 
124   @Override
makeCurrentImpl()125   protected void makeCurrentImpl() throws GLException {
126   }
127 
128   @Override
releaseImpl()129   protected void releaseImpl() throws GLException {
130   }
131 
132   @Override
destroyImpl()133   protected void destroyImpl() throws GLException {
134   }
135 
136   // Need to provide the display connection to extension querying APIs
137   static class Drawable extends MacOSXCGLDrawable {
Drawable(final GLDrawableFactory factory, final NativeSurface comp)138     Drawable(final GLDrawableFactory factory, final NativeSurface comp) {
139       super(factory, comp, true);
140     }
141 
142     @Override
createContext(final GLContext shareWith)143     public GLContext createContext(final GLContext shareWith) {
144       throw new GLException("Should not call this");
145     }
146 
147     @Override
getSurfaceWidth()148     public int getSurfaceWidth() {
149       throw new GLException("Should not call this");
150     }
151 
152     @Override
getSurfaceHeight()153     public int getSurfaceHeight() {
154       throw new GLException("Should not call this");
155     }
156 
setSize(final int width, final int height)157     public void setSize(final int width, final int height) {
158       throw new GLException("Should not call this");
159     }
160   }
161 }
162