2008-08-19 12:46:03 -04:00
|
|
|
/*
|
2008-04-07 14:42:36 -04:00
|
|
|
* Copyright (c) 2002-2008 LWJGL Project
|
2005-02-20 06:24:22 -05:00
|
|
|
* All rights reserved.
|
2008-08-19 12:46:03 -04:00
|
|
|
*
|
2005-02-20 06:24:22 -05:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2008-08-19 12:46:03 -04:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2005-02-20 06:24:22 -05:00
|
|
|
* met:
|
2008-08-19 12:46:03 -04:00
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2005-02-20 06:24:22 -05:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2008-08-19 12:46:03 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2005-02-20 06:24:22 -05:00
|
|
|
* from this software without specific prior written permission.
|
2008-08-19 12:46:03 -04:00
|
|
|
*
|
2005-02-20 06:24:22 -05:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2008-08-19 12:46:03 -04:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
2005-02-20 06:24:22 -05:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2008-08-19 12:46:03 -04:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2005-02-20 06:24:22 -05:00
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include "org_lwjgl_opengl_LinuxContextImplementation.h"
|
|
|
|
#include "extgl_glx.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "common_tools.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2005-02-24 05:44:34 -05:00
|
|
|
GLXExtensions extension_flags;
|
2005-02-20 06:24:22 -05:00
|
|
|
GLXContext context;
|
|
|
|
} X11Context;
|
|
|
|
|
|
|
|
static bool checkContext(JNIEnv *env, Display *display, GLXContext context) {
|
|
|
|
if (context == NULL) {
|
|
|
|
throwException(env, "Could not create GLX context");
|
|
|
|
return false;
|
|
|
|
}
|
2006-02-23 14:29:49 -05:00
|
|
|
/*
|
|
|
|
* Ditched the requirement that contexts have to be direct. It was
|
|
|
|
* never true that all accelerated contexts are direct, but it
|
2006-06-12 09:01:05 -04:00
|
|
|
* was a reasonable test until the appearance of Xgl and AIGLX.
|
2006-02-23 14:29:49 -05:00
|
|
|
* Now the test is at best useless, and at worst wrong,
|
|
|
|
* in case the current X server accelerates indirect rendering.
|
|
|
|
*/
|
|
|
|
/* jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Display.allowSoftwareOpenGL");
|
2005-11-20 08:19:39 -05:00
|
|
|
if (!allow_software_acceleration && lwjgl_glXIsDirect(display, context) == False) {
|
|
|
|
lwjgl_glXDestroyContext(display, context);
|
2005-02-20 06:24:22 -05:00
|
|
|
throwException(env, "Could not create a direct GLX context");
|
|
|
|
return false;
|
2006-02-23 14:29:49 -05:00
|
|
|
}*/
|
2005-02-20 06:24:22 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-24 16:30:30 -04:00
|
|
|
static void createContextGLX13(JNIEnv *env, X11PeerInfo *peer_info, X11Context *context_info, jobject attribs, GLXContext shared_context) {
|
2005-02-20 06:24:22 -05:00
|
|
|
GLXFBConfig *config = getFBConfigFromPeerInfo(env, peer_info);
|
|
|
|
if (config == NULL)
|
|
|
|
return;
|
2009-04-24 16:30:30 -04:00
|
|
|
GLXContext context;
|
|
|
|
if (attribs) {
|
|
|
|
const int *attrib_list = (const int *)(*env)->GetDirectBufferAddress(env, attribs);
|
|
|
|
context = lwjgl_glXCreateContextAttribsARB(peer_info->display, *config, shared_context, True, attrib_list);
|
|
|
|
} else {
|
2009-04-24 16:35:33 -04:00
|
|
|
int render_type;
|
|
|
|
if (lwjgl_glXGetFBConfigAttrib(peer_info->display, *config, GLX_RENDER_TYPE, &render_type) != 0) {
|
|
|
|
throwException(env, "Could not get GLX_RENDER_TYPE attribute");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int context_render_type = (render_type & GLX_RGBA_FLOAT_BIT) != 0 ? GLX_RGBA_FLOAT_TYPE : GLX_RGBA_TYPE;
|
2009-04-24 16:30:30 -04:00
|
|
|
context = lwjgl_glXCreateNewContext(peer_info->display, *config, context_render_type, shared_context, True);
|
|
|
|
}
|
2005-02-20 06:24:22 -05:00
|
|
|
XFree(config);
|
|
|
|
if (!checkContext(env, peer_info->display, context))
|
|
|
|
return;
|
|
|
|
context_info->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createContextGLX(JNIEnv *env, X11PeerInfo *peer_info, X11Context *context_info, GLXContext shared_context) {
|
|
|
|
XVisualInfo *vis_info = getVisualInfoFromPeerInfo(env, peer_info);
|
|
|
|
if (vis_info == NULL)
|
|
|
|
return;
|
2005-11-20 08:19:39 -05:00
|
|
|
GLXContext context = lwjgl_glXCreateContext(peer_info->display, vis_info, shared_context, True);
|
2005-02-20 06:24:22 -05:00
|
|
|
XFree(vis_info);
|
|
|
|
if (!checkContext(env, peer_info->display, context))
|
|
|
|
return;
|
|
|
|
context_info->context = context;
|
|
|
|
}
|
|
|
|
|
2010-09-26 19:43:24 -04:00
|
|
|
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_getGLXContext(JNIEnv *env, jclass clazz, jobject context_handle) {
|
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
|
|
|
return (intptr_t)context_info->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_getDisplay(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_info_handle);
|
|
|
|
return (intptr_t)peer_info->display;
|
|
|
|
}
|
|
|
|
|
2006-01-01 14:50:06 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nSetSwapInterval
|
|
|
|
(JNIEnv *env, jclass clazz, jobject context_handle, jint value)
|
2005-02-20 06:24:22 -05:00
|
|
|
{
|
2005-02-24 05:44:34 -05:00
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
|
|
|
if (context_info->extension_flags.GLX_SGI_swap_control) {
|
2006-01-01 14:50:06 -05:00
|
|
|
lwjgl_glXSwapIntervalSGI(value);
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-22 08:59:33 -05:00
|
|
|
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nCreate
|
2008-08-19 12:46:03 -04:00
|
|
|
(JNIEnv *env , jclass clazz, jobject peer_handle, jobject attribs, jobject shared_context_handle) {
|
2005-02-22 08:59:33 -05:00
|
|
|
jobject context_handle = newJavaManagedByteBuffer(env, sizeof(X11Context));
|
|
|
|
if (context_handle == NULL) {
|
|
|
|
throwException(env, "Could not allocate handle buffer");
|
|
|
|
return NULL;
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_handle);
|
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
2005-02-24 05:44:34 -05:00
|
|
|
GLXExtensions extension_flags;
|
|
|
|
if (!extgl_InitGLX(peer_info->display, peer_info->screen, &extension_flags)) {
|
2005-02-20 06:24:22 -05:00
|
|
|
throwException(env, "Could not initialize GLX");
|
2005-02-22 08:59:33 -05:00
|
|
|
return NULL;
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
GLXContext shared_context = NULL;
|
|
|
|
if (shared_context_handle != NULL) {
|
|
|
|
X11Context *shared_context_info = (*env)->GetDirectBufferAddress(env, shared_context_handle);
|
|
|
|
shared_context = shared_context_info->context;
|
2008-08-19 12:46:03 -04:00
|
|
|
}
|
2005-02-20 06:24:22 -05:00
|
|
|
if (peer_info->glx13) {
|
2009-04-24 16:30:30 -04:00
|
|
|
createContextGLX13(env, peer_info, context_info, extension_flags.GLX_ARB_create_context ? attribs : NULL, shared_context);
|
2005-02-20 06:24:22 -05:00
|
|
|
} else {
|
|
|
|
createContextGLX(env, peer_info, context_info, shared_context);
|
|
|
|
}
|
2005-02-24 05:44:34 -05:00
|
|
|
context_info->extension_flags = extension_flags;
|
2005-02-22 08:59:33 -05:00
|
|
|
return context_handle;
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nDestroy
|
|
|
|
(JNIEnv *env, jclass clazz, jobject peer_handle, jobject context_handle) {
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_handle);
|
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
2005-11-20 08:19:39 -05:00
|
|
|
lwjgl_glXDestroyContext(peer_info->display, context_info->context);
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nReleaseCurrentContext
|
|
|
|
(JNIEnv *env , jclass clazz, jobject peer_info_handle) {
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_info_handle);
|
|
|
|
Bool result;
|
2005-02-24 05:44:34 -05:00
|
|
|
if (peer_info->glx13) {
|
2005-11-20 08:19:39 -05:00
|
|
|
result = lwjgl_glXMakeContextCurrent(peer_info->display, None, None, NULL);
|
2005-02-20 06:24:22 -05:00
|
|
|
} else {
|
2005-11-20 08:19:39 -05:00
|
|
|
result = lwjgl_glXMakeCurrent(peer_info->display, None, NULL);
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
if (!result)
|
|
|
|
throwException(env, "Could not release current context");
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nMakeCurrent
|
|
|
|
(JNIEnv *env, jclass clazz, jobject peer_info_handle, jobject context_handle) {
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_info_handle);
|
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
|
|
|
Bool result;
|
2005-02-24 05:44:34 -05:00
|
|
|
if (peer_info->glx13) {
|
2005-11-20 08:19:39 -05:00
|
|
|
result = lwjgl_glXMakeContextCurrent(peer_info->display, peer_info->drawable, peer_info->drawable, context_info->context);
|
2005-02-20 06:24:22 -05:00
|
|
|
} else {
|
2005-11-20 08:19:39 -05:00
|
|
|
result = lwjgl_glXMakeCurrent(peer_info->display, peer_info->drawable, context_info->context);
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
if (!result)
|
|
|
|
throwException(env, "Could not make context current");
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nIsCurrent
|
|
|
|
(JNIEnv *env, jclass clazz, jobject context_handle) {
|
|
|
|
X11Context *context_info = (*env)->GetDirectBufferAddress(env, context_handle);
|
2005-11-20 08:19:39 -05:00
|
|
|
return context_info->context == lwjgl_glXGetCurrentContext();
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxContextImplementation_nSwapBuffers
|
|
|
|
(JNIEnv *env, jclass clazz, jobject peer_info_handle) {
|
|
|
|
X11PeerInfo *peer_info = (*env)->GetDirectBufferAddress(env, peer_info_handle);
|
2005-11-20 08:19:39 -05:00
|
|
|
lwjgl_glXSwapBuffers(peer_info->display, peer_info->drawable);
|
2005-02-20 06:24:22 -05:00
|
|
|
}
|