Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- **Breaking:** Add `Pixel` struct, and use that for pixels instead of `u32`.

- Update to `objc2` 0.6.0.
- Bump MSRV to Rust 1.71.
- Make `Context` cloneable.
Expand Down
13 changes: 2 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ harness = false

[features]
default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"]
kms = ["bytemuck", "drm", "rustix"]
kms = ["drm", "rustix"]
wayland = [
"wayland-backend",
"wayland-client",
Expand All @@ -28,14 +28,7 @@ wayland = [
"fastrand",
]
wayland-dlopen = ["wayland-sys/dlopen"]
x11 = [
"as-raw-xcb-connection",
"bytemuck",
"fastrand",
"rustix",
"tiny-xlib",
"x11rb",
]
x11 = ["as-raw-xcb-connection", "fastrand", "rustix", "tiny-xlib", "x11rb"]
x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]

[dependencies]
Expand All @@ -45,12 +38,10 @@ raw_window_handle = { package = "raw-window-handle", version = "0.6", features =
tracing = { version = "0.1.41", default-features = false }

[target.'cfg(target_os = "android")'.dependencies]
bytemuck = "1.12.3"
ndk = "0.9.0"

[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
as-raw-xcb-connection = { version = "1.0.0", optional = true }
bytemuck = { version = "1.12.3", optional = true }
drm = { version = "0.14.1", default-features = false, optional = true }
fastrand = { version = "2.0.0", optional = true }
memmap2 = { version = "0.9.0", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ fn main() {
for index in 0..(buffer.width().get() * buffer.height().get()) {
let y = index / buffer.width().get();
let x = index % buffer.width().get();
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let red = (x % 255) as u8;
let green = (y % 255) as u8;
let blue = ((x * y) % 255) as u8;

buffer[index as usize] = blue | (green << 8) | (red << 16);
buffer[index as usize] = softbuffer::Pixel::new_rgb(red, green, blue);
}

buffer.present().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn buffer_mut(c: &mut Criterion) {
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
{
use criterion::black_box;
use softbuffer::{Context, Surface};
use softbuffer::{Context, Pixel, Surface};
use std::num::NonZeroU32;
use winit::event_loop::ControlFlow;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
Expand Down Expand Up @@ -51,7 +51,7 @@ fn buffer_mut(c: &mut Criterion) {
let mut buffer = surface.buffer_mut().unwrap();
b.iter(|| {
for _ in 0..500 {
let x: &mut [u32] = &mut buffer;
let x: &mut [Pixel] = &mut buffer;
black_box(x);
}
});
Expand Down
16 changes: 7 additions & 9 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use softbuffer::Pixel;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use web_time::Instant;
Expand Down Expand Up @@ -94,7 +95,7 @@ fn main() {
winit_app::run_app(event_loop, app);
}

fn pre_render_frames(width: u32, height: u32) -> Vec<Vec<u32>> {
fn pre_render_frames(width: u32, height: u32) -> Vec<Vec<Pixel>> {
let render = |frame_id| {
let elapsed = ((frame_id as f64) / (60.0)) * 2.0 * PI;

Expand All @@ -103,14 +104,11 @@ fn pre_render_frames(width: u32, height: u32) -> Vec<Vec<u32>> {
.map(|(x, y)| {
let y = (y as f64) / (height as f64);
let x = (x as f64) / (width as f64);
let red =
((((y + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let green =
((((x + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let blue =
((((y - elapsed).cos() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);

blue | (green << 8) | (red << 16)
let r = ((((y + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let g = ((((x + elapsed).sin() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);
let b = ((((y - elapsed).cos() * 0.5 + 0.5) * 255.0).round() as u32).clamp(0, 255);

Pixel::new_rgb(r as u8, g as u8, b as u8)
})
.collect::<Vec<_>>()
};
Expand Down
7 changes: 3 additions & 4 deletions examples/drm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod imple {
use drm::Device;

use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
use softbuffer::{Context, Surface};
use softbuffer::{Context, Pixel, Surface};

use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
Expand Down Expand Up @@ -150,16 +150,15 @@ mod imple {
Ok(())
}

fn draw_to_buffer(buf: &mut [u32], tick: usize) {
fn draw_to_buffer(buf: &mut [Pixel], tick: usize) {
let scale = colorous::SINEBOW;
let mut i = (tick as f64) / 20.0;
while i > 1.0 {
i -= 1.0;
}

let color = scale.eval_continuous(i);
let pixel = ((color.r as u32) << 16) | ((color.g as u32) << 8) | (color.b as u32);
buf.fill(pixel);
buf.fill(Pixel::new_rgb(color.r, color.g, color.b));
}

struct Card(std::fs::File);
Expand Down
9 changes: 3 additions & 6 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use image::GenericImageView;
use softbuffer::Pixel;
use std::num::NonZeroU32;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand Down Expand Up @@ -52,12 +53,8 @@ fn main() {
let mut buffer = surface.buffer_mut().unwrap();
let width = fruit.width();
for (x, y, pixel) in fruit.pixels() {
let red = pixel.0[0] as u32;
let green = pixel.0[1] as u32;
let blue = pixel.0[2] as u32;

let color = blue | (green << 8) | (red << 16);
buffer[(y * width + x) as usize] = color;
let pixel = Pixel::new_rgb(pixel.0[0], pixel.0[1], pixel.0[2]);
buffer[(y * width + x) as usize] = pixel;
}

buffer.present().unwrap();
Expand Down
4 changes: 1 addition & 3 deletions examples/libxcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ mod example {
xcb_ffi::XCBConnection,
};

const RED: u32 = 255 << 16;

pub(crate) fn run() {
// Create a new XCB connection
let (conn, screen) = XCBConnection::connect(None).expect("Failed to connect to X server");
Expand Down Expand Up @@ -113,7 +111,7 @@ mod example {
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(RED);
buffer.fill(softbuffer::Pixel::new_rgb(0xff, 0, 0));
buffer.present().unwrap();
}
Event::ConfigureNotify(configure_notify) => {
Expand Down
6 changes: 3 additions & 3 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use softbuffer::Buffer;
use softbuffer::{Buffer, Pixel};
use std::num::NonZeroU32;
use winit::event::{ElementState, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand All @@ -14,12 +14,12 @@ fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
for y in 0..height {
for x in 0..width {
let value = if flag && x >= 100 && x < width - 100 && y >= 100 && y < height - 100 {
0x00ffffff
Pixel::new_rgb(0xff, 0xff, 0xff)
} else {
let red = (x & 0xff) ^ (y & 0xff);
let green = (x & 0x7f) ^ (y & 0x7f);
let blue = (x & 0x3f) ^ (y & 0x3f);
blue | (green << 8) | (red << 16)
Pixel::new_rgb(red as u8, green as u8, blue as u8)
};
buffer[(y * width + x) as usize] = value;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let red = (x % 255) as u8;
let green = (y % 255) as u8;
let blue = ((x * y) % 255) as u8;
let index = y * buffer.width().get() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
buffer[index as usize] = softbuffer::Pixel::new_rgb(red, green, blue);
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/winit_multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub mod ex {
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let red = (x % 255) as u8;
let green = (y % 255) as u8;
let blue = ((x * y) % 255) as u8;
let index = y * buffer.width().get() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
buffer[index as usize] = softbuffer::Pixel::new_rgb(red, green, blue);
}
}

Expand Down
9 changes: 5 additions & 4 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use softbuffer::Pixel;
use std::num::NonZeroU32;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand Down Expand Up @@ -39,11 +40,11 @@ fn main() {
let width = buffer.width().get();
for y in 0..buffer.height().get() {
for x in 0..width {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let red = (x % 255) as u8;
let green = (y % 255) as u8;
let blue = ((x * y) % 255) as u8;

let color = blue | (green << 8) | (red << 16);
let color = Pixel::new_rgb(red, green, blue);
buffer[(y * width + x) as usize] = color;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Implements `buffer_interface::*` traits for enums dispatching to backends

use crate::{backend_interface::*, backends, InitError, Rect, SoftBufferError};
use crate::{backend_interface::*, backends, InitError, Pixel, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroU32;
Expand Down Expand Up @@ -107,7 +107,7 @@ macro_rules! make_dispatch {
}
}

fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
fn fetch(&mut self) -> Result<Vec<Pixel>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -146,7 +146,7 @@ macro_rules! make_dispatch {
}

#[inline]
fn pixels(&self) -> &[u32] {
fn pixels(&self) -> &[Pixel] {
match self {
$(
$(#[$attr])*
Expand All @@ -156,7 +156,7 @@ macro_rules! make_dispatch {
}

#[inline]
fn pixels_mut(&mut self) -> &mut [u32] {
fn pixels_mut(&mut self) -> &mut [Pixel] {
match self {
$(
$(#[$attr])*
Expand Down
8 changes: 4 additions & 4 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Interface implemented by backends

use crate::{InitError, Rect, SoftBufferError};
use crate::{InitError, Pixel, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroU32;
Expand Down Expand Up @@ -29,16 +29,16 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
/// Get a mutable reference to the buffer.
fn buffer_mut(&mut self) -> Result<Self::Buffer<'_>, SoftBufferError>;
/// Fetch the buffer from the window.
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
fn fetch(&mut self) -> Result<Vec<Pixel>, SoftBufferError> {
Err(SoftBufferError::Unimplemented)
}
}

pub(crate) trait BufferInterface {
fn width(&self) -> NonZeroU32;
fn height(&self) -> NonZeroU32;
fn pixels(&self) -> &[u32];
fn pixels_mut(&mut self) -> &mut [u32];
fn pixels(&self) -> &[Pixel];
fn pixels_mut(&mut self) -> &mut [Pixel];
fn age(&self) -> u8;
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError>;
fn present(self) -> Result<(), SoftBufferError>;
Expand Down
25 changes: 13 additions & 12 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use raw_window_handle::AndroidNdkWindowHandle;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};

use crate::error::InitError;
use crate::{BufferInterface, Rect, SoftBufferError, SurfaceInterface};
use crate::{BufferInterface, Pixel, Rect, SoftBufferError, SurfaceInterface};

/// The handle to a window for software buffering.
pub struct AndroidImpl<D, W> {
Expand Down Expand Up @@ -98,7 +98,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
));
}

let buffer = vec![0; native_window_buffer.width() * native_window_buffer.height()];
let buffer =
vec![Pixel::default(); native_window_buffer.width() * native_window_buffer.height()];

Ok(BufferImpl {
native_window_buffer,
Expand All @@ -108,14 +109,14 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
}

/// Fetch the buffer from the window.
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
fn fetch(&mut self) -> Result<Vec<Pixel>, SoftBufferError> {
Err(SoftBufferError::Unimplemented)
}
}

pub struct BufferImpl<'a, D: ?Sized, W> {
native_window_buffer: NativeWindowBufferLockGuard<'a>,
buffer: Vec<u32>,
buffer: Vec<Pixel>,
marker: PhantomData<(&'a D, &'a W)>,
}

Expand All @@ -132,12 +133,12 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl
}

#[inline]
fn pixels(&self) -> &[u32] {
fn pixels(&self) -> &[Pixel] {
&self.buffer
}

#[inline]
fn pixels_mut(&mut self) -> &mut [u32] {
fn pixels_mut(&mut self) -> &mut [Pixel] {
&mut self.buffer
}

Expand All @@ -159,13 +160,13 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl
// .lines() removed the stride
assert_eq!(output.len(), input.len() * 4);

// Write RGB(A) to the output.
// TODO: Use `slice::write_copy_of_slice` once stable and in MSRV.
for (i, pixel) in input.iter().enumerate() {
// Swizzle colors from BGR(A) to RGB(A)
let [b, g, r, a] = pixel.to_le_bytes();
output[i * 4].write(r);
output[i * 4 + 1].write(g);
output[i * 4 + 2].write(b);
output[i * 4 + 3].write(a);
output[i * 4].write(pixel.r);
output[i * 4 + 1].write(pixel.g);
output[i * 4 + 2].write(pixel.b);
output[i * 4 + 3].write(pixel.a);
}
}
Ok(())
Expand Down
Loading
Loading