JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLContextSurfaceLockNEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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 java.io.IOException;
32import java.util.concurrent.atomic.AtomicInteger;
33
34import com.jogamp.common.os.Platform;
35import com.jogamp.common.util.InterruptSource;
36import com.jogamp.junit.util.JunitTracer;
37import com.jogamp.nativewindow.NativeSurface;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLCapabilities;
40import com.jogamp.opengl.GLEventListener;
41import com.jogamp.opengl.GLProfile;
42
43import org.junit.Test;
44import org.junit.BeforeClass;
45import org.junit.FixMethodOrder;
46import org.junit.runners.MethodSorters;
47
48import com.jogamp.newt.opengl.GLWindow;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
50import com.jogamp.opengl.test.junit.util.UITestCase;
51
52@FixMethodOrder(MethodSorters.NAME_ASCENDING)
54 static final int demoSizePos = 80;
55
56 public abstract class MyRunnable implements Runnable {
57 final Object postSync;
58 final int id;
59 boolean done = false;
60
61 public MyRunnable(final Object postSync, final int id) {
62 this.postSync = postSync;
63 this.id = id;
64 }
65
66 public boolean done() { return done; }
67 }
68
69 public class RudeAnimator extends MyRunnable {
70 private final GLAutoDrawable glad;
71 private final int frameCount;
72
73 public RudeAnimator(final GLAutoDrawable glad, final int frameCount, final Object postSync, final int id) {
74 super(postSync, id);
75 this.glad = glad;
76 this.frameCount = frameCount;
77 }
78
79 @Override
80 public void run() {
81 System.err.println("Animatr "+id+", count "+frameCount+": PRE: "+Thread.currentThread().getName());
82
83 for(int c=0; c<frameCount; c++) {
84 System.err.println("Animatr "+id+": Action "+c+" / "+frameCount+": "+Thread.currentThread().getName());
85
86 glad.display();
87 }
88
89 System.err.println("Animatr "+id+": DONE/SYNC: "+Thread.currentThread().getName());
90 synchronized (postSync) {
91 done = true;
92 System.err.println("Animatr "+id+": END: "+Thread.currentThread().getName());
93 postSync.notifyAll();
94 }
95 }
96 }
97
98 /**
99 * Emulates a resize behavior with immediate display() call
100 * while the surface is still locked.
101 */
102 public class RudeResizer extends MyRunnable {
103 private final GLWindow win;
104 private final int actionCount;
105
106 public RudeResizer(final GLWindow win, final int actionCount, final Object postSync, final int id) {
107 super(postSync, id);
108 this.win = win;
109 this.actionCount = actionCount;
110 }
111
112 @Override
113 public void run() {
114 System.err.println("Resizer "+id+", count "+actionCount+": PRE: "+Thread.currentThread().getName());
115
116 for(int c=0; c<actionCount; c++) {
117 final int _c = c;
118 win.runOnEDTIfAvail(true, new Runnable() {
119 int i = _c;
120 @Override
121 public void run() {
122 System.err.println("Resizer "+id+": Action "+i+" / "+actionCount+": "+Thread.currentThread().getName());
123 // Normal resize, may trigger immediate display within lock
124 win.setSize(win.getSurfaceWidth()+1, win.getSurfaceHeight()+1);
125
126 // Force display within surface lock.
127 // This procedure emulates the sensitive behavior
128 // for all platforms directly.
129 final int res = win.lockSurface();
131 try {
132 win.display();
133 } finally {
134 win.unlockSurface();
135 }
136 }
137 }});
138 }
139
140 System.err.println("Resizer "+id+": DONE/SYNC: "+Thread.currentThread().getName());
141 synchronized (postSync) {
142 done = true;
143 System.err.println("Resizer "+id+": END: "+Thread.currentThread().getName());
144 postSync.notifyAll();
145 }
146 }
147 }
148
149 protected static boolean done(final MyRunnable[] tasks) {
150 for(int i=tasks.length-1; i>=0; i--) {
151 if(!tasks[i].done()) {
152 return false;
153 }
154 }
155 return true;
156 }
157
158 protected static boolean isDead(final Thread[] threads) {
159 for(int i=threads.length-1; i>=0; i--) {
160 if(threads[i].isAlive()) {
161 return false;
162 }
163 }
164 return true;
165 }
166
167 protected static class MyEventCounter implements GLEventListener {
168 AtomicInteger reshapeCount = new AtomicInteger(0);
169 AtomicInteger displayCount = new AtomicInteger(0);
170
171 @Override
172 public void init(final GLAutoDrawable drawable) {
173 }
174
175 @Override
176 public void dispose(final GLAutoDrawable drawable) {
177 System.err.println("*** reshapes: "+reshapeCount+", displays "+displayCount);
178 }
179
180 @Override
181 public void display(final GLAutoDrawable drawable) {
182 displayCount.incrementAndGet();
183 }
184
185 @Override
186 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
187 reshapeCount.incrementAndGet();
188 }
189
190 public void reset() {
191 reshapeCount.set(0);
192 displayCount.set(0);
193 }
194 }
195
196 protected void runJOGLTasks(final int animThreadCount, final int frameCount, final int reszThreadCount, final int resizeCount) throws InterruptedException {
198 final MyEventCounter myEventCounter = new MyEventCounter();
199
200 glWindow.addGLEventListener(new GearsES2(0));
201 glWindow.addGLEventListener(myEventCounter);
202 glWindow.setPosition(demoSizePos, demoSizePos);
203 glWindow.setSize(demoSizePos, demoSizePos);
204 glWindow.setVisible(true);
205
206 final String currentThreadName = Thread.currentThread().getName();
207 final Object sync = new Object();
208 final MyRunnable[] animTasks = new MyRunnable[animThreadCount];
209 final MyRunnable[] resizeTasks = new MyRunnable[animThreadCount];
210 final InterruptSource.Thread[] animThreads = new InterruptSource.Thread[reszThreadCount];
211 final InterruptSource.Thread[] resizeThreads = new InterruptSource.Thread[reszThreadCount];
212
213 System.err.println("animThreadCount "+animThreadCount+", frameCount "+frameCount);
214 System.err.println("reszThreadCount "+reszThreadCount+", resizeCount "+resizeCount);
215 System.err.println("tasks "+(animTasks.length+resizeTasks.length)+", threads "+(animThreads.length+resizeThreads.length));
216
217 for(int i=0; i<animThreadCount; i++) {
218 System.err.println("create anim task/thread "+i);
219 animTasks[i] = new RudeAnimator(glWindow, frameCount, sync, i);
220 animThreads[i] = new InterruptSource.Thread(null, animTasks[i], currentThreadName+"-anim"+i);
221 }
222 for(int i=0; i<reszThreadCount; i++) {
223 System.err.println("create resz task/thread "+i);
224 resizeTasks[i] = new RudeResizer(glWindow, resizeCount, sync, i);
225 resizeThreads[i] = new InterruptSource.Thread(null, resizeTasks[i], currentThreadName+"-resz"+i);
226 }
227
228 myEventCounter.reset();
229
230 int j=0, k=0;
231 for(int i=0; i<reszThreadCount+animThreadCount; i++) {
232 if(0==i%2) {
233 System.err.println("start resize thread "+j);
234 resizeThreads[j++].start();
235 } else {
236 System.err.println("start anim thread "+k);
237 animThreads[k++].start();
238 }
239 }
240 synchronized (sync) {
241 while(!done(resizeTasks) || !done(animTasks)) {
242 try {
243 sync.wait();
244 } catch (final InterruptedException e) {
245 throw new RuntimeException(e);
246 }
247 }
248 }
249 int i=0;
250 while(i<30 && !isDead(animThreads) && !isDead(resizeThreads)) {
251 Thread.sleep(100);
252 i++;
253 }
254
255 glWindow.destroy();
256 }
257
258 @Test
259 public void test01_1A1RThreads_100Resizes() throws InterruptedException {
260 runJOGLTasks(1, 200, 1, 100);
261 }
262
263 @Test
264 public void test01_3A3RThreads_50Resizes() throws InterruptedException {
265 runJOGLTasks(3, 100, 3, 50);
266 }
267
268 @BeforeClass
269 public static void beforeClass() throws Exception {
270 if( !manual_test ) {
271 if( Platform.OSType.MACOS == Platform.getOSType() ) {
272 JunitTracer.setTestSupported(false);
273 }
274 }
275 }
276 static boolean manual_test = false;
277
278 public static void main(final String args[]) throws IOException {
279 manual_test = true;
280 for(int i=0; i<args.length; i++) {
281 if(args[i].equals("-time")) {
282 i++;
283 try {
284 // duration = Integer.parseInt(args[i]);
285 } catch (final Exception ex) { ex.printStackTrace(); }
286 }
287 }
288 final String tstname = TestGLContextSurfaceLockNEWT.class.getName();
289 org.junit.runner.JUnitCore.main(tstname);
290 }
291
292}
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int lockSurface()
Lock the surface of this native window.
Definition: GLWindow.java:994
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
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 int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
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 void unlockSurface()
Unlock the surface of this native window.
Definition: GLWindow.java:999
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final void runOnEDTIfAvail(final boolean wait, final Runnable task)
Definition: GLWindow.java:857
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
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
RudeAnimator(final GLAutoDrawable glad, final int frameCount, final Object postSync, final int id)
Emulates a resize behavior with immediate display() call while the surface is still locked.
RudeResizer(final GLWindow win, final int actionCount, final Object postSync, final int id)
void runJOGLTasks(final int animThreadCount, final int frameCount, final int reszThreadCount, final int resizeCount)
Provides low-level information required for hardware-accelerated rendering using a surface in a platf...
static final int LOCK_SURFACE_NOT_READY
Returned by lockSurface() if the surface is not ready to be locked, {@value}.
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.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.