From be7c9f520703d38cf1c13d8c28e87d4491abaa4d Mon Sep 17 00:00:00 2001 From: keith Date: Tue, 17 Dec 2019 12:07:23 -0800 Subject: [PATCH] refactor somewhat --- Cargo.lock | 380 +++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 11 +- src/renderer/caching_shaper.rs | 29 +++ src/renderer/fps_tracker.rs | 28 +++ src/renderer/mod.rs | 153 +++++++++++++ src/window.rs | 227 +------------------- 7 files changed, 606 insertions(+), 224 deletions(-) create mode 100644 src/renderer/caching_shaper.rs create mode 100644 src/renderer/fps_tracker.rs create mode 100644 src/renderer/mod.rs diff --git a/Cargo.lock b/Cargo.lock index cbba2e7..85e8b9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,6 +55,19 @@ dependencies = [ "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "arrayref" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "arrayvec" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "arrayvec" version = "0.5.1" @@ -82,6 +95,34 @@ name = "autocfg" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "backtrace" +version = "0.3.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace-sys" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "base64" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bindgen" version = "0.52.0" @@ -110,6 +151,16 @@ name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "blake2b_simd" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "block" version = "0.1.6" @@ -188,6 +239,14 @@ dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cmake" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cocoa" version = "0.19.1" @@ -220,6 +279,11 @@ dependencies = [ "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "constant_time_eq" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "core-foundation" version = "0.6.4" @@ -229,11 +293,25 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "core-foundation" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-foundation-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "core-foundation-sys" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "core-foundation-sys" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "core-graphics" version = "0.17.3" @@ -245,6 +323,28 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "core-graphics" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-text" +version = "15.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", + "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "core-video-sys" version = "0.1.3" @@ -265,6 +365,15 @@ dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-utils" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "curl" version = "0.4.25" @@ -303,6 +412,26 @@ dependencies = [ "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dirs" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dirs-sys" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dispatch" version = "0.1.4" @@ -321,6 +450,16 @@ name = "downcast-rs" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "dwrote" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -333,6 +472,43 @@ dependencies = [ "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "euclid" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "expat-sys" +version = "2.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "failure" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "failure_derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "filetime" version = "0.2.8" @@ -355,6 +531,34 @@ dependencies = [ "miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "float-ord" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "font-kit" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-text 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "dwrote 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "euclid 0.20.7 (registry+https://github.com/rust-lang/crates.io-index)", + "float-ord 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "freetype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "lyon_path 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", + "servo-fontconfig 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "foreign-types" version = "0.3.2" @@ -368,6 +572,20 @@ name = "foreign-types-shared" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "freetype" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "servo-freetype-sys 4.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -516,6 +734,24 @@ dependencies = [ "hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lyon_geom" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "euclid 0.20.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lyon_path" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lyon_geom 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "malloc_buf" version = "0.0.6" @@ -611,6 +847,8 @@ version = "0.1.0" dependencies = [ "derive-new 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "euclid 0.20.7 (registry+https://github.com/rust-lang/crates.io-index)", + "font-kit 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "lru 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "neovim-lib 0.6.0 (git+https://github.com/daa84/neovim-lib)", "rmpv 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -649,6 +887,11 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "nom" version = "4.2.3" @@ -818,6 +1061,19 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand_core" version = "0.5.1" @@ -834,6 +1090,19 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "raw-window-handle" version = "0.3.3" @@ -842,11 +1111,30 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_syscall" version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "redox_users" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "1.3.1" @@ -883,6 +1171,21 @@ dependencies = [ "serde_bytes 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rust-argon2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "blake2b_simd 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-hash" version = "1.0.1" @@ -960,6 +1263,34 @@ dependencies = [ "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "servo-fontconfig" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "servo-fontconfig-sys 4.0.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "servo-fontconfig-sys" +version = "4.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "expat-sys 2.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "servo-freetype-sys 4.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "servo-freetype-sys" +version = "4.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "shared_library" version = "0.1.9" @@ -1075,6 +1406,17 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synstructure" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tar" version = "0.4.26" @@ -1364,12 +1706,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3" +"checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" +"checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" "checksum ash 0.29.0 (registry+https://github.com/rust-lang/crates.io-index)" = "003d1fb2eb12eb06d4a03dbe02eea67a9fac910fa97932ab9e3a75b96a1ea5e5" "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" "checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" +"checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea" +"checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491" +"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum bindgen 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f1c85344eb535a31b62f0af37be84441ba9e7f0f4111eb0530f43d15e513fe57" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum blake2b_simd 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b83b7baab1e671718d78204225800d6b170e648188ac7dc992e9d6bddf87d0c0" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" @@ -1380,25 +1728,43 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62" "checksum cocoa 0.19.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f29f7768b2d1be17b96158e3285951d366b40211320fb30826a76cb7a0da6400" "checksum const-random 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7b641a8c9867e341f3295564203b1c250eb8ce6cb6126e007941f78c4d2ed7fe" "checksum const-random-macro 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c750ec12b83377637110d5a57f5ae08e895b06c4b16e2bdbf1a94ef717428c59" +"checksum constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "995a44c877f9212528ccc74b21a232f66ad69001e40ede5bcee2ac9ef2657120" "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" +"checksum core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171" "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" +"checksum core-foundation-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" "checksum core-graphics 0.17.3 (registry+https://github.com/rust-lang/crates.io-index)" = "56790968ab1c8a1202a102e6de05fc6e1ec87da99e4e93e9a7d13efbfc1e95a9" +"checksum core-graphics 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "59e78b2e0aaf43f08e7ae0d6bc96895ef72ff0921c7d4ff4762201b2dba376dd" +"checksum core-text 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "131b3fd1f8bd5db9f2b398fa4fdb6008c64afc04d447c306ac2c7e98fba2a61d" "checksum core-video-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8dc065219542086f72d1e9f7aadbbab0989e980263695d129d502082d063a9d0" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +"checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" "checksum curl 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "06aa71e9208a54def20792d877bc663d6aae0732b9852e612c4a933177c31283" "checksum curl-sys 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "f659f3ffac9582d6177bb86d1d2aa649f4eb9d0d4de9d03ccc08b402832ea340" "checksum derive-new 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "71f31892cd5c62e414316f2963c5689242c43d8e7bbcaaeca97e5e28c95d91d9" +"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" +"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b" "checksum dispatch 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e93ca78226c51902d7aa8c12c988338aadd9e85ed9c6be8aaac39192ff3605" "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a" "checksum downcast-rs 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "52ba6eb47c2131e784a38b726eb54c1e1484904f013e576a25354d0124161af6" +"checksum dwrote 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0bd1369e02db5e9b842a9b67bce8a2fcc043beafb2ae8a799dd482d46ea1ff0d" "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +"checksum euclid 0.20.7 (registry+https://github.com/rust-lang/crates.io-index)" = "3f852d320142e1cceb15dccef32ed72a9970b83109d8a4e24b1ab04d579f485d" +"checksum expat-sys 2.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "658f19728920138342f68408b7cf7644d90d4784353d8ebc32e7e8663dbe45fa" +"checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" +"checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08" "checksum filetime 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d" "checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f" +"checksum float-ord 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" +"checksum font-kit 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09b6e2b877097ffd0abac6597fea26fccb5ed7eb9da0a4094f11ccc8aba64efb" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +"checksum freetype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "11926b2b410b469d0e9399eca4cbbe237a9ef02176c485803b29216307e8e028" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407" @@ -1419,6 +1785,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum lock_api 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e57b3997725d2b60dbec1297f6c2e2957cc383db1cebd6be812163f969c7d586" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum lru 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0609345ddee5badacf857d4f547e0e5a2e987db77085c24cd887f73573a04237" +"checksum lyon_geom 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ca04310c9807612a311506106000b6eccb2e27bca9bfb594ce80fb8a31231f9d" +"checksum lyon_path 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0bcb57ac24a5428539e2c7c0592766d5933c937d703f430990c669c00de96862" "checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" "checksum maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" @@ -1431,6 +1799,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum neovim-lib 0.6.0 (git+https://github.com/daa84/neovim-lib)" = "" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" +"checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" "checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" "checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" "checksum objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" @@ -1452,14 +1821,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" "checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" "checksum raw-window-handle 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a441a7a6c80ad6473bd4b74ec1c9a4c951794285bf941c2126f607c72e48211" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +"checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" "checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd" "checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716" "checksum rmp 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f594cb7ff8f1c5a7907f6be91f15795c8301e0d5718eb007fb5832723dd716e" "checksum rmpv 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "83caf745acbd99179ab6ce95398ddf548edfa2d3a99ff195248b30cd1524c43f" +"checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" +"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rusttype 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5" "checksum rusttype 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "14a911032fb5791ccbeec9f28fdcb9bf0983b81f227bafdfd227c658d0731c8a" @@ -1470,6 +1846,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702" "checksum serde_bytes 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "45af0182ff64abaeea290235eb67da3825a576c5d53e642c4d5b652e12e6effc" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" +"checksum servo-fontconfig 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a088f8d775a5c5314aae09bd77340bc9c67d72b9a45258be34c83548b4814cd9" +"checksum servo-fontconfig-sys 4.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "62b3e166450f523f4db06c14f02a2d39e76d49b5d8cbd224338d93e3595c156c" +"checksum servo-freetype-sys 4.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2c4ccb6d0d32d277d3ef7dea86203d8210945eb7a45fba89dd445b3595dd0dfc" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum skia-bindings 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c2eee489fbdc6fe3004f95e4457d7f3bd184b7902982cca0a2d8d01b7438328b" @@ -1482,6 +1861,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum stb_truetype 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f77b6b07e862c66a9f3e62a07588fee67cd90a9135a2b942409f195507b4fb51" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92" +"checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "b3196bfbffbba3e57481b6ea32249fbaf590396a52505a2615adbb79d9d826d3" "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" diff --git a/Cargo.toml b/Cargo.toml index d2c1bb9..215b4ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ authors = ["keith "] edition = "2018" [dependencies] +euclid = "0.20.7" +font-kit = "0.5.0" lru = "0.4.3" skulpin = { git = "https://github.com/Kethku/skulpin" } derive-new = "0.5" diff --git a/src/main.rs b/src/main.rs index 770ced1..495df74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,18 @@ -#![windows_subsystem = "windows"] +// #![windows_subsystem = "windows"] mod editor; mod events; mod window; mod keybindings; +mod renderer; #[macro_use] extern crate derive_new; -use std::panic; -use std::process::{Command, Stdio, exit}; -use std::sync::mpsc::Receiver; +use std::process::{Command, Stdio}; use std::sync::{Arc, Mutex}; use std::thread; -use env_logger::Env as LoggerEnv; use neovim_lib::{Neovim, UiAttachOptions, Session}; -use rmpv::Value; use window::ui_loop; use editor::Editor; @@ -71,7 +68,7 @@ fn start_nvim(editor: Arc>) -> Neovim { // Quit process when nvim exits thread::spawn(move || { - join_handle.join(); + join_handle.join().expect("Could not join neovim process..."); std::process::exit(0); }); diff --git a/src/renderer/caching_shaper.rs b/src/renderer/caching_shaper.rs new file mode 100644 index 0000000..19dde4c --- /dev/null +++ b/src/renderer/caching_shaper.rs @@ -0,0 +1,29 @@ +use lru::LruCache; +use skulpin::skia_safe::{Shaper, TextBlob, Font, Point}; + +pub struct CachingShaper { + shaper: Shaper, + cache: LruCache +} + +impl CachingShaper { + pub fn new() -> CachingShaper { + CachingShaper { + shaper: Shaper::new(None), + cache: LruCache::new(1000) + } + } + + pub fn shape(&self, text: &str, font: &Font) -> TextBlob { + let (blob, _) = self.shaper.shape_text_blob(text, font, true, 1000000.0, Point::default()).unwrap(); + blob + } + + pub fn shape_cached(&mut self, text: String, font: &Font) -> &TextBlob { + if !self.cache.contains(&text) { + self.cache.put(text.clone(), self.shape(&text, &font)); + } + + self.cache.get(&text).unwrap() + } +} diff --git a/src/renderer/fps_tracker.rs b/src/renderer/fps_tracker.rs new file mode 100644 index 0000000..48f624e --- /dev/null +++ b/src/renderer/fps_tracker.rs @@ -0,0 +1,28 @@ +use std::time::Instant; + +pub struct FpsTracker { + last_record_time: Instant, + frame_count: usize, + pub fps: usize +} + +impl FpsTracker { + pub fn new() -> FpsTracker { + FpsTracker { + fps: 0, + last_record_time: Instant::now(), + frame_count: 0 + } + } + + pub fn record_frame(&mut self) { + self.frame_count = self.frame_count + 1; + let now = Instant::now(); + let time_since = (now - self.last_record_time).as_secs_f32(); + if time_since > 1.0 { + self.fps = self.frame_count; + self.last_record_time = now; + self.frame_count = 0; + } + } +} diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs new file mode 100644 index 0000000..1f8f2ea --- /dev/null +++ b/src/renderer/mod.rs @@ -0,0 +1,153 @@ +use std::sync::{Arc, Mutex}; +use skulpin::skia_safe::{Canvas, Paint, Rect, Typeface, Font, FontStyle, colors}; + +mod caching_shaper; +mod fps_tracker; +mod font_measurer; + +use caching_shaper::CachingShaper; +use fps_tracker::FpsTracker; + +use crate::editor::{Editor, CursorType, Style, Colors}; + +const FONT_NAME: &str = "Delugia Nerd Font"; +const FONT_SIZE: f32 = 14.0; + +pub struct Renderer { + editor: Arc>, + + paint: Paint, + font: Font, + shaper: CachingShaper, + + pub font_width: f32, + pub font_height: f32, + cursor_pos: (f32, f32), + + fps_tracker: FpsTracker +} + +impl Renderer { + pub fn new(editor: Arc>) -> Renderer { + let paint = Paint::new(colors::WHITE, None); + let typeface = Typeface::new(FONT_NAME, FontStyle::default()).expect("Could not load font file."); + let font = Font::from_typeface(typeface, FONT_SIZE); + let shaper = CachingShaper::new(); + + let (_, bounds) = font.measure_str("0", Some(&paint)); + let font_width = bounds.width(); + let (_, metrics) = font.metrics(); + let font_height = metrics.descent - metrics.ascent; + let cursor_pos = (0.0, 0.0); + + let fps_tracker = FpsTracker::new(); + + Renderer { editor, paint, font, shaper, font_width, font_height, cursor_pos, fps_tracker } + } + + fn draw_background(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors) { + let (grid_x, grid_y) = grid_pos; + let x = grid_x as f32 * self.font_width; + let y = grid_y as f32 * self.font_height; + let width = text.chars().count() as f32 * self.font_width; + let height = self.font_height; + let region = Rect::new(x, y, x + width, y + height); + self.paint.set_color(style.background(default_colors).to_color()); + canvas.draw_rect(region, &self.paint); + } + + fn draw_foreground(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors, update_cache: bool) { + let (grid_x, grid_y) = grid_pos; + let x = grid_x as f32 * self.font_width; + let y = grid_y as f32 * self.font_height; + let width = text.chars().count() as f32 * self.font_width; + + if style.underline || style.undercurl { + let (_, metrics) = self.font.metrics(); + let line_position = metrics.underline_position().unwrap(); + + self.paint.set_color(style.special(&default_colors).to_color()); + canvas.draw_line((x, y - line_position + self.font_height), (x + width, y - line_position + self.font_height), &self.paint); + } + + self.paint.set_color(style.foreground(&default_colors).to_color()); + let text = text.trim_end(); + if text.len() > 0 { + let reference; + let blob = if update_cache { + self.shaper.shape_cached(text.to_string(), &self.font) + } else { + reference = self.shaper.shape(text, &self.font); + &reference + }; + canvas.draw_text_blob(blob, (x, y), &self.paint); + } + } + + fn draw_text(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors, update_cache: bool) { + self.draw_background(canvas, text, grid_pos, style, default_colors); + self.draw_foreground(canvas, text, grid_pos, style, default_colors, update_cache); + } + + pub fn draw(&mut self, canvas: &mut Canvas) { + let (draw_commands, default_colors, (width, height), cursor_grid_pos, cursor_type, cursor_foreground, cursor_background, cursor_enabled) = { + let editor = self.editor.lock().unwrap(); + ( + editor.build_draw_commands().clone(), + editor.default_colors.clone(), + editor.size.clone(), + editor.cursor_pos.clone(), + editor.cursor_type.clone(), + editor.cursor_foreground(), + editor.cursor_background(), + editor.cursor_enabled + ) + }; + + canvas.clear(default_colors.background.clone().unwrap().to_color()); + + for command in draw_commands.iter() { + self.draw_background(canvas, &command.text, command.grid_position.clone(), &command.style, &default_colors); + } + for command in draw_commands { + self.draw_foreground(canvas, &command.text, command.grid_position.clone(), &command.style, &default_colors, true); + } + + self.fps_tracker.record_frame(); + self.draw_text(canvas, &self.fps_tracker.fps.to_string(), (width - 2, height - 1), &Style::new(default_colors.clone()), &default_colors, false); + + let (cursor_grid_x, cursor_grid_y) = cursor_grid_pos; + let target_cursor_x = cursor_grid_x as f32 * self.font_width; + let target_cursor_y = cursor_grid_y as f32 * self.font_height; + let (previous_cursor_x, previous_cursor_y) = self.cursor_pos; + + let cursor_x = (target_cursor_x - previous_cursor_x) * 0.5 + previous_cursor_x; + let cursor_y = (target_cursor_y - previous_cursor_y) * 0.5 + previous_cursor_y; + + self.cursor_pos = (cursor_x, cursor_y); + if cursor_enabled { + let cursor_width = match cursor_type { + CursorType::Vertical => self.font_width / 8.0, + CursorType::Horizontal | CursorType::Block => self.font_width + }; + let cursor_height = match cursor_type { + CursorType::Horizontal => self.font_width / 8.0, + CursorType::Vertical | CursorType::Block => self.font_height + }; + let cursor = Rect::new(cursor_x, cursor_y, cursor_x + cursor_width, cursor_y + cursor_height); + self.paint.set_color(cursor_background.to_color()); + canvas.draw_rect(cursor, &self.paint); + + if let CursorType::Block = cursor_type { + self.paint.set_color(cursor_foreground.to_color()); + let editor = self.editor.lock().unwrap(); + let character = editor.grid[cursor_grid_y as usize][cursor_grid_x as usize].clone() + .map(|(character, _)| character) + .unwrap_or(' '); + canvas.draw_text_blob( + self.shaper.shape_cached(character.to_string(), &self.font), + (cursor_x, cursor_y), &self.paint); + } + } + } +} diff --git a/src/window.rs b/src/window.rs index 30ecf3b..ae61c89 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,230 +1,18 @@ use std::sync::{Arc, Mutex}; -use std::time::Instant; - -use lru::LruCache; - use skulpin::{CoordinateSystem, RendererBuilder}; -use skulpin::skia_safe::{Canvas, colors, Font, FontStyle, Paint, Point, Rect, Shaper, TextBlob, Typeface}; use skulpin::skia_safe::icu; use skulpin::winit::dpi::LogicalSize; use skulpin::winit::event::{ElementState, Event, MouseScrollDelta, WindowEvent}; use skulpin::winit::event_loop::{ControlFlow, EventLoop}; -use skulpin::winit::window::{Window, WindowBuilder}; - +use skulpin::winit::window::WindowBuilder; use neovim_lib::{Neovim, NeovimApi}; - -use crate::editor::{Editor, CursorType, Style, Colors}; +use crate::editor::Editor; use crate::keybindings::construct_keybinding_string; - -const FONT_NAME: &str = "Delugia Nerd Font"; -const FONT_SIZE: f32 = 14.0; - -struct CachingShaper { - shaper: Shaper, - cache: LruCache -} - -impl CachingShaper { - pub fn new() -> CachingShaper { - CachingShaper { - shaper: Shaper::new(None), - cache: LruCache::new(1000) - } - } - - pub fn shape(&self, text: &str, font: &Font) -> TextBlob { - let (blob, _) = self.shaper.shape_text_blob(text, font, true, 1000000.0, Point::default()).unwrap(); - blob - } - - pub fn shape_cached(&mut self, text: String, font: &Font) -> &TextBlob { - if !self.cache.contains(&text) { - self.cache.put(text.clone(), self.shape(&text, &font)); - } - - self.cache.get(&text).unwrap() - } -} - -struct FpsTracker { - last_record_time: Instant, - frame_count: usize, - fps: usize -} - -impl FpsTracker { - pub fn new() -> FpsTracker { - FpsTracker { - fps: 0, - last_record_time: Instant::now(), - frame_count: 0 - } - } - - pub fn record_frame(&mut self) { - self.frame_count = self.frame_count + 1; - let now = Instant::now(); - let time_since = (now - self.last_record_time).as_secs_f32(); - if time_since > 1.0 { - self.fps = self.frame_count; - self.last_record_time = now; - self.frame_count = 0; - } - } -} - -struct Renderer { - editor: Arc>, - - title: String, - - paint: Paint, - font: Font, - shaper: CachingShaper, - - font_width: f32, - font_height: f32, - cursor_pos: (f32, f32), - - fps_tracker: FpsTracker -} - -impl Renderer { - pub fn new(editor: Arc>) -> Renderer { - let title = "".to_string(); - - let paint = Paint::new(colors::WHITE, None); - let typeface = Typeface::new(FONT_NAME, FontStyle::default()).expect("Could not load font file."); - let font = Font::from_typeface(typeface, FONT_SIZE); - let shaper = CachingShaper::new(); - - let (_, bounds) = font.measure_str("0", Some(&paint)); - let font_width = bounds.width(); - let (_, metrics) = font.metrics(); - let font_height = metrics.descent - metrics.ascent; - let cursor_pos = (0.0, 0.0); - - let fps_tracker = FpsTracker::new(); - - Renderer { editor, title, paint, font, shaper, font_width, font_height, cursor_pos, fps_tracker } - } - - fn draw_background(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors) { - let (grid_x, grid_y) = grid_pos; - let x = grid_x as f32 * self.font_width; - let y = grid_y as f32 * self.font_height; - let width = text.chars().count() as f32 * self.font_width; - let height = self.font_height; - let region = Rect::new(x, y, x + width, y + height); - self.paint.set_color(style.background(default_colors).to_color()); - canvas.draw_rect(region, &self.paint); - } - - fn draw_foreground(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors, update_cache: bool) { - let (grid_x, grid_y) = grid_pos; - let x = grid_x as f32 * self.font_width; - let y = grid_y as f32 * self.font_height; - let width = text.chars().count() as f32 * self.font_width; - - if style.underline || style.undercurl { - let (_, metrics) = self.font.metrics(); - let line_position = metrics.underline_position().unwrap(); - - self.paint.set_color(style.special(&default_colors).to_color()); - canvas.draw_line((x, y - line_position + self.font_height), (x + width, y - line_position + self.font_height), &self.paint); - } - - self.paint.set_color(style.foreground(&default_colors).to_color()); - let text = text.trim_end(); - if text.len() > 0 { - let reference; - let blob = if update_cache { - self.shaper.shape_cached(text.to_string(), &self.font) - } else { - reference = self.shaper.shape(text, &self.font); - &reference - }; - canvas.draw_text_blob(blob, (x, y), &self.paint); - } - } - - fn draw_text(&mut self, canvas: &mut Canvas, text: &str, grid_pos: (u64, u64), style: &Style, default_colors: &Colors, update_cache: bool) { - self.draw_background(canvas, text, grid_pos, style, default_colors); - self.draw_foreground(canvas, text, grid_pos, style, default_colors, update_cache); - } - - pub fn draw(&mut self, window: &Window, canvas: &mut Canvas) { - let (draw_commands, title, default_colors, (width, height), cursor_grid_pos, cursor_type, cursor_foreground, cursor_background, cursor_enabled) = { - let editor = self.editor.lock().unwrap(); - ( - editor.build_draw_commands().clone(), - editor.title.clone(), - editor.default_colors.clone(), - editor.size.clone(), - editor.cursor_pos.clone(), - editor.cursor_type.clone(), - editor.cursor_foreground(), - editor.cursor_background(), - editor.cursor_enabled - ) - }; - - canvas.clear(default_colors.background.clone().unwrap().to_color()); - - for command in draw_commands.iter() { - self.draw_background(canvas, &command.text, command.grid_position.clone(), &command.style, &default_colors); - } - for command in draw_commands { - self.draw_foreground(canvas, &command.text, command.grid_position.clone(), &command.style, &default_colors, true); - } - - self.fps_tracker.record_frame(); - self.draw_text(canvas, &self.fps_tracker.fps.to_string(), (width - 2, height - 1), &Style::new(default_colors.clone()), &default_colors, false); - - let (cursor_grid_x, cursor_grid_y) = cursor_grid_pos; - let target_cursor_x = cursor_grid_x as f32 * self.font_width; - let target_cursor_y = cursor_grid_y as f32 * self.font_height; - let (previous_cursor_x, previous_cursor_y) = self.cursor_pos; - - let cursor_x = (target_cursor_x - previous_cursor_x) * 0.5 + previous_cursor_x; - let cursor_y = (target_cursor_y - previous_cursor_y) * 0.5 + previous_cursor_y; - - self.cursor_pos = (cursor_x, cursor_y); - if cursor_enabled { - let cursor_width = match cursor_type { - CursorType::Vertical => self.font_width / 8.0, - CursorType::Horizontal | CursorType::Block => self.font_width - }; - let cursor_height = match cursor_type { - CursorType::Horizontal => self.font_width / 8.0, - CursorType::Vertical | CursorType::Block => self.font_height - }; - let cursor = Rect::new(cursor_x, cursor_y, cursor_x + cursor_width, cursor_y + cursor_height); - self.paint.set_color(cursor_background.to_color()); - canvas.draw_rect(cursor, &self.paint); - - if let CursorType::Block = cursor_type { - self.paint.set_color(cursor_foreground.to_color()); - let editor = self.editor.lock().unwrap(); - let character = editor.grid[cursor_grid_y as usize][cursor_grid_x as usize].clone() - .map(|(character, _)| character) - .unwrap_or(' '); - canvas.draw_text_blob( - self.shaper.shape_cached(character.to_string(), &self.font), - (cursor_x, cursor_y), &self.paint); - } - } - - if self.title != title { - window.set_title(&title); - self.title = title; - } - } -} +use crate::renderer::Renderer; pub fn ui_loop(editor: Arc>, nvim: Neovim, initial_size: (u64, u64)) { let mut nvim = nvim; - let mut renderer = Renderer::new(editor); + let mut renderer = Renderer::new(editor.clone()); let event_loop = EventLoop::<()>::with_user_event(); let (width, height) = initial_size; @@ -345,8 +133,13 @@ pub fn ui_loop(editor: Arc>, nvim: Neovim, initial_size: (u64, u64 event: WindowEvent::RedrawRequested, .. } => { + { + let editor = editor.lock().unwrap(); + window.set_title(&editor.title); + }; + if let Err(e) = skulpin_renderer.draw(&window, |canvas, _coordinate_system_helper| { - renderer.draw(&window, canvas); + renderer.draw(canvas); }) { println!("Error during draw: {:?}", e); *control_flow = ControlFlow::Exit