Jetson Linux API Reference

39.2 Release
NvDrmRenderer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
38 #ifndef __NV_DRM_RENDERER_H__
39 #define __NV_DRM_RENDERER_H__
40 
41 #include "NvElement.h"
42 #include <stdint.h>
43 #include <pthread.h>
44 #include <queue>
45 #include <unordered_map>
46 
56 typedef struct _NvDrmBO {
57  uint32_t bo_handle;
58  int width;
59  int height;
60  int pitch;
61  uint8_t* data;
62 } NvDrmBO;
63 
65 typedef struct _NvDrmFB {
66  uint32_t fb_id;
67  int width;
68  int height;
69  int format;
72  NvDrmBO bo[4];
75 } NvDrmFB;
76 
77 
94 {
95 public:
112  static NvDrmRenderer *createDrmRenderer(int adrm_fd, const char *name, uint32_t width,
113  uint32_t height, uint32_t w_x, uint32_t w_y,
114  uint32_t connector, uint32_t crtc);
115  ~NvDrmRenderer();
116 
128  int enqueBuffer(int fd);
129 
140  int dequeBuffer();
141 
150  int setFPS(float fps);
151 
159  bool enableUniversalPlanes(int enable);
160 
176  uint32_t createDumbFB(uint32_t width, uint32_t height, uint32_t drm_format, NvDrmFB *fb);
177 
185  int removeFB(uint32_t fb_id);
186 
187 
196  int drmUtilCloseGemBo(int fd, uint32_t bo_handle);
197 
226  int setPlane(uint32_t pl_index,
227  uint32_t fb_id,
228  uint32_t crtc_x,
229  uint32_t crtc_y,
230  uint32_t crtc_w,
231  uint32_t crtc_h,
232  uint32_t src_x,
233  uint32_t src_y,
234  uint32_t src_w,
235  uint32_t src_h);
236 
248  int getPlaneCount();
249 
263  int getPlaneIndex(uint32_t crtc_index,
264  int32_t* plane_index);
265 
271  int getCrtcCount();
272 
278  int getEncoderCount();
279 
280 private:
281 
282  struct timespec last_render_time;
284  int drm_fd;
287  int conn, crtc;
288  uint32_t width, height;
289  uint32_t offset_x, offset_y;
290  uint32_t drm_conn_id;
291  uint32_t drm_enc_id;
292  uint32_t drm_crtc_id;
293  uint32_t last_fb;
294  int activeFd;
295  int flippedFd;
296  bool flipPending;
297  bool renderingStarted;
298  bool is_nvidia_drm;
299  uint32_t plane_idx;
300 
301  uint32_t hdrBlobId;
302  bool hdrBlobCreated;
303 
304  std::queue<int> freeBuffers;
305  std::queue<int> pendingBuffers;
306  std::unordered_map <int, int> map_list;
307 
308  bool stop_thread;
310  pthread_t render_thread;
312  pthread_mutex_t render_lock;
313  pthread_cond_t render_cond;
314  pthread_mutex_t enqueue_lock;
315  pthread_cond_t enqueue_cond;
316  pthread_mutex_t dequeue_lock;
317  pthread_cond_t dequeue_cond;
319  float fps;
320  uint64_t render_time_sec;
322  uint64_t render_time_nsec;
339  NvDrmRenderer(int adrm_fd, const char *name, uint32_t width, uint32_t height,
340  uint32_t w_x, uint32_t w_y, uint32_t connector, uint32_t crtc);
341 
351  static void * renderThread(void *arg);
352  static void * renderThreadOrin(void *arg);
353 
358  static void page_flip_handler(int fd, unsigned int frame,
359  unsigned int sec, unsigned int usec, void *data);
360 
367  int renderInternal(int fd);
368 
369  /*
370  * Returns a DRM buffer_object handle.
371  *
372  * \param[in] w Width of the window in pixels.
373  * \param[in] h Height of the window in pixels.
374  * \param[in] bpp Bits per pixel in the window.
375  * \param[in] bo A pointer to a buffer object handle.
376  * \return An allocated DRM buffer_object handle of size (w,h)
377  */
378  int createDumbBO(int w, int h, int bpp, NvDrmBO *bo);
379 
380  static const NvElementProfiler::ProfilerField valid_fields =
384 };
385 
387 #endif
_NvDrmBO::width
int width
Holds width of the DRM buffer, in pixels.
Definition: NvDrmRenderer.h:58
NvElementProfiler::PROFILER_FIELD_LATE_UNITS
static const ProfilerField PROFILER_FIELD_LATE_UNITS
Definition: NvElementProfiler.h:82
_NvDrmBO::pitch
int pitch
Holds stride/pitch of the DRM buffer.
Definition: NvDrmRenderer.h:60
NvElementProfiler::ProfilerField
int ProfilerField
Definition: NvElementProfiler.h:79
NvDrmRenderer::dequeBuffer
int dequeBuffer()
Dequeues a previously rendered buffer.
_NvDrmFB::num_buffers
int num_buffers
Holds the number of DRM buffers, which depends on the buffer format.
Definition: NvDrmRenderer.h:73
_NvDrmFB::height
int height
Holds height of the frame, in pixels.
Definition: NvDrmRenderer.h:68
_NvDrmBO::data
uint8_t * data
Holds mapped CPU accessible address.
Definition: NvDrmRenderer.h:61
_NvDrmFB::width
int width
Holds width of the frame, in pixels.
Definition: NvDrmRenderer.h:67
NvDrmRenderer::enableUniversalPlanes
bool enableUniversalPlanes(int enable)
Enables/disables DRM universal planes client caps, such as DRM_CLIENT_CAP_UNIVERSAL_PLANES.
_NvDrmBO::height
int height
Holds height of the DRM buffer, in pixels.
Definition: NvDrmRenderer.h:59
NvDrmFB
struct _NvDrmFB NvDrmFB
Holds information about the frame.
_NvDrmFB::bo
NvDrmBO bo[4]
Holds DRM buffer handles.
Definition: NvDrmRenderer.h:72
NvDrmRenderer::setFPS
int setFPS(float fps)
Sets the rendering rate in terms of frames per second.
NvDrmRenderer::createDumbFB
uint32_t createDumbFB(uint32_t width, uint32_t height, uint32_t drm_format, NvDrmFB *fb)
Allocates a framebuffer of size (w, h).
_NvDrmFB::format
int format
Holds frame format, such as DRM_FORMAT_RGB332.
Definition: NvDrmRenderer.h:69
NvDrmRenderer::getPlaneCount
int getPlaneCount()
Gets total number of planes available.
NvDrmRenderer::getEncoderCount
int getEncoderCount()
Gets count of available encoders.
NvDrmRenderer::createDrmRenderer
static NvDrmRenderer * createDrmRenderer(int adrm_fd, const char *name, uint32_t width, uint32_t height, uint32_t w_x, uint32_t w_y, uint32_t connector, uint32_t crtc)
Creates a new DRM based renderer named name.
NvDrmRenderer::drmUtilCloseGemBo
int drmUtilCloseGemBo(int fd, uint32_t bo_handle)
Close GEM (Graphics Execution Manager) handles.
_NvDrmFB
Holds information about the frame.
Definition: NvDrmRenderer.h:65
_NvDrmBO
Holds a buffer object handle.
Definition: NvDrmRenderer.h:56
NvElementProfiler::PROFILER_FIELD_FPS
static const ProfilerField PROFILER_FIELD_FPS
Definition: NvElementProfiler.h:84
NvDrmRenderer::setPlane
int setPlane(uint32_t pl_index, uint32_t fb_id, uint32_t crtc_x, uint32_t crtc_y, uint32_t crtc_w, uint32_t crtc_h, uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h)
Changes a plane's framebuffer and position.
NvElement
Every element has a unique name that can be used for identifying the element in debug logs.
Definition: NvElement.h:63
NvDrmRenderer::~NvDrmRenderer
~NvDrmRenderer()
_NvDrmBO::bo_handle
uint32_t bo_handle
Holds DRM buffer index.
Definition: NvDrmRenderer.h:57
NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS
static const ProfilerField PROFILER_FIELD_TOTAL_UNITS
Definition: NvElementProfiler.h:81
NvDrmBO
struct _NvDrmBO NvDrmBO
Holds a buffer object handle.
NvDrmRenderer::removeFB
int removeFB(uint32_t fb_id)
Destroys (frees) a framebuffer previously allocated by createDumbFB().
NvElement.h
NvDrmRenderer::getPlaneIndex
int getPlaneIndex(uint32_t crtc_index, int32_t *plane_index)
Gets the plane indexes supported by the given crtc index.
NvDrmRenderer
Helper class for rendering using LibDRM.
Definition: NvDrmRenderer.h:93
_NvDrmFB::fb_id
uint32_t fb_id
Holds the frame ID.
Definition: NvDrmRenderer.h:66
NvDrmRenderer::enqueBuffer
int enqueBuffer(int fd)
Enqueues a buffer file descriptor for rendering.
NvDrmRenderer::getCrtcCount
int getCrtcCount()
Gets count of available CRTCs.