summaryrefslogtreecommitdiffstats
path: root/src/net/java/games/jogl/GLJPanel.java
blob: d8c41e006f8b96a56b2d5f1701d2e40604bf7e4d (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package net.java.games.jogl;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.security.*;
import javax.swing.JComponent;
import javax.swing.JPanel;
import net.java.games.jogl.impl.*;

// FIXME: Subclasses need to call resetGLFunctionAvailability() on their
// context whenever the displayChanged() function is called on their
// GLEventListeners

/** A lightweight Swing component which provides OpenGL rendering
    support. Provided for compatibility with Swing user interfaces
    when adding a heavyweight doesn't work either because of
    Z-ordering or LayoutManager problems. This component attempts to
    use hardware-accelerated rendering via pbuffers and falls back on
    to software rendering if problems occur. This class can not be
    instantiated directly; use {@link GLDrawableFactory} to construct
    them. */

public final class GLJPanel extends JPanel implements GLDrawable {
  private GLDrawableHelper drawableHelper = new GLDrawableHelper();

  // Data used for either pbuffers or pixmap-based offscreen surfaces
  private GLCapabilities        offscreenCaps;
  private GLCapabilitiesChooser chooser;
  private GLDrawable            shareWith;
  private BufferedImage         offscreenImage;
  private int                   neededOffscreenImageWidth;
  private int                   neededOffscreenImageHeight;
  private DataBufferByte   dbByte;
  private DataBufferInt    dbInt;
  private Object semaphore = new Object();
  private int panelWidth   = 0;
  private int panelHeight  = 0;
  private Updater updater;
  private int awtFormat;
  private int glFormat;
  private int glType;

  // Implementation using pbuffers
  private static boolean hardwareAccelerationDisabled =
    Debug.isPropertyDefined("jogl.gljpanel.nohw");
  private boolean   pbufferInitializationCompleted;
  private GLPbuffer pbuffer;
  private int       pbufferWidth  = 256;
  private int       pbufferHeight = 256;
  private GLCanvas  heavyweight;
  private Frame     toplevel;

  // Implementation using software rendering
  private GLContext offscreenContext;

  // For saving/restoring of OpenGL state during ReadPixels
  private int[] swapbytes    = new int[1];
  private int[] rowlength    = new int[1];
  private int[] skiprows     = new int[1];
  private int[] skippixels   = new int[1];
  private int[] alignment    = new int[1];

  GLJPanel(GLCapabilities capabilities, GLCapabilitiesChooser chooser, GLDrawable shareWith) {
    super();

    // Works around problems on many vendors' cards; we don't need a
    // back buffer for the offscreen surface anyway
    offscreenCaps = (GLCapabilities) capabilities.clone();
    offscreenCaps.setDoubleBuffered(false);
    this.chooser = chooser;
    this.shareWith = shareWith;
    
    initialize();
  }

  public void display() {
    if (EventQueue.isDispatchThread()) {
      // Want display() to be synchronous, so call paintImmediately()
      paintImmediately(0, 0, getWidth(), getHeight());
    } else {
      // Multithreaded redrawing of Swing components is not allowed,
      // so do everything on the event dispatch thread
      try {
        // Wait a reasonable period of time for the repaint to
        // complete, so that we don't swamp the AWT event queue thread
        // with repaint requests. We used to have an explicit flag to
        // detect when the repaint completed; unfortunately, under
        // some circumstances, the top-level window can be torn down
        // while we're waiting for the repaint to complete, which will
        // never happen. It doesn't look like there's enough
        // information in the EventQueue to figure out whether there
        // are pending events without posting to the queue, which we
        // don't want to do during shutdown, and adding a
        // HierarchyListener and watching for displayability events
        // might be fragile since we don't know exactly how this
        // component will be used in users' applications. For these
        // reasons we simply wait up to a brief period of time for the
        // repaint to complete.
        synchronized(semaphore) {
          repaint();
          semaphore.wait(100);
        }
      } catch (InterruptedException e) {
      }
    }
  }

  /** Overridden from JComponent; calls {@link
      GLEventListener#display}. Should not be invoked by applications
      directly. */
  public void paintComponent(Graphics g) {
    updater.setGraphics(g);
    if (!hardwareAccelerationDisabled) {
      if (!pbufferInitializationCompleted) {
        try {
          heavyweight.display();
          pbuffer.display();
        } catch (GLException e) {
          // We consider any exception thrown during updating of the
          // heavyweight or pbuffer during the initialization phases
          // to be an indication that there was a problem
          // instantiating the pbuffer, regardless of whether the
          // exception originated in the user's GLEventListener. In
          // these cases we immediately back off and use software
          // rendering.
          disableHardwareRendering();
        }
      } else {
        pbuffer.display();
      }
    } else {
      offscreenContext.invokeGL(displayAction, false, initAction);
    }
    synchronized(semaphore) {
      semaphore.notifyAll();
    }
  }

  /** Overridden from Canvas; causes {@link GLDrawableHelper#reshape}
      to be called on all registered {@link GLEventListener}s. Called
      automatically by the AWT; should not be invoked by applications
      directly. */
  public void reshape(int x, int y, int width, int height) {
    super.reshape(x, y, width, height);

    GLContext context = null;
    neededOffscreenImageWidth = 0;
    neededOffscreenImageHeight = 0;

    if (!hardwareAccelerationDisabled) {
      if (width > pbufferWidth || height > pbufferHeight) {
        // Must destroy and recreate pbuffer to fit
        pbuffer.destroy();
        if (width > pbufferWidth) {
          pbufferWidth = getNextPowerOf2(width);
        }
        if (height > pbufferHeight) {
          pbufferHeight = getNextPowerOf2(height);
        }
        initialize();
      }
      GLPbufferImpl pbufferImpl = (GLPbufferImpl) pbuffer;
      context = pbufferImpl.getContext();
      // It looks like NVidia's drivers (at least the ones on my
      // notebook) are buggy and don't allow a rectangle of less than
      // the pbuffer's width to be read...this doesn't really matter
      // because it's the Graphics.drawImage() calls that are the
      // bottleneck. Should probably make the size of the offscreen
      // image be the exact size of the pbuffer to save some work on
      // resize operations...
      neededOffscreenImageWidth  = pbufferWidth;
      neededOffscreenImageHeight = height;
    } else {
      offscreenContext.resizeOffscreenContext(width, height);
      context = offscreenContext;
      neededOffscreenImageWidth  = width;
      neededOffscreenImageHeight = height;
    }

    if (offscreenImage != null &&
        (offscreenImage.getWidth()  != neededOffscreenImageWidth ||
         offscreenImage.getHeight() != neededOffscreenImageHeight)) {
      offscreenImage.flush();
      offscreenImage = null;
    }

    panelWidth = width;
    panelHeight = height;
    final int fx      = 0;
    final int fy      = 0;

    context.invokeGL(new Runnable() {
        public void run() {
          getGL().glViewport(fx, fy, panelWidth, panelHeight);
          drawableHelper.reshape(GLJPanel.this, fx, fy, panelWidth, panelHeight);
        }
      }, true, initAction);
  }

  public void addGLEventListener(GLEventListener listener) {
    drawableHelper.addGLEventListener(listener);
  }

  public void removeGLEventListener(GLEventListener listener) {
    drawableHelper.removeGLEventListener(listener);
  }

  public GL getGL() {
    if (!hardwareAccelerationDisabled) {
      return pbuffer.getGL();
    } else {
      return offscreenContext.getGL();
    }
  }

  public void setGL(GL gl) {
    if (!hardwareAccelerationDisabled) {
      pbuffer.setGL(gl);
    } else {
      offscreenContext.setGL(gl);
    }
  }

  public GLU getGLU() {
    if (!hardwareAccelerationDisabled) {
      return pbuffer.getGLU();
    } else {
      return offscreenContext.getGLU();
    }
  }
  
  public void setGLU(GLU glu) {
    if (!hardwareAccelerationDisabled) {
      pbuffer.setGLU(glu);
    } else {
      offscreenContext.setGLU(glu);
    }
  }
  
  public void setRenderingThread(Thread currentThreadOrNull) throws GLException {
    // Not supported for GLJPanel because all repaint requests must be
    // handled by the AWT thread
  }

  public Thread getRenderingThread() {
    return null;
  }

  public void setNoAutoRedrawMode(boolean noAutoRedraws) {
  }

  public boolean getNoAutoRedrawMode() {
    return false;
  }

  public void setAutoSwapBufferMode(boolean onOrOff) {
    if (!hardwareAccelerationDisabled) {
      pbuffer.setAutoSwapBufferMode(onOrOff);
    } else {
      offscreenContext.setAutoSwapBufferMode(onOrOff);
    }
  }

  public boolean getAutoSwapBufferMode() {
    if (!hardwareAccelerationDisabled) {
      return pbuffer.getAutoSwapBufferMode();
    } else {
      return offscreenContext.getAutoSwapBufferMode();
    }
  }

  public void swapBuffers() {
    if (!hardwareAccelerationDisabled) {
      pbuffer.swapBuffers();
    } else {
      offscreenContext.invokeGL(swapBuffersAction, false, initAction);
    }
  }

  public boolean canCreateOffscreenDrawable() {
    // For now let's say no, although we could using the heavyweight
    // if hardware acceleration is still enabled
    return false;
  }

  public GLPbuffer createOffscreenDrawable(GLCapabilities capabilities,
                                           int initialWidth,
                                           int initialHeight) {
    throw new GLException("Not supported");
  }

  GLContext getContext() {
    if (!hardwareAccelerationDisabled) {
      return ((GLPbufferImpl) pbuffer).getContext();
    } else {
      return offscreenContext;
    }
  }

  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private void disableHardwareRendering() {
    if (Debug.verbose()) {
      System.err.println("GLJPanel: Falling back on software rendering due to pbuffer problems");
    }
    hardwareAccelerationDisabled = true;
    pbufferInitializationCompleted = false;
    EventQueue.invokeLater(new Runnable() {
        public void run() {
          toplevel.setVisible(false);
          // Should dispose of this -- not sure about stability on
          // various cards -- should test (FIXME)
          // toplevel.dispose();
        }
      });
    initialize();
  }

  private void initialize() {
    // Initialize either the hardware-accelerated rendering path or
    // the lightweight rendering path
    if (!hardwareAccelerationDisabled) {
      boolean firstTime = false;
      if (heavyweight == null) {
        // Make the heavyweight share with the "shareWith" parameter.
        // The pbuffer shares textures and display lists with the
        // heavyweight, so by transitivity the pbuffer will share with
        // it as well.
        heavyweight = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities(), shareWith);
        firstTime = true;
      }
      if (heavyweight.canCreateOffscreenDrawable()) {
        if (firstTime) {
          toplevel = new Frame();
          toplevel.setUndecorated(true);
        }
        pbuffer = heavyweight.createOffscreenDrawable(offscreenCaps, pbufferWidth, pbufferHeight);
        updater = new Updater();
        pbuffer.addGLEventListener(updater);
        pbufferInitializationCompleted = false;
        if (firstTime) {
          toplevel.add(heavyweight);
          toplevel.setSize(0, 0);
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
              try {
                toplevel.setVisible(true);
              } catch (GLException e) {
                disableHardwareRendering();
              }
            }
          });
        return;
      } else {
        // If the heavyweight reports that it can't create an
        // offscreen drawable (pbuffer), don't try again the next
        // time, and fall through to the software rendering path
        hardwareAccelerationDisabled = true;
      }
    }

    // Create an offscreen context instead
    offscreenContext = GLContextFactory.getFactory().createGLContext(null, offscreenCaps, chooser,
                                                                     GLContextHelper.getContext(shareWith));
    offscreenContext.resizeOffscreenContext(panelWidth, panelHeight);
    updater = new Updater();
    if (panelWidth > 0 && panelHeight > 0) {
      offscreenContext.invokeGL(new Runnable() {
          public void run() {
            getGL().glViewport(0, 0, panelWidth, panelHeight);
            drawableHelper.reshape(GLJPanel.this, 0, 0, panelWidth, panelHeight);
          }
        }, true, initAction);
    }
  }

  class Updater implements GLEventListener {
    private Graphics g;

    public void setGraphics(Graphics g) {
      this.g = g;
    }

    public void init(GLDrawable drawable) {
      if (!hardwareAccelerationDisabled) {
        pbufferInitializationCompleted = true;
        EventQueue.invokeLater(new Runnable() {
            public void run() {
              toplevel.setVisible(false);
            }
          });
      }
      drawableHelper.init(GLJPanel.this);
    }

    public void display(GLDrawable drawable) {
      drawableHelper.display(GLJPanel.this);

      // Must now copy pixels from offscreen context into surface
      if (offscreenImage == null) {
        if (panelWidth > 0 && panelHeight > 0) {
          // It looks like NVidia's drivers (at least the ones on my
          // notebook) are buggy and don't allow a sub-rectangle to be
          // read from a pbuffer...this doesn't really matter because
          // it's the Graphics.drawImage() calls that are the
          // bottleneck

          int awtFormat = 0;
          int hwGLFormat = 0;
          if (!hardwareAccelerationDisabled) {
            // Should be more flexible in these BufferedImage formats;
            // perhaps see what the preferred image types are on the
            // given platform
            if (offscreenCaps.getAlphaBits() > 0) {
              awtFormat = BufferedImage.TYPE_INT_ARGB;
            } else {
              awtFormat = BufferedImage.TYPE_INT_RGB;
            }

            // This seems to be a good choice on all platforms
            hwGLFormat = GL.GL_UNSIGNED_INT_8_8_8_8_REV;
          } else {
            awtFormat = offscreenContext.getOffscreenContextBufferedImageType();
          }

          offscreenImage = new BufferedImage(neededOffscreenImageWidth,
                                             neededOffscreenImageHeight,
                                             awtFormat);
          switch (awtFormat) {
            case BufferedImage.TYPE_3BYTE_BGR:
              glFormat = GL.GL_BGR;
              glType   = GL.GL_UNSIGNED_BYTE;
              dbByte   = (DataBufferByte) offscreenImage.getRaster().getDataBuffer();
              break;

            case BufferedImage.TYPE_INT_RGB:
            case BufferedImage.TYPE_INT_ARGB:
              glFormat = GL.GL_BGRA;
              glType   = (hardwareAccelerationDisabled
                            ? offscreenContext.getOffscreenContextPixelDataType()
                            : hwGLFormat);
              dbInt    = (DataBufferInt) offscreenImage.getRaster().getDataBuffer();
              break;

            default:
              // FIXME: Support more off-screen image types (current
              // offscreen context implementations don't use others, and
              // some of the OpenGL formats aren't supported in the 1.1
              // headers, which we're currently using)
              throw new GLException("Unsupported offscreen image type " + awtFormat);
          }
        }
      }

      if (offscreenImage != null) {
        GL gl = getGL();
        // Save current modes
        gl.glGetIntegerv(GL.GL_PACK_SWAP_BYTES,    swapbytes);
        gl.glGetIntegerv(GL.GL_PACK_ROW_LENGTH,    rowlength);
        gl.glGetIntegerv(GL.GL_PACK_SKIP_ROWS,     skiprows);
        gl.glGetIntegerv(GL.GL_PACK_SKIP_PIXELS,   skippixels);
        gl.glGetIntegerv(GL.GL_PACK_ALIGNMENT,     alignment);

        gl.glPixelStorei(GL.GL_PACK_SWAP_BYTES,    GL.GL_FALSE);
        gl.glPixelStorei(GL.GL_PACK_ROW_LENGTH,    offscreenImage.getWidth());
        gl.glPixelStorei(GL.GL_PACK_SKIP_ROWS,     0);
        gl.glPixelStorei(GL.GL_PACK_SKIP_PIXELS,   0);
        gl.glPixelStorei(GL.GL_PACK_ALIGNMENT,     1);

        // Actually read the pixels.
        gl.glReadBuffer(GL.GL_FRONT);
        if (dbByte != null) {
          gl.glReadPixels(0, 0, offscreenImage.getWidth(), offscreenImage.getHeight(), glFormat, glType, dbByte.getData());
        } else if (dbInt != null) {
          gl.glReadPixels(0, 0, offscreenImage.getWidth(), offscreenImage.getHeight(), glFormat, glType, dbInt.getData());
        }

        // Restore saved modes.
        gl.glPixelStorei(GL.GL_PACK_SWAP_BYTES,  swapbytes[0]);
        gl.glPixelStorei(GL.GL_PACK_ROW_LENGTH,  rowlength[0]);
        gl.glPixelStorei(GL.GL_PACK_SKIP_ROWS,   skiprows[0]);
        gl.glPixelStorei(GL.GL_PACK_SKIP_PIXELS, skippixels[0]);
        gl.glPixelStorei(GL.GL_PACK_ALIGNMENT,   alignment[0]);
      
        if (!hardwareAccelerationDisabled ||
            offscreenContext.offscreenImageNeedsVerticalFlip()) {
          // This performs reasonably well; the snippet below does not.
          // Should figure out if we need to set the image scaling
          // preference to FAST since it doesn't require subsampling
          // of pixels -- FIXME
          for (int i = 0; i < panelHeight - 1; i++) {
            g.drawImage(offscreenImage,
                        0, i, panelWidth, i+1,
                        0, panelHeight - i - 2, panelWidth, panelHeight - i - 1,
                        GLJPanel.this);
          }
        } else {
          g.drawImage(offscreenImage, 0, 0, offscreenImage.getWidth(), offscreenImage.getHeight(), GLJPanel.this);
        }
      }
    }

    public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
      // This is handled above and dispatched directly to the appropriate context
    }

    public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
  }

  class InitAction implements Runnable {
    public void run() {
      updater.init(GLJPanel.this);
    }
  }
  private InitAction initAction = new InitAction();

  class DisplayAction implements Runnable {
    public void run() {
      updater.display(GLJPanel.this);
    }
  }
  private DisplayAction displayAction = new DisplayAction();

  // This one is used exclusively in the non-hardware-accelerated case
  class SwapBuffersAction implements Runnable {
    public void run() {
      offscreenContext.swapBuffers();
    }
  }
  private SwapBuffersAction swapBuffersAction = new SwapBuffersAction();

  private int getNextPowerOf2(int number) {
    if (((number-1) & number) == 0) {
      //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0
      return number;
    }
    int power = 0;
    while (number > 0) {
      number = number>>1;
      power++;
    }
    return (1<<power);
  }
}