Merge pull request #38 from hecrj/feature/performance-metrics

Debug view
This commit is contained in:
Héctor Ramón 2019-11-05 21:46:37 +01:00 committed by GitHub
commit ae6156f779
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 552 additions and 298 deletions

View file

@ -7,10 +7,6 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
rust: [stable, beta]
include:
- os: ubuntu-latest
rust: stable
targets: 'wasm32-unknown-unknown'
steps:
- uses: hecrj/setup-rust-action@v1
with:
@ -23,4 +19,6 @@ jobs:
sudo apt-get install -y libasound2-dev libudev-dev
- uses: actions/checkout@master
- name: Run tests
run: cargo test --verbose --all --all-features
run: |
cargo test --verbose --all
cargo test --verbose --all --all-features

View file

@ -11,6 +11,10 @@ readme = "README.md"
keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"]
[features]
# Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"]
[badges]
maintenance = { status = "actively-developed" }

View file

@ -1,202 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

View file

@ -24,7 +24,7 @@ mod debugger;
mod windowed;
pub use debugger::Debugger;
pub use windowed::Windowed;
pub use windowed::{Target, Windowed};
pub trait Renderer {
type Output;

View file

@ -2,16 +2,28 @@ use crate::MouseCursor;
use raw_window_handle::HasRawWindowHandle;
pub trait Windowed: super::Renderer {
type Target;
pub trait Windowed: super::Renderer + Sized {
type Target: Target<Renderer = Self>;
fn new<W: HasRawWindowHandle>(window: &W) -> Self;
fn new() -> Self;
fn target(&self, width: u16, height: u16) -> Self::Target;
fn draw(
fn draw<T: AsRef<str>>(
&mut self,
output: &Self::Output,
overlay: &[T],
target: &mut Self::Target,
) -> MouseCursor;
}
pub trait Target {
type Renderer;
fn new<W: HasRawWindowHandle>(
window: &W,
width: u16,
height: u16,
renderer: &Self::Renderer,
) -> Self;
fn resize(&mut self, width: u16, height: u16, renderer: &Self::Renderer);
}

View file

@ -5,7 +5,7 @@ mod platform;
pub use platform::*;
pub trait Application {
type Message;
type Message: std::fmt::Debug;
fn update(&mut self, message: Self::Message);

View file

@ -14,4 +14,5 @@ wgpu_glyph = { version = "0.4", git = "https://github.com/hecrj/wgpu_glyph", rev
raw-window-handle = "0.3"
image = "0.22"
glam = "0.8"
font-kit = "0.4"
log = "0.4"

36
wgpu/src/font.rs Normal file
View file

@ -0,0 +1,36 @@
pub use font_kit::error::SelectionError as LoadError;
pub use font_kit::family_name::FamilyName as Family;
pub struct Source {
raw: font_kit::source::SystemSource,
}
impl Source {
pub fn new() -> Self {
Source {
raw: font_kit::source::SystemSource::new(),
}
}
pub fn load(&self, families: &[Family]) -> Result<Vec<u8>, LoadError> {
let font = self.raw.select_best_match(
families,
&font_kit::properties::Properties::default(),
)?;
match font {
font_kit::handle::Handle::Path { path, .. } => {
use std::io::Read;
let mut buf = Vec::new();
let mut reader = std::fs::File::open(path).expect("Read font");
let _ = reader.read_to_end(&mut buf);
Ok(buf)
}
font_kit::handle::Handle::Memory { bytes, .. } => {
Ok(bytes.as_ref().clone())
}
}
}
}

View file

@ -1,3 +1,4 @@
mod font;
mod image;
mod primitive;
mod quad;

View file

@ -1,32 +1,24 @@
use crate::{quad, Image, Primitive, Quad, Transformation};
use crate::{font, quad, Image, Primitive, Quad, Transformation};
use iced_native::{
renderer::Debugger, renderer::Windowed, Background, Color, Layout,
MouseCursor, Point, Rectangle, Vector, Widget,
};
use raw_window_handle::HasRawWindowHandle;
use wgpu::{
Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor,
Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions, Surface,
SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage,
Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions,
TextureFormat,
};
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section};
use std::{cell::RefCell, rc::Rc};
mod button;
mod checkbox;
mod column;
mod image;
mod radio;
mod row;
mod scrollable;
mod slider;
mod text;
mod text_input;
mod target;
mod widget;
pub use target::Target;
pub struct Renderer {
surface: Surface,
device: Device,
queue: Queue,
quad_pipeline: quad::Pipeline,
@ -35,13 +27,6 @@ pub struct Renderer {
glyph_brush: Rc<RefCell<GlyphBrush<'static, ()>>>,
}
pub struct Target {
width: u16,
height: u16,
transformation: Transformation,
swap_chain: SwapChain,
}
pub struct Layer<'a> {
bounds: Rectangle<u32>,
offset: Vector<u32>,
@ -63,7 +48,7 @@ impl<'a> Layer<'a> {
}
impl Renderer {
fn new<W: HasRawWindowHandle>(window: &W) -> Self {
fn new() -> Self {
let adapter = Adapter::request(&RequestAdapterOptions {
power_preference: PowerPreference::LowPower,
backends: BackendBit::all(),
@ -77,21 +62,24 @@ impl Renderer {
limits: Limits { max_bind_groups: 2 },
});
let surface = Surface::create(window);
// TODO: Font customization
let font_source = font::Source::new();
let default_font = font_source
.load(&[font::Family::SansSerif, font::Family::Serif])
.expect("Find sans-serif or serif font");
// TODO: Think about font loading strategy
// Loading system fonts with fallback may be a good idea
let font: &[u8] =
include_bytes!("../../examples/resources/Roboto-Regular.ttf");
let mono_font = font_source
.load(&[font::Family::Monospace])
.expect("Find monospace font");
let glyph_brush = GlyphBrushBuilder::using_font_bytes(font)
.build(&mut device, TextureFormat::Bgra8UnormSrgb);
let glyph_brush =
GlyphBrushBuilder::using_fonts_bytes(vec![default_font, mono_font])
.build(&mut device, TextureFormat::Bgra8UnormSrgb);
let quad_pipeline = quad::Pipeline::new(&mut device);
let image_pipeline = crate::image::Pipeline::new(&mut device);
Self {
surface,
device,
queue,
quad_pipeline,
@ -101,32 +89,17 @@ impl Renderer {
}
}
fn target(&self, width: u16, height: u16) -> Target {
Target {
width,
height,
transformation: Transformation::orthographic(width, height),
swap_chain: self.device.create_swap_chain(
&self.surface,
&SwapChainDescriptor {
usage: TextureUsage::OUTPUT_ATTACHMENT,
format: TextureFormat::Bgra8UnormSrgb,
width: u32::from(width),
height: u32::from(height),
present_mode: wgpu::PresentMode::Vsync,
},
),
}
}
fn draw(
fn draw<T: AsRef<str>>(
&mut self,
(primitive, mouse_cursor): &(Primitive, MouseCursor),
overlay: &[T],
target: &mut Target,
) -> MouseCursor {
log::debug!("Drawing");
let frame = target.swap_chain.get_next_texture();
let (width, height) = target.dimensions();
let transformation = target.transformation();
let frame = target.next_frame();
let mut encoder = self
.device
@ -154,21 +127,17 @@ impl Renderer {
Rectangle {
x: 0,
y: 0,
width: u32::from(target.width),
height: u32::from(target.height),
width: u32::from(width),
height: u32::from(height),
},
Vector::new(0, 0),
));
self.draw_primitive(primitive, &mut layers);
self.draw_overlay(overlay, &mut layers);
for layer in layers {
self.flush(
target.transformation,
&layer,
&mut encoder,
&frame.view,
);
self.flush(transformation, &layer, &mut encoder, &frame.view);
}
self.queue.submit(&[encoder.finish()]);
@ -302,6 +271,41 @@ impl Renderer {
}
}
fn draw_overlay<'a, T: AsRef<str>>(
&mut self,
lines: &'a [T],
layers: &mut Vec<Layer<'a>>,
) {
let first = layers.first().unwrap();
let mut overlay = Layer::new(first.bounds, Vector::new(0, 0));
let font_id =
wgpu_glyph::FontId(self.glyph_brush.borrow().fonts().len() - 1);
let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };
for (i, line) in lines.iter().enumerate() {
overlay.text.push(Section {
text: line.as_ref(),
screen_position: (11.0, 11.0 + 25.0 * i as f32),
color: [0.9, 0.9, 0.9, 1.0],
scale,
font_id,
..Section::default()
});
overlay.text.push(Section {
text: line.as_ref(),
screen_position: (10.0, 10.0 + 25.0 * i as f32),
color: [0.0, 0.0, 0.0, 1.0],
scale,
font_id,
..Section::default()
});
}
layers.push(overlay);
}
fn flush(
&mut self,
transformation: Transformation,
@ -369,20 +373,17 @@ impl iced_native::Renderer for Renderer {
impl Windowed for Renderer {
type Target = Target;
fn new<W: HasRawWindowHandle>(window: &W) -> Self {
Self::new(window)
fn new() -> Self {
Self::new()
}
fn target(&self, width: u16, height: u16) -> Target {
self.target(width, height)
}
fn draw(
fn draw<T: AsRef<str>>(
&mut self,
output: &Self::Output,
overlay: &[T],
target: &mut Target,
) -> MouseCursor {
self.draw(output, target)
self.draw(output, overlay, target)
}
}

View file

@ -0,0 +1,74 @@
use crate::{Renderer, Transformation};
use raw_window_handle::HasRawWindowHandle;
pub struct Target {
surface: wgpu::Surface,
width: u16,
height: u16,
transformation: Transformation,
swap_chain: wgpu::SwapChain,
}
impl Target {
pub fn dimensions(&self) -> (u16, u16) {
(self.width, self.height)
}
pub fn transformation(&self) -> Transformation {
self.transformation
}
pub fn next_frame(&mut self) -> wgpu::SwapChainOutput {
self.swap_chain.get_next_texture()
}
}
impl iced_native::renderer::Target for Target {
type Renderer = Renderer;
fn new<W: HasRawWindowHandle>(
window: &W,
width: u16,
height: u16,
renderer: &Renderer,
) -> Target {
let surface = wgpu::Surface::create(window);
let swap_chain =
new_swap_chain(&surface, width, height, &renderer.device);
Target {
surface,
width,
height,
transformation: Transformation::orthographic(width, height),
swap_chain,
}
}
fn resize(&mut self, width: u16, height: u16, renderer: &Renderer) {
self.width = width;
self.height = height;
self.transformation = Transformation::orthographic(width, height);
self.swap_chain =
new_swap_chain(&self.surface, width, height, &renderer.device);
}
}
fn new_swap_chain(
surface: &wgpu::Surface,
width: u16,
height: u16,
device: &wgpu::Device,
) -> wgpu::SwapChain {
device.create_swap_chain(
&surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: u32::from(width),
height: u32::from(height),
present_mode: wgpu::PresentMode::Vsync,
},
)
}

View file

@ -0,0 +1,10 @@
mod button;
mod checkbox;
mod column;
mod image;
mod radio;
mod row;
mod scrollable;
mod slider;
mod text;
mod text_input;

View file

@ -7,6 +7,9 @@ description = "A winit runtime for Iced"
license = "MIT"
repository = "https://github.com/hecrj/iced"
[features]
debug = []
[dependencies]
iced_native = { version = "0.1.0-alpha", path = "../native" }
winit = { version = "0.20.0-alpha3", git = "https://github.com/rust-windowing/winit", rev = "709808eb4e69044705fcb214bcc30556db761405"}

View file

@ -1,14 +1,14 @@
use crate::{
column, conversion,
input::{keyboard, mouse},
renderer::Windowed,
Cache, Column, Element, Event, Length, MouseCursor, UserInterface,
renderer::{Target, Windowed},
Cache, Column, Debug, Element, Event, Length, MouseCursor, UserInterface,
};
pub trait Application {
type Renderer: Windowed + column::Renderer;
type Message;
type Message: std::fmt::Debug;
fn update(&mut self, message: Self::Message);
@ -24,6 +24,9 @@ pub trait Application {
window::WindowBuilder,
};
let mut debug = Debug::new();
debug.startup_started();
let event_loop = EventLoop::new();
// TODO: Ask for window settings and configure this properly
@ -41,19 +44,31 @@ pub trait Application {
.into();
let mut new_size: Option<Size> = None;
let mut renderer = Self::Renderer::new(&window);
let mut target = renderer.target(size.width, size.height);
let mut renderer = Self::Renderer::new();
let user_interface = UserInterface::build(
document(&mut self, size),
Cache::default(),
let mut target = <Self::Renderer as Windowed>::Target::new(
&window,
size.width,
size.height,
&renderer,
);
debug.layout_started();
let user_interface = UserInterface::build(
document(&mut self, size, &mut debug),
Cache::default(),
&renderer,
);
debug.layout_finished();
debug.draw_started();
let mut primitive = user_interface.draw(&mut renderer);
debug.draw_finished();
let mut cache = Some(user_interface.into_cache());
let mut events = Vec::new();
let mut mouse_cursor = MouseCursor::OutOfBounds;
debug.startup_finished();
window.request_redraw();
@ -64,17 +79,23 @@ pub trait Application {
//
// This will allow us to rebuild it only when a message is
// handled.
debug.layout_started();
let mut user_interface = UserInterface::build(
document(&mut self, size),
document(&mut self, size, &mut debug),
cache.take().unwrap(),
&renderer,
);
debug.layout_finished();
debug.event_processing_started();
let messages =
user_interface.update(&renderer, events.drain(..));
debug.event_processing_finished();
if messages.is_empty() {
debug.draw_started();
primitive = user_interface.draw(&mut renderer);
debug.draw_finished();
cache = Some(user_interface.into_cache());
} else {
@ -85,16 +106,24 @@ pub trait Application {
for message in messages {
log::debug!("Updating");
debug.log_message(&message);
debug.update_started();
self.update(message);
debug.update_finished();
}
debug.layout_started();
let user_interface = UserInterface::build(
document(&mut self, size),
document(&mut self, size, &mut debug),
temp_cache,
&renderer,
);
debug.layout_finished();
debug.draw_started();
primitive = user_interface.draw(&mut renderer);
debug.draw_finished();
cache = Some(user_interface.into_cache());
}
@ -102,12 +131,18 @@ pub trait Application {
window.request_redraw();
}
event::Event::RedrawRequested(_) => {
debug.render_started();
if let Some(new_size) = new_size.take() {
target = renderer.target(new_size.width, new_size.height);
target.resize(new_size.width, new_size.height, &renderer);
size = new_size;
}
let new_mouse_cursor = renderer.draw(&primitive, &mut target);
let new_mouse_cursor =
renderer.draw(&primitive, &debug.overlay(), &mut target);
debug.render_finished();
if new_mouse_cursor != mouse_cursor {
window.set_cursor_icon(conversion::mouse_cursor(
@ -183,6 +218,14 @@ pub trait Application {
},
..
} => {
match (virtual_keycode, state) {
(
winit::event::VirtualKeyCode::F12,
winit::event::ElementState::Pressed,
) => debug.toggle(),
_ => {}
}
events.push(Event::Keyboard(keyboard::Event::Input {
key_code: conversion::key_code(virtual_keycode),
state: conversion::button_state(state),
@ -221,17 +264,22 @@ impl From<winit::dpi::PhysicalSize> for Size {
}
}
fn document<Application>(
application: &mut Application,
fn document<'a, Application>(
application: &'a mut Application,
size: Size,
) -> Element<Application::Message, Application::Renderer>
debug: &mut Debug,
) -> Element<'a, Application::Message, Application::Renderer>
where
Application: self::Application,
Application::Message: 'static,
{
debug.view_started();
let view = application.view();
debug.view_finished();
Column::new()
.width(Length::Units(size.width))
.height(Length::Units(size.height))
.push(application.view())
.push(view)
.into()
}

212
winit/src/debug/basic.rs Normal file
View file

@ -0,0 +1,212 @@
use std::collections::VecDeque;
use std::time;
#[derive(Debug)]
pub struct Debug {
is_enabled: bool,
startup_start: time::Instant,
startup_duration: time::Duration,
update_start: time::Instant,
update_durations: TimeBuffer,
view_start: time::Instant,
view_durations: TimeBuffer,
layout_start: time::Instant,
layout_durations: TimeBuffer,
event_start: time::Instant,
event_durations: TimeBuffer,
draw_start: time::Instant,
draw_durations: TimeBuffer,
render_start: time::Instant,
render_durations: TimeBuffer,
message_count: usize,
last_messages: VecDeque<String>,
}
impl Debug {
pub fn new() -> Self {
let now = time::Instant::now();
Self {
is_enabled: false,
startup_start: now,
startup_duration: time::Duration::from_secs(0),
update_start: now,
update_durations: TimeBuffer::new(200),
view_start: now,
view_durations: TimeBuffer::new(200),
layout_start: now,
layout_durations: TimeBuffer::new(200),
event_start: now,
event_durations: TimeBuffer::new(200),
draw_start: now,
draw_durations: TimeBuffer::new(200),
render_start: now,
render_durations: TimeBuffer::new(50),
message_count: 0,
last_messages: VecDeque::new(),
}
}
pub fn toggle(&mut self) {
self.is_enabled = !self.is_enabled;
}
pub fn startup_started(&mut self) {
self.startup_start = time::Instant::now();
}
pub fn startup_finished(&mut self) {
self.startup_duration = time::Instant::now() - self.startup_start;
}
pub fn update_started(&mut self) {
self.update_start = time::Instant::now();
}
pub fn update_finished(&mut self) {
self.update_durations
.push(time::Instant::now() - self.update_start);
}
pub fn view_started(&mut self) {
self.view_start = time::Instant::now();
}
pub fn view_finished(&mut self) {
self.view_durations
.push(time::Instant::now() - self.view_start);
}
pub fn layout_started(&mut self) {
self.layout_start = time::Instant::now();
}
pub fn layout_finished(&mut self) {
self.layout_durations
.push(time::Instant::now() - self.layout_start);
}
pub fn event_processing_started(&mut self) {
self.event_start = time::Instant::now();
}
pub fn event_processing_finished(&mut self) {
self.event_durations
.push(time::Instant::now() - self.event_start);
}
pub fn draw_started(&mut self) {
self.draw_start = time::Instant::now();
}
pub fn draw_finished(&mut self) {
self.draw_durations
.push(time::Instant::now() - self.draw_start);
}
pub fn render_started(&mut self) {
self.render_start = time::Instant::now();
}
pub fn render_finished(&mut self) {
self.render_durations
.push(time::Instant::now() - self.render_start);
}
pub fn log_message<Message: std::fmt::Debug>(&mut self, message: &Message) {
self.last_messages.push_back(format!("{:?}", message));
if self.last_messages.len() > 10 {
let _ = self.last_messages.pop_front();
}
self.message_count += 1;
}
pub fn overlay(&self) -> Vec<String> {
if !self.is_enabled {
return Vec::new();
}
let mut lines = Vec::new();
fn key_value<T: std::fmt::Debug>(key: &str, value: T) -> String {
format!("{: <30} {:?}", key, value)
}
lines.push(format!(
"{} {} - {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_REPOSITORY"),
));
lines.push(key_value("Startup:", self.startup_duration));
lines.push(key_value("Update:", self.update_durations.average()));
lines.push(key_value("View:", self.view_durations.average()));
lines.push(key_value("Layout:", self.layout_durations.average()));
lines.push(key_value(
"Event processing:",
self.event_durations.average(),
));
lines.push(key_value(
"Primitive generation:",
self.draw_durations.average(),
));
lines.push(key_value("Render:", self.render_durations.average()));
lines.push(key_value("Message count:", self.message_count));
lines.push(String::from("Last messages:"));
lines.extend(
self.last_messages.iter().map(|msg| format!(" {}", msg)),
);
lines
}
}
#[derive(Debug)]
struct TimeBuffer {
head: usize,
size: usize,
contents: Vec<time::Duration>,
}
impl TimeBuffer {
fn new(capacity: usize) -> TimeBuffer {
TimeBuffer {
head: 0,
size: 0,
contents: vec![time::Duration::from_secs(0); capacity],
}
}
fn push(&mut self, duration: time::Duration) {
self.head = (self.head + 1) % self.contents.len();
self.contents[self.head] = duration;
self.size = (self.size + 1).min(self.contents.len());
}
fn average(&self) -> time::Duration {
let sum: time::Duration = if self.size == self.contents.len() {
self.contents[..].iter().sum()
} else {
self.contents[..self.size].iter().sum()
};
sum / self.size.max(1) as u32
}
}

48
winit/src/debug/null.rs Normal file
View file

@ -0,0 +1,48 @@
#[derive(Debug)]
pub struct Debug;
impl Debug {
pub fn new() -> Self {
Self
}
pub fn toggle(&mut self) {}
pub fn startup_started(&mut self) {}
pub fn startup_finished(&mut self) {}
pub fn update_started(&mut self) {}
pub fn update_finished(&mut self) {}
pub fn view_started(&mut self) {}
pub fn view_finished(&mut self) {}
pub fn layout_started(&mut self) {}
pub fn layout_finished(&mut self) {}
pub fn event_processing_started(&mut self) {}
pub fn event_processing_finished(&mut self) {}
pub fn draw_started(&mut self) {}
pub fn draw_finished(&mut self) {}
pub fn render_started(&mut self) {}
pub fn render_finished(&mut self) {}
pub fn log_message<Message: std::fmt::Debug>(
&mut self,
_message: &Message,
) {
}
pub fn overlay(&self) -> Vec<String> {
Vec::new()
}
}

View file

@ -6,3 +6,11 @@ pub mod conversion;
mod application;
pub use application::Application;
// We disable debug capabilities on release builds unless the `debug` feature
// is explicitly enabled.
#[cfg_attr(feature = "debug", path = "debug/basic.rs")]
#[cfg_attr(not(feature = "debug"), path = "debug/null.rs")]
mod debug;
use debug::Debug;