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

Code before:
                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);

        let size = DeviceIntSize::new(self.metadata.width as i32, self.metadata.height as i32);
        let descriptor = ImageDescriptor {
            size,
            stride: None,
            format,
            offset: frame.byte_range.start as i32,
            flags,
        };
        (descriptor, GenericSharedMemory::from_bytes(&data))
    }

    /// For animations the image already exists in a cache in 'Painter'. We just send the description.
    /// Currently we do not support 'PixelFormat::RGB8'
    pub fn webrender_image_descriptor_for_image_animation(&self) -> Option<ImageDescriptor> {
        if self.format == PixelFormat::RGB8 ||
            self.format == PixelFormat::K8 ||
            self.format == PixelFormat::KA8
        {
            return None;
        }
        let format = Self::pixel_format_to_webrender_image_format(self.format);
        let mut flags = ImageDescriptorFlags::ALLOW_MIPMAPS;
        flags.set(ImageDescriptorFlags::IS_OPAQUE, self.is_opaque);

        let size = DeviceIntSize::new(self.metadata.width as i32, self.metadata.height as i32);
        let descriptor = ImageDescriptor {
            size,
            stride: None,
            format,
            offset: 0,
            flags,
        };
        Some(descriptor)
    }

    pub fn to_shared(&self) -> Arc<SharedRasterImage> {
        Arc::new(SharedRasterImage {
            metadata: self.metadata,
            format: self.format,

Review comment (performance):
```suggestion
    /// For animations the image already exists in a cache in 'Painter'. We just send the description.
    /// Currently we do not support 'PixelFormat::RGB8'
    pub fn webrender_image_descriptor_and_offset_for_frame(&self) -> Option<ImageDescriptor> {
```

Code after:
        flags.set(ImageDescriptorFlags::IS_OPAQUE, self.is_opaque);

        let size = DeviceIntSize::new(self.metadata.width as i32, self.metadata.height as i32);
        let descriptor = ImageDescriptor {
            size,
            stride: None,
            format,
            offset: frame.byte_range.start as i32,
            flags,
        };
        (descriptor, GenericSharedMemory::from_bytes(&data))
    }

    /// For animations the image already exists in a cache in 'Painter'. We just send the description.
    /// Currently we do not support 'PixelFormat::RGB8'
    pub fn webrender_image_descriptor_and_offset_for_frame(&self) -> Option<ImageDescriptor> {
        if self.format == PixelFormat::RGB8 ||
            self.format == PixelFormat::K8 ||
            self.format == PixelFormat::KA8
        {
            return None;
        }
        let format = match self.format {
            PixelFormat::BGRA8 => WebRenderImageFormat::BGRA8,
            PixelFormat::RGBA8 => WebRenderImageFormat::RGBA8,
            PixelFormat::RGB8 => WebRenderImageFormat::BGRA8,
            PixelFormat::KA8 | PixelFormat::K8 => {
                error!("Pixel format currently not supported");
                return None;
            },
        };
        let mut flags = ImageDescriptorFlags::ALLOW_MIPMAPS;
        flags.set(ImageDescriptorFlags::IS_OPAQUE, self.is_opaque);

        let size = DeviceIntSize::new(self.metadata.width as i32, self.metadata.height as i32);
        let descriptor = ImageDescriptor {
            size,
            stride: None,
            format,
            offset: 0,
            flags,
        };
        Some(descriptor)
    }

    pub fn to_shared(&self) -> Arc<SharedRasterImage> {
        Arc::new(SharedRasterImage {
            metadata: self.metadata,
            format: self.format,
            id: self.id,
            cors_status: self.cors_status,