1 /*
2  * Copyright (c) 2003 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.windows.wgl;
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.GLCapabilities;
48 import com.jogamp.opengl.GLContext;
49 import com.jogamp.opengl.GLDrawableFactory;
50 import com.jogamp.opengl.GLException;
51 import com.jogamp.opengl.GLProfile;
52 
53 import jogamp.nativewindow.WrappedSurface;
54 import jogamp.nativewindow.windows.GDI;
55 import jogamp.opengl.GLContextShareSet;
56 
57 public class WindowsExternalWGLContext extends WindowsWGLContext {
58 
WindowsExternalWGLContext(final Drawable drawable, final long ctx, final WindowsWGLGraphicsConfiguration cfg)59   private WindowsExternalWGLContext(final Drawable drawable, final long ctx, final WindowsWGLGraphicsConfiguration cfg) {
60     super(drawable, null);
61     this.contextHandle = ctx;
62     if (DEBUG) {
63       System.err.println(getThreadName() + ": Created external OpenGL context " + toHexString(ctx) + " for " + this);
64     }
65     GLContextShareSet.contextCreated(this);
66     if( !setGLFunctionAvailability(false, 0, 0, CTX_PROFILE_COMPAT, false /* strictMatch */, false /* withinGLVersionsMapping */) ) { // use GL_VERSION
67         throw new InternalError("setGLFunctionAvailability !strictMatch failed");
68     }
69     getGLStateTracker().setEnabled(false); // external context usage can't track state in Java
70   }
71 
create(final GLDrawableFactory factory, final GLProfile glp)72   protected static WindowsExternalWGLContext create(final GLDrawableFactory factory, final GLProfile glp) {
73     if(DEBUG) {
74         System.err.println("WindowsExternalWGLContext 0: werr: " + GDI.GetLastError());
75     }
76 
77     final long ctx = WGL.wglGetCurrentContext();
78     if (0 == ctx) {
79       throw new GLException("Error: attempted to make an external GLContext without a context current, werr " + GDI.GetLastError());
80     }
81 
82     final long hdc = WGL.wglGetCurrentDC();
83     if (0 == hdc) {
84       throw new GLException("Error: attempted to make an external GLDrawable without a drawable current, werr " + GDI.GetLastError());
85     }
86     final AbstractGraphicsScreen aScreen = DefaultGraphicsScreen.createDefault(NativeWindowFactory.TYPE_WINDOWS);
87     WindowsWGLGraphicsConfiguration cfg;
88     final int pfdID = WGLUtil.GetPixelFormat(hdc);
89     if (0 == pfdID) {
90         // This could have happened if the HDC was released right after the GL ctx made current (SWT),
91         // WinXP-32bit will not be able to use this HDC afterwards.
92         // Workaround: Use a fake default configuration
93         final int werr = GDI.GetLastError();
94         cfg = WindowsWGLGraphicsConfigurationFactory.createDefaultGraphicsConfiguration(new GLCapabilities(GLProfile.getDefault()), aScreen);
95         cfg.markExternal();
96         if(DEBUG) {
97             System.err.println("WindowsExternalWGLContext invalid hdc/pfd werr "+werr+", using default cfg: " + cfg);
98         }
99     } else {
100         cfg = WindowsWGLGraphicsConfiguration.createFromExternal(factory, hdc, pfdID, glp, aScreen, true);
101         if(DEBUG) {
102             System.err.println("WindowsExternalWGLContext valid hdc/pfd, retrieved cfg: " + cfg);
103         }
104     }
105     return new WindowsExternalWGLContext(new Drawable(factory, new WrappedSurface(cfg, hdc, 64, 64, true)), ctx, cfg);
106   }
107 
108   @Override
makeCurrentImpl()109   protected void makeCurrentImpl() throws GLException {
110   }
111 
112   @Override
releaseImpl()113   protected void releaseImpl() throws GLException {
114   }
115 
116   @Override
destroyImpl()117   protected void destroyImpl() throws GLException {
118   }
119 
120   // Need to provide the display connection to extension querying APIs
121   static class Drawable extends WindowsWGLDrawable {
Drawable(final GLDrawableFactory factory, final NativeSurface comp)122     Drawable(final GLDrawableFactory factory, final NativeSurface comp) {
123       super(factory, comp, true);
124     }
125 
126     @Override
createContext(final GLContext shareWith)127     public GLContext createContext(final GLContext shareWith) {
128       throw new GLException("Should not call this");
129     }
130 
131     @Override
getSurfaceWidth()132     public int getSurfaceWidth() {
133       throw new GLException("Should not call this");
134     }
135 
136     @Override
getSurfaceHeight()137     public int getSurfaceHeight() {
138       throw new GLException("Should not call this");
139     }
140 
setSize(final int width, final int height)141     public void setSize(final int width, final int height) {
142       throw new GLException("Should not call this");
143     }
144   }
145 }
146