JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
GLReadBuffer00BaseAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 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 java.awt.image.BufferedImage;
31import java.io.File;
32import java.io.IOException;
33import java.util.Locale;
34
35import javax.imageio.ImageIO;
36import com.jogamp.opengl.GL;
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLDrawable;
39import com.jogamp.opengl.GLEventListener;
40
41import com.jogamp.opengl.util.GLDrawableUtil;
42import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
43import com.jogamp.opengl.util.texture.TextureIO;
44
45/**
46 * Test synchronous GLAutoDrawable display, swap-buffer and read-pixels
47 * including non-MSAA and MSAA framebuffer.
48 * <p>
49 * See {@link GLReadBuffer00Base} for related bugs and further details.
50 * </p>
51 */
52public abstract class GLReadBuffer00BaseAWT extends GLReadBuffer00Base {
53
54 protected class SnapshotGLELAWT implements GLEventListener {
55 final TextRendererGLEL textRendererGLEL;
56 final AWTGLReadBufferUtil glReadBufferUtil;
57 final boolean skipGLOrientationVerticalFlip;
58 boolean defAutoSwapMode;
59 boolean swapBuffersBeforeRead;
60 int i;
61
62 SnapshotGLELAWT(final TextRendererGLEL textRendererGLEL, final AWTGLReadBufferUtil glReadBufferUtil, final boolean skipGLOrientationVerticalFlip) {
63 this.textRendererGLEL = textRendererGLEL;
64 this.glReadBufferUtil = glReadBufferUtil;
65 this.skipGLOrientationVerticalFlip = skipGLOrientationVerticalFlip;
66 this.defAutoSwapMode = true;
67 this.swapBuffersBeforeRead = false;
68 i = 0;
69 }
70
71 @Override
72 public void init(final GLAutoDrawable drawable) {
73 defAutoSwapMode = drawable.getAutoSwapBufferMode();
74 swapBuffersBeforeRead = GLDrawableUtil.swapBuffersBeforeRead(drawable.getChosenGLCapabilities());
75 drawable.setAutoSwapBufferMode( !swapBuffersBeforeRead );
76 }
77 @Override
78 public void dispose(final GLAutoDrawable drawable) {
79 drawable.setAutoSwapBufferMode( defAutoSwapMode );
80 }
81 @Override
82 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
83 @Override
84 public void display(final GLAutoDrawable drawable) {
85 snapshot(i++, drawable.getGL(), TextureIO.PNG, null);
86 }
87 public void snapshot(final int sn, final GL gl, final String fileSuffix, final String destPath) {
88 final GLDrawable drawable = gl.getContext().getGLReadDrawable();
89 final String postSNDetail = String.format((Locale)null, "awt-usr%03d", textRendererGLEL.userCounter);
90 final String filenameAWT = getSnapshotFilename(sn, postSNDetail,
91 drawable.getChosenGLCapabilities(), drawable.getSurfaceWidth(), drawable.getSurfaceHeight(),
92 glReadBufferUtil.hasAlpha(), fileSuffix, destPath);
93 if( swapBuffersBeforeRead ) {
94 drawable.swapBuffers();
95 // Just to test whether we use the right buffer,
96 // i.e. back-buffer shall no more be required ..
98 } else {
99 gl.glFinish(); // just make sure rendering finished ..
100 }
101
102 final boolean awtOrientation = !( drawable.isGLOriented() && skipGLOrientationVerticalFlip );
103 System.err.println(Thread.currentThread().getName()+": ** screenshot: awtOrient/v-flip "+awtOrientation+", swapBuffersBeforeRead "+swapBuffersBeforeRead+", "+filenameAWT);
104
105 final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(gl, awtOrientation);
106 final File fout = new File(filenameAWT);
107 try {
108 ImageIO.write(image, "png", fout);
109 } catch (final IOException e) {
110 e.printStackTrace();
111 }
112 /**
113 final String filenameJGL = getSnapshotFilename(sn, "jgl",
114 drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(),
115 glReadBufferUtil.hasAlpha(), fileSuffix, destPath);
116 glReadBufferUtil.write(new File(filenameJGL));
117 */
118 }
119 };
120
121}
abstract GLDrawable getGLReadDrawable()
Returns the read-Drawable this context uses for read framebuffer operations.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void snapshot(final int sn, final GL gl, final String fileSuffix, final String destPath)
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.
Test synchronous GLAutoDrawable display, swap-buffer and read-pixels including non-MSAA and MSAA fram...
Test synchronous GLAutoDrawable display, swap-buffer and read-pixels including non-MSAA and MSAA fram...
String getSnapshotFilename(final int sn, String postSNDetail, final GLCapabilitiesImmutable caps, final int width, final int height, final boolean sinkHasAlpha, String fileSuffix, final String destPath)
Providing utility functions dealing w/ GLDrawables, GLAutoDrawable and their GLEventListener.
static final boolean swapBuffersBeforeRead(final GLCapabilitiesImmutable chosenCaps)
Determines whether the chosen GLCapabilitiesImmutable requires a swap-buffers before reading pixels.
GLReadBufferUtil specialization allowing to read out a frambuffer to an AWT BufferedImage utilizing A...
BufferedImage readPixelsToBufferedImage(final GL gl, final boolean awtOrientation)
Read the drawable's pixels to TextureData and Texture, if requested at construction,...
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.
boolean getAutoSwapBufferMode()
Indicates whether automatic buffer swapping is enabled for this drawable.
void setAutoSwapBufferMode(boolean enable)
Enables or disables automatic buffer swapping for this drawable.
GLContext getContext()
Returns the GLContext associated which this GL object.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
void swapBuffers()
Swaps the front and back buffers of this drawable.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
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 glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glFinish()
Entry point to C language function: void {@native glFinish}() Part of GL_ES_VERSION_2_0,...