Skip to content

Commit 483dddd

Browse files
committed
Test code for using EGL instead of OSMesa if possible. Authored by Travis Cobbs. Code deactivated by default
1 parent 204afd8 commit 483dddd

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

OSMesa/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ MAKEMODE = POSTFIX=-osmesa USE_BOOST=NO TESTING="$(TESTING)"
3838

3939
CFLAGS = -o $(OBJDIR)/$*.o $(TESTING) -Wall -D_GNU_SOURCE -O3 $(EXPORT_3DS) -DHAVE_MINIZIP
4040

41+
# Uncomment the following two lines to enable EGL support
42+
#CFLAGS += -DEGL
43+
#LIBS += -lEGL
44+
4145
ifeq ("$(USE_CPP11)","YES")
4246

4347
CFLAGS += -DUSE_CPP11 -std=c++11

OSMesa/ldview.cpp

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
#include <unistd.h>
66
#include <string>
77
#include <map>
8+
#ifdef EGL
9+
#include <sstream>
10+
#include <stdexcept>
11+
#include <EGL/egl.h>
12+
#include <GL/gl.h>
13+
#endif
814
#include <TCFoundation/TCUserDefaults.h>
915
#include <TCFoundation/mystring.h>
1016
#include <LDLib/LDSnapshotTaker.h>
@@ -250,27 +256,137 @@ bool fileCaseCallback(char *filename)
250256
return false;
251257
}
252258

259+
#ifdef EGL
260+
void assertEGLError(const std::string& msg) {
261+
EGLint error = eglGetError();
262+
263+
if (error != EGL_SUCCESS) {
264+
std::stringstream s;
265+
s << "EGL error 0x" << std::hex << error << " at " << msg;
266+
throw std::runtime_error(s.str());
267+
}
268+
}
269+
270+
void assertOpenGLError(const std::string& msg) {
271+
GLenum error = glGetError();
272+
273+
if (error != GL_NO_ERROR) {
274+
std::stringstream s;
275+
s << "OpenGL error 0x" << std::hex << error << " at " << msg;
276+
throw std::runtime_error(s.str());
277+
}
278+
}
279+
280+
void setupEGL(EGLDisplay& display, EGLContext& context, EGLSurface& surface)
281+
{
282+
EGLConfig config;
283+
EGLint configCount;
284+
EGLint attribs[] = {
285+
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
286+
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
287+
EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,
288+
EGL_RED_SIZE, 8,
289+
EGL_GREEN_SIZE, 8,
290+
EGL_BLUE_SIZE, 8,
291+
EGL_ALPHA_SIZE, 8,
292+
EGL_DEPTH_SIZE, 24,
293+
EGL_STENCIL_SIZE, 8,
294+
EGL_BUFFER_SIZE, 24,
295+
EGL_NONE,
296+
};
297+
EGLint pbufferAttribs[] = {
298+
EGL_WIDTH, 1024,
299+
EGL_HEIGHT, 1024,
300+
EGL_NONE,
301+
};
302+
303+
eglBindAPI(EGL_OPENGL_API);
304+
assertEGLError("eglBindAPI");
305+
306+
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
307+
assertEGLError("eglGetDisplay");
308+
309+
eglInitialize(display, NULL, NULL);
310+
assertEGLError("eglInitialize");
311+
312+
eglChooseConfig(display, attribs, &config, 1, &configCount);
313+
assertEGLError("eglChooseConfig");
314+
315+
printf("EGL configCount: %d\n", configCount);
316+
317+
surface = eglCreatePbufferSurface(display, config, pbufferAttribs);
318+
assertEGLError("eglCreatePbufferSurface");
319+
320+
context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
321+
assertEGLError("eglCreateContext");
322+
323+
if (!eglMakeCurrent(display, surface, surface, context))
324+
{
325+
throw std::runtime_error("Error making current.");
326+
}
327+
glViewport(0, 0, 1024, 1024);
328+
assertOpenGLError("glViewport");
329+
GLint viewport[4] = {0};
330+
glGetIntegerv(GL_VIEWPORT, viewport);
331+
assertOpenGLError("glGetIntegerv");
332+
if (viewport[2] == 0 || viewport[3] == 0 || glGetString(GL_VENDOR) == NULL)
333+
{
334+
throw std::runtime_error("EGL initialization failed.");
335+
}
336+
}
337+
#endif
338+
253339
int main(int argc, char *argv[])
254340
{
255-
void *buffer;
341+
void *buffer = NULL;
256342
OSMesaContext ctx;
257343
int stringTableSize = sizeof(LDViewMessages_bytes);
258344
char *stringTable = new char[sizeof(LDViewMessages_bytes) + 1];
259-
345+
bool useEGL = false;
346+
#ifdef EGL
347+
EGLDisplay display = NULL;
348+
EGLContext context = NULL;
349+
EGLSurface surface = NULL;
350+
#endif
260351
memcpy(stringTable, LDViewMessages_bytes, stringTableSize);
261352
stringTable[stringTableSize] = 0;
262353
TCLocalStrings::setStringTable(stringTable);
263354
setupDefaults(argv);
264-
if ((buffer = setupContext(ctx)) != NULL)
355+
#ifdef EGL
356+
try
357+
{
358+
setupEGL(display, context, surface);
359+
useEGL = true;
360+
}
361+
catch (std::runtime_error e)
362+
{
363+
// Do nothing.
364+
}
365+
catch (...)
366+
{
367+
}
368+
#endif
369+
if (useEGL || (buffer = setupContext(ctx)) != NULL)
265370
{
266371
//ProgressHandler *progressHandler = new ProgressHandler;
267372

268373
TREMainModel::setStudTextureData(StudLogo_bytes,
269374
sizeof(StudLogo_bytes));
270375
LDLModel::setFileCaseCallback(fileCaseCallback);
271376
LDSnapshotTaker::doCommandLine();
272-
OSMesaDestroyContext(ctx);
273-
free(buffer);
377+
#ifdef EGL
378+
if (display != NULL)
379+
{
380+
eglDestroyContext(display, context);
381+
eglDestroySurface(display, surface);
382+
eglTerminate(display);
383+
}
384+
#endif
385+
if (!useEGL)
386+
{
387+
OSMesaDestroyContext(ctx);
388+
free(buffer);
389+
}
274390
//TCObject::release(progressHandler);
275391
}
276392
TCAutoreleasePool::processReleases();

0 commit comments

Comments
 (0)