JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
NEWTGLContext.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.util;
29
30import com.jogamp.newt.Display;
31import com.jogamp.newt.NewtFactory;
32import com.jogamp.newt.Screen;
33import com.jogamp.newt.Window;
34
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLContext;
37import com.jogamp.opengl.GLDrawable;
38import com.jogamp.opengl.GLDrawableFactory;
39import com.jogamp.opengl.util.caps.NonFSAAGLCapsChooser;
40
41import org.junit.Assert;
42
43public class NEWTGLContext {
44
45 public static class WindowContext {
46 public final Window window;
47 public final GLDrawable drawable;
48 public final GLContext context;
49
50 public WindowContext(final Window w, final GLDrawable d, final GLContext c) {
51 window = w;
52 drawable = d;
53 context = c;
54 }
55 }
56
57 public static WindowContext createWindow(final GLCapabilities caps, final int width, final int height, final boolean debugGL) throws InterruptedException {
58 //
59 // Create native windowing resources .. X11/Win/OSX
60 //
61 final Display display = NewtFactory.createDisplay(null); // local display
62 Assert.assertNotNull(display);
63
64 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
65 Assert.assertNotNull(screen);
66
67 final Window window = NewtFactory.createWindow(screen, caps);
68 Assert.assertNotNull(window);
69 window.setSize(width, height);
70 if( !caps.getSampleBuffers() ) {
71 // Make sure to not have FSAA if not requested
72 // TODO: Implement in default chooser?
74 }
75
76 window.setVisible(true);
77 Assert.assertTrue(NewtTestUtil.waitForVisible(window, true, null));
78 Assert.assertTrue(NewtTestUtil.waitForRealized(window, true, null));
79
80 final GLDrawableFactory factory = GLDrawableFactory.getFactory(caps.getGLProfile());
81 final GLDrawable drawable = factory.createGLDrawable(window);
82 Assert.assertNotNull(drawable);
83
84 drawable.setRealized(true);
85 Assert.assertTrue(drawable.isRealized());
86
87 final GLContext context = drawable.createContext(null);
88 Assert.assertNotNull(context);
89
90 context.enableGLDebugMessage(debugGL);
91
92 final int res = context.makeCurrent();
93 Assert.assertTrue(GLContext.CONTEXT_CURRENT_NEW==res || GLContext.CONTEXT_CURRENT==res);
94
95 return new WindowContext(window, drawable, context);
96 }
97
98 public static void destroyWindow(final WindowContext winctx) {
99 final GLDrawable drawable = winctx.context.getGLDrawable();
100
101 Assert.assertNotNull(winctx.context);
102 winctx.context.destroy();
103
104 Assert.assertNotNull(drawable);
105 drawable.setRealized(false);
106
107 Assert.assertNotNull(winctx.window);
108 winctx.window.destroy();
109 }
110
111}
static Display createDisplay(final String name)
Create a Display entity.
static Window createWindow(final CapabilitiesImmutable caps)
Create a top level Window entity on the default Display and default Screen.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
Specifies a set of OpenGL capabilities.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
abstract int makeCurrent()
Makes this GLContext current on the calling thread.
static final int CONTEXT_CURRENT
Indicates that the context was made current during the last call to makeCurrent, value {@value}.
Definition: GLContext.java:114
abstract void enableGLDebugMessage(boolean enable)
Enables or disables the GLDebugOutput feature of extension GLExtensions#ARB_debug_output or GLExtensi...
abstract void destroy()
Destroys this OpenGL context and frees its associated resources.
static final int CONTEXT_CURRENT_NEW
Indicates that a newly-created context was made current during the last call to makeCurrent,...
Definition: GLContext.java:116
abstract GLDrawable createGLDrawable(NativeSurface target)
Returns an unrealized GLDrawable according to it's chosen GLCapabilitiesImmutable,...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
WindowContext(final Window w, final GLDrawable d, final GLContext c)
static WindowContext createWindow(final GLCapabilities caps, final int width, final int height, final boolean debugGL)
static void destroyWindow(final WindowContext winctx)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
Custom GLCapabilitiesChooser, filtering out all full screen anti-aliasing (FSAA, multisample) capabil...
Specifying NEWT's Window functionality:
Definition: Window.java:115
CapabilitiesChooser setCapabilitiesChooser(CapabilitiesChooser chooser)
Set the CapabilitiesChooser to help determine the native visual type.
void setSize(int width, int height)
Sets the size of the window's client area in window units, excluding decorations.
void setVisible(boolean visible)
Calls setVisible(true, visible), i.e.
void destroy()
Destroys this window incl.releasing all related resources.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
boolean isRealized()
Returns true if this drawable is realized, otherwise false.
void setRealized(boolean realized)
Indicates to GLDrawable implementations whether the underlying surface has been created and can be dr...
GLContext createContext(GLContext shareWith)
Creates a new context for drawing to this drawable that will optionally share buffer objects,...