JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextWithJTabbedPaneAWT.java
Go to the documentation of this file.
1/**
2 * Copyright (C) 2013 United States Government as represented by the Administrator of the
3 * National Aeronautics and Space Administration.
4 * All Rights Reserved.
5 *
6 * Copyright 2010 JogAmp Community. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without modification, are
9 * permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice, this list of
12 * conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 * of conditions and the following disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * The views and conclusions contained in the software and documentation are those of the
29 * authors and should not be interpreted as representing official policies, either expressed
30 * or implied, of JogAmp Community.
31 */
32package com.jogamp.opengl.test.junit.jogl.acore;
33
34import java.awt.BorderLayout;
35import java.lang.reflect.InvocationTargetException;
36import java.nio.FloatBuffer;
37
38import com.jogamp.opengl.GL;
39import com.jogamp.opengl.GL2;
40import com.jogamp.opengl.GLAutoDrawable;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLContext;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.awt.GLCanvas;
46import com.jogamp.opengl.fixedfunc.GLLightingFunc;
47import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
48import com.jogamp.opengl.fixedfunc.GLPointerFunc;
49import javax.swing.JFrame;
50import javax.swing.JPanel;
51import javax.swing.JTabbedPane;
52import javax.swing.SwingUtilities;
53import javax.swing.WindowConstants;
54
55import org.junit.Assert;
56import org.junit.FixMethodOrder;
57import org.junit.Test;
58import org.junit.runners.MethodSorters;
59
60import com.jogamp.common.nio.Buffers;
61import com.jogamp.opengl.test.junit.util.MiscUtils;
62import com.jogamp.opengl.test.junit.util.UITestCase;
63
64@FixMethodOrder(MethodSorters.NAME_ASCENDING)
66
67 static class DemoInstance {
68 protected static GLCapabilities getCaps() {
70
71 caps.setAlphaBits(8);
72 caps.setRedBits(8);
73 caps.setGreenBits(8);
74 caps.setBlueBits(8);
75 caps.setDepthBits(24);
76 caps.setDoubleBuffered(true);
77
78 return caps;
79 }
80
81 int[] bufferId;
82
83 @SuppressWarnings("serial")
84 class SharedGLPanel extends JPanel implements GLEventListener {
85 final GLCanvas canvas;
86 final boolean shared;
87
88 public SharedGLPanel(final GLCanvas shareWith, final int width, final int height) {
89 final GLContext sharedCtx = shareWith != null ? shareWith.getContext() : null;
90 System.err.println("XXX WWPanel: shareWith "+shareWith+", sharedCtx "+sharedCtx);
91 canvas = new GLCanvas(getCaps()); // same caps for 1st and 2nd shared ctx !
92 if( null != shareWith) {
93 canvas.setSharedAutoDrawable(shareWith);
94 shared = true;
95 } else {
96 shared = false;
97 }
98 canvas.setSize(new java.awt.Dimension(width, height));
99
100 setLayout(new BorderLayout(5, 5));
101 add(canvas, BorderLayout.CENTER);
102 setOpaque(false);
103
104 canvas.addGLEventListener(this);
105 }
106
107 @Override
108 public void init(final GLAutoDrawable glAutoDrawable) {
109 if (!shared) {
110 Assert.assertNull("Buffer is set, but instance is share master", bufferId);
111 makeVBO(glAutoDrawable);
112 System.err.println("XXX Create Buffer "+bufferId[0]);
113 } else {
114 Assert.assertNotNull("Buffer is not set, but instance is share slave", bufferId);
115 Assert.assertTrue("Context is not shared", glAutoDrawable.getContext().isShared());
116 System.err.println("XXX Reuse Buffer "+bufferId[0]);
117 }
118 final GL2 gl = glAutoDrawable.getGL().getGL2();
119 if( shared ) {
120 gl.glColor3f(1, 1, 1);
121 gl.glClearColor(0.3f, 0.3f, 0.3f, 1f);
122 } else {
123 gl.glColor3f(0, 0, 0);
124 gl.glClearColor(1f, 1f, 1f, 1f);
125 }
127 }
128
129 @Override
130 public void dispose(final GLAutoDrawable glAutoDrawable) {}
131
132 @Override
133 public void display(final GLAutoDrawable glAutoDrawable) {
134 final GL2 gl = glAutoDrawable.getGL().getGL2();
135
137
139 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferId[0]);
140 gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
141 gl.glDrawArrays(GL.GL_LINES, 0, 2);
142 }
143
144 @Override
145 public void reshape(final GLAutoDrawable glAutoDrawable, final int i, final int i1, final int i2, final int i3) {
146 final int w = getWidth();
147 final int h = getHeight();
148
149 final GL2 gl = glAutoDrawable.getGL().getGL2();
150
151 gl.glViewport(0, 0, w, h);
153 gl.glLoadIdentity();
154 gl.glOrtho(0, 1, 0, 1, -1, 1);
156 gl.glLoadIdentity();
157 }
158 }
159
160 protected void makeVBO(final GLAutoDrawable drawable) {
161 final GL2 gl = drawable.getGL().getGL2();
162
163 bufferId = new int[1];
164 gl.glGenBuffers(1, bufferId, 0);
165 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferId[0]);
166
167 final FloatBuffer vertices = Buffers.newDirectFloatBuffer(6);
168 vertices.put(0).put(0).put(0);
169 vertices.put(1).put(1).put(0);
170 gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.capacity() * 4, vertices.rewind(), GL.GL_STATIC_DRAW);
171 }
172
173 public JTabbedPane tabbedPanel;
174
175 public DemoInstance(final JFrame f) {
176 try
177 {
178 GLProfile.initSingleton(); // Lets have init debug messages above below marker
179 System.err.println("XXX START DEMO XXX");
180
181 // Create the application frame and the tabbed pane and add the pane to the frame.
182 tabbedPanel = new JTabbedPane();
183 f.add(tabbedPanel, BorderLayout.CENTER);
184
185 // Create two World Windows that share resources.
186 final SharedGLPanel wwpA = new SharedGLPanel(null, 600, 600);
187 final SharedGLPanel wwpB = new SharedGLPanel(wwpA.canvas, wwpA.getWidth(), wwpA.getHeight());
188
189 tabbedPanel.add(wwpA, "Window A");
190 tabbedPanel.add(wwpB, "Window B");
191
192 // Add the card panel to the frame.
193 f.add(tabbedPanel, BorderLayout.CENTER);
194
195 // Position and display the frame.
196 f.setTitle("Multi-Window Tabbed Pane");
197 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
198 f.pack();
199 f.setResizable(true);
200 } catch (final Exception e) {
201 e.printStackTrace();
202 }
203 }
204 }
205
206 static long durationPerTest = 500*4; // ms
207 static boolean manual = false;
208
209 @Test
210 public void test01() throws InterruptedException, InvocationTargetException {
211 final JFrame f = new JFrame();
212 f.setTitle("Shared GLContext AWT GLCanvas JTabbedPane");
213 final DemoInstance demo = new DemoInstance(f);
214 SwingUtilities.invokeLater(new Runnable() {
215 @Override
216 public void run() {
217 System.err.println("XXX SetVisible XXX");
218 f.setVisible(true);
219 } });
220
221 if(manual) {
222 for(long w=durationPerTest; w>0; w-=100) {
223 Thread.sleep(100);
224 }
225 } else {
226 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
227 @Override
228 public void run() {
229 demo.tabbedPanel.setSelectedIndex(0);
230 }});
231 Thread.sleep(durationPerTest/4);
232
233 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
234 @Override
235 public void run() {
236 demo.tabbedPanel.setSelectedIndex(1);
237 }});
238 Thread.sleep(durationPerTest/4);
239
240 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
241 @Override
242 public void run() {
243 demo.tabbedPanel.setSelectedIndex(0);
244 }});
245 Thread.sleep(durationPerTest/4);
246
247 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
248 @Override
249 public void run() {
250 demo.tabbedPanel.setSelectedIndex(1);
251 }});
252 Thread.sleep(durationPerTest/4);
253 }
254
255 SwingUtilities.invokeLater(new Runnable() {
256 // SwingUtilities.invokeAndWait(new Runnable() {
257 @Override
258 public void run() {
259 System.err.println("XXX SetVisible XXX");
260 f.dispose();
261 } });
262 }
263
264 public static void main(final String args[]) {
265 for(int i=0; i<args.length; i++) {
266 if(args[i].equals("-time")) {
267 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest);
268 } else if(args[i].equals("-manual")) {
269 manual = true;
270 }
271 }
272 org.junit.runner.JUnitCore.main(TestSharedContextWithJTabbedPaneAWT.class.getName());
273 }
274}
void setRedBits(final int redBits)
Sets the number of bits requested for the color buffer's red component.
void setGreenBits(final int greenBits)
Sets the number of bits requested for the color buffer's green component.
void setBlueBits(final int blueBits)
Sets the number of bits requested for the color buffer's blue component.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
Specifies a set of OpenGL capabilities.
void setDoubleBuffered(final boolean enable)
Enables or disables double buffering.
void setDepthBits(final int depthBits)
Sets the number of bits requested for the depth buffer.
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
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getMaxFixedFunc(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the fixed function pipeline.
Definition: GLProfile.java:808
static void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
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
GLContext getContext()
Returns the context associated with this drawable.
Definition: GLCanvas.java:1166
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
void glOrtho(double left, double right, double bottom, double top, double near_val, double far_val)
void glEnableClientState(int cap)
Entry point to C language function: void {@native glEnableClientState}(GLenum cap) Part of GL_NV_v...
void glColor3f(float red, float green, float blue)
Entry point to C language function: void {@native glColor3f}(GLfloat red, GLfloat green,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GLContext getContext()
Returns the context associated with this drawable.
GL2 getGL2()
Casts this object to the GL2 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glGenBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glGenBuffers}(GLsizei n, GLuint * buffers) Part ...
void glDrawArrays(int mode, int first, int count)
Entry point to C language function: void {@native glDrawArrays}(GLenum mode, GLint first,...
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
Definition: GL.java:786
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glViewport(int x, int y, int width, int height)
Entry point to C language function: void {@native glViewport}(GLint x, GLint y, GLsizei width,...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
static final int GL_LINES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINES" with expressio...
Definition: GL.java:430
void glBindBuffer(int target, int buffer)
Entry point to C language function: void {@native glBindBuffer}(GLenum target, GLuint buffer) Part...
void glBufferData(int target, long size, Buffer data, int usage)
Entry point to C language function: void {@native glBufferData}(GLenum target, GLsizeiptr size,...
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.
void glVertexPointer(GLArrayData array)