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

RemoveCurrent method for thread context sharing #398

Merged
merged 10 commits into from
Sep 5, 2018
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# All examples depend on Pangolin GUI
if(BUILD_PANGOLIN_GUI)
add_subdirectory(HelloPangolin)
add_subdirectory(HelloPangolinThreads)
add_subdirectory(SimpleMultiDisplay)
add_subdirectory(SimpleDisplayImage)
add_subdirectory(SimpleScene)
Expand Down
6 changes: 6 additions & 0 deletions examples/HelloPangolinThreads/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Find Pangolin (https://github.com/stevenlovegrove/Pangolin)
find_package(Pangolin 0.5 REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})

add_executable(HelloPangolinThreads main.cpp)
target_link_libraries(HelloPangolinThreads ${Pangolin_LIBRARIES})
64 changes: 64 additions & 0 deletions examples/HelloPangolinThreads/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <pangolin/pangolin.h>
#include <thread>

static const std::string window_name = "HelloPangolinThreads";

void setup() {
// create a window and bind its context to the main thread
pangolin::CreateWindowAndBind(window_name, 640, 480);

// enable depth
glEnable(GL_DEPTH_TEST);

// unset the current context from the main thread
pangolin::GetBoundWindow()->RemoveCurrent();
}

void run() {
// fetch the context and bind it to this thread
pangolin::BindToContext(window_name);

// we manually need to restore the properties of the context
glEnable(GL_DEPTH_TEST);

// Define Projection and initial ModelView matrix
pangolin::OpenGlRenderState s_cam(
pangolin::ProjectionMatrix(640,480,420,420,320,240,0.2,100),
pangolin::ModelViewLookAt(-2,2,-2, 0,0,0, pangolin::AxisY)
);

// Create Interactive View in window
pangolin::Handler3D handler(s_cam);
pangolin::View& d_cam = pangolin::CreateDisplay()
.SetBounds(0.0, 1.0, 0.0, 1.0, -640.0f/480.0f)
.SetHandler(&handler);

while( !pangolin::ShouldQuit() )
{
// Clear screen and activate view to render into
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);

// Render OpenGL Cube
pangolin::glDrawColouredCube();

// Swap frames and Process Events
pangolin::FinishFrame();
}

// unset the current context from the main thread
pangolin::GetBoundWindow()->RemoveCurrent();
}

int main( int /*argc*/, char** /*argv*/ )
{
// create window and context in the main thread
setup();

// use the context in a separate rendering thread
std::thread render_loop;
render_loop = std::thread(run);
render_loop.join();

return 0;
}
2 changes: 2 additions & 0 deletions include/pangolin/display/device/OsxWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct OsxWindow : public PangolinGl

void MakeCurrent() override;

void RemoveCurrent() override;

void SwapBuffers() override;

void ProcessEvents() override;
Expand Down
2 changes: 2 additions & 0 deletions include/pangolin/display/device/WinWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct WinWindow : public PangolinGl

void MakeCurrent() override;

void RemoveCurrent() override;

void SwapBuffers() override;

void ProcessEvents() override;
Expand Down
2 changes: 2 additions & 0 deletions include/pangolin/display/device/X11Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ struct X11Window : public PangolinGl

void MakeCurrent() override;

void RemoveCurrent() override;

void SwapBuffers() override;

void ProcessEvents() override;
Expand Down
4 changes: 4 additions & 0 deletions include/pangolin/display/display_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ struct PANGOLIN_EXPORT PangolinGl : public WindowInterface
pango_print_warn("MakeCurrent: Not available with non-pangolin window.\n");
}

virtual void RemoveCurrent() override {
pango_print_warn("RemoveCurrent: Not available with non-pangolin window.\n");
}

virtual void Move(int /*x*/, int /*y*/) override {
pango_print_warn("Move: Not available with non-pangolin window.\n");
}
Expand Down
15 changes: 15 additions & 0 deletions include/pangolin/display/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,25 @@ class WindowInterface
virtual ~WindowInterface() {}

virtual void ToggleFullscreen() = 0;

virtual void Move(int x, int y) = 0;

virtual void Resize(unsigned int w, unsigned int h) = 0;

/**
* @brief MakeCurrent set the current context
* to be called in a thread before accessing OpenGL
*/
virtual void MakeCurrent() = 0;

/**
* @brief RemoveCurrent remove the current context
* to be called at the end of a thread
*/
virtual void RemoveCurrent() = 0;

virtual void ProcessEvents() = 0;

virtual void SwapBuffers() = 0;
};

Expand Down
6 changes: 6 additions & 0 deletions src/display/device/display_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ inline void FixOsxFocus()
void OsxWindow::MakeCurrent()
{
[[view openGLContext] makeCurrentContext];
context = this;
}

void OsxWindow::RemoveCurrent()
{
[NSOpenGLContext clearCurrentContext];
}

void OsxWindow::SwapBuffers()
Expand Down
6 changes: 6 additions & 0 deletions src/display/device/display_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ struct WaylandWindow : public PangolinGl

void MakeCurrent() override;

void RemoveCurrent() override;

void SwapBuffers() override;

void ProcessEvents() override;
Expand Down Expand Up @@ -836,6 +838,10 @@ void WaylandWindow::MakeCurrent() {
context = this;
}

void WaylandWindow::RemoveCurrent() {
eglMakeCurrent(display->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}

void WaylandWindow::ToggleFullscreen() {
is_fullscreen = !is_fullscreen; // state for Pangolin
display->is_fullscreen = is_fullscreen; // state for Wayland
Expand Down
Loading