|
5 | 5 | #include <unistd.h>
|
6 | 6 | #include <string>
|
7 | 7 | #include <map>
|
| 8 | +#ifdef EGL |
| 9 | +#include <sstream> |
| 10 | +#include <stdexcept> |
| 11 | +#include <EGL/egl.h> |
| 12 | +#include <GL/gl.h> |
| 13 | +#endif |
8 | 14 | #include <TCFoundation/TCUserDefaults.h>
|
9 | 15 | #include <TCFoundation/mystring.h>
|
10 | 16 | #include <LDLib/LDSnapshotTaker.h>
|
@@ -250,27 +256,137 @@ bool fileCaseCallback(char *filename)
|
250 | 256 | return false;
|
251 | 257 | }
|
252 | 258 |
|
| 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 | + |
253 | 339 | int main(int argc, char *argv[])
|
254 | 340 | {
|
255 |
| - void *buffer; |
| 341 | + void *buffer = NULL; |
256 | 342 | OSMesaContext ctx;
|
257 | 343 | int stringTableSize = sizeof(LDViewMessages_bytes);
|
258 | 344 | 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 |
260 | 351 | memcpy(stringTable, LDViewMessages_bytes, stringTableSize);
|
261 | 352 | stringTable[stringTableSize] = 0;
|
262 | 353 | TCLocalStrings::setStringTable(stringTable);
|
263 | 354 | 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) |
265 | 370 | {
|
266 | 371 | //ProgressHandler *progressHandler = new ProgressHandler;
|
267 | 372 |
|
268 | 373 | TREMainModel::setStudTextureData(StudLogo_bytes,
|
269 | 374 | sizeof(StudLogo_bytes));
|
270 | 375 | LDLModel::setFileCaseCallback(fileCaseCallback);
|
271 | 376 | 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 | + } |
274 | 390 | //TCObject::release(progressHandler);
|
275 | 391 | }
|
276 | 392 | TCAutoreleasePool::processReleases();
|
|
0 commit comments