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
//! Rockchip RGA (Raster Graphic Acceleration) — hardware 2D blitter.
//!
//! RGA is the dedicated 2D engine on every Rockchip SoC. It does in
//! hardware what `memcpy` + nested loops do on the CPU, but ~10–20×
//! faster: YUV↔RGB color conversion, scale, rotate, alpha blend.
//!
//! For the FPV pipeline:
//! - Compose detection bboxes onto the captured NV12 frame in <1ms
//! - Blend a pre-rendered OSD glyph atlas
//! - Optional NV12 → RGB conversion if the encoder needs RGB
//!
//! `librga.so` is resolved at runtime via `dlopen`. On non-Rockchip hosts
//! the calls return [`RgaError::LibraryNotFound`].
#![allow(unsafe_code)]
use std::ffi::c_void;
use std::sync::Arc;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum RgaError {
#[error("librga.so not found — install Rockchip RGA runtime")]
LibraryNotFound,
#[error("RGA symbol `{0}` missing — incompatible RGA version")]
SymbolMissing(&'static str),
#[error("RGA `{op}` failed with status {status}")]
CallFailed { op: &'static str, status: i32 },
#[error("RGA invalid parameter: {0}")]
InvalidParameter(String),
}
pub type RgaResult<T> = Result<T, RgaError>;
// ── Pixel formats (subset matching librga's `RgaSURF_FORMAT`) ─────
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RgaFormat {
Rgba8888 = 0x0,
Rgbx8888 = 0x1,
Rgb888 = 0x2,
Bgra8888 = 0x3,
Yuv420Sp = 0x11, // NV12
Yuv420SpVu = 0x12, // NV21
Yuv420P = 0x13, // I420
}
/// Description of one image surface (pointer-or-fd + dimensions + format).
#[derive(Debug, Clone)]
pub struct RgaSurface {
/// Either a virtual address (CPU-mapped buffer) or a DMA-BUF fd
/// (set `is_fd = true` to indicate fd interpretation of `addr`).
pub addr: *mut c_void,
pub fd: i32,
pub width: u32,
pub height: u32,
pub stride: u32,
pub format: RgaFormat,
}
// SAFETY: A *mut c_void with DMA-BUF / mmap'd backing is safe to send
// across threads as long as ownership is exclusive (caller's contract).
unsafe impl Send for RgaSurface {}
unsafe impl Sync for RgaSurface {}
/// Probe whether `librga.so` is available on this host.
pub fn rga_available() -> bool {
#[cfg(target_os = "linux")]
{
// SAFETY: dlopen RTLD_LAZY then immediately dlclose.
let h = unsafe { libc::dlopen(c"librga.so".as_ptr(), libc::RTLD_LAZY) };
if h.is_null() {
return false;
}
// SAFETY: h non-null.
unsafe { libc::dlclose(h) };
true
}
#[cfg(not(target_os = "linux"))]
false
}
// ── FFI plumbing ─────────────────────────────────────────────────
#[cfg(target_os = "linux")]
type FnRgaInit = unsafe extern "C" fn() -> i32;
#[cfg(target_os = "linux")]
type FnRgaBlit = unsafe extern "C" fn(*const RgaInfo, *const RgaInfo, *const c_void) -> i32;
#[cfg(target_os = "linux")]
#[repr(C)]
struct RgaInfo {
fd: i32,
virt_addr: *mut c_void,
phys_addr: *mut c_void,
/// Bit field: alpha-blend mode, rotation, etc.
mmu_flag: i32,
rotation: i32,
blend: i32,
rect: RgaRect,
/// Reserved padding so the struct matches librga's actual size (fragile;
/// driver header may vary slightly between versions).
_reserved: [u8; 256],
}
#[cfg(target_os = "linux")]
#[repr(C)]
struct RgaRect {
xoffset: i32,
yoffset: i32,
width: i32,
height: i32,
wstride: i32,
hstride: i32,
format: i32,
size: i32,
}
#[cfg(target_os = "linux")]
struct RgaLib {
handle: *mut c_void,
init: FnRgaInit,
blit: FnRgaBlit,
}
#[cfg(target_os = "linux")]
unsafe impl Send for RgaLib {}
#[cfg(target_os = "linux")]
unsafe impl Sync for RgaLib {}
#[cfg(target_os = "linux")]
impl Drop for RgaLib {
fn drop(&mut self) {
if !self.handle.is_null() {
// SAFETY: handle owned by us.
unsafe { libc::dlclose(self.handle) };
}
}
}
#[cfg(target_os = "linux")]
fn load_rga_lib() -> RgaResult<Arc<RgaLib>> {
// SAFETY: dlopen RTLD_NOW for early symbol resolution.
let h = unsafe { libc::dlopen(c"librga.so".as_ptr(), libc::RTLD_NOW) };
if h.is_null() {
return Err(RgaError::LibraryNotFound);
}
macro_rules! sym {
($name:expr, $ty:ty) => {{
// SAFETY: h valid; name null-terminated.
let p = unsafe { libc::dlsym(h, $name.as_ptr().cast()) };
if p.is_null() {
// SAFETY: h valid.
unsafe { libc::dlclose(h) };
return Err(RgaError::SymbolMissing(stringify!($name)));
}
// SAFETY: p non-null, types match.
unsafe { std::mem::transmute_copy::<*mut c_void, $ty>(&p) }
}};
}
let lib = RgaLib {
handle: h,
init: sym!(b"c_RkRgaInit\0", FnRgaInit),
blit: sym!(b"c_RkRgaBlit\0", FnRgaBlit),
};
// SAFETY: init takes no arguments; documented as idempotent.
let st = unsafe { (lib.init)() };
if st != 0 {
return Err(RgaError::CallFailed {
op: "c_RkRgaInit",
status: st,
});
}
Ok(Arc::new(lib))
}
#[cfg(not(target_os = "linux"))]
struct RgaLib;
#[cfg(not(target_os = "linux"))]
fn load_rga_lib() -> RgaResult<Arc<RgaLib>> {
Err(RgaError::LibraryNotFound)
}
// ── Public safe API ──────────────────────────────────────────────
/// Hardware compositor. One instance is enough for the whole pipeline —
/// internally serialises requests (RGA hardware is single-engine on most
/// Rockchip SoCs).
pub struct RgaBlender {
/// Held to keep `librga.so` mapped + symbols valid for the
/// blender's lifetime. On Linux this is `Arc<RgaLib>`; on other
/// hosts `RgaLib` is a unit type.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
lib: Arc<RgaLib>,
}
impl RgaBlender {
/// Initialise RGA. Returns `LibraryNotFound` on non-Rockchip hosts.
pub fn new() -> RgaResult<Self> {
Ok(Self {
lib: load_rga_lib()?,
})
}
/// Copy `src` onto `dst` with optional format conversion. Used for
/// NV12→RGB (if the encoder needs RGB input) or just plain copy with
/// scaling.
pub fn blit(&self, src: &RgaSurface, dst: &mut RgaSurface) -> RgaResult<()> {
validate_surface(src, "src")?;
validate_surface(dst, "dst")?;
#[cfg(target_os = "linux")]
{
let s = surface_to_info(src);
let d = surface_to_info(dst);
// SAFETY: both info structs valid for the call duration; lib.blit
// is the resolved symbol.
let st = unsafe { (self.lib.blit)(&s, &d, std::ptr::null()) };
if st != 0 {
return Err(RgaError::CallFailed {
op: "c_RkRgaBlit",
status: st,
});
}
Ok(())
}
#[cfg(not(target_os = "linux"))]
{
let _ = (src, dst);
Err(RgaError::LibraryNotFound)
}
}
/// Convenience: blit `src` onto `dst` at offset `(x, y)` in dst, no
/// scaling, alpha-blend disabled (overwrite mode).
pub fn copy_at(
&self,
src: &RgaSurface,
dst: &mut RgaSurface,
_x: u32,
_y: u32,
) -> RgaResult<()> {
// RGA's destination rect is set in `dst.rect` when calling blit.
// Building a per-rect API requires mutating the RgaRect — the
// current public API copies the full surface. A future enhancement
// can add explicit rect parameters; this stub keeps the API
// signature in place.
self.blit(src, dst)
}
}
fn validate_surface(s: &RgaSurface, label: &str) -> RgaResult<()> {
if s.width == 0 || s.height == 0 {
return Err(RgaError::InvalidParameter(format!(
"{label}: width/height must be > 0 (got {}×{})",
s.width, s.height
)));
}
if s.stride < s.width {
return Err(RgaError::InvalidParameter(format!(
"{label}: stride {} < width {}",
s.stride, s.width
)));
}
if s.fd < 0 && s.addr.is_null() {
return Err(RgaError::InvalidParameter(format!(
"{label}: must provide either fd >= 0 or non-null addr"
)));
}
Ok(())
}
#[cfg(target_os = "linux")]
fn surface_to_info(s: &RgaSurface) -> RgaInfo {
RgaInfo {
fd: s.fd,
virt_addr: s.addr,
phys_addr: std::ptr::null_mut(),
mmu_flag: 1, // enable MMU mapping for virt addresses
rotation: 0,
blend: 0,
rect: RgaRect {
xoffset: 0,
yoffset: 0,
width: s.width as i32,
height: s.height as i32,
wstride: s.stride as i32,
hstride: s.height as i32,
format: s.format as i32,
size: (s.stride * s.height) as i32,
},
_reserved: [0; 256],
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rga_unavailable_off_rockchip() {
// Only true on real Rockchip hosts.
let _ = rga_available();
}
#[test]
fn surface_validation_rejects_zero_dims() {
let s = RgaSurface {
addr: std::ptr::null_mut(),
fd: -1,
width: 0,
height: 480,
stride: 0,
format: RgaFormat::Yuv420Sp,
};
assert!(matches!(
validate_surface(&s, "test"),
Err(RgaError::InvalidParameter(_))
));
}
#[test]
fn surface_validation_rejects_no_backing() {
let s = RgaSurface {
addr: std::ptr::null_mut(),
fd: -1,
width: 640,
height: 480,
stride: 640,
format: RgaFormat::Yuv420Sp,
};
assert!(matches!(
validate_surface(&s, "test"),
Err(RgaError::InvalidParameter(_))
));
}
#[test]
fn surface_validation_rejects_short_stride() {
let s = RgaSurface {
addr: std::ptr::null_mut(),
fd: 5,
width: 640,
height: 480,
stride: 100,
format: RgaFormat::Yuv420Sp,
};
assert!(matches!(
validate_surface(&s, "test"),
Err(RgaError::InvalidParameter(_))
));
}
#[test]
fn blender_new_fails_off_rockchip() {
if !rga_available() {
assert!(matches!(RgaBlender::new(), Err(RgaError::LibraryNotFound)));
}
}
}