From 2c6bfdbc8c2262c3550fa16d4472e29d73077956 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 3 Nov 2019 04:39:11 +0100 Subject: Implement debug view and load system fonts --- Cargo.toml | 4 + examples/resources/Roboto-LICENSE | 202 --------------------------------- examples/resources/Roboto-Regular.ttf | Bin 171272 -> 0 bytes examples/resources/ui.png | Bin 16691 -> 0 bytes native/src/lib.rs | 2 - native/src/metrics.rs | 11 -- native/src/renderer/windowed.rs | 6 +- src/lib.rs | 2 +- wgpu/Cargo.toml | 1 + wgpu/src/font.rs | 38 +++++++ wgpu/src/lib.rs | 1 + wgpu/src/renderer.rs | 64 +++++++++-- winit/Cargo.toml | 3 + winit/src/application.rs | 60 ++++++++-- winit/src/debug/basic.rs | 206 ++++++++++++++++++++++++++++++++++ winit/src/debug/null.rs | 48 ++++++++ winit/src/lib.rs | 11 ++ 17 files changed, 418 insertions(+), 241 deletions(-) delete mode 100644 examples/resources/Roboto-LICENSE delete mode 100644 examples/resources/Roboto-Regular.ttf delete mode 100644 examples/resources/ui.png delete mode 100644 native/src/metrics.rs create mode 100644 wgpu/src/font.rs create mode 100644 winit/src/debug/basic.rs create mode 100644 winit/src/debug/null.rs diff --git a/Cargo.toml b/Cargo.toml index e8b53066..8f9769b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/examples/resources/Roboto-LICENSE b/examples/resources/Roboto-LICENSE deleted file mode 100644 index 75b52484..00000000 --- a/examples/resources/Roboto-LICENSE +++ /dev/null @@ -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. diff --git a/examples/resources/Roboto-Regular.ttf b/examples/resources/Roboto-Regular.ttf deleted file mode 100644 index 2b6392ff..00000000 Binary files a/examples/resources/Roboto-Regular.ttf and /dev/null differ diff --git a/examples/resources/ui.png b/examples/resources/ui.png deleted file mode 100644 index 4fd3beb3..00000000 Binary files a/examples/resources/ui.png and /dev/null differ diff --git a/native/src/lib.rs b/native/src/lib.rs index 86bfb3ca..fa72a553 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -206,7 +206,6 @@ mod element; mod event; mod hasher; mod layout; -mod metrics; mod mouse_cursor; mod node; mod style; @@ -225,7 +224,6 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; -pub use metrics::Metrics; pub use mouse_cursor::MouseCursor; pub use node::Node; pub use renderer::Renderer; diff --git a/native/src/metrics.rs b/native/src/metrics.rs deleted file mode 100644 index 7c344b9b..00000000 --- a/native/src/metrics.rs +++ /dev/null @@ -1,11 +0,0 @@ -use std::time; - -/// A bunch of metrics about an Iced application. -#[derive(Debug, Clone, Copy)] -pub struct Metrics { - pub startup_time: time::Duration, - pub update_time: time::Duration, - pub view_time: time::Duration, - pub renderer_output_time: time::Duration, - pub message_count: usize, -} diff --git a/native/src/renderer/windowed.rs b/native/src/renderer/windowed.rs index 0499ca70..6e4ae611 100644 --- a/native/src/renderer/windowed.rs +++ b/native/src/renderer/windowed.rs @@ -1,4 +1,4 @@ -use crate::{Metrics, MouseCursor}; +use crate::MouseCursor; use raw_window_handle::HasRawWindowHandle; @@ -7,10 +7,10 @@ pub trait Windowed: super::Renderer + Sized { fn new() -> Self; - fn draw( + fn draw>( &mut self, output: &Self::Output, - metrics: Option, + overlay: &[T], target: &mut Self::Target, ) -> MouseCursor; } diff --git a/src/lib.rs b/src/lib.rs index 1bcdada2..da108804 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 04fae248..2c212286 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -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" diff --git a/wgpu/src/font.rs b/wgpu/src/font.rs new file mode 100644 index 00000000..e2838268 --- /dev/null +++ b/wgpu/src/font.rs @@ -0,0 +1,38 @@ +pub use font_kit::family_name::FamilyName as Family; + +pub struct Source { + raw: font_kit::sources::fontconfig::FontconfigSource, +} + +impl Source { + pub fn new() -> Self { + Source { + raw: font_kit::sources::fontconfig::FontconfigSource::new(), + } + } + + pub fn load(&self, families: &[Family]) -> Vec { + let font = self + .raw + .select_best_match( + families, + &font_kit::properties::Properties::default(), + ) + .expect("Find font"); + + 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); + + buf + } + font_kit::handle::Handle::Memory { bytes, .. } => { + bytes.as_ref().clone() + } + } + } +} diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 01dc4c20..f504897d 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,3 +1,4 @@ +mod font; mod image; mod primitive; mod quad; diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 1988dfae..770430ad 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,6 +1,6 @@ -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, Metrics, + renderer::Debugger, renderer::Windowed, Background, Color, Layout, MouseCursor, Point, Rectangle, Widget, }; @@ -62,13 +62,16 @@ impl Renderer { limits: Limits { max_bind_groups: 2 }, }); - // 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"); + // TODO: Font customization + let font_source = font::Source::new(); + let sans_serif_font = font_source.load(&[font::Family::SansSerif]); + let mono_font = font_source.load(&[font::Family::Monospace]); - let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) - .build(&mut device, TextureFormat::Bgra8UnormSrgb); + let glyph_brush = GlyphBrushBuilder::using_fonts_bytes(vec![ + sans_serif_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); @@ -83,9 +86,10 @@ impl Renderer { } } - fn draw( + fn draw>( &mut self, (primitive, mouse_cursor): &(Primitive, MouseCursor), + overlay: &[T], target: &mut Target, ) -> MouseCursor { log::debug!("Drawing"); @@ -127,6 +131,7 @@ impl Renderer { )); self.draw_primitive(primitive, &mut layers); + self.draw_overlay(overlay, &mut layers); for layer in layers { self.flush(transformation, &layer, &mut encoder, &frame.view); @@ -260,6 +265,41 @@ impl Renderer { } } + fn draw_overlay<'a, T: AsRef>( + &mut self, + lines: &'a [T], + layers: &mut Vec>, + ) { + let first = layers.first().unwrap(); + let mut overlay = Layer::new(first.bounds, 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, @@ -328,13 +368,13 @@ impl Windowed for Renderer { Self::new() } - fn draw( + fn draw>( &mut self, output: &Self::Output, - metrics: Option, + overlay: &[T], target: &mut Target, ) -> MouseCursor { - self.draw(output, target) + self.draw(output, overlay, target) } } diff --git a/winit/Cargo.toml b/winit/Cargo.toml index c8227ac4..2831ba2f 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -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"} diff --git a/winit/src/application.rs b/winit/src/application.rs index 8c7d8c37..5d1aae38 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -2,13 +2,13 @@ use crate::{ column, conversion, input::{keyboard, mouse}, renderer::{Target, Windowed}, - Cache, Column, Element, Event, Length, MouseCursor, UserInterface, + 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 @@ -50,16 +53,22 @@ pub trait Application { &renderer, ); + debug.layout_started(); let user_interface = UserInterface::build( - document(&mut self, size), + 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(); @@ -70,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 { @@ -91,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()); } @@ -108,6 +131,8 @@ pub trait Application { window.request_redraw(); } event::Event::RedrawRequested(_) => { + debug.render_started(); + if let Some(new_size) = new_size.take() { target.resize(new_size.width, new_size.height, &renderer); @@ -115,7 +140,9 @@ pub trait Application { } let new_mouse_cursor = - renderer.draw(&primitive, None, &mut target); + renderer.draw(&primitive, &debug.overlay(), &mut target); + + debug.render_finished(); if new_mouse_cursor != mouse_cursor { window.set_cursor_icon(conversion::mouse_cursor( @@ -191,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), @@ -229,17 +264,22 @@ impl From for Size { } } -fn document( - application: &mut Application, +fn document<'a, Application>( + application: &'a mut Application, size: Size, -) -> Element + 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() } diff --git a/winit/src/debug/basic.rs b/winit/src/debug/basic.rs new file mode 100644 index 00000000..09bb5ae1 --- /dev/null +++ b/winit/src/debug/basic.rs @@ -0,0 +1,206 @@ +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, +} + +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(&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 { + if !self.is_enabled { + return Vec::new(); + } + + let mut lines = Vec::new(); + + fn key_value(key: &str, value: T) -> String { + format!("{: <30} {:?}", key, value) + } + + 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, +} + +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 + } +} diff --git a/winit/src/debug/null.rs b/winit/src/debug/null.rs new file mode 100644 index 00000000..9c809dd4 --- /dev/null +++ b/winit/src/debug/null.rs @@ -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( + &mut self, + _message: &Message, + ) { + } + + pub fn overlay(&self) -> Vec { + Vec::new() + } +} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index b08fcb6c..f66c0553 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -6,3 +6,14 @@ 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(any(debug_assertions, feature = "debug"), path = "debug/basic.rs")] +#[cfg_attr( + not(any(debug_assertions, feature = "debug")), + path = "debug/null.rs" +)] +mod debug; + +use debug::Debug; -- cgit