JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLReadBufferUtilTextureIOWrite02AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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.util.texture;
30
31import java.awt.Dimension;
32import java.awt.Frame;
33
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLEventListener;
37import com.jogamp.opengl.GLProfile;
38import com.jogamp.opengl.Threading;
39import com.jogamp.opengl.awt.GLCanvas;
40
41import jogamp.nativewindow.jawt.JAWTUtil;
42
43import com.jogamp.opengl.util.Animator;
44import com.jogamp.opengl.util.GLReadBufferUtil;
45import com.jogamp.opengl.util.texture.TextureIO;
46
47import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
48import com.jogamp.opengl.test.junit.util.UITestCase;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
50
51import org.junit.Assert;
52import org.junit.Assume;
53import org.junit.BeforeClass;
54import org.junit.Test;
55import org.junit.FixMethodOrder;
56import org.junit.runners.MethodSorters;
57
58@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 static GLProfile glp;
61 static GLCapabilities caps;
62 static int width, height;
63
64 @BeforeClass
65 public static void initClass() {
66 glp = GLProfile.getDefault();
67 Assert.assertNotNull(glp);
68 caps = new GLCapabilities(glp);
69 Assert.assertNotNull(caps);
70 caps.setAlphaBits(1); // req. alpha channel
71 width = 64;
72 height = 64;
73 }
74
75 protected void testWritePNGWithResizeImpl(final boolean offscreenLayer) throws InterruptedException {
76 if(!offscreenLayer && JAWTUtil.isOffscreenLayerRequired()) {
77 System.err.println("onscreen layer n/a");
78 return;
79 }
80 if(offscreenLayer && !JAWTUtil.isOffscreenLayerSupported()) {
81 System.err.println("offscreen layer n/a");
82 return;
83 }
84 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
85 final GLCanvas glc = new GLCanvas(caps);
86 glc.setShallUseOffscreenLayer(offscreenLayer); // trigger offscreen layer - if supported
87 final Dimension glc_sz = new Dimension(width, height);
88 glc.setMinimumSize(glc_sz);
89 glc.setPreferredSize(glc_sz);
90 glc.setSize(glc_sz);
91 final Frame frame = new Frame(getSimpleTestName("."));
92 Assert.assertNotNull(frame);
93 frame.add(glc);
94
95 glc.addGLEventListener(new GearsES2(1));
97 int i=0, fw_old=0, dw_old=0, c=0;
98 public void init(final GLAutoDrawable drawable) {}
99 public void dispose(final GLAutoDrawable drawable) {}
100 public void display(final GLAutoDrawable drawable) {
101 final int fw = frame.getWidth();
102 final int fh = frame.getHeight();
103 final int dw = drawable.getSurfaceWidth();
104 final int dh = drawable.getSurfaceHeight();
105 final boolean sz_changed = fw_old != fw && dw_old != dw && dw <= 512; // need to check both sizes [frame + drawable], due to async resize of AWT!
106 final boolean snap;
107 if(sz_changed) {
108 c++;
109 snap = c>3; // only snap the 3rd image ..
110 } else {
111 snap = false;
112 }
113
114 if(snap) {
115 System.err.println("XXX: ["+fw_old+", "+dw_old+"], "+fw+"x"+fh+", "+dw+"x"+dh+", sz_changed "+sz_changed+", snap "+snap);
116 c=0;
117 snapshot(i++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
118 dw_old = dw;
119 fw_old = fw;
120 Threading.invoke(true, new Runnable() {
121 public void run() {
122 final Dimension new_sz = new Dimension(2*dw, 2*dh);
123 glc.setMinimumSize(new_sz);
124 glc.setPreferredSize(new_sz);
125 glc.setSize(new_sz);
126 frame.pack();
127 frame.validate();
128 } }, glc.getTreeLock());
129 }
130 }
131 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
132 });
133
134 final Animator animator = new Animator(glc);
135 animator.setUpdateFPSFrames(60, null);
136
137 try {
138 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
139 public void run() {
140 frame.pack();
141 frame.setVisible(true);
142 }});
143 } catch( final Throwable throwable ) {
144 throwable.printStackTrace();
145 Assume.assumeNoException( throwable );
146 }
147 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc, true, null));
148 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(glc, true, null));
149 Assert.assertEquals(JAWTUtil.isOffscreenLayerSupported() && offscreenLayer,
151 animator.start();
152
153 while(animator.getTotalFPSFrames() < 30) {
154 Thread.sleep(60);
155 }
156
157 animator.stop();
158 try {
159 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
160 public void run() {
161 frame.setVisible(false);
162 frame.remove(glc);
163 frame.dispose();
164 }});
165 } catch( final Throwable throwable ) {
166 throwable.printStackTrace();
167 Assume.assumeNoException( throwable );
168 }
169 }
170
171 @Test
172 public void testOnscreenWritePNGWithResize() throws InterruptedException {
173 testWritePNGWithResizeImpl(false);
174 }
175
176 @Test
177 public void testOffscreenWritePNGWithResize() throws InterruptedException {
178 testWritePNGWithResizeImpl(true);
179 }
180
181 public static void main(final String args[]) {
182 org.junit.runner.JUnitCore.main(TestGLReadBufferUtilTextureIOWrite02AWT.class.getName());
183 }
184}
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
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
This API provides access to the threading model for the implementation of the classes in this package...
Definition: Threading.java:119
static final void invoke(final boolean wait, final Runnable r, final Object lock)
If not isOpenGLThread() and the lock is not being hold by this thread, invoke Runnable r on the OpenG...
Definition: Threading.java:218
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
Definition: GLCanvas.java:304
final boolean isOffscreenLayerSurfaceEnabled()
Returns true if this instance uses an offscreen layer, otherwise false.
Definition: GLCanvas.java:314
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, 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
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.