Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setSwapInterval operation on AWTGLCanvas #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/org/lwjgl/opengl/awt/AWTGLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public final void swapBuffers() {
platformCanvas.swapBuffers();
}

public final void setSwapInterval(int interval) {
platformCanvas.setSwapInterval(interval);
}

/**
* Returns Graphics object that ignores {@link Graphics#clearRect(int, int, int, int)}
* calls.
Expand Down
1 change: 1 addition & 0 deletions src/org/lwjgl/opengl/awt/PlatformGLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface PlatformGLCanvas {
boolean makeCurrent(long context);
boolean isCurrent(long context);
boolean swapBuffers();
void setSwapInterval(int interval);
boolean delayBeforeSwapNV(float seconds);
void lock() throws AWTException;
void unlock() throws AWTException;
Expand Down
20 changes: 17 additions & 3 deletions src/org/lwjgl/opengl/awt/PlatformLinuxGLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.opengl.GLX.*;
import static org.lwjgl.opengl.GLX13.*;
import static org.lwjgl.opengl.GLXEXTSwapControl.*;
import static org.lwjgl.opengl.GLXARBCreateContext.*;
import static org.lwjgl.opengl.GLXARBCreateContextProfile.*;
import static org.lwjgl.opengl.GLXARBCreateContextRobustness.*;
Expand All @@ -14,7 +15,8 @@
import java.awt.Canvas;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;

import org.lwjgl.BufferUtils;
Expand Down Expand Up @@ -49,9 +51,14 @@ public class PlatformLinuxGLCanvas implements PlatformGLCanvas {
public long display;
public long drawable;
public JAWTDrawingSurface ds;
public Collection<String> extensions;
public boolean has_GLX_EXT_swap_control, has_GLX_EXT_swap_control_tear;

private long create(int depth, GLData attribs, GLData effective) throws AWTException {
int screen = X11.XDefaultScreen(display);
extensions = new HashSet<String>(Arrays.asList(glXQueryExtensionsString(display, screen).split(" ")));
has_GLX_EXT_swap_control = extensions.contains("GLX_EXT_swap_control");
has_GLX_EXT_swap_control_tear = extensions.contains("GLX_EXT_swap_control_tear");
IntBuffer attrib_list = BufferUtils.createIntBuffer(16 * 2);
attrib_list.put(GLX_DRAWABLE_TYPE).put(GLX_WINDOW_BIT);
attrib_list.put(GLX_RENDER_TYPE).put(GLX_RGBA_BIT);
Expand Down Expand Up @@ -149,6 +156,14 @@ public boolean swapBuffers() {
return true;
}

public void setSwapInterval(int interval) {
if(has_GLX_EXT_swap_control) {
if(interval < 0 && !has_GLX_EXT_swap_control_tear)
interval = -interval;
glXSwapIntervalEXT(display, drawable, interval);
}
}

public boolean delayBeforeSwapNV(float seconds) {
throw new UnsupportedOperationException("NYI");
}
Expand All @@ -160,8 +175,7 @@ public void dispose() {
}
}

private static void verifyGLXCapabilities(long display, int screen, GLData data) throws AWTException {
List<String> extensions = Arrays.asList(glXQueryExtensionsString(display, screen).split(" "));
private void verifyGLXCapabilities(long display, int screen, GLData data) throws AWTException {
if (!extensions.contains("GLX_ARB_create_context")) {
throw new AWTException("GLX_ARB_create_context is unavailable");
}
Expand Down
5 changes: 5 additions & 0 deletions src/org/lwjgl/opengl/awt/PlatformMacOSXGLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ public boolean swapBuffers() {
return true;
}

@Override
public void setSwapInterval(int interval) {
/* XXX: How does this work on MacOS? */
}

@Override
public boolean deleteContext(long context) {
// frees created NSOpenGLView
Expand Down
36 changes: 21 additions & 15 deletions src/org/lwjgl/opengl/awt/PlatformWin32GLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class PlatformWin32GLCanvas implements PlatformGLCanvas {
}

public long hwnd;
public long wglSwapIntervalEXTAddr = 0L;
public boolean has_WGL_EXT_swap_control_tear = false;
public long wglDelayBeforeSwapNVAddr = 0L;
public boolean wglDelayBeforeSwapNVAddr_set = false;
public JAWTDrawingSurface ds;
Expand Down Expand Up @@ -158,7 +160,7 @@ public long create(Canvas canvas, GLData attribs, GLData effective) throws AWTEx
}
}

private static long create(MemoryStack stack, long windowHandle, long dummyWindowHandle, GLData attribs, GLData effective) throws AWTException {
private long create(MemoryStack stack, long windowHandle, long dummyWindowHandle, GLData attribs, GLData effective) throws AWTException {
long bufferAddr = stack.nmalloc(4, (4*2) << 2);

// Validate context attributes
Expand Down Expand Up @@ -252,6 +254,11 @@ private static long create(MemoryStack stack, long windowHandle, long dummyWindo
throw new AWTException("Could not release dummy DC");
}

if (wglExtensionsList.contains("WGL_EXT_swap_control")) {
this.wglSwapIntervalEXTAddr = wglGetProcAddress("wglSwapIntervalEXT");
this.has_WGL_EXT_swap_control_tear = wglExtensionsList.contains("WGL_EXT_swap_control_tear");
}

// For some constellations of context attributes, we can stop right here.
if (!atLeast30(attribs.majorVersion, attribs.minorVersion) && attribs.samples == 0 && !attribs.sRGB && !attribs.pixelFormatFloat
&& attribs.contextReleaseBehavior == null && !attribs.robustness && attribs.api != API.GLES) {
Expand All @@ -267,16 +274,14 @@ private static long create(MemoryStack stack, long windowHandle, long dummyWindo
long context = wglCreateContext(hDC);

if (attribs.swapInterval != null) {
boolean has_WGL_EXT_swap_control = wglExtensionsList.contains("WGL_EXT_swap_control");
if (!has_WGL_EXT_swap_control) {
if (wglSwapIntervalEXTAddr == 0) {
ReleaseDC(windowHandle, hDC);
wglMakeCurrent(currentDc, currentContext);
wglDeleteContext(context);
throw new AWTException("Swap interval requested but WGL_EXT_swap_control is unavailable");
}
if (attribs.swapInterval < 0) {
// Only allowed if WGL_EXT_swap_control_tear is available
boolean has_WGL_EXT_swap_control_tear = wglExtensionsList.contains("WGL_EXT_swap_control_tear");
if (!has_WGL_EXT_swap_control_tear) {
ReleaseDC(windowHandle, hDC);
wglMakeCurrent(currentDc, currentContext);
Expand All @@ -292,10 +297,7 @@ private static long create(MemoryStack stack, long windowHandle, long dummyWindo
wglDeleteContext(context);
throw new AWTException("Could not make GL context current");
}
long wglSwapIntervalEXTAddr = wglGetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXTAddr != 0L) {
callI(attribs.swapInterval, wglSwapIntervalEXTAddr);
}
callI(attribs.swapInterval, wglSwapIntervalEXTAddr);
}

if (attribs.swapGroupNV > 0 || attribs.swapBarrierNV > 0) {
Expand Down Expand Up @@ -611,27 +613,22 @@ else if (attribs.contextReleaseBehavior == ReleaseBehavior.FLUSH)
// Make context current for next operations
wglMakeCurrent(hDC, newCtx);
if (attribs.swapInterval != null) {
boolean has_WGL_EXT_swap_control = wglExtensionsList.contains("WGL_EXT_swap_control");
if (!has_WGL_EXT_swap_control) {
if (wglSwapIntervalEXTAddr == 0) {
ReleaseDC(windowHandle, hDC);
wglMakeCurrent(currentDc, currentContext);
wglDeleteContext(newCtx);
throw new AWTException("Swap interval requested but WGL_EXT_swap_control is unavailable");
}
if (attribs.swapInterval < 0) {
// Only allowed if WGL_EXT_swap_control_tear is available
boolean has_WGL_EXT_swap_control_tear = wglExtensionsList.contains("WGL_EXT_swap_control_tear");
if (!has_WGL_EXT_swap_control_tear) {
ReleaseDC(windowHandle, hDC);
wglMakeCurrent(currentDc, currentContext);
wglDeleteContext(newCtx);
throw new AWTException("Negative swap interval requested but WGL_EXT_swap_control_tear is unavailable");
}
}
long wglSwapIntervalEXTAddr = wglGetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXTAddr != 0L) {
callI(attribs.swapInterval, wglSwapIntervalEXTAddr);
}
callI(attribs.swapInterval, wglSwapIntervalEXTAddr);
}
if (attribs.swapGroupNV > 0 || attribs.swapBarrierNV > 0) {
// Only allowed if WGL_NV_swap_group is available
Expand Down Expand Up @@ -770,6 +767,15 @@ public boolean swapBuffers() {
return ret;
}

@Override
public void setSwapInterval(int interval) {
if (wglSwapIntervalEXTAddr != 0) {
if (interval < 0 && !has_WGL_EXT_swap_control_tear)
interval = -interval;
callI(interval, wglSwapIntervalEXTAddr);
}
}

@Override
public boolean delayBeforeSwapNV(float seconds) {
if (!wglDelayBeforeSwapNVAddr_set) {
Expand Down
Loading