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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
// SPDX-License-Identifier: Apache-2.0
use edgefirst_decoder::{DetectBox, ProtoData, Segmentation};
use std::panic::AssertUnwindSafe;
use std::ptr::NonNull;
use std::thread::JoinHandle;
use tokio::sync::mpsc::{Sender, WeakSender};
use super::processor::GLProcessorST;
use super::shaders::check_gl_error;
use super::{EglDisplayKind, Int8InterpolationMode, TransferBackend};
use crate::{Crop, Error, Flip, ImageProcessorTrait, Rotation};
use edgefirst_tensor::TensorDyn;
#[allow(clippy::type_complexity)]
enum GLProcessorMessage {
ImageConvert(
SendablePtr<TensorDyn>,
SendablePtr<TensorDyn>,
Rotation,
Flip,
Crop,
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
SetColors(
Vec<[u8; 4]>,
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
DrawDecodedMasks(
SendablePtr<TensorDyn>,
SendablePtr<DetectBox>,
SendablePtr<Segmentation>,
f32, // opacity
Option<SendablePtr<TensorDyn>>, // background
Option<[f32; 4]>, // letterbox
crate::ColorMode,
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
DrawProtoMasks(
SendablePtr<TensorDyn>,
SendablePtr<DetectBox>,
SendablePtr<ProtoData>,
f32, // opacity
Option<SendablePtr<TensorDyn>>, // background
Option<[f32; 4]>, // letterbox
crate::ColorMode,
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
SetInt8Interpolation(
Int8InterpolationMode,
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
PboCreate(
usize, // buffer size in bytes
tokio::sync::oneshot::Sender<Result<u32, Error>>,
),
PboMap(
u32, // buffer_id
usize, // size
tokio::sync::oneshot::Sender<Result<edgefirst_tensor::PboMapping, Error>>,
),
PboUnmap(
u32, // buffer_id
tokio::sync::oneshot::Sender<Result<(), Error>>,
),
PboDelete(u32), // fire-and-forget, no reply
}
/// Implements PboOps by sending commands to the GL thread.
///
/// Uses a `WeakSender` so that PBO images don't keep the GL thread's channel
/// alive. When the `GLProcessorThreaded` is dropped, its `Sender` is the last
/// strong reference — dropping it closes the channel and lets the GL thread
/// exit. PBO operations after that return `PboDisconnected`.
struct GlPboOps {
sender: WeakSender<GLProcessorMessage>,
}
// SAFETY: GlPboOps sends all GL operations to the dedicated GL thread via a
// channel. `map_buffer` returns a CPU-visible pointer from `glMapBufferRange`
// that remains valid until `unmap_buffer` calls `glUnmapBuffer` on the GL thread.
// `delete_buffer` sends a fire-and-forget deletion command to the GL thread.
unsafe impl edgefirst_tensor::PboOps for GlPboOps {
fn map_buffer(
&self,
buffer_id: u32,
size: usize,
) -> edgefirst_tensor::Result<edgefirst_tensor::PboMapping> {
let sender = self
.sender
.upgrade()
.ok_or(edgefirst_tensor::Error::PboDisconnected)?;
let (tx, rx) = tokio::sync::oneshot::channel();
sender
.blocking_send(GLProcessorMessage::PboMap(buffer_id, size, tx))
.map_err(|_| edgefirst_tensor::Error::PboDisconnected)?;
rx.blocking_recv()
.map_err(|_| edgefirst_tensor::Error::PboDisconnected)?
.map_err(|e| {
edgefirst_tensor::Error::NotImplemented(format!("GL PBO map failed: {e:?}"))
})
}
fn unmap_buffer(&self, buffer_id: u32) -> edgefirst_tensor::Result<()> {
let sender = self
.sender
.upgrade()
.ok_or(edgefirst_tensor::Error::PboDisconnected)?;
let (tx, rx) = tokio::sync::oneshot::channel();
sender
.blocking_send(GLProcessorMessage::PboUnmap(buffer_id, tx))
.map_err(|_| edgefirst_tensor::Error::PboDisconnected)?;
rx.blocking_recv()
.map_err(|_| edgefirst_tensor::Error::PboDisconnected)?
.map_err(|e| {
edgefirst_tensor::Error::NotImplemented(format!("GL PBO unmap failed: {e:?}"))
})
}
fn delete_buffer(&self, buffer_id: u32) {
if let Some(sender) = self.sender.upgrade() {
let _ = sender.blocking_send(GLProcessorMessage::PboDelete(buffer_id));
}
}
}
/// OpenGL multi-threaded image converter. The actual conversion is done in a
/// separate rendering thread, as OpenGL contexts are not thread-safe. This can
/// be safely sent between threads. The `convert()` call sends the conversion
/// request to the rendering thread and waits for the result.
#[derive(Debug)]
pub struct GLProcessorThreaded {
// This is only None when the converter is being dropped.
handle: Option<JoinHandle<()>>,
// This is only None when the converter is being dropped.
sender: Option<Sender<GLProcessorMessage>>,
transfer_backend: TransferBackend,
}
unsafe impl Send for GLProcessorThreaded {}
unsafe impl Sync for GLProcessorThreaded {}
struct SendablePtr<T: Send> {
ptr: NonNull<T>,
len: usize,
}
unsafe impl<T> Send for SendablePtr<T> where T: Send {}
/// Extract a human-readable message from a `catch_unwind` panic payload.
fn panic_message(info: &(dyn std::any::Any + Send)) -> String {
if let Some(s) = info.downcast_ref::<&str>() {
s.to_string()
} else if let Some(s) = info.downcast_ref::<String>() {
s.clone()
} else {
"unknown panic".to_string()
}
}
impl GLProcessorThreaded {
/// Creates a new OpenGL multi-threaded image converter.
pub fn new(kind: Option<EglDisplayKind>) -> Result<Self, Error> {
let (send, mut recv) = tokio::sync::mpsc::channel::<GLProcessorMessage>(1);
let (create_ctx_send, create_ctx_recv) = tokio::sync::oneshot::channel();
let func = move || {
let init_result = {
let _guard = super::context::GL_MUTEX
.lock()
.unwrap_or_else(|e| e.into_inner());
GLProcessorST::new(kind)
};
let mut gl_converter = match init_result {
Ok(gl) => gl,
Err(e) => {
let _ = create_ctx_send.send(Err(e));
return;
}
};
let _ = create_ctx_send.send(Ok(gl_converter.gl_context.transfer_backend));
let mut poisoned = false;
while let Some(msg) = recv.blocking_recv() {
// Serialize all GL operations across GLProcessorST instances.
// See `GL_MUTEX` doc comment in context.rs for rationale.
let _guard = super::context::GL_MUTEX
.lock()
.unwrap_or_else(|e| e.into_inner());
// After a panic, the GL context is in an undefined state. Reject
// all subsequent messages with an error rather than risking wrong
// output or a GPU hang from corrupted GL state. This follows the
// same pattern as std::sync::Mutex poisoning.
if poisoned {
let poison_err = crate::Error::Internal(
"GL context is poisoned after a prior panic".to_string(),
);
match msg {
GLProcessorMessage::ImageConvert(.., resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::DrawDecodedMasks(.., resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::DrawProtoMasks(.., resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::SetColors(_, resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::SetInt8Interpolation(_, resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::PboCreate(_, resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::PboMap(_, _, resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::PboUnmap(_, resp) => {
let _ = resp.send(Err(poison_err));
}
GLProcessorMessage::PboDelete(_) => {}
}
continue;
}
match msg {
GLProcessorMessage::ImageConvert(src, mut dst, rotation, flip, crop, resp) => {
// SAFETY: This is safe because the convert() function waits for the resp to
// be sent before dropping the borrow for src and dst
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let src = unsafe { src.ptr.as_ref() };
let dst = unsafe { dst.ptr.as_mut() };
gl_converter.convert(src, dst, rotation, flip, crop)
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during ImageConvert: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::DrawDecodedMasks(
mut dst,
det,
seg,
opacity,
bg,
letterbox,
color_mode,
resp,
) => {
// SAFETY: This is safe because the draw_decoded_masks() function waits for the
// resp to be sent before dropping the borrow for dst, detect,
// segmentation, and background
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let dst = unsafe { dst.ptr.as_mut() };
let det =
unsafe { std::slice::from_raw_parts(det.ptr.as_ptr(), det.len) };
let seg =
unsafe { std::slice::from_raw_parts(seg.ptr.as_ptr(), seg.len) };
let bg_ref = bg.map(|p| unsafe { &*p.ptr.as_ptr() });
gl_converter.draw_decoded_masks(
dst,
det,
seg,
crate::MaskOverlay {
background: bg_ref,
opacity,
letterbox,
color_mode,
},
)
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during DrawDecodedMasks: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::DrawProtoMasks(
mut dst,
det,
proto_data,
opacity,
bg,
letterbox,
color_mode,
resp,
) => {
// SAFETY: Same safety invariant as DrawDecodedMasks — caller
// blocks on resp before dropping borrows.
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let dst = unsafe { dst.ptr.as_mut() };
let det =
unsafe { std::slice::from_raw_parts(det.ptr.as_ptr(), det.len) };
let bg_ref = bg.map(|p| unsafe { &*p.ptr.as_ptr() });
let proto_data = unsafe { proto_data.ptr.as_ref() };
gl_converter.draw_proto_masks(
dst,
det,
proto_data,
crate::MaskOverlay {
background: bg_ref,
opacity,
letterbox,
color_mode,
},
)
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during DrawProtoMasks: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::SetColors(colors, resp) => {
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
gl_converter.set_class_colors(&colors)
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during SetColors: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::SetInt8Interpolation(mode, resp) => {
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
gl_converter.set_int8_interpolation_mode(mode);
Ok(())
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during SetInt8Interpolation: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::PboCreate(size, resp) => {
let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
let mut id: u32 = 0;
gls::gl::GenBuffers(1, &mut id);
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, id);
gls::gl::BufferData(
gls::gl::PIXEL_PACK_BUFFER,
size as isize,
std::ptr::null(),
gls::gl::STREAM_COPY,
);
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
match check_gl_error("PboCreate", 0) {
Ok(()) => Ok(id),
Err(e) => {
gls::gl::DeleteBuffers(1, &id);
Err(e)
}
}
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during PboCreate: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::PboMap(buffer_id, size, resp) => {
let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, buffer_id);
let ptr = gls::gl::MapBufferRange(
gls::gl::PIXEL_PACK_BUFFER,
0,
size as isize,
gls::gl::MAP_READ_BIT | gls::gl::MAP_WRITE_BIT,
);
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
if ptr.is_null() {
Err(crate::Error::OpenGl(
"glMapBufferRange returned null".to_string(),
))
} else {
Ok(edgefirst_tensor::PboMapping {
ptr: ptr as *mut u8,
size,
})
}
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during PboMap: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::PboUnmap(buffer_id, resp) => {
let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, buffer_id);
let ok = gls::gl::UnmapBuffer(gls::gl::PIXEL_PACK_BUFFER);
gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
if ok == gls::gl::FALSE {
Err(Error::OpenGl(
"PBO data was corrupted during mapping".into(),
))
} else {
check_gl_error("PboUnmap", 0)
}
}));
let _ = resp.send(match result {
Ok(res) => res,
Err(e) => {
poisoned = true;
Err(crate::Error::Internal(format!(
"GL thread panicked during PboUnmap: {}",
panic_message(e.as_ref()),
)))
}
});
}
GLProcessorMessage::PboDelete(buffer_id) => {
if let Err(e) = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
gls::gl::DeleteBuffers(1, &buffer_id);
})) {
poisoned = true;
log::error!(
"GL thread panicked during PboDelete: {}",
panic_message(e.as_ref()),
);
}
}
}
}
// Explicitly drop under the mutex so EGL teardown is serialized.
let _guard = super::context::GL_MUTEX
.lock()
.unwrap_or_else(|e| e.into_inner());
drop(gl_converter);
};
// let handle = tokio::task::spawn(func());
let handle = std::thread::spawn(func);
let transfer_backend = match create_ctx_recv.blocking_recv() {
Ok(Err(e)) => return Err(e),
Err(_) => {
return Err(Error::Internal(
"GL converter error messaging closed without update".to_string(),
));
}
Ok(Ok(tb)) => tb,
};
Ok(Self {
handle: Some(handle),
sender: Some(send),
transfer_backend,
})
}
}
impl ImageProcessorTrait for GLProcessorThreaded {
fn convert(
&mut self,
src: &TensorDyn,
dst: &mut TensorDyn,
rotation: crate::Rotation,
flip: Flip,
crop: Crop,
) -> crate::Result<()> {
let (err_send, err_recv) = tokio::sync::oneshot::channel();
self.sender
.as_ref()
.ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
.blocking_send(GLProcessorMessage::ImageConvert(
SendablePtr {
ptr: NonNull::from(src),
len: 1,
},
SendablePtr {
ptr: NonNull::from(dst),
len: 1,
},
rotation,
flip,
crop,
err_send,
))
.map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
err_recv.blocking_recv().map_err(|_| {
Error::Internal("GL converter error messaging closed without update".to_string())
})?
}
fn draw_decoded_masks(
&mut self,
dst: &mut TensorDyn,
detect: &[crate::DetectBox],
segmentation: &[crate::Segmentation],
overlay: crate::MaskOverlay<'_>,
) -> crate::Result<()> {
let (err_send, err_recv) = tokio::sync::oneshot::channel();
self.sender
.as_ref()
.ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
.blocking_send(GLProcessorMessage::DrawDecodedMasks(
SendablePtr {
ptr: NonNull::from(dst),
len: 1,
},
SendablePtr {
ptr: NonNull::new(detect.as_ptr() as *mut DetectBox).unwrap(),
len: detect.len(),
},
SendablePtr {
ptr: NonNull::new(segmentation.as_ptr() as *mut Segmentation).unwrap(),
len: segmentation.len(),
},
overlay.opacity,
overlay.background.map(|bg| SendablePtr {
ptr: NonNull::from(bg).cast::<TensorDyn>(),
len: 1,
}),
overlay.letterbox,
overlay.color_mode,
err_send,
))
.map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
err_recv.blocking_recv().map_err(|_| {
Error::Internal("GL converter error messaging closed without update".to_string())
})?
}
fn draw_proto_masks(
&mut self,
dst: &mut TensorDyn,
detect: &[DetectBox],
proto_data: &ProtoData,
overlay: crate::MaskOverlay<'_>,
) -> crate::Result<()> {
let (err_send, err_recv) = tokio::sync::oneshot::channel();
self.sender
.as_ref()
.ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
.blocking_send(GLProcessorMessage::DrawProtoMasks(
SendablePtr {
ptr: NonNull::from(dst),
len: 1,
},
SendablePtr {
ptr: NonNull::new(detect.as_ptr() as *mut DetectBox).unwrap(),
len: detect.len(),
},
SendablePtr {
ptr: NonNull::from(proto_data).cast::<ProtoData>(),
len: 1,
},
overlay.opacity,
overlay.background.map(|bg| SendablePtr {
ptr: NonNull::from(bg).cast::<TensorDyn>(),
len: 1,
}),
overlay.letterbox,
overlay.color_mode,
err_send,
))
.map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
err_recv.blocking_recv().map_err(|_| {
Error::Internal("GL converter error messaging closed without update".to_string())
})?
}
fn set_class_colors(&mut self, colors: &[[u8; 4]]) -> Result<(), crate::Error> {
let (err_send, err_recv) = tokio::sync::oneshot::channel();
self.sender
.as_ref()
.ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
.blocking_send(GLProcessorMessage::SetColors(colors.to_vec(), err_send))
.map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
err_recv.blocking_recv().map_err(|_| {
Error::Internal("GL converter error messaging closed without update".to_string())
})?
}
}
impl GLProcessorThreaded {
/// Sets the interpolation mode for int8 proto textures.
pub fn set_int8_interpolation_mode(
&mut self,
mode: Int8InterpolationMode,
) -> Result<(), crate::Error> {
let (err_send, err_recv) = tokio::sync::oneshot::channel();
self.sender
.as_ref()
.ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
.blocking_send(GLProcessorMessage::SetInt8Interpolation(mode, err_send))
.map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
err_recv.blocking_recv().map_err(|_| {
Error::Internal("GL converter error messaging closed without update".to_string())
})?
}
/// Create a PBO-backed [`Tensor<u8>`] image on the GL thread.
pub fn create_pbo_image(
&self,
width: usize,
height: usize,
format: edgefirst_tensor::PixelFormat,
) -> Result<edgefirst_tensor::Tensor<u8>, Error> {
let sender = self
.sender
.as_ref()
.ok_or(Error::OpenGl("GL processor is shutting down".to_string()))?;
let channels = format.channels();
let size = match format.layout() {
edgefirst_tensor::PixelLayout::SemiPlanar => {
// NV12: W*H*3/2, NV16: W*H*2
match format {
edgefirst_tensor::PixelFormat::Nv12 => width * height * 3 / 2,
edgefirst_tensor::PixelFormat::Nv16 => width * height * 2,
_ => width * height * channels,
}
}
edgefirst_tensor::PixelLayout::Packed | edgefirst_tensor::PixelLayout::Planar => {
width * height * channels
}
_ => width * height * channels,
};
if size == 0 {
return Err(Error::OpenGl("Invalid image dimensions".to_string()));
}
// Allocate PBO on the GL thread
let (tx, rx) = tokio::sync::oneshot::channel();
sender
.blocking_send(GLProcessorMessage::PboCreate(size, tx))
.map_err(|_| Error::OpenGl("GL thread channel closed".to_string()))?;
let buffer_id = rx
.blocking_recv()
.map_err(|_| Error::OpenGl("GL thread did not respond".to_string()))??;
let ops: std::sync::Arc<dyn edgefirst_tensor::PboOps> = std::sync::Arc::new(GlPboOps {
sender: sender.downgrade(),
});
let shape = match format.layout() {
edgefirst_tensor::PixelLayout::Planar => vec![channels, height, width],
edgefirst_tensor::PixelLayout::SemiPlanar => {
let total_h = match format {
edgefirst_tensor::PixelFormat::Nv12 => height * 3 / 2,
edgefirst_tensor::PixelFormat::Nv16 => height * 2,
_ => height * 2,
};
vec![total_h, width]
}
_ => vec![height, width, channels],
};
let pbo_tensor =
edgefirst_tensor::PboTensor::<u8>::from_pbo(buffer_id, size, &shape, None, ops)
.map_err(|e| Error::OpenGl(format!("PBO tensor creation failed: {e:?}")))?;
let mut tensor = edgefirst_tensor::Tensor::from_pbo(pbo_tensor);
tensor
.set_format(format)
.map_err(|e| Error::OpenGl(format!("Failed to set format on PBO tensor: {e:?}")))?;
Ok(tensor)
}
/// Returns the active transfer backend.
pub(crate) fn transfer_backend(&self) -> TransferBackend {
self.transfer_backend
}
}
impl Drop for GLProcessorThreaded {
fn drop(&mut self) {
drop(self.sender.take());
let _ = self.handle.take().and_then(|h| h.join().ok());
}
}