Skip to content

Commit 81904ea

Browse files
authored
Implement Debug for Context, Surface and Buffer (#268)
* Implement Debug for all types * Make Debug impls not output pixel data
1 parent 7ef6372 commit 81904ea

File tree

13 files changed

+132
-19
lines changed

13 files changed

+132
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Make `Context` cloneable.
66
- Added `Buffer::width()` and `Buffer::height()` getters.
77
- Added support for `wasm64-*` targets.
8+
- `Context`, `Surface` and `Buffer` now implement `Debug`.
89

910
# 0.4.6
1011

src/backend_dispatch.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{backend_interface::*, backends, InitError, Rect, SoftBufferError};
44

55
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
6+
use std::fmt;
67
use std::num::NonZeroU32;
78

89
/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
@@ -54,6 +55,17 @@ macro_rules! make_dispatch {
5455
}
5556
}
5657

58+
impl<D: fmt::Debug> fmt::Debug for ContextDispatch<D> {
59+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60+
match self {
61+
$(
62+
$(#[$attr])*
63+
Self::$name(inner) => inner.fmt(f),
64+
)*
65+
}
66+
}
67+
}
68+
5769
#[allow(clippy::large_enum_variant)] // it's boxed anyways
5870
pub(crate) enum SurfaceDispatch<$dgen, $wgen> {
5971
$(
@@ -115,6 +127,17 @@ macro_rules! make_dispatch {
115127
}
116128
}
117129

130+
impl<D: fmt::Debug, W: fmt::Debug> fmt::Debug for SurfaceDispatch<D, W> {
131+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132+
match self {
133+
$(
134+
$(#[$attr])*
135+
Self::$name(inner) => inner.fmt(f),
136+
)*
137+
}
138+
}
139+
}
140+
118141
pub(crate) enum BufferDispatch<'a, $dgen, $wgen> {
119142
$(
120143
$(#[$attr])*
@@ -190,6 +213,17 @@ macro_rules! make_dispatch {
190213
}
191214
}
192215
}
216+
217+
impl<D: fmt::Debug, W: fmt::Debug> fmt::Debug for BufferDispatch<'_, D, W> {
218+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219+
match self {
220+
$(
221+
$(#[$attr])*
222+
Self::$name(inner) => inner.fmt(f),
223+
)*
224+
}
225+
}
226+
}
193227
};
194228
}
195229

src/backends/android.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use raw_window_handle::AndroidNdkWindowHandle;
1212
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};
1313

1414
use crate::error::InitError;
15-
use crate::{BufferInterface, Rect, SoftBufferError, SurfaceInterface};
15+
use crate::{util, BufferInterface, Rect, SoftBufferError, SurfaceInterface};
1616

1717
/// The handle to a window for software buffering.
18+
#[derive(Debug)]
1819
pub struct AndroidImpl<D, W> {
1920
native_window: NativeWindow,
2021
window: W,
@@ -102,7 +103,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
102103

103104
Ok(BufferImpl {
104105
native_window_buffer,
105-
buffer,
106+
buffer: util::PixelBuffer(buffer),
106107
marker: PhantomData,
107108
})
108109
}
@@ -113,9 +114,10 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
113114
}
114115
}
115116

117+
#[derive(Debug)]
116118
pub struct BufferImpl<'a, D: ?Sized, W> {
117119
native_window_buffer: NativeWindowBufferLockGuard<'a>,
118-
buffer: Vec<u32>,
120+
buffer: util::PixelBuffer,
119121
marker: PhantomData<(&'a D, &'a W)>,
120122
}
121123

src/backends/cg.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Softbuffer implementation using CoreGraphics.
22
use crate::backend_interface::*;
33
use crate::error::InitError;
4-
use crate::{Rect, SoftBufferError};
4+
use crate::{util, Rect, SoftBufferError};
55
use objc2::rc::Retained;
66
use objc2::runtime::{AnyObject, Bool};
77
use objc2::{define_class, msg_send, AllocAnyThread, DefinedClass, MainThreadMarker, Message};
@@ -29,6 +29,7 @@ define_class!(
2929
#[unsafe(super(NSObject))]
3030
#[name = "SoftbufferObserver"]
3131
#[ivars = SendCALayer]
32+
#[derive(Debug)]
3233
struct Observer;
3334

3435
/// NSKeyValueObserving
@@ -93,6 +94,7 @@ impl Observer {
9394
}
9495
}
9596

97+
#[derive(Debug)]
9698
pub struct CGImpl<D, W> {
9799
/// Our layer.
98100
layer: SendCALayer,
@@ -258,15 +260,16 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
258260

259261
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
260262
Ok(BufferImpl {
261-
buffer: vec![0; self.width * self.height].into(),
263+
buffer: util::PixelBuffer(vec![0; self.width * self.height]),
262264
imp: self,
263265
})
264266
}
265267
}
266268

269+
#[derive(Debug)]
267270
pub struct BufferImpl<'a, D, W> {
268271
imp: &'a mut CGImpl<D, W>,
269-
buffer: Box<[u32]>,
272+
buffer: util::PixelBuffer,
270273
}
271274

272275
impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
@@ -306,7 +309,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_,
306309

307310
let data_provider = {
308311
let len = self.buffer.len() * size_of::<u32>();
309-
let buffer: *mut [u32] = Box::into_raw(self.buffer);
312+
let buffer: *mut [u32] = Box::into_raw(self.buffer.0.into_boxed_slice());
310313
// Convert slice pointer to thin pointer.
311314
let data_ptr = buffer.cast::<c_void>();
312315

@@ -363,6 +366,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_,
363366
}
364367
}
365368

369+
#[derive(Debug)]
366370
struct SendCALayer(Retained<CALayer>);
367371

368372
// SAFETY: CALayer is dubiously thread safe, like most things in Core Animation.

src/backends/kms.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use drm::Device;
1212
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle};
1313

1414
use std::collections::HashSet;
15+
use std::fmt;
1516
use std::marker::PhantomData;
1617
use std::num::NonZeroU32;
1718
use std::os::unix::io::{AsFd, BorrowedFd};
@@ -118,6 +119,13 @@ pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
118119
_window: PhantomData<&'a mut W>,
119120
}
120121

122+
impl<D: ?Sized + fmt::Debug, W: ?Sized + fmt::Debug> fmt::Debug for BufferImpl<'_, D, W> {
123+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124+
// FIXME: Derive instead once `DumbMapping` impls `Debug`.
125+
f.debug_struct("BufferImpl").finish_non_exhaustive()
126+
}
127+
}
128+
121129
/// The combined frame buffer and dumb buffer.
122130
#[derive(Debug)]
123131
struct SharedBuffer {

src/backends/orbital.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use raw_window_handle::{HasDisplayHandle, HasWindowHandle, OrbitalWindowHandle,
33
use std::{cmp, marker::PhantomData, num::NonZeroU32, slice, str};
44

55
use crate::backend_interface::*;
6-
use crate::{Rect, SoftBufferError};
6+
use crate::{util, Rect, SoftBufferError};
77

8+
#[derive(Debug)]
89
struct OrbitalMap {
910
address: usize,
1011
size: usize,
@@ -55,6 +56,7 @@ impl Drop for OrbitalMap {
5556
}
5657
}
5758

59+
#[derive(Debug)]
5860
pub struct OrbitalImpl<D, W> {
5961
handle: ThreadSafeWindowHandle,
6062
width: u32,
@@ -64,6 +66,7 @@ pub struct OrbitalImpl<D, W> {
6466
_display: PhantomData<D>,
6567
}
6668

69+
#[derive(Debug)]
6770
struct ThreadSafeWindowHandle(OrbitalWindowHandle);
6871
unsafe impl Send for ThreadSafeWindowHandle {}
6972
unsafe impl Sync for ThreadSafeWindowHandle {}
@@ -174,17 +177,23 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital
174177
.expect("failed to map orbital window"),
175178
)
176179
} else {
177-
Pixels::Buffer(vec![0; self.width as usize * self.height as usize])
180+
Pixels::Buffer(util::PixelBuffer(vec![
181+
0;
182+
self.width as usize
183+
* self.height as usize
184+
]))
178185
};
179186
Ok(BufferImpl { imp: self, pixels })
180187
}
181188
}
182189

190+
#[derive(Debug)]
183191
enum Pixels {
184192
Mapping(OrbitalMap),
185-
Buffer(Vec<u32>),
193+
Buffer(util::PixelBuffer),
186194
}
187195

196+
#[derive(Debug)]
188197
pub struct BufferImpl<'a, D, W> {
189198
imp: &'a mut OrbitalImpl<D, W>,
190199
pixels: Pixels,

src/backends/wayland/buffer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ unsafe fn map_file(file: &File) -> MmapMut {
6767
unsafe { MmapMut::map_mut(file.as_raw_fd()).expect("Failed to map shared memory") }
6868
}
6969

70+
#[derive(Debug)]
7071
pub(super) struct WaylandBuffer {
7172
qh: QueueHandle<State>,
7273
tempfile: File,

src/backends/wayland/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use buffer::WaylandBuffer;
2020

2121
struct State;
2222

23+
#[derive(Debug)]
2324
pub struct WaylandDisplayImpl<D: ?Sized> {
2425
conn: Option<Connection>,
2526
event_queue: Mutex<EventQueue<State>>,
@@ -74,6 +75,7 @@ impl<D: ?Sized> Drop for WaylandDisplayImpl<D> {
7475
}
7576
}
7677

78+
#[derive(Debug)]
7779
pub struct WaylandImpl<D: ?Sized, W: ?Sized> {
7880
display: Arc<WaylandDisplayImpl<D>>,
7981
surface: Option<wl_surface::WlSurface>,
@@ -261,6 +263,7 @@ impl<D: ?Sized, W: ?Sized> Drop for WaylandImpl<D, W> {
261263
}
262264
}
263265

266+
#[derive(Debug)]
264267
pub struct BufferImpl<'a, D: ?Sized, W> {
265268
stack: util::BorrowStack<'a, WaylandImpl<D, W>, [u32]>,
266269
width: i32,

src/backends/web.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::num::NonZeroU32;
1818
/// Display implementation for the web platform.
1919
///
2020
/// This just caches the document to prevent having to query it every time.
21-
#[derive(Clone)]
21+
#[derive(Clone, Debug)]
2222
pub struct WebDisplayImpl<D> {
2323
document: web_sys::Document,
2424
_display: D,
@@ -43,12 +43,13 @@ impl<D: HasDisplayHandle> ContextInterface<D> for WebDisplayImpl<D> {
4343
}
4444
}
4545

46+
#[derive(Debug)]
4647
pub struct WebImpl<D, W> {
4748
/// The handle and context to the canvas that we're drawing to.
4849
canvas: Canvas,
4950

5051
/// The buffer that we're drawing to.
51-
buffer: Vec<u32>,
52+
buffer: util::PixelBuffer,
5253

5354
/// Buffer has been presented.
5455
buffer_presented: bool,
@@ -65,6 +66,7 @@ pub struct WebImpl<D, W> {
6566

6667
/// Holding canvas and context for [`HtmlCanvasElement`] or [`OffscreenCanvas`],
6768
/// since they have different types.
69+
#[derive(Debug)]
6870
enum Canvas {
6971
Canvas {
7072
canvas: HtmlCanvasElement,
@@ -82,7 +84,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> WebImpl<D, W> {
8284

8385
Ok(Self {
8486
canvas: Canvas::Canvas { canvas, ctx },
85-
buffer: Vec::new(),
87+
buffer: util::PixelBuffer(Vec::new()),
8688
buffer_presented: false,
8789
size: None,
8890
window_handle: window,
@@ -98,7 +100,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> WebImpl<D, W> {
98100

99101
Ok(Self {
100102
canvas: Canvas::OffscreenCanvas { canvas, ctx },
101-
buffer: Vec::new(),
103+
buffer: util::PixelBuffer(Vec::new()),
102104
buffer_presented: false,
103105
size: None,
104106
window_handle: window,
@@ -373,6 +375,7 @@ impl Canvas {
373375
}
374376
}
375377

378+
#[derive(Debug)]
376379
pub struct BufferImpl<'a, D, W> {
377380
imp: &'a mut WebImpl<D, W>,
378381
}

src/backends/win32.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const ZERO_QUAD: Gdi::RGBQUAD = Gdi::RGBQUAD {
2525
rgbReserved: 0,
2626
};
2727

28+
#[derive(Debug)]
2829
struct Buffer {
2930
dc: Gdi::HDC,
3031
bitmap: Gdi::HBITMAP,
@@ -135,6 +136,7 @@ impl Buffer {
135136
}
136137

137138
/// The handle to a window for software buffering.
139+
#[derive(Debug)]
138140
pub struct Win32Impl<D: ?Sized, W> {
139141
/// The window handle.
140142
window: OnlyUsedFromOrigin<HWND>,
@@ -281,6 +283,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
281283
}
282284
}
283285

286+
#[derive(Debug)]
284287
pub struct BufferImpl<'a, D, W>(&'a mut Win32Impl<D, W>);
285288

286289
impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
@@ -468,6 +471,7 @@ impl Command {
468471
}
469472
}
470473

474+
#[derive(Debug)]
471475
struct OnlyUsedFromOrigin<T>(T);
472476
unsafe impl<T> Send for OnlyUsedFromOrigin<T> {}
473477

0 commit comments

Comments
 (0)