Skip to content

Commit 99d6306

Browse files
committed
Take a reference to a window in GraphicsContext::new, like glutin and wgpu
1 parent 85b4f18 commit 99d6306

File tree

13 files changed

+40
-78
lines changed

13 files changed

+40
-78
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ use winit::window::WindowBuilder;
6363
fn main() {
6464
let event_loop = EventLoop::new();
6565
let window = WindowBuilder::new().build(&event_loop).unwrap();
66-
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
66+
let mut graphics_context = unsafe { GraphicsContext::new(&window) }.unwrap();
6767
6868
event_loop.run(move |event, _, control_flow| {
6969
*control_flow = ControlFlow::Wait;
7070
7171
match event {
72-
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
72+
Event::RedrawRequested(window_id) if window_id == window.id() => {
7373
let (width, height) = {
74-
let size = graphics_context.window().inner_size();
74+
let size = window.inner_size();
7575
(size.width, size.height)
7676
};
7777
let buffer = (0..((width * height) as usize))
@@ -93,7 +93,7 @@ fn main() {
9393
Event::WindowEvent {
9494
event: WindowEvent::CloseRequested,
9595
window_id,
96-
} if window_id == graphics_context.window().id() => {
96+
} if window_id == window.id() => {
9797
*control_flow = ControlFlow::Exit;
9898
}
9999
_ => {}

examples/animation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main() {
2525
.unwrap();
2626
}
2727

28-
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
28+
let mut graphics_context = unsafe { GraphicsContext::new(&window) }.unwrap();
2929

3030
let mut old_size = (0, 0);
3131
let mut frames = pre_render_frames(0, 0);
@@ -35,10 +35,10 @@ fn main() {
3535
*control_flow = ControlFlow::Poll;
3636

3737
match event {
38-
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
38+
Event::RedrawRequested(window_id) if window_id == window.id() => {
3939
let elapsed = start.elapsed().as_secs_f64() % 1.0;
4040
let (width, height) = {
41-
let size = graphics_context.window().inner_size();
41+
let size = window.inner_size();
4242
(size.width, size.height)
4343
};
4444

@@ -51,12 +51,12 @@ fn main() {
5151
graphics_context.set_buffer(buffer.as_slice(), width as u16, height as u16);
5252
}
5353
Event::MainEventsCleared => {
54-
graphics_context.window().request_redraw();
54+
window.request_redraw();
5555
}
5656
Event::WindowEvent {
5757
event: WindowEvent::CloseRequested,
5858
window_id,
59-
} if window_id == graphics_context.window().id() => {
59+
} if window_id == window.id() => {
6060
*control_flow = ControlFlow::Exit;
6161
}
6262
_ => {}
@@ -89,4 +89,4 @@ fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
8989

9090
#[cfg(not(target_arch = "wasm32"))]
9191
(0..60).into_par_iter().map(render).collect()
92-
}
92+
}

examples/fruit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ fn main() {
3232
.unwrap();
3333
}
3434

35-
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
35+
let mut graphics_context = unsafe { GraphicsContext::new(&window) }.unwrap();
3636

3737
event_loop.run(move |event, _, control_flow| {
3838
*control_flow = ControlFlow::Wait;
3939

4040
match event {
41-
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
41+
Event::RedrawRequested(window_id) if window_id == window.id() => {
4242
graphics_context.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
4343
}
4444
Event::WindowEvent {
4545
event: WindowEvent::CloseRequested,
4646
window_id,
47-
} if window_id == graphics_context.window().id() => {
47+
} if window_id == window.id() => {
4848
*control_flow = ControlFlow::Exit;
4949
}
5050
_ => {}

examples/winit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ fn main() {
2121
.unwrap();
2222
}
2323

24-
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
24+
let mut graphics_context = unsafe { GraphicsContext::new(&window) }.unwrap();
2525

2626
event_loop.run(move |event, _, control_flow| {
2727
*control_flow = ControlFlow::Wait;
2828

2929
match event {
30-
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
30+
Event::RedrawRequested(window_id) if window_id == window.id() => {
3131
let (width, height) = {
32-
let size = graphics_context.window().inner_size();
32+
let size = window.inner_size();
3333
(size.width, size.height)
3434
};
3535
let buffer = (0..((width * height) as usize))
@@ -51,7 +51,7 @@ fn main() {
5151
Event::WindowEvent {
5252
event: WindowEvent::CloseRequested,
5353
window_id,
54-
} if window_id == graphics_context.window().id() => {
54+
} if window_id == window.id() => {
5555
*control_flow = ControlFlow::Exit;
5656
}
5757
_ => {}

examples/winit_wrong_sized_buffer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ fn main() {
2424
.unwrap();
2525
}
2626

27-
let mut graphics_context = unsafe { GraphicsContext::new(window) }.unwrap();
27+
let mut graphics_context = unsafe { GraphicsContext::new(&window) }.unwrap();
2828

2929
event_loop.run(move |event, _, control_flow| {
3030
*control_flow = ControlFlow::Wait;
3131

3232
match event {
33-
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
33+
Event::RedrawRequested(window_id) if window_id == window.id() => {
3434
let buffer = (0..((BUFFER_WIDTH * BUFFER_HEIGHT) as usize))
3535
.map(|index| {
3636
let y = index / (BUFFER_WIDTH as usize);
@@ -50,7 +50,7 @@ fn main() {
5050
Event::WindowEvent {
5151
event: WindowEvent::CloseRequested,
5252
window_id,
53-
} if window_id == graphics_context.window().id() => {
53+
} if window_id == window.id() => {
5454
*control_flow = ControlFlow::Exit;
5555
}
5656
_ => {}

src/cg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{GraphicsContextImpl, SwBufError};
2-
use raw_window_handle::{HasRawWindowHandle, AppKitWindowHandle};
2+
use raw_window_handle::AppKitWindowHandle;
33
use core_graphics::base::{kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault};
44
use core_graphics::color_space::CGColorSpace;
55
use core_graphics::data_provider::CGDataProvider;
@@ -17,7 +17,7 @@ pub struct CGImpl {
1717
}
1818

1919
impl CGImpl {
20-
pub unsafe fn new<W: HasRawWindowHandle>(handle: AppKitWindowHandle) -> Result<Self, SwBufError<W>> {
20+
pub unsafe fn new(handle: AppKitWindowHandle) -> Result<Self, SwBufError> {
2121
let view = handle.ns_view as id;
2222
let layer = CALayer::new();
2323
let subview: id = NSView::alloc(nil).initWithFrame_(view.frame());

src/error.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::error::Error;
2-
use raw_window_handle::{HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
2+
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
33
use thiserror::Error;
44

55
#[derive(Error, Debug)]
6-
pub enum SwBufError<W: HasRawWindowHandle> {
6+
pub enum SwBufError {
77
#[error(
88
"The provided window returned an unsupported platform: {human_readable_window_platform_name}, {human_readable_display_platform_name}."
99
)]
1010
UnsupportedPlatform {
11-
window: W,
1211
human_readable_window_platform_name: &'static str,
1312
human_readable_display_platform_name: &'static str,
1413
window_handle: RawWindowHandle,
@@ -19,9 +18,9 @@ pub enum SwBufError<W: HasRawWindowHandle> {
1918
}
2019

2120
#[allow(unused)] // This isn't used on all platforms
22-
pub(crate) fn unwrap<T, E: std::error::Error + 'static, W: HasRawWindowHandle>(res: Result<T, E>, str: &str) -> Result<T, SwBufError<W>>{
21+
pub(crate) fn unwrap<T, E: std::error::Error + 'static>(res: Result<T, E>, str: &str) -> Result<T, SwBufError>{
2322
match res{
2423
Ok(t) => Ok(t),
2524
Err(e) => Err(SwBufError::PlatformError(Some(str.into()), Some(Box::new(e))))
2625
}
27-
}
26+
}

src/lib.rs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandl
2828
/// write to a window on that platform. This struct owns the window that this data corresponds to
2929
/// to ensure safety, as that data must be destroyed before the window itself is destroyed. You may
3030
/// access the underlying window via [`window`](Self::window) and [`window_mut`](Self::window_mut).
31-
pub struct GraphicsContext<W: HasRawWindowHandle + HasRawDisplayHandle> {
32-
window: W,
31+
pub struct GraphicsContext {
3332
graphics_context_impl: Box<dyn GraphicsContextImpl>,
3433
}
3534

36-
impl<W: HasRawWindowHandle + HasRawDisplayHandle> GraphicsContext<W> {
35+
impl GraphicsContext {
3736
/// Creates a new instance of this struct, consuming the given window.
3837
///
3938
/// # Safety
4039
///
41-
/// - Ensure that the passed object is valid to draw a 2D buffer to
42-
pub unsafe fn new(window: W) -> Result<Self, SwBufError<W>> {
40+
/// - Ensure that the passed object is valid to draw a 2D buffer to, and is valid for the
41+
/// lifetime of the GraphicsContext
42+
pub unsafe fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(window: &W) -> Result<Self, SwBufError> {
4343
let raw_window_handle = window.raw_window_handle();
4444
let raw_display_handle = window.raw_display_handle();
4545

@@ -57,7 +57,6 @@ impl<W: HasRawWindowHandle + HasRawDisplayHandle> GraphicsContext<W> {
5757
#[cfg(target_os = "redox")]
5858
(RawWindowHandle::Orbital(orbital_handle), _) => Box::new(orbital::OrbitalImpl::new(orbital_handle)?),
5959
(unimplemented_window_handle, unimplemented_display_handle) => return Err(SwBufError::UnsupportedPlatform {
60-
window,
6160
human_readable_window_platform_name: window_handle_type_name(&unimplemented_window_handle),
6261
human_readable_display_platform_name: display_handle_type_name(&unimplemented_display_handle),
6362
window_handle: unimplemented_window_handle,
@@ -66,36 +65,10 @@ impl<W: HasRawWindowHandle + HasRawDisplayHandle> GraphicsContext<W> {
6665
};
6766

6867
Ok(Self {
69-
window,
7068
graphics_context_impl: imple,
7169
})
7270
}
7371

74-
/// Gets shared access to the underlying window.
75-
#[inline]
76-
pub fn window(&self) -> &W {
77-
&self.window
78-
}
79-
80-
/// Gets mut/exclusive access to the underlying window.
81-
///
82-
/// This method is `unsafe` because it could be used to replace the window with another one,
83-
/// thus dropping the original window and violating the property that this [`GraphicsContext`]
84-
/// will always be destroyed before the window it writes into. This method should only be used
85-
/// when the window type in use requires mutable access to perform some action on an existing
86-
/// window.
87-
///
88-
/// # Safety
89-
///
90-
/// - After the returned mutable reference is dropped, the window must still be the same window
91-
/// which this [`GraphicsContext`] was created for; and within that window, the
92-
/// platform-specific configuration for 2D drawing must not have been modified. (For example,
93-
/// on macOS the view hierarchy of the window must not have been modified.)
94-
#[inline]
95-
pub unsafe fn window_mut(&mut self) -> &mut W {
96-
&mut self.window
97-
}
98-
9972
/// Shows the given buffer with the given width and height on the window corresponding to this
10073
/// graphics context. Panics if buffer.len() ≠ width*height. If the size of the buffer does
10174
/// not match the size of the window, the buffer is drawn in the upper-left corner of the window.
@@ -131,14 +104,6 @@ impl<W: HasRawWindowHandle + HasRawDisplayHandle> GraphicsContext<W> {
131104
}
132105
}
133106

134-
impl<W: HasRawWindowHandle + HasRawDisplayHandle> AsRef<W> for GraphicsContext<W> {
135-
/// Equivalent to [`self.window()`](Self::window()).
136-
#[inline]
137-
fn as_ref(&self) -> &W {
138-
self.window()
139-
}
140-
}
141-
142107
trait GraphicsContextImpl {
143108
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16);
144109
}

src/orbital.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use raw_window_handle::HasRawWindowHandle;
21
use raw_window_handle::OrbitalWindowHandle;
32
use std::{
43
cmp,
@@ -47,7 +46,7 @@ pub struct OrbitalImpl {
4746
}
4847

4948
impl OrbitalImpl {
50-
pub fn new<W: HasRawWindowHandle>(handle: OrbitalWindowHandle) -> Result<Self, SwBufError<W>> {
49+
pub fn new(handle: OrbitalWindowHandle) -> Result<Self, SwBufError> {
5150
Ok(Self { handle })
5251
}
5352
}

src/wayland.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{error::unwrap, GraphicsContextImpl, SwBufError};
22
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
3-
use raw_window_handle::{HasRawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle};
3+
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
44
use std::{
55
ffi::CStr,
66
fs::File,
@@ -40,10 +40,10 @@ impl Drop for WaylandBuffer {
4040
}
4141

4242
impl WaylandImpl {
43-
pub unsafe fn new<W: HasRawWindowHandle>(
43+
pub unsafe fn new(
4444
window_handle: WaylandWindowHandle,
4545
display_handle: WaylandDisplayHandle,
46-
) -> Result<Self, SwBufError<W>> {
46+
) -> Result<Self, SwBufError> {
4747
let conn = Connection::from_backend(Backend::from_foreign_display(
4848
display_handle.display as *mut _,
4949
));

0 commit comments

Comments
 (0)