File size: 9,904 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | /*
* Copyright 2017-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 "FramePresenterGLX.h"
#include <iostream>
#include <cstring>
#include <thread>
#include "../Utils/NvCodecUtils.h"
/**
* @brief Constructor to initialize GLX, OpenGL and CUDA resources. Also launches rendering thread.
* @param None
* @return None
*/
FramePresenterGLX::FramePresenterGLX(int w, int h) {
frameFeeder.setSize(BUFFER_COUNT);
currentFrame = 0;
endOfDecoding = false;
endOfRendering = false;
cuResource[0] = 0;
cuResource[1] = 0;
// Initialize the width and height to be used in texture updates
setDimensions(w, h);
// Initliaze GLX window, context
initWindowSystem();
// Initialize OpenGL resources
initOpenGLResources();
for (int i = 0; i < BUFFER_COUNT; i++) {
// Attach pbo to each of the cuda graphics resource
ck(cuGraphicsGLRegisterBuffer(&cuResource[i], pbo[i], CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD));
}
// Create a thread handle using Render function of the class for execution
renderingThread = NvThread(std::thread(&FramePresenterGLX::Render, this));
}
/**
* @brief Check if valid NVIDIA libraries are installed.
* @param None
* @return None
*/
bool FramePresenterGLX::isVendorNvidia() {
char * vendor = (char*) glGetString(GL_VENDOR);
if (!strcmp(vendor, "NVIDIA Corporation")) {
return true;
} else {
return false;
}
}
/**
* @brief Create PBO and CUResource binding.
* @param None
* @return None
*/
bool FramePresenterGLX::GetDeviceFrameBuffer(CUdeviceptr *dpFrame, int *pitch) {
mapBufferObject(dpFrame);
*pitch = getWidth()*4;
return true;
}
/**
* @brief Release the mapping so that, Rendering thread can consume the updated PBO.
* @param None
* @return None
*/
void FramePresenterGLX::ReleaseDeviceFrameBuffer() {
unmapBufferObject();
}
/**
* @brief Binds CUResource to PBO and returns the CUdeviceptr that caller can work with.
* @param None
* @return None
*/
void FramePresenterGLX::mapBufferObject(CUdeviceptr* dpBuffer) {
ck(cuGraphicsMapResources(1, &cuResource[currentFrame], 0));
size_t nSize = 0;
ck(cuGraphicsResourceGetMappedPointer(dpBuffer, &nSize, cuResource[currentFrame]));
}
/**
* @brief Unmap the binding between CUResource and PBO.
* @param None
* @return None
*/
void FramePresenterGLX::unmapBufferObject() {
ck(cuGraphicsUnmapResources(1, &cuResource[currentFrame], 0));
frameFeeder.push_back(currentFrame);
// CurrentFrame goes from between 0 and (BUFFER_COUNT - 1)
currentFrame = (currentFrame + 1) % BUFFER_COUNT;
}
/**
* @brief Function to render decoded frame using OpenGL calls.
* The PBO used here is populated by Decode function.
* Rendering is done using shared GLX context.
* @param None
* @return None
*/
void FramePresenterGLX::Render() {
int w = getWidth();
int h = getHeight();
glXMakeCurrent(display, win, shared_ctx);
int currentRender = 0;
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glDisable(GL_DEPTH_TEST);
while (!endOfDecoding) {
currentRender = frameFeeder.front();
// Bind OpenGL buffer object and upload the data
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo[currentRender]);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, tex[currentRender]);
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, (GLfloat)h);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f((GLfloat)w, (GLfloat)h);
glVertex2f(1.0f, -1.0f);
glTexCoord2f((GLfloat)w, 0);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex2f(-1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glXSwapBuffers(display, win);
frameFeeder.pop_front();
}
glDisable(GL_FRAGMENT_PROGRAM_ARB);
glXMakeCurrent(display, 0, 0);
endOfRendering = true;
}
/**
* @brief Function to release GLX resources like display, window, context and colormap.
* @param None
* @return None
*/
void FramePresenterGLX::releaseWindowSystem() {
glXMakeCurrent(display, 0, 0);
glXDestroyContext(display, ctx);
glXDestroyContext(display, shared_ctx);
XDestroyWindow(display, win);
XFreeColormap(display, cmap);
XCloseDisplay(display);
}
/**
* @brief Function to create GLX resources like display, window, context and colormap.
* Exits the app in case any GLX API fails.
* @param None
* @return None
*/
void FramePresenterGLX::initWindowSystem() {
XInitThreads();
XVisualInfo *visinfo;
GLXFBConfig config;
display = XOpenDisplay(NULL);
if (display == NULL) {
std::cout << "\nDisplay not found ! Make sure X server is running and DISPLAY environment variable set appropriately !\n";
exit(1);
}
int configAttr[] = {
GLX_CONFIG_CAVEAT ,GLX_NONE,
GLX_RENDER_TYPE ,GLX_RGBA_BIT,
GLX_RED_SIZE , 8,
GLX_GREEN_SIZE , 8,
GLX_BLUE_SIZE , 8,
GLX_ALPHA_SIZE , 8,
GLX_DEPTH_SIZE , 24,
GLX_STENCIL_SIZE , 8,
GLX_DOUBLEBUFFER , True,
None,
};
GLXFBConfig *configs = NULL;
int numConfigs = 0;
int screen = DefaultScreen(display);
configs = glXChooseFBConfig(display, screen, configAttr, &numConfigs);
if (numConfigs <= 0 || configs == NULL) {
std::cout << "\nFailed to find a suitable GLXFBConfig!\n";
exit(1);
}
config = configs[0];
XFree(configs);
visinfo = glXGetVisualFromFBConfig(display, config);
if (!visinfo) {
std::cout << "\nFailed to find a suitable visual!\n";
exit(1);
}
Window root;
XSetWindowAttributes wattr;
int wattr_mask;
root = RootWindow(display, screen);
cmap = XCreateColormap(display, root, visinfo->visual, AllocNone);
if (!cmap) {
std::cout << "\nFailed to create colormap!\n";
exit(1);
}
wattr_mask = CWBackPixmap | CWBorderPixel | CWColormap;
wattr.background_pixmap = None;
wattr.border_pixel = 0;
wattr.bit_gravity = StaticGravity;
wattr.colormap = cmap;
win = XCreateWindow(display, root, 0, 0, 640, 480, 0,
visinfo->depth, InputOutput,
visinfo->visual, wattr_mask, &wattr);
if (!win) {
std::cout << "\nFailed to create window!\n";
exit(1);
}
XMapWindow(display, win);
ctx = glXCreateNewContext(display, config, GLX_RGBA_TYPE, 0, True);
if (!ctx) {
std::cout << "\nFailed to create GLX context !\n";
exit(1);
}
shared_ctx = glXCreateNewContext(display, config, GLX_RGBA_TYPE, ctx, True);
if (!shared_ctx) {
std::cout << "\nFailed to create shared GLX context !\n";
exit(1);
}
glXMakeCurrent(display, win, ctx);
}
/**
* @brief Function to create OpenGL resources viz. pixel buffer objects, textures, program objects.
* @param None
* @return None
*/
void FramePresenterGLX::initOpenGLResources() {
glewInit();
GLuint &shader = getProgramObject();
glGenTextures(BUFFER_COUNT, tex);
glGenBuffers(BUFFER_COUNT, pbo);
for (int i=0; i<BUFFER_COUNT; i++) {
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo[i]);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 4, NULL, GL_DYNAMIC_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, tex[i]);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
}
// Create and initialize fragment program
static const char *code =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord, texture[0], RECT; \n"
"END";
glGenProgramsARB(1, &shader);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader);
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(code), (GLubyte *)code);
}
/**
* @brief Function to release OpenGL resources viz. pixel buffer objects, textures, program objects.
* @param None
* @return None
*/
FramePresenterGLX::~FramePresenterGLX() {
endOfDecoding = true;
// Don't release resources till rendering is finished.
while (!endOfRendering) {
}
renderingThread.join();
for (int i = 0; i < BUFFER_COUNT; i++) {
ck(cuGraphicsUnregisterResource(cuResource[i]));
}
GLuint &shader = getProgramObject();
// Release OpenGL resources
glDeleteBuffersARB(BUFFER_COUNT, pbo);
glDeleteTextures(BUFFER_COUNT, tex);
glDeleteProgramsARB(1, &shader);
// Release GLX resources
releaseWindowSystem();
}
|