Пример #1
0
  protected void runTestAGL(GLProfile glprofile) throws InterruptedException {
    GLData gldata = new GLData();
    gldata.doubleBuffer = true;
    // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
    // at the wrong times (we use glClear for this instead)
    final GLCanvas glcanvas = new GLCanvas(composite, SWT.NO_BACKGROUND, gldata);
    Assert.assertNotNull(glcanvas);
    glcanvas.setCurrent();
    final GLContext glcontext = GLDrawableFactory.getFactory(glprofile).createExternalGLContext();
    Assert.assertNotNull(glcontext);

    // fix the viewport when the user resizes the window
    glcanvas.addListener(
        SWT.Resize,
        new Listener() {
          public void handleEvent(Event event) {
            Rectangle rectangle = glcanvas.getClientArea();
            glcanvas.setCurrent();
            glcontext.makeCurrent();
            GL2ES1 gl = glcontext.getGL().getGL2ES1();
            OneTriangle.setup(gl, rectangle.width, rectangle.height);
            glcontext.release();
            System.err.println("resize");
          }
        });

    // draw the triangle when the OS tells us that any part of the window needs drawing
    glcanvas.addPaintListener(
        new PaintListener() {
          public void paintControl(PaintEvent paintevent) {
            Rectangle rectangle = glcanvas.getClientArea();
            glcanvas.setCurrent();
            glcontext.makeCurrent();
            GL2ES1 gl = glcontext.getGL().getGL2ES1();
            OneTriangle.render(gl, rectangle.width, rectangle.height);
            glcanvas.swapBuffers();
            glcontext.release();
            System.err.println("paint");
          }
        });

    shell.setText(getClass().getName());
    shell.setSize(640, 480);
    shell.open();

    long lStartTime = System.currentTimeMillis();
    long lEndTime = lStartTime + duration;
    try {
      while ((System.currentTimeMillis() < lEndTime) && !glcanvas.isDisposed()) {
        if (!display.readAndDispatch()) {
          // blocks on linux .. display.sleep();
          Thread.sleep(10);
        }
      }
    } catch (Throwable throwable) {
      throwable.printStackTrace();
      Assume.assumeNoException(throwable);
    }
    glcanvas.dispose();
  }
Пример #2
0
    @Override
    public synchronized void setVisibleActionPost(boolean visible, boolean nativeWindowCreated) {
      long t0;
      if (Window.DEBUG_IMPLEMENTATION) {
        t0 = System.nanoTime();
        System.err.println(
            "GLWindow.setVisibleActionPost("
                + visible
                + ", "
                + nativeWindowCreated
                + ") "
                + WindowImpl.getThreadName()
                + ", start");
      } else {
        t0 = 0;
      }

      if (null == drawable
          && visible
          && 0 != window.getWindowHandle()
          && 0 < getWidth() * getHeight()) {
        if ((null != context)) {
          throw new InternalError(
              "GLWindow.LifecycleHook.setVisiblePost: "
                  + WindowImpl.getThreadName()
                  + " - Null drawable, but valid context - "
                  + GLWindow.this);
        }
        final NativeSurface ns;
        {
          final NativeSurface wrapped_ns = window.getWrappedSurface();
          ns = null != wrapped_ns ? wrapped_ns : window;
        }
        final GLCapabilitiesImmutable glCaps =
            (GLCapabilitiesImmutable) ns.getGraphicsConfiguration().getChosenCapabilities();
        if (null == factory) {
          factory = GLDrawableFactory.getFactory(glCaps.getGLProfile());
        }
        drawable = (GLDrawableImpl) factory.createGLDrawable(ns);
        drawable.setRealized(true);

        if (!GLWindow.this.pushGLEventListenerState()) {
          context = (GLContextImpl) drawable.createContext(sharedContext);
          context.setContextCreationFlags(additionalCtxCreationFlags);
        }
      }
      if (Window.DEBUG_IMPLEMENTATION) {
        System.err.println(
            "GLWindow.setVisibleActionPost("
                + visible
                + ", "
                + nativeWindowCreated
                + ") "
                + WindowImpl.getThreadName()
                + ", fin: dt "
                + (System.nanoTime() - t0) / 1e6
                + "ms");
      }
    }
Пример #3
0
    @Override
    public synchronized void setVisibleActionPost(boolean visible, boolean nativeWindowCreated) {
      long t0;
      if (Window.DEBUG_IMPLEMENTATION) {
        t0 = System.nanoTime();
        System.err.println(
            "GLWindow.setVisibleActionPost("
                + visible
                + ", "
                + nativeWindowCreated
                + ") "
                + WindowImpl.getThreadName()
                + ", start");
      } else {
        t0 = 0;
      }

      /* if (nativeWindowCreated && null != context) {
          throw new GLException("InternalError: Native Windows has been just created, but context wasn't destroyed (is not null)");
      } */
      if (null == context
          && visible
          && 0 != window.getWindowHandle()
          && 0 < getWidth() * getHeight()) {
        NativeWindow nw;
        if (window.getWrappedWindow() != null) {
          nw =
              NativeWindowFactory.getNativeWindow(
                  window.getWrappedWindow(), window.getPrivateGraphicsConfiguration());
        } else {
          nw = window;
        }
        GLCapabilitiesImmutable glCaps =
            (GLCapabilitiesImmutable) nw.getGraphicsConfiguration().getChosenCapabilities();
        if (null == factory) {
          factory = GLDrawableFactory.getFactory(glCaps.getGLProfile());
        }
        if (null == drawable) {
          drawable = (GLDrawableImpl) factory.createGLDrawable(nw);
        }
        drawable.setRealized(true);
        context = (GLContextImpl) drawable.createContext(sharedContext);
        context.setContextCreationFlags(additionalCtxCreationFlags);
      }
      if (Window.DEBUG_IMPLEMENTATION) {
        System.err.println(
            "GLWindow.setVisibleActionPost("
                + visible
                + ", "
                + nativeWindowCreated
                + ") "
                + WindowImpl.getThreadName()
                + ", fin: dt "
                + (System.nanoTime() - t0) / 1e6
                + "ms");
      }
    }
Пример #4
0
  public static GLOffscreenAutoDrawable newOffscreenDrawable(GLProfile profile) {
    GLDrawableFactory drawableFactory = GLDrawableFactory.getFactory(profile);
    GLCapabilities caps = new GLCapabilities(profile);
    GLOffscreenAutoDrawable offscreenDrawable =
        drawableFactory.createOffscreenAutoDrawable(null, caps, null, 1, 1);

    // Trigger context creation
    offscreenDrawable.display();

    return offscreenDrawable;
  }
  @Override
  public GLAutoDrawable createGLAutoDrawable(
      final QuitAdapter quitAdapter,
      final GLCapabilitiesImmutable caps,
      final int width,
      final int height)
      throws InterruptedException, InvocationTargetException {
    final GLAutoDrawable glad;
    if (caps.isOnscreen()) {
      final Frame frame = new Frame("Gears AWT Test");
      Assert.assertNotNull(frame);

      final GLCanvas glCanvas = new GLCanvas(caps);
      Assert.assertNotNull(glCanvas);
      final Dimension glc_sz = new Dimension(width, height);
      glCanvas.setMinimumSize(glc_sz);
      glCanvas.setPreferredSize(glc_sz);
      glCanvas.setSize(glc_sz);
      glad = glCanvas;

      new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glCanvas).addTo(frame);

      frame.setLayout(new BorderLayout());
      frame.add(glCanvas, BorderLayout.CENTER);
      javax.swing.SwingUtilities.invokeAndWait(
          new Runnable() {
            public void run() {
              frame.pack();
              frame.setVisible(true);
            }
          });
    } else {
      final GLDrawableFactory factory = GLDrawableFactory.getFactory(caps.getGLProfile());
      glad = factory.createOffscreenAutoDrawable(null, caps, null, width, height);
      Assert.assertNotNull(glad);
    }
    return glad;
  }
Пример #6
0
  public void createPartControl(final Composite parent) {
    Composite top = new Composite(parent, SWT.NONE);
    top.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;
    canvas = new GLCanvas(top, SWT.NONE, data);
    canvas.setCurrent();
    final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();
    context.makeCurrent();
    GL gl = context.getGL();
    geocontext.initialize(context);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    context.release();

    canvas.addListener(
        SWT.Resize,
        new Listener() {
          public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            canvas.setCurrent();
            context.makeCurrent();
            GL gl = context.getGL();
            geocontext.setHeight(bounds.height);
            geocontext.setWidth(bounds.width);
            gl.glViewport(0, 0, bounds.width, bounds.height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            new GLU().gluOrtho2D(0, 1, 0, 1);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
            context.release();
          }
        });

    canvas.addMouseMoveListener(
        new MouseMoveListener() {

          @Override
          public void mouseMove(MouseEvent e) {
            if (e.stateMask == SWT.SHIFT) {
              dxx = (int) (geocontext.getZoom() * (e.x - canvas.getBounds().width / 2) / 10);
              dyy = (int) (geocontext.getZoom() * (e.y - canvas.getBounds().height / 2) / 10);
              geocontext.setDirty(true);
            } else {
              Point p = new Point();
              try {
                p.x =
                    (int)
                        (geocontext.getX()
                            + e.x
                                * geocontext.getWidth()
                                / canvas.getBounds().width
                                * geocontext.getZoom());
                p.y =
                    (int)
                        (geocontext.getY()
                            + e.y
                                * geocontext.getHeight()
                                / canvas.getBounds().height
                                * geocontext.getZoom());
                root.mouseMoved(p, geocontext);
              } catch (Exception ex) {
              }
            }
          }
        });

    canvas.addMouseWheelListener(
        new MouseWheelListener() {

          @Override
          public void mouseScrolled(MouseEvent e) {

            float zoom = (float) (geocontext.getZoom() * Math.pow(2, -e.count / 20.0));

            // check mouse position
            int posX = e.x;
            int posY = e.y;
            // calculate the image position of the current position of the mouse
            int x =
                (int)
                    (geocontext.getX()
                        + posX
                            * geocontext.getWidth()
                            / canvas.getBounds().width
                            * geocontext.getZoom());
            int y =
                (int)
                    (geocontext.getY()
                        + posY
                            * geocontext.getHeight()
                            / canvas.getBounds().height
                            * geocontext.getZoom());
            geocontext.setZoom(zoom);
            // translate the image origin to have the same mouse position in the geocontext
            geocontext.setX(
                (int)
                    (x
                        - posX
                            * geocontext.getWidth()
                            / canvas.getBounds().width
                            * geocontext.getZoom()));
            geocontext.setY(
                (int)
                    (y
                        - posY
                            * geocontext.getHeight()
                            / canvas.getBounds().height
                            * geocontext.getZoom()));
            geocontext.setDirty(true);
          }
        });

    final Runnable core =
        new Runnable() {
          int count = 0;
          long[] times = new long[500];

          public void run() {
            if (!canvas.isDisposed()) {
              if (count == times.length) {
                count = 0;
                double fps = 0;
                for (int i = 0; i < times.length - 1; i++) {
                  fps += (times[i + 1] - times[i]) / 1000.;
                }
                fps /= times.length - 1;
                System.out.println(1 / fps);
              } else {
                times[count++] = System.currentTimeMillis();
              }
              canvas.setCurrent();
              context.makeCurrent();
              GL gl = context.getGL();
              gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
              if (dxx != 0 || dyy != 0) {
                dxx /= 1.2;
                dyy /= 1.2;
                geocontext.setX(geocontext.getX() + dxx);
                geocontext.setY(geocontext.getY() + dyy);
              }
              geocontext.initialize(context);
              geocontext.setDirty(true);
              root.render(geocontext);
              canvas.swapBuffers();
              context.release();
              try {
                Thread.sleep(15);
              } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              canvas.getDisplay().asyncExec(this);
            }
          }
        };
    canvas.getDisplay().asyncExec(core);

    registerView();
    renderLayers();
  }
Пример #7
0
  void doTest(
      final boolean onscreen, final GLEventListener demo, final GLProfile glp, final int msaaCount)
      throws IOException {
    final GLCapabilities caps = new GLCapabilities(glp);
    caps.setDoubleBuffered(onscreen);
    if (msaaCount > 0) {
      caps.setSampleBuffers(true);
      caps.setNumSamples(msaaCount);
    }

    final int maxTileSize = 256;
    final GLAutoDrawable glad;
    if (onscreen) {
      final GLWindow glWin = GLWindow.create(caps);
      glWin.setSize(maxTileSize, maxTileSize);
      glWin.setVisible(true);
      glad = glWin;
    } else {
      final GLDrawableFactory factory = GLDrawableFactory.getFactory(glp);
      glad = factory.createOffscreenAutoDrawable(null, caps, null, maxTileSize, maxTileSize);
    }

    glad.addGLEventListener(demo);

    // Fix the image size for now
    final int imageWidth = glad.getSurfaceWidth() * 6;
    final int imageHeight = glad.getSurfaceHeight() * 4;

    final String filename =
        this.getSnapshotFilename(
            0,
            "-tile",
            glad.getChosenGLCapabilities(),
            imageWidth,
            imageHeight,
            false,
            TextureIO.PNG,
            null);
    final File file = new File(filename);

    // Initialize the tile rendering library
    final TileRenderer renderer = new TileRenderer();
    renderer.setImageSize(imageWidth, imageHeight);
    renderer.setTileSize(glad.getSurfaceWidth(), glad.getSurfaceHeight(), 0);
    renderer.attachAutoDrawable(glad);

    final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider =
        GLPixelBuffer.defaultProviderWithRowStride;
    final boolean[] flipVertically = {false};

    final GLEventListener preTileGLEL =
        new GLEventListener() {
          @Override
          public void init(final GLAutoDrawable drawable) {
            final GL gl = drawable.getGL();
            final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, 3);
            final GLPixelBuffer pixelBuffer =
                pixelBufferProvider.allocate(gl, pixelAttribs, imageWidth, imageHeight, 1, true, 0);
            renderer.setImageBuffer(pixelBuffer);
            if (drawable.isGLOriented()) {
              flipVertically[0] = false;
            } else {
              flipVertically[0] = true;
            }
          }

          @Override
          public void dispose(final GLAutoDrawable drawable) {}

          @Override
          public void display(final GLAutoDrawable drawable) {}

          @Override
          public void reshape(
              final GLAutoDrawable drawable,
              final int x,
              final int y,
              final int width,
              final int height) {}
        };
    renderer.setGLEventListener(preTileGLEL, null);

    while (!renderer.eot()) {
      renderer.display();
    }

    renderer.detachAutoDrawable();

    // Restore viewport and Gear's PMV matrix
    // .. even though we close the demo, this is for documentation!
    glad.invoke(
        true,
        new GLRunnable() {
          @Override
          public boolean run(final GLAutoDrawable drawable) {
            drawable
                .getGL()
                .glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
            demo.reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
            return false;
          }
        });

    final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
    final TextureData textureData =
        new TextureData(
            caps.getGLProfile(),
            0 /* internalFormat */,
            imageWidth,
            imageHeight,
            0,
            imageBuffer.pixelAttributes,
            false,
            false,
            flipVertically[0],
            imageBuffer.buffer,
            null /* Flusher */);

    TextureIO.write(textureData, file);

    glad.destroy();
  }
  void doTest(final GLCapabilitiesImmutable reqGLCaps, final GLEventListener demo)
      throws InterruptedException {
    System.out.println("Requested  GL Caps: " + reqGLCaps);
    final GLDrawableFactory factory = GLDrawableFactory.getFactory(reqGLCaps.getGLProfile());
    final GLCapabilitiesImmutable expGLCaps =
        GLGraphicsConfigurationUtil.fixGLCapabilities(reqGLCaps, factory, null);
    System.out.println("Expected   GL Caps: " + expGLCaps);

    //
    // Create native OpenGL resources .. XGL/WGL/CGL ..
    // equivalent to GLAutoDrawable methods: setVisible(true)
    //
    final GLOffscreenAutoDrawable glad =
        factory.createOffscreenAutoDrawable(
            null, reqGLCaps, null, widthStep * szStep, heightStep * szStep);

    Assert.assertNotNull(glad);
    System.out.println(
        "Drawable    Pre-GL(0): "
            + glad.getClass().getName()
            + ", "
            + glad.getNativeSurface().getClass().getName());
    Assert.assertTrue(glad.isRealized());

    // Check caps of NativeWindow config w/o GL
    final CapabilitiesImmutable chosenCaps = glad.getChosenGLCapabilities();
    System.out.println("Drawable Caps Pre_GL : " + chosenCaps);
    Assert.assertNotNull(chosenCaps);
    Assert.assertTrue(chosenCaps.getGreenBits() > 4);
    Assert.assertTrue(chosenCaps.getBlueBits() > 4);
    Assert.assertTrue(chosenCaps.getRedBits() > 4);

    glad.display(); // force native context creation

    // Check caps of GLDrawable after realization
    final GLCapabilitiesImmutable chosenGLCaps = glad.getChosenGLCapabilities();
    System.out.println("Chosen     GL CTX (1): " + glad.getContext().getGLVersion());
    System.out.println("Chosen     GL Caps(1): " + chosenGLCaps);
    System.out.println(
        "Chosen     GL Caps(2): "
            + glad.getNativeSurface().getGraphicsConfiguration().getChosenCapabilities());

    Assert.assertNotNull(chosenGLCaps);
    Assert.assertTrue(chosenGLCaps.getGreenBits() > 4);
    Assert.assertTrue(chosenGLCaps.getBlueBits() > 4);
    Assert.assertTrue(chosenGLCaps.getRedBits() > 4);
    Assert.assertTrue(chosenGLCaps.getDepthBits() > 4);
    Assert.assertEquals(expGLCaps.isOnscreen(), chosenGLCaps.isOnscreen());
    Assert.assertEquals(expGLCaps.isFBO(), chosenGLCaps.isFBO());
    Assert.assertEquals(expGLCaps.isPBuffer(), chosenGLCaps.isPBuffer());
    Assert.assertEquals(expGLCaps.isBitmap(), chosenGLCaps.isBitmap());
    /**
     * Single/Double buffer cannot be checked since result may vary .. if(chosenGLCaps.isOnscreen()
     * || chosenGLCaps.isFBO()) { // dbl buffer may be disabled w/ offscreen pbuffer and bitmap
     * Assert.assertEquals(expGLCaps.getDoubleBuffered(), chosenGLCaps.getDoubleBuffered()); }
     */
    glad.addGLEventListener(demo);

    final SnapshotGLEventListener snapshotGLEventListener = new SnapshotGLEventListener();
    glad.addGLEventListener(snapshotGLEventListener);

    glad.display(); // initial resize/display

    // 1 - szStep = 2
    Assert.assertTrue(
        "Size not reached: Expected "
            + (widthStep * szStep)
            + "x"
            + (heightStep * szStep)
            + ", Is "
            + glad.getSurfaceWidth()
            + "x"
            + glad.getSurfaceHeight(),
        AWTRobotUtil.waitForSize(glad, widthStep * szStep, heightStep * szStep));
    snapshotGLEventListener.setMakeSnapshot();
    glad.display();

    // 2, 3 (resize + display)
    szStep = 1;
    glad.setSurfaceSize(widthStep * szStep, heightStep * szStep);
    Assert.assertTrue(
        "Size not reached: Expected "
            + (widthStep * szStep)
            + "x"
            + (heightStep * szStep)
            + ", Is "
            + glad.getSurfaceWidth()
            + "x"
            + glad.getSurfaceHeight(),
        AWTRobotUtil.waitForSize(glad, widthStep * szStep, heightStep * szStep));
    snapshotGLEventListener.setMakeSnapshot();
    glad.display();

    // 4, 5 (resize + display)
    szStep = 4;
    glad.setSurfaceSize(widthStep * szStep, heightStep * szStep);
    Assert.assertTrue(
        "Size not reached: Expected "
            + (widthStep * szStep)
            + "x"
            + (heightStep * szStep)
            + ", Is "
            + glad.getSurfaceWidth()
            + "x"
            + glad.getSurfaceHeight(),
        AWTRobotUtil.waitForSize(glad, widthStep * szStep, heightStep * szStep));
    snapshotGLEventListener.setMakeSnapshot();
    glad.display();

    Thread.sleep(50);

    glad.destroy();
    System.out.println("Fin Drawable: " + glad);
  }