JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextListAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.jogl.acore;
30
31import com.jogamp.opengl.GLCapabilities;
32import com.jogamp.opengl.GLDrawableFactory;
33import com.jogamp.opengl.GLOffscreenAutoDrawable;
34import com.jogamp.opengl.GLProfile;
35import com.jogamp.opengl.awt.GLCanvas;
36
37import com.jogamp.opengl.util.Animator;
38
39import com.jogamp.opengl.test.junit.util.UITestCase;
40import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
41import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
42
43import java.awt.Frame;
44import java.lang.reflect.InvocationTargetException;
45
46import org.junit.Assert;
47import org.junit.Assume;
48import org.junit.BeforeClass;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 static GLProfile glp;
56 static GLCapabilities caps;
57 static int width, height;
58 GLOffscreenAutoDrawable sharedDrawable;
59 Gears sharedGears;
60
61 @BeforeClass
62 public static void initClass() {
65 Assert.assertNotNull(glp);
66 caps = new GLCapabilities(glp);
67 Assert.assertNotNull(caps);
68 width = 256;
69 height = 256;
70 } else {
71 setTestSupported(false);
72 }
73 }
74
75 private void initShared() {
76 sharedDrawable = GLDrawableFactory.getFactory(glp).createOffscreenAutoDrawable(null, caps, null, width, height);
77 Assert.assertNotNull(sharedDrawable);
78 sharedGears = new Gears();
79 Assert.assertNotNull(sharedGears);
80 sharedDrawable.addGLEventListener(sharedGears);
81 // init and render one frame, which will setup the Gears display lists
82 sharedDrawable.display();
83 }
84
85 private void releaseShared() {
86 Assert.assertNotNull(sharedDrawable);
87 sharedDrawable.destroy();
88 }
89
90 protected void setFrameTitle(final Frame f, final boolean useShared)
91 throws InterruptedException, InvocationTargetException {
92 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
93 public void run() {
94 f.setTitle("Shared Gears AWT Test: "+f.getX()+"/"+f.getY()+" shared "+useShared);
95 }
96 });
97 }
98
99 protected GLCanvas runTestGL(final Frame frame, final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync)
100 throws InterruptedException, InvocationTargetException
101 {
102 final GLCanvas glCanvas = new GLCanvas(caps);
103 Assert.assertNotNull(glCanvas);
104 glCanvas.setSharedAutoDrawable(sharedDrawable);
105 frame.add(glCanvas);
106 frame.setLocation(x, y);
107 frame.setSize(width, height);
108
109 final Gears gears = new Gears(vsync ? 1 : 0);
110 if(useShared) {
111 gears.setSharedGears(sharedGears);
112 }
113 glCanvas.addGLEventListener(gears);
114
115 animator.add(glCanvas);
116
117 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
118 public void run() {
119 frame.setVisible(true);
120 } } );
121 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas, true, null));
122
123 return glCanvas;
124 }
125
126 @Test
127 public void test01() throws InterruptedException, InvocationTargetException {
128 initShared();
129 final Frame f1 = new Frame();
130 final Frame f2 = new Frame();
131 final Frame f3 = new Frame();
132 final Animator animator = new Animator();
133
134 final GLCanvas glc1 = runTestGL(f1, animator, 0, 0, true, false);
135 final int x0 = f1.getX();
136 final int y0 = f1.getY();
137
138 final GLCanvas glc2 = runTestGL(f2, animator,
139 x0+width,
140 y0+0,
141 true, false);
142
143 final GLCanvas glc3 = runTestGL(f3, animator,
144 x0+0,
145 y0+height,
146 false, true);
147
148 setFrameTitle(f1, true);
149 setFrameTitle(f2, true);
150 setFrameTitle(f3, false);
151
152 animator.setUpdateFPSFrames(1, null);
153 animator.start();
154 while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
155 Thread.sleep(100);
156 }
157 animator.stop();
158
159 try {
160 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
161 public void run() {
162 try {
163 f1.dispose();
164 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc1, false, null));
165 f2.dispose();
166 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc2, false, null));
167 f3.dispose();
168 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc3, false, null));
169 } catch (final Throwable t) {
170 throw new RuntimeException(t);
171 }
172 }});
173 } catch( final Throwable throwable ) {
174 throwable.printStackTrace();
175 Assume.assumeNoException( throwable );
176 }
177
178 releaseShared();
179 }
180
181 static long duration = 500; // ms
182
183 public static void main(final String args[]) {
184 for(int i=0; i<args.length; i++) {
185 if(args[i].equals("-time")) {
186 i++;
187 try {
188 duration = Integer.parseInt(args[i]);
189 } catch (final Exception ex) { ex.printStackTrace(); }
190 }
191 }
192 org.junit.runner.JUnitCore.main(TestSharedContextListAWT.class.getName());
193 }
194}
Specifies a set of OpenGL capabilities.
abstract GLOffscreenAutoDrawable createOffscreenAutoDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates a realized GLOffscreenAutoDrawable incl it's offscreen NativeSurface with the given capabilit...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
final void setSharedAutoDrawable(final GLAutoDrawable sharedAutoDrawable)
Specifies an GLAutoDrawable, which OpenGL context shall be shared by this GLAutoDrawable's GLContext.
Definition: GLCanvas.java:288
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
GLCanvas runTestGL(final Frame frame, final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync)
Gears.java author: Brian Paul (converted to Java by Ron Cemer and Sven Gothel)
Definition: Gears.java:34
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Platform-independent GLAutoDrawable specialization, exposing offscreen functionality.