aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/com/sun/opengl/impl/macosx/MacOSXPbufferGLContext.java
blob: c4e3058daa7baf132ddf325960ef9eabb84b4410 (plain)
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
332
333
334
335
336
337
338
339
340
package com.sun.opengl.impl.macosx;

import java.security.*;
import java.util.*;

import javax.media.opengl.*;
import com.sun.opengl.impl.*;

public class MacOSXPbufferGLContext extends MacOSXGLContext {
  protected MacOSXPbufferGLDrawable drawable;

  // State for render-to-texture and render-to-texture-rectangle support
  private int textureTarget; // e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_NV
  private int texture;       // actual texture object

  private static boolean isTigerOrLater;

  static {
    String osVersion =
      (String) AccessController.doPrivileged(new PrivilegedAction() {
	  public Object run() {
	    return System.getProperty("os.version");
	  }
	});
    StringTokenizer tok = new StringTokenizer(osVersion, ". ");
    int major = Integer.parseInt(tok.nextToken());
    int minor = Integer.parseInt(tok.nextToken());
    isTigerOrLater = ((major > 10) || (minor > 3));
  }

  public MacOSXPbufferGLContext(MacOSXPbufferGLDrawable drawable,
                                GLContext shareWith) {
    super(drawable, shareWith);
    this.drawable = drawable;
    initOpenGLImpl();
  }

  public void bindPbufferToTexture() {
    GL gl = getGL();
    gl.glBindTexture(textureTarget, texture);
    // FIXME: not clear whether this is really necessary, but since
    // the API docs seem to imply it is and since it doesn't seem to
    // impact performance, leaving it in
    CGL.setContextTextureImageToPBuffer(nsContext, drawable.getPbuffer(), GL.GL_FRONT);
  }

  public void releasePbufferFromTexture() {
  }

  protected int makeCurrentImpl() throws GLException {
    if (drawable.getPbuffer() == 0) {
      if (DEBUG) {
        System.err.println("Pbuffer not instantiated yet for " + this);
      }
      // pbuffer not instantiated yet
      return CONTEXT_NOT_CURRENT;
    }

    if (getOpenGLMode() != drawable.getOpenGLMode()) {
      setOpenGLMode(drawable.getOpenGLMode());
    }

    boolean created = false;
    if (nsContext == 0) {
      if (!create()) {
        return CONTEXT_NOT_CURRENT;
      }
      if (DEBUG) {
        System.err.println("!!! Created OpenGL context " + toHexString(nsContext) + " for " + getClass().getName());
      }
      created = true;
    }
    
    if (!impl.makeCurrent(nsContext)) {
      throw new GLException("Error making nsContext current");
    }
            
    if (created) {
      resetGLFunctionAvailability();

      // Initialize render-to-texture support if requested
      boolean rect = drawable.getCapabilities().getPbufferRenderToTextureRectangle();
      GL gl = getGL();
      if (rect) {
        if (!gl.isExtensionAvailable("GL_EXT_texture_rectangle")) {
          System.err.println("MacOSXPbufferGLContext: WARNING: GL_EXT_texture_rectangle extension not " +
                             "supported; skipping requested render_to_texture_rectangle support for pbuffer");
          rect = false;
        }
      }
      textureTarget = (rect ? GL.GL_TEXTURE_RECTANGLE_EXT : GL.GL_TEXTURE_2D);
      int[] tmp = new int[1];
      gl.glGenTextures(1, tmp, 0);
      texture = tmp[0];
      gl.glBindTexture(textureTarget, texture);
      gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
      gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
      gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
      gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
      gl.glCopyTexImage2D(textureTarget, 0, GL.GL_RGB, 0, 0, drawable.getWidth(), drawable.getHeight(), 0);

      return CONTEXT_CURRENT_NEW;
    }
    return CONTEXT_CURRENT;
  }

  protected void releaseImpl() throws GLException {
    if (!impl.release(nsContext)) {
      throw new GLException("Error releasing OpenGL nsContext");
    }
  }

  protected void destroyImpl() throws GLException {
    if (nsContext != 0) {
      if (!impl.destroy(nsContext)) {
        throw new GLException("Unable to delete OpenGL context");
      }
      if (DEBUG) {
        System.err.println("!!! Destroyed OpenGL context " + nsContext);
      }
      nsContext = 0;
      GLContextShareSet.contextDestroyed(this);
    }
  }

  public void setSwapInterval(int interval) {
    if (nsContext == 0) {
      throw new GLException("OpenGL context not current");
    }
    impl.setSwapInterval(nsContext, interval);
  }

  public int getFloatingPointMode() {
    return GLPbuffer.APPLE_FLOAT;
  }

  protected boolean create() {
    GLCapabilities capabilities = drawable.getCapabilities();
    if (capabilities.getPbufferFloatingPointBuffers() &&
	!isTigerOrLater) {
      throw new GLException("Floating-point pbuffers supported only on OS X 10.4 or later");
    }
    // Change our OpenGL mode to match that of any share context before we create ourselves
    MacOSXGLContext other = (MacOSXGLContext) GLContextShareSet.getShareContext(this);
    if (other != null) {
      setOpenGLMode(other.getOpenGLMode());
    }
    // Will throw exception upon error
    nsContext = impl.create();
    return true;
  }

  //---------------------------------------------------------------------------
  // OpenGL "mode switching" functionality
  //
  private boolean haveSetOpenGLMode = false;
  // FIXME: should consider switching the default mode based on
  // whether the Java2D/JOGL bridge is active -- need to ask ourselves
  // whether it's more likely that we will share with a GLCanvas or a
  // GLJPanel when the bridge is turned on
  private int     openGLMode = MacOSXGLDrawable.NSOPENGL_MODE;
  // Implementation object (either NSOpenGL-based or CGL-based)
  protected Impl impl;

  public void setOpenGLMode(int mode) {
    if (mode == openGLMode) {
      return;
    }
    if (haveSetOpenGLMode) {
      throw new GLException("Can't switch between using NSOpenGLPixelBuffer and CGLPBufferObj more than once");
    }
    destroyImpl();
    drawable.setOpenGLMode(mode);
    openGLMode = mode;
    haveSetOpenGLMode = true;
    if (DEBUG) {
      System.err.println("Switching PBuffer context mode to " +
                         ((mode == MacOSXGLDrawable.NSOPENGL_MODE) ? "NSOPENGL_MODE" : "CGL_MODE"));
    }
    initOpenGLImpl();
  }

  public int  getOpenGLMode() {
    return openGLMode;
  }

  private void initOpenGLImpl() {
    switch (openGLMode) {
      case MacOSXGLDrawable.NSOPENGL_MODE:
        impl = new NSOpenGLImpl();
        break;
      case MacOSXGLDrawable.CGL_MODE:
        impl = new CGLImpl();
        break;
      default:
        throw new InternalError("Illegal implementation mode " + openGLMode);
    }
  }

  // Abstract interface for implementation of this context (either
  // NSOpenGL-based or CGL-based)
  interface Impl {
    public long    create();
    public boolean destroy(long ctx);
    public boolean makeCurrent(long ctx);
    public boolean release(long ctx);
    public void    setSwapInterval(long ctx, int interval);
  }

  // NSOpenGLContext-based implementation
  class NSOpenGLImpl implements Impl {
    public long create() {
      GLCapabilities capabilities = drawable.getCapabilities();
      if (capabilities.getPbufferFloatingPointBuffers() &&
          !isTigerOrLater) {
        throw new GLException("Floating-point pbuffers supported only on OS X 10.4 or later");
      }
      if (!MacOSXPbufferGLContext.this.create(true, capabilities.getPbufferFloatingPointBuffers())) {
        throw new GLException("Error creating context for pbuffer");
      }
      // Must now associate the pbuffer with our newly-created context
      CGL.setContextPBuffer(nsContext, drawable.getPbuffer());
      return nsContext;
    }

    public boolean destroy(long ctx) {
      return CGL.deleteContext(ctx);
    }

    public boolean makeCurrent(long ctx) {
      return CGL.makeCurrentContext(ctx);
    }

    public boolean release(long ctx) {
      return CGL.clearCurrentContext(ctx);
    }

    public void setSwapInterval(long ctx, int interval) {
      CGL.setSwapInterval(ctx, interval);      
    }
  }

  class CGLImpl implements Impl {
    public long create() {
      // Find and configure share context
      MacOSXGLContext other = (MacOSXGLContext) GLContextShareSet.getShareContext(MacOSXPbufferGLContext.this);
      long share = 0;
      if (other != null) {
        // Reconfigure pbuffer-based GLContexts
        if (other instanceof MacOSXPbufferGLContext) {
          MacOSXPbufferGLContext ctx = (MacOSXPbufferGLContext) other;
          ctx.setOpenGLMode(MacOSXGLDrawable.CGL_MODE);
        } else {
          if (other.getOpenGLMode() != MacOSXGLDrawable.CGL_MODE) {
            throw new GLException("Can't share between NSOpenGLContexts and CGLContextObjs");
          }
        }
        share = other.getNSContext();
        // Note we don't check for a 0 return value, since switching
        // the context's mode causes it to be destroyed and not
        // re-initialized until the next makeCurrent
      }

      // Set up pixel format attributes
      int[] attrs = new int[256];
      int i = 0;
      attrs[i++] = CGL.kCGLPFAPBuffer;
      GLCapabilities capabilities = drawable.getCapabilities();
      if (capabilities.getPbufferFloatingPointBuffers())
        attrs[i++] = CGL.kCGLPFAColorFloat;
      if (capabilities.getDoubleBuffered())
        attrs[i++] = CGL.kCGLPFADoubleBuffer;
      if (capabilities.getStereo())
        attrs[i++] = CGL.kCGLPFAStereo;
      attrs[i++] = CGL.kCGLPFAColorSize;
      attrs[i++] = (capabilities.getRedBits() +
                    capabilities.getGreenBits() +
                    capabilities.getBlueBits());
      attrs[i++] = CGL.kCGLPFAAlphaSize;
      attrs[i++] = capabilities.getAlphaBits();
      attrs[i++] = CGL.kCGLPFADepthSize;
      attrs[i++] = capabilities.getDepthBits();
      // FIXME: should validate stencil size as is done in MacOSXWindowSystemInterface.m
      attrs[i++] = CGL.kCGLPFAStencilSize;
      attrs[i++] = capabilities.getStencilBits();
      attrs[i++] = CGL.kCGLPFAAccumSize;
      attrs[i++] = (capabilities.getAccumRedBits() +
                    capabilities.getAccumGreenBits() +
                    capabilities.getAccumBlueBits() +
                    capabilities.getAccumAlphaBits());
      if (capabilities.getSampleBuffers()) {
        attrs[i++] = CGL.kCGLPFASampleBuffers;
        attrs[i++] = 1;
        attrs[i++] = CGL.kCGLPFASamples;
        attrs[i++] = capabilities.getNumSamples();
      }

      // Use attribute array to select pixel format
      long[] fmt = new long[1];
      long[] numScreens = new long[1];
      int res = CGL.CGLChoosePixelFormat(attrs, 0, fmt, 0, numScreens, 0);
      if (res != CGL.kCGLNoError) {
        throw new GLException("Error code " + res + " while choosing pixel format");
      }
      
      // Create new context
      long[] ctx = new long[1];
      if (DEBUG) {
        System.err.println("Share context for CGL-based pbuffer context is " + toHexString(share));
      }
      res = CGL.CGLCreateContext(fmt[0], share, ctx, 0);
      CGL.CGLDestroyPixelFormat(fmt[0]);
      if (res != CGL.kCGLNoError) {
        throw new GLException("Error code " + res + " while creating context");
      }
      // Attach newly-created context to the pbuffer
      res = CGL.CGLSetPBuffer(ctx[0], drawable.getPbuffer(), 0, 0, 0);
      if (res != CGL.kCGLNoError) {
        throw new GLException("Error code " + res + " while attaching context to pbuffer");
      }
      return ctx[0];
    }
    
    public boolean destroy(long ctx) {
      return (CGL.CGLDestroyContext(ctx) == CGL.kCGLNoError);
    }

    public boolean makeCurrent(long ctx) {
      return CGL.CGLSetCurrentContext(ctx) == CGL.kCGLNoError;
    }

    public boolean release(long ctx) {
      return (CGL.CGLSetCurrentContext(0) == CGL.kCGLNoError);
    }

    public void setSwapInterval(long ctx, int interval) {
      // For now not supported (not really relevant for off-screen contexts anyway)
    }
  }
}