JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextVBOES1NEWT.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.newt.opengl.GLWindow;
32
33import com.jogamp.nativewindow.util.InsetsImmutable;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLContext;
37import com.jogamp.opengl.GLDrawable;
38import com.jogamp.opengl.GLDrawableFactory;
39import com.jogamp.opengl.GLProfile;
40
41import com.jogamp.opengl.GLAutoDrawableDelegate;
42import com.jogamp.opengl.util.Animator;
43import com.jogamp.opengl.test.junit.util.GLTestUtil;
44import com.jogamp.opengl.test.junit.util.MiscUtils;
45import com.jogamp.opengl.test.junit.util.NewtTestUtil;
46import com.jogamp.opengl.test.junit.util.UITestCase;
47import com.jogamp.opengl.test.junit.jogl.demos.es1.GearsES1;
48
49import org.junit.Assert;
50import org.junit.BeforeClass;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55/**
56 * Sharing the VBO of 3 GearsES1 instances, each in their own GLWindow.
57 * <p>
58 * This is achieved by creating a <i>master</i> GLContext to an offscreen invisible GLAutoDrawable,
59 * which is then shared by the 3 GLContext of the three GLWindow instances.
60 * </p>
61 * <p>
62 * The original VBO is created by attaching a GearsES1 instance to
63 * the <i>master</i> GLAutoDrawable and initializing it.
64 * </p>
65 * <p>
66 * Above method allows random creation of all GLWindow instances.
67 * </p>
68 * <p>
69 * One animator is being used, hence the GLWindow, GLDrawable and GLContext
70 * creation of all 3 GLWindows is sequential.
71 * </p>
72 */
73@FixMethodOrder(MethodSorters.NAME_ASCENDING)
75 static GLProfile glp;
76 static GLCapabilities caps;
77 static int width, height;
78 GLAutoDrawable sharedDrawable;
79 GearsES1 sharedGears;
80
81 @BeforeClass
82 public static void initClass() {
85 Assert.assertNotNull(glp);
86 caps = new GLCapabilities(glp);
87 Assert.assertNotNull(caps);
88 width = 256;
89 height = 256;
90 } else {
91 setTestSupported(false);
92 }
93 }
94
95 private void initShared() throws InterruptedException {
96 final GLDrawable dummyDrawable = GLDrawableFactory.getFactory(glp).createDummyDrawable(null, true /* createNewDevice */, caps, null);
97 dummyDrawable.setRealized(true);
98 sharedDrawable = new GLAutoDrawableDelegate(dummyDrawable, null, null, true /*ownDevice*/, null) { };
99 Assert.assertNotNull(sharedDrawable);
100 final GLAutoDrawable obj = sharedDrawable;
101 Assert.assertTrue(GLTestUtil.waitForRealized(obj, true, null));
102
103 sharedGears = new GearsES1();
104 Assert.assertNotNull(sharedGears);
105 sharedDrawable.addGLEventListener(sharedGears);
106 // init and render one frame, which will setup the Gears display lists
107 sharedDrawable.display();
108 final GLContext ctxM = sharedDrawable.getContext();
109 Assert.assertTrue("Master ctx not created", GLTestUtil.waitForContextCreated(sharedDrawable, true, null));
110 Assert.assertTrue("Master Ctx is shared before shared creation", !ctxM.isShared());
111 Assert.assertTrue("Master Gears is shared", !sharedGears.usesSharedGears());
112 }
113
114 private void releaseShared() {
115 Assert.assertNotNull(sharedDrawable);
116 sharedDrawable.destroy();
117 }
118
119 protected GLWindow runTestGL(final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync) throws InterruptedException {
120 final GLWindow glWindow = GLWindow.create(caps);
121 Assert.assertNotNull(glWindow);
122 glWindow.setPosition(x, y);
123 glWindow.setTitle("Shared Gears NEWT Test: "+x+"/"+y+" shared "+useShared);
124 if(useShared) {
125 glWindow.setSharedAutoDrawable(sharedDrawable);
126 }
127
128 glWindow.setSize(width, height);
129
130 final GearsES1 gears = new GearsES1(vsync ? 1 : 0);
131 if(useShared) {
132 gears.setSharedGears(sharedGears);
133 }
134 glWindow.addGLEventListener(gears);
135
136 animator.add(glWindow);
137
138 glWindow.setVisible(true);
139 Assert.assertTrue(NewtTestUtil.waitForRealized(glWindow, true, null));
140 Assert.assertTrue(NewtTestUtil.waitForVisible(glWindow, true, null));
141 Assert.assertTrue(GLTestUtil.waitForContextCreated(glWindow, true, null));
142
143 MiscUtils.dumpSharedGLContext("Master Context", sharedDrawable.getContext());
144 MiscUtils.dumpSharedGLContext("New Context", glWindow.getContext());
145 if( useShared ) {
146 Assert.assertEquals("Master Context not shared as expected", true, sharedDrawable.getContext().isShared());
147 Assert.assertEquals("Master Context is different", sharedDrawable.getContext(), glWindow.getContext().getSharedMaster());
148 } else {
149
150 }
151 Assert.assertEquals("New Context not shared as expected", useShared, glWindow.getContext().isShared());
152 Assert.assertEquals("Gears is not shared as expected", useShared, gears.usesSharedGears());
153 return glWindow;
154 }
155
156 @Test
157 public void test01() throws InterruptedException {
158 initShared();
159 final Animator animator = new Animator(0 /* w/o AWT */);
160 final GLWindow f1 = runTestGL(animator, 0, 0, true, false);
161 final InsetsImmutable insets = f1.getInsets();
162 final GLWindow f2 = runTestGL(animator, f1.getX()+width+insets.getTotalWidth(),
163 f1.getY()+0, true, false);
164 final GLWindow f3 = runTestGL(animator, f1.getX()+0,
165 f1.getY()+height+insets.getTotalHeight(), false, true);
166 animator.setUpdateFPSFrames(1, null);
167 animator.start();
168 while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
169 Thread.sleep(100);
170 }
171 animator.stop();
172
173 f1.destroy();
174 f2.destroy();
175 f3.destroy();
176
177 releaseShared();
178 }
179
180 static long duration = 500; // ms
181
182 public static void main(final String args[]) {
183 for(int i=0; i<args.length; i++) {
184 if(args[i].equals("-time")) {
185 i++;
186 try {
187 duration = Integer.parseInt(args[i]);
188 } catch (final Exception ex) { ex.printStackTrace(); }
189 }
190 }
191 org.junit.runner.JUnitCore.main(TestSharedContextVBOES1NEWT.class.getName());
192 }
193}
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setTitle(final String title)
Definition: GLWindow.java:297
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Fully functional GLAutoDrawable implementation utilizing already created GLDrawable and GLContext ins...
Specifies a set of OpenGL capabilities.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
final boolean isShared()
Returns true if this GLContext is shared, otherwise false.
Definition: GLContext.java:261
final GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
Definition: GLContext.java:272
abstract GLDrawable createDummyDrawable(AbstractGraphicsDevice deviceReq, boolean createNewDevice, GLCapabilitiesImmutable capsRequested, GLCapabilitiesChooser chooser)
Creates an unrealized dummy GLDrawable.
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 GL2ES1
The intersection of the desktop GL2 and embedded ES1 profile.
Definition: GLProfile.java:591
Sharing the VBO of 3 GearsES1 instances, each in their own GLWindow.
GLWindow runTestGL(final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync)
static boolean waitForContextCreated(final GLAutoDrawable autoDrawable, final boolean created, final Runnable waitAction)
Definition: GLTestUtil.java:42
static void dumpSharedGLContext(final String prefix, final GLContext self)
Definition: MiscUtils.java:271
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, 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
Immutable insets representing rectangular window decoration insets on all four edges in window units.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
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.
GLContext getContext()
Returns the context associated with this drawable.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
void setRealized(boolean realized)
Indicates to GLDrawable implementations whether the underlying surface has been created and can be dr...