JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLReadBufferUtilTextureIOWrite01AWT.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.awt.GLCanvas;
39
40import jogamp.nativewindow.jawt.JAWTUtil;
41
42import com.jogamp.opengl.util.Animator;
43import com.jogamp.opengl.util.GLReadBufferUtil;
44import com.jogamp.opengl.util.texture.TextureIO;
45
46import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
47import com.jogamp.opengl.test.junit.util.UITestCase;
48import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
49
50import org.junit.Assert;
51import org.junit.Assume;
52import org.junit.BeforeClass;
53import org.junit.Test;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 static GLProfile glp;
60 static GLCapabilities caps;
61 static int width, height;
62
63 @BeforeClass
64 public static void initClass() {
65 glp = GLProfile.getDefault();
66 Assert.assertNotNull(glp);
67 caps = new GLCapabilities(glp);
68 Assert.assertNotNull(caps);
69 caps.setAlphaBits(1); // req. alpha channel
70 width = 256;
71 height = 256;
72 }
73
74 protected void testWritePNG_Impl(final boolean offscreenLayer) throws InterruptedException {
75 final GLReadBufferUtil screenshotRGB = new GLReadBufferUtil(false, false);
76 final GLReadBufferUtil screenshotRGBA = new GLReadBufferUtil(true, false);
77
78 if(!offscreenLayer && JAWTUtil.isOffscreenLayerRequired()) {
79 System.err.println("onscreen layer n/a");
80 return;
81 }
82 if(offscreenLayer && !JAWTUtil.isOffscreenLayerSupported()) {
83 System.err.println("offscreen layer n/a");
84 return;
85 }
86 final GLCanvas glc = new GLCanvas(caps);
87 glc.setShallUseOffscreenLayer(offscreenLayer); // trigger offscreen layer - if supported
88 final Dimension glc_sz = new Dimension(width, height);
89 glc.setMinimumSize(glc_sz);
90 glc.setPreferredSize(glc_sz);
91 final Frame frame = new Frame(getSimpleTestName("."));
92 Assert.assertNotNull(frame);
93 frame.add(glc);
94
95 glc.setSize(width, height);
96 glc.addGLEventListener(new GearsES2(1));
98 int f = 0;
99 public void init(final GLAutoDrawable drawable) {}
100 public void dispose(final GLAutoDrawable drawable) {}
101 public void display(final GLAutoDrawable drawable) {
102 snapshot(f, null, drawable.getGL(), screenshotRGBA, TextureIO.PNG, null);
103 snapshot(f, null, drawable.getGL(), screenshotRGB, TextureIO.PNG, null);
104 f++;
105 }
106 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
107 });
108
109 final Animator animator = new Animator(glc);
110 animator.setUpdateFPSFrames(60, null);
111
112 try {
113 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
114 public void run() {
115 frame.pack();
116 frame.setVisible(true);
117 }});
118 } catch( final Throwable throwable ) {
119 throwable.printStackTrace();
120 Assume.assumeNoException( throwable );
121 }
122 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc, true, null));
123 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(glc, true, null));
124 Assert.assertEquals(JAWTUtil.isOffscreenLayerSupported() && offscreenLayer,
126 animator.start();
127
128 while(animator.getTotalFPSFrames() < 2) {
129 Thread.sleep(60);
130 }
131
132 animator.stop();
133 try {
134 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
135 public void run() {
136 frame.setVisible(false);
137 frame.remove(glc);
138 frame.dispose();
139 }});
140 } catch( final Throwable throwable ) {
141 throwable.printStackTrace();
142 Assume.assumeNoException( throwable );
143 }
144 }
145
146 @Test
147 public void testOnscreenWritePNG() throws InterruptedException {
148 testWritePNG_Impl(false);
149 }
150
151 @Test
152 public void testOffscreenWritePNG() throws InterruptedException {
153 testWritePNG_Impl(true);
154 }
155
156 public static void main(final String args[]) {
157 org.junit.runner.JUnitCore.main(TestGLReadBufferUtilTextureIOWrite01AWT.class.getName());
158 }
159}
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
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.
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.