⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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:** Remove generic type parameters `D` and `W` from `Buffer<'_>` struct.

# 0.4.7

- Fix documentation building on `docs.rs`.
Expand Down
33 changes: 27 additions & 6 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,39 @@ fn buffer_mut(c: &mut criterion::Criterion) {

c.bench_function("buffer_mut()", |b| {
b.iter(|| {
for _ in 0..500 {
black_box(surface.buffer_mut().unwrap());
}
black_box(surface.buffer_mut().unwrap());
});
});

c.bench_function("pixels_mut()", |b| {
let mut buffer = surface.buffer_mut().unwrap();
b.iter(|| {
for _ in 0..500 {
let x: &mut [u32] = &mut buffer;
black_box(x);
let pixels: &mut [u32] = &mut buffer;
black_box(pixels);
});
});

c.bench_function("fill", |b| {
let mut buffer = surface.buffer_mut().unwrap();
b.iter(|| {
let buffer = black_box(&mut buffer);
buffer.fill(0x00000000);
});
});

c.bench_function("render", |b| {
let mut buffer = surface.buffer_mut().unwrap();
b.iter(|| {
let buffer = black_box(&mut buffer);
let width = buffer.width().get();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
let red = (x & 0xff) ^ (y & 0xff);
let green = (x & 0x7f) ^ (y & 0x7f);
let blue = (x & 0x3f) ^ (y & 0x3f);
let value = blue | (green << 8) | (red << 16);
buffer[(y * width + x) as usize] = value;
}
}
});
});
Expand Down
19 changes: 3 additions & 16 deletions examples/raytracing/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::time::{Duration, Instant};

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use softbuffer::Buffer;

use crate::camera::Camera;
Expand Down Expand Up @@ -44,21 +43,13 @@ impl Game {
}
}

pub fn draw(
&self,
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
scale_factor: f32,
) {
pub fn draw(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
self.draw_scene(buffer, scale_factor);
self.draw_ui(buffer, scale_factor);
}

/// Draw the 3D scene.
fn draw_scene(
&self,
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
scale_factor: f32,
) {
fn draw_scene(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
// Raytracing is expensive, so we only do it once every 4x4 pixel.
//
// FIXME(madsmtm): Avoid the need for this once we can do hardware scaling.
Expand Down Expand Up @@ -130,11 +121,7 @@ impl Game {
}

/// Draw a simple example UI on top of the scene.
fn draw_ui(
&self,
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
scale_factor: f32,
) {
fn draw_ui(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
struct Rect {
left: f32,
right: f32,
Expand Down
3 changes: 1 addition & 2 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use softbuffer::Buffer;
use std::num::NonZeroU32;
use winit::event::{ElementState, KeyEvent, WindowEvent};
Expand All @@ -7,7 +6,7 @@ use winit::keyboard::{Key, NamedKey};

mod util;

fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>, flag: bool) {
fn redraw(buffer: &mut Buffer<'_>, flag: bool) {
let width = buffer.width().get();
let height = buffer.height().get();
for y in 0..height {
Expand Down
26 changes: 13 additions & 13 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ macro_rules! make_dispatch {

impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for SurfaceDispatch<D, W> {
type Context = ContextDispatch<D>;
type Buffer<'a> = BufferDispatch<'a, D, W> where Self: 'a;
type Buffer<'a> = BufferDispatch<'a> where Self: 'a;

fn new(window: W, display: &Self::Context) -> Result<Self, InitError<W>>
where
Expand Down Expand Up @@ -108,7 +108,7 @@ macro_rules! make_dispatch {
}
}

fn buffer_mut(&mut self) -> Result<BufferDispatch<'_, D, W>, SoftBufferError> {
fn buffer_mut(&mut self) -> Result<BufferDispatch<'_>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -138,14 +138,14 @@ macro_rules! make_dispatch {
}
}

pub(crate) enum BufferDispatch<'a, $dgen, $wgen> {
pub(crate) enum BufferDispatch<'a> {
$(
$(#[$attr])*
$name($buffer_inner),
)*
}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
impl BufferInterface for BufferDispatch<'_> {
#[inline]
fn width(&self) -> NonZeroU32 {
match self {
Expand Down Expand Up @@ -214,7 +214,7 @@ macro_rules! make_dispatch {
}
}

impl<D: fmt::Debug, W: fmt::Debug> fmt::Debug for BufferDispatch<'_, D, W> {
impl fmt::Debug for BufferDispatch<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
$(
Expand All @@ -232,7 +232,7 @@ macro_rules! make_dispatch {
make_dispatch! {
<D, W> =>
#[cfg(target_os = "android")]
Android(D, backends::android::AndroidImpl<D, W>, backends::android::BufferImpl<'a, D, W>),
Android(D, backends::android::AndroidImpl<D, W>, backends::android::BufferImpl<'a>),
#[cfg(all(
feature = "x11",
not(any(
Expand All @@ -243,7 +243,7 @@ make_dispatch! {
target_os = "windows"
))
))]
X11(std::sync::Arc<backends::x11::X11DisplayImpl<D>>, backends::x11::X11Impl<D, W>, backends::x11::BufferImpl<'a, D, W>),
X11(std::sync::Arc<backends::x11::X11DisplayImpl<D>>, backends::x11::X11Impl<D, W>, backends::x11::BufferImpl<'a>),
#[cfg(all(
feature = "wayland",
not(any(
Expand All @@ -254,7 +254,7 @@ make_dispatch! {
target_os = "windows"
))
))]
Wayland(std::sync::Arc<backends::wayland::WaylandDisplayImpl<D>>, backends::wayland::WaylandImpl<D, W>, backends::wayland::BufferImpl<'a, D, W>),
Wayland(std::sync::Arc<backends::wayland::WaylandDisplayImpl<D>>, backends::wayland::WaylandImpl<D, W>, backends::wayland::BufferImpl<'a>),
#[cfg(all(
feature = "kms",
not(any(
Expand All @@ -265,13 +265,13 @@ make_dispatch! {
target_os = "windows"
))
))]
Kms(std::sync::Arc<backends::kms::KmsDisplayImpl<D>>, backends::kms::KmsImpl<D, W>, backends::kms::BufferImpl<'a, D, W>),
Kms(std::sync::Arc<backends::kms::KmsDisplayImpl<D>>, backends::kms::KmsImpl<D, W>, backends::kms::BufferImpl<'a>),
#[cfg(target_os = "windows")]
Win32(D, backends::win32::Win32Impl<D, W>, backends::win32::BufferImpl<'a, D, W>),
Win32(D, backends::win32::Win32Impl<D, W>, backends::win32::BufferImpl<'a>),
#[cfg(target_vendor = "apple")]
CoreGraphics(D, backends::cg::CGImpl<D, W>, backends::cg::BufferImpl<'a, D, W>),
CoreGraphics(D, backends::cg::CGImpl<D, W>, backends::cg::BufferImpl<'a>),
#[cfg(target_family = "wasm")]
Web(backends::web::WebDisplayImpl<D>, backends::web::WebImpl<D, W>, backends::web::BufferImpl<'a, D, W>),
Web(backends::web::WebDisplayImpl<D>, backends::web::WebImpl<D, W>, backends::web::BufferImpl<'a>),
#[cfg(target_os = "redox")]
Orbital(D, backends::orbital::OrbitalImpl<D, W>, backends::orbital::BufferImpl<'a, D, W>),
Orbital(D, backends::orbital::OrbitalImpl<D, W>, backends::orbital::BufferImpl<'a>),
}
12 changes: 5 additions & 7 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct AndroidImpl<D, W> {
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for AndroidImpl<D, W> {
type Context = D;
type Buffer<'a>
= BufferImpl<'a, D, W>
= BufferImpl<'a>
where
Self: 'a;

Expand Down Expand Up @@ -76,7 +76,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
})
}

fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
let native_window_buffer = self.native_window.lock(None).map_err(|err| {
SoftBufferError::PlatformError(
Some("Failed to lock ANativeWindow".to_owned()),
Expand Down Expand Up @@ -104,7 +104,6 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
Ok(BufferImpl {
native_window_buffer,
buffer: util::PixelBuffer(buffer),
marker: PhantomData,
})
}

Expand All @@ -115,16 +114,15 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
}

#[derive(Debug)]
pub struct BufferImpl<'a, D: ?Sized, W> {
pub struct BufferImpl<'a> {
native_window_buffer: NativeWindowBufferLockGuard<'a>,
buffer: util::PixelBuffer,
marker: PhantomData<(&'a D, &'a W)>,
}

// TODO: Move to NativeWindowBufferLockGuard?
unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {}
unsafe impl Send for BufferImpl<'_> {}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> {
impl BufferInterface for BufferImpl<'_> {
fn width(&self) -> NonZeroU32 {
NonZeroU32::new(self.native_window_buffer.width() as u32).unwrap()
}
Expand Down
32 changes: 19 additions & 13 deletions src/backends/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<D, W> Drop for CGImpl<D, W> {
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<D, W> {
type Context = D;
type Buffer<'a>
= BufferImpl<'a, D, W>
= BufferImpl<'a>
where
Self: 'a;

Expand Down Expand Up @@ -258,27 +258,33 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
Ok(())
}

fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
Ok(BufferImpl {
buffer: util::PixelBuffer(vec![0; self.width * self.height]),
imp: self,
width: self.width,
height: self.height,
color_space: &self.color_space,
layer: &mut self.layer,
})
}
}

#[derive(Debug)]
pub struct BufferImpl<'a, D, W> {
imp: &'a mut CGImpl<D, W>,
pub struct BufferImpl<'a> {
width: usize,
height: usize,
color_space: &'a CGColorSpace,
buffer: util::PixelBuffer,
layer: &'a mut SendCALayer,
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
impl BufferInterface for BufferImpl<'_> {
fn width(&self) -> NonZeroU32 {
NonZeroU32::new(self.imp.width as u32).unwrap()
NonZeroU32::new(self.width as u32).unwrap()
}

fn height(&self) -> NonZeroU32 {
NonZeroU32::new(self.imp.height as u32).unwrap()
NonZeroU32::new(self.height as u32).unwrap()
}

#[inline]
Expand Down Expand Up @@ -333,12 +339,12 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_,

let image = unsafe {
CGImage::new(
self.imp.width,
self.imp.height,
self.width,
self.height,
8,
32,
self.imp.width * 4,
Some(&self.imp.color_space),
self.width * 4,
Some(self.color_space),
bitmap_info,
Some(&data_provider),
ptr::null(),
Expand All @@ -355,7 +361,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_,
CATransaction::setDisableActions(true);

// SAFETY: The contents is `CGImage`, which is a valid class for `contents`.
unsafe { self.imp.layer.setContents(Some(image.as_ref())) };
unsafe { self.layer.setContents(Some(image.as_ref())) };

CATransaction::commit();
Ok(())
Expand Down
Loading
Loading