1use std::sync::Arc;
28
29use iced::widget::image;
30
31pub mod engines;
33pub use engines::{Engine, PageType, PixelFormat, ViewId};
34
35mod webview;
36pub use basic::{Action, WebView};
37pub use webview::{advanced, basic};
38
39#[cfg(feature = "blitz")]
40pub use engines::blitz::Blitz;
41
42#[cfg(feature = "litehtml")]
43pub use engines::litehtml::Litehtml;
44
45#[cfg(feature = "servo")]
46pub use engines::servo::Servo;
47
48#[cfg(feature = "cef")]
49pub use engines::cef_engine::{cef_subprocess_check, Cef};
50
51pub(crate) mod util;
52
53#[cfg(any(feature = "litehtml", feature = "blitz"))]
54pub(crate) mod fetch;
55
56#[derive(Clone, Debug)]
58pub struct ImageInfo {
59 width: u32,
60 height: u32,
61 handle: image::Handle,
62 raw_pixels: Arc<Vec<u8>>,
63}
64
65impl Default for ImageInfo {
66 fn default() -> Self {
67 Self::blank(Self::WIDTH, Self::HEIGHT)
68 }
69}
70
71impl ImageInfo {
72 const WIDTH: u32 = 800;
74 const HEIGHT: u32 = 800;
75
76 #[allow(dead_code)]
77 fn new(mut pixels: Vec<u8>, format: PixelFormat, width: u32, height: u32) -> Self {
78 assert_eq!(pixels.len() % 4, 0);
80
81 if let PixelFormat::Bgra = format {
82 pixels.chunks_mut(4).for_each(|chunk| chunk.swap(0, 2));
83 }
84
85 let raw_pixels = Arc::new(pixels);
86 Self {
87 width,
88 height,
89 handle: image::Handle::from_rgba(width, height, (*raw_pixels).clone()),
90 raw_pixels,
91 }
92 }
93
94 #[allow(dead_code)]
98 pub(crate) fn from_shader_pixels(pixels: Vec<u8>, width: u32, height: u32) -> Self {
99 debug_assert_eq!(pixels.len() % 4, 0);
100 Self {
101 width,
102 height,
103 handle: image::Handle::from_rgba(1, 1, vec![0u8; 4]),
105 raw_pixels: Arc::new(pixels),
106 }
107 }
108
109 pub fn as_handle(&self) -> image::Handle {
111 self.handle.clone()
112 }
113
114 pub fn image_width(&self) -> u32 {
116 self.width
117 }
118
119 pub fn image_height(&self) -> u32 {
121 self.height
122 }
123
124 pub fn pixels(&self) -> Arc<Vec<u8>> {
126 Arc::clone(&self.raw_pixels)
127 }
128
129 fn blank(width: u32, height: u32) -> Self {
130 let (w, h) = (width as usize)
131 .checked_mul(height as usize)
132 .and_then(|n| n.checked_mul(4))
133 .map_or((1u32, 1u32), |_| (width, height));
134
135 let pixels = vec![255; (w as usize * h as usize) * 4];
136 let raw_pixels = Arc::new(pixels.clone());
137 Self {
138 width: w,
139 height: h,
140 handle: image::Handle::from_rgba(w, h, pixels),
141 raw_pixels,
142 }
143 }
144}