Skip to main content

edgefirst_tensor/
tensor_dyn.rs

1// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{DType, PixelFormat, Tensor, TensorMemory, TensorTrait};
5use half::f16;
6use std::fmt;
7
8/// Type-erased tensor. Wraps a `Tensor<T>` with runtime element type.
9#[non_exhaustive]
10pub enum TensorDyn {
11    /// Unsigned 8-bit integer tensor.
12    U8(Tensor<u8>),
13    /// Signed 8-bit integer tensor.
14    I8(Tensor<i8>),
15    /// Unsigned 16-bit integer tensor.
16    U16(Tensor<u16>),
17    /// Signed 16-bit integer tensor.
18    I16(Tensor<i16>),
19    /// Unsigned 32-bit integer tensor.
20    U32(Tensor<u32>),
21    /// Signed 32-bit integer tensor.
22    I32(Tensor<i32>),
23    /// Unsigned 64-bit integer tensor.
24    U64(Tensor<u64>),
25    /// Signed 64-bit integer tensor.
26    I64(Tensor<i64>),
27    /// 16-bit floating-point tensor.
28    F16(Tensor<f16>),
29    /// 32-bit floating-point tensor.
30    F32(Tensor<f32>),
31    /// 64-bit floating-point tensor.
32    F64(Tensor<f64>),
33}
34
35/// Dispatch a method call across all TensorDyn variants.
36macro_rules! dispatch {
37    ($self:expr, $method:ident $(, $arg:expr)*) => {
38        match $self {
39            TensorDyn::U8(t) => t.$method($($arg),*),
40            TensorDyn::I8(t) => t.$method($($arg),*),
41            TensorDyn::U16(t) => t.$method($($arg),*),
42            TensorDyn::I16(t) => t.$method($($arg),*),
43            TensorDyn::U32(t) => t.$method($($arg),*),
44            TensorDyn::I32(t) => t.$method($($arg),*),
45            TensorDyn::U64(t) => t.$method($($arg),*),
46            TensorDyn::I64(t) => t.$method($($arg),*),
47            TensorDyn::F16(t) => t.$method($($arg),*),
48            TensorDyn::F32(t) => t.$method($($arg),*),
49            TensorDyn::F64(t) => t.$method($($arg),*),
50        }
51    };
52}
53
54/// Like [`dispatch!`], but for methods returning `Result<Tensor<T>>`: rewrap the
55/// typed result back into the matching `TensorDyn` variant. Keeps sub-region
56/// fan-out (`batch`, future `view`) to one line instead of an 11-arm match.
57macro_rules! dyn_fanout {
58    ($self:expr, $method:ident $(, $arg:expr)*) => {
59        match $self {
60            TensorDyn::U8(t) => t.$method($($arg),*).map(TensorDyn::U8),
61            TensorDyn::I8(t) => t.$method($($arg),*).map(TensorDyn::I8),
62            TensorDyn::U16(t) => t.$method($($arg),*).map(TensorDyn::U16),
63            TensorDyn::I16(t) => t.$method($($arg),*).map(TensorDyn::I16),
64            TensorDyn::U32(t) => t.$method($($arg),*).map(TensorDyn::U32),
65            TensorDyn::I32(t) => t.$method($($arg),*).map(TensorDyn::I32),
66            TensorDyn::U64(t) => t.$method($($arg),*).map(TensorDyn::U64),
67            TensorDyn::I64(t) => t.$method($($arg),*).map(TensorDyn::I64),
68            TensorDyn::F16(t) => t.$method($($arg),*).map(TensorDyn::F16),
69            TensorDyn::F32(t) => t.$method($($arg),*).map(TensorDyn::F32),
70            TensorDyn::F64(t) => t.$method($($arg),*).map(TensorDyn::F64),
71        }
72    };
73}
74
75/// Generate the three downcast methods (ref, mut ref, owned) for one variant.
76macro_rules! downcast_methods {
77    ($variant:ident, $ty:ty, $as_name:ident, $as_mut_name:ident, $into_name:ident) => {
78        /// Returns a shared reference to the inner tensor if the type matches.
79        pub fn $as_name(&self) -> Option<&Tensor<$ty>> {
80            match self {
81                Self::$variant(t) => Some(t),
82                _ => None,
83            }
84        }
85
86        /// Returns a mutable reference to the inner tensor if the type matches.
87        pub fn $as_mut_name(&mut self) -> Option<&mut Tensor<$ty>> {
88            match self {
89                Self::$variant(t) => Some(t),
90                _ => None,
91            }
92        }
93
94        /// Unwraps the inner tensor if the type matches, otherwise returns `self` as `Err`.
95        /// The Err variant is necessarily large (returns the unconsumed TensorDyn).
96        #[allow(clippy::result_large_err)]
97        pub fn $into_name(self) -> Result<Tensor<$ty>, Self> {
98            match self {
99                Self::$variant(t) => Ok(t),
100                other => Err(other),
101            }
102        }
103    };
104}
105
106impl TensorDyn {
107    /// Return the runtime element type discriminant.
108    pub fn dtype(&self) -> DType {
109        match self {
110            Self::U8(_) => DType::U8,
111            Self::I8(_) => DType::I8,
112            Self::U16(_) => DType::U16,
113            Self::I16(_) => DType::I16,
114            Self::U32(_) => DType::U32,
115            Self::I32(_) => DType::I32,
116            Self::U64(_) => DType::U64,
117            Self::I64(_) => DType::I64,
118            Self::F16(_) => DType::F16,
119            Self::F32(_) => DType::F32,
120            Self::F64(_) => DType::F64,
121        }
122    }
123
124    /// Return the tensor shape.
125    pub fn shape(&self) -> &[usize] {
126        dispatch!(self, shape)
127    }
128
129    /// Return the tensor name.
130    pub fn name(&self) -> String {
131        dispatch!(self, name)
132    }
133
134    /// Return the pixel format (None if not an image tensor).
135    pub fn format(&self) -> Option<PixelFormat> {
136        dispatch!(self, format)
137    }
138
139    /// Return the image width (None if not an image tensor).
140    pub fn width(&self) -> Option<usize> {
141        dispatch!(self, width)
142    }
143
144    /// Return the image height (None if not an image tensor).
145    pub fn height(&self) -> Option<usize> {
146        dispatch!(self, height)
147    }
148
149    /// Return the total size of this tensor in bytes.
150    pub fn size(&self) -> usize {
151        dispatch!(self, size)
152    }
153
154    /// Return the memory allocation type.
155    pub fn memory(&self) -> TensorMemory {
156        dispatch!(self, memory)
157    }
158
159    /// Reshape this tensor. Total element count must remain the same.
160    pub fn reshape(&mut self, shape: &[usize]) -> crate::Result<()> {
161        dispatch!(self, reshape, shape)
162    }
163
164    /// Attach pixel format metadata to this tensor.
165    ///
166    /// Validates that the tensor's shape is compatible with the format's
167    /// layout (packed, planar, or semi-planar).
168    ///
169    /// # Arguments
170    ///
171    /// * `format` - The pixel format to attach
172    ///
173    /// # Returns
174    ///
175    /// `Ok(())` on success, with the format stored as metadata on the tensor.
176    ///
177    /// # Errors
178    ///
179    /// Returns `Error::InvalidShape` if the tensor shape doesn't match
180    /// the expected layout for the given format.
181    pub fn set_format(&mut self, format: PixelFormat) -> crate::Result<()> {
182        dispatch!(self, set_format, format)
183    }
184
185    /// Attach pixel format metadata, consuming and returning self.
186    ///
187    /// Enables builder-style chaining.
188    ///
189    /// # Arguments
190    ///
191    /// * `format` - The pixel format to attach
192    ///
193    /// # Returns
194    ///
195    /// The tensor with format metadata attached.
196    ///
197    /// # Errors
198    ///
199    /// Returns `Error::InvalidShape` if the tensor shape doesn't match
200    /// the expected layout for the given format.
201    pub fn with_format(mut self, format: PixelFormat) -> crate::Result<Self> {
202        self.set_format(format)?;
203        Ok(self)
204    }
205
206    /// Colorimetry metadata (`None` = undefined; never auto-filled).
207    pub fn colorimetry(&self) -> Option<crate::Colorimetry> {
208        dispatch!(self, colorimetry)
209    }
210
211    /// Attach/clear colorimetry metadata.
212    pub fn set_colorimetry(&mut self, c: Option<crate::Colorimetry>) {
213        dispatch!(self, set_colorimetry, c)
214    }
215
216    /// Builder-style colorimetry attach (consumes and returns self).
217    pub fn with_colorimetry(mut self, c: crate::Colorimetry) -> Self {
218        self.set_colorimetry(Some(c));
219        self
220    }
221
222    /// Row stride in bytes (`None` = tightly packed).
223    pub fn row_stride(&self) -> Option<usize> {
224        dispatch!(self, row_stride)
225    }
226
227    /// Effective row stride: stored stride or computed from format and width.
228    pub fn effective_row_stride(&self) -> Option<usize> {
229        dispatch!(self, effective_row_stride)
230    }
231
232    /// Set logical dimensions + format to a decoded image, reusing the
233    /// allocation. See [`Tensor::configure_image`].
234    pub fn configure_image(
235        &mut self,
236        width: usize,
237        height: usize,
238        format: PixelFormat,
239    ) -> crate::Result<()> {
240        dispatch!(self, configure_image, width, height, format)
241    }
242
243    /// Set the row stride in bytes for externally allocated buffers with
244    /// row padding.
245    ///
246    /// Must be called before the tensor is first used for rendering. The
247    /// format must be set before calling this method.
248    pub fn set_row_stride(&mut self, stride: usize) -> crate::Result<()> {
249        dispatch!(self, set_row_stride, stride)
250    }
251
252    /// Builder-style: set row stride, consuming and returning self.
253    pub fn with_row_stride(mut self, stride: usize) -> crate::Result<Self> {
254        self.set_row_stride(stride)?;
255        Ok(self)
256    }
257
258    /// Byte offset within the DMA-BUF where image data starts (`None` = 0).
259    pub fn plane_offset(&self) -> Option<usize> {
260        dispatch!(self, plane_offset)
261    }
262
263    /// The parent-image snapshot if this tensor is a [`view`](Self::view)/
264    /// [`batch`](Self::batch) sub-region; `None` for a whole tensor. See
265    /// [`Tensor::view_origin`].
266    pub fn view_origin(&self) -> Option<crate::ViewOrigin> {
267        dispatch!(self, view_origin)
268    }
269
270    /// Set the byte offset within the DMA-BUF where image data starts.
271    pub fn set_plane_offset(&mut self, offset: usize) {
272        dispatch!(self, set_plane_offset, offset)
273    }
274
275    /// Borrow batch element `n` of a batched tensor (leading `N` dimension) as a
276    /// zero-copy view sharing this tensor's allocation. See [`Tensor::batch`].
277    pub fn batch(&self, n: usize) -> crate::Result<TensorDyn> {
278        dyn_fanout!(self, batch, n)
279    }
280
281    /// Borrow a rectangular spatial sub-region (the destination/source crop) as
282    /// a zero-copy view sharing this tensor's allocation. See [`Tensor::view`].
283    pub fn view(&self, region: crate::Region) -> crate::Result<TensorDyn> {
284        dyn_fanout!(self, view, region)
285    }
286
287    /// The CUDA registration for this tensor, if any.
288    ///
289    /// Returns `None` when no CUDA handle has been attached (the common non-CUDA case).
290    /// This check is a pure local field read — no thread routing occurs.
291    pub fn cuda(&self) -> Option<&crate::cuda::CudaHandle> {
292        dispatch!(self, cuda)
293    }
294
295    /// Fast-fail CUDA map: `None` when no handle is attached; else maps the
296    /// PBO through the GL worker and returns a scoped device-pointer guard.
297    ///
298    /// The same try-`cuda_map`-then-[`map`](crate::TensorTrait::map) fallback pattern that applies to
299    /// [`Tensor::cuda_map`](crate::Tensor::cuda_map) applies here: call `cuda_map()` first for a
300    /// zero-copy device pointer; when it returns `None` (no CUDA handle attached), fall back to the
301    /// typed host mapping via the inner tensor.
302    ///
303    /// # Example
304    ///
305    /// ```no_run
306    /// use edgefirst_tensor::TensorDyn;
307    /// # fn feed_tensorrt(_dptr: *mut std::ffi::c_void, _bytes: usize) {}
308    /// # fn demo(t: &TensorDyn) {
309    /// if let Some(cuda) = t.cuda_map() {
310    ///     feed_tensorrt(cuda.device_ptr(), cuda.len());
311    /// } else {
312    ///     // No CUDA handle — use the typed inner tensor for host access.
313    ///     // See Tensor::cuda_map for the full fallback example.
314    /// }
315    /// # }
316    /// ```
317    pub fn cuda_map(&self) -> Option<crate::cuda::CudaMap<'_>> {
318        dispatch!(self, cuda_map)
319    }
320
321    /// Quantization metadata. Returns `None` for float variants (F16, F32,
322    /// F64) — quantization does not apply to floating-point tensors.
323    /// Otherwise delegates to the typed `Tensor<T>::quantization()` accessor.
324    pub fn quantization(&self) -> Option<&crate::Quantization> {
325        match self {
326            Self::U8(t) => t.quantization(),
327            Self::I8(t) => t.quantization(),
328            Self::U16(t) => t.quantization(),
329            Self::I16(t) => t.quantization(),
330            Self::U32(t) => t.quantization(),
331            Self::I32(t) => t.quantization(),
332            Self::U64(t) => t.quantization(),
333            Self::I64(t) => t.quantization(),
334            Self::F16(_) | Self::F32(_) | Self::F64(_) => None,
335        }
336    }
337
338    /// Attach quantization metadata. Fails on float variants with
339    /// [`Error::QuantizationInvalid`]; delegates to the typed setter for
340    /// integer variants.
341    pub fn set_quantization(&mut self, q: crate::Quantization) -> crate::Result<()> {
342        match self {
343            Self::U8(t) => t.set_quantization(q),
344            Self::I8(t) => t.set_quantization(q),
345            Self::U16(t) => t.set_quantization(q),
346            Self::I16(t) => t.set_quantization(q),
347            Self::U32(t) => t.set_quantization(q),
348            Self::I32(t) => t.set_quantization(q),
349            Self::U64(t) => t.set_quantization(q),
350            Self::I64(t) => t.set_quantization(q),
351            Self::F16(_) | Self::F32(_) | Self::F64(_) => Err(crate::Error::QuantizationInvalid {
352                field: "dtype_is_integer",
353                expected: "integer tensor dtype (u8/i8/u16/i16/u32/i32/u64/i64)".to_string(),
354                got: format!("{:?}", self.dtype()),
355            }),
356        }
357    }
358
359    /// Builder-style variant of [`Self::set_quantization`]. Consumes self
360    /// and returns it with quantization applied (or the original error).
361    pub fn with_quantization(mut self, q: crate::Quantization) -> crate::Result<Self> {
362        self.set_quantization(q)?;
363        Ok(self)
364    }
365
366    /// Clear any quantization metadata. No-op on float variants.
367    pub fn clear_quantization(&mut self) {
368        match self {
369            Self::U8(t) => t.clear_quantization(),
370            Self::I8(t) => t.clear_quantization(),
371            Self::U16(t) => t.clear_quantization(),
372            Self::I16(t) => t.clear_quantization(),
373            Self::U32(t) => t.clear_quantization(),
374            Self::I32(t) => t.clear_quantization(),
375            Self::U64(t) => t.clear_quantization(),
376            Self::I64(t) => t.clear_quantization(),
377            Self::F16(_) | Self::F32(_) | Self::F64(_) => {}
378        }
379    }
380
381    /// Clone the file descriptor associated with this tensor.
382    #[cfg(unix)]
383    pub fn clone_fd(&self) -> crate::Result<std::os::fd::OwnedFd> {
384        dispatch!(self, clone_fd)
385    }
386
387    /// Clone the DMA-BUF file descriptor backing this tensor (Linux only).
388    ///
389    /// # Returns
390    ///
391    /// An owned duplicate of the DMA-BUF file descriptor.
392    ///
393    /// # Errors
394    ///
395    /// * `Error::NotImplemented` if the tensor is not DMA-backed (Mem/Shm/Pbo)
396    /// * `Error::IoError` if the fd clone syscall fails (e.g., fd limit reached)
397    #[cfg(target_os = "linux")]
398    pub fn dmabuf_clone(&self) -> crate::Result<std::os::fd::OwnedFd> {
399        if self.memory() != TensorMemory::Dma {
400            return Err(crate::Error::NotImplemented(format!(
401                "dmabuf_clone requires DMA-backed tensor, got {:?}",
402                self.memory()
403            )));
404        }
405        self.clone_fd()
406    }
407
408    /// Borrow the DMA-BUF file descriptor backing this tensor (Linux only).
409    ///
410    /// # Returns
411    ///
412    /// A borrowed reference to the DMA-BUF file descriptor, tied to `self`'s
413    /// lifetime.
414    ///
415    /// # Errors
416    ///
417    /// * `Error::NotImplemented` if the tensor is not DMA-backed
418    #[cfg(target_os = "linux")]
419    pub fn dmabuf(&self) -> crate::Result<std::os::fd::BorrowedFd<'_>> {
420        dispatch!(self, dmabuf)
421    }
422
423    /// Return `true` if this tensor uses separate plane allocations.
424    pub fn is_multiplane(&self) -> bool {
425        dispatch!(self, is_multiplane)
426    }
427
428    /// Return the [`BufferIdentity`](crate::BufferIdentity) of the underlying
429    /// allocation.
430    ///
431    /// Two `TensorDyn` values share a [`BufferIdentity::id`] iff they were
432    /// produced by cloning the same allocation (e.g. through
433    /// [`DmaTensor::try_clone`](crate::dma::DmaTensor::try_clone)). Separate
434    /// imports of the same physical buffer (e.g. two `from_fd` calls on the
435    /// same dmabuf fd) have **distinct** identities — use
436    /// [`aliases`](Self::aliases) if you need to detect that case.
437    pub fn buffer_identity(&self) -> &crate::BufferIdentity {
438        dispatch!(self, buffer_identity)
439    }
440
441    /// Return `true` if `self` and `other` reference the same underlying
442    /// buffer.
443    ///
444    /// This is the correct check for APIs that require distinct input and
445    /// output tensors (e.g. `ImageProcessor::draw_decoded_masks`, where
446    /// aliasing `dst` and `background` would cause the GL backend to read
447    /// and write the same texture — undefined behaviour on most drivers).
448    ///
449    /// Matching is conservative:
450    /// 1. Matching [`BufferIdentity::id`] → same buffer (always).
451    /// 2. Matching backing type + matching dmabuf fd number (Linux, DMA
452    ///    tensors only) → same buffer, even across separate `from_fd`
453    ///    imports in the same process.
454    ///
455    /// Two distinct `dup`'d fds pointing at the same kernel dma-buf are
456    /// **not** detected — there is no cheap way to resolve that without a
457    /// round-trip through the kernel.
458    pub fn aliases(&self, other: &Self) -> bool {
459        if self.buffer_identity().id() == other.buffer_identity().id() {
460            return true;
461        }
462        if self.memory() != other.memory() {
463            return false;
464        }
465        #[cfg(target_os = "linux")]
466        if self.memory() == TensorMemory::Dma {
467            use std::os::fd::AsRawFd;
468            if let (Ok(a), Ok(b)) = (self.dmabuf(), other.dmabuf()) {
469                return a.as_raw_fd() == b.as_raw_fd();
470            }
471        }
472        false
473    }
474
475    // --- Downcasting ---
476
477    downcast_methods!(U8, u8, as_u8, as_u8_mut, into_u8);
478    downcast_methods!(I8, i8, as_i8, as_i8_mut, into_i8);
479    downcast_methods!(U16, u16, as_u16, as_u16_mut, into_u16);
480    downcast_methods!(I16, i16, as_i16, as_i16_mut, into_i16);
481    downcast_methods!(U32, u32, as_u32, as_u32_mut, into_u32);
482    downcast_methods!(I32, i32, as_i32, as_i32_mut, into_i32);
483    downcast_methods!(U64, u64, as_u64, as_u64_mut, into_u64);
484    downcast_methods!(I64, i64, as_i64, as_i64_mut, into_i64);
485    downcast_methods!(F16, f16, as_f16, as_f16_mut, into_f16);
486    downcast_methods!(F32, f32, as_f32, as_f32_mut, into_f32);
487    downcast_methods!(F64, f64, as_f64, as_f64_mut, into_f64);
488
489    /// Create a type-erased tensor with the given shape and element type.
490    pub fn new(
491        shape: &[usize],
492        dtype: DType,
493        memory: Option<TensorMemory>,
494        name: Option<&str>,
495    ) -> crate::Result<Self> {
496        match dtype {
497            DType::U8 => Tensor::<u8>::new(shape, memory, name).map(Self::U8),
498            DType::I8 => Tensor::<i8>::new(shape, memory, name).map(Self::I8),
499            DType::U16 => Tensor::<u16>::new(shape, memory, name).map(Self::U16),
500            DType::I16 => Tensor::<i16>::new(shape, memory, name).map(Self::I16),
501            DType::U32 => Tensor::<u32>::new(shape, memory, name).map(Self::U32),
502            DType::I32 => Tensor::<i32>::new(shape, memory, name).map(Self::I32),
503            DType::U64 => Tensor::<u64>::new(shape, memory, name).map(Self::U64),
504            DType::I64 => Tensor::<i64>::new(shape, memory, name).map(Self::I64),
505            DType::F16 => Tensor::<f16>::new(shape, memory, name).map(Self::F16),
506            DType::F32 => Tensor::<f32>::new(shape, memory, name).map(Self::F32),
507            DType::F64 => Tensor::<f64>::new(shape, memory, name).map(Self::F64),
508        }
509    }
510
511    /// Create a type-erased tensor from a file descriptor.
512    #[cfg(unix)]
513    pub fn from_fd(
514        fd: std::os::fd::OwnedFd,
515        shape: &[usize],
516        dtype: DType,
517        name: Option<&str>,
518    ) -> crate::Result<Self> {
519        match dtype {
520            DType::U8 => Tensor::<u8>::from_fd(fd, shape, name).map(Self::U8),
521            DType::I8 => Tensor::<i8>::from_fd(fd, shape, name).map(Self::I8),
522            DType::U16 => Tensor::<u16>::from_fd(fd, shape, name).map(Self::U16),
523            DType::I16 => Tensor::<i16>::from_fd(fd, shape, name).map(Self::I16),
524            DType::U32 => Tensor::<u32>::from_fd(fd, shape, name).map(Self::U32),
525            DType::I32 => Tensor::<i32>::from_fd(fd, shape, name).map(Self::I32),
526            DType::U64 => Tensor::<u64>::from_fd(fd, shape, name).map(Self::U64),
527            DType::I64 => Tensor::<i64>::from_fd(fd, shape, name).map(Self::I64),
528            DType::F16 => Tensor::<f16>::from_fd(fd, shape, name).map(Self::F16),
529            DType::F32 => Tensor::<f32>::from_fd(fd, shape, name).map(Self::F32),
530            DType::F64 => Tensor::<f64>::from_fd(fd, shape, name).map(Self::F64),
531        }
532    }
533
534    /// Wrap externally-owned memory as a type-erased tensor without copying.
535    /// The tensor borrows `[ptr, ptr + shape.product() * dtype.size())` as
536    /// [`TensorMemory::Mem`]; `owner`, when `Some`, co-owns the source so it
537    /// outlives the tensor (and all derived views/maps). See
538    /// [`crate::ForeignOwner`] and [`Tensor::from_foreign`].
539    ///
540    /// # Safety
541    ///
542    /// `ptr` must be non-null, aligned to the element type, and valid for
543    /// `shape.product()` elements of `dtype` for as long as the returned
544    /// tensor — and every view/map sharing its backing — is alive. Pass an
545    /// `owner` that co-owns the source to uphold that contract.
546    pub unsafe fn from_foreign_ptr(
547        ptr: *mut u8,
548        shape: &[usize],
549        dtype: DType,
550        owner: Option<crate::ForeignOwner>,
551        name: Option<&str>,
552    ) -> crate::Result<Self> {
553        match dtype {
554            DType::U8 => Tensor::<u8>::from_foreign(ptr.cast(), shape, owner, name).map(Self::U8),
555            DType::I8 => Tensor::<i8>::from_foreign(ptr.cast(), shape, owner, name).map(Self::I8),
556            DType::U16 => Tensor::<u16>::from_foreign(ptr.cast(), shape, owner, name).map(Self::U16),
557            DType::I16 => Tensor::<i16>::from_foreign(ptr.cast(), shape, owner, name).map(Self::I16),
558            DType::U32 => Tensor::<u32>::from_foreign(ptr.cast(), shape, owner, name).map(Self::U32),
559            DType::I32 => Tensor::<i32>::from_foreign(ptr.cast(), shape, owner, name).map(Self::I32),
560            DType::U64 => Tensor::<u64>::from_foreign(ptr.cast(), shape, owner, name).map(Self::U64),
561            DType::I64 => Tensor::<i64>::from_foreign(ptr.cast(), shape, owner, name).map(Self::I64),
562            DType::F16 => Tensor::<f16>::from_foreign(ptr.cast(), shape, owner, name).map(Self::F16),
563            DType::F32 => Tensor::<f32>::from_foreign(ptr.cast(), shape, owner, name).map(Self::F32),
564            DType::F64 => Tensor::<f64>::from_foreign(ptr.cast(), shape, owner, name).map(Self::F64),
565        }
566    }
567
568    /// Wrap an externally-allocated IOSurface as a type-erased tensor
569    /// (macOS only).
570    ///
571    /// # Safety
572    ///
573    /// `surface_ref` must be a valid live `IOSurfaceRef`. `shape` must
574    /// match the IOSurface's pixel dimensions and chosen element type.
575    #[cfg(target_os = "macos")]
576    pub unsafe fn from_iosurface(
577        surface_ref: *mut std::ffi::c_void,
578        shape: &[usize],
579        dtype: DType,
580        name: Option<&str>,
581    ) -> crate::Result<Self> {
582        unsafe {
583            match dtype {
584                DType::U8 => Tensor::<u8>::from_iosurface(surface_ref, shape, name).map(Self::U8),
585                DType::I8 => Tensor::<i8>::from_iosurface(surface_ref, shape, name).map(Self::I8),
586                DType::U16 => {
587                    Tensor::<u16>::from_iosurface(surface_ref, shape, name).map(Self::U16)
588                }
589                DType::I16 => {
590                    Tensor::<i16>::from_iosurface(surface_ref, shape, name).map(Self::I16)
591                }
592                DType::U32 => {
593                    Tensor::<u32>::from_iosurface(surface_ref, shape, name).map(Self::U32)
594                }
595                DType::I32 => {
596                    Tensor::<i32>::from_iosurface(surface_ref, shape, name).map(Self::I32)
597                }
598                DType::U64 => {
599                    Tensor::<u64>::from_iosurface(surface_ref, shape, name).map(Self::U64)
600                }
601                DType::I64 => {
602                    Tensor::<i64>::from_iosurface(surface_ref, shape, name).map(Self::I64)
603                }
604                DType::F16 => {
605                    Tensor::<f16>::from_iosurface(surface_ref, shape, name).map(Self::F16)
606                }
607                DType::F32 => {
608                    Tensor::<f32>::from_iosurface(surface_ref, shape, name).map(Self::F32)
609                }
610                DType::F64 => {
611                    Tensor::<f64>::from_iosurface(surface_ref, shape, name).map(Self::F64)
612                }
613            }
614        }
615    }
616
617    /// IOSurfaceID for cross-process surface sharing (macOS only).
618    /// Returns `None` when the tensor is not IOSurface-backed.
619    #[cfg(target_os = "macos")]
620    pub fn iosurface_id(&self) -> Option<u32> {
621        dispatch!(self, iosurface_id)
622    }
623
624    /// Borrow the raw `IOSurfaceRef` backing this tensor (macOS only).
625    /// Returns `None` when the tensor is not IOSurface-backed. The
626    /// pointer's lifetime is tied to `self`.
627    #[cfg(target_os = "macos")]
628    pub fn iosurface_ref(&self) -> Option<*mut std::ffi::c_void> {
629        dispatch!(self, iosurface_ref)
630    }
631
632    /// Physical IOSurface dimensions in texels, independent of the logical
633    /// shape (macOS only). `None` when not IOSurface-backed. The GL backend
634    /// binds the EGL pbuffer at these dims so one cached pbuffer serves every
635    /// frame size a reused pool surface holds.
636    #[cfg(target_os = "macos")]
637    pub fn iosurface_physical_dims(&self) -> Option<(usize, usize)> {
638        dispatch!(self, iosurface_physical_dims)
639    }
640
641    /// Create a type-erased image tensor.
642    ///
643    /// # Arguments
644    ///
645    /// * `width` - Image width in pixels
646    /// * `height` - Image height in pixels
647    /// * `format` - Pixel format
648    /// * `dtype` - Element type discriminant
649    /// * `memory` - Optional memory backend (None selects the best available)
650    ///
651    /// # Returns
652    ///
653    /// A new `TensorDyn` wrapping an image tensor of the requested element type.
654    ///
655    /// # Errors
656    ///
657    /// Returns an error if the underlying `Tensor::image` call fails.
658    pub fn image(
659        width: usize,
660        height: usize,
661        format: PixelFormat,
662        dtype: DType,
663        memory: Option<TensorMemory>,
664    ) -> crate::Result<Self> {
665        match dtype {
666            DType::U8 => Tensor::<u8>::image(width, height, format, memory).map(Self::U8),
667            DType::I8 => Tensor::<i8>::image(width, height, format, memory).map(Self::I8),
668            DType::U16 => Tensor::<u16>::image(width, height, format, memory).map(Self::U16),
669            DType::I16 => Tensor::<i16>::image(width, height, format, memory).map(Self::I16),
670            DType::U32 => Tensor::<u32>::image(width, height, format, memory).map(Self::U32),
671            DType::I32 => Tensor::<i32>::image(width, height, format, memory).map(Self::I32),
672            DType::U64 => Tensor::<u64>::image(width, height, format, memory).map(Self::U64),
673            DType::I64 => Tensor::<i64>::image(width, height, format, memory).map(Self::I64),
674            DType::F16 => Tensor::<f16>::image(width, height, format, memory).map(Self::F16),
675            DType::F32 => Tensor::<f32>::image(width, height, format, memory).map(Self::F32),
676            DType::F64 => Tensor::<f64>::image(width, height, format, memory).map(Self::F64),
677        }
678    }
679
680    /// Create a DMA-backed image tensor with an explicit row stride that
681    /// may exceed the natural `width * channels * sizeof(T)` pitch.
682    ///
683    /// See [`Tensor::image_with_stride`] for the detailed contract and
684    /// constraints. The TensorDyn wrapper dispatches to the appropriate
685    /// monomorphised `Tensor<T>` based on `dtype`.
686    ///
687    /// # Example
688    ///
689    /// ```no_run
690    /// use edgefirst_tensor::{TensorDyn, PixelFormat, DType, TensorMemory};
691    /// # fn main() -> edgefirst_tensor::Result<()> {
692    /// // Allocate a 3004×1688 RGBA8 canvas with 64-byte pitch alignment
693    /// // (12032 bytes per row instead of the natural 12016).
694    /// let img = TensorDyn::image_with_stride(
695    ///     3004, 1688,
696    ///     PixelFormat::Rgba, DType::U8,
697    ///     12032,
698    ///     Some(TensorMemory::Dma),
699    /// )?;
700    /// assert_eq!(img.width(), Some(3004));       // logical, unchanged
701    /// assert_eq!(img.effective_row_stride(), Some(12032)); // padded
702    /// # Ok(())
703    /// # }
704    /// ```
705    pub fn image_with_stride(
706        width: usize,
707        height: usize,
708        format: PixelFormat,
709        dtype: DType,
710        row_stride_bytes: usize,
711        memory: Option<TensorMemory>,
712    ) -> crate::Result<Self> {
713        match dtype {
714            DType::U8 => {
715                Tensor::<u8>::image_with_stride(width, height, format, row_stride_bytes, memory)
716                    .map(Self::U8)
717            }
718            DType::I8 => {
719                Tensor::<i8>::image_with_stride(width, height, format, row_stride_bytes, memory)
720                    .map(Self::I8)
721            }
722            DType::U16 => {
723                Tensor::<u16>::image_with_stride(width, height, format, row_stride_bytes, memory)
724                    .map(Self::U16)
725            }
726            DType::I16 => {
727                Tensor::<i16>::image_with_stride(width, height, format, row_stride_bytes, memory)
728                    .map(Self::I16)
729            }
730            DType::U32 => {
731                Tensor::<u32>::image_with_stride(width, height, format, row_stride_bytes, memory)
732                    .map(Self::U32)
733            }
734            DType::I32 => {
735                Tensor::<i32>::image_with_stride(width, height, format, row_stride_bytes, memory)
736                    .map(Self::I32)
737            }
738            DType::U64 => {
739                Tensor::<u64>::image_with_stride(width, height, format, row_stride_bytes, memory)
740                    .map(Self::U64)
741            }
742            DType::I64 => {
743                Tensor::<i64>::image_with_stride(width, height, format, row_stride_bytes, memory)
744                    .map(Self::I64)
745            }
746            DType::F16 => {
747                Tensor::<f16>::image_with_stride(width, height, format, row_stride_bytes, memory)
748                    .map(Self::F16)
749            }
750            DType::F32 => {
751                Tensor::<f32>::image_with_stride(width, height, format, row_stride_bytes, memory)
752                    .map(Self::F32)
753            }
754            DType::F64 => {
755                Tensor::<f64>::image_with_stride(width, height, format, row_stride_bytes, memory)
756                    .map(Self::F64)
757            }
758        }
759    }
760}
761
762// --- From impls ---
763
764impl From<Tensor<u8>> for TensorDyn {
765    fn from(t: Tensor<u8>) -> Self {
766        Self::U8(t)
767    }
768}
769
770impl From<Tensor<i8>> for TensorDyn {
771    fn from(t: Tensor<i8>) -> Self {
772        Self::I8(t)
773    }
774}
775
776impl From<Tensor<u16>> for TensorDyn {
777    fn from(t: Tensor<u16>) -> Self {
778        Self::U16(t)
779    }
780}
781
782impl From<Tensor<i16>> for TensorDyn {
783    fn from(t: Tensor<i16>) -> Self {
784        Self::I16(t)
785    }
786}
787
788impl From<Tensor<u32>> for TensorDyn {
789    fn from(t: Tensor<u32>) -> Self {
790        Self::U32(t)
791    }
792}
793
794impl From<Tensor<i32>> for TensorDyn {
795    fn from(t: Tensor<i32>) -> Self {
796        Self::I32(t)
797    }
798}
799
800impl From<Tensor<u64>> for TensorDyn {
801    fn from(t: Tensor<u64>) -> Self {
802        Self::U64(t)
803    }
804}
805
806impl From<Tensor<i64>> for TensorDyn {
807    fn from(t: Tensor<i64>) -> Self {
808        Self::I64(t)
809    }
810}
811
812impl From<Tensor<f16>> for TensorDyn {
813    fn from(t: Tensor<f16>) -> Self {
814        Self::F16(t)
815    }
816}
817
818impl From<Tensor<f32>> for TensorDyn {
819    fn from(t: Tensor<f32>) -> Self {
820        Self::F32(t)
821    }
822}
823
824impl From<Tensor<f64>> for TensorDyn {
825    fn from(t: Tensor<f64>) -> Self {
826        Self::F64(t)
827    }
828}
829
830impl fmt::Debug for TensorDyn {
831    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
832        dispatch!(self, fmt, f)
833    }
834}
835
836#[cfg(test)]
837mod tests {
838    use super::*;
839
840    #[test]
841    fn from_typed_tensor() {
842        let t = Tensor::<u8>::new(&[10], None, None).unwrap();
843        let dyn_t: TensorDyn = t.into();
844        assert_eq!(dyn_t.dtype(), DType::U8);
845        assert_eq!(dyn_t.shape(), &[10]);
846    }
847
848    #[test]
849    fn from_foreign_ptr_wraps_borrowed_memory() {
850        use crate::TensorMapTrait;
851        // The CUDA zero-copy export shape: wrap an externally-allocated buffer as
852        // a type-erased Mem tensor, with an owner that frees it on last drop.
853        let mut vec: Vec<f32> = vec![0.0; 4];
854        let ptr = vec.as_mut_ptr() as *mut u8;
855        let owner: crate::ForeignOwner = Box::new(vec);
856        let t = unsafe {
857            TensorDyn::from_foreign_ptr(ptr, &[2, 2], DType::F32, Some(owner), Some("trt_output"))
858        }
859        .unwrap();
860        assert_eq!(t.dtype(), DType::F32);
861        assert_eq!(t.memory(), TensorMemory::Mem);
862        assert_eq!(t.shape(), &[2, 2]);
863        {
864            let mut m = t.as_f32().unwrap().map().unwrap();
865            m.as_mut_slice().copy_from_slice(&[1.0, 2.0, 3.0, 4.0]);
866        }
867        let m = t.as_f32().unwrap().map().unwrap();
868        assert_eq!(m.as_slice(), &[1.0, 2.0, 3.0, 4.0]);
869    }
870
871    // -------------------------------------------------------------------------
872    // TensorDyn::from_foreign_ptr guard paths.
873    //
874    // The happy path (F32) is covered by `from_foreign_ptr_wraps_borrowed_memory`
875    // above. These cells add the null-ptr, empty-shape, and overflow guards, plus
876    // a U8 dtype to confirm the match-arm dispatch is exercised for integer types.
877    // -------------------------------------------------------------------------
878
879    #[test]
880    fn from_foreign_ptr_rejects_null_ptr() {
881        let err = unsafe {
882            TensorDyn::from_foreign_ptr(std::ptr::null_mut(), &[4], DType::U8, None, None)
883        }
884        .unwrap_err();
885        // The null guard fires inside Tensor<u8>::from_foreign.
886        assert!(
887            matches!(err, crate::error::Error::InvalidArgument(ref m) if m.contains("non-null")),
888            "expected InvalidArgument(non-null), got {err:?}"
889        );
890    }
891
892    #[test]
893    fn from_foreign_ptr_rejects_empty_shape() {
894        let mut dummy: u8 = 0;
895        let err = unsafe {
896            TensorDyn::from_foreign_ptr(&mut dummy as *mut u8, &[], DType::U8, None, None)
897        }
898        .unwrap_err();
899        assert!(
900            matches!(err, crate::error::Error::InvalidSize(0)),
901            "expected InvalidSize(0) for empty shape, got {err:?}"
902        );
903    }
904
905    #[test]
906    fn from_foreign_ptr_rejects_overflow_shape() {
907        let mut dummy: u8 = 0;
908        let huge = [usize::MAX / 2 + 1, 2];
909        let err = unsafe {
910            TensorDyn::from_foreign_ptr(&mut dummy, &huge, DType::U8, None, None)
911        }
912        .unwrap_err();
913        assert!(
914            matches!(err, crate::error::Error::InvalidArgument(ref m) if m.contains("overflow")),
915            "expected InvalidArgument(overflow), got {err:?}"
916        );
917    }
918
919    #[test]
920    fn from_foreign_ptr_u8_dtype_dispatch() {
921        // Exercises the U8 arm of from_foreign_ptr's match, which wraps
922        // the raw pointer as Tensor<u8> and downcasts correctly.
923        let mut buf: Vec<u8> = vec![1, 2, 3, 4];
924        let ptr = buf.as_mut_ptr();
925        let owner: crate::ForeignOwner = Box::new(buf);
926        let t = unsafe {
927            TensorDyn::from_foreign_ptr(ptr, &[4], DType::U8, Some(owner), Some("u8_foreign"))
928        }
929        .unwrap();
930        assert_eq!(t.dtype(), DType::U8);
931        assert_eq!(t.shape(), &[4]);
932        let m = t.as_u8().unwrap().map().unwrap();
933        use crate::TensorMapTrait;
934        assert_eq!(m.as_slice(), &[1u8, 2, 3, 4]);
935    }
936
937    #[test]
938    fn downcast_ref() {
939        let t = Tensor::<u8>::new(&[10], None, None).unwrap();
940        let dyn_t: TensorDyn = t.into();
941        assert!(dyn_t.as_u8().is_some());
942        assert!(dyn_t.as_i8().is_none());
943    }
944
945    #[test]
946    fn downcast_into() {
947        let t = Tensor::<u8>::new(&[10], None, None).unwrap();
948        let dyn_t: TensorDyn = t.into();
949        let back = dyn_t.into_u8().unwrap();
950        assert_eq!(back.shape(), &[10]);
951    }
952
953    #[test]
954    fn image_accessors() {
955        let t = Tensor::<u8>::image(640, 480, PixelFormat::Rgba, None).unwrap();
956        let dyn_t: TensorDyn = t.into();
957        assert_eq!(dyn_t.format(), Some(PixelFormat::Rgba));
958        assert_eq!(dyn_t.width(), Some(640));
959        assert_eq!(dyn_t.height(), Some(480));
960        assert!(!dyn_t.is_multiplane());
961    }
962
963    #[test]
964    fn image_constructor() {
965        let dyn_t = TensorDyn::image(640, 480, PixelFormat::Rgb, DType::U8, None).unwrap();
966        assert_eq!(dyn_t.dtype(), DType::U8);
967        assert_eq!(dyn_t.format(), Some(PixelFormat::Rgb));
968        assert_eq!(dyn_t.width(), Some(640));
969    }
970
971    #[test]
972    fn image_constructor_i8() {
973        let dyn_t = TensorDyn::image(640, 480, PixelFormat::Rgb, DType::I8, None).unwrap();
974        assert_eq!(dyn_t.dtype(), DType::I8);
975        assert_eq!(dyn_t.format(), Some(PixelFormat::Rgb));
976    }
977
978    #[test]
979    fn set_format_packed() {
980        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
981        assert_eq!(t.format(), None);
982        t.set_format(PixelFormat::Rgb).unwrap();
983        assert_eq!(t.format(), Some(PixelFormat::Rgb));
984        assert_eq!(t.width(), Some(640));
985        assert_eq!(t.height(), Some(480));
986    }
987
988    #[test]
989    fn set_format_planar() {
990        let mut t = TensorDyn::new(&[3, 480, 640], DType::U8, None, None).unwrap();
991        t.set_format(PixelFormat::PlanarRgb).unwrap();
992        assert_eq!(t.format(), Some(PixelFormat::PlanarRgb));
993        assert_eq!(t.width(), Some(640));
994        assert_eq!(t.height(), Some(480));
995    }
996
997    #[test]
998    fn set_format_rejects_wrong_shape() {
999        let mut t = TensorDyn::new(&[480, 640, 4], DType::U8, None, None).unwrap();
1000        assert!(t.set_format(PixelFormat::Rgb).is_err());
1001    }
1002
1003    #[test]
1004    fn with_format_builder() {
1005        let t = TensorDyn::new(&[480, 640, 4], DType::U8, None, None)
1006            .unwrap()
1007            .with_format(PixelFormat::Rgba)
1008            .unwrap();
1009        assert_eq!(t.format(), Some(PixelFormat::Rgba));
1010        assert_eq!(t.width(), Some(640));
1011        assert_eq!(t.height(), Some(480));
1012    }
1013
1014    #[cfg(target_os = "linux")]
1015    #[test]
1016    fn dmabuf_clone_mem_tensor_fails() {
1017        let t = TensorDyn::new(&[480, 640, 3], DType::U8, Some(TensorMemory::Mem), None).unwrap();
1018        assert_eq!(t.memory(), TensorMemory::Mem);
1019        assert!(t.dmabuf_clone().is_err());
1020    }
1021
1022    #[cfg(target_os = "linux")]
1023    #[test]
1024    fn dmabuf_mem_tensor_fails() {
1025        let t = TensorDyn::new(&[480, 640, 3], DType::U8, Some(TensorMemory::Mem), None).unwrap();
1026        assert!(t.dmabuf().is_err());
1027    }
1028
1029    #[test]
1030    fn set_format_semi_planar_nv12() {
1031        // 720 rows = 480 * 3/2 (NV12: height + height/2 for chroma)
1032        let mut t = TensorDyn::new(&[720, 640], DType::U8, Some(TensorMemory::Mem), None).unwrap();
1033        t.set_format(PixelFormat::Nv12).unwrap();
1034        assert_eq!(t.format(), Some(PixelFormat::Nv12));
1035        assert_eq!(t.width(), Some(640));
1036        assert_eq!(t.height(), Some(480));
1037    }
1038
1039    #[test]
1040    fn set_format_semi_planar_nv16() {
1041        // 960 rows = 480 * 2 (NV16: height + height for chroma)
1042        let mut t = TensorDyn::new(&[960, 640], DType::U8, Some(TensorMemory::Mem), None).unwrap();
1043        t.set_format(PixelFormat::Nv16).unwrap();
1044        assert_eq!(t.format(), Some(PixelFormat::Nv16));
1045        assert_eq!(t.width(), Some(640));
1046        assert_eq!(t.height(), Some(480));
1047    }
1048
1049    #[test]
1050    fn with_format_rejects_wrong_shape() {
1051        let result = TensorDyn::new(&[480, 640, 4], DType::U8, None, None)
1052            .unwrap()
1053            .with_format(PixelFormat::Rgb);
1054        assert!(result.is_err());
1055    }
1056
1057    #[test]
1058    fn set_format_preserved_after_rejection() {
1059        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1060        t.set_format(PixelFormat::Rgb).unwrap();
1061        assert_eq!(t.format(), Some(PixelFormat::Rgb));
1062
1063        // Rgba requires 4 channels, should fail on a 3-channel tensor
1064        assert!(t.set_format(PixelFormat::Rgba).is_err());
1065
1066        // Original format should be preserved
1067        assert_eq!(t.format(), Some(PixelFormat::Rgb));
1068    }
1069
1070    #[test]
1071    fn set_format_idempotent() {
1072        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1073        t.set_format(PixelFormat::Rgb).unwrap();
1074        t.set_format(PixelFormat::Rgb).unwrap();
1075        assert_eq!(t.format(), Some(PixelFormat::Rgb));
1076        assert_eq!(t.width(), Some(640));
1077        assert_eq!(t.height(), Some(480));
1078    }
1079
1080    // --- Row stride tests ---
1081
1082    #[test]
1083    fn set_row_stride_valid() {
1084        // RGBA 100px wide: min stride = 400, set 512
1085        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgba, DType::U8, None).unwrap();
1086        t.set_row_stride(512).unwrap();
1087        assert_eq!(t.row_stride(), Some(512));
1088        assert_eq!(t.effective_row_stride(), Some(512));
1089    }
1090
1091    #[test]
1092    fn set_row_stride_equals_min() {
1093        // RGB 100px: min stride = 300, set exactly 300
1094        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgb, DType::U8, None).unwrap();
1095        t.set_row_stride(300).unwrap();
1096        assert_eq!(t.row_stride(), Some(300));
1097    }
1098
1099    #[test]
1100    fn set_row_stride_too_small() {
1101        // RGBA 64px (a 64-aligned width: 64*4 = 256, already a multiple of 64)
1102        // carries no implicit stride. min stride = 256; setting 200 must error
1103        // and leave row_stride unset. (Non-64-aligned widths now record the
1104        // padded stride at allocation — see `Tensor::image`.)
1105        let mut t = TensorDyn::image(64, 100, PixelFormat::Rgba, DType::U8, None).unwrap();
1106        assert!(t.set_row_stride(200).is_err());
1107        assert_eq!(t.row_stride(), None);
1108    }
1109
1110    #[test]
1111    fn set_row_stride_zero() {
1112        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgb, DType::U8, None).unwrap();
1113        assert!(t.set_row_stride(0).is_err());
1114    }
1115
1116    #[test]
1117    fn set_row_stride_requires_format() {
1118        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1119        assert!(t.set_row_stride(2048).is_err());
1120    }
1121
1122    #[test]
1123    fn effective_row_stride_without_stride() {
1124        // A 64-aligned-width packed image carries no explicit stride; the
1125        // effective stride falls back to the computed tight pitch. (Width 64
1126        // RGB → 64*3 = 192, already a multiple of 64, so no padding is added.
1127        // Non-aligned widths now record the padded stride — see `Tensor::image`.)
1128        let t = TensorDyn::image(64, 100, PixelFormat::Rgb, DType::U8, None).unwrap();
1129        assert_eq!(t.row_stride(), None);
1130        assert_eq!(t.effective_row_stride(), Some(192)); // 64 * 3
1131    }
1132
1133    #[test]
1134    fn effective_row_stride_padded_packed_dma() {
1135        // A non-64-aligned packed width on a DMA buffer records the 64-aligned
1136        // stride so the EGLImage import is accepted by Mali/Vivante (RGB 100px:
1137        // 100*3 = 300 → padded to 320). This padding is DMA-specific — host-only
1138        // memory keeps the tight pitch — so skip when DMA is unavailable (e.g. CI
1139        // without dma_heap); the behaviour is also validated on-target.
1140        let t = match TensorDyn::image(
1141            100,
1142            100,
1143            PixelFormat::Rgb,
1144            DType::U8,
1145            Some(TensorMemory::Dma),
1146        ) {
1147            Ok(t) if t.memory() == TensorMemory::Dma => t,
1148            _ => return,
1149        };
1150        assert_eq!(t.row_stride(), Some(320));
1151        assert_eq!(t.effective_row_stride(), Some(320));
1152    }
1153
1154    #[test]
1155    fn effective_row_stride_no_format() {
1156        let t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1157        assert_eq!(t.effective_row_stride(), None);
1158    }
1159
1160    #[test]
1161    fn with_row_stride_builder() {
1162        let t = TensorDyn::image(100, 100, PixelFormat::Rgba, DType::U8, None)
1163            .unwrap()
1164            .with_row_stride(512)
1165            .unwrap();
1166        assert_eq!(t.row_stride(), Some(512));
1167        assert_eq!(t.effective_row_stride(), Some(512));
1168    }
1169
1170    #[test]
1171    fn with_row_stride_rejects_small() {
1172        let result = TensorDyn::image(100, 100, PixelFormat::Rgba, DType::U8, None)
1173            .unwrap()
1174            .with_row_stride(200);
1175        assert!(result.is_err());
1176    }
1177
1178    #[test]
1179    fn set_format_clears_row_stride() {
1180        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1181        t.set_format(PixelFormat::Rgb).unwrap();
1182        t.set_row_stride(2048).unwrap();
1183        assert_eq!(t.row_stride(), Some(2048));
1184
1185        // Incompatible format change (4-chan on 3-chan shape) fails — stride preserved
1186        let _ = t.set_format(PixelFormat::Bgra);
1187        assert_eq!(t.row_stride(), Some(2048));
1188
1189        // Re-set to same format — stride preserved
1190        t.set_format(PixelFormat::Rgb).unwrap();
1191        assert_eq!(t.row_stride(), Some(2048));
1192
1193        // Reshape clears format and stride
1194        t.reshape(&[480 * 640 * 3]).unwrap();
1195        assert_eq!(t.row_stride(), None);
1196        assert_eq!(t.format(), None);
1197    }
1198
1199    #[test]
1200    fn set_format_different_compatible_clears_stride() {
1201        // RGBA and BGRA are both 4-channel packed — switching between them
1202        // succeeds and must clear the stored stride.
1203        let mut t = TensorDyn::new(&[480, 640, 4], DType::U8, None, None).unwrap();
1204        t.set_format(PixelFormat::Rgba).unwrap();
1205        t.set_row_stride(4096).unwrap();
1206        assert_eq!(t.row_stride(), Some(4096));
1207
1208        // Successful format change to a different compatible format clears stride
1209        t.set_format(PixelFormat::Bgra).unwrap();
1210        assert_eq!(t.format(), Some(PixelFormat::Bgra));
1211        assert_eq!(t.row_stride(), None);
1212    }
1213
1214    #[test]
1215    fn set_format_same_preserves_stride() {
1216        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgb, DType::U8, None).unwrap();
1217        t.set_row_stride(512).unwrap();
1218        // Re-setting the same format should not clear stride
1219        t.set_format(PixelFormat::Rgb).unwrap();
1220        assert_eq!(t.row_stride(), Some(512));
1221    }
1222
1223    #[test]
1224    fn effective_row_stride_planar() {
1225        let t = TensorDyn::image(640, 480, PixelFormat::PlanarRgb, DType::U8, None).unwrap();
1226        assert_eq!(t.effective_row_stride(), Some(640)); // planar: width only
1227    }
1228
1229    #[test]
1230    fn effective_row_stride_nv12() {
1231        let t = TensorDyn::image(640, 480, PixelFormat::Nv12, DType::U8, None).unwrap();
1232        assert_eq!(t.effective_row_stride(), Some(640)); // semi-planar: width only
1233    }
1234
1235    #[test]
1236    fn map_rejects_strided_tensor() {
1237        let mut t =
1238            Tensor::<u8>::image(100, 100, PixelFormat::Rgba, Some(TensorMemory::Mem)).unwrap();
1239        // Map works before stride is set
1240        assert!(t.map().is_ok());
1241        // After setting stride, map should be rejected
1242        t.set_row_stride(512).unwrap();
1243        let err = t.map();
1244        assert!(err.is_err());
1245    }
1246
1247    // ── plane_offset tests ──────────────────────────────────────────
1248
1249    #[test]
1250    fn plane_offset_default_none() {
1251        let t = TensorDyn::image(100, 100, PixelFormat::Rgba, DType::U8, None).unwrap();
1252        assert_eq!(t.plane_offset(), None);
1253    }
1254
1255    #[test]
1256    fn set_plane_offset_basic() {
1257        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgba, DType::U8, None).unwrap();
1258        t.set_plane_offset(4096);
1259        assert_eq!(t.plane_offset(), Some(4096));
1260    }
1261
1262    #[test]
1263    fn set_plane_offset_zero() {
1264        let mut t = TensorDyn::image(100, 100, PixelFormat::Rgb, DType::U8, None).unwrap();
1265        t.set_plane_offset(0);
1266        assert_eq!(t.plane_offset(), Some(0));
1267    }
1268
1269    #[test]
1270    fn set_plane_offset_no_format() {
1271        // plane_offset does not require format (it is format-independent)
1272        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1273        t.set_plane_offset(4096);
1274        assert_eq!(t.plane_offset(), Some(4096));
1275    }
1276
1277    #[test]
1278    fn set_format_clears_plane_offset() {
1279        let mut t = TensorDyn::new(&[480, 640, 3], DType::U8, None, None).unwrap();
1280        t.set_format(PixelFormat::Rgb).unwrap();
1281        t.set_plane_offset(4096);
1282        assert_eq!(t.plane_offset(), Some(4096));
1283
1284        // Re-set same format — offset preserved
1285        t.set_format(PixelFormat::Rgb).unwrap();
1286        assert_eq!(t.plane_offset(), Some(4096));
1287
1288        // Reshape clears everything
1289        t.reshape(&[480 * 640 * 3]).unwrap();
1290        assert_eq!(t.plane_offset(), None);
1291        assert_eq!(t.format(), None);
1292    }
1293
1294    #[test]
1295    fn map_rejects_out_of_bounds_offset() {
1296        let mut t =
1297            Tensor::<u8>::image(100, 100, PixelFormat::Rgba, Some(TensorMemory::Mem)).unwrap();
1298        // Map works before offset is set.
1299        assert!(t.map().is_ok());
1300        // Heap offsets are now honored, but an offset that pushes the full
1301        // logical window (40000 bytes) past the allocation must be rejected.
1302        t.set_plane_offset(4096);
1303        assert!(t.map().is_err());
1304    }
1305
1306    #[test]
1307    fn mem_subview_in_bounds_maps_at_offset() {
1308        // An in-bounds heap sub-view now maps at its offset (previously every
1309        // non-zero heap offset was rejected outright).
1310        let parent =
1311            Tensor::<u8>::image(100, 100, PixelFormat::Rgba, Some(TensorMemory::Mem)).unwrap();
1312        // A 10x10 RGBA window (400 bytes) at byte offset 4096 fits in 40000.
1313        let view = parent.subview(4096, &[10, 10, 4]).unwrap();
1314        assert_eq!(view.plane_offset(), Some(4096));
1315        assert!(view.map().is_ok());
1316    }
1317
1318    #[test]
1319    fn dyn_batch_dispatches_every_dtype() {
1320        // `TensorDyn::batch` fans out across all 11 dtype arms via `dyn_fanout!`;
1321        // exercise each so element `n` preserves the element type and shape.
1322        // A `[N=2, 4]` raw parent: element 1 is the contiguous 4-element window.
1323        use DType::*;
1324        for dt in [U8, I8, U16, I16, U32, I32, U64, I64, F16, F32, F64] {
1325            let parent = TensorDyn::new(&[2, 4], dt, Some(TensorMemory::Mem), None).unwrap();
1326            let view = parent.batch(1).unwrap();
1327            assert_eq!(view.dtype(), dt, "batch must preserve dtype {dt:?}");
1328            assert_eq!(view.shape(), &[4], "{dt:?}");
1329        }
1330    }
1331
1332    #[test]
1333    fn map_accepts_zero_offset_tensor() {
1334        let mut t =
1335            Tensor::<u8>::image(100, 100, PixelFormat::Rgba, Some(TensorMemory::Mem)).unwrap();
1336        t.set_plane_offset(0);
1337        // Zero offset is fine for CPU mapping
1338        assert!(t.map().is_ok());
1339    }
1340
1341    #[test]
1342    fn dyn_configure_image_nv12() {
1343        let mut t = TensorDyn::image(640, 480, PixelFormat::Rgb, DType::U8, None).unwrap();
1344        t.configure_image(320, 240, PixelFormat::Nv12).unwrap();
1345        assert_eq!(t.format(), Some(PixelFormat::Nv12));
1346        assert_eq!((t.width(), t.height()), (Some(320), Some(240)));
1347    }
1348
1349    #[test]
1350    fn tensordyn_colorimetry_roundtrip() {
1351        use crate::{ColorEncoding, Colorimetry, DType, PixelFormat};
1352        let mut t = TensorDyn::image(1280, 720, PixelFormat::Nv12, DType::U8, None).unwrap();
1353        assert_eq!(t.colorimetry(), None);
1354        let c = Colorimetry::default().with_encoding(ColorEncoding::Bt709);
1355        t.set_colorimetry(Some(c));
1356        assert_eq!(t.colorimetry(), Some(c));
1357    }
1358
1359    #[test]
1360    fn from_planes_propagates_plane_offset() {
1361        let mut luma =
1362            Tensor::<u8>::new(&[480, 640], Some(TensorMemory::Mem), Some("luma")).unwrap();
1363        luma.set_plane_offset(4096);
1364        let chroma =
1365            Tensor::<u8>::new(&[240, 640], Some(TensorMemory::Mem), Some("chroma")).unwrap();
1366        let combined = Tensor::<u8>::from_planes(luma, chroma, PixelFormat::Nv12).unwrap();
1367        assert_eq!(combined.plane_offset(), Some(4096));
1368    }
1369
1370    #[test]
1371    fn cuda_passthrough_none_for_mem_tensor() {
1372        // Build a Mem-backed dynamic tensor the same way the other tests here do,
1373        // then confirm the CUDA accessors pass through to None (no handle).
1374        let t: TensorDyn = Tensor::<f32>::new(&[10], Some(TensorMemory::Mem), None)
1375            .unwrap()
1376            .into();
1377        assert!(t.cuda().is_none());
1378        assert!(t.cuda_map().is_none());
1379    }
1380}