Skip to main content

edgefirst_image/gl/
threaded.rs

1// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4use edgefirst_decoder::{DetectBox, ProtoData, Segmentation};
5use std::panic::AssertUnwindSafe;
6use std::ptr::NonNull;
7use std::thread::JoinHandle;
8use tokio::sync::mpsc::{Sender, WeakSender};
9
10use super::processor::GLProcessorST;
11use super::shaders::check_gl_error;
12use super::{EglDisplayKind, Int8InterpolationMode, TransferBackend};
13use crate::{Crop, Error, Flip, ImageProcessorTrait, Rotation};
14use edgefirst_tensor::TensorDyn;
15
16#[allow(clippy::type_complexity)]
17enum GLProcessorMessage {
18    ImageConvert(
19        SendablePtr<TensorDyn>,
20        SendablePtr<TensorDyn>,
21        Rotation,
22        Flip,
23        Crop,
24        bool, // defer: skip the per-tile glFinish (batch); flush syncs once
25        tokio::sync::oneshot::Sender<Result<(), Error>>,
26    ),
27    /// Complete any deferred batch render with a single GPU sync.
28    Flush(tokio::sync::oneshot::Sender<Result<(), Error>>),
29    SetColors(
30        Vec<[u8; 4]>,
31        tokio::sync::oneshot::Sender<Result<(), Error>>,
32    ),
33    DrawDecodedMasks(
34        SendablePtr<TensorDyn>,
35        SendablePtr<DetectBox>,
36        SendablePtr<Segmentation>,
37        f32,                            // opacity
38        Option<SendablePtr<TensorDyn>>, // background
39        Option<[f32; 4]>,               // letterbox
40        crate::ColorMode,
41        tokio::sync::oneshot::Sender<Result<(), Error>>,
42    ),
43    DrawProtoMasks(
44        SendablePtr<TensorDyn>,
45        SendablePtr<DetectBox>,
46        SendablePtr<ProtoData>,
47        f32,                            // opacity
48        Option<SendablePtr<TensorDyn>>, // background
49        Option<[f32; 4]>,               // letterbox
50        crate::ColorMode,
51        tokio::sync::oneshot::Sender<Result<(), Error>>,
52    ),
53    SetInt8Interpolation(
54        Int8InterpolationMode,
55        tokio::sync::oneshot::Sender<Result<(), Error>>,
56    ),
57    /// Set the colorimetry/performance trade-off (`ColorimetryMode`).
58    SetColorimetryMode(
59        crate::ColorimetryMode,
60        tokio::sync::oneshot::Sender<Result<(), Error>>,
61    ),
62    /// Snapshot the EGLImage cache counters (steady-state import assertions).
63    EglCacheStats(tokio::sync::oneshot::Sender<Result<super::cache::GlCacheStats, Error>>),
64    PboCreate(
65        usize, // buffer size in bytes
66        tokio::sync::oneshot::Sender<Result<u32, Error>>,
67    ),
68    PboMap(
69        u32,   // buffer_id
70        usize, // size
71        tokio::sync::oneshot::Sender<Result<edgefirst_tensor::PboMapping, Error>>,
72    ),
73    PboUnmap(
74        u32, // buffer_id
75        tokio::sync::oneshot::Sender<Result<(), Error>>,
76    ),
77    PboDelete(u32), // fire-and-forget, no reply
78    CudaRegisterBuffer(u32, tokio::sync::oneshot::Sender<Option<usize>>),
79    CudaMap(usize, tokio::sync::oneshot::Sender<Option<(usize, usize)>>),
80    CudaUnmap(usize),      // fire-and-forget
81    CudaUnregister(usize), // fire-and-forget
82}
83
84/// Compute the flat element count for a PBO image buffer of the given format.
85///
86/// NV12 and NV16 are semiplanar with non-trivial element counts; all other
87/// formats use `width * height * channels`.
88///
89/// Returns `None` on `usize` overflow. A wrapped element count would size
90/// the PBO too small and corrupt memory on readback, so callers must treat
91/// `None` as an invalid-dimensions error (the same way they already reject
92/// a zero count).
93fn pbo_elem_count(
94    width: usize,
95    height: usize,
96    format: edgefirst_tensor::PixelFormat,
97) -> Option<usize> {
98    // Element count == product of the canonical image shape (the PBO holds u8,
99    // one byte per element). Delegating to `image_shape` keeps the combined
100    // semi-planar height in lockstep with every other consumer (and is exact
101    // for odd heights — `height*3/2` truncation under-sized odd-H NV12 by a
102    // row, and the old NV24 arm fell through to `channels` = far too small).
103    // `checked_mul` so a wrapped count can't silently undersize the PBO.
104    format
105        .image_shape(width, height)?
106        .iter()
107        .try_fold(1usize, |acc, &d| acc.checked_mul(d))
108}
109
110/// Compute the tensor shape for a PBO image of the given format — the canonical
111/// [`PixelFormat::image_shape`] (Planar `[C,H,W]`, SemiPlanar `[total_h, W]`
112/// with the odd-height-exact combined-plane height, Packed `[H,W,C]`).
113fn pbo_shape(width: usize, height: usize, format: edgefirst_tensor::PixelFormat) -> Vec<usize> {
114    format
115        .image_shape(width, height)
116        .unwrap_or_else(|| vec![height, width, format.channels()])
117}
118
119/// Best-effort registration of a freshly-created PBO with CUDA GL interop.
120///
121/// When libcudart is present, asks the GL worker (where the GL context is
122/// current) to call `cudaGraphicsGLRegisterBuffer` for `buffer_id`, then
123/// attaches a [`CudaHandle`] so `cuda_map()` can later yield a linear,
124/// 256-byte-aligned device pointer for TensorRT. On any failure (no CUDA,
125/// channel closed, registration rejected) the handle is left unset and the
126/// PBO is still usable as a normal CPU/GL buffer.
127fn register_pbo_cuda<T>(
128    tensor: &mut edgefirst_tensor::Tensor<T>,
129    buffer_id: u32,
130    size: usize,
131    sender: &Sender<GLProcessorMessage>,
132) where
133    T: num_traits::Num + Clone + std::fmt::Debug + Send + Sync,
134{
135    if !edgefirst_tensor::is_cuda_available() {
136        return;
137    }
138    let (tx, rx) = tokio::sync::oneshot::channel();
139    if sender
140        .blocking_send(GLProcessorMessage::CudaRegisterBuffer(buffer_id, tx))
141        .is_ok()
142    {
143        if let Ok(Some(resource)) = rx.blocking_recv() {
144            let ops = std::sync::Arc::new(GlCudaOps {
145                sender: sender.downgrade(),
146            });
147            tensor.set_cuda_handle(edgefirst_tensor::CudaHandle::new_gl(
148                resource as *mut std::ffi::c_void,
149                size,
150                ops,
151            ));
152        }
153    }
154}
155
156/// Implements PboOps by sending commands to the GL thread.
157///
158/// Uses a `WeakSender` so that PBO images don't keep the GL thread's channel
159/// alive. When the `GLProcessorThreaded` is dropped, its `Sender` is the last
160/// strong reference — dropping it closes the channel and lets the GL thread
161/// exit. PBO operations after that return `PboDisconnected`.
162struct GlPboOps {
163    sender: WeakSender<GLProcessorMessage>,
164}
165
166// SAFETY: GlPboOps sends all GL operations to the dedicated GL thread via a
167// channel. `map_buffer` returns a CPU-visible pointer from `glMapBufferRange`
168// that remains valid until `unmap_buffer` calls `glUnmapBuffer` on the GL thread.
169// `delete_buffer` sends a fire-and-forget deletion command to the GL thread.
170unsafe impl edgefirst_tensor::PboOps for GlPboOps {
171    fn map_buffer(
172        &self,
173        buffer_id: u32,
174        size: usize,
175    ) -> edgefirst_tensor::Result<edgefirst_tensor::PboMapping> {
176        let sender = self
177            .sender
178            .upgrade()
179            .ok_or(edgefirst_tensor::Error::PboDisconnected)?;
180        let (tx, rx) = tokio::sync::oneshot::channel();
181        sender
182            .blocking_send(GLProcessorMessage::PboMap(buffer_id, size, tx))
183            .map_err(|_| edgefirst_tensor::Error::PboDisconnected)?;
184        rx.blocking_recv()
185            .map_err(|_| edgefirst_tensor::Error::PboDisconnected)?
186            .map_err(|e| {
187                edgefirst_tensor::Error::NotImplemented(format!("GL PBO map failed: {e:?}"))
188            })
189    }
190
191    fn unmap_buffer(&self, buffer_id: u32) -> edgefirst_tensor::Result<()> {
192        let sender = self
193            .sender
194            .upgrade()
195            .ok_or(edgefirst_tensor::Error::PboDisconnected)?;
196        let (tx, rx) = tokio::sync::oneshot::channel();
197        sender
198            .blocking_send(GLProcessorMessage::PboUnmap(buffer_id, tx))
199            .map_err(|_| edgefirst_tensor::Error::PboDisconnected)?;
200        rx.blocking_recv()
201            .map_err(|_| edgefirst_tensor::Error::PboDisconnected)?
202            .map_err(|e| {
203                edgefirst_tensor::Error::NotImplemented(format!("GL PBO unmap failed: {e:?}"))
204            })
205    }
206
207    fn delete_buffer(&self, buffer_id: u32) {
208        if let Some(sender) = self.sender.upgrade() {
209            let _ = sender.blocking_send(GLProcessorMessage::PboDelete(buffer_id));
210        }
211    }
212}
213
214/// Routes CUDA GL-interop map/unmap/unregister to the GL thread.
215///
216/// Mirrors [`GlPboOps`]: holds a [`WeakSender`] so a registered PBO doesn't
217/// keep the GL thread's channel alive. The CUDA primitives
218/// (`cudaGraphicsMapResources` etc.) require the GL context to be current,
219/// so they must execute on the dedicated GL worker thread.
220struct GlCudaOps {
221    sender: WeakSender<GLProcessorMessage>,
222}
223
224impl edgefirst_tensor::CudaGlOps for GlCudaOps {
225    fn map(&self, resource: *mut std::ffi::c_void) -> Option<(*mut std::ffi::c_void, usize)> {
226        let sender = self.sender.upgrade()?;
227        let (tx, rx) = tokio::sync::oneshot::channel();
228        sender
229            .blocking_send(GLProcessorMessage::CudaMap(resource as usize, tx))
230            .ok()?;
231        rx.blocking_recv()
232            .ok()?
233            .map(|(p, l)| (p as *mut std::ffi::c_void, l))
234    }
235
236    fn unmap(&self, resource: *mut std::ffi::c_void) {
237        if let Some(sender) = self.sender.upgrade() {
238            let _ = sender.blocking_send(GLProcessorMessage::CudaUnmap(resource as usize));
239        }
240    }
241
242    fn unregister(&self, resource: *mut std::ffi::c_void) {
243        if let Some(sender) = self.sender.upgrade() {
244            let _ = sender.blocking_send(GLProcessorMessage::CudaUnregister(resource as usize));
245        }
246    }
247}
248
249/// OpenGL multi-threaded image converter. The actual conversion is done in a
250/// separate rendering thread, as OpenGL contexts are not thread-safe. This can
251/// be safely sent between threads. The `convert()` call sends the conversion
252/// request to the rendering thread and waits for the result.
253#[derive(Debug)]
254pub struct GLProcessorThreaded {
255    // This is only None when the converter is being dropped.
256    handle: Option<JoinHandle<()>>,
257
258    // This is only None when the converter is being dropped.
259    sender: Option<Sender<GLProcessorMessage>>,
260    /// Immutable capability surface (transfer backend, float render
261    /// support, serialization policy), captured once from the worker at
262    /// construction. See `PlatformCaps` in `platform/mod.rs`.
263    caps: super::platform::PlatformCaps,
264}
265
266unsafe impl Send for GLProcessorThreaded {}
267unsafe impl Sync for GLProcessorThreaded {}
268
269struct SendablePtr<T: Send> {
270    ptr: NonNull<T>,
271    len: usize,
272}
273
274unsafe impl<T> Send for SendablePtr<T> where T: Send {}
275
276/// Extract a human-readable message from a `catch_unwind` panic payload.
277fn panic_message(info: &(dyn std::any::Any + Send)) -> String {
278    if let Some(s) = info.downcast_ref::<&str>() {
279        s.to_string()
280    } else if let Some(s) = info.downcast_ref::<String>() {
281        s.clone()
282    } else {
283        "unknown panic".to_string()
284    }
285}
286
287impl GLProcessorThreaded {
288    /// Creates a new OpenGL multi-threaded image converter.
289    pub fn new(kind: Option<EglDisplayKind>) -> Result<Self, Error> {
290        let (send, mut recv) = tokio::sync::mpsc::channel::<GLProcessorMessage>(1);
291
292        let (create_ctx_send, create_ctx_recv) = tokio::sync::oneshot::channel();
293
294        let func = move || {
295            // Creation runs under the global lifecycle lock on Linux
296            // (bring-up races on some embedded drivers); ANGLE serializes
297            // display-level entry points internally, so macOS needs none
298            // (validated by the A0 lifecycle-churn spike leg).
299            #[cfg(target_os = "linux")]
300            let init_result = {
301                let _guard = super::context::GL_MUTEX
302                    .lock()
303                    .unwrap_or_else(|e| e.into_inner());
304                GLProcessorST::new(kind)
305            };
306            #[cfg(target_os = "macos")]
307            let init_result = GLProcessorST::new(kind);
308            let mut gl_converter = match init_result {
309                Ok(gl) => gl,
310                Err(e) => {
311                    let _ = create_ctx_send.send(Err(e));
312                    return;
313                }
314            };
315            // Capability surface captured ONCE before the message loop —
316            // immutable for the worker's life, never re-probed per message.
317            let caps = gl_converter.platform_caps();
318            let _ = create_ctx_send.send(Ok(caps));
319            let mut poisoned = false;
320            // Per-message serialization policy: Full where the platform
321            // demands it (`caps.serialize_gl` — Vivante/galcore is not
322            // thread-safe for concurrent GL across contexts), LifecycleOnly
323            // everywhere else — multiple processors then run GL work in
324            // parallel, each on its own thread + context. See the GL_MUTEX
325            // doc comment in context.rs. EDGEFIRST_GL_SERIALIZE overrides
326            // per-driver defaults (full = escape hatch for unknown-bad
327            // drivers; lifecycle = force-parallel).
328            let serialize_per_msg = match std::env::var("EDGEFIRST_GL_SERIALIZE").as_deref() {
329                Ok("full") => true,
330                Ok("lifecycle") => false,
331                _ => caps.serialize_gl,
332            };
333            log::debug!(
334                "GL serialization policy: {}",
335                if serialize_per_msg {
336                    "Full (per-message global lock)"
337                } else {
338                    "LifecycleOnly (parallel across processors)"
339                }
340            );
341            while let Some(msg) = recv.blocking_recv() {
342                // Full policy: one processor's message at a time, process-wide.
343                // Linux locks GL_MUTEX (messages must also exclude the locked
344                // lifecycle operations — on Vivante a convert racing a context
345                // bring-up is part of the driver bug class). macOS has no
346                // lifecycle lock (ANGLE serializes display entry points
347                // internally), so message-vs-message exclusion suffices —
348                // needed on virtualized GPUs, where concurrent GL across
349                // contexts mis-renders (paravirtual Metal, macOS CI runners).
350                #[cfg(target_os = "linux")]
351                let _guard = serialize_per_msg.then(|| {
352                    super::context::GL_MUTEX
353                        .lock()
354                        .unwrap_or_else(|e| e.into_inner())
355                });
356                #[cfg(target_os = "macos")]
357                let _guard = serialize_per_msg.then(|| {
358                    static MSG_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
359                    MSG_MUTEX.lock().unwrap_or_else(|e| e.into_inner())
360                });
361
362                // After a panic, the GL context is in an undefined state. Reject
363                // all subsequent messages with an error rather than risking wrong
364                // output or a GPU hang from corrupted GL state. This follows the
365                // same pattern as std::sync::Mutex poisoning.
366                if poisoned {
367                    let poison_err = crate::Error::Internal(
368                        "GL context is poisoned after a prior panic".to_string(),
369                    );
370                    match msg {
371                        GLProcessorMessage::ImageConvert(.., resp) => {
372                            let _ = resp.send(Err(poison_err));
373                        }
374                        GLProcessorMessage::Flush(resp) => {
375                            let _ = resp.send(Err(poison_err));
376                        }
377                        GLProcessorMessage::DrawDecodedMasks(.., resp) => {
378                            let _ = resp.send(Err(poison_err));
379                        }
380                        GLProcessorMessage::DrawProtoMasks(.., resp) => {
381                            let _ = resp.send(Err(poison_err));
382                        }
383                        GLProcessorMessage::SetColors(_, resp) => {
384                            let _ = resp.send(Err(poison_err));
385                        }
386                        GLProcessorMessage::SetInt8Interpolation(_, resp) => {
387                            let _ = resp.send(Err(poison_err));
388                        }
389                        GLProcessorMessage::SetColorimetryMode(_, resp) => {
390                            let _ = resp.send(Err(poison_err));
391                        }
392                        GLProcessorMessage::EglCacheStats(resp) => {
393                            let _ = resp.send(Err(poison_err));
394                        }
395                        GLProcessorMessage::PboCreate(_, resp) => {
396                            let _ = resp.send(Err(poison_err));
397                        }
398                        GLProcessorMessage::PboMap(_, _, resp) => {
399                            let _ = resp.send(Err(poison_err));
400                        }
401                        GLProcessorMessage::PboUnmap(_, resp) => {
402                            let _ = resp.send(Err(poison_err));
403                        }
404                        GLProcessorMessage::PboDelete(_) => {}
405                        GLProcessorMessage::CudaRegisterBuffer(_, resp) => {
406                            let _ = resp.send(None);
407                        }
408                        GLProcessorMessage::CudaMap(_, resp) => {
409                            let _ = resp.send(None);
410                        }
411                        // GL context is poisoned/dead — the GL buffer is unusable, so unmapping/
412                        // unregistering the CUDA interop is moot (the resource is reclaimed at
413                        // process exit). Same accepted "GL is gone" no-op as PboDelete above.
414                        GLProcessorMessage::CudaUnmap(_) => {}
415                        GLProcessorMessage::CudaUnregister(_) => {}
416                    }
417                    continue;
418                }
419
420                match msg {
421                    GLProcessorMessage::ImageConvert(
422                        src,
423                        mut dst,
424                        rotation,
425                        flip,
426                        crop,
427                        defer,
428                        resp,
429                    ) => {
430                        // SAFETY: This is safe because the convert() function waits for the resp to
431                        // be sent before dropping the borrow for src and dst
432                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
433                            let src = unsafe { src.ptr.as_ref() };
434                            let dst = unsafe { dst.ptr.as_mut() };
435                            // A deferred convert skips the per-tile glFinish and
436                            // leaves a single sync owed at Flush; an eager convert
437                            // finishes as usual.
438                            if defer {
439                                gl_converter.convert_deferred(src, dst, rotation, flip, crop)
440                            } else {
441                                gl_converter.convert(src, dst, rotation, flip, crop)
442                            }
443                        }));
444                        let _ = resp.send(match result {
445                            Ok(res) => res,
446                            Err(e) => {
447                                poisoned = true;
448                                Err(crate::Error::Internal(format!(
449                                    "GL thread panicked during ImageConvert: {}",
450                                    panic_message(e.as_ref()),
451                                )))
452                            }
453                        });
454                    }
455                    GLProcessorMessage::Flush(resp) => {
456                        let result =
457                            std::panic::catch_unwind(AssertUnwindSafe(|| gl_converter.flush()));
458                        let _ = resp.send(match result {
459                            Ok(res) => res,
460                            Err(e) => {
461                                poisoned = true;
462                                Err(crate::Error::Internal(format!(
463                                    "GL thread panicked during Flush: {}",
464                                    panic_message(e.as_ref()),
465                                )))
466                            }
467                        });
468                    }
469                    GLProcessorMessage::DrawDecodedMasks(
470                        mut dst,
471                        det,
472                        seg,
473                        opacity,
474                        bg,
475                        letterbox,
476                        color_mode,
477                        resp,
478                    ) => {
479                        // SAFETY: This is safe because the draw_decoded_masks() function waits for the
480                        // resp to be sent before dropping the borrow for dst, detect,
481                        // segmentation, and background
482                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
483                            let dst = unsafe { dst.ptr.as_mut() };
484                            let det =
485                                unsafe { std::slice::from_raw_parts(det.ptr.as_ptr(), det.len) };
486                            let seg =
487                                unsafe { std::slice::from_raw_parts(seg.ptr.as_ptr(), seg.len) };
488                            let bg_ref = bg.map(|p| unsafe { &*p.ptr.as_ptr() });
489                            gl_converter.draw_decoded_masks(
490                                dst,
491                                det,
492                                seg,
493                                crate::MaskOverlay {
494                                    background: bg_ref,
495                                    opacity,
496                                    letterbox,
497                                    color_mode,
498                                },
499                            )
500                        }));
501                        let _ = resp.send(match result {
502                            Ok(res) => res,
503                            Err(e) => {
504                                poisoned = true;
505                                Err(crate::Error::Internal(format!(
506                                    "GL thread panicked during DrawDecodedMasks: {}",
507                                    panic_message(e.as_ref()),
508                                )))
509                            }
510                        });
511                    }
512                    GLProcessorMessage::DrawProtoMasks(
513                        mut dst,
514                        det,
515                        proto_data,
516                        opacity,
517                        bg,
518                        letterbox,
519                        color_mode,
520                        resp,
521                    ) => {
522                        // SAFETY: Same safety invariant as DrawDecodedMasks — caller
523                        // blocks on resp before dropping borrows.
524                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
525                            let dst = unsafe { dst.ptr.as_mut() };
526                            let det =
527                                unsafe { std::slice::from_raw_parts(det.ptr.as_ptr(), det.len) };
528                            let bg_ref = bg.map(|p| unsafe { &*p.ptr.as_ptr() });
529                            let proto_data = unsafe { proto_data.ptr.as_ref() };
530                            gl_converter.draw_proto_masks(
531                                dst,
532                                det,
533                                proto_data,
534                                crate::MaskOverlay {
535                                    background: bg_ref,
536                                    opacity,
537                                    letterbox,
538                                    color_mode,
539                                },
540                            )
541                        }));
542                        let _ = resp.send(match result {
543                            Ok(res) => res,
544                            Err(e) => {
545                                poisoned = true;
546                                Err(crate::Error::Internal(format!(
547                                    "GL thread panicked during DrawProtoMasks: {}",
548                                    panic_message(e.as_ref()),
549                                )))
550                            }
551                        });
552                    }
553                    GLProcessorMessage::SetColors(colors, resp) => {
554                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
555                            gl_converter.set_class_colors(&colors)
556                        }));
557                        let _ = resp.send(match result {
558                            Ok(res) => res,
559                            Err(e) => {
560                                poisoned = true;
561                                Err(crate::Error::Internal(format!(
562                                    "GL thread panicked during SetColors: {}",
563                                    panic_message(e.as_ref()),
564                                )))
565                            }
566                        });
567                    }
568                    GLProcessorMessage::SetColorimetryMode(mode, resp) => {
569                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
570                            gl_converter.set_colorimetry_mode(mode);
571                            Ok(())
572                        }));
573                        let _ = resp.send(match result {
574                            Ok(res) => res,
575                            Err(e) => {
576                                poisoned = true;
577                                Err(crate::Error::Internal(format!(
578                                    "GL thread panicked during SetColorimetryMode: {}",
579                                    panic_message(e.as_ref()),
580                                )))
581                            }
582                        });
583                    }
584                    GLProcessorMessage::SetInt8Interpolation(mode, resp) => {
585                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
586                            gl_converter.set_int8_interpolation_mode(mode);
587                            Ok(())
588                        }));
589                        let _ = resp.send(match result {
590                            Ok(res) => res,
591                            Err(e) => {
592                                poisoned = true;
593                                Err(crate::Error::Internal(format!(
594                                    "GL thread panicked during SetInt8Interpolation: {}",
595                                    panic_message(e.as_ref()),
596                                )))
597                            }
598                        });
599                    }
600                    GLProcessorMessage::EglCacheStats(resp) => {
601                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
602                            Ok(gl_converter.egl_cache_stats())
603                        }));
604                        let _ = resp.send(match result {
605                            Ok(res) => res,
606                            Err(e) => {
607                                poisoned = true;
608                                Err(crate::Error::Internal(format!(
609                                    "GL thread panicked during EglCacheStats: {}",
610                                    panic_message(e.as_ref()),
611                                )))
612                            }
613                        });
614                    }
615                    GLProcessorMessage::PboCreate(size, resp) => {
616                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
617                            let mut id: u32 = 0;
618                            gls::gl::GenBuffers(1, &mut id);
619                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, id);
620                            gls::gl::BufferData(
621                                gls::gl::PIXEL_PACK_BUFFER,
622                                size as isize,
623                                std::ptr::null(),
624                                gls::gl::STREAM_COPY,
625                            );
626                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
627                            match check_gl_error("PboCreate", 0) {
628                                Ok(()) => Ok(id),
629                                Err(e) => {
630                                    gls::gl::DeleteBuffers(1, &id);
631                                    Err(e)
632                                }
633                            }
634                        }));
635                        let _ = resp.send(match result {
636                            Ok(res) => res,
637                            Err(e) => {
638                                poisoned = true;
639                                Err(crate::Error::Internal(format!(
640                                    "GL thread panicked during PboCreate: {}",
641                                    panic_message(e.as_ref()),
642                                )))
643                            }
644                        });
645                    }
646                    GLProcessorMessage::PboMap(buffer_id, size, resp) => {
647                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
648                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, buffer_id);
649                            let ptr = gls::gl::MapBufferRange(
650                                gls::gl::PIXEL_PACK_BUFFER,
651                                0,
652                                size as isize,
653                                gls::gl::MAP_READ_BIT | gls::gl::MAP_WRITE_BIT,
654                            );
655                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
656                            if ptr.is_null() {
657                                Err(crate::Error::OpenGl(
658                                    "glMapBufferRange returned null".to_string(),
659                                ))
660                            } else {
661                                Ok(edgefirst_tensor::PboMapping {
662                                    ptr: ptr as *mut u8,
663                                    size,
664                                })
665                            }
666                        }));
667                        let _ = resp.send(match result {
668                            Ok(res) => res,
669                            Err(e) => {
670                                poisoned = true;
671                                Err(crate::Error::Internal(format!(
672                                    "GL thread panicked during PboMap: {}",
673                                    panic_message(e.as_ref()),
674                                )))
675                            }
676                        });
677                    }
678                    GLProcessorMessage::PboUnmap(buffer_id, resp) => {
679                        let result = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
680                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, buffer_id);
681                            let ok = gls::gl::UnmapBuffer(gls::gl::PIXEL_PACK_BUFFER);
682                            gls::gl::BindBuffer(gls::gl::PIXEL_PACK_BUFFER, 0);
683                            if ok == gls::gl::FALSE {
684                                Err(Error::OpenGl(
685                                    "PBO data was corrupted during mapping".into(),
686                                ))
687                            } else {
688                                check_gl_error("PboUnmap", 0)
689                            }
690                        }));
691                        let _ = resp.send(match result {
692                            Ok(res) => res,
693                            Err(e) => {
694                                poisoned = true;
695                                Err(crate::Error::Internal(format!(
696                                    "GL thread panicked during PboUnmap: {}",
697                                    panic_message(e.as_ref()),
698                                )))
699                            }
700                        });
701                    }
702                    GLProcessorMessage::PboDelete(buffer_id) => {
703                        if let Err(e) = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe {
704                            gls::gl::DeleteBuffers(1, &buffer_id);
705                        })) {
706                            poisoned = true;
707                            log::error!(
708                                "GL thread panicked during PboDelete: {}",
709                                panic_message(e.as_ref()),
710                            );
711                        }
712                    }
713                    GLProcessorMessage::CudaRegisterBuffer(buffer_id, resp) => {
714                        // CUDA GL-interop must run on the GL-context thread.
715                        let _ = resp.send(edgefirst_tensor::gl_register_buffer(buffer_id));
716                    }
717                    GLProcessorMessage::CudaMap(resource, resp) => {
718                        // Auto-flush: if a batched `convert_deferred` is still
719                        // owed its single GPU sync, complete it before CUDA maps
720                        // the buffer — otherwise the device could read a render
721                        // still in flight. This makes `cuda_map()` correct on a
722                        // batched output with no API change (the caller pays the
723                        // sync it would have owed anyway).
724                        gl_converter.flush_pending();
725                        let _ = resp.send(edgefirst_tensor::gl_map_resource(resource));
726                    }
727                    GLProcessorMessage::CudaUnmap(resource) => {
728                        edgefirst_tensor::gl_unmap_resource(resource);
729                    }
730                    GLProcessorMessage::CudaUnregister(resource) => {
731                        edgefirst_tensor::gl_unregister_resource(resource);
732                    }
733                }
734                // Per-pass platform texture attachments (macOS pbuffer
735                // binds) are released once the message's GPU work has
736                // synced; deferred batches keep theirs until Flush.
737                gl_converter.end_gpu_pass_if_synced();
738            }
739            // Explicitly drop under the mutex so EGL teardown is serialized
740            // (Linux; ANGLE teardown is display-internal on macOS).
741            #[cfg(target_os = "linux")]
742            let _guard = super::context::GL_MUTEX
743                .lock()
744                .unwrap_or_else(|e| e.into_inner());
745            drop(gl_converter);
746        };
747
748        // let handle = tokio::task::spawn(func());
749        let handle = std::thread::spawn(func);
750
751        let caps = match create_ctx_recv.blocking_recv() {
752            Ok(Err(e)) => return Err(e),
753            Err(_) => {
754                return Err(Error::Internal(
755                    "GL converter error messaging closed without update".to_string(),
756                ));
757            }
758            Ok(Ok(caps)) => caps,
759        };
760
761        Ok(Self {
762            handle: Some(handle),
763            sender: Some(send),
764            caps,
765        })
766    }
767}
768
769impl ImageProcessorTrait for GLProcessorThreaded {
770    fn convert(
771        &mut self,
772        src: &TensorDyn,
773        dst: &mut TensorDyn,
774        rotation: crate::Rotation,
775        flip: Flip,
776        crop: Crop,
777    ) -> crate::Result<()> {
778        let (err_send, err_recv) = tokio::sync::oneshot::channel();
779        self.sender
780            .as_ref()
781            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
782            .blocking_send(GLProcessorMessage::ImageConvert(
783                SendablePtr {
784                    ptr: NonNull::from(src),
785                    len: 1,
786                },
787                SendablePtr {
788                    ptr: NonNull::from(dst),
789                    len: 1,
790                },
791                rotation,
792                flip,
793                crop,
794                false, // eager: finish per convert
795                err_send,
796            ))
797            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
798        err_recv.blocking_recv().map_err(|_| {
799            Error::Internal("GL converter error messaging closed without update".to_string())
800        })?
801    }
802
803    fn convert_deferred(
804        &mut self,
805        src: &TensorDyn,
806        dst: &mut TensorDyn,
807        rotation: crate::Rotation,
808        flip: Flip,
809        crop: Crop,
810    ) -> crate::Result<()> {
811        let (err_send, err_recv) = tokio::sync::oneshot::channel();
812        self.sender
813            .as_ref()
814            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
815            .blocking_send(GLProcessorMessage::ImageConvert(
816                SendablePtr {
817                    ptr: NonNull::from(src),
818                    len: 1,
819                },
820                SendablePtr {
821                    ptr: NonNull::from(dst),
822                    len: 1,
823                },
824                rotation,
825                flip,
826                crop,
827                true, // defer: skip glFinish; flush() syncs the batch once
828                err_send,
829            ))
830            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
831        err_recv.blocking_recv().map_err(|_| {
832            Error::Internal("GL converter error messaging closed without update".to_string())
833        })?
834    }
835
836    fn flush(&mut self) -> crate::Result<()> {
837        let (err_send, err_recv) = tokio::sync::oneshot::channel();
838        self.sender
839            .as_ref()
840            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
841            .blocking_send(GLProcessorMessage::Flush(err_send))
842            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
843        err_recv.blocking_recv().map_err(|_| {
844            Error::Internal("GL converter error messaging closed without update".to_string())
845        })?
846    }
847
848    fn draw_decoded_masks(
849        &mut self,
850        dst: &mut TensorDyn,
851        detect: &[crate::DetectBox],
852        segmentation: &[crate::Segmentation],
853        overlay: crate::MaskOverlay<'_>,
854    ) -> crate::Result<()> {
855        let (err_send, err_recv) = tokio::sync::oneshot::channel();
856        self.sender
857            .as_ref()
858            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
859            .blocking_send(GLProcessorMessage::DrawDecodedMasks(
860                SendablePtr {
861                    ptr: NonNull::from(dst),
862                    len: 1,
863                },
864                SendablePtr {
865                    ptr: NonNull::new(detect.as_ptr() as *mut DetectBox).unwrap(),
866                    len: detect.len(),
867                },
868                SendablePtr {
869                    ptr: NonNull::new(segmentation.as_ptr() as *mut Segmentation).unwrap(),
870                    len: segmentation.len(),
871                },
872                overlay.opacity,
873                overlay.background.map(|bg| SendablePtr {
874                    ptr: NonNull::from(bg).cast::<TensorDyn>(),
875                    len: 1,
876                }),
877                overlay.letterbox,
878                overlay.color_mode,
879                err_send,
880            ))
881            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
882        err_recv.blocking_recv().map_err(|_| {
883            Error::Internal("GL converter error messaging closed without update".to_string())
884        })?
885    }
886
887    fn draw_proto_masks(
888        &mut self,
889        dst: &mut TensorDyn,
890        detect: &[DetectBox],
891        proto_data: &ProtoData,
892        overlay: crate::MaskOverlay<'_>,
893    ) -> crate::Result<()> {
894        let (err_send, err_recv) = tokio::sync::oneshot::channel();
895        self.sender
896            .as_ref()
897            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
898            .blocking_send(GLProcessorMessage::DrawProtoMasks(
899                SendablePtr {
900                    ptr: NonNull::from(dst),
901                    len: 1,
902                },
903                SendablePtr {
904                    ptr: NonNull::new(detect.as_ptr() as *mut DetectBox).unwrap(),
905                    len: detect.len(),
906                },
907                SendablePtr {
908                    ptr: NonNull::from(proto_data).cast::<ProtoData>(),
909                    len: 1,
910                },
911                overlay.opacity,
912                overlay.background.map(|bg| SendablePtr {
913                    ptr: NonNull::from(bg).cast::<TensorDyn>(),
914                    len: 1,
915                }),
916                overlay.letterbox,
917                overlay.color_mode,
918                err_send,
919            ))
920            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
921        err_recv.blocking_recv().map_err(|_| {
922            Error::Internal("GL converter error messaging closed without update".to_string())
923        })?
924    }
925
926    fn set_class_colors(&mut self, colors: &[[u8; 4]]) -> Result<(), crate::Error> {
927        let (err_send, err_recv) = tokio::sync::oneshot::channel();
928        self.sender
929            .as_ref()
930            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
931            .blocking_send(GLProcessorMessage::SetColors(colors.to_vec(), err_send))
932            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
933        err_recv.blocking_recv().map_err(|_| {
934            Error::Internal("GL converter error messaging closed without update".to_string())
935        })?
936    }
937}
938
939impl GLProcessorThreaded {
940    /// Sets the colorimetry/performance trade-off (see
941    /// [`crate::ColorimetryMode`]). The `EDGEFIRST_COLORIMETRY` environment
942    /// variable takes precedence — when set, this call logs and keeps the
943    /// env-selected mode.
944    pub fn set_colorimetry_mode(&mut self, mode: crate::ColorimetryMode) -> Result<(), Error> {
945        let (err_send, err_recv) = tokio::sync::oneshot::channel();
946        self.sender
947            .as_ref()
948            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
949            .blocking_send(GLProcessorMessage::SetColorimetryMode(mode, err_send))
950            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
951        err_recv.blocking_recv().map_err(|_| {
952            Error::Internal("GL converter error messaging closed without update".to_string())
953        })?
954    }
955
956    /// Sets the interpolation mode for int8 proto textures.
957    pub fn set_int8_interpolation_mode(
958        &mut self,
959        mode: Int8InterpolationMode,
960    ) -> Result<(), crate::Error> {
961        let (err_send, err_recv) = tokio::sync::oneshot::channel();
962        self.sender
963            .as_ref()
964            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
965            .blocking_send(GLProcessorMessage::SetInt8Interpolation(mode, err_send))
966            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
967        err_recv.blocking_recv().map_err(|_| {
968            Error::Internal("GL converter error messaging closed without update".to_string())
969        })?
970    }
971
972    /// Snapshot the EGLImage cache counters (src, dst, NV R8) from the GL
973    /// thread. Steady-state tests capture this after warmup and after an
974    /// N-frame loop over a fixed buffer pool and assert
975    /// [`total_misses`](super::cache::GlCacheStats::total_misses) stays flat —
976    /// any increase means a convert re-imported a buffer it should have found
977    /// cached (the cache-behavior equality gate for GL refactors).
978    pub fn egl_cache_stats(&self) -> Result<super::cache::GlCacheStats, Error> {
979        let (send, recv) = tokio::sync::oneshot::channel();
980        self.sender
981            .as_ref()
982            .ok_or_else(|| Error::Internal("GL processor is shutting down".to_string()))?
983            .blocking_send(GLProcessorMessage::EglCacheStats(send))
984            .map_err(|_| Error::Internal("GL converter thread exited".to_string()))?;
985        recv.blocking_recv().map_err(|_| {
986            Error::Internal("GL converter error messaging closed without update".to_string())
987        })?
988    }
989
990    /// Create a PBO-backed [`Tensor<u8>`] image on the GL thread.
991    pub fn create_pbo_image(
992        &self,
993        width: usize,
994        height: usize,
995        format: edgefirst_tensor::PixelFormat,
996    ) -> Result<edgefirst_tensor::Tensor<u8>, Error> {
997        let sender = self
998            .sender
999            .as_ref()
1000            .ok_or(Error::OpenGl("GL processor is shutting down".to_string()))?;
1001
1002        let size = pbo_elem_count(width, height, format)
1003            .filter(|&s| s != 0)
1004            .ok_or_else(|| Error::OpenGl("Invalid image dimensions".to_string()))?;
1005
1006        // Allocate PBO on the GL thread
1007        let (tx, rx) = tokio::sync::oneshot::channel();
1008        sender
1009            .blocking_send(GLProcessorMessage::PboCreate(size, tx))
1010            .map_err(|_| Error::OpenGl("GL thread channel closed".to_string()))?;
1011        let buffer_id = rx
1012            .blocking_recv()
1013            .map_err(|_| Error::OpenGl("GL thread did not respond".to_string()))??;
1014
1015        let ops: std::sync::Arc<dyn edgefirst_tensor::PboOps> = std::sync::Arc::new(GlPboOps {
1016            sender: sender.downgrade(),
1017        });
1018
1019        let shape = pbo_shape(width, height, format);
1020
1021        let pbo_tensor =
1022            edgefirst_tensor::PboTensor::<u8>::from_pbo(buffer_id, size, &shape, None, ops)
1023                .map_err(|e| Error::OpenGl(format!("PBO tensor creation failed: {e:?}")))?;
1024        let mut tensor = edgefirst_tensor::Tensor::from_pbo(pbo_tensor);
1025        tensor
1026            .set_format(format)
1027            .map_err(|e| Error::OpenGl(format!("Failed to set format on PBO tensor: {e:?}")))?;
1028        // Register the PBO with CUDA (best-effort; no-op without libcudart) so
1029        // `cuda_map()` can yield a device pointer — matching the float PBO path
1030        // in `create_pbo_image_dtype`. Without this, u8 PBOs were never
1031        // CUDA-interop-capable, so the codec's nvJPEG backend (and any CUDA
1032        // consumer) could not use a `create_image`-allocated PBO destination.
1033        // The i8 transmute by the caller preserves the attached handle.
1034        register_pbo_cuda(&mut tensor, buffer_id, size, sender);
1035        Ok(tensor)
1036    }
1037
1038    /// Create a PBO-backed [`TensorDyn`] image on the GL thread with the given dtype.
1039    ///
1040    /// Sizes the underlying GL buffer by `elems * dtype.size()` and wraps it in
1041    /// the appropriately-typed [`PboTensor`]. Supports `DType::U8`, `DType::F16`,
1042    /// and `DType::F32`; returns an error for other dtypes.
1043    pub(crate) fn create_pbo_image_dtype(
1044        &self,
1045        width: usize,
1046        height: usize,
1047        format: edgefirst_tensor::PixelFormat,
1048        dtype: edgefirst_tensor::DType,
1049    ) -> Result<TensorDyn, Error> {
1050        let sender = self
1051            .sender
1052            .as_ref()
1053            .ok_or(Error::OpenGl("GL processor is shutting down".to_string()))?;
1054
1055        let elems = pbo_elem_count(width, height, format)
1056            .filter(|&e| e != 0)
1057            .ok_or_else(|| Error::OpenGl("Invalid image dimensions".to_string()))?;
1058
1059        let size = elems
1060            .checked_mul(dtype.size())
1061            .ok_or_else(|| Error::OpenGl("PBO size overflow".to_string()))?;
1062
1063        // Allocate PBO on the GL thread
1064        let (tx, rx) = tokio::sync::oneshot::channel();
1065        sender
1066            .blocking_send(GLProcessorMessage::PboCreate(size, tx))
1067            .map_err(|_| Error::OpenGl("GL thread channel closed".to_string()))?;
1068        let buffer_id = rx
1069            .blocking_recv()
1070            .map_err(|_| Error::OpenGl("GL thread did not respond".to_string()))??;
1071
1072        let ops: std::sync::Arc<dyn edgefirst_tensor::PboOps> = std::sync::Arc::new(GlPboOps {
1073            sender: sender.downgrade(),
1074        });
1075
1076        let shape = pbo_shape(width, height, format);
1077
1078        let map_err = |e: edgefirst_tensor::Error| {
1079            Error::OpenGl(format!("PBO tensor creation failed: {e:?}"))
1080        };
1081        let set_err = |e: edgefirst_tensor::Error| {
1082            Error::OpenGl(format!("Failed to set format on PBO tensor: {e:?}"))
1083        };
1084
1085        match dtype {
1086            edgefirst_tensor::DType::U8 => {
1087                let pbo =
1088                    edgefirst_tensor::PboTensor::<u8>::from_pbo(buffer_id, size, &shape, None, ops)
1089                        .map_err(map_err)?;
1090                let mut t = edgefirst_tensor::Tensor::from_pbo(pbo);
1091                t.set_format(format).map_err(set_err)?;
1092                register_pbo_cuda(&mut t, buffer_id, size, sender);
1093                Ok(TensorDyn::from(t))
1094            }
1095            edgefirst_tensor::DType::F16 => {
1096                let pbo = edgefirst_tensor::PboTensor::<edgefirst_tensor::f16>::from_pbo(
1097                    buffer_id, size, &shape, None, ops,
1098                )
1099                .map_err(map_err)?;
1100                let mut t = edgefirst_tensor::Tensor::from_pbo(pbo);
1101                t.set_format(format).map_err(set_err)?;
1102                register_pbo_cuda(&mut t, buffer_id, size, sender);
1103                Ok(TensorDyn::from(t))
1104            }
1105            edgefirst_tensor::DType::F32 => {
1106                let pbo = edgefirst_tensor::PboTensor::<f32>::from_pbo(
1107                    buffer_id, size, &shape, None, ops,
1108                )
1109                .map_err(map_err)?;
1110                let mut t = edgefirst_tensor::Tensor::from_pbo(pbo);
1111                t.set_format(format).map_err(set_err)?;
1112                register_pbo_cuda(&mut t, buffer_id, size, sender);
1113                Ok(TensorDyn::from(t))
1114            }
1115            other => Err(Error::OpenGl(format!("unsupported PBO dtype {other:?}"))),
1116        }
1117    }
1118
1119    /// Returns the active transfer backend.
1120    pub(crate) fn transfer_backend(&self) -> TransferBackend {
1121        self.caps.transfer_backend
1122    }
1123
1124    /// Report which float dtypes the GPU can render to.
1125    ///
1126    /// Values are probed once at construction time and adjusted for
1127    /// Vivante GC7000UL, whose float readback latency (170-320 ms) makes
1128    /// GL float destinations impractical; `ImageProcessor::convert()` falls
1129    /// back to CPU float output (normalized to `[0, 1]`) for these targets.
1130    pub(crate) fn supported_render_dtypes(&self) -> crate::RenderDtypeSupport {
1131        self.caps.render_dtypes
1132    }
1133}
1134
1135impl Drop for GLProcessorThreaded {
1136    fn drop(&mut self) {
1137        drop(self.sender.take());
1138        let _ = self.handle.take().and_then(|h| h.join().ok());
1139    }
1140}
1141
1142// `pbo_elem_count` / `pbo_shape` are pure (no GL), so they are unit-testable
1143// without a GPU. The overflow→None arm of `pbo_elem_count` guards against an
1144// undersized PBO allocation, so it is worth pinning explicitly.
1145#[cfg(test)]
1146#[cfg_attr(coverage_nightly, coverage(off))]
1147mod tests {
1148    use super::{pbo_elem_count, pbo_shape};
1149    use edgefirst_tensor::PixelFormat;
1150
1151    #[test]
1152    fn elem_count_per_format() {
1153        // Packed RGBA: w*h*4.
1154        assert_eq!(pbo_elem_count(8, 4, PixelFormat::Rgba), Some(8 * 4 * 4));
1155        // Packed RGB: w*h*3.
1156        assert_eq!(pbo_elem_count(8, 4, PixelFormat::Rgb), Some(8 * 4 * 3));
1157        // NV12 semiplanar: w*h*3/2.
1158        assert_eq!(pbo_elem_count(8, 4, PixelFormat::Nv12), Some(8 * 4 * 3 / 2));
1159        // NV16 semiplanar: w*h*2.
1160        assert_eq!(pbo_elem_count(8, 4, PixelFormat::Nv16), Some(8 * 4 * 2));
1161    }
1162
1163    #[test]
1164    fn elem_count_overflow_is_none() {
1165        // w*h already overflows usize → None (never a wrapped, undersized count).
1166        assert_eq!(pbo_elem_count(usize::MAX, 2, PixelFormat::Rgba), None);
1167        // w*h fits but *channels overflows → None.
1168        assert_eq!(pbo_elem_count(usize::MAX, 1, PixelFormat::Rgb), None);
1169    }
1170
1171    #[test]
1172    fn shape_per_format() {
1173        // Planar: [channels, height, width].
1174        assert_eq!(pbo_shape(8, 4, PixelFormat::PlanarRgb), vec![3, 4, 8]);
1175        // SemiPlanar NV12: [height*3/2, width].
1176        assert_eq!(pbo_shape(8, 4, PixelFormat::Nv12), vec![4 * 3 / 2, 8]);
1177        // SemiPlanar NV16: [height*2, width].
1178        assert_eq!(pbo_shape(8, 4, PixelFormat::Nv16), vec![4 * 2, 8]);
1179        // Packed: [height, width, channels].
1180        assert_eq!(pbo_shape(8, 4, PixelFormat::Rgba), vec![4, 8, 4]);
1181    }
1182}