use crate::page_ir::DeviceRect;
use crate::render_api::{Background, OutputFormat};
use std::{mem::MaybeUninit, sync::Arc};
#[derive(Debug)]
pub struct Surface {
pub width: usize,
pub height: usize,
pub origin_x: usize,
pub origin_y: usize,
data: Arc<[u8]>,
}
impl Clone for Surface {
fn clone(&self) -> Self {
Self {
width: self.width,
height: self.height,
origin_x: self.origin_x,
origin_y: self.origin_y,
data: Arc::from(self.data.as_ref()),
}
}
}
impl Surface {
fn checked_pixels(width: usize, height: usize) -> Option<usize> {
let pixels = width.checked_mul(height)?;
pixels.checked_mul(4).map(|_| pixels)
}
pub fn new(width: usize, height: usize, background: Background) -> Self {
Self::new_recycled(width, height, background, None)
}
pub fn new_recycled(
width: usize,
height: usize,
background: Background,
recycled: Option<Arc<[u8]>>,
) -> Self {
let Some(pixels) = Self::checked_pixels(width, height) else {
return Self::empty();
};
let len = pixels * 4;
let data = match reuse(recycled, len) {
Some(mut buf) => {
paint_background(unique_arc_mut(&mut buf), background);
buf
}
None => match background {
Background::Transparent => filled_arc(len, 0),
Background::White => filled_arc(len, 0xFF),
Background::Solid(c) => repeated_rgba_arc(pixels, solid_rgba(c)),
},
};
Self {
width,
height,
origin_x: 0,
origin_y: 0,
data,
}
}
pub(crate) fn new_unpainted(width: usize, height: usize, recycled: Option<Arc<[u8]>>) -> Self {
let Some(pixels) = Self::checked_pixels(width, height) else {
return Self::empty();
};
let len = pixels * 4;
Self {
width,
height,
origin_x: 0,
origin_y: 0,
data: reuse(recycled, len).unwrap_or_else(|| zeroed_arc(len)),
}
}
pub(crate) fn pixels(&self) -> &[u8] {
&self.data
}
pub(crate) fn into_exact_rgb(self, parked: &mut Option<Arc<[u8]>>) -> (usize, Arc<[u8]>) {
let mut rgb = zeroed_arc(self.width * self.height * 3);
rgba_rows_to_rgb8(&self.data, unique_arc_mut(&mut rgb));
let stride = self.width * 3;
*parked = Some(self.data);
(stride, rgb)
}
pub(crate) fn into_buffer(self) -> Arc<[u8]> {
self.data
}
pub fn offscreen(bounds: DeviceRect) -> Self {
let width = bounds.width as usize;
let height = bounds.height as usize;
let Some(pixels) = Self::checked_pixels(width, height) else {
return Self::empty();
};
Self {
width,
height,
origin_x: bounds.x.max(0) as usize,
origin_y: bounds.y.max(0) as usize,
data: filled_arc(pixels * 4, 0),
}
}
fn empty() -> Self {
Self {
width: 0,
height: 0,
origin_x: 0,
origin_y: 0,
data: filled_arc(0, 0),
}
}
pub fn offscreen_seeded(parent: &Surface, bounds: DeviceRect) -> Self {
let mut off = Surface::offscreen(bounds);
let stride = off.width * 4;
let x_start = off.origin_x.max(parent.origin_x);
let x_end = (off.origin_x + off.width).min(parent.origin_x + parent.width);
if x_end <= x_start {
return off;
}
let span = (x_end - x_start) * 4;
let dst_off = (x_start - off.origin_x) * 4;
let src_off = (x_start - parent.origin_x) * 4;
let y_start = off.origin_y.max(parent.origin_y);
let y_end = (off.origin_y + off.height).min(parent.origin_y + parent.height);
for abs_y in y_start..y_end {
let ly = abs_y - off.origin_y;
let prow = parent.local_row(abs_y - parent.origin_y);
let orow = &mut unique_arc_mut(&mut off.data)[ly * stride..(ly + 1) * stride];
orow[dst_off..dst_off + span].copy_from_slice(&prow[src_off..src_off + span]);
}
off
}
pub fn bytes(&self) -> u64 {
self.data.len() as u64
}
#[inline]
pub fn row_mut(&mut self, y: usize) -> &mut [u8] {
let stride = self.width * 4;
let local = y - self.origin_y;
&mut unique_arc_mut(&mut self.data)[local * stride..(local + 1) * stride]
}
#[inline]
pub(crate) fn rows_mut_abs(&mut self, y0: usize, y1: usize) -> (&mut [u8], usize, usize) {
let stride = self.width * 4;
let local0 = y0.saturating_sub(self.origin_y).min(self.height);
let local1 = y1.saturating_sub(self.origin_y).min(self.height);
let local0 = local0.min(local1);
let first_abs_y = self.origin_y + local0;
(
&mut unique_arc_mut(&mut self.data)[local0 * stride..local1 * stride],
first_abs_y,
stride,
)
}
#[inline]
pub fn local_row(&self, local_y: usize) -> &[u8] {
let stride = self.width * 4;
&self.data[local_y * stride..(local_y + 1) * stride]
}
pub(crate) fn copy_pixels_from(&mut self, source: &Self) {
debug_assert_eq!(self.width, source.width);
debug_assert_eq!(self.height, source.height);
debug_assert_eq!(self.origin_x, source.origin_x);
debug_assert_eq!(self.origin_y, source.origin_y);
unique_arc_mut(&mut self.data).copy_from_slice(&source.data);
}
#[allow(
unsafe_code,
reason = "Arc::new_uninit_slice hands back MaybeUninit; see SAFETY comment"
)]
pub fn into_output(self, format: OutputFormat) -> (usize, Arc<[u8]>) {
self.into_output_recycling(format, &mut None)
}
pub(crate) fn into_packed_rgb(mut self, parked: &mut Option<Arc<[u8]>>) -> (usize, Arc<[u8]>) {
let data = unique_arc_mut(&mut self.data);
for pixel in 1..data.len() / 4 {
let (src, dst) = (pixel * 4, pixel * 3);
data[dst] = data[src];
data[dst + 1] = data[src + 1];
data[dst + 2] = data[src + 2];
}
*parked = Some(Arc::clone(&self.data));
(self.width * 3, self.data)
}
#[allow(
unsafe_code,
reason = "Arc::new_uninit_slice hands back MaybeUninit; see SAFETY comment"
)]
pub(crate) fn into_output_recycling(
self,
format: OutputFormat,
parked: &mut Option<Arc<[u8]>>,
) -> (usize, Arc<[u8]>) {
match format {
OutputFormat::Rgba8PremultipliedSrgb => {
*parked = Some(Arc::clone(&self.data));
(self.width * 4, self.data)
}
OutputFormat::Gray8 => {
let pixels = self.width * self.height;
debug_assert_eq!(self.data.len(), pixels * 4);
let mut gray = zeroed_arc(pixels);
rgba_rows_to_gray8(
&self.data,
unique_arc_mut(&mut gray),
crate::render_cpu::exec::output_fan_out(),
);
let width = self.width;
*parked = Some(self.data);
(width, gray)
}
}
}
}
pub(crate) fn rgba_rows_to_gray8(rgba: &[u8], gray: &mut [u8], fan_out: bool) {
const TASK: usize = 1 << 16;
if fan_out && gray.len() >= 4 * TASK {
use rayon::prelude::*;
gray.par_chunks_mut(TASK)
.zip(rgba.par_chunks(TASK * 4))
.for_each(|(gray, rgba)| gray8_rows_best(rgba, gray));
} else {
gray8_rows_best(rgba, gray);
}
}
#[allow(
unsafe_code,
reason = "dispatches the AVX2 build of the same safe loop behind a runtime probe"
)]
fn gray8_rows_best(rgba: &[u8], gray: &mut [u8]) {
#[cfg(target_arch = "x86_64")]
if is_x86_feature_detected!("avx2") {
return unsafe { gray8_rows_avx2(rgba, gray) };
}
gray8_rows(rgba, gray);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(unsafe_code)]
unsafe fn gray8_rows_avx2(rgba: &[u8], gray: &mut [u8]) {
gray8_rows(rgba, gray);
}
#[inline(always)]
fn gray8_rows(rgba: &[u8], gray: &mut [u8]) {
const BLOCK: usize = 64;
for (dst, src) in gray.chunks_mut(BLOCK).zip(rgba.chunks(BLOCK * 4)) {
let mut halves = 0u32;
for (out, px) in dst.iter_mut().zip(src.chunks_exact(4)) {
let sum = 2126 * u32::from(px[0]) + 7152 * u32::from(px[1]) + 722 * u32::from(px[2]);
let whole = sum / 10_000;
let rest = sum - whole * 10_000;
halves |= u32::from(rest == 5_000);
*out = (whole + u32::from(rest > 5_000)) as u8;
}
if halves != 0 {
for (out, px) in dst.iter_mut().zip(src.chunks_exact(4)) {
*out = gray8_luma(px[0], px[1], px[2]);
}
}
}
}
pub(crate) fn rgba_rows_to_rgb8(rgba: &[u8], rgb: &mut [u8]) {
for (dst, px) in rgb.chunks_exact_mut(3).zip(rgba.chunks_exact(4)) {
dst.copy_from_slice(&px[..3]);
}
}
#[inline(always)]
fn gray8_luma(r: u8, g: u8, b: u8) -> u8 {
let y = 0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32;
let floor = y as i32;
(floor + i32::from(y - floor as f32 >= 0.5)).min(255) as u8
}
#[allow(
unsafe_code,
reason = "Arc::new_uninit_slice/new_zeroed_slice hand back MaybeUninit; see SAFETY comments"
)]
pub(crate) fn filled_arc(len: usize, byte: u8) -> Arc<[u8]> {
if byte == 0 {
let data = Arc::<[u8]>::new_zeroed_slice(len);
return unsafe { data.assume_init() };
}
let mut data = Arc::<[u8]>::new_uninit_slice(len);
unique_arc_mut(&mut data).fill(MaybeUninit::new(byte));
unsafe { data.assume_init() }
}
pub(crate) fn zeroed_arc(len: usize) -> Arc<[u8]> {
filled_arc(len, 0)
}
#[inline]
fn solid_rgba(c: crate::page_ir::Color) -> [u8; 4] {
let a = c.a.clamp(0.0, 1.0);
[
to_u8(c.r.clamp(0.0, 1.0) * a),
to_u8(c.g.clamp(0.0, 1.0) * a),
to_u8(c.b.clamp(0.0, 1.0) * a),
to_u8(a),
]
}
fn paint_background(buf: &mut [u8], background: Background) {
match background {
Background::Transparent => buf.fill(0),
Background::White => buf.fill(0xFF),
Background::Solid(c) => {
let px = solid_rgba(c);
for chunk in buf.chunks_exact_mut(4) {
chunk.copy_from_slice(&px);
}
}
}
}
pub(crate) const SURFACE_POOL_BYTES: usize = 256 << 20;
const POOL_SIZES: usize = 3;
#[derive(Debug)]
pub(crate) struct SurfacePool {
cap_bytes: usize,
parked: std::sync::Mutex<Vec<Arc<[u8]>>>,
}
static SLOT_HITS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static SLOT_MISSES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub fn surface_slot_stats() -> (u64, u64) {
use std::sync::atomic::Ordering::Relaxed;
(SLOT_HITS.load(Relaxed), SLOT_MISSES.load(Relaxed))
}
impl SurfacePool {
pub(crate) fn new(cap_bytes: usize) -> Self {
Self {
cap_bytes,
parked: std::sync::Mutex::new(Vec::new()),
}
}
fn lock(&self) -> std::sync::MutexGuard<'_, Vec<Arc<[u8]>>> {
self.parked
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub(crate) fn take(&self, width: usize, height: usize) -> Option<Arc<[u8]>> {
use std::sync::atomic::Ordering::Relaxed;
let found = Surface::checked_pixels(width, height)
.map(|pixels| pixels * 4)
.and_then(|len| {
let mut parked = self.lock();
let index = parked
.iter()
.rposition(|buf| buf.len() == len && Arc::strong_count(buf) == 1)?;
Some(parked.remove(index))
});
match found {
Some(_) => SLOT_HITS.fetch_add(1, Relaxed),
None => SLOT_MISSES.fetch_add(1, Relaxed),
};
found
}
pub(crate) fn park(&self, buf: Arc<[u8]>) {
if buf.len() > self.cap_bytes {
return;
}
let mut parked = self.lock();
parked.push(buf);
let mut sizes = [0usize; POOL_SIZES];
let mut kept = 0;
for buf in parked.iter().rev() {
if kept < POOL_SIZES && !sizes[..kept].contains(&buf.len()) {
sizes[kept] = buf.len();
kept += 1;
}
}
parked.retain(|buf| sizes[..kept].contains(&buf.len()));
let mut total: usize = parked.iter().map(|buf| buf.len()).sum();
while total > self.cap_bytes && parked.len() > 1 {
total -= parked.remove(0).len();
}
}
pub(crate) fn clear(&self) {
self.lock().clear();
}
pub(crate) fn idle_bytes(&self) -> usize {
self.lock()
.iter()
.filter(|buf| Arc::strong_count(buf) == 1)
.map(|buf| buf.len())
.sum()
}
}
fn reuse(recycled: Option<Arc<[u8]>>, len: usize) -> Option<Arc<[u8]>> {
let mut buf = recycled?;
if buf.len() != len {
return None;
}
Arc::get_mut(&mut buf)?;
Some(buf)
}
#[allow(
unsafe_code,
reason = "Arc::new_uninit_slice hands back MaybeUninit; see SAFETY comment"
)]
fn repeated_rgba_arc(pixels: usize, rgba: [u8; 4]) -> Arc<[u8]> {
let mut data = Arc::<[u8]>::new_uninit_slice(pixels * 4);
for chunk in unique_arc_mut(&mut data).chunks_exact_mut(4) {
for (dst, src) in chunk.iter_mut().zip(rgba) {
dst.write(src);
}
}
unsafe { data.assume_init() }
}
#[inline]
fn to_u8(v: f32) -> u8 {
(v * 255.0 + 0.5).clamp(0.0, 255.0) as u8
}
#[inline]
pub(crate) fn unique_arc_mut<T>(data: &mut Arc<[T]>) -> &mut [T] {
let Some(data) = Arc::get_mut(data) else {
unreachable!("a final buffer is never shared while it is mutable")
};
data
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use super::*;
#[test]
fn rgba_output_hands_off_the_surface_pixels() {
let mut surface = Surface::new(2, 1, Background::White);
surface.row_mut(0)[0] = 17;
let (stride, output) = surface.into_output(OutputFormat::Rgba8PremultipliedSrgb);
assert_eq!(stride, 8);
assert_eq!(&*output, &[17, 255, 255, 255, 255, 255, 255, 255]);
assert_eq!(Arc::strong_count(&output), 1);
}
#[test]
fn a_recycled_surface_is_byte_identical_to_a_fresh_one() {
let backgrounds = [
Background::White,
Background::Transparent,
Background::Solid(crate::page_ir::Color {
r: 0.25,
g: 0.5,
b: 0.75,
a: 0.5,
}),
];
for background in backgrounds {
let recycled = filled_arc(5 * 3 * 4, 0x5A);
let allocation = Arc::as_ptr(&recycled);
let reused = Surface::new_recycled(5, 3, background, Some(recycled));
let fresh = Surface::new(5, 3, background);
assert_eq!(&*reused.data, &*fresh.data, "{background:?}");
assert_eq!(
Arc::as_ptr(&reused.data),
allocation,
"the recycled allocation should have been kept"
);
}
}
#[test]
fn recycling_declines_a_shared_or_mismatched_buffer() {
let shared = filled_arc(2 * 4, 0x11);
let keep = Arc::clone(&shared);
let surface = Surface::new_recycled(2, 1, Background::White, Some(shared));
assert_ne!(Arc::as_ptr(&surface.data), Arc::as_ptr(&keep));
assert_eq!(&*keep, &[0x11; 8]);
assert_eq!(&*surface.data, &[255; 8]);
let wrong = filled_arc(4, 0x22);
let ptr = Arc::as_ptr(&wrong);
let surface = Surface::new_recycled(2, 1, Background::White, Some(wrong));
assert_ne!(Arc::as_ptr(&surface.data), ptr);
assert_eq!(&*surface.data, &[255; 8]);
}
#[test]
fn gray8_luma_matches_round_on_every_input() {
for r in 0..=255u8 {
for g in 0..=255u8 {
for b in 0..=255u8 {
let y = 0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32;
let want = y.round().clamp(0.0, 255.0) as u8;
assert_eq!(gray8_luma(r, g, b), want, "rgb ({r}, {g}, {b})");
}
}
}
}
#[test]
fn gray8_rows_match_the_float_luma_on_every_input() {
let mut rgba = vec![0u8; 256 * 4];
let mut got = vec![0u8; 256];
let mut plain = vec![0u8; 256];
let mut halves = 0u32;
for r in 0..=255u8 {
for g in 0..=255u8 {
for (b, px) in rgba.chunks_exact_mut(4).enumerate() {
px.copy_from_slice(&[r, g, b as u8, 255]);
let sum = 2126 * u32::from(r) + 7152 * u32::from(g) + 722 * b as u32;
halves += u32::from(sum % 10_000 == 5_000);
}
rgba_rows_to_gray8(&rgba, &mut got, false);
gray8_rows(&rgba, &mut plain);
for (b, (&got, &plain)) in got.iter().zip(&plain).enumerate() {
let want = gray8_luma(r, g, b as u8);
assert_eq!(got, want, "rgb ({r}, {g}, {b})");
assert_eq!(plain, want, "rgb ({r}, {g}, {b}), baseline build");
}
}
}
assert!(halves > 1_000, "{halves} exact halves");
let rgba: Vec<u8> = (0..4 * 77).map(|i| (i * 37 % 251) as u8).collect();
let mut got = vec![7u8; 70];
rgba_rows_to_gray8(&rgba, &mut got, false);
for (i, &got) in got.iter().enumerate() {
let px = &rgba[i * 4..];
assert_eq!(got, gray8_luma(px[0], px[1], px[2]), "pixel {i}");
}
let pixels = (5 << 16) + 123;
let rgba: Vec<u8> = (0..pixels * 4).map(|i| (i * 131 % 253) as u8).collect();
let (mut serial, mut split) = (vec![0u8; pixels], vec![0u8; pixels]);
rgba_rows_to_gray8(&rgba, &mut serial, false);
rgba_rows_to_gray8(&rgba, &mut split, true);
assert!(serial == split);
}
#[test]
fn into_output_parks_the_buffer_for_the_next_render() {
let mut pool = None;
let surface = Surface::new(3, 2, Background::White);
let allocation = Arc::as_ptr(&surface.data);
let (_, pixels) =
surface.into_output_recycling(OutputFormat::Rgba8PremultipliedSrgb, &mut pool);
assert_eq!(Arc::as_ptr(pool.as_ref().unwrap()), allocation);
assert!(reuse(pool.clone(), 3 * 2 * 4).is_none());
drop(pixels);
let recycled = reuse(pool, 3 * 2 * 4).expect("sole owner now");
assert_eq!(Arc::as_ptr(&recycled), allocation);
let mut pool = None;
let surface = Surface::new(3, 2, Background::White);
let allocation = Arc::as_ptr(&surface.data);
let (_, gray) = surface.into_output_recycling(OutputFormat::Gray8, &mut pool);
assert_eq!(gray.len(), 6);
assert_eq!(Arc::as_ptr(pool.as_ref().unwrap()), allocation);
assert!(reuse(pool, 3 * 2 * 4).is_some());
}
#[test]
fn alternating_sizes_reuse_their_parked_buffers() {
let sizes = [(5usize, 4usize), (9, 7), (3, 2)];
let pool = SurfacePool::new(SURFACE_POOL_BYTES);
let mut addrs = [None; 3];
let (h0, m0) = surface_slot_stats();
for round in 0..4 {
for (i, &(w, h)) in sizes.iter().enumerate() {
let recycled = pool.take(w, h);
if round == 0 {
assert!(recycled.is_none());
} else {
let r = recycled.as_ref().expect("parked slot hit");
assert_eq!(Arc::as_ptr(r), addrs[i].unwrap(), "size {i} round {round}");
}
let surface = Surface::new_recycled(w, h, Background::White, recycled);
addrs[i].get_or_insert(Arc::as_ptr(&surface.data));
assert_eq!(Arc::as_ptr(&surface.data), addrs[i].unwrap());
let mut parked = None;
let (_, out) = surface.into_output_recycling(OutputFormat::Gray8, &mut parked);
pool.park(parked.unwrap());
assert_eq!(out.len(), w * h);
}
}
let (h1, m1) = surface_slot_stats();
assert!(h1 - h0 >= 9 && m1 - m0 >= 3);
let fresh = Surface::new(9, 7, Background::White);
let again = Surface::new_recycled(9, 7, Background::White, pool.take(9, 7));
assert_eq!(fresh.data, again.data);
}
#[test]
fn the_pool_keeps_no_more_than_its_cap_and_never_lends_a_held_buffer() {
let pool = SurfacePool::new(128);
let page = |w: usize, h: usize| {
let surface = Surface::new(w, h, Background::White);
let mut parked = None;
let (_, pixels) =
surface.into_output_recycling(OutputFormat::Rgba8PremultipliedSrgb, &mut parked);
(pixels, parked.unwrap())
};
let (held, handle) = page(4, 4);
pool.park(handle);
assert!(pool.take(4, 4).is_none());
assert_eq!(pool.idle_bytes(), 0);
drop(held);
assert_eq!(pool.idle_bytes(), 64);
for _ in 0..2 {
let (pixels, handle) = page(4, 4);
drop(pixels);
pool.park(handle);
}
assert_eq!(pool.idle_bytes(), 128);
let (pixels, handle) = page(8, 8);
drop(pixels);
pool.park(handle);
assert_eq!(pool.idle_bytes(), 128);
assert!(pool.take(8, 8).is_none());
assert!(pool.take(4, 4).is_some());
pool.clear();
assert_eq!(pool.idle_bytes(), 0);
let pool = SurfacePool::new(usize::MAX);
for side in 1..=6usize {
let (pixels, handle) = page(side, side);
drop(pixels);
pool.park(handle);
}
assert_eq!(pool.idle_bytes(), (16 + 25 + 36) * 4);
assert!(pool.take(3, 3).is_none());
assert!(pool.take(5, 5).is_some());
}
#[test]
fn packing_rgb_in_place_matches_dropping_alpha_into_a_copy() {
let (w, h) = (7usize, 5usize);
let mut surface = Surface::new(w, h, Background::White);
for y in 0..h {
for (x, px) in surface.row_mut(y).chunks_exact_mut(4).enumerate() {
px.copy_from_slice(&[(x * 31 + y) as u8, (y * 17) as u8, (x ^ y) as u8, 255]);
}
}
let expected: Vec<u8> = surface
.data
.chunks_exact(4)
.flat_map(|px| [px[0], px[1], px[2]])
.collect();
let allocation = Arc::as_ptr(&surface.data);
let mut parked = None;
let (stride, pixels) = surface.into_packed_rgb(&mut parked);
assert_eq!(stride, w * 3);
assert_eq!(Arc::as_ptr(&pixels), allocation, "no second buffer");
assert_eq!(&pixels[..w * h * 3], &expected[..]);
drop(pixels);
assert!(reuse(parked, w * h * 4).is_some());
}
#[test]
fn clone_remains_an_independent_pixel_snapshot() {
let original = Surface::new(
1,
1,
Background::Solid(crate::page_ir::Color {
r: 0.25,
g: 0.5,
b: 0.75,
a: 1.0,
}),
);
let mut clone = original.clone();
clone.row_mut(0).fill(0);
assert_ne!(original.local_row(0), clone.local_row(0));
assert_eq!(original.local_row(0), &[64, 128, 191, 255]);
}
#[test]
fn copying_a_snapshot_reuses_the_destination_allocation() {
let source = Surface::new(4, 3, Background::White);
let mut scratch = Surface::new(4, 3, Background::Transparent);
let allocation = Arc::as_ptr(&scratch.data);
scratch.copy_pixels_from(&source);
assert_eq!(Arc::as_ptr(&scratch.data), allocation);
assert_eq!(scratch.data, source.data);
}
#[test]
fn gray_output_is_fully_initialized() {
let surface = Surface::new(2, 1, Background::White);
let (stride, output) = surface.into_output(OutputFormat::Gray8);
assert_eq!(stride, 2);
assert_eq!(&*output, &[255, 255]);
}
}