Skip to main content

pixels/
snapshot.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::ops::{Bound, Deref, DerefMut, Range, RangeBounds};
6use std::sync::Arc;
7
8use euclid::default::{Rect, Size2D};
9use image::codecs::jpeg::JpegEncoder;
10use image::codecs::png::PngEncoder;
11use image::codecs::webp::WebPEncoder;
12use image::{ExtendedColorType, GenericImageView, ImageEncoder, ImageError, Rgb};
13use malloc_size_of_derive::MallocSizeOf;
14use serde::{Deserialize, Serialize};
15use servo_base::generic_channel::GenericSharedMemory;
16
17use crate::{EncodedImageType, Multiply, rgba8_get_rect, transform_inplace};
18
19#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
20pub enum SnapshotPixelFormat {
21    #[default]
22    RGBA,
23    BGRA,
24}
25
26#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
27pub enum Alpha {
28    Premultiplied,
29    NotPremultiplied,
30    /// This is used for opaque textures for which the presence of alpha in the
31    /// output data format does not matter.
32    DontCare,
33}
34
35impl Alpha {
36    pub const fn from_premultiplied(is_premultiplied: bool) -> Self {
37        if is_premultiplied {
38            Self::Premultiplied
39        } else {
40            Self::NotPremultiplied
41        }
42    }
43
44    pub const fn needs_alpha_multiplication(&self) -> bool {
45        match self {
46            Alpha::Premultiplied => false,
47            Alpha::NotPremultiplied => true,
48            Alpha::DontCare => false,
49        }
50    }
51}
52
53#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
54pub enum SnapshotAlphaMode {
55    /// Internal data is opaque (alpha is cleared to 1)
56    Opaque,
57    /// Internal data should be treated as opaque (does not mean it actually is)
58    AsOpaque { premultiplied: bool },
59    /// Data is not opaque
60    Transparent { premultiplied: bool },
61}
62
63impl Default for SnapshotAlphaMode {
64    fn default() -> Self {
65        Self::Transparent {
66            premultiplied: true,
67        }
68    }
69}
70
71impl SnapshotAlphaMode {
72    pub const fn alpha(&self) -> Alpha {
73        match self {
74            SnapshotAlphaMode::Opaque => Alpha::DontCare,
75            SnapshotAlphaMode::AsOpaque { premultiplied } => {
76                Alpha::from_premultiplied(*premultiplied)
77            },
78            SnapshotAlphaMode::Transparent { premultiplied } => {
79                Alpha::from_premultiplied(*premultiplied)
80            },
81        }
82    }
83}
84
85/// The data in a [`Snapshot`]. If created via shared memory, this will be
86/// the `SharedMemory` variant, but otherwise it is the `Owned` variant.
87/// any attempt to mutate the [`Snapshot`] will convert it to the `Owned`
88/// variant.
89#[derive(Clone, Debug, MallocSizeOf)]
90pub enum SnapshotData {
91    SharedMemory(
92        #[conditional_malloc_size_of] Arc<GenericSharedMemory>,
93        Range<usize>,
94    ),
95    SharedVec(#[conditional_malloc_size_of] Arc<Vec<u8>>, Range<usize>),
96    Owned(Vec<u8>),
97}
98
99impl SnapshotData {
100    fn to_vec(&self) -> Vec<u8> {
101        match &self {
102            SnapshotData::SharedMemory(data, byte_range) => Vec::from(&data[byte_range.clone()]),
103            SnapshotData::SharedVec(data, byte_range) => Vec::from(&data[byte_range.clone()]),
104            SnapshotData::Owned(data) => data.clone(),
105        }
106    }
107}
108
109impl DerefMut for SnapshotData {
110    fn deref_mut(&mut self) -> &mut Self::Target {
111        match self {
112            SnapshotData::SharedMemory(..) | SnapshotData::SharedVec(..) => {
113                *self = SnapshotData::Owned(self.to_vec());
114                &mut *self
115            },
116            SnapshotData::Owned(items) => items,
117        }
118    }
119}
120
121impl Deref for SnapshotData {
122    type Target = [u8];
123
124    fn deref(&self) -> &Self::Target {
125        match &self {
126            SnapshotData::SharedMemory(data, byte_range) => &data[byte_range.clone()],
127            SnapshotData::SharedVec(data, byte_range) => &data[byte_range.clone()],
128            SnapshotData::Owned(items) => items,
129        }
130    }
131}
132
133/// Represents image bitmap with metadata, usually as snapshot of canvas
134///
135/// This allows us to hold off conversions (BGRA <-> RGBA, (un)premultiply)
136/// to when/if they are actually needed (WebGL/WebGPU can load both BGRA and RGBA).
137///
138/// Inspired by snapshot for concept in WebGPU spec:
139/// <https://gpuweb.github.io/gpuweb/#abstract-opdef-get-a-copy-of-the-image-contents-of-a-context>
140#[derive(Clone, Debug, MallocSizeOf)]
141pub struct Snapshot {
142    size: Size2D<u32>,
143    /// internal data (can be any format it will be converted on use if needed)
144    data: SnapshotData,
145    /// RGBA/BGRA (reflect internal data)
146    format: SnapshotPixelFormat,
147    /// How to treat alpha channel
148    alpha_mode: SnapshotAlphaMode,
149}
150
151impl Snapshot {
152    pub const fn size(&self) -> Size2D<u32> {
153        self.size
154    }
155
156    pub const fn format(&self) -> SnapshotPixelFormat {
157        self.format
158    }
159
160    pub const fn alpha_mode(&self) -> SnapshotAlphaMode {
161        self.alpha_mode
162    }
163
164    pub fn empty() -> Self {
165        Self {
166            size: Size2D::zero(),
167            data: SnapshotData::Owned(vec![]),
168            format: SnapshotPixelFormat::RGBA,
169            alpha_mode: SnapshotAlphaMode::Transparent {
170                premultiplied: true,
171            },
172        }
173    }
174
175    /// Returns snapshot with provided size that is black transparent alpha
176    pub fn cleared(size: Size2D<u32>) -> Self {
177        Self {
178            size,
179            data: SnapshotData::Owned(vec![0; size.area() as usize * 4]),
180            format: SnapshotPixelFormat::RGBA,
181            alpha_mode: SnapshotAlphaMode::Transparent {
182                premultiplied: true,
183            },
184        }
185    }
186
187    pub fn from_vec(
188        size: Size2D<u32>,
189        format: SnapshotPixelFormat,
190        alpha_mode: SnapshotAlphaMode,
191        data: Vec<u8>,
192    ) -> Self {
193        Self {
194            size,
195            data: SnapshotData::Owned(data),
196            format,
197            alpha_mode,
198        }
199    }
200
201    pub fn from_arc_vec(
202        size: Size2D<u32>,
203        format: SnapshotPixelFormat,
204        alpha_mode: SnapshotAlphaMode,
205        data: Arc<Vec<u8>>,
206        byte_range_bounds: impl RangeBounds<usize>,
207    ) -> Self {
208        let range_start = match byte_range_bounds.start_bound() {
209            Bound::Included(bound) => *bound,
210            Bound::Excluded(bound) => *bound + 1,
211            Bound::Unbounded => 0,
212        };
213        let range_end = match byte_range_bounds.end_bound() {
214            Bound::Included(bound) => *bound + 1,
215            Bound::Excluded(bound) => *bound,
216            Bound::Unbounded => data.len(),
217        };
218        Self {
219            size,
220            data: SnapshotData::SharedVec(data, range_start..range_end),
221            format,
222            alpha_mode,
223        }
224    }
225
226    pub fn get_rect(&self, rect: Rect<u32>) -> Self {
227        let data = rgba8_get_rect(self.as_raw_bytes(), self.size(), rect).to_vec();
228        Self::from_vec(rect.size, self.format, self.alpha_mode, data)
229    }
230
231    /// Convert inner data of snapshot to target format and alpha mode.
232    /// If data is already in target format and alpha mode no work will be done.
233    pub fn transform(
234        &mut self,
235        target_alpha_mode: SnapshotAlphaMode,
236        target_format: SnapshotPixelFormat,
237    ) {
238        if self.alpha_mode == target_alpha_mode && target_format == self.format {
239            return;
240        }
241
242        let swap_rb = target_format != self.format;
243        let multiply = match (self.alpha_mode, target_alpha_mode) {
244            (SnapshotAlphaMode::Opaque, _) => Multiply::None,
245            (alpha_mode, SnapshotAlphaMode::Opaque)
246                if alpha_mode.alpha() == Alpha::Premultiplied =>
247            {
248                Multiply::UnMultiply
249            },
250            (_, SnapshotAlphaMode::Opaque) => Multiply::None,
251            (
252                SnapshotAlphaMode::Transparent { premultiplied } |
253                SnapshotAlphaMode::AsOpaque { premultiplied },
254                SnapshotAlphaMode::Transparent {
255                    premultiplied: target_premultiplied,
256                } |
257                SnapshotAlphaMode::AsOpaque {
258                    premultiplied: target_premultiplied,
259                },
260            ) => {
261                if premultiplied == target_premultiplied {
262                    Multiply::None
263                } else if target_premultiplied {
264                    Multiply::PreMultiply
265                } else {
266                    Multiply::UnMultiply
267                }
268            },
269        };
270
271        let clear_alpha = !matches!(self.alpha_mode, SnapshotAlphaMode::Opaque) &&
272            matches!(target_alpha_mode, SnapshotAlphaMode::Opaque);
273
274        if matches!(multiply, Multiply::None) && !swap_rb && !clear_alpha {
275            return;
276        }
277
278        transform_inplace(self.data.deref_mut(), multiply, swap_rb, clear_alpha);
279        self.alpha_mode = target_alpha_mode;
280        self.format = target_format;
281    }
282
283    pub fn as_raw_bytes(&self) -> &[u8] {
284        &self.data
285    }
286
287    pub fn as_raw_bytes_mut(&mut self) -> &mut [u8] {
288        &mut self.data
289    }
290
291    pub fn to_shared(&self) -> SharedSnapshot {
292        let (data, byte_range) = match &self.data {
293            SnapshotData::SharedMemory(data, byte_range) => (data.clone(), byte_range.clone()),
294            SnapshotData::SharedVec(data, byte_range) => (
295                Arc::new(GenericSharedMemory::from_arc_vec(data.clone())),
296                byte_range.clone(),
297            ),
298            SnapshotData::Owned(data) => (
299                Arc::new(GenericSharedMemory::from_bytes(data)),
300                0..data.len(),
301            ),
302        };
303        SharedSnapshot {
304            size: self.size,
305            data,
306            byte_range,
307            format: self.format,
308            alpha_mode: self.alpha_mode,
309        }
310    }
311
312    /// Convert into a shared snapshot, reusing owned storage in single-process mode.
313    pub fn into_shared(self) -> SharedSnapshot {
314        let (data, byte_range) = match self.data {
315            SnapshotData::SharedMemory(data, byte_range) => (data, byte_range),
316            SnapshotData::SharedVec(data, byte_range) => (
317                Arc::new(GenericSharedMemory::from_arc_vec(data)),
318                byte_range,
319            ),
320            SnapshotData::Owned(data) => {
321                let byte_range = 0..data.len();
322                (Arc::new(GenericSharedMemory::from_vec(data)), byte_range)
323            },
324        };
325        SharedSnapshot {
326            size: self.size,
327            data,
328            byte_range,
329            format: self.format,
330            alpha_mode: self.alpha_mode,
331        }
332    }
333
334    pub fn encode_for_mime_type<W: std::io::Write>(
335        &mut self,
336        image_type: &EncodedImageType,
337        quality: Option<f64>,
338        encoder: &mut W,
339    ) -> Result<(), ImageError> {
340        let width = self.size.width;
341        let height = self.size.height;
342        let alpha_mode = match image_type {
343            EncodedImageType::Jpeg => SnapshotAlphaMode::AsOpaque {
344                premultiplied: true,
345            },
346            _ => SnapshotAlphaMode::Transparent {
347                premultiplied: false,
348            },
349        };
350
351        self.transform(alpha_mode, SnapshotPixelFormat::RGBA);
352        let data = &self.data;
353
354        match image_type {
355            EncodedImageType::Png => {
356                // FIXME(nox): https://github.com/image-rs/image-png/issues/86
357                // FIXME(nox): https://github.com/image-rs/image-png/issues/87
358                PngEncoder::new(encoder).write_image(data, width, height, ExtendedColorType::Rgba8)
359            },
360            EncodedImageType::Jpeg => {
361                let mut jpeg_encoder = if let Some(quality) = quality {
362                    // The specification allows quality to be in [0.0..1.0] but the JPEG encoder
363                    // expects it to be in [1..100]
364                    if (0.0..=1.0).contains(&quality) {
365                        JpegEncoder::new_with_quality(
366                            encoder,
367                            (quality * 100.0).round().clamp(1.0, 100.0) as u8,
368                        )
369                    } else {
370                        JpegEncoder::new(encoder)
371                    }
372                } else {
373                    JpegEncoder::new(encoder)
374                };
375
376                // JPEG doesn't support transparency, so simply calling jpeg_encoder.write_image fails here.
377                // Instead we have to create a struct to translate from rgba to rgb.
378                struct RgbaDataForJpegEncoder<'a> {
379                    width: u32,
380                    height: u32,
381                    data: &'a [u8],
382                }
383
384                impl<'a> GenericImageView for RgbaDataForJpegEncoder<'a> {
385                    type Pixel = Rgb<u8>;
386
387                    fn dimensions(&self) -> (u32, u32) {
388                        (self.width, self.height)
389                    }
390
391                    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
392                        let offset = (self.width * y + x) as usize * 4;
393                        Rgb([
394                            self.data[offset],
395                            self.data[offset + 1],
396                            self.data[offset + 2],
397                        ])
398                    }
399                }
400
401                let image = RgbaDataForJpegEncoder {
402                    width,
403                    height,
404                    data,
405                };
406
407                jpeg_encoder.encode_image(&image)
408            },
409            EncodedImageType::Webp => {
410                // No quality support because of https://github.com/image-rs/image/issues/1984
411                WebPEncoder::new_lossless(encoder).write_image(
412                    data,
413                    width,
414                    height,
415                    ExtendedColorType::Rgba8,
416                )
417            },
418        }
419    }
420}
421
422impl From<Snapshot> for Vec<u8> {
423    fn from(value: Snapshot) -> Self {
424        match value.data {
425            SnapshotData::SharedMemory(..) => Vec::from(value.as_raw_bytes()),
426            SnapshotData::SharedVec(..) => Vec::from(value.as_raw_bytes()),
427            SnapshotData::Owned(data) => data,
428        }
429    }
430}
431
432/// A version of [`Snapshot`] that can be sent across IPC channels.
433#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
434pub struct SharedSnapshot {
435    /// The physical size of this [`SharedSnapshot`].
436    size: Size2D<u32>,
437    /// The shared data of this [`SharedSnapshot`].
438    #[conditional_malloc_size_of]
439    data: Arc<GenericSharedMemory>,
440    /// The byte range of the data within the shared memory segment. This is used to
441    /// send individual image frames of animated images.
442    byte_range: Range<usize>,
443    /// The [`SnapshotPixelFormat`] of this [`SharedSnapshot`]
444    format: SnapshotPixelFormat,
445    /// The [`SnapshotAlphaMode`] of this [`SharedSnapshot`].
446    alpha_mode: SnapshotAlphaMode,
447}
448
449impl SharedSnapshot {
450    pub fn to_owned(&self) -> Snapshot {
451        Snapshot {
452            size: self.size,
453            data: SnapshotData::SharedMemory(self.data.clone(), self.byte_range.clone()),
454            format: self.format,
455            alpha_mode: self.alpha_mode,
456        }
457    }
458
459    /// Returns snapshot with provided size that is black transparent alpha
460    pub fn cleared(size: Size2D<u32>) -> Self {
461        let length_in_bytes = size.area() as usize * 4;
462        Self {
463            size,
464            data: Arc::new(GenericSharedMemory::from_byte(0, length_in_bytes)),
465            byte_range: 0..length_in_bytes,
466            format: SnapshotPixelFormat::RGBA,
467            alpha_mode: SnapshotAlphaMode::Transparent {
468                premultiplied: true,
469            },
470        }
471    }
472
473    pub const fn size(&self) -> Size2D<u32> {
474        self.size
475    }
476
477    pub const fn format(&self) -> SnapshotPixelFormat {
478        self.format
479    }
480
481    pub const fn alpha_mode(&self) -> SnapshotAlphaMode {
482        self.alpha_mode
483    }
484
485    pub fn data(&self) -> &[u8] {
486        &(&self.data)[self.byte_range.clone()]
487    }
488
489    pub fn shared_memory(&self) -> GenericSharedMemory {
490        (*self.data).clone()
491    }
492}