perftest/
perftest.rs

1// Copyright 2018 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16
17use time::Instant;
18
19use piet_common::kurbo::{Line, Size};
20use piet_common::{Color, FontFamily, Piet, RenderContext, Text, TextLayoutBuilder};
21
22use druid_shell::{Application, KeyEvent, Region, WinHandler, WindowBuilder, WindowHandle};
23
24const BG_COLOR: Color = Color::rgb8(0x27, 0x28, 0x22);
25const FG_COLOR: Color = Color::rgb8(0xf0, 0xf0, 0xea);
26const RED: Color = Color::rgb8(0xff, 0x80, 0x80);
27const CYAN: Color = Color::rgb8(0x80, 0xff, 0xff);
28
29struct PerfTest {
30    handle: WindowHandle,
31    size: Size,
32    start_time: Instant,
33    last_time: Instant,
34    red: bool,
35}
36
37impl WinHandler for PerfTest {
38    fn connect(&mut self, handle: &WindowHandle) {
39        self.handle = handle.clone();
40    }
41
42    fn prepare_paint(&mut self) {
43        self.handle.invalidate();
44    }
45
46    fn paint(&mut self, piet: &mut Piet, _: &Region) {
47        let rect = self.size.to_rect();
48        piet.fill(rect, &BG_COLOR);
49
50        piet.stroke(
51            Line::new((0.0, self.size.height), (self.size.width, 0.0)),
52            &FG_COLOR,
53            1.0,
54        );
55
56        let current_ns = (Instant::now() - self.start_time).whole_nanoseconds();
57        let th = ::std::f64::consts::PI * (current_ns as f64) * 2e-9;
58        let dx = 100.0 * th.sin();
59        let dy = 100.0 * th.cos();
60        piet.stroke(
61            Line::new((100.0, 100.0), (100.0 + dx, 100.0 - dy)),
62            &FG_COLOR,
63            1.0,
64        );
65
66        let now = Instant::now();
67        let msg = format!("{}ms", (now - self.last_time).whole_milliseconds());
68        self.last_time = now;
69        let layout = piet
70            .text()
71            .new_text_layout(msg)
72            .font(FontFamily::MONOSPACE, 15.0)
73            .text_color(FG_COLOR)
74            .build()
75            .unwrap();
76        piet.draw_text(&layout, (10.0, 210.0));
77
78        let msg = "VSYNC";
79        let color = if self.red { RED } else { CYAN };
80
81        let layout = piet
82            .text()
83            .new_text_layout(msg)
84            .text_color(color)
85            .font(FontFamily::MONOSPACE, 48.0)
86            .build()
87            .unwrap();
88        piet.draw_text(&layout, (10.0, 280.0));
89        self.red = !self.red;
90
91        let msg = "Hello DWrite! This is a somewhat longer string of text intended to provoke slightly longer draw times.";
92        let layout = piet
93            .text()
94            .new_text_layout(msg)
95            .font(FontFamily::MONOSPACE, 15.0)
96            .text_color(FG_COLOR)
97            .build()
98            .unwrap();
99        let dy = 15.0;
100        let x0 = 210.0;
101        let y0 = 10.0;
102        for i in 0..60 {
103            let y = y0 + (i as f64) * dy;
104            piet.draw_text(&layout, (x0, y));
105        }
106        self.handle.request_anim_frame();
107    }
108
109    fn command(&mut self, id: u32) {
110        match id {
111            0x100 => self.handle.close(),
112            _ => println!("unexpected id {id}"),
113        }
114    }
115
116    fn key_down(&mut self, event: KeyEvent) -> bool {
117        println!("keydown: {event:?}");
118        false
119    }
120
121    fn size(&mut self, size: Size) {
122        self.size = size;
123    }
124
125    fn request_close(&mut self) {
126        self.handle.close();
127    }
128
129    fn destroy(&mut self) {
130        Application::global().quit()
131    }
132
133    fn as_any(&mut self) -> &mut dyn Any {
134        self
135    }
136}
137
138fn main() {
139    tracing_subscriber::fmt().init();
140    let app = Application::new().unwrap();
141    let mut builder = WindowBuilder::new(app.clone());
142    let perf_test = PerfTest {
143        size: Size::ZERO,
144        handle: Default::default(),
145        start_time: time::Instant::now(),
146        last_time: time::Instant::now(),
147        red: true,
148    };
149    builder.set_handler(Box::new(perf_test));
150    builder.set_title("Performance tester");
151
152    let window = builder.build().unwrap();
153    window.show();
154
155    app.run(None);
156}