1use crate::image_pipeline::ObjectFit;
12use crate::paint::ExternalTextureId;
13use crate::solver::Region;
14
15#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
17pub enum VideoPixelFormat {
18 Nv12,
20 I420,
22 Rgba8,
24 Bgra8,
26}
27
28#[derive(Copy, Clone, PartialEq, Eq, Debug)]
30pub struct VideoSurfaceSpec {
31 pub texture_id: Option<ExternalTextureId>,
32 pub video_width: u32,
33 pub video_height: u32,
34 pub format: VideoPixelFormat,
35 pub fit: ObjectFit,
36 pub rounded: u8,
37 pub opacity: u8,
38}
39
40impl VideoSurfaceSpec {
41 #[inline(always)]
42 pub const fn new(video_width: u32, video_height: u32, format: VideoPixelFormat) -> Self {
43 Self {
44 texture_id: None,
45 video_width,
46 video_height,
47 format,
48 fit: ObjectFit::Contain,
49 rounded: 8,
50 opacity: 255,
51 }
52 }
53
54 #[inline(always)]
55 pub const fn external_texture(mut self, id: ExternalTextureId) -> Self {
56 self.texture_id = Some(id);
57 self
58 }
59
60 #[inline(always)]
61 pub const fn fit(mut self, fit: ObjectFit) -> Self {
62 self.fit = fit;
63 self
64 }
65
66 #[inline(always)]
67 pub const fn rounded(mut self, r: u8) -> Self {
68 self.rounded = r;
69 self
70 }
71}
72
73#[inline(always)]
74fn yuv_to_rgb_bt709(y: u8, u: u8, v: u8) -> (u8, u8, u8) {
75 let c = (y as i32 - 16).max(0);
76 let d = u as i32 - 128;
77 let e = v as i32 - 128;
78 let r = (298 * c + 459 * e + 128) >> 8;
79 let g = (298 * c - 55 * d - 136 * e + 128) >> 8;
80 let b = (298 * c + 541 * d + 128) >> 8;
81 (
82 r.clamp(0, 255) as u8,
83 g.clamp(0, 255) as u8,
84 b.clamp(0, 255) as u8,
85 )
86}
87
88pub fn convert_nv12_to_rgba8(
91 y_plane: &[u8],
92 uv_plane: &[u8],
93 width: usize,
94 height: usize,
95 out_rgba: &mut [u8],
96) -> usize {
97 let pixels = width.saturating_mul(height);
98 let uv_w = (width + 1) / 2;
99 let uv_h = (height + 1) / 2;
100 if pixels == 0
101 || y_plane.len() < pixels
102 || uv_plane.len() < uv_w.saturating_mul(uv_h).saturating_mul(2)
103 || out_rgba.len() < pixels.saturating_mul(4)
104 {
105 return 0;
106 }
107 for row in 0..height {
108 let y_row = row * width;
109 let uv_row = (row >> 1) * uv_w * 2;
110 let out_row = row * width * 4;
111 for col in 0..width {
112 let y = y_plane[y_row + col];
113 let uv_idx = uv_row + (col >> 1) * 2;
114 let u = uv_plane[uv_idx];
115 let v = uv_plane[uv_idx + 1];
116 let (r, g, b) = yuv_to_rgb_bt709(y, u, v);
117 let o = out_row + col * 4;
118 out_rgba[o] = r;
119 out_rgba[o + 1] = g;
120 out_rgba[o + 2] = b;
121 out_rgba[o + 3] = 255;
122 }
123 }
124 pixels
125}
126
127pub fn convert_i420_to_rgba8(
129 y_plane: &[u8],
130 u_plane: &[u8],
131 v_plane: &[u8],
132 width: usize,
133 height: usize,
134 out_rgba: &mut [u8],
135) -> usize {
136 let pixels = width.saturating_mul(height);
137 let cw = (width + 1) / 2;
138 let ch = (height + 1) / 2;
139 let c_len = cw.saturating_mul(ch);
140 if pixels == 0
141 || y_plane.len() < pixels
142 || u_plane.len() < c_len
143 || v_plane.len() < c_len
144 || out_rgba.len() < pixels.saturating_mul(4)
145 {
146 return 0;
147 }
148 for row in 0..height {
149 let y_row = row * width;
150 let c_row = (row >> 1) * cw;
151 let out_row = row * width * 4;
152 for col in 0..width {
153 let y = y_plane[y_row + col];
154 let c_idx = c_row + (col >> 1);
155 let (r, g, b) = yuv_to_rgb_bt709(y, u_plane[c_idx], v_plane[c_idx]);
156 let o = out_row + col * 4;
157 out_rgba[o] = r;
158 out_rgba[o + 1] = g;
159 out_rgba[o + 2] = b;
160 out_rgba[o + 3] = 255;
161 }
162 }
163 pixels
164}
165
166#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
168pub struct WebViewHostRegion {
169 pub id: u32,
170 pub logical_bounds: Region,
171 pub clip_bounds: Region,
172 pub visible: bool,
173 pub z_order: i16,
174}
175
176impl WebViewHostRegion {
177 #[inline]
179 pub fn effective_bounds(&self) -> Region {
180 if !self.visible {
181 return Region::EMPTY;
182 }
183 self.logical_bounds.intersect(self.clip_bounds)
184 }
185
186 #[inline]
188 pub fn physical_rect(&self, scale_factor: f32) -> (i32, i32, u32, u32) {
189 let eff = self.effective_bounds();
190 if eff.is_empty() {
191 return (0, 0, 0, 0);
192 }
193 let sf = scale_factor.clamp(0.5, 4.0);
194 let x = ((eff.x as f32) * sf) as i32;
195 let y = ((eff.y as f32) * sf) as i32;
196 let w = ((eff.w.max(0) as f32) * sf) as u32;
197 let h = ((eff.h.max(0) as f32) * sf) as u32;
198 (x, y, w, h)
199 }
200}
201
202#[derive(Copy, Clone, PartialEq, Eq, Debug)]
204pub struct WebViewHostUpdate {
205 pub id: u32,
206 pub physical_rect: (i32, i32, u32, u32),
207 pub visible: bool,
208 pub z_order: i16,
209}
210
211#[derive(Clone, Debug, Default)]
213pub struct WebViewHostSync {
214 last: [Option<WebViewHostUpdate>; 8],
215}
216
217impl WebViewHostSync {
218 #[inline(always)]
219 pub const fn new() -> Self {
220 Self { last: [None; 8] }
221 }
222
223 pub fn diff_region(
225 &mut self,
226 region: WebViewHostRegion,
227 scale_factor: f32,
228 ) -> Option<WebViewHostUpdate> {
229 let phys = region.physical_rect(scale_factor);
230 let vis = region.visible && phys.2 > 0 && phys.3 > 0;
231 let next = WebViewHostUpdate {
232 id: region.id,
233 physical_rect: phys,
234 visible: vis,
235 z_order: region.z_order,
236 };
237 for slot in &mut self.last {
238 if let Some(prev) = slot {
239 if prev.id == region.id {
240 if *prev == next {
241 return None;
242 }
243 *prev = next;
244 return Some(next);
245 }
246 }
247 }
248 for slot in &mut self.last {
249 if slot.is_none() {
250 *slot = Some(next);
251 return Some(next);
252 }
253 }
254 Some(next)
255 }
256}