JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug669RecursiveGLContext01NEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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 */
28package com.jogamp.opengl.test.junit.jogl.acore;
29
30import com.jogamp.opengl.GLAutoDrawable;
31import com.jogamp.opengl.GLCapabilities;
32import com.jogamp.opengl.GLContext;
33import com.jogamp.opengl.GLEventListener;
34import com.jogamp.opengl.GLProfile;
35
36import jogamp.opengl.GLContextImpl;
37
38import org.junit.Assert;
39import org.junit.Test;
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43import com.jogamp.newt.opengl.GLWindow;
44import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
45import com.jogamp.opengl.test.junit.util.UITestCase;
46import com.jogamp.opengl.util.Animator;
47
48/**
49 * Tests simple recursive GLContext behavior.
50 *
51 * <p>
52 * Issues {@link GLContext#makeCurrent()} and {@link GLContext#release()}
53 * from within {@link GLEventListener#display(GLAutoDrawable)}.
54 * </p>
55 *
56 * <https://jogamp.org/bugzilla/show_bug.cgi?id=669>
57 */
58@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60
61 @Test(timeout=10000)
62 public void test01_Plain() {
63 test01Impl(false);
64 }
65
66 @Test(timeout=10000)
67 public void test01_Anim() {
68 test01Impl(true);
69 }
70
71 private void test01Impl(final boolean anim) {
72 final String profile = GLProfile.GL2ES2;
73 if(!GLProfile.isAvailable(profile)) { System.err.println(profile+" n/a"); return; }
74
75 final GLProfile pro = GLProfile.get(profile);
76 final GLCapabilities caps = new GLCapabilities(pro);
77 final GLWindow window = GLWindow.create(caps);
78
79 final Animator animator = new Animator(0 /* w/o AWT */);
80 if(anim) {
81 animator.add(window);
82 }
83 animator.start();
84
85 window.setSize(640, 480);
86 window.addGLEventListener(new GLEventListener() {
87 private void makeCurrentRecursive(final GLContextImpl context, final int lockCount) {
88 Assert.assertEquals(true, context.isOwner(Thread.currentThread()));
89 Assert.assertEquals(lockCount, context.getLockCount());
90 Assert.assertEquals(true, context.isCurrent());
91
92 Assert.assertEquals(GLContext.CONTEXT_CURRENT, context.makeCurrent()); // recursive: lock +1
93
94 Assert.assertEquals(true, context.isOwner(Thread.currentThread()));
95 Assert.assertEquals(lockCount+1, context.getLockCount());
96 Assert.assertEquals(true, context.isCurrent());
97 }
98 private void releaseRecursive(final GLContextImpl context, final int lockCount) {
99 Assert.assertEquals(true, context.isOwner(Thread.currentThread()));
100 Assert.assertEquals(lockCount, context.getLockCount());
101 Assert.assertEquals(true, context.isCurrent()); // still current
102
103 context.release(); // recursive: lock -1
104
105 Assert.assertEquals(true, context.isOwner(Thread.currentThread()));
106 Assert.assertEquals(lockCount-1, context.getLockCount());
107 Assert.assertEquals(true, context.isCurrent()); // still current
108 }
109
110 @Override
111 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
112
113 @Override
114 public void init(final GLAutoDrawable drawable) { }
115
116 @Override
117 public void dispose(final GLAutoDrawable drawable) { }
118
119 @Override
120 public void display(final GLAutoDrawable drawable) {
121 final GLContextImpl context = (GLContextImpl)drawable.getContext();
122 makeCurrentRecursive(context, 1);
123 releaseRecursive(context, 2);
124 }
125 });
126 window.addGLEventListener(new GearsES2());
127
128 try {
129 window.setVisible(true);
130 window.display();
131 } finally {
132 animator.stop();
133 window.destroy();
134 }
135 }
136
137 public static void main(final String args[]) {
138 org.junit.runner.JUnitCore.main(TestBug669RecursiveGLContext01NEWT.class.getName());
139 }
140
141}
142
Specifies a set of OpenGL capabilities.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
static final int CONTEXT_CURRENT
Indicates that the context was made current during the last call to makeCurrent, value {@value}.
Definition: GLContext.java:114
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 final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.