NvEglRenderer.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @file
  30. * <b>NVIDIA Multimedia API: EGL Renderer API</b>
  31. *
  32. * @b Description: This file declares the NvEgl Renderer API.
  33. */
  34. #ifndef __NV_EGL_RENDERER_H__
  35. #define __NV_EGL_RENDERER_H__
  36. #include "NvElement.h"
  37. #include <EGL/egl.h>
  38. #include <EGL/eglext.h>
  39. #include <GLES2/gl2.h>
  40. #include <GLES2/gl2ext.h>
  41. #include <X11/Xlib.h>
  42. /**
  43. * @defgroup l4t_mm_nveglrenderer_group Rendering API
  44. *
  45. * The \c %NvEglRenderer API provides EGL and Open GL ES 2.0 rendering
  46. * functionality.
  47. *
  48. * @ingroup aa_framework_api_group
  49. * @{
  50. */
  51. /**
  52. *
  53. * @c %NvEglRenderer is a helper class for rendering using EGL and OpenGL
  54. * ES 2.0. The renderer requires the file descriptor (FD) of a buffer
  55. * as an input. The rendering rate, in frames per second (fps), is
  56. * configurable.
  57. *
  58. * The renderer creates an X Window of its own. The width, height,
  59. * horizontal offset, and vertical offset of the window are
  60. * configurable.
  61. *
  62. * All EGL calls must be made through one thread only. This class
  63. * internally creates a thread which performs all EGL/GL
  64. * initializations, gets @c EGLImage objects from FD, renders the @c
  65. * EGLImage objects, and then deinitializes all the EGL/GL structures.
  66. *
  67. */
  68. class NvEglRenderer:public NvElement
  69. {
  70. public:
  71. /**
  72. * Creates a new EGL-based renderer named @a name.
  73. *
  74. * This method creates a new X window for rendering, of size @a
  75. * width and @a height, that is offset by @a x_offset and @a
  76. * y_offset. If @a width or @a height is zero, a full screen
  77. * window is created with @a x_offset and @a y_offset set to zero.
  78. *
  79. * It internally initializes EGL, creates an @c eglContext, an
  80. * @c eglSurface, a GL texture, and shaders for rendering.
  81. *
  82. *
  83. * @param[in] name Specifies a pointer to a unique name to identity the
  84. * element instance.
  85. * @param[in] width Specifies the width of the window in pixels.
  86. * @param[in] height Specifies the height of the window in pixels.
  87. * @param[in] x_offset Specifies the horizontal offset of the window in pixels.
  88. * @param[in] y_offset Specifies the vertical offset of the window in pixels.
  89. * @return A reference to the newly created renderer object, otherwise @c NULL in
  90. * case of failure during initialization.
  91. */
  92. static NvEglRenderer *createEglRenderer(const char *name, uint32_t width,
  93. uint32_t height, uint32_t x_offset,
  94. uint32_t y_offset);
  95. ~NvEglRenderer();
  96. /**
  97. * Renders a buffer.
  98. *
  99. * This method waits until the rendering time of the next buffer,
  100. * caluclated from the rendering time of the last buffer and the
  101. * render rate in frames per second (fps). This is a blocking
  102. * call.
  103. *
  104. * @param[in] fd Specifies the file descriptor (FD) of the exported buffer
  105. * to render.
  106. * @return 0 for success, -1 otherwise.
  107. */
  108. int render(int fd);
  109. /**
  110. * Sets the rendering rate in frames per second (fps).
  111. *
  112. * @warning An @a fps of zero is not allowed.
  113. *
  114. * @param[in] fps Specifies the render rate in fps.
  115. * @return 0 for success, -1 otherwise.
  116. */
  117. int setFPS(float fps);
  118. /**
  119. * Gets underlying EGLDisplay.
  120. *
  121. * @return EGLDisplay handle
  122. */
  123. EGLDisplay getEGLDisplay() { return egl_display; }
  124. /**
  125. * Gets the display resolution.
  126. *
  127. *
  128. * @param[out] width A pointer to the full screen width, in pixels.
  129. * @param[out] height A pointer to the full screen height, in pixels.
  130. * @return 0 for success, -1 otherwise.
  131. */
  132. static int getDisplayResolution(uint32_t &width, uint32_t &height);
  133. /**
  134. * Sets the overlay string.
  135. *
  136. *
  137. * @param[in] str A pointer to the overlay text.
  138. * @param[in] x Horizontal offset, in pixels.
  139. * @param[in] y Vertical offset, in pixels.
  140. * @return 0 for success, -1 otherwise.
  141. */
  142. int setOverlayText(char *str, uint32_t x, uint32_t y);
  143. private:
  144. Display * x_display; /**< Connection to the X server created using
  145. XOpenDisplay(). */
  146. Window x_window; /**< Holds the window to be used for rendering created using
  147. XCreateWindow(). */
  148. EGLDisplay egl_display; /**< Holds the EGL Display connection. */
  149. EGLContext egl_context; /**< Holds the EGL rendering context. */
  150. EGLSurface egl_surface; /**< Holds the EGL Window render surface. */
  151. EGLConfig egl_config; /**< Holds the EGL frame buffer configuration to be used
  152. for rendering. */
  153. uint32_t texture_id; /**< Holds the GL Texture ID used for rendering. */
  154. GC gc; /**< Graphic Context */
  155. XFontStruct *fontinfo; /**< Brush's font info */
  156. char overlay_str[512]; /**< Overlay's text */
  157. /**
  158. * Creates a GL texture used for rendering.
  159. *
  160. * @return 0 for success, -1 otherwise.
  161. */
  162. int create_texture();
  163. /**
  164. * Initializes shaders with shader programs required for drawing a
  165. * buffer.
  166. *
  167. * @return 0 for success, -1 otherwise.
  168. */
  169. int InitializeShaders();
  170. /**
  171. * Creates, compiles and attaches a shader to the @a program.
  172. *
  173. * @param[in] program Specifies the GL Program ID.
  174. * @param[in] type Specifies the type of the vertex shader. Must be either
  175. @c GL_VERTEX_SHADER or @c GL_FRAGMENT_SHADER.
  176. * @param[in] source Specifies the source code of the shader in form of a string.
  177. * @param[in] size Specifies the character length of @a source.
  178. */
  179. void CreateShader(GLuint program, GLenum type, const char *source,
  180. int size);
  181. struct timespec last_render_time; /**< Rendering time for the last buffer. */
  182. int render_fd; /**< File descriptor (FD) of the next buffer to
  183. render. */
  184. bool stop_thread; /**< Boolean variable used to signal rendering thread
  185. to stop. */
  186. pthread_t render_thread; /**< The pthread id of the rendering thread. */
  187. pthread_mutex_t render_lock; /**< Used for synchronization. */
  188. pthread_cond_t render_cond; /**< Used for synchronization. */
  189. uint32_t overlay_str_x_offset; /**< Overlay text's position in horizontal direction. */
  190. uint32_t overlay_str_y_offset; /**< Overlay text's position in vertical direction. */
  191. float fps; /**< The render rate in frames per second. */
  192. uint64_t render_time_sec; /**< Seconds component of the time for which a
  193. frame should be displayed. */
  194. uint64_t render_time_nsec; /**< Nanoseconds component of the time for which
  195. a frame should be displayed. */
  196. /**
  197. * Constructor called by the wrapper createEglRenderer.
  198. */
  199. NvEglRenderer(const char *name, uint32_t width, uint32_t height,
  200. uint32_t x_offset, uint32_t y_offset);
  201. /**
  202. * Gets the pointers to the required EGL methods.
  203. */
  204. static int initEgl();
  205. /**
  206. * Method executed by the renderThread.
  207. *
  208. * This method continues to execute infinitely until signalled to
  209. * stop with the @c stop_thread variable. This method contains a
  210. * while loop that calls NvEglRenderer::renderInternal().
  211. */
  212. static void * renderThread(void *arg);
  213. /**
  214. * This method contains the actual logic of rendering a buffer
  215. * and waiting until the buffer render time.
  216. */
  217. int renderInternal();
  218. /**
  219. * These EGL function pointers are required by the renderer.
  220. */
  221. static PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
  222. static PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
  223. static PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR;
  224. static PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR;
  225. static PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR;
  226. static PFNEGLGETSYNCATTRIBKHRPROC eglGetSyncAttribKHR;
  227. static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
  228. static const NvElementProfiler::ProfilerField valid_fields =
  229. NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
  230. NvElementProfiler::PROFILER_FIELD_FPS |
  231. NvElementProfiler::PROFILER_FIELD_LATE_UNITS;
  232. };
  233. /** @} */
  234. #endif