mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Introduce snapshot concept of canvas (#36119)
Each canvas context returns snapshot instead of just raw bytes. This allows as to hold off conversions (BGRA <-> RGBA, (un)premultiply) to when/if they are actually needed. For example when loading snapshot into webgl we can load both RGBA and BGRA so no conversion is really needed. Currently whole thing is designed to be able to be extend on https://github.com/servo/ipc-channel/pull/356, to make less copies. Hence some commented out code. Fixes #35759 There are tests for these changes in WPT --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
parent
b6967fc4c8
commit
73b778e67f
37 changed files with 724 additions and 251 deletions
|
@ -84,6 +84,7 @@ pub fn rgba8_premultiply_inplace(pixels: &mut [u8]) -> bool {
|
|||
is_opaque
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn multiply_u8_color(a: u8, b: u8) -> u8 {
|
||||
(a as u32 * b as u32 / 255) as u8
|
||||
}
|
||||
|
@ -254,6 +255,69 @@ pub fn unmultiply_inplace<const SWAP_RB: bool>(pixels: &mut [u8]) {
|
|||
}
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
pub enum Multiply {
|
||||
None = 0,
|
||||
PreMultiply = 1,
|
||||
UnMultiply = 2,
|
||||
}
|
||||
|
||||
pub fn transform_inplace(pixels: &mut [u8], multiply: Multiply, swap_rb: bool, clear_alpha: bool) {
|
||||
match (multiply, swap_rb, clear_alpha) {
|
||||
(Multiply::None, true, true) => generic_transform_inplace::<0, true, true>(pixels),
|
||||
(Multiply::None, true, false) => generic_transform_inplace::<0, true, false>(pixels),
|
||||
(Multiply::None, false, true) => generic_transform_inplace::<0, false, true>(pixels),
|
||||
(Multiply::None, false, false) => generic_transform_inplace::<0, false, false>(pixels),
|
||||
(Multiply::PreMultiply, true, true) => generic_transform_inplace::<1, true, true>(pixels),
|
||||
(Multiply::PreMultiply, true, false) => generic_transform_inplace::<1, true, false>(pixels),
|
||||
(Multiply::PreMultiply, false, true) => generic_transform_inplace::<1, false, true>(pixels),
|
||||
(Multiply::PreMultiply, false, false) => {
|
||||
generic_transform_inplace::<1, false, false>(pixels)
|
||||
},
|
||||
(Multiply::UnMultiply, true, true) => generic_transform_inplace::<2, true, true>(pixels),
|
||||
(Multiply::UnMultiply, true, false) => generic_transform_inplace::<2, true, false>(pixels),
|
||||
(Multiply::UnMultiply, false, true) => generic_transform_inplace::<2, false, true>(pixels),
|
||||
(Multiply::UnMultiply, false, false) => {
|
||||
generic_transform_inplace::<2, false, false>(pixels)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generic_transform_inplace<
|
||||
const MULTIPLY: u8, // 1 premultiply, 2 unmultiply
|
||||
const SWAP_RB: bool,
|
||||
const CLEAR_ALPHA: bool,
|
||||
>(
|
||||
pixels: &mut [u8],
|
||||
) {
|
||||
for rgba in pixels.chunks_mut(4) {
|
||||
match MULTIPLY {
|
||||
1 => {
|
||||
let a = rgba[3];
|
||||
multiply_u8_color(rgba[0], a);
|
||||
multiply_u8_color(rgba[1], a);
|
||||
multiply_u8_color(rgba[2], a);
|
||||
},
|
||||
2 => {
|
||||
let a = rgba[3] as u32;
|
||||
|
||||
if a > 0 {
|
||||
rgba[0] = (rgba[0] as u32 * 255 / a) as u8;
|
||||
rgba[1] = (rgba[1] as u32 * 255 / a) as u8;
|
||||
rgba[2] = (rgba[2] as u32 * 255 / a) as u8;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
if SWAP_RB {
|
||||
rgba.swap(0, 2);
|
||||
}
|
||||
if CLEAR_ALPHA {
|
||||
rgba[3] = u8::MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_gif(buffer: &[u8]) -> bool {
|
||||
buffer.starts_with(b"GIF87a") || buffer.starts_with(b"GIF89a")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue