Skip to content
Snippets Groups Projects
Commit e2f9340a authored by Doug Binks's avatar Doug Binks
Browse files

Improved usercontext test with threading

parent 58aad6c1
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,7 @@ add_executable(iconify iconify.c ${GETOPT} ${GLAD_GL}) ...@@ -28,7 +28,7 @@ add_executable(iconify iconify.c ${GETOPT} ${GLAD_GL})
add_executable(monitors monitors.c ${GETOPT} ${GLAD_GL}) add_executable(monitors monitors.c ${GETOPT} ${GLAD_GL})
add_executable(reopen reopen.c ${GLAD_GL}) add_executable(reopen reopen.c ${GLAD_GL})
add_executable(cursor cursor.c ${GLAD_GL}) add_executable(cursor cursor.c ${GLAD_GL})
add_executable(usercontext usercontext.c ${GLAD_GL}) add_executable(usercontext usercontext.c ${TINYCTHREAD} ${GLAD_GL})
add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD_GL}) add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD_GL})
add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD_GL}) add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD_GL})
......
//========================================================================
// User context test
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//
// This test is intended to verify whether the OpenGL user context part of
// the GLFW API is able to be used from multiple threads
//
//========================================================================
#include "tinycthread.h"
#define GLAD_GL_IMPLEMENTATION #define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h> #include <glad/gl.h>
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
...@@ -9,10 +41,44 @@ static void error_callback(int error, const char* description) ...@@ -9,10 +41,44 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", description); fprintf(stderr, "Error: %s\n", description);
} }
static int thread_main(void* data)
{
GLFWusercontext* usercontext = (GLFWusercontext*)data;
/* set the user context current */
glfwMakeUserContextCurrent(usercontext);
if (glfwGetCurrentContext() != NULL)
{
fprintf(stderr, "Current glfw window context not NULL after glfwMakeUserContextCurrent\n");
glfwTerminate();
return -1;
}
if (glfwGetCurrentUserContext() != usercontext)
{
fprintf(stderr, "Current user context not correct after glfwMakeUserContextCurrent\n");
glfwTerminate();
return -1;
}
/* set the user context to NULL */
glfwMakeUserContextCurrent(NULL);
if (glfwGetCurrentUserContext() != NULL)
{
fprintf(stderr, "Current user context not NULL after glfwMakeContextCurrent\n");
glfwTerminate();
return -1;
}
return 0;
}
int main(void) int main(void)
{ {
GLFWwindow* window; GLFWwindow* window;
GLFWusercontext* usercontext; GLFWusercontext* usercontext;
thrd_t thread_id;
int result, count;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
...@@ -41,7 +107,6 @@ int main(void) ...@@ -41,7 +107,6 @@ int main(void)
return -1; return -1;
} }
/* set the user context current */ /* set the user context current */
glfwMakeUserContextCurrent(usercontext); glfwMakeUserContextCurrent(usercontext);
...@@ -76,9 +141,19 @@ int main(void) ...@@ -76,9 +141,19 @@ int main(void)
glClearColor( 0.4f, 0.3f, 0.4f, 1.0f ); glClearColor( 0.4f, 0.3f, 0.4f, 1.0f );
// Launch a thread which should create and use the usercontext
if (thrd_create(&thread_id, thread_main, usercontext ) !=
thrd_success)
{
fprintf(stderr, "Failed to create secondary thread\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
/* Loop until the user closes the window */ /* Loop 60 times or until the user closes the window */
while (!glfwWindowShouldClose(window)) count = 0;
while (!glfwWindowShouldClose(window) && count++ < 60)
{ {
/* Render here */ /* Render here */
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
...@@ -90,6 +165,35 @@ int main(void) ...@@ -90,6 +165,35 @@ int main(void)
glfwPollEvents(); glfwPollEvents();
} }
thrd_join(thread_id, &result);
/* One more test now the thread has joined */
/* set the user context current */
glfwMakeUserContextCurrent(usercontext);
if (glfwGetCurrentContext() != NULL)
{
fprintf(stderr, "Current glfw window context not NULL after glfwMakeUserContextCurrent\n");
glfwTerminate();
return -1;
}
if (glfwGetCurrentUserContext() != usercontext)
{
fprintf(stderr, "Current user context not correct after glfwMakeUserContextCurrent\n");
glfwTerminate();
return -1;
}
/* set the user context to NULL */
glfwMakeUserContextCurrent(NULL);
if (glfwGetCurrentUserContext() != NULL)
{
fprintf(stderr, "Current user context not NULL after glfwMakeContextCurrent\n");
glfwTerminate();
return -1;
}
glfwDestroyUserContext(usercontext); glfwDestroyUserContext(usercontext);
glfwTerminate(); glfwTerminate();
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment