⚠ 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
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ redox_syscall = "0.5"

[dev-dependencies]
colorous = "1.0.12"
criterion = { version = "0.8.1", default-features = false, features = [
"cargo_bench_support",
] }
web-time = "1.0.0"
winit = "0.30.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[target.'cfg(target_os = "android")'.dev-dependencies]
winit = { version = "0.30.0", features = ["android-native-activity"] }
Expand All @@ -151,9 +149,14 @@ features = ["jpeg"]
[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
# Turn rayon back on everywhere else; creating the separate entry resets the features to default.
rayon = "1.5.1"
criterion = { version = "0.8.1", default-features = false, features = [
"cargo_bench_support",
] }

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = "0.3"
console_error_panic_hook = "0.1"
tracing-web = "0.1"

[target.'cfg(not(any(target_os = "android", target_vendor = "apple", target_os = "redox", target_family = "wasm", target_os = "windows")))'.dev-dependencies]
rustix = { version = "1.0.1", features = ["event"] }
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::Window;

#[path = "../examples/utils/winit_app.rs"]
mod winit_app;
#[path = "../examples/util/mod.rs"]
mod util;

fn main() {
let event_loop = EventLoop::new().unwrap();
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();

let mut app = winit_app::WinitAppBuilder::with_init(
let mut app = util::WinitAppBuilder::with_init(
|elwt| {
let window = elwt.create_window(Window::default_attributes());
Rc::new(window.unwrap())
Expand All @@ -98,7 +98,7 @@ fn main() {
match event {
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
tracing::error!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let size = window.inner_size();
Expand Down
120 changes: 67 additions & 53 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
#![allow(deprecated)] // TODO

use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(not(any(
target_family = "wasm",
all(target_vendor = "apple", not(target_os = "macos")),
target_os = "redox"
)))]
fn buffer_mut(c: &mut criterion::Criterion) {
use criterion::black_box;
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use winit::event_loop::ControlFlow;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;

fn buffer_mut(c: &mut Criterion) {
#[cfg(target_family = "wasm")]
{
// Do nothing.
let _ = c;
}

#[cfg(not(target_family = "wasm"))]
{
use criterion::black_box;
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use winit::event_loop::ControlFlow;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;

let mut evl = winit::event_loop::EventLoop::new().unwrap();
let context = Context::new(evl.owned_display_handle()).unwrap();
let window = evl
.create_window(winit::window::Window::default_attributes().with_visible(false))
.unwrap();
let mut evl = winit::event_loop::EventLoop::new().unwrap();
let context = Context::new(evl.owned_display_handle()).unwrap();
let window = evl
.create_window(winit::window::Window::default_attributes().with_visible(false))
.unwrap();

evl.run_on_demand(move |ev, elwt| {
elwt.set_control_flow(ControlFlow::Poll);
evl.run_on_demand(move |ev, elwt| {
elwt.set_control_flow(ControlFlow::Poll);

if let winit::event::Event::AboutToWait = ev {
elwt.exit();
if let winit::event::Event::AboutToWait = ev {
elwt.exit();

let mut surface = Surface::new(&context, &window).unwrap();
let mut surface = Surface::new(&context, &window).unwrap();

let size = window.inner_size();
surface
.resize(
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();
let size = window.inner_size();
surface
.resize(
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();

c.bench_function("buffer_mut()", |b| {
b.iter(|| {
for _ in 0..500 {
black_box(surface.buffer_mut().unwrap());
}
});
c.bench_function("buffer_mut()", |b| {
b.iter(|| {
for _ in 0..500 {
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);
}
});
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);
}
});
}
})
.unwrap();
}
});
}
})
.unwrap();
}

criterion_group!(benches, buffer_mut);
criterion_main!(benches);
#[cfg(not(any(
target_family = "wasm",
all(target_vendor = "apple", not(target_os = "macos")),
target_os = "redox"
)))]
criterion::criterion_group!(benches, buffer_mut);

#[cfg(not(any(
target_family = "wasm",
all(target_vendor = "apple", not(target_os = "macos")),
target_os = "redox"
)))]
criterion::criterion_main!(benches);

#[cfg(any(
target_family = "wasm",
all(target_vendor = "apple", not(target_os = "macos")),
target_os = "redox"
))]
fn main() {
panic!("unsupported on WASM, iOS and Redox");
}
6 changes: 6 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
disallowed-macros = [
{ path = "std::print", reason = "use tracing in examples instead, that works on WASM too" },
{ path = "std::eprint", reason = "use tracing in examples instead, that works on WASM too" },
{ path = "std::println", reason = "use tracing in examples instead, that works on WASM too" },
{ path = "std::eprintln", reason = "use tracing in examples instead, that works on WASM too" },
]
15 changes: 8 additions & 7 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};

#[path = "utils/winit_app.rs"]
mod winit_app;
mod util;

fn main() {
util::setup();

let event_loop = EventLoop::new().unwrap();
let start = Instant::now();

let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();

let app = winit_app::WinitAppBuilder::with_init(
let app = util::WinitAppBuilder::with_init(
|event_loop| {
let window = winit_app::make_window(event_loop, |w| w);
let window = util::make_window(event_loop, |w| w);

let old_size = (0, 0);
let frames = pre_render_frames(0, 0);
Expand All @@ -41,7 +42,7 @@ fn main() {
match event {
WindowEvent::Resized(size) => {
let Some(surface) = surface else {
eprintln!("Resized fired before Resumed or after Suspended");
tracing::error!("Resized fired before Resumed or after Suspended");
return;
};

Expand All @@ -53,7 +54,7 @@ fn main() {
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
tracing::error!("RedrawRequested fired before Resumed or after Suspended");
return;
};

Expand Down Expand Up @@ -91,7 +92,7 @@ fn main() {
window.request_redraw();
});

winit_app::run_app(event_loop, app);
util::run_app(event_loop, app);
}

fn pre_render_frames(width: u32, height: u32) -> Vec<Vec<u32>> {
Expand Down
17 changes: 10 additions & 7 deletions examples/drm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Example of using softbuffer with drm-rs.

mod util;

#[cfg(all(
feature = "kms",
not(any(
Expand Down Expand Up @@ -122,7 +124,7 @@ mod imple {
let mut tick = 0;
while Instant::now().duration_since(start) < Duration::from_secs(2) {
tick += 1;
println!("Drawing tick {tick}");
tracing::info!("Drawing tick {tick}");

// Start drawing.
let mut buffer = surface.buffer_mut()?;
Expand All @@ -140,17 +142,17 @@ mod imple {

// Receive the events.
let events = device.receive_events()?;
println!("Got some events...");
tracing::info!("Got some events...");
for event in events {
match event {
Event::PageFlip(_) => {
println!("Page flip event.");
tracing::info!("Page flip event.");
}
Event::Vblank(_) => {
println!("Vblank event.");
tracing::info!("Vblank event.");
}
_ => {
println!("Unknown event.");
tracing::info!("Unknown event.");
}
}
}
Expand Down Expand Up @@ -231,11 +233,12 @@ mod imple {
)))]
mod imple {
pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("This example requires the `kms` feature.");
Ok(())
panic!("This example requires the `kms` feature.")
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
util::setup();

imple::entry()
}
13 changes: 7 additions & 6 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};

#[path = "utils/winit_app.rs"]
mod winit_app;
mod util;

fn main() {
util::setup();

//see fruit.jpg.license for the license of fruit.jpg
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();
let (width, height) = (fruit.width(), fruit.height());

let event_loop = EventLoop::new().unwrap();
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();

let app = winit_app::WinitAppBuilder::with_init(
let app = util::WinitAppBuilder::with_init(
move |elwt| {
winit_app::make_window(elwt, |w| {
util::make_window(elwt, |w| {
w.with_inner_size(winit::dpi::PhysicalSize::new(width, height))
})
},
Expand Down Expand Up @@ -45,7 +46,7 @@ fn main() {
match event {
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
tracing::error!("RedrawRequested fired before Resumed or after Suspended");
return;
};

Expand Down Expand Up @@ -77,5 +78,5 @@ fn main() {
}
});

winit_app::run_app(event_loop, app);
util::run_app(event_loop, app);
}
8 changes: 7 additions & 1 deletion examples/libxcb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Example of using `softbuffer` with `libxcb`.

mod util;

#[cfg(all(
feature = "x11",
not(any(
Expand Down Expand Up @@ -155,6 +157,8 @@ mod example {
))
))]
fn main() {
util::setup();

example::run();
}

Expand All @@ -169,5 +173,7 @@ fn main() {
))
)))]
fn main() {
eprintln!("This example requires the `x11` feature to be enabled on a supported platform.");
util::setup();

panic!("This example requires the `x11` feature to be enabled on a supported platform.")
}
Loading