File size: 6,567 Bytes
ea55f45 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | ////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020-2024 NVIDIA Corporation. All rights reserved.
//
// Please refer to the NVIDIA end user license agreement (EULA) associated
// with this source code for terms and conditions that govern your use of
// this software. Any use, reproduction, disclosure, or distribution of
// this software and related documentation outside the terms of the EULA
// is strictly prohibited.
//
////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string.h>
#include <X11/Xlib.h>
#include <EGL/egl.h>
#include "../Utils/Logger.h"
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/freeglut_ext.h>
void showErrorAndExit (const char* message)
{
bool bThrowError = false;
std::ostringstream oss;
if (message)
{
bThrowError = true;
oss << message << std::endl;
}
if (bThrowError)
{
throw std::invalid_argument(oss.str());
}
else
{
std::cout << oss.str();
exit(0);
}
}
// egl resources
static Display *display;
static int screen;
static Window win = 0;
static EGLDisplay eglDisplay = EGL_NO_DISPLAY;
static EGLSurface eglSurface = EGL_NO_SURFACE;
static EGLContext eglContext = EGL_NO_CONTEXT;
// glut window handle
static int window;
// Initialization function to create a simple X window and associate EGLContext and EGLSurface with it
bool SetupEGLResources(int xpos, int ypos, int width, int height, const char *windowname)
{
bool status = true;
EGLint configAttrs[] =
{
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_DEPTH_SIZE, 16,
EGL_SAMPLE_BUFFERS, 0,
EGL_SAMPLES, 0,
EGL_CONFORMANT, EGL_OPENGL_BIT,
EGL_NONE
};
EGLint contextAttrs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
EGLint windowAttrs[] = {EGL_NONE};
EGLConfig* configList = NULL;
EGLint configCount;
display = XOpenDisplay(NULL);
if (!display)
{
std::cout << "\nError opening X display\n";
return false;
}
screen = DefaultScreen(display);
eglDisplay = eglGetDisplay(display);
if (eglDisplay == EGL_NO_DISPLAY)
{
std::cout << "\nEGL : failed to obtain display\n";
return false;
}
if (!eglInitialize(eglDisplay, 0, 0))
{
std::cout << "\nEGL : failed to initialize\n";
return false;
}
if (!eglChooseConfig(eglDisplay, configAttrs, NULL, 0, &configCount) || !configCount)
{
std::cout << "\nEGL : failed to return any matching configurations\n";
return false;
}
configList = (EGLConfig*)malloc(configCount * sizeof(EGLConfig));
if (!eglChooseConfig(eglDisplay, configAttrs, configList, configCount, &configCount) || !configCount)
{
std::cout << "\nEGL : failed to populate configuration list\n";
status = false;
goto end;
}
win = XCreateSimpleWindow(display, RootWindow(display, screen),
xpos, ypos, width, height, 0,
BlackPixel(display, screen),
WhitePixel(display, screen));
eglSurface = eglCreateWindowSurface(eglDisplay, configList[0],
(EGLNativeWindowType) win, windowAttrs);
if (!eglSurface)
{
std::cout << "\nEGL : couldn't create window surface\n";
status = false;
goto end;
}
eglBindAPI(EGL_OPENGL_API);
eglContext = eglCreateContext(eglDisplay, configList[0], NULL, contextAttrs);
if (!eglContext)
{
std::cout << "\nEGL : couldn't create context\n";
status = false;
goto end;
}
if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext))
{
std::cout << "\nEGL : couldn't make context/surface current\n";
status = false;
goto end;
}
end:
free(configList);
return status;
}
// Cleanup function to destroy the window and context
void DestroyEGLResources()
{
if (eglDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (eglContext != EGL_NO_CONTEXT)
{
eglDestroyContext(eglDisplay, eglContext);
}
if (eglSurface != EGL_NO_SURFACE)
{
eglDestroySurface(eglDisplay, eglSurface);
}
eglTerminate(eglDisplay);
}
if (win)
{
XDestroyWindow(display, win);
}
XCloseDisplay(display);
}
bool SetupGLXResources()
{
int argc = 1;
char *argv[1] = {(char*)"dummy"};
// Use glx context/surface
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(16, 16);
window = glutCreateWindow("AppEncGL");
if (!window)
{
std::cout << "\nUnable to create GLUT window.\n" << std::endl;
return false;
}
glutHideWindow();
return true;
}
// Cleanup function to destroy glut resources
void DestroyGLXResources()
{
glutDestroyWindow(window);
}
void GraphicsCloseWindow(const char* contextType)
{
if (!strcmp(contextType, "egl"))
{
DestroyEGLResources();
}
else if (!strcmp(contextType, "glx"))
{
DestroyGLXResources();
}
else
{
std::cout << "\nInvalid context type specified.\n";
}
}
void GraphicsSetupWindow(const char* contextType)
{
if (!strcmp(contextType, "egl"))
{
// Use egl context/surface
if (!SetupEGLResources(0, 0, 16, 16, "AppEncGL"))
{
showErrorAndExit("\nFailed to setup window.\n");
}
}
else if (!strcmp(contextType, "glx"))
{
// Use glx context/surface
if (!SetupGLXResources())
{
showErrorAndExit("\nFailed to setup window.\n");
}
}
else
{
showErrorAndExit("\nInvalid context type specified.\n");
}
char * vendor = (char*) glGetString(GL_VENDOR);
if (strcmp(vendor, "NVIDIA Corporation"))
{
GraphicsCloseWindow(contextType);
showErrorAndExit("\nFailed to find NVIDIA libraries\n");
}
}
|