Skip to content
Merged
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
2 changes: 2 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Some important variables for CMake are:

- GLVIS_USE_EGL: Use EGL for headless rendering. Default is "OFF".

- GLVIS_USE_CGL: Use CGL for headless rendering on macos. Default is "OFF".

- GLVIS_MULTISAMPLE and GLVIS_MS_LINEWIDTH: See building considerations below
for more information on these variables.

Expand Down
19 changes: 10 additions & 9 deletions lib/aux_vis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <iostream>
#include <sstream>
#include <fstream>
#include <cmath>
#include <chrono>
#include <regex>
Expand Down Expand Up @@ -65,7 +64,6 @@ MainThread& GetMainThread(bool headless)
return EglMainThread::Get();
}
#endif

return GetSdlMainThread();
}

Expand Down Expand Up @@ -116,13 +114,16 @@ GLWindow* InitVisualization(const char name[], int x, int y, int w, int h,
bool headless)
{
#ifdef GLVIS_DEBUG
if (!headless)
{
cout << "OpenGL Visualization" << endl;
}
if (!headless) { cout << "OpenGL Visualization" << endl; }
else
{
#if defined(GLVIS_USE_EGL)
cout << "OpenGL+EGL Visualization" << endl;
#elif defined(GLVIS_USE_CGL)
cout << "OpenGL+CGL Visualization" << endl;
#else
cout << "Headless rendering requires EGL or CGL!" << endl;
#endif
}
#endif

Expand Down Expand Up @@ -161,10 +162,10 @@ GLWindow* InitVisualization(const char name[], int x, int y, int w, int h,
{
wnd->clearEvents();
}
#else //GLVIS_USE_EGL || GLVIS_USE_CGL
#else // GLVIS_USE_EGL || GLVIS_USE_CGL
cerr << "EGL or CGL are required for headless rendering!" << endl;
return NULL;
#endif //GLVIS_USE_EGL || GLVIS_USE_CGL
#endif // GLVIS_USE_EGL || GLVIS_USE_CGL
}

#ifdef GLVIS_DEBUG
Expand Down Expand Up @@ -1029,7 +1030,7 @@ int SaveAsPNG(const char *fname, int w, int h, bool is_hidpi, bool with_alpha,
}

png_uint_32 ppi = is_hidpi ? 144 : 72; // pixels/inch
png_uint_32 ppm = ppi/0.0254 + 0.5; // pixels/meter
auto ppm = static_cast<png_uint_32>(ppi/0.0254 + 0.5); // pixels/meter
png_set_pHYs(png_ptr, info_ptr, ppm, ppm, PNG_RESOLUTION_METER);

png_init_io(png_ptr, fp);
Expand Down
34 changes: 22 additions & 12 deletions lib/egl/egl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.

#if defined(GLVIS_USE_EGL) or defined(GLVIS_USE_CGL)
#if defined(GLVIS_USE_EGL) || defined(GLVIS_USE_CGL)

#include <cassert>
#include <iostream>
#include <vector>

#include "egl.hpp"
#include "egl_main.hpp"
#include "../aux_vis.hpp"
#include <iostream>
#include <vector>
#include <future>

#ifdef GLVIS_DEBUG
#define PRINT_DEBUG(s) std::cerr << s
Expand All @@ -25,10 +27,6 @@

using namespace std;

EglWindow::EglWindow()
{
}

EglWindow::~EglWindow()
{
EglMainThread::Get().DeleteWindow(this, handle);
Expand All @@ -43,6 +41,10 @@ bool EglWindow::createWindow(const char *, int, int, int w, int h,
return false;
}

#ifdef GLVIS_USE_CGL
return true; // CGL already called initGLEW() during CreateWindow()
#endif

#ifndef __EMSCRIPTEN__
glEnable(GL_DEBUG_OUTPUT);
#endif
Expand Down Expand Up @@ -167,8 +169,16 @@ void EglWindow::getGLDrawSize(int& w, int& h) const
#endif
#ifdef GLVIS_USE_CGL
GLint cgl_w, cgl_h;
glGetRenderbufferParameteriv(handle.buf_color, GL_RENDERBUFFER_WIDTH, &cgl_w);
glGetRenderbufferParameteriv(handle.buf_color, GL_RENDERBUFFER_HEIGHT, &cgl_h);
// Bind the color renderbuffer
glBindRenderbuffer(GL_RENDERBUFFER, handle.buf_color);
assert(glGetError() == GL_NO_ERROR);
// Query width and height
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &cgl_w);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &cgl_h);
assert(glGetError() == GL_NO_ERROR);
// Unbind to restore state
glBindRenderbuffer(GL_RENDERBUFFER, 0);
assert(glGetError() == GL_NO_ERROR);
w = cgl_w;
h = cgl_h;
#endif
Expand All @@ -193,7 +203,7 @@ void EglWindow::signalKeyDown(SDL_Keycode k, SDL_Keymod m)

void EglWindow::signalQuit()
{
queueEvents({{EventType::Quit}});
queueEvents({{EventType::Quit, {}}});
}

void EglWindow::screenshot(string filename, bool convert)
Expand All @@ -206,4 +216,4 @@ void EglWindow::screenshot(string filename, bool convert)
signalExpose();
}

#endif //GLVIS_USE_EGL || GLVIS_USE_CGL
#endif // GLVIS_USE_EGL || GLVIS_USE_CGL
29 changes: 15 additions & 14 deletions lib/egl/egl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

#ifndef GLVIS_EGL_HPP
#define GLVIS_EGL_HPP
#if defined(GLVIS_USE_EGL) or defined(GLVIS_USE_CGL)

#if defined(GLVIS_USE_EGL) || defined(GLVIS_USE_CGL)

#include "../glwindow.hpp"

Expand All @@ -21,15 +22,13 @@

#ifdef GLVIS_USE_CGL
#define GL_SILENCE_DEPRECATION // CGL has been deprecated since MacOS 10.14
#include <OpenGL/CGLTypes.h>
#include <OpenGL/CGLCurrent.h>
#include <OpenGL/OpenGL.h>
#endif

#include <condition_variable>
#include <memory>
#include <mutex>
#include <deque>
#include <list>

class EglWindow : public GLWindow
{
Expand All @@ -43,8 +42,11 @@ class EglWindow : public GLWindow
#endif
#ifdef GLVIS_USE_CGL
GLuint buf_frame {}, buf_color {}, buf_depth {};
CGLPixelFormatObj pixFmt {};
CGLContextObj ctx{};
CGLPixelFormatObj pix {};

using CGLContextDeleter = void(*)(CGLContextObj);
std::unique_ptr<_CGLContextObject, CGLContextDeleter> ctx
{nullptr, [](CGLContextObj ctx) { CGLDestroyContext(ctx); }};
#endif

bool isInitialized()
Expand All @@ -61,17 +63,17 @@ class EglWindow : public GLWindow
private:
Handle handle;

bool running{false};

bool is_multithreaded{true};
bool call_idle_func{false};
bool running {false};
bool is_multithreaded {true};
bool call_idle_func {false};

enum class EventType
{
Keydown,
Screenshot,
Quit,
};

struct Event
{
EventType type;
Expand All @@ -93,15 +95,14 @@ class EglWindow : public GLWindow
};

std::string screenshot_filename;

std::condition_variable events_available;
std::mutex event_mutex;
std::deque<Event> waiting_events;

void queueEvents(std::vector<Event> events);

public:
EglWindow();
EglWindow() = default;
~EglWindow();

/** @brief Creates a new OpenGL window. Returns false if EGL or OpenGL
Expand Down Expand Up @@ -144,5 +145,5 @@ class EglWindow : public GLWindow
void screenshot(std::string filename, bool convert = false) override;
};

#endif //GLVIS_USE_EGL || GLVIS_USE_CGL
#endif //GLVIS_EGL_HPP
#endif // GLVIS_USE_EGL || GLVIS_USE_CGL
#endif // GLVIS_EGL_HPP
Loading
Loading