Repository: servo/servo
File: components/pixels/lib.rs (Rust)

Code before:
            PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
            PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
            pixel_format => {
                unimplemented!("unsupported pixel format ({pixel_format:?})");
            },
        };

        let alpha_mode = SnapshotAlphaMode::Transparent {
            premultiplied: true,
        };

        Snapshot::from_arc_vec(
            size.cast(),
            format,
            alpha_mode,
            self.bytes.clone(),
            self.frames[0].byte_range.clone(),
        )
    }

    pub fn frame_data(&self, index: usize) -> Option<&ImageFrame> {
        self.frames.get(index)
    }

    /// This is not a general conversion function but special for some specific usecases.
    fn pixel_format_to_webrender_image_format(format: PixelFormat) -> Option<WebRenderImageFormat> {
        match format {
            PixelFormat::BGRA8 => Some(WebRenderImageFormat::BGRA8),
            PixelFormat::RGBA8 => Some(WebRenderImageFormat::RGBA8),
            PixelFormat::RGB8 => Some(WebRenderImageFormat::BGRA8),
            PixelFormat::KA8 | PixelFormat::K8 => None,
        }
    }

    pub fn webrender_image_descriptor_and_data_for_frame(
        &self,
        frame_index: usize,
    ) -> (ImageDescriptor, GenericSharedMemory) {
        let frame = self
            .frames
            .get(frame_index)
            .expect("Asked for a frame that did not exist: {frame_index:?}");

        let (format, data) = match self.format {
            PixelFormat::BGRA8 => (WebRenderImageFormat::BGRA8, (*self.bytes).clone()),
            PixelFormat::RGBA8 => (WebRenderImageFormat::RGBA8, (*self.bytes).clone()),
            PixelFormat::RGB8 => {
                let frame_bytes = &self.bytes[frame.byte_range.clone()];
                let mut bytes = Vec::with_capacity(frame_bytes.len() / 3 * 4);
                for rgb in frame_bytes.chunks(3) {
                    bytes.extend_from_slice(&[rgb[2], rgb[1], rgb[0], 0xff]);

Review comment (refactor):
This is only used in one place and it's a bit mysterious to just refer to "specific usecases," so please move this inline into `webrender_image_descriptor_and_offset_for_frame` where you have to check the `PixelFormat` anyway.


Code after:
            PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
            PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
            pixel_format => {
                unimplemented!("unsupported pixel format ({pixel_format:?})");
            },
        };

        let alpha_mode = SnapshotAlphaMode::Transparent {
            premultiplied: true,
        };

        Snapshot::from_arc_vec(
            size.cast(),
            format,
            alpha_mode,
            self.bytes.clone(),
            self.frames[0].byte_range.clone(),
        )
    }

    pub fn frame_data(&self, index: usize) -> Option<&ImageFrame> {
        self.frames.get(index)
    }

    pub fn webrender_image_descriptor_and_data_for_frame(
        &self,
        frame_index: usize,
    ) -> (ImageDescriptor, GenericSharedMemory) {
        let frame = self
            .frames
            .get(frame_index)
            .expect("Asked for a frame that did not exist: {frame_index:?}");

        let (format, data) = match self.format {
            PixelFormat::BGRA8 => (WebRenderImageFormat::BGRA8, (*self.bytes).clone()),
            PixelFormat::RGBA8 => (WebRenderImageFormat::RGBA8, (*self.bytes).clone()),
            PixelFormat::RGB8 => {
                let frame_bytes = &self.bytes[frame.byte_range.clone()];
                let mut bytes = Vec::with_capacity(frame_bytes.len() / 3 * 4);
                for rgb in frame_bytes.chunks(3) {
                    bytes.extend_from_slice(&[rgb[2], rgb[1], rgb[0], 0xff]);
                }
                (WebRenderImageFormat::BGRA8, bytes)
            },
            PixelFormat::K8 | PixelFormat::KA8 => {
                panic!("Not support by webrender yet");
            },
        };
        let mut flags = ImageDescriptorFlags::ALLOW_MIPMAPS;
        flags.set(ImageDescriptorFlags::IS_OPAQUE, self.is_opaque);