00001 /* 00002 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of NVIDIA CORPORATION nor the names of its 00013 * contributors may be used to endorse or promote products derived 00014 * from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 00017 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00019 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00020 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00021 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00022 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00023 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00024 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 /** 00030 * @file 00031 * <b>NVIDIA Eagle Eye API: EGL Renderer API</b> 00032 * 00033 * @b Description: This file declares the NvEgl Renderer API. 00034 */ 00035 #ifndef __NV_EGL_RENDERER_H__ 00036 #define __NV_EGL_RENDERER_H__ 00037 00038 #include "NvElement.h" 00039 00040 #include <EGL/egl.h> 00041 #include <EGL/eglext.h> 00042 #include <GLES2/gl2.h> 00043 #include <GLES2/gl2ext.h> 00044 00045 #include <X11/Xlib.h> 00046 00047 /** 00048 * @defgroup ee_nveglrenderer_group Rendering API 00049 * 00050 * The @c %NvEglRenderer API provides EGL and Open GL ES 2.0 rendering 00051 * functionality. 00052 * 00053 * @{ 00054 */ 00055 00056 /** 00057 * 00058 * @c %NvEglRenderer is a helper class for rendering using EGL and OpenGL 00059 * ES 2.0. The renderer requires the file descriptor (FD) of a buffer 00060 * as an input. The rendering rate, in frames per second (fps), is 00061 * configurable. 00062 * 00063 * The renderer creates an X Window of its own. The width, height, 00064 * horizontal offset, and vertical offset of the window are 00065 * configurable. 00066 * 00067 * All EGL calls must be made through one thread only. This class 00068 * internally creates a thread which performs all EGL/GL 00069 * initializations, gets @c EGLImage objects from FD, renders the @c 00070 * EGLImage objects, and then deinitializes all the EGL/GL structures. 00071 * 00072 */ 00073 class NvEglRenderer:public NvElement 00074 { 00075 public: 00076 /** 00077 * Creates a new EGL-based renderer named @a name. 00078 * 00079 * This method creates a new X window for rendering, of size @a 00080 * width and @a height, that is offset by @a x_offset and @a 00081 * y_offset. If @a width or @a height is zero, a full screen 00082 * window is created with @a x_offset and @a y_offset set to zero. 00083 * 00084 * It internally initializes EGL, creates an @c eglContext, an 00085 * @c eglSurface, a GL texture, and shaders for rendering. 00086 * 00087 * 00088 * @param[in] name Specifies a pointer to a unique name to identity the 00089 * element instance. 00090 * @param[in] width Specifies the width of the window in pixels. 00091 * @param[in] height Specifies the height of the window in pixels. 00092 * @param[in] x_offset Specifies the horizontal offset of the window in pixels. 00093 * @param[in] y_offset Specifies the vertical offset of the window in pixels. 00094 * @return A reference to the newly created renderer object, otherwise @c NULL in 00095 * case of failure during initialization. 00096 */ 00097 static NvEglRenderer *createEglRenderer(const char *name, uint32_t width, 00098 uint32_t height, uint32_t x_offset, 00099 uint32_t y_offset); 00100 ~NvEglRenderer(); 00101 00102 /** 00103 * Renders a buffer. 00104 * 00105 * This method waits until the rendering time of the next buffer, 00106 * caluclated from the rendering time of the last buffer and the 00107 * render rate in frames per second (fps). This is a blocking 00108 * call. 00109 * 00110 * @param[in] fd Specifies the file descriptor (FD) of the exported buffer 00111 * to render. 00112 * @return 0 for success, -1 otherwise. 00113 */ 00114 int render(int fd); 00115 00116 /** 00117 * Sets the rendering rate in frames per second (fps). 00118 * 00119 * @warning An @a fps of zero is not allowed. 00120 * 00121 * @param[in] fps Specifies the render rate in fps. 00122 * @return 0 for success, -1 otherwise. 00123 */ 00124 int setFPS(float fps); 00125 00126 /** 00127 * Get underlying EGLDisplay 00128 * 00129 * @return EGLDisplay handle 00130 */ 00131 EGLDisplay getEGLDisplay() { return egl_display; } 00132 00133 /** 00134 * Get Display's resolution 00135 * 00136 * 00137 * @param[out] Full screen width, in pixel 00138 * @param[out] Full screen height, in pixel 00139 * @returns 0 for success, -1 for failure 00140 */ 00141 static int getDisplayResolution(uint32_t &width, uint32_t &height); 00142 00143 /** 00144 * Set Overlay string 00145 * 00146 * 00147 * @param[in] str Overlay Text 00148 * @param[in] Horizontal offset, in pixel 00149 * @param[in] Vertical offset, in pixel 00150 * @returns 0 for success, -1 for failure 00151 */ 00152 int setOverlayText(char *str, uint32_t x, uint32_t y); 00153 00154 private: 00155 Display * x_display; /**< Connection to the X server created using 00156 XOpenDisplay(). */ 00157 Window x_window; /**< Holds the window to be used for rendering 00158 created using XCreateWindow(). */ 00159 00160 EGLDisplay egl_display; /**< Holds the EGL Display connection. */ 00161 EGLContext egl_context; /**< Holds the EGL rendering context. */ 00162 EGLSurface egl_surface; /**< Holds the EGL Window render surface. */ 00163 EGLConfig egl_config; /**< Holds the EGL frame buffer configuration 00164 to be usedfor rendering. */ 00165 00166 uint32_t texture_id; /**< Holds the GL Texture ID used for rendering. */ 00167 GC gc; /**< Graphic Context */ 00168 XFontStruct *fontinfo; /**< Brush's font info */ 00169 char overlay_str[512]; /**< Overlay's text */ 00170 00171 /** 00172 * Creates a GL texture used for rendering. 00173 * 00174 * @return 0 for success, -1 otherwise. 00175 */ 00176 int create_texture(); 00177 /** 00178 * Initializes shaders with shader programs required for drawing a 00179 * buffer. 00180 * 00181 * @return 0 for success, -1 otherwise. 00182 */ 00183 int InitializeShaders(); 00184 /** 00185 * Creates, compiles and attaches a shader to the @a program. 00186 * 00187 * @param[in] program Specifies the GL Program ID. 00188 * @param[in] type Specifies the type of the vertex shader. Must be either 00189 @c GL_VERTEX_SHADER or @c GL_FRAGMENT_SHADER. 00190 * @param[in] source Specifies the source code of the shader in form of a string. 00191 * @param[in] size Specifies the character length of @a source. 00192 */ 00193 void CreateShader(GLuint program, GLenum type, const char *source, 00194 int size); 00195 00196 struct timespec last_render_time; /**< Rendering time for the last buffer. */ 00197 00198 int render_fd; /**< File descriptor (FD) of the next buffer to 00199 render. */ 00200 bool stop_thread; /**< Boolean variable used to signal rendering thread 00201 to stop. */ 00202 pthread_t render_thread; /**< The pthread id of the rendering thread. */ 00203 pthread_mutex_t render_lock; /**< Used for synchronization. */ 00204 pthread_cond_t render_cond; /**< Used for synchronization. */ 00205 uint32_t overlay_str_x_offset; /**< overlay text's position in horizontal direction. */ 00206 uint32_t overlay_str_y_offset; /**< overlay text's position in vertical direction. */ 00207 float fps; /**< The render rate in frames per second. */ 00208 uint64_t render_time_sec; /**< Seconds component of the time for which a 00209 frame should be displayed. */ 00210 uint64_t render_time_nsec; /**< Nanoseconds component of the time for which 00211 a frame should be displayed. */ 00212 00213 /** 00214 * Constructor called by the wrapper createEglRenderer. 00215 */ 00216 NvEglRenderer(const char *name, uint32_t width, uint32_t height, 00217 uint32_t x_offset, uint32_t y_offset); 00218 /** 00219 * Gets the pointers to the required EGL methods. 00220 */ 00221 static int initEgl(); 00222 /** 00223 * Method executed by the renderThread. 00224 * 00225 * This method continues to execute infinitely until signalled to 00226 * stop with the @c stop_thread variable. This method contains a 00227 * while loop that calls NvEglRenderer::renderInternal(). 00228 */ 00229 static void * renderThread(void *arg); 00230 /** 00231 * This method contains the actual logic of rendering a buffer 00232 * and waiting until the buffer render time. 00233 */ 00234 int renderInternal(); 00235 00236 /** 00237 * These EGL function pointers are required by the renderer. 00238 */ 00239 static PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR; 00240 static PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR; 00241 static PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR; 00242 static PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR; 00243 static PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR; 00244 static PFNEGLGETSYNCATTRIBKHRPROC eglGetSyncAttribKHR; 00245 static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; 00246 00247 static const NvElementProfiler::ProfilerField valid_fields = 00248 NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS | 00249 NvElementProfiler::PROFILER_FIELD_FPS | 00250 NvElementProfiler::PROFILER_FIELD_LATE_UNITS; 00251 }; 00252 /** @} */ 00253 #endif