Skip to main content

align_width_for_gpu_pitch

Function align_width_for_gpu_pitch 

Source
pub fn align_width_for_gpu_pitch(width: usize, bpp: usize) -> usize
Expand description

Round width (in pixels) up so the resulting row stride width * bpp is a multiple of GPU_DMA_BUF_PITCH_ALIGNMENT_BYTES AND a multiple of bpp (so the rounded width is an integer pixel count).

bpp must be the per-pixel byte count for the image’s primary plane (e.g. 4 for RGBA8/BGRA8, 3 for RGB888, 1 for Grey/NV12-luma).

External callers — GStreamer plugins, video pipelines, anyone wrapping a foreign DMA-BUF — should call this when sizing the destination so that eglCreateImageKHR doesn’t reject the import on Mali. Pre-aligned widths (640, 1280, 1920, 3008, 3840 …) round-trip unchanged; misaligned widths are bumped up to the next valid value.

§Overflow behaviour

All arithmetic is checked. If the alignment computation or the rounded width would overflow usize, the function logs a warning and returns the original width unchanged rather than wrapping or producing a smaller value. Callers can rely on the returned width being at least the requested width.

bpp == 0 and width == 0 short-circuit to return the input unchanged.

§Examples

use edgefirst_image::align_width_for_gpu_pitch;

// RGBA8 (bpp=4): width must round to a multiple of 16 pixels (64-byte stride).
assert_eq!(align_width_for_gpu_pitch(1920, 4), 1920); // already aligned
assert_eq!(align_width_for_gpu_pitch(3004, 4), 3008); // crowd.png case: +4 px
assert_eq!(align_width_for_gpu_pitch(1281, 4), 1296); // +15 px

// RGB888 (bpp=3): width must round to a multiple of 64 pixels (192-byte stride).
assert_eq!(align_width_for_gpu_pitch(640, 3), 640);
assert_eq!(align_width_for_gpu_pitch(641, 3), 704);