use crate::image_pipeline::ObjectFit;
use crate::paint::ExternalTextureId;
use crate::solver::Region;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum VideoPixelFormat {
Nv12,
I420,
Rgba8,
Bgra8,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct VideoSurfaceSpec {
pub texture_id: Option<ExternalTextureId>,
pub video_width: u32,
pub video_height: u32,
pub format: VideoPixelFormat,
pub fit: ObjectFit,
pub rounded: u8,
pub opacity: u8,
}
impl VideoSurfaceSpec {
#[inline(always)]
pub const fn new(video_width: u32, video_height: u32, format: VideoPixelFormat) -> Self {
Self {
texture_id: None,
video_width,
video_height,
format,
fit: ObjectFit::Contain,
rounded: 8,
opacity: 255,
}
}
#[inline(always)]
pub const fn external_texture(mut self, id: ExternalTextureId) -> Self {
self.texture_id = Some(id);
self
}
#[inline(always)]
pub const fn fit(mut self, fit: ObjectFit) -> Self {
self.fit = fit;
self
}
#[inline(always)]
pub const fn rounded(mut self, r: u8) -> Self {
self.rounded = r;
self
}
}
#[inline(always)]
fn yuv_to_rgb_bt709(y: u8, u: u8, v: u8) -> (u8, u8, u8) {
let c = (y as i32 - 16).max(0);
let d = u as i32 - 128;
let e = v as i32 - 128;
let r = (298 * c + 459 * e + 128) >> 8;
let g = (298 * c - 55 * d - 136 * e + 128) >> 8;
let b = (298 * c + 541 * d + 128) >> 8;
(
r.clamp(0, 255) as u8,
g.clamp(0, 255) as u8,
b.clamp(0, 255) as u8,
)
}
pub fn convert_nv12_to_rgba8(
y_plane: &[u8],
uv_plane: &[u8],
width: usize,
height: usize,
out_rgba: &mut [u8],
) -> usize {
let pixels = width.saturating_mul(height);
let uv_w = (width + 1) / 2;
let uv_h = (height + 1) / 2;
if pixels == 0
|| y_plane.len() < pixels
|| uv_plane.len() < uv_w.saturating_mul(uv_h).saturating_mul(2)
|| out_rgba.len() < pixels.saturating_mul(4)
{
return 0;
}
for row in 0..height {
let y_row = row * width;
let uv_row = (row >> 1) * uv_w * 2;
let out_row = row * width * 4;
for col in 0..width {
let y = y_plane[y_row + col];
let uv_idx = uv_row + (col >> 1) * 2;
let u = uv_plane[uv_idx];
let v = uv_plane[uv_idx + 1];
let (r, g, b) = yuv_to_rgb_bt709(y, u, v);
let o = out_row + col * 4;
out_rgba[o] = r;
out_rgba[o + 1] = g;
out_rgba[o + 2] = b;
out_rgba[o + 3] = 255;
}
}
pixels
}
pub fn convert_i420_to_rgba8(
y_plane: &[u8],
u_plane: &[u8],
v_plane: &[u8],
width: usize,
height: usize,
out_rgba: &mut [u8],
) -> usize {
let pixels = width.saturating_mul(height);
let cw = (width + 1) / 2;
let ch = (height + 1) / 2;
let c_len = cw.saturating_mul(ch);
if pixels == 0
|| y_plane.len() < pixels
|| u_plane.len() < c_len
|| v_plane.len() < c_len
|| out_rgba.len() < pixels.saturating_mul(4)
{
return 0;
}
for row in 0..height {
let y_row = row * width;
let c_row = (row >> 1) * cw;
let out_row = row * width * 4;
for col in 0..width {
let y = y_plane[y_row + col];
let c_idx = c_row + (col >> 1);
let (r, g, b) = yuv_to_rgb_bt709(y, u_plane[c_idx], v_plane[c_idx]);
let o = out_row + col * 4;
out_rgba[o] = r;
out_rgba[o + 1] = g;
out_rgba[o + 2] = b;
out_rgba[o + 3] = 255;
}
}
pixels
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct WebViewHostRegion {
pub id: u32,
pub logical_bounds: Region,
pub clip_bounds: Region,
pub visible: bool,
pub z_order: i16,
}
impl WebViewHostRegion {
#[inline]
pub fn effective_bounds(&self) -> Region {
if !self.visible {
return Region::EMPTY;
}
self.logical_bounds.intersect(self.clip_bounds)
}
#[inline]
pub fn physical_rect(&self, scale_factor: f32) -> (i32, i32, u32, u32) {
let eff = self.effective_bounds();
if eff.is_empty() {
return (0, 0, 0, 0);
}
let sf = scale_factor.clamp(0.5, 4.0);
let x = ((eff.x as f32) * sf) as i32;
let y = ((eff.y as f32) * sf) as i32;
let w = ((eff.w.max(0) as f32) * sf) as u32;
let h = ((eff.h.max(0) as f32) * sf) as u32;
(x, y, w, h)
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct WebViewHostUpdate {
pub id: u32,
pub physical_rect: (i32, i32, u32, u32),
pub visible: bool,
pub z_order: i16,
}
#[derive(Clone, Debug, Default)]
pub struct WebViewHostSync {
last: [Option<WebViewHostUpdate>; 8],
}
impl WebViewHostSync {
#[inline(always)]
pub const fn new() -> Self {
Self { last: [None; 8] }
}
pub fn diff_region(
&mut self,
region: WebViewHostRegion,
scale_factor: f32,
) -> Option<WebViewHostUpdate> {
let phys = region.physical_rect(scale_factor);
let vis = region.visible && phys.2 > 0 && phys.3 > 0;
let next = WebViewHostUpdate {
id: region.id,
physical_rect: phys,
visible: vis,
z_order: region.z_order,
};
for slot in &mut self.last {
if let Some(prev) = slot {
if prev.id == region.id {
if *prev == next {
return None;
}
*prev = next;
return Some(next);
}
}
}
for slot in &mut self.last {
if slot.is_none() {
*slot = Some(next);
return Some(next);
}
}
Some(next)
}
}