|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | + * copy of this software and associated documentation files (the "Software"), |
| 7 | + * to deal in the Software without restriction, including without limitation |
| 8 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | + * DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef CUOSD_H |
| 25 | +#define CUOSD_H |
| 26 | + |
| 27 | +typedef struct |
| 28 | +{ |
| 29 | +} cuOSDContext; |
| 30 | + |
| 31 | +typedef cuOSDContext *cuOSDContext_t; |
| 32 | + |
| 33 | +enum class cuOSDClockFormat : int |
| 34 | +{ |
| 35 | + None = 0, |
| 36 | + YYMMDD_HHMMSS = 1, |
| 37 | + YYMMDD = 2, |
| 38 | + HHMMSS = 3 |
| 39 | +}; |
| 40 | + |
| 41 | +enum class cuOSDImageFormat : int |
| 42 | +{ |
| 43 | + None = 0, |
| 44 | + RGB = 1, |
| 45 | + RGBA = 2, |
| 46 | + BlockLinearNV12 = 3, |
| 47 | + PitchLinearNV12 = 4 |
| 48 | +}; |
| 49 | + |
| 50 | +enum class cuOSDTextBackend : int |
| 51 | +{ |
| 52 | + None = 0, |
| 53 | + PangoCairo = 1, |
| 54 | + StbTrueType = 2 |
| 55 | +}; |
| 56 | + |
| 57 | +typedef struct _cuOSDColor |
| 58 | +{ |
| 59 | + unsigned char r; |
| 60 | + unsigned char g; |
| 61 | + unsigned char b; |
| 62 | + unsigned char a; |
| 63 | +} cuOSDColor; |
| 64 | + |
| 65 | +// cuosd_context_create: support online generate text bitmap with required font. |
| 66 | +cuOSDContext_t cuosd_context_create(); |
| 67 | + |
| 68 | +// set context text rendering backend |
| 69 | +void cuosd_set_text_backend(cuOSDContext_t context, cuOSDTextBackend text_backend); |
| 70 | + |
| 71 | +// cuosd_context_destroy: deallocate all resource related to allocated cuOSD context |
| 72 | +void cuosd_context_destroy(cuOSDContext_t context); |
| 73 | + |
| 74 | +// cuosd_measure_text: API to get tight width, height and upper offset from the given text's tight bounding box |
| 75 | +void cuosd_measure_text(cuOSDContext_t context, const char *utf8_text, int font_size, const char *font, int *width, |
| 76 | + int *height, int *yoffset); |
| 77 | + |
| 78 | +// cuosd_draw_text: draw utf8 text on given cuOSD context. |
| 79 | +// x, y stands for left upper corner of the text's bounding box. |
| 80 | +// bg_color stands for textbox background color in case alpha != 0 |
| 81 | +// Draw nothing if font_size <=0, font_size is scaled by 3 and clamped to 10 - 500 pixels by default |
| 82 | +void cuosd_draw_text(cuOSDContext_t context, const char *utf8_text, int font_size, const char *font, int x, int y, |
| 83 | + cuOSDColor border_color, cuOSDColor bg_color = {0, 0, 0, 0}); |
| 84 | + |
| 85 | +// cuosd_draw_clock: draw clock element on given cuOSD context. |
| 86 | +// x, y stands for left upper corner of the text's bounding box. 3 clock formats are supported: |
| 87 | +// YYMMDD_HHMMSS, YYMMDD, HHMMSS |
| 88 | +// Draw nothing if font_size <=0, font_size is scaled by 3 and clamped to 10 - 500 pixels by default |
| 89 | +void cuosd_draw_clock(cuOSDContext_t context, cuOSDClockFormat format, long time, int font_size, const char *font, |
| 90 | + int x, int y, cuOSDColor border_color, cuOSDColor bg_color = {0, 0, 0, 0}); |
| 91 | + |
| 92 | +// cuosd_draw_line: draw line element on given cuOSD context. |
| 93 | +// x0, y0 stands for start point coordinate of the line, and x1, y1 stands for end point coordinate of the line. |
| 94 | +void cuosd_draw_line(cuOSDContext_t context, int x0, int y0, int x1, int y1, int thickness, cuOSDColor color, |
| 95 | + bool interpolation = true); |
| 96 | + |
| 97 | +// cuosd_draw_arrow: draw arrow element on given cuOSD context. |
| 98 | +// x0, y0 stands for start point coordinate of the arrow, and x1, y1 stands for end point coordinate of the arrow. |
| 99 | +void cuosd_draw_arrow(cuOSDContext_t context, int x0, int y0, int x1, int y1, int arrow_size, int thickness, |
| 100 | + cuOSDColor color, bool interpolation = false); |
| 101 | + |
| 102 | +// cuosd_draw_point: draw point element on given cuOSD context. |
| 103 | +// cx, cy stands for center point coordinate of the point. |
| 104 | +void cuosd_draw_point(cuOSDContext_t context, int cx, int cy, int radius, cuOSDColor color); |
| 105 | + |
| 106 | +// cuosd_draw_circle: draw circle element on given cuOSD context. |
| 107 | +// cx, cy stands for center point coordinate of the circle. |
| 108 | +// thickness stands for border width when thickness > 0; stands for filled mode when thickness = -1. |
| 109 | +// bg_color stands for inner color inside hollow circle in case alpha != 0 |
| 110 | +void cuosd_draw_circle(cuOSDContext_t context, int cx, int cy, int radius, int thickness, cuOSDColor border_color, |
| 111 | + cuOSDColor bg_color = {0, 0, 0, 0}); |
| 112 | + |
| 113 | +// cuosd_draw_rectangle: draw rectangle element on given cuOSD context. |
| 114 | +// thickness stands for border width when thickness > 0; stands for filled mode when thickness = -1. |
| 115 | +// bg_color stands for inner color inside hollow rectangle in case alpha != 0 |
| 116 | +void cuosd_draw_rectangle(cuOSDContext_t context, int left, int top, int right, int bottom, int thickness, |
| 117 | + cuOSDColor border_color, cuOSDColor bg_color = {0, 0, 0, 0}); |
| 118 | + |
| 119 | +// cuosd_draw_boxblur: Mean filtering in the region of interest |
| 120 | +// The region of interest is first scaled to 32x32, filtered, and then scaled to the region of interest by nearest neighbor interpolation |
| 121 | +// It is executed by a separate kernel function that is independent from the other drawing functions |
| 122 | +void cuosd_draw_boxblur(cuOSDContext_t context, int left, int top, int right, int bottom, int kernel_size = 7); |
| 123 | + |
| 124 | +// cuosd_draw_rotationbox: draw rotated rectangle element on given cuOSD context. |
| 125 | +// yaw: rotation angle from y-axis, clockwise +, unit in rad. |
| 126 | +void cuosd_draw_rotationbox(cuOSDContext_t _context, int cx, int cy, int width, int height, float yaw, int thickness, |
| 127 | + cuOSDColor border_color, bool interpolation = false, cuOSDColor bg_color = {0, 0, 0, 0}); |
| 128 | + |
| 129 | +// cuosd_draw_segmentmask: draw segmentation mask on given cuOSD context. |
| 130 | +// d_seg: device pointer of segmentation mask, alpha in seg_color is ignored. |
| 131 | +// thickness should > 0 for drawing border, threshold: Threshold for binarization |
| 132 | +// 1. resize mask rect to object rect of given left, top, right, bottom. |
| 133 | +// 2. set the alpha to 127 if mask value > threshold, else alpha = 0. |
| 134 | +void cuosd_draw_segmentmask(cuOSDContext_t context, int left, int top, int right, int bottom, int thickness, |
| 135 | + float *d_seg, int seg_width, int seg_height, float seg_threshold, cuOSDColor border_color, |
| 136 | + cuOSDColor seg_color = {0, 0, 0, 0}); |
| 137 | + |
| 138 | +// cuosd_draw_polyline: draw polyline element on given cuOSD context. |
| 139 | +// h_pts: host point of polyline points in { int x, int y } data format. |
| 140 | +// d_pts: device point of polyline points, shall not be nullptr if fill_color.a != 0. |
| 141 | +// n_pts: number of polyline points, thickness: polyline thickness. |
| 142 | +// is_closed: if the end point shall be connected to start point. |
| 143 | +// border_color: polyline color, fill_color: polyfill color. |
| 144 | +void cuosd_draw_polyline(cuOSDContext_t context, int *h_pts, int *d_pts, int n_pts, int thickness, bool is_closed, |
| 145 | + cuOSDColor border_color, bool interpolation = true, cuOSDColor fill_color = {0, 0, 0, 0}); |
| 146 | + |
| 147 | +// cuosd_draw_rgba_source: draw color from rgba source image on given cuOSD context. |
| 148 | +// 1. resize incoming rgba source rect to target rect of given left, top, right, bottom. |
| 149 | +// 2. blend incoming rgba src on target image rect in bilinear interpolation manner. |
| 150 | +void cuosd_draw_rgba_source(cuOSDContext_t _context, int left, int top, int right, int bottom, void *d_src, |
| 151 | + int src_width, int src_stride, int src_height); |
| 152 | + |
| 153 | +// cuosd_draw_nv12_source: draw color from nv12 source image on given cuOSD context. |
| 154 | +// 1. resize incoming nv12 source rect to target rect of given left, top, right, bottom. |
| 155 | +// 2. blend incoming nv12 src on target image rect in bilinear interpolation manner. |
| 156 | +// note: use unified alpha and can support both PL and BL nv12 format. |
| 157 | +void cuosd_draw_nv12_source(cuOSDContext_t context, int left, int top, int right, int bottom, void *d_src0, |
| 158 | + void *d_src1, int src_width, int src_stride, int src_height, unsigned char alpha = 127, |
| 159 | + bool block_linear = false); |
| 160 | + |
| 161 | +// cuosd_apply: calculate bounding box of all elements and transfer drawing commands to GPU. |
| 162 | +// If format is RGBA, data0 is RGBA buffer, and data1 must be nullptr. |
| 163 | +// If format is BlockLinearNV12, data0 and data1 is cudaSurfaceObject_t for Luma(Y) plane and Chroma(UV) plane |
| 164 | +// If format is PitchLinearNV12, data0 is Luma(Y) plane buffer, and data1 is Chroma(UV) plane buffer |
| 165 | +void cuosd_apply(cuOSDContext_t context, void *data0, void *data1, int width, int stride, int height, |
| 166 | + cuOSDImageFormat format, void *stream = nullptr, bool launch_and_clear = true); |
| 167 | + |
| 168 | +// clear all pushed commands |
| 169 | +void cuosd_clear(cuOSDContext_t context); |
| 170 | + |
| 171 | +// cuosd_launch: launch drawing kernel in async manner. |
| 172 | +// If format is RGBA, data0 is RGBA buffer, and data1 must be nullptr. |
| 173 | +// If format is BlockLinearNV12, data0 and data1 is cudaSurfaceObject_t for Luma(Y) plane and Chroma(UV) plane |
| 174 | +// If format is PitchLinearNV12, data0 is Luma(Y) plane buffer, and data1 is Chroma(UV) plane buffer |
| 175 | +void cuosd_launch(cuOSDContext_t context, void *data0, void *data1, int width, int stride, int height, |
| 176 | + cuOSDImageFormat format, void *stream = nullptr); |
| 177 | + |
| 178 | +#endif // CUOSD_H |
0 commit comments