Merge main into release for EffectCraft v0.24.0

This commit is contained in:
FredApps committed 2026-11-11 16:57:24 +01:00
commit 80b06319f5
162 files changed
+10435 -986

No files matched your search

+6
View File
@@ -129,6 +129,12 @@ configuration from these Apache-2.0 projects:
- `crates/segment/src/mobilesam.rs` follows the MobileSAM, TinyViT and Segment Anything
architectures and their Apache-2.0 reference code.
## Frozen matte regression fixture
| Asset | Author | Source | Licence |
|---|---|---|---|
| `crates/engine/src/tests/fixtures/object-matte-0.22.ecproj` | lisn0 | Original work, synthetic project saved by EffectCraft 0.22.0 | MIT OR Apache-2.0 |
## First-party brand marks
The ArtCraft name, wordmark and logos are trademarks of the ArtCraft Team. They are not open source and are not
+1 -1
View File
@@ -26,7 +26,7 @@ that variable set.
```sh
cargo run -p effectcraft -- --demo # the desktop app, with the demo project
cargo test --workspace
cargo xtask ci # what every commit must pass
cargo xtask ci # what every commit must pass (dev-profile checks and tests)
```
The default `dev` profile keeps debug info to line tables: builds are fast and panics/backtraces
Generated
+189 -80
View File
@@ -1092,7 +1092,7 @@ dependencies = [
[[package]]
name = "craft-docpath"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
[[package]]
name = "craft-ecproj-format"
@@ -1107,12 +1107,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -1143,7 +1143,7 @@ dependencies = [
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-syntax",
"thiserror 2.0.21",
@@ -1152,7 +1152,7 @@ dependencies = [
[[package]]
name = "craft-quit"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"dispatch2",
"egui",
@@ -1163,7 +1163,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -1420,8 +1420,9 @@ dependencies = [
[[package]]
name = "effectcraft"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"chrono",
"cpal",
"craft-update",
"effectcraft-engine",
@@ -1439,6 +1440,7 @@ dependencies = [
"rfd",
"serde",
"serde_json",
"sysinfo",
"winit",
"winresource",
"zbus",
@@ -1446,7 +1448,7 @@ dependencies = [
[[package]]
name = "effectcraft-aep"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-color",
"effectcraft-effects",
@@ -1460,7 +1462,7 @@ dependencies = [
[[package]]
name = "effectcraft-automation"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-engine",
"effectcraft-host",
@@ -1474,14 +1476,14 @@ dependencies = [
[[package]]
name = "effectcraft-av1enc"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"filmcraft-av1",
]
[[package]]
name = "effectcraft-cli"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"cpu-time",
"effectcraft-automation",
@@ -1500,7 +1502,7 @@ dependencies = [
[[package]]
name = "effectcraft-color"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"proptest",
"serde",
@@ -1508,7 +1510,7 @@ dependencies = [
[[package]]
name = "effectcraft-effects"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"chrono",
"effectcraft-color",
@@ -1526,7 +1528,7 @@ dependencies = [
[[package]]
name = "effectcraft-engine"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"chrono",
"craft-docpath",
@@ -1565,7 +1567,7 @@ dependencies = [
[[package]]
name = "effectcraft-export"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-av1enc",
"effectcraft-color",
@@ -1596,7 +1598,7 @@ dependencies = [
[[package]]
name = "effectcraft-expr"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"boa_engine",
"effectcraft-color",
@@ -1613,7 +1615,7 @@ dependencies = [
[[package]]
name = "effectcraft-geom"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"proptest",
"serde",
@@ -1621,7 +1623,7 @@ dependencies = [
[[package]]
name = "effectcraft-gpu"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"bytemuck",
"effectcraft-color",
@@ -1644,7 +1646,7 @@ dependencies = [
[[package]]
name = "effectcraft-helper-host"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"bytemuck",
"effectcraft-effects",
@@ -1655,14 +1657,14 @@ dependencies = [
[[package]]
name = "effectcraft-hevcenc"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"filmcraft-hevc",
]
[[package]]
name = "effectcraft-host"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-engine",
"effectcraft-export",
@@ -1689,7 +1691,7 @@ dependencies = [
[[package]]
name = "effectcraft-interchange"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-color",
"effectcraft-keyframe",
@@ -1708,7 +1710,7 @@ dependencies = [
[[package]]
name = "effectcraft-keyframe"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-color",
"effectcraft-geom",
@@ -1720,7 +1722,7 @@ dependencies = [
[[package]]
name = "effectcraft-lottie"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"craft-docpath",
"effectcraft-color",
@@ -1738,7 +1740,7 @@ dependencies = [
[[package]]
name = "effectcraft-media"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"craft-docpath",
"effectcraft-color",
@@ -1771,7 +1773,7 @@ dependencies = [
[[package]]
name = "effectcraft-model"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-geom",
"image",
@@ -1784,14 +1786,14 @@ dependencies = [
[[package]]
name = "effectcraft-opusenc"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"filmcraft-opus",
]
[[package]]
name = "effectcraft-path"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-geom",
"effectcraft-keyframe",
@@ -1803,7 +1805,7 @@ dependencies = [
[[package]]
name = "effectcraft-pdf"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"craft-pdf",
"effectcraft-raster",
@@ -1819,7 +1821,7 @@ dependencies = [
[[package]]
name = "effectcraft-plugin"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-effects",
"effectcraft-keyframe",
@@ -1831,7 +1833,7 @@ dependencies = [
[[package]]
name = "effectcraft-project"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"craft-ecproj-format",
"effectcraft-color",
@@ -1845,14 +1847,14 @@ dependencies = [
[[package]]
name = "effectcraft-psd"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"miniz_oxide 0.9.1",
]
[[package]]
name = "effectcraft-raster"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-color",
"effectcraft-geom",
@@ -1862,7 +1864,7 @@ dependencies = [
[[package]]
name = "effectcraft-render"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-color",
"effectcraft-effects",
@@ -1885,7 +1887,7 @@ dependencies = [
[[package]]
name = "effectcraft-sam2"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"bytemuck",
"candle-core",
@@ -1900,7 +1902,7 @@ dependencies = [
[[package]]
name = "effectcraft-script"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"boa_engine",
"effectcraft-engine",
@@ -1918,7 +1920,7 @@ dependencies = [
[[package]]
name = "effectcraft-segment"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"miniz_oxide 0.9.1",
"ndarray",
@@ -1927,7 +1929,7 @@ dependencies = [
[[package]]
name = "effectcraft-svg"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-geom",
"effectcraft-path",
@@ -1939,7 +1941,7 @@ dependencies = [
[[package]]
name = "effectcraft-text"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-keyframe",
"harfrust",
@@ -1951,7 +1953,7 @@ dependencies = [
[[package]]
name = "effectcraft-time"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"proptest",
"serde",
@@ -1959,7 +1961,7 @@ dependencies = [
[[package]]
name = "effectcraft-track"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-raster",
"effectcraft-segment",
@@ -1971,7 +1973,7 @@ dependencies = [
[[package]]
name = "effectcraft-ui-egui"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"bytemuck",
"chrono",
@@ -2012,11 +2014,11 @@ dependencies = [
[[package]]
name = "effectcraft-vp9enc"
version = "0.23.1"
version = "0.24.0"
[[package]]
name = "effectcraft-web"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"effectcraft-engine",
"effectcraft-export",
@@ -3068,7 +3070,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"
dependencies = [
"rustix 1.1.5",
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -3321,7 +3323,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a"
[[package]]
name = "hayro-jbig2"
version = "0.3.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-ccitt",
]
@@ -3335,7 +3337,7 @@ checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1"
[[package]]
name = "hayro-syntax"
version = "0.7.2"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
dependencies = [
"hayro-jbig2",
"hayro-jpeg2000",
@@ -3668,7 +3670,7 @@ dependencies = [
"simd_cesu8",
"thiserror 2.0.21",
"walkdir",
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -3814,7 +3816,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
"cfg-if",
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -4120,6 +4122,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "ntapi"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
dependencies = [
"winapi",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
@@ -4434,6 +4445,16 @@ dependencies = [
"objc2-core-foundation",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]]
name = "objc2-io-surface"
version = "0.3.2"
@@ -4664,7 +4685,7 @@ dependencies = [
"libc",
"redox_syscall 0.5.18",
"smallvec",
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -6071,6 +6092,20 @@ dependencies = [
"walkdir",
]
[[package]]
name = "sysinfo"
version = "0.37.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16607d5caffd1c07ce073528f9ed972d88db15dd44023fa57142963be3feb11f"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows 0.61.3",
]
[[package]]
name = "tag_ptr"
version = "0.1.0"
@@ -7224,16 +7259,38 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows"
version = "0.61.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893"
dependencies = [
"windows-collections 0.2.0",
"windows-core 0.61.2",
"windows-future 0.2.1",
"windows-link 0.1.3",
"windows-numerics 0.2.0",
]
[[package]]
name = "windows"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
dependencies = [
"windows-collections",
"windows-collections 0.3.2",
"windows-core 0.62.2",
"windows-future",
"windows-numerics",
"windows-future 0.3.2",
"windows-numerics 0.3.1",
]
[[package]]
name = "windows-collections"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8"
dependencies = [
"windows-core 0.61.2",
]
[[package]]
@@ -7255,6 +7312,19 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link 0.1.3",
"windows-result 0.3.4",
"windows-strings 0.4.2",
]
[[package]]
name = "windows-core"
version = "0.62.2"
@@ -7263,9 +7333,20 @@ checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-link 0.2.1",
"windows-result 0.4.1",
"windows-strings",
"windows-strings 0.5.1",
]
[[package]]
name = "windows-future"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e"
dependencies = [
"windows-core 0.61.2",
"windows-link 0.1.3",
"windows-threading 0.1.0",
]
[[package]]
@@ -7275,8 +7356,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
dependencies = [
"windows-core 0.62.2",
"windows-link",
"windows-threading",
"windows-link 0.2.1",
"windows-threading 0.2.1",
]
[[package]]
@@ -7301,12 +7382,28 @@ dependencies = [
"syn 2.0.119",
]
[[package]]
name = "windows-link"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-numerics"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1"
dependencies = [
"windows-core 0.61.2",
"windows-link 0.1.3",
]
[[package]]
name = "windows-numerics"
version = "0.3.1"
@@ -7314,7 +7411,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
dependencies = [
"windows-core 0.62.2",
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -7323,9 +7420,9 @@ version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720"
dependencies = [
"windows-link",
"windows-link 0.2.1",
"windows-result 0.4.1",
"windows-strings",
"windows-strings 0.5.1",
]
[[package]]
@@ -7337,13 +7434,31 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-result"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
"windows-link 0.2.1",
]
[[package]]
name = "windows-strings"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
@@ -7352,7 +7467,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -7388,7 +7503,7 @@ version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -7422,13 +7537,22 @@ dependencies = [
"windows_x86_64_msvc 0.52.6",
]
[[package]]
name = "windows-threading"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6"
dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-threading"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
dependencies = [
"windows-link",
"windows-link 0.2.1",
]
[[package]]
@@ -7711,7 +7835,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
[[package]]
name = "xtask"
version = "0.23.1"
version = "0.24.0"
dependencies = [
"chrono",
"pkg-config",
@@ -8037,18 +8161,3 @@ dependencies = [
"syn 3.0.6",
"winnow",
]
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_system_font_provider"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
+9 -10
View File
@@ -4,16 +4,16 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*"]
[workspace.package]
version = "0.23.1"
version = "0.24.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
repository = "https://github.com/storytold/effectcraft"
[workspace.dependencies]
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ca1b5e2edb8568335c6c14f390307bd5beae8238" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
@@ -117,7 +117,7 @@ half = "2.7"
# ui
# Shared frontend components; keep this dependency above the engine boundary.
# Shared UI and the complete egui family select reviewed immutable commits below.
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["serde"] }
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", features = ["serde"] }
# `default_fonts` and `monochrome_emoji_fonts` are explicit: the app's `FontDefinitions::default()`
# and its `✔`/`▣` chrome glyphs need both, and kittest's own dev-dependencies do not propagate.
egui = { version = "0.37", default-features = false, features = ["default_fonts", "monochrome_emoji_fonts"] }
@@ -242,8 +242,8 @@ lto = "thin"
codegen-units = 1
[patch.crates-io]
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.3.0" }
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545", version = "=0.3.0" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
@@ -254,8 +254,7 @@ egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc79968
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
# Patch a crate only together with the dependency that needs it: an unused patch makes the Nix
# build, which uses vendored sources, fail (`cargo xtask lock-patches`).
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
+60 -1
View File
@@ -31,6 +31,7 @@
//! effectcraft-cli bench --roto [--frames N] [--sam2 model.safetensors] [--mobilesam mobile_sam.pt] [--fixes] [--clip FRAMES --truth MASKS] [--json] Roto Brush trackers on synthetic clips with known mattes
//! (classical engine, MobileSAM and SAM 2.1 (any installed size) when installed; IoU, flicker, re-appearance, seconds per frame)
//! effectcraft-cli script FILE.jsx [F.ecproj] | --eval CODE run an After Effects-style script
//! effectcraft-cli replay SCRIPT.jsonl [--keep-going] replay a repro script (docs/agents.md)
//! effectcraft-cli mcp [--autosave | --bridge PORT] MCP server on stdio
//!
//! Project: --project F.ecproj (or a positional *.ecproj) | --demo | --empty (default: demo; mcp: empty)
@@ -97,7 +98,7 @@ fn stdout_broken() -> bool {
STDOUT_FAILED.get().is_some_and(|k| *k != std::io::ErrorKind::BrokenPipe)
}
const USAGE: &str = "usage: effectcraft-cli <info|commands|exec|run|props|get|set|render-frame|render|script|mcp> [args] [--json]
const USAGE: &str = "usage: effectcraft-cli <info|commands|exec|run|props|get|set|render-frame|render|script|replay|mcp> [args] [--json]
info project + engine summary
commands [--filter TEXT] [--enabled] list engine commands
exec <command-id> [--params JSON] run one engine command (exec --list: list them, as `commands`)
@@ -138,6 +139,9 @@ const USAGE: &str = "usage: effectcraft-cli <info|commands|exec|run|props|get|se
script FILE.jsx [F.ecproj] | --eval CODE run JavaScript with the After Effects-style object model
(app.project, comps, layers, properties…); prints writeLn
output and the result; errors exit 1 with file:line:col
replay SCRIPT.jsonl [--keep-going] replay a repro script headless (journal.export, crash repros):
per-step ok/error and the first failure; exits 1 when a step
fails, an expectation is not met or a command panics
mcp [--autosave | --bridge PORT] MCP server (JSON-RPC over stdio); --autosave preserves unsaved headless work
[--automation-read-root DIR] [--automation-write-root DIR]
confine the agent's file access: reads under a read or write root,
@@ -222,6 +226,7 @@ const FLAGS: &[&str] = &[
"--small",
"--adv3d",
"--autosave",
"--keep-going",
"--progress",
];
@@ -552,6 +557,59 @@ fn script_cmd(args: &Args, json_out: bool) -> Result<(), Failure> {
Ok(())
}
/// `replay SCRIPT.jsonl`: run a repro script against a headless session (its start line picks
/// the project) and report every step. Exits 1 when the replay fails.
fn replay_cmd(args: &Args, json_out: bool) -> Result<(), Failure> {
use effectcraft_engine::replay;
let Some(file) = args.pos.first() else { return usage_err("replay needs a .jsonl script") };
if args.opt("--bridge").is_some() || args.project.is_some() || args.flag("--demo") || args.flag("--empty") {
return usage_err("replay runs headless from the script's start line ({\"start\": \"new\" | \"demo\" | {\"project\": path}})");
}
use std::io::Read;
let path = std::path::Path::new(file);
let input = std::fs::File::open(path).map_err(|e| Failure::Error(format!("cannot read {file}: {e}")))?;
// Bound the read itself: metadata can be stale or describe a pipe rather than its contents.
let mut script = String::new();
input.take(replay::MAX_SCRIPT_BYTES as u64 + 1).read_to_string(&mut script).map_err(|e| Failure::Error(format!("cannot read {file}: {e}")))?;
if script.len() > replay::MAX_SCRIPT_BYTES {
return Err(Failure::Error(format!("{file} is larger than {} MB", replay::MAX_SCRIPT_BYTES >> 20)));
}
let mut s = session(args)?;
let opts = replay::Options { keep_going: args.flag("--keep-going"), base_dir: path.parent().map(std::path::Path::to_path_buf) };
let report = replay::run(&mut s, &script, &opts);
if json_out {
emit(&serde_json::to_value(&report).unwrap_or_else(|e| json!({"error": e.to_string()})), true);
} else {
for st in &report.steps {
let what = st.command.as_deref().unwrap_or(st.kind);
match (&st.error, st.ok) {
(None, _) => say!("line {:>4} ok {what}", st.line),
(Some(e), true) => say!("line {:>4} ok {what} (tolerated error: {e})", st.line),
(Some(e), false) => say!("line {:>4} FAIL {what}: {e}", st.line),
}
}
}
let Some(first) = &report.first_failure else {
if !json_out {
say!("replay ok: {} steps", report.steps.len());
}
return Ok(());
};
let msg = format!(
"failed at line {} ({}){}: {}{}",
first.line,
first.command.as_deref().unwrap_or(first.kind),
if report.panicked { ", a command panicked" } else { "" },
first.error.as_deref().unwrap_or(""),
if report.skipped > 0 { format!(" ({} later steps not run)", report.skipped) } else { String::new() }
);
if json_out {
note!("effectcraft-cli replay: {msg}");
std::process::exit(1);
}
Err(Failure::Error(msg))
}
/// Print a result: compact JSON with `--json`, else pretty JSON.
fn emit(v: &Value, json_out: bool) {
if json_out {
@@ -614,6 +672,7 @@ fn run(cmd: &str, args: &Args, json_out: bool) -> Result<(), Failure> {
"commands" => list_commands_cmd(args, json_out)?,
"exec" if args.flag("--list") => list_commands_cmd(args, json_out)?,
"script" => script_cmd(args, json_out)?,
"replay" => replay_cmd(args, json_out)?,
"exec" => {
let Some(id) = args.pos.first().cloned() else { return usage_err("exec needs a command id") };
let params = match (args.opt("--params"), args.pos.get(1).map(String::as_str)) {
+36
View File
@@ -1223,3 +1223,39 @@ fn render_exr_compression_and_depth() {
assert!(reply["error"].as_str().unwrap().contains("exrBitDepth: half|float"), "{reply}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn replay_reports_steps_and_the_first_failure() {
let repro = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/repros/comp-zero-frame-rate.jsonl");
let v = ok_json(&["replay", repro.to_str().unwrap()]);
assert_eq!(v["ok"], true, "{v}");
assert!(v["steps"].as_array().unwrap().len() > 5);
let bad = tmp("bad-repro.jsonl");
std::fs::write(
&bad,
"{\"start\": \"new\"}\n{\"command\": \"comp.new\"}\n{\"expect\": {\"query\": \"comp.info\", \"pointer\": \"/width\", \"equals\": 1}}\nnot json\n",
)
.unwrap();
let (code, v) = run_json(&["replay", bad.to_str().unwrap()]);
assert_eq!(code, 1);
assert_eq!(v["ok"], false);
assert_eq!(v["first_failure"]["line"], 3, "{v}");
assert_eq!(v["skipped"], 1);
let out = bin().args(["replay", bad.to_str().unwrap(), "--keep-going"]).output().unwrap();
assert_eq!(out.status.code(), Some(1));
let text = String::from_utf8_lossy(&out.stdout);
assert!(text.contains("line 2 ok comp.new") && text.contains("line 4 FAIL invalid: not JSON"), "{text}");
assert!(String::from_utf8_lossy(&out.stderr).contains("failed at line 3 (comp.info)"));
// The start line picks the project.
assert_eq!(bin().args(["replay", bad.to_str().unwrap(), "--demo"]).output().unwrap().status.code(), Some(2));
}
/// Text editors can prefix an exported JSON-lines repro with a UTF-8 BOM.
#[test]
fn replay_accepts_a_utf8_bom() {
let path = tmp("bom-repro.jsonl");
std::fs::write(&path, "\u{feff}{\"command\":\"comp.new\"}\n").unwrap();
let (code, report) = run_json(&["replay", path.to_str().unwrap()]);
assert_eq!(code, 0, "{report}");
assert_eq!(report["ok"], true);
}
+3
View File
@@ -25,6 +25,9 @@ rfd = { workspace = true }
pollster = { workspace = true }
cpal = { workspace = true }
log = { workspace = true }
chrono = { workspace = true }
# MIT; safe process handles and target-only memory observation for the crash helper.
sysinfo = { version = "=0.37.2", default-features = false, features = ["system"] }
[target.'cfg(target_os = "linux")'.dependencies]
# Native GNOME/Wayland controls without a bundled title font.
+264
View File
@@ -0,0 +1,264 @@
//! Bounded observations made outside the failed GUI. Measurements are evidence, not a cause.
use std::io::{self, Write};
use std::sync::mpsc::{self, Receiver};
use std::time::{Duration, Instant, SystemTime};
use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, ProcessesToUpdate, System};
pub fn utc() -> String {
chrono::DateTime::<chrono::Utc>::from(SystemTime::now()).to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
}
#[derive(Default)]
struct Memory {
samples: u64,
resident_peak: u64,
virtual_peak: u64,
available_min: Option<u64>,
total: u64,
last_at: Option<String>,
last_uptime_secs: Option<u64>,
}
impl Memory {
fn sample(&mut self, resident: u64, virtual_bytes: u64, available: u64, total: u64) {
self.samples = self.samples.saturating_add(1);
self.resident_peak = self.resident_peak.max(resident);
self.virtual_peak = self.virtual_peak.max(virtual_bytes);
// Zero total means the platform could not supply physical-memory information.
if total != 0 {
self.available_min = Some(self.available_min.map_or(available, |old| old.min(available)));
self.total = total;
}
self.last_at = Some(utc());
}
}
pub struct Observation {
pid: Pid,
system: System,
birth: Option<u64>,
started: Instant,
started_at: String,
detected_at: Option<String>,
last_sample: Instant,
memory: Memory,
// Only Windows can retrieve an unrelated process's ExitStatus through a retained handle.
exit: Receiver<Option<std::process::ExitStatus>>,
status: Option<std::process::ExitStatus>,
alive: Option<bool>,
}
fn target_system(pid: Pid) -> System {
let mut system = System::new();
system.refresh_processes_specifics(ProcessesToUpdate::Some(&[pid]), false, ProcessRefreshKind::nothing().with_memory());
system
}
impl Observation {
/// Acquire the Windows handle before acknowledging reporter readiness, so even an immediate
/// GUI abort leaves an observable process object. Never enumerate commands or environments.
pub fn start(pid: u32) -> Self {
let pid = Pid::from_u32(pid);
let system = target_system(pid);
let birth = system.process(pid).map(|p| p.start_time());
let (send, exit) = mpsc::sync_channel(1);
#[cfg(windows)]
{
let waiting = target_system(pid);
let _ = std::thread::Builder::new().name("ec-exit-status".into()).spawn(move || {
let status = waiting.process(pid).and_then(|p| p.wait());
let _ = send.send(status);
});
}
#[cfg(not(windows))]
drop(send);
let now = Instant::now();
let mut result = Self {
pid,
system,
birth,
started: now,
started_at: utc(),
detected_at: None,
last_sample: now,
memory: Memory::default(),
exit,
status: None,
alive: None,
};
result.sample();
result
}
fn sample(&mut self) {
self.system.refresh_processes_specifics(ProcessesToUpdate::Some(&[self.pid]), false, ProcessRefreshKind::nothing().with_memory());
self.system.refresh_memory_specifics(MemoryRefreshKind::nothing().with_ram());
let process = self.system.process(self.pid).filter(|p| Some(p.start_time()) == self.birth && p.exists());
self.alive = self.birth.map(|_| process.is_some());
if let Some(process) = process {
self.memory.last_uptime_secs = Some(process.run_time());
self.memory.sample(process.memory(), process.virtual_memory(), self.system.available_memory(), self.system.total_memory());
}
if let Ok(status) = self.exit.try_recv() {
self.status = status;
}
self.last_sample = Instant::now();
}
pub fn tick(&mut self) {
if self.last_sample.elapsed() >= Duration::from_secs(1) {
self.sample();
}
}
/// Pipe closure can precede the OS signalling process termination. Bound that grace period;
/// a broken pipe or early reporter drop must never be called a confirmed process exit.
pub fn finish(&mut self) {
self.detected_at = Some(utc());
if self.status.is_none()
&& let Ok(status) = self.exit.recv_timeout(Duration::from_millis(750))
{
self.status = status;
}
self.sample();
}
pub fn confirmed_exit(&self) -> bool {
self.status.is_some()
}
pub fn write(&self, report: &mut impl Write) -> io::Result<()> {
writeln!(
report,
"Detection UTC: {}\nObserver started UTC: {}\nObserved duration: {} ms",
self.detected_at.as_deref().unwrap_or("not captured"),
self.started_at,
self.started.elapsed().as_millis()
)?;
writeln!(
report,
"Process observation: {}",
if self.confirmed_exit() {
"process termination observed"
} else if self.alive == Some(true) {
"GUI still present when the control channel ended"
} else {
"target absent from refresh or process identity/status unavailable (not proof of termination)"
}
)?;
match &self.status {
Some(status) => {
writeln!(report, "OS exit status: {status}")?;
#[cfg(windows)]
if let Some(code) = status.code() {
write_windows_status(report, code as u32)?;
}
}
None => writeln!(report, "OS exit status: unavailable (not inferred from the control pipe)")?,
}
let memory = &self.memory;
writeln!(report, "\nMemory evidence (1 s sampling; sampled maxima, not OS peaks):\nTarget-present sampling attempts: {}", memory.samples)?;
if let Some(uptime) = memory.last_uptime_secs {
writeln!(report, "GUI uptime at last target-present observation: {uptime} s")?;
}
#[cfg(windows)]
let virtual_label = "Peak sampled private committed bytes";
#[cfg(not(windows))]
let virtual_label = "Peak sampled virtual bytes";
if memory.samples != 0 {
writeln!(
report,
"Last target sampling attempt UTC: {}\nPeak sampled resident bytes: {}\n{virtual_label}: {}",
memory.last_at.as_deref().unwrap_or("unavailable"),
memory.resident_peak,
memory.virtual_peak
)?;
}
if let Some(available) = memory.available_min {
writeln!(report, "Minimum sampled system available RAM bytes: {available}\nSystem total RAM bytes at last sample: {}", memory.total)?;
}
writeln!(
report,
"Samples can miss short spikes or retain stale/zero readings when an OS query fails; per-field success is not exposed by sysinfo. Windows private commit is not GPU VRAM; on other platforms virtual memory is not physical RAM. System commit limit/usage and GPU memory budgets were not collected. No memory-pressure threshold establishes the cause of this incident.\nThe memory observer does not collect process command lines, environments, documents, native dumps or system event logs. The existing session-log tail below may contain paths or other logged details; review it before sharing."
)
}
}
#[cfg(any(windows, test))]
fn write_windows_status(report: &mut impl Write, code: u32) -> io::Result<()> {
let meaning = match code {
0xc0000005 => "STATUS_ACCESS_VIOLATION",
0xc0000017 => "STATUS_NO_MEMORY",
0xc000012d => "STATUS_COMMITMENT_LIMIT",
0xc0000374 => "STATUS_HEAP_CORRUPTION",
0xc0000409 => "STATUS_STACK_BUFFER_OVERRUN / fast fail",
0x40000015 => "STATUS_FATAL_APP_EXIT",
_ => "unclassified",
};
writeln!(
report,
"Windows status bits: 0x{code:08X} ({meaning})\nThese are process exit bits, not a verified exception record. Software can set an exit code explicitly; an access violation does not identify the faulting module or prove a driver crash."
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn missing_target_without_exit_status_does_not_confirm_termination() {
let (send, exit) = mpsc::sync_channel(1);
drop(send);
let now = Instant::now();
let observation = Observation {
pid: Pid::from_u32(1),
system: System::new(),
birth: Some(1),
started: now,
started_at: utc(),
detected_at: Some(utc()),
last_sample: now,
memory: Memory::default(),
exit,
status: None,
alive: Some(false),
};
assert!(!observation.confirmed_exit());
let mut report = Vec::new();
observation.write(&mut report).unwrap();
let report = String::from_utf8(report).unwrap();
assert!(report.contains("not proof of termination"));
assert!(!report.contains("process termination observed"));
}
#[test]
fn memory_high_water_marks_are_bounded_and_not_a_causal_diagnosis() {
let mut memory = Memory::default();
memory.sample(40, 80, 30, 100);
memory.sample(20, 90, 50, 100);
assert_eq!((memory.samples, memory.resident_peak, memory.virtual_peak, memory.available_min), (2, 40, 90, Some(30)));
memory.sample(0, 0, 0, 0);
assert_eq!(memory.available_min, Some(30));
}
#[test]
fn windows_status_bits_preserve_unsigned_codes_without_claiming_a_driver_crash() {
for (code, name) in
[(0xc0000005, "STATUS_ACCESS_VIOLATION"), (0xc0000017, "STATUS_NO_MEMORY"), (0xc000012d, "STATUS_COMMITMENT_LIMIT"), (42, "unclassified")]
{
let mut text = Vec::new();
write_windows_status(&mut text, code).unwrap();
let text = String::from_utf8(text).unwrap();
assert!(text.contains(name));
assert!(text.contains(&format!("0x{code:08X}")));
assert!(text.contains("not a verified exception record"));
}
}
#[test]
fn utc_timestamps_are_parseable_and_zero_offset() {
let time = chrono::DateTime::parse_from_rfc3339(&utc()).unwrap();
assert_eq!(time.offset().local_minus_utc(), 0);
}
}
+253 -47
View File
@@ -2,23 +2,31 @@
//!
//! Allocation failure can abort without a panic hook. The GUI therefore owns a pipe to a
//! helper which never loads a project, font database or GPU. Only an explicit marker after
//! normal teardown suppresses the report. EOF alone identifies an unexpected exit, not its
//! cause. System-wide exhaustion can still prevent the helper or its native dialog working.
//! normal teardown suppresses the report. EOF alone identifies a lost control channel, not
//! a process exit or its cause. System-wide exhaustion can still prevent the helper or its native dialog working.
use std::collections::VecDeque;
use std::ffi::{OsStr, OsString};
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::sync::mpsc::{self, RecvTimeoutError};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
#[path = "crash_observation.rs"]
mod observation;
const FLAG: &str = "--internal-crash-reporter";
const CLEAN: &[u8] = b"EFFECTCRAFT_CLEAN_EXIT_V1\n";
const LOG_BYTES: usize = 64 * 1024;
const HEADER_BYTES: usize = 128;
const READY: &[u8] = b"READY\n";
const EVENT_BYTES: usize = 128;
const EVENT_COUNT: usize = 32;
/// Owned only by the GUI, never handed to another child. Dropping it without `finish` closes
/// the pipe and reports an unexpected exit; there is intentionally no clean-exit Drop hook.
/// the pipe and reports the incident; there is intentionally no clean-exit Drop hook.
pub struct Reporter {
pipe: ChildStdin,
child: Child,
@@ -72,6 +80,20 @@ impl Shutdown {
Ok(())
}
/// Keep only fixed-vocabulary, bounded lifecycle facts outside the GUI. Callers emit each
/// category once; their combined payload fits well below the smallest OS pipe buffer.
pub fn record(&self, event: &'static str) -> io::Result<()> {
if event.len() > 100 || event.contains(['\n', '\r']) {
return Err(io::Error::other("invalid diagnostic observation"));
}
let mut slot = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?;
if let Some(reporter) = slot.as_mut() {
writeln!(reporter.pipe, "EVENT {event}")?;
reporter.pipe.flush()?;
}
Ok(())
}
/// Consume the reporter exactly once, after the applicable platform teardown boundary.
pub fn finish(&self) -> io::Result<()> {
let reporter = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?.take();
@@ -105,12 +127,18 @@ pub fn start(id: &str, log: Option<&Path>, reports: &Path, interactive: bool) ->
log.map_or_else(OsString::new, |p| p.as_os_str().to_owned()),
reports.as_os_str().to_owned(),
OsString::from(if interactive { "1" } else { "0" }),
OsString::from(std::process::id().to_string()),
];
spawn(&exe, &args)
}
fn spawn(exe: &Path, args: &[OsString]) -> io::Result<Reporter> {
let mut child = Command::new(exe).args(args).stdin(Stdio::piped()).stdout(Stdio::null()).spawn()?;
let mut child = Command::new(exe).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
if let Err(error) = ready(&mut child) {
let _ = child.kill();
let _ = finish_child(&mut child, Duration::from_millis(100));
return Err(error);
}
match child.stdin.take() {
Some(pipe) => Ok(Reporter { pipe, child }),
None => {
@@ -121,6 +149,36 @@ fn spawn(exe: &Path, args: &[OsString]) -> io::Result<Reporter> {
}
}
/// A fixed acknowledgement means the helper holds the process observation before heavy GUI
/// work begins. A suspended helper cannot hold startup indefinitely.
fn ready(child: &mut Child) -> io::Result<()> {
let mut output = child.stdout.take().ok_or_else(|| io::Error::other("crash reporter has no readiness pipe"))?;
let (send, receive) = mpsc::sync_channel(1);
std::thread::Builder::new().name("ec-reporter-ready".into()).spawn(move || {
let mut bytes = [0; READY.len()];
#[cfg(not(test))]
let result =
output.read_exact(&mut bytes).and_then(|()| if bytes == READY { Ok(()) } else { Err(io::Error::other("invalid reporter acknowledgement")) });
// libtest prints its bounded banner before invoking the helper role.
#[cfg(test)]
let result = (|| {
for _ in 0..1024 {
bytes.rotate_left(1);
output.read_exact(&mut bytes[READY.len() - 1..])?;
if bytes == READY {
return Ok(());
}
}
Err(io::Error::other("reporter acknowledgement missing"))
})();
let _ = send.send(result);
// Keep the pipe open for libtest's trailing result (and future helper output), without
// storing it. Dropping it at READY would turn a clean helper exit into BrokenPipe.
let _ = io::copy(&mut output, &mut io::sink());
})?;
receive.recv_timeout(Duration::from_secs(3)).map_err(|_| io::Error::other("crash reporter readiness timed out"))?
}
/// Handle the internal mode before logging, argument parsing, updates or GUI initialization.
/// The callback is a native dialog on this helper's main thread, not on the failed GUI's.
pub fn maybe_run(args: &[OsString], show: impl FnOnce(&str)) -> bool {
@@ -128,7 +186,7 @@ pub fn maybe_run(args: &[OsString], show: impl FnOnce(&str)) -> bool {
return false;
}
let parsed = match args {
[_, id, log, reports, interactive] => id
[_, id, log, reports, interactive, pid] => id
.to_str()
.filter(|id| valid_id(id))
.zip(match interactive.to_str() {
@@ -136,27 +194,61 @@ pub fn maybe_run(args: &[OsString], show: impl FnOnce(&str)) -> bool {
Some("1") => Some(true),
_ => None,
})
.map(|(id, interactive)| (id, (!log.is_empty()).then(|| Path::new(log)), Path::new(reports), interactive)),
.zip(pid.to_str().and_then(|p| p.parse::<u32>().ok()).filter(|pid| *pid != 0))
.map(|((id, interactive), pid)| (id, (!log.is_empty()).then(|| Path::new(log)), Path::new(reports), interactive, pid)),
_ => None,
};
let Some((id, log, reports, interactive)) = parsed else {
let Some((id, log, reports, interactive, pid)) = parsed else {
eprintln!("EffectCraft crash reporter: invalid internal arguments");
return true;
};
// Keep the exact file open while the GUI is alive: a later rotation must not substitute
// another instance's log. If rotation won the startup race, the session marker rejects it.
let mut log = SessionLog::open(log, id);
match clean_exit(&mut io::stdin().lock()) {
Ok(true) => {}
result => {
let failure = result.err().map(|e| e.to_string());
let saved = write_report(reports, id, &mut log, failure.as_deref());
let description = description(&saved);
eprintln!("{description}");
if interactive {
show(&description);
let mut observation = observation::Observation::start(pid);
// A GUI that dies during helper startup can already have closed this read end. Keep
// watching its control pipe and save the report rather than losing that early failure.
let _ = io::stdout().write_all(READY).and_then(|()| io::stdout().flush());
// The reader has one bounded queue; the monitor never waits on a live pipe, so it can
// sample the target even when no log or lifecycle message is emitted.
let (send, receive) = mpsc::sync_channel(1);
let reader = std::thread::Builder::new().name("ec-reporter-pipe".into()).spawn(move || {
let mut input = io::stdin().lock();
loop {
let message = read_message(&mut input);
let terminal = !matches!(message, Ok(Message::Event(_)));
if send.send(message).is_err() || terminal {
break;
}
}
});
let mut events = VecDeque::with_capacity(EVENT_COUNT);
let failure = if reader.is_err() {
Some("control reader could not start".to_owned())
} else {
loop {
observation.tick();
match receive.recv_timeout(Duration::from_millis(250)) {
Ok(Ok(Message::Clean)) => return true,
Ok(Ok(Message::Event(event))) => {
if events.len() == EVENT_COUNT {
events.pop_front();
}
events.push_back(format!("{} {event}", observation::utc()));
}
Ok(Ok(Message::Ended)) => break None,
Ok(Err(error)) => break Some(error.to_string()),
Err(RecvTimeoutError::Timeout) => continue,
Err(RecvTimeoutError::Disconnected) => break Some("control reader disconnected".to_owned()),
}
}
};
observation.finish();
let saved = write_report(reports, id, &mut log, failure.as_deref(), &observation, &events);
let description = description(&saved, observation.confirmed_exit());
eprintln!("{description}");
if interactive {
show(&description);
}
true
}
@@ -165,20 +257,43 @@ fn valid_id(id: &str) -> bool {
!id.is_empty() && id.len() <= 80 && id.bytes().all(|c| c.is_ascii_digit() || c == b'-')
}
/// Fixed-size comparison: arbitrary/truncated input never grows a buffer or counts as clean.
fn clean_exit(input: &mut impl Read) -> io::Result<bool> {
let mut marker = [0u8; CLEAN.len()];
let mut filled = 0;
while filled < marker.len() {
let remaining = marker.get_mut(filled..).ok_or_else(|| io::Error::other("invalid control marker offset"))?;
match input.read(remaining) {
Ok(0) => return Ok(false),
Ok(n) => filled += n,
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
enum Message {
Clean,
Event(String),
Ended,
}
/// Every line and the retained event ring are bounded, including malformed control input.
fn read_message(input: &mut impl Read) -> io::Result<Message> {
let mut line = [0u8; EVENT_BYTES];
for i in 0..line.len() {
let byte = line.get_mut(i).ok_or_else(|| io::Error::other("invalid control offset"))?;
loop {
match input.read(std::slice::from_mut(byte)) {
Ok(0) => return Ok(Message::Ended),
Ok(_) => break,
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
Err(error) => return Err(error),
}
}
if *byte == b'\n' {
let text = line.get(..=i).ok_or_else(|| io::Error::other("invalid control line"))?;
if text == CLEAN {
return Ok(Message::Clean);
}
if let Some(event) = text.strip_prefix(b"EVENT ") {
let event = std::str::from_utf8(event).map_err(|_| io::Error::other("non-UTF8 diagnostic observation"))?;
return Ok(Message::Event(event.trim_end().to_owned()));
}
return Ok(Message::Ended);
}
}
Ok(marker == CLEAN)
Err(io::Error::other("control line exceeded its fixed limit"))
}
#[cfg(test)]
fn clean_exit(input: &mut impl Read) -> io::Result<bool> {
Ok(matches!(read_message(input)?, Message::Clean))
}
enum SessionLog {
@@ -228,7 +343,14 @@ impl SessionLog {
}
}
fn write_report(reports: &Path, id: &str, log: &mut SessionLog, pipe_error: Option<&str>) -> io::Result<PathBuf> {
fn write_report(
reports: &Path,
id: &str,
log: &mut SessionLog,
pipe_error: Option<&str>,
observation: &observation::Observation,
events: &VecDeque<String>,
) -> io::Result<PathBuf> {
if !valid_id(id) {
return Err(io::Error::other("invalid report identity"));
}
@@ -244,8 +366,13 @@ fn write_report(reports: &Path, id: &str, log: &mut SessionLog, pipe_error: Opti
)?;
writeln!(
report,
"The GUI's control pipe ended without its clean-exit marker.\nThe exit status and exact cause are not available to this reporter.\nMemory exhaustion, a native crash or external termination are possible.\nThis report was saved locally; it was not sent anywhere."
"The GUI control channel ended without its clean-exit marker.\nThe exact cause is not established. GPU fallback, memory pressure and process exit are separate observations, not a proven causal chain.\nThis report was saved locally; it was not sent anywhere."
)?;
observation.write(&mut report)?;
writeln!(report, "\nRetained lifecycle observations (last {EVENT_COUNT}; UTC is helper receipt time):")?;
for event in events {
writeln!(report, "{event}")?;
}
if let Some(error) = pipe_error {
writeln!(report, "Control pipe error: {error}")?;
}
@@ -256,13 +383,15 @@ fn write_report(reports: &Path, id: &str, log: &mut SessionLog, pipe_error: Opti
Ok(path)
}
fn description(saved: &io::Result<PathBuf>) -> String {
fn description(saved: &io::Result<PathBuf>, confirmed_exit: bool) -> String {
let diagnostic = match saved {
Ok(path) => format!("A diagnostic report was saved to:\n{}", path.display()),
Err(error) => format!("A diagnostic report could not be saved: {error}"),
};
let incident =
if confirmed_exit { "EffectCraft stopped unexpectedly." } else { "EffectCraft lost its diagnostic control channel; process exit is not confirmed." };
format!(
"EffectCraft stopped unexpectedly.\n\n{diagnostic}\n\nThe exact cause is unknown. Reopen EffectCraft to check for an existing auto-save. Changes since the last auto-save may be lost.\n\nNo report has been sent anywhere."
"{incident}\n\n{diagnostic}\n\nThe exact cause is unknown. If EffectCraft has closed, reopen it to check for an existing auto-save. Changes since the last auto-save may be lost.\n\nNo report has been sent anywhere."
)
}
@@ -276,6 +405,10 @@ mod tests {
static NEXT: AtomicUsize = AtomicUsize::new(0);
const ROLE: &str = "EFFECTCRAFT_CRASH_REPORT_TEST_ROLE";
fn test_report(dir: &Path, id: &str, log: &mut SessionLog) -> io::Result<PathBuf> {
write_report(dir, id, log, None, &observation::Observation::start(std::process::id()), &VecDeque::new())
}
fn folder() -> PathBuf {
let p = std::env::temp_dir().join(format!("ec-crash-report-{}-{}", run_id(), NEXT.fetch_add(1, Ordering::Relaxed)));
std::fs::create_dir_all(&p).unwrap();
@@ -283,7 +416,7 @@ mod tests {
}
fn test_name(name: &str) -> String {
// Also usable as a standalone std-only rustc test while the app's build cache is busy.
// Also usable from an isolated harness with the reporter dependencies.
let prefix = module_path!().split_once("::").map_or("", |(_, rest)| rest);
format!("{prefix}::{name}")
}
@@ -299,10 +432,12 @@ mod tests {
.env("EC_REPORT_DIR", dir)
.env("EC_REPORT_ID", id)
.env("EC_REPORT_INTERACTIVE", if interactive { "1" } else { "0" })
.env("EC_REPORT_PID", std::process::id().to_string())
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stdout(Stdio::piped())
.spawn()
.unwrap();
ready(&mut child).unwrap();
Reporter { pipe: child.stdin.take().unwrap(), child }
}
@@ -321,7 +456,7 @@ mod tests {
for mut input in [b"".as_slice(), &CLEAN[..CLEAN.len() - 1], b"clean", b"EFFECTCRAFT_CLEAN_EXIT_V0\n"] {
assert!(!clean_exit(&mut input).unwrap());
}
assert!(!clean_exit(&mut &vec![b'x'; 100_000][..]).unwrap());
assert!(clean_exit(&mut &vec![b'x'; 100_000][..]).is_err());
}
#[test]
@@ -336,9 +471,9 @@ mod tests {
let mut old = OpenOptions::new().append(true).open(dir.join("effectcraft.1.log")).unwrap();
old.write_all(&vec![b'a'; LOG_BYTES * 2]).unwrap();
old.write_all(b"\nGPU context retired\n").unwrap();
let report = write_report(&dir, id, &mut log, None).unwrap();
let report = test_report(&dir, id, &mut log).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.len() < LOG_BYTES + 2048);
assert!(text.len() < LOG_BYTES + 4096);
assert!(text.contains("GPU context retired"));
assert!(!text.contains("OTHER SESSION"));
std::fs::remove_dir_all(dir).unwrap();
@@ -350,7 +485,7 @@ mod tests {
let path = dir.join("log");
std::fs::write(&path, "private other session").unwrap();
for (id, source) in [("1-1", Some(path.as_path())), ("1-2", None)] {
let report = write_report(&dir, id, &mut SessionLog::open(source, id), None).unwrap();
let report = test_report(&dir, id, &mut SessionLog::open(source, id)).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.contains("Recent log unavailable"));
assert!(!text.contains("private other session"));
@@ -365,7 +500,7 @@ mod tests {
std::fs::write(&path, "crash-report session 1-2\noriginal session\n").unwrap();
let mut log = SessionLog::open(Some(&path), "1-2");
std::fs::write(&path, "crash-report session 3-4\nprivate other session\n").unwrap();
let report = write_report(&dir, "1-2", &mut log, None).unwrap();
let report = test_report(&dir, "1-2", &mut log).unwrap();
let text = std::fs::read_to_string(report).unwrap();
assert!(text.contains("Recent log unavailable"));
assert!(!text.contains("private other session"));
@@ -388,13 +523,13 @@ mod tests {
fn report_write_failure_is_described_and_existing_report_is_not_overwritten() {
let dir = folder();
let mut log = SessionLog::Unavailable("no log".into());
let first = write_report(&dir, "2-1", &mut log, None).unwrap();
let first = test_report(&dir, "2-1", &mut log).unwrap();
let before = std::fs::read(&first).unwrap();
let result = write_report(&dir, "2-1", &mut log, None);
let result = test_report(&dir, "2-1", &mut log);
assert!(result.is_err());
assert!(description(&result).contains("could not be saved"));
assert!(description(&result, false).contains("could not be saved"));
assert_eq!(std::fs::read(first).unwrap(), before);
assert!(write_report(&dir, "../escape", &mut log, None).is_err());
assert!(test_report(&dir, "../escape", &mut log).is_err());
std::fs::remove_dir_all(dir).unwrap();
}
@@ -470,7 +605,10 @@ mod tests {
assert!(!status.success());
wait_file(&dir.join("ready-4-1"));
let text = std::fs::read_to_string(dir.join("crash-4-1.txt")).unwrap();
assert!(text.contains("exact cause are not available"));
assert!(text.contains("exact cause is not established"));
assert!(text.contains("Detection UTC:"));
#[cfg(windows)]
assert!(text.contains("Windows status bits:"));
std::fs::remove_dir_all(dir).unwrap();
}
@@ -531,15 +669,70 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn lost_pipe_keeps_bounded_lifecycle_evidence_without_claiming_process_exit() {
let dir = folder();
let reporter = Shutdown::default();
reporter.install(helper(&dir, "9-1")).unwrap();
for _ in 0..40 {
reporter.record("GPU observation: wgpu validation error").unwrap();
}
drop(reporter);
wait_file(&dir.join("ready-9-1"));
let text = std::fs::read_to_string(dir.join("crash-9-1.txt")).unwrap();
assert!(text.contains("GUI still present"));
assert!(!text.contains("process termination observed"));
assert_eq!(text.matches("GPU observation: wgpu validation error").count(), EVENT_COUNT);
assert!(text.contains("Target-present sampling attempts:"));
assert!(text.len() < 8192);
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn clean_exit_after_lifecycle_events_does_not_write_a_report() {
let dir = folder();
let reporter = Shutdown::default();
reporter.install(helper(&dir, "9-2")).unwrap();
reporter.record("Backend selection source: default").unwrap();
reporter.finish().unwrap();
wait_file(&dir.join("ready-9-2"));
assert!(!dir.join("crash-9-2.txt").exists());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
#[cfg(windows)]
fn windows_report_preserves_real_unsigned_exit_bits() {
let dir = folder();
let status = Command::new(std::env::current_exe().unwrap())
.args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"])
.env(ROLE, "windows-status")
.env("EC_REPORT_DIR", &dir)
.stdout(Stdio::null())
.status()
.unwrap();
assert_eq!(status.code().map(|code| code as u32), Some(0xc0000017));
wait_file(&dir.join("ready-9-3"));
let text = std::fs::read_to_string(dir.join("crash-9-3.txt")).unwrap();
assert!(text.contains("Windows status bits: 0xC0000017 (STATUS_NO_MEMORY)"));
assert!(text.contains("not a verified exception record"));
assert!(text.contains("process termination observed"));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn internal_mode_rejects_invalid_arguments_without_launching_the_app() {
assert!(!maybe_run(&[], |_| panic!("no dialog")));
assert!(!maybe_run(&[OsString::from("--version")], |_| panic!("no dialog")));
assert!(maybe_run(&[OsString::from(FLAG)], |_| panic!("no dialog")));
let args = [FLAG, "../escape", "", "", "1"].map(OsString::from);
let args = [FLAG, "../escape", "", "", "1", "1"].map(OsString::from);
assert!(maybe_run(&args, |_| panic!("no dialog")));
let args = [FLAG, "1-1", "", "", "invalid"].map(OsString::from);
let args = [FLAG, "1-1", "", "", "invalid", "1"].map(OsString::from);
assert!(maybe_run(&args, |_| panic!("no dialog")));
for pid in ["0", "not-a-pid", "4294967296"] {
let args = [FLAG, "1-1", "", "", "0", pid].map(OsString::from);
assert!(maybe_run(&args, |_| panic!("no dialog")));
}
}
#[test]
@@ -556,7 +749,14 @@ mod tests {
let dir = PathBuf::from(std::env::var_os("EC_REPORT_DIR").unwrap());
let id = std::env::var("EC_REPORT_ID").unwrap();
let interactive = std::env::var("EC_REPORT_INTERACTIVE").unwrap();
let args = [OsString::from(FLAG), OsString::from(&id), OsString::new(), dir.as_os_str().to_owned(), OsString::from(&interactive)];
let args = [
OsString::from(FLAG),
OsString::from(&id),
OsString::new(),
dir.as_os_str().to_owned(),
OsString::from(&interactive),
std::env::var_os("EC_REPORT_PID").unwrap(),
];
assert!(maybe_run(&args, |description| {
assert_eq!(interactive, "1", "non-interactive reporter must not show a dialog");
assert!(dir.join(format!("crash-{id}.txt")).exists());
@@ -577,6 +777,12 @@ mod tests {
after_teardown(Some(&reporter.clone()), || {}).unwrap();
std::process::exit(0); // Models macOS not returning from its native event loop.
}
if role == "windows-status" {
let _reporter = helper(&dir, "9-3");
// The real OS status is intentionally set, not actual memory exhaustion. The
// report must preserve this distinction rather than claiming a verified exception.
std::process::exit(0xc0000017u32 as i32);
}
assert_eq!(role, "abort");
let _reporter = helper(&dir, "4-1");
std::process::abort();
+147
View File
@@ -0,0 +1,147 @@
//! Fixed-vocabulary observations retained by the crash helper independently of the log tail.
//! Driver messages and environment text can contain private data; never forward either here.
use std::ffi::OsStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use crate::crash_report::Shutdown;
pub fn record(reporter: &Shutdown, event: &'static str) {
log::info!("diagnostic observation: {event}");
if let Err(error) = reporter.record(event) {
log::warn!("could not retain diagnostic observation: {error}");
}
}
/// Mirror only wgpu's documented names/aliases, without calling its parser: that parser
/// logs unknown tokens verbatim. Unknown, non-Unicode and oversized values remain private.
pub fn backend_override_events(value: &OsStr) -> Vec<&'static str> {
let Some(value) = value.to_str().filter(|value| value.len() <= 4096) else {
return vec!["WGPU_BACKEND: non-Unicode or oversized value omitted"];
};
let mut selected = [false; 6];
let mut unknown = false;
for token in value.split(',').map(str::trim) {
let aliases: &[&[&str]] = &[&["vulkan", "vk"], &["dx12", "d3d12"], &["metal", "mtl"], &["opengl", "gles", "gl"], &["webgpu"], &["noop"]];
if let Some(index) = aliases.iter().position(|names| names.iter().any(|name| token.eq_ignore_ascii_case(name))) {
if let Some(flag) = selected.get_mut(index) {
*flag = true;
}
} else {
unknown = true;
}
}
let events = [
"WGPU_BACKEND requests: vulkan",
"WGPU_BACKEND requests: dx12",
"WGPU_BACKEND requests: metal",
"WGPU_BACKEND requests: gl",
"WGPU_BACKEND requests: webgpu",
"WGPU_BACKEND requests: noop",
];
let mut result: Vec<_> = selected.into_iter().zip(events).filter_map(|(present, event)| present.then_some(event)).collect();
if unknown {
result.push("WGPU_BACKEND: unrecognized or empty tokens omitted");
}
result
}
/// At most eight events per device, even when a driver floods its callbacks. A wgpu OOM
/// observation is not proof of system RAM exhaustion; device loss is not a driver-crash verdict.
#[derive(Clone)]
pub struct GpuObservations {
seen: Arc<AtomicU8>,
reporter: Shutdown,
}
impl GpuObservations {
pub fn new(reporter: &Shutdown) -> Self {
Self { seen: Arc::new(AtomicU8::new(0)), reporter: reporter.clone() }
}
pub fn uncaptured(&self, error: &eframe::wgpu::Error) {
let (bit, event) = match error {
eframe::wgpu::Error::OutOfMemory { .. } => (1, "GPU observation: wgpu out-of-memory error (not proof of system RAM exhaustion)"),
eframe::wgpu::Error::Validation { .. } => (2, "GPU observation: wgpu validation error"),
eframe::wgpu::Error::Internal { .. } => (4, "GPU observation: wgpu internal error"),
};
self.once(bit, event);
}
pub fn scoped(&self, error: effectcraft_ui_egui::gpu_failure::ScopedError) {
use effectcraft_ui_egui::gpu_failure::ScopedError;
let (bit, event) = match error {
ScopedError::OutOfMemory => (32, "GPU scoped observation: out-of-memory (not proof of system RAM exhaustion)"),
ScopedError::Internal => (64, "GPU scoped observation: internal error (not confirmed out-of-memory)"),
ScopedError::Validation => (128, "GPU scoped observation: validation error"),
};
self.once(bit, event);
}
pub fn device_lost(&self, reason: eframe::wgpu::DeviceLostReason) {
match reason {
eframe::wgpu::DeviceLostReason::Unknown => self.once(8, "GPU observation: device lost, reason unknown (not proof of a driver crash)"),
eframe::wgpu::DeviceLostReason::Destroyed => self.once(16, "GPU observation: device-lost callback, explicit device destruction"),
};
}
fn once(&self, bit: u8, event: &'static str) -> bool {
if self.seen.fetch_or(bit, Ordering::Relaxed) & bit != 0 {
return false;
}
record(&self.reporter, event);
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn override_names_are_normalized_deduplicated_and_private() {
let events = backend_override_events(OsStr::new(" Vk, VULKAN, d3d12, MTL, gles, webgpu, noop, private-token"));
assert_eq!(events.len(), 7);
assert_eq!(events.first(), Some(&"WGPU_BACKEND requests: vulkan"));
assert!(!events.join(" ").contains("private-token"));
assert!(events.iter().all(|event| event.len() <= 100));
assert_eq!(backend_override_events(OsStr::new("")).len(), 1);
assert_eq!(backend_override_events(OsStr::new(&"x".repeat(4097))), ["WGPU_BACKEND: non-Unicode or oversized value omitted"]);
}
#[test]
fn gpu_categories_are_latched_independently() {
let observations = GpuObservations::new(&Shutdown::default());
observations.uncaptured(&eframe::wgpu::Error::OutOfMemory { source: Box::new(std::io::Error::other("private driver detail")) });
observations.device_lost(eframe::wgpu::DeviceLostReason::Unknown);
observations.device_lost(eframe::wgpu::DeviceLostReason::Unknown);
assert_eq!(observations.seen.load(Ordering::Relaxed), 9);
observations.device_lost(eframe::wgpu::DeviceLostReason::Destroyed);
observations.uncaptured(&eframe::wgpu::Error::Validation {
source: Box::new(std::io::Error::other("private driver detail")),
description: "private validation detail".into(),
});
observations.uncaptured(&eframe::wgpu::Error::Internal {
source: Box::new(std::io::Error::other("private driver detail")),
description: "private internal detail".into(),
});
assert_eq!(observations.seen.load(Ordering::Relaxed), 31);
use effectcraft_ui_egui::gpu_failure::ScopedError;
observations.scoped(ScopedError::Internal);
assert_eq!(observations.seen.load(Ordering::Relaxed), 95, "internal scope must not set scoped OOM bit");
observations.scoped(ScopedError::OutOfMemory);
observations.scoped(ScopedError::Validation);
observations.scoped(ScopedError::OutOfMemory);
assert_eq!(observations.seen.load(Ordering::Relaxed), 255);
assert!(!observations.once(1, "GPU observation: duplicate"));
assert!(!observations.once(8, "GPU observation: duplicate"));
}
#[test]
#[cfg(unix)]
fn non_unicode_override_is_not_copied() {
use std::os::unix::ffi::OsStrExt;
assert_eq!(backend_override_events(OsStr::from_bytes(b"private\xff")), ["WGPU_BACKEND: non-Unicode or oversized value omitted"]);
}
}
+326 -15
View File
@@ -8,9 +8,9 @@
//! until its window has kept drawing for [`SETTLE`] (or the app quits normally before then). A
//! launch that finds the previous one's marker switches Window Graphics to OpenGL, which those
//! drivers run, and says so once the window is up. `WGPU_BACKEND` (wgpu's own override) still
//! wins over both. The marker is locked while its launch runs, so a second copy opened meanwhile
//! isn't taken for a crash; and a window OpenGL can't open either puts Window Graphics back to
//! Automatic ([`opengl_failed`]) rather than failing every launch.
//! wins over both. A separate guard file is locked while its launch runs, so a second copy opened
//! meanwhile isn't taken for a crash; and a window OpenGL can't open either puts Window Graphics
//! back to Automatic ([`opengl_failed`]) rather than failing every launch.
use std::fs::File;
use std::path::{Path, PathBuf};
@@ -21,6 +21,46 @@ use eframe::wgpu::Backends;
/// Left in the settings folder from a launch until its window has settled.
const MARKER: &str = "launch-pending";
const LOCK: &str = "launch.lock";
/// Keep this file in place: unlinking a locked file lets a second inode acquire a new lock.
fn acquire_guard(dir: &Path) -> Option<File> {
let file = match std::fs::create_dir_all(dir)
.and_then(|()| std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).open(dir.join(LOCK)))
{
Ok(file) => file,
Err(e) => {
log::warn!("launch guard unavailable: {e}");
return None;
}
};
match file.try_lock() {
Ok(()) => {}
Err(std::fs::TryLockError::WouldBlock) => return None,
Err(e) => {
log::warn!("launch guard lock unavailable: {e}");
return None;
}
}
// Older versions lock the marker itself. Probe only the lock: Windows also prevents
// reads through another handle while it is held. An unreadable marker is not a stale one.
match File::open(dir.join(MARKER)) {
Ok(marker) => match marker.try_lock() {
Ok(()) => {}
Err(std::fs::TryLockError::WouldBlock) => return None,
Err(e) => {
log::warn!("launch marker lock unavailable: {e}");
return None;
}
},
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => {
log::warn!("launch marker unavailable: {e}");
return None;
}
}
Some(file)
}
/// How long the window keeps drawing after its first frame before the launch counts as made.
/// A driver that fails as the window is shown and its surface re-created does so within the
@@ -28,16 +68,39 @@ const MARKER: &str = "launch-pending";
/// switch back in Settings.
pub const SETTLE: Duration = Duration::from_secs(3);
/// Why this launch requested its window graphics APIs. An interrupted startup is an
/// observation, not proof that a driver caused the previous exit.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum BackendSource {
#[default]
Default,
SavedPreference,
InterruptedStartup,
EnvironmentOverride,
}
impl BackendSource {
pub fn event(self) -> &'static str {
match self {
Self::Default => "backend source: default",
Self::SavedPreference => "backend source: saved OpenGL preference",
Self::InterruptedStartup => "backend source: previous interrupted startup; OpenGL fallback",
Self::EnvironmentOverride => "backend source: WGPU_BACKEND present",
}
}
}
/// This launch's window graphics.
#[derive(Debug, Default)]
pub struct Launch {
/// The graphics APIs the window may use (`None`: eframe's default).
pub backends: Option<Backends>,
pub source: BackendSource,
/// Switched to OpenGL because the previous launch stopped as its window opened: the message
/// to show.
pub notice: Option<String>,
marker: Option<PathBuf>,
/// Holds the marker's lock until the launch settles (or the process ends).
/// Holds the separate guard file's lock until the launch settles (or the process ends).
lock: Option<File>,
/// When the window drew its first frame, until the launch settles.
first_frame: Option<Instant>,
@@ -48,14 +111,22 @@ impl Launch {
/// launch stopped as its window opened (where OpenGL exists: not macOS), and leave this
/// launch's marker. `env_override`: `WGPU_BACKEND` is set.
pub fn begin(dir: Option<&Path>, env_override: bool) -> Launch {
let Some(dir) = dir.filter(|_| !env_override) else { return Launch::default() };
if env_override {
return Launch { source: BackendSource::EnvironmentOverride, ..Launch::default() };
}
let Some(dir) = dir else { return Launch::default() };
let prefs_path = dir.join(PREFS_FILE);
let mut prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default();
let marker = dir.join(MARKER);
// Locked by another launch that is still opening its window: not a crash.
let held = File::open(&marker).is_ok_and(|f| matches!(f.try_lock(), Err(std::fs::TryLockError::WouldBlock)));
let Some(lock) = acquire_guard(dir) else {
let backends = (prefs.startup.window_graphics == "gl").then_some(Backends::GL);
let source = if backends.is_some() { BackendSource::SavedPreference } else { BackendSource::Default };
return Launch { backends, source, ..Launch::default() };
};
// Read again while owning the guard: another launch may have just changed preferences.
prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default();
let mut notice = None;
if marker.exists() && !held && prefs.startup.window_graphics == "auto" && cfg!(not(target_os = "macos")) {
if marker.exists() && prefs.startup.window_graphics == "auto" && cfg!(not(target_os = "macos")) {
prefs.startup.window_graphics = "gl".into();
match std::fs::create_dir_all(dir).and_then(|()| std::fs::write(&prefs_path, prefs.to_json())) {
Ok(()) => log::warn!("the last launch stopped as its window opened: switching Window Graphics to OpenGL"),
@@ -69,15 +140,21 @@ impl Launch {
);
}
let backends = (prefs.startup.window_graphics == "gl").then_some(Backends::GL);
let (marker, lock) = match std::fs::create_dir_all(dir).and_then(|()| File::create(&marker)) {
// Where locks aren't available the guard works as before.
Ok(f) => (Some(marker), f.try_lock().is_ok().then_some(f)),
let source = if notice.is_some() {
BackendSource::InterruptedStartup
} else if backends.is_some() {
BackendSource::SavedPreference
} else {
BackendSource::Default
};
let marker = match File::create(&marker) {
Ok(_) => Some(marker),
Err(e) => {
log::warn!("launch marker: {e}");
(None, None)
None
}
};
Launch { backends, notice, marker, lock, first_frame: None }
Launch { backends, source, notice, marker, lock: Some(lock), first_frame: None }
}
/// The window drew a frame. Until the launch has settled, returns how long that still takes
@@ -104,18 +181,19 @@ impl Launch {
/// The launch made it: release and remove its marker.
fn settled(&mut self) {
self.lock = None;
if let Some(m) = self.marker.take()
&& let Err(e) = std::fs::remove_file(&m)
{
log::warn!("launch marker: {e}");
}
self.lock = None;
}
}
/// The window couldn't open with OpenGL (an error, not a crash): back to Automatic, and no marker.
pub fn opengl_failed(dir: Option<&Path>) {
let Some(dir) = dir else { return };
let Some(_lock) = acquire_guard(dir) else { return };
let prefs_path = dir.join(PREFS_FILE);
let mut prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default();
prefs.startup.window_graphics = "auto".into();
@@ -142,6 +220,7 @@ mod tests {
// may still crash as the window is shown); frames for `SETTLE` do.
let mut l = Launch::begin(Some(&dir), false);
assert_eq!((l.backends, l.notice.is_some()), (None, false));
assert_eq!(l.source, BackendSource::Default);
assert!(dir.join(MARKER).exists());
let t0 = Instant::now();
assert_eq!(l.frame_drawn_at(t0), Some(SETTLE));
@@ -159,14 +238,18 @@ mod tests {
} else {
assert_eq!(l.backends, Some(Backends::GL));
assert!(l.notice.is_some());
assert_eq!(l.source, BackendSource::InterruptedStartup);
assert_eq!(window_graphics(&dir), "gl", "saved in Settings");
l.exited();
let l = Launch::begin(Some(&dir), false);
assert_eq!((l.backends, l.notice.is_some()), (Some(Backends::GL), false), "it stays, without the notice");
assert_eq!(l.source, BackendSource::SavedPreference);
}
// `WGPU_BACKEND` wins, and leaves no marker.
let _ = std::fs::remove_file(dir.join(MARKER));
assert_eq!(Launch::begin(Some(&dir), true).backends, None);
let overridden = Launch::begin(Some(&dir), true);
assert_eq!(overridden.backends, None);
assert_eq!(overridden.source, BackendSource::EnvironmentOverride);
assert!(!dir.join(MARKER).exists());
let _ = std::fs::remove_dir_all(&dir);
}
@@ -217,4 +300,232 @@ mod tests {
}
let _ = std::fs::remove_dir_all(&dir);
}
/// A fresh settings folder for one test.
fn fresh_dir(case: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("ec-launch-guard-{case}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// Window Graphics saved as OpenGL.
fn save_opengl(dir: &Path) {
let mut prefs = Prefs::default();
prefs.startup.window_graphics = "gl".into();
std::fs::write(dir.join(PREFS_FILE), prefs.to_json()).unwrap();
}
#[test]
fn a_second_copy_reports_the_saved_backend_without_owning_the_guard() {
let dir = fresh_dir("second-source");
save_opengl(&dir);
let mut first = Launch::begin(Some(&dir), false);
let mut second = Launch::begin(Some(&dir), false);
assert!(second.lock.is_none());
assert_eq!((second.backends, second.source), (Some(Backends::GL), BackendSource::SavedPreference));
second.exited();
first.exited();
let _ = std::fs::remove_dir_all(dir);
}
/// A second copy that quits normally while the first is still opening doesn't remove the first
/// one's marker (#477).
#[test]
fn a_second_copy_quitting_leaves_the_running_launchs_marker() {
let dir = fresh_dir("second-quits");
let mut first = Launch::begin(Some(&dir), false);
let mut second = Launch::begin(Some(&dir), false);
second.exited();
let kept = dir.join(MARKER).exists();
first.exited();
let _ = std::fs::remove_dir_all(&dir);
assert!(kept, "the second copy removed the running launch's marker");
}
/// A second copy whose OpenGL window fails doesn't remove a running launch's marker.
#[test]
fn failed_opengl_in_a_second_copy_leaves_the_running_launchs_marker() {
let dir = fresh_dir("second-gl-fails");
let mut first = Launch::begin(Some(&dir), false);
let second = Launch::begin(Some(&dir), false);
opengl_failed(Some(&dir));
let kept = dir.join(MARKER).exists();
drop(second);
first.exited();
let _ = std::fs::remove_dir_all(&dir);
assert!(kept, "OpenGL cleanup removed another running launch's marker");
}
/// With `WGPU_BACKEND` set, a stale marker and a saved OpenGL choice are left as they are.
#[test]
fn wgpu_backend_leaves_a_stale_marker_and_saved_opengl_alone() {
let dir = fresh_dir("env-override");
save_opengl(&dir);
std::fs::write(dir.join(MARKER), b"stale-marker").unwrap();
let before = std::fs::read(dir.join(PREFS_FILE)).unwrap();
let mut l = Launch::begin(Some(&dir), true);
assert_eq!(l.backends, None);
assert!(l.notice.is_none());
l.exited();
assert!(dir.join(MARKER).exists());
assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before);
let _ = std::fs::remove_dir_all(&dir);
}
/// After OpenGL fails to open a window, the next launch is back on Automatic, without a notice.
#[test]
fn failed_opengl_puts_the_next_launch_back_on_automatic() {
let dir = fresh_dir("gl-recovers");
save_opengl(&dir);
let l = Launch::begin(Some(&dir), false);
assert_eq!(l.backends, Some(Backends::GL));
drop(l);
opengl_failed(Some(&dir));
assert!(!dir.join(MARKER).exists());
assert_eq!(Prefs::from_json(&std::fs::read_to_string(dir.join(PREFS_FILE)).unwrap()).startup.window_graphics, "auto");
let mut l = Launch::begin(Some(&dir), false);
assert_eq!(l.backends, None);
assert!(l.notice.is_none());
l.exited();
let _ = std::fs::remove_dir_all(&dir);
}
/// The guard file is never removed or replaced: unlinking a locked file would let the next
/// launch lock a new one while the old lock is still held.
#[cfg(unix)]
#[test]
fn the_guard_file_stays_the_same_file_across_launches() {
use std::os::unix::fs::MetadataExt;
let dir = fresh_dir("guard-identity");
let mut a = Launch::begin(Some(&dir), false);
let before = std::fs::metadata(dir.join(LOCK)).unwrap();
let t0 = Instant::now();
assert_eq!(a.frame_drawn_at(t0), Some(SETTLE));
assert_eq!(a.frame_drawn_at(t0 + SETTLE), None);
assert!(!dir.join(MARKER).exists());
let after = std::fs::metadata(dir.join(LOCK)).unwrap();
assert_eq!((before.dev(), before.ino()), (after.dev(), after.ino()));
let mut b = Launch::begin(Some(&dir), false);
assert!(b.lock.is_some());
let reused = std::fs::metadata(dir.join(LOCK)).unwrap();
assert_eq!((before.dev(), before.ino()), (reused.dev(), reused.ino()));
b.exited();
let _ = std::fs::remove_dir_all(dir);
}
/// A second copy's frames and quit neither settle nor unlock the running launch.
#[test]
fn a_second_copys_frames_dont_settle_the_running_launch() {
let dir = fresh_dir("second-frames");
let mut a = Launch::begin(Some(&dir), false);
std::fs::write(dir.join(MARKER), b"owner-marker").unwrap();
let mut b = Launch::begin(Some(&dir), false);
assert!(b.lock.is_none());
assert!(b.marker.is_none());
let t0 = Instant::now();
assert_eq!(b.frame_drawn_at(t0), None);
assert_eq!(b.frame_drawn_at(t0 + SETTLE), None);
b.exited();
assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"owner-marker");
let probe = File::open(dir.join(LOCK)).unwrap();
assert!(matches!(probe.try_lock(), Err(std::fs::TryLockError::WouldBlock)));
a.exited();
let _ = std::fs::remove_dir_all(dir);
}
/// Older versions lock the marker itself: while one runs, a new launch changes nothing, and
/// once it has stopped without settling the next launch still switches to OpenGL.
#[test]
fn a_running_older_version_holding_the_marker_is_left_alone() {
let dir = fresh_dir("older-version");
std::fs::write(dir.join(MARKER), b"old-owner").unwrap();
std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap();
let old = File::open(dir.join(MARKER)).unwrap();
old.try_lock().unwrap();
let before = std::fs::read(dir.join(PREFS_FILE)).unwrap();
let mut b = Launch::begin(Some(&dir), false);
assert!(b.lock.is_none());
assert!(b.marker.is_none());
assert!(b.backends.is_none());
assert!(b.notice.is_none());
b.exited();
opengl_failed(Some(&dir));
assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before);
// Windows locks forbid reading through another handle. Inspect the contents only after
// releasing the old owner's lock, but before recovery can truncate the marker.
drop(old);
assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"old-owner");
let mut recovered = Launch::begin(Some(&dir), false);
if cfg!(not(target_os = "macos")) {
assert_eq!(recovered.backends, Some(Backends::GL));
}
recovered.exited();
let _ = std::fs::remove_dir_all(dir);
}
/// A Windows sharing violation is not evidence that a marker is stale or absent.
#[cfg(windows)]
#[test]
fn an_unopenable_marker_leaves_settings_and_marker_alone() {
use std::os::windows::fs::OpenOptionsExt;
let dir = fresh_dir("marker-unopenable");
std::fs::write(dir.join(MARKER), b"old-owner").unwrap();
std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap();
let old = std::fs::OpenOptions::new().read(true).write(true).share_mode(0).open(dir.join(MARKER)).unwrap();
let before = std::fs::read(dir.join(PREFS_FILE)).unwrap();
let mut launch = Launch::begin(Some(&dir), false);
assert!(launch.lock.is_none());
assert!(launch.marker.is_none());
assert!(launch.backends.is_none());
assert!(launch.notice.is_none());
launch.exited();
opengl_failed(Some(&dir));
assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before);
drop(old);
assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"old-owner");
let _ = std::fs::remove_dir_all(dir);
}
/// A guard file that can't be opened leaves Window Graphics and the marker as they are.
#[test]
fn an_unusable_guard_file_leaves_settings_and_marker_alone() {
let dir = fresh_dir("guard-unusable");
std::fs::create_dir(dir.join(LOCK)).unwrap();
std::fs::write(dir.join(MARKER), b"stale-marker").unwrap();
std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap();
let before = std::fs::read(dir.join(PREFS_FILE)).unwrap();
let mut l = Launch::begin(Some(&dir), false);
assert!(l.backends.is_none());
assert!(l.notice.is_none());
assert!(l.lock.is_none());
assert!(l.marker.is_none());
l.exited();
opengl_failed(Some(&dir));
assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before);
assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"stale-marker");
let _ = std::fs::remove_dir_all(dir);
}
/// OpenGL cleanup from elsewhere waits until the running launch has let go; then it resets
/// Window Graphics to Automatic and removes the marker.
#[test]
fn failed_opengl_cleanup_waits_for_the_running_launch() {
let dir = fresh_dir("gl-cleanup-waits");
save_opengl(&dir);
let launch = Launch::begin(Some(&dir), false);
assert_eq!(launch.backends, Some(Backends::GL));
assert!(launch.lock.is_some());
let before = std::fs::read(dir.join(PREFS_FILE)).unwrap();
opengl_failed(Some(&dir));
assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before);
assert!(dir.join(MARKER).exists(), "cleanup elsewhere can't remove a running launch's marker");
drop(launch);
opengl_failed(Some(&dir));
let after = Prefs::from_json(&std::fs::read_to_string(dir.join(PREFS_FILE)).unwrap()).startup.window_graphics;
assert_eq!(after, "auto", "once the launch has let go, failed OpenGL resets Window Graphics");
assert!(!dir.join(MARKER).exists());
let _ = std::fs::remove_dir_all(dir);
}
}
+31 -5
View File
@@ -23,6 +23,7 @@ mod apple_events;
mod audio_out;
mod control_server;
mod crash_report;
mod diagnostic_state;
mod launch_guard;
#[cfg(target_os = "macos")]
mod native_menu;
@@ -183,7 +184,7 @@ fn start_helpers_if_opted_in(session: &mut effectcraft_engine::Session) {
fn main() -> eframe::Result {
let args: Vec<_> = std::env::args_os().skip(1).collect();
if crash_report::maybe_run(&args, |description| {
rfd::MessageDialog::new().set_level(rfd::MessageLevel::Error).set_title("EffectCraft stopped unexpectedly").set_description(description).show();
rfd::MessageDialog::new().set_level(rfd::MessageLevel::Error).set_title("EffectCraft diagnostic report").set_description(description).show();
}) {
return Ok(());
}
@@ -246,8 +247,15 @@ fn run_desktop(reporter: &crash_report::Shutdown) -> eframe::Result {
#[cfg(target_os = "linux")]
appimage::integrate_from_env(ICON_PNG);
// Settings ▸ Startup & Repair ▸ Window Graphics, switched to OpenGL when the last launch
// stopped as its window opened (a crashing graphics driver, #243).
let mut launch = launch_guard::Launch::begin(effectcraft_host::config_dir().as_deref(), std::env::var_os("WGPU_BACKEND").is_some());
// stopped as its window opened (the marker alone does not establish a cause).
let backend_override = std::env::var_os("WGPU_BACKEND");
let mut launch = launch_guard::Launch::begin(effectcraft_host::config_dir().as_deref(), backend_override.is_some());
diagnostic_state::record(reporter, launch.source.event());
if let Some(value) = backend_override {
for event in diagnostic_state::backend_override_events(&value) {
diagnostic_state::record(reporter, event);
}
}
let on_gl = launch.backends == Some(eframe::wgpu::Backends::GL);
let notice = launch.notice.take();
let options = eframe::NativeOptions {
@@ -281,16 +289,33 @@ fn run_desktop(reporter: &crash_report::Shutdown) -> eframe::Result {
Box::new(move |cc| {
EffectcraftApp::install_ime_reset_hooks(&cc.egui_ctx);
match &cc.wgpu_render_state {
Some(rs) => log::info!("graphics: {:?}", rs.adapter.get_info()),
None => log::info!("graphics: no wgpu device"),
Some(rs) => {
let info = rs.adapter.get_info();
log::info!("graphics: {info:?}");
let event = match info.backend {
eframe::wgpu::Backend::Noop => "actual GPU backend: noop",
eframe::wgpu::Backend::Vulkan => "actual GPU backend: vulkan",
eframe::wgpu::Backend::Metal => "actual GPU backend: metal",
eframe::wgpu::Backend::Dx12 => "actual GPU backend: dx12",
eframe::wgpu::Backend::Gl => "actual GPU backend: gl",
eframe::wgpu::Backend::BrowserWebGpu => "actual GPU backend: webgpu",
};
diagnostic_state::record(&desktop_reporter, event);
}
None => diagnostic_state::record(&desktop_reporter, "actual GPU backend: no wgpu device"),
}
// This executable owns the shared device's handlers. Install them before
// EffectCraft's compositor pipelines are built, without changing handlers from
// inside Gpu::new. eframe has already constructed its presentation renderer here.
let gpu_failures = effectcraft_ui_egui::gpu_failure::GpuFailureBridge::new(&cc.egui_ctx);
let observations = diagnostic_state::GpuObservations::new(&desktop_reporter);
let scoped_observed = observations.clone();
gpu_failures.set_scoped_error_observer(move |error| scoped_observed.scoped(error));
if let Some(rs) = &cc.wgpu_render_state {
let errors_observed = observations.clone();
let errors = gpu_failures.clone();
rs.device.on_uncaptured_error(std::sync::Arc::new(move |error| {
errors_observed.uncaptured(&error);
let message = format!("uncaptured GPU error: {error}");
if errors.report(&message, false) {
log::error!("{message}");
@@ -298,6 +323,7 @@ fn run_desktop(reporter: &crash_report::Shutdown) -> eframe::Result {
}));
let lost = gpu_failures.clone();
rs.device.set_device_lost_callback(move |reason, message| {
observations.device_lost(reason);
lost.report(&format!("GPU device lost ({reason:?}): {message}"), true);
});
}
+41 -1
View File
@@ -1142,11 +1142,37 @@ pub const GLOW_MIDPOINT_MAX: f64 = 0.990_005_493_164_062_5;
/// The key a popup item is matched by between the two programs: trimmed, lower case, and
/// Set Matte's "Red Channel" ... "Alpha Channel" are EffectCraft's "Red" ... "Alpha" (904 E24).
///
/// Lower case is Unicode's, not only ASCII's: After Effects stores popup items in its UI
/// language, so an item can start with a capital outside ASCII (French "Écran"), and its key must
/// be the same as for the same word in lower case.
pub fn popup_key(item: &str) -> String {
let s = item.trim().to_ascii_lowercase();
let s = item.trim().to_lowercase();
s.strip_suffix(" channel").map(str::to_string).unwrap_or(s)
}
/// The English [`popup_key`] a non-English After Effects popup item name stands for, when this
/// table knows it; `None` otherwise. EffectCraft's own popup options are English only, so a
/// project saved by an After Effects set to another UI language never matches them by name and
/// the parameter falls back to its default (observed on a real "Niveaux" project, French After
/// Effects 2026 26.0.0x67: Levels' Channel and Clip to Output Black / White). Add an entry only
/// from a confirmed After Effects label, never a guess at a whole menu's translation.
pub fn popup_alias(key: &str) -> Option<&'static str> {
Some(match key {
// French: Levels' Channel popup (`ADBE Easy Levels2-0001`). "Alpha" is the same word in
// both languages, so it needs no entry.
"rvb" => "rgb",
"rouge" => "red",
"vert" => "green",
"bleu" => "blue",
// French: Levels' Clip to Output Black / White popups (`-0008`, `-0009`).
"activé" => "on",
"désactivé" => "off",
"désactivé pour la couleur 32 bpc" => "off for 32 bpc color",
_ => return None,
})
}
/// The table row of an After Effects match name.
pub fn by_match_name(match_name: &str) -> Option<&'static EffectMap> {
EFFECTS.iter().find(|m| m.match_name == match_name)
@@ -1164,3 +1190,17 @@ pub const HUE_SATURATION_MASTER: [&str; 3] = ["hue", "saturation", "lightness"];
/// The EffectCraft parameters Levels' RGB channel values (`Extra::Levels`) go to, in the order of
/// the data (input black, input white, gamma, output black, output white: 512-516).
pub const LEVELS_RGB: [&str; 5] = ["inBlack", "inWhite", "gamma", "outBlack", "outWhite"];
#[cfg(test)]
mod tests {
use super::*;
/// A capital outside ASCII has the same key as its lower case; ASCII keys are unchanged.
#[test]
fn popup_key_lowers_non_ascii_capitals() {
assert_eq!(popup_key("ÉCRAN"), popup_key("écran"));
assert_eq!(popup_key(" Désactivé "), "désactivé");
assert_eq!(popup_key("Off for 32 bpc Color"), "off for 32 bpc color");
assert_eq!(popup_key("Red Channel"), "red");
}
}
+11 -6
View File
@@ -34,7 +34,7 @@
//! import. Hue/Saturation's master values and Levels' values are arbitrary data, decoded by the
//! reader (444-446, 512-516).
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_key};
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_alias, popup_key};
use crate::{
CryptomatteData, CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, ExtractorChannels, HueSaturationData,
Layer as AepLayer, LayerRef, LevelsData, MeshData, ParamKind, ParamValue,
@@ -83,11 +83,11 @@ pub(crate) fn import_effects<'f, 'a>(
}
let shown = fx.display_name().unwrap_or_else(|| fx.match_name().to_string());
let Some(map) = EFFECTS.iter().find(|m| m.match_name == fx.match_name()) else {
col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
col.lose_unmapped_effect(fx.match_name(), format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
continue;
};
let Some(spec) = effectcraft_effects::find(map.effect) else {
col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
col.lose_unmapped_effect(fx.match_name(), format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
continue;
};
if defs.source(&fx) == DefinitionSource::Missing {
@@ -413,8 +413,8 @@ pub(crate) fn resolve_layer_refs(
}
}
/// The EffectCraft item of each file item (by name, ignoring case and separators); `None` when
/// the parameter here is not a popup.
/// The EffectCraft item of each file item (by name, ignoring case and separators, and a known
/// non-English name for it, [`popup_alias`]); `None` when the parameter here is not a popup.
fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>> {
let ParamUi::Popup { options } = ui else { return None };
let norm = popup_key;
@@ -425,7 +425,12 @@ fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>
if item.trim() == "(-" {
return None;
}
options.iter().position(|o| norm(o) == norm(item)).and_then(|i| u32::try_from(i).ok())
let key = norm(item);
options
.iter()
.position(|o| norm(o) == key)
.or_else(|| popup_alias(&key).and_then(|a| options.iter().position(|o| norm(o) == a)))
.and_then(|i| u32::try_from(i).ok())
})
.collect(),
)
+71 -1
View File
@@ -269,8 +269,15 @@ pub(crate) struct Collector {
losses: BTreeMap<LossCategory, Loss>,
pub missing_footage: Vec<String>,
pub missing_fonts: Vec<String>,
/// The effects with no counterpart, by match name: the first instance's report line and how
/// many instances there were. They get a line each in [`Collector::finish`], so one that
/// comes after the example lines are used up is still named (#3365).
unmapped_effects: BTreeMap<String, (String, usize)>,
}
/// How many different unmapped effects get a line of their own (a file names at most this many).
const MAX_UNMAPPED_EFFECTS: usize = 100;
impl Collector {
/// Records one loss.
pub fn lose(&mut self, category: LossCategory, detail: impl Into<String>) {
@@ -281,7 +288,28 @@ impl Collector {
}
}
pub fn finish(self) -> ImportReport {
/// Records one instance of an effect with no counterpart. The loss is counted now; its line is
/// made in [`Collector::finish`], one per effect with the number of instances, so that many
/// other losses cannot hide which effects were dropped.
pub fn lose_unmapped_effect(&mut self, match_name: &str, detail: String) {
let loss = self.losses.entry(LossCategory::Effects).or_insert_with(|| Loss { category: LossCategory::Effects, count: 0, details: Vec::new() });
loss.count = loss.count.saturating_add(1);
if let Some(seen) = self.unmapped_effects.get_mut(match_name) {
seen.1 = seen.1.saturating_add(1);
} else if self.unmapped_effects.len() < MAX_UNMAPPED_EFFECTS {
self.unmapped_effects.insert(match_name.to_string(), (detail, 1));
}
// (Past the limit an effect is only counted: its name is not worth a line of its own.)
}
pub fn finish(mut self) -> ImportReport {
if let Some(loss) = self.losses.get_mut(&LossCategory::Effects) {
let named = std::mem::take(&mut self.unmapped_effects).into_values().map(|(first, n)| match n {
1 => first,
_ => format!("{first}, and {} more like it", n - 1),
});
loss.details.splice(0..0, named);
}
ImportReport {
imported: self.imported,
losses: self.losses.into_values().collect(),
@@ -301,3 +329,45 @@ pub const LIMITATIONS: &[&str] = &[
"Text: per-character 3D, text on a path and several style slots of unknown meaning are not decoded (stored values that differ from the default are reported as text layer features).",
"Footage that is not a solid is linked by its stored path with its stored frame rate and duration; the importer never opens the files (use Reload Footage after the files are found).",
];
#[cfg(test)]
mod tests {
use super::*;
/// #3365: with other losses filling the 20 example lines, an effect with no counterpart still
/// gets its own line, with how many instances were lost. The category's count is unchanged.
#[test]
fn every_unmapped_effect_is_named_however_many_other_losses_there_are() {
let mut col = Collector::default();
for i in 0..30 {
col.lose(LossCategory::Effects, format!("C: 'L{i}': parameter P not imported"));
}
for i in 0..25 {
col.lose_unmapped_effect("CC Cylinder", format!("C: 'Cyl{i}' has the effect 'CC Cylinder' (CC Cylinder)"));
}
col.lose_unmapped_effect("Pseudo/", "C: 'D' has the effect 'Dropdown' (Pseudo/)".to_string());
let report = col.finish();
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
assert_eq!(effects.count, 56, "30 parameters, 25 cylinders and 1 dropdown");
let named = |what: &str| effects.details.iter().filter(|d| d.contains(what)).collect::<Vec<_>>();
assert_eq!(named("(CC Cylinder)").len(), 1, "{:?}", effects.details);
assert!(named("(CC Cylinder)")[0].contains("24 more"), "{:?}", named("(CC Cylinder)"));
// One instance stays the single line it was.
assert_eq!(named("(Pseudo/)"), ["C: 'D' has the effect 'Dropdown' (Pseudo/)"]);
assert!(effects.details.len() <= MAX_DETAILS + 2, "the examples stay bounded");
}
/// A file with more than [`MAX_UNMAPPED_EFFECTS`] different unmapped effects names that many
/// and counts the rest, whatever room the example lines have.
#[test]
fn at_most_the_limit_of_unmapped_effects_are_named() {
let mut col = Collector::default();
for i in 0..=MAX_UNMAPPED_EFFECTS {
col.lose_unmapped_effect(&format!("Effect {i}"), format!("C: 'L' has the effect 'Effect {i}' (Effect {i})"));
}
let report = col.finish();
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
assert_eq!(effects.count, MAX_UNMAPPED_EFFECTS + 1, "every instance is counted");
assert_eq!(effects.details.len(), MAX_UNMAPPED_EFFECTS, "{} lines", effects.details.len());
}
}
+56
View File
@@ -842,3 +842,59 @@ fn a_default_state_colour_record_holds_the_value() {
assert_eq!(colour(&default_state), colour(&written));
assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default");
}
/// `SYNTHETIC` with Levels' Channel and Clip to Output Black / White popups' item names (`pdnm`)
/// rewritten to the French labels a French-language After Effects stores there, keeping the same
/// item order so the index each popup is set to is unaffected.
fn with_french_levels_popups() -> Vec<u8> {
let (mut channel, mut clip) = (0, 0);
let bytes = mutate(&mut |c| {
if !c.is(b"Utf8") {
return;
}
if c.bytes() == Some(b"RGB|Red|Green|Blue|Alpha".as_slice()) {
*c = Chunk::data(*b"Utf8", b"RVB|Rouge|Vert|Bleu|Alpha".to_vec());
channel += 1;
} else if c.bytes() == Some(b"On|Off|Off for 32 bpc Color".as_slice()) {
*c = Chunk::data(*b"Utf8", "Activé|Désactivé|Désactivé pour la couleur 32 bpc".as_bytes().to_vec());
clip += 1;
}
});
assert_eq!(channel, 2, "Levels' Channel pdnm");
assert_eq!(clip, 4, "Levels' two Clip pdnm (Black and White), each stored twice");
bytes
}
/// A popup's item names in the file are matched to EffectCraft's own (English) ones by name
/// (`popup_key`): a project saved by a French After Effects stores "RVB", "Activé", "Désactivé
/// pour la couleur 32 bpc" for Levels' Channel and Clip to Output Black / White (observed in a
/// real "Niveaux" effect, After Effects 2026 26.0.0x67). Before `popup_alias`, none of these
/// matched EffectCraft's English-only options, so each came back "has no counterpart, default
/// used" and the real selection was silently lost.
#[test]
fn french_levels_popup_items_are_recognized() {
let (unchanged, r1) = run(SYNTHETIC, true);
let (french, r2) = run(&with_french_levels_popups(), true);
let (r1, r2) = (r1.unwrap(), r2.unwrap());
let found = losses(&r2, LossCategory::Effects);
assert!(!found.iter().any(|d| d.contains("has no counterpart")), "{found:?}");
for id in ["channel", "clipToOutputBlack", "clipToOutputWhite"] {
let want: Vec<_> = effect_values(&unchanged).into_iter().filter(|(p, _)| p == id).collect();
let got: Vec<_> = effect_values(&french).into_iter().filter(|(p, _)| p == id).collect();
assert_eq!(got, want, "{id}");
}
assert_eq!(r1.report.imported.effects, r2.report.imported.effects);
}
/// `popup_alias` must cover every item of a popup it claims to know, not only the one a first
/// report happened to show: Channel's French "Rouge", "Vert", "Bleu" ("Alpha" is the same word in
/// both languages) alongside the already-covered "RVB" (`effect_map.rs`, reviewed on #3432).
#[test]
fn french_levels_channel_aliases_cover_every_item() {
use effectcraft_aep::effect_map::popup_alias;
for (french, english) in [("rvb", "rgb"), ("rouge", "red"), ("vert", "green"), ("bleu", "blue")] {
assert_eq!(popup_alias(french), Some(english), "{french}");
}
// "Alpha" needs no alias: the same word in both languages.
assert_eq!(popup_alias("alpha"), None);
}
+11
View File
@@ -143,6 +143,8 @@ const WRITES: &[(&str, &[&str])] = &[
("shortcuts.export", &["path"]),
("renderQueue.add", &["output", "path"]),
("file.exportTimeline", &["path"]),
("journal.export", &["path"]),
("help.exportRepro", &["path"]),
// `path` / `output` may also be a file-name template, as for render.preRender.
("file.createProxy", &["path", "output"]),
];
@@ -280,6 +282,11 @@ impl Confinement {
if matches!(id, "file.openRecent" | "file.importRecent" | "anim.applyRecentPreset") && !given {
return Err(Error::BadArgs(format!("`{id}`: give `path` while file access is confined")));
}
// The default repro destination is the settings store, not an automation write root.
// Require a single explicit path so missing, null or array parameters cannot bypass it.
if id == "help.exportRepro" && params.get("path").and_then(Value::as_str).is_none() {
return Err(Error::BadArgs("`help.exportRepro`: give `path` inside an automation write root while file access is confined".into()));
}
let mut lookups = vec![];
// An output the agent did not name is derived from where the project sits: the Default
// Output Folder preference when that is set, beside the project file otherwise — and the
@@ -381,6 +388,10 @@ impl FootageSource for ConfinedFootage {
fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
self.readable(footage).then(|| self.inner.frame_with(item, footage, t, size, float)).flatten()
}
/// The frame as 8-bit levels, or `None` when the footage lies outside the allowed roots.
fn frame8(&self, item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option<Arc<effectcraft_engine::render::disk_cache::Frame8>> {
self.readable(footage).then(|| self.inner.frame8(item, footage, t, float)).flatten()
}
/// The footage audio from the inner source, or `None` when the footage lies outside the allowed roots.
fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option<Vec<f32>> {
self.readable(footage).then(|| self.inner.audio(item, footage, t, frames, rate)).flatten()
+25
View File
@@ -1865,3 +1865,28 @@ fn automation_roots_check_relinked_footage_again() {
assert!(!(px[0] > 200 && px[3] > 200), "the link pointed outside afterwards was read: {px:?}");
let _ = std::fs::remove_dir_all(&base);
}
/// Exporting a repro must not write to the settings store outside the automation roots.
#[test]
fn confined_repro_export_requires_an_explicit_writable_path() {
use crate::confine::Confinement;
use effectcraft_engine::config::{ConfigStore, MemoryConfig};
use std::sync::Arc;
let dir = tempfile::tempdir().unwrap();
let store = Arc::new(MemoryConfig::default());
let session = Session { config: Some(store.clone()), ..Default::default() };
let mut s = McpServer::new(Backend::headless(session));
s.backend().confine(Confinement::new(&[], &[dir.path().to_path_buf()]).unwrap());
for params in [json!({}), json!({"path": null}), json!({"path": []}), json!({"path": [dir.path().join("repro.jsonl")]})] {
let (content, error) = call(&mut s, "execute_command", json!({"command": "help.exportRepro", "params": params}));
assert!(error, "{content:?}");
assert!(store.list("Repros").is_empty(), "implicit settings-store writes must be refused");
}
let path = dir.path().join("repro.jsonl");
call_json(&mut s, "execute_command", json!({"command": "help.exportRepro", "params": {"path": path}}));
assert!(path.is_file());
let journal = call_json(&mut s, "execute_command", json!({"command": "journal.export"}));
assert!(journal["script"].is_string(), "in-memory exports need no write root");
assert!(store.list("Repros").is_empty());
}
+50 -8
View File
@@ -325,6 +325,9 @@ pub struct Clut {
pub data: Vec<f32>,
}
/// Most interpolating inputs [`Clut::eval`] combines (ICC allows 15).
pub const MAX_CLUT_INPUTS: usize = 16;
impl Clut {
/// Multilinear interpolation at `x` (0..1 per input).
pub fn eval(&self, x: &[f64]) -> Vec<f64> {
@@ -334,26 +337,40 @@ impl Clut {
let mut s = self.outputs;
for i in (0..n).rev() {
stride[i] = s;
s *= self.grid[i].max(1);
// Saturating: a table with enough axes to overflow cannot hold its data anyway, and a
// saturated offset reads nothing (zero) instead of panicking.
s = s.saturating_mul(self.grid[i].max(1));
}
let mut frac = vec![0.0; n];
let mut step = vec![0usize; n];
for i in 0..n {
let g = self.grid[i].max(1);
let f = x.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0) * (g - 1) as f64;
let k = (f.floor() as usize).min(g.saturating_sub(2));
let mut k = (f.floor() as usize).min(g.saturating_sub(2));
frac[i] = if g > 1 { f - k as f64 } else { 0.0 };
step[i] = if g > 1 { stride[i] } else { 0 };
base += k * stride[i];
// The last grid point is a corner of its own, not an interpolation toward the next.
if frac[i] >= 1.0 {
k += 1;
frac[i] = 0.0;
}
base = base.saturating_add(k.saturating_mul(stride[i]));
}
let mut out = vec![0.0; self.outputs];
for corner in 0..(1usize << n) {
// Only the axes that interpolate have two corners: a singleton axis, or one sampled on a
// grid point, contributes its lower corner alone. ICC tables have at most 15 inputs; a
// table built with more interpolating axes than that evaluates to zero.
let active: Vec<usize> = (0..n).filter(|&i| step[i] != 0 && frac[i] != 0.0).collect();
if active.len() > MAX_CLUT_INPUTS {
return out;
}
for corner in 0..(1usize << active.len()) {
let mut w = 1.0;
let mut off = base;
for i in 0..n {
if corner >> i & 1 == 1 {
for (bit, &i) in active.iter().enumerate() {
if corner >> bit & 1 == 1 {
w *= frac[i];
off += step[i];
off = off.saturating_add(step[i]);
} else {
w *= 1.0 - frac[i];
}
@@ -362,7 +379,7 @@ impl Clut {
continue;
}
for (o, v) in out.iter_mut().enumerate() {
*v += w * self.data.get(off + o).copied().unwrap_or(0.0) as f64;
*v += w * self.data.get(off.saturating_add(o)).copied().unwrap_or(0.0) as f64;
}
}
out
@@ -1052,6 +1069,31 @@ mod tests {
assert!(matches!(parse_profile(&write_matrix_profile("m", P709, D65, 2.2)).unwrap(), Profile::Matrix(_)));
}
#[test]
fn clut_corners_count_only_interpolating_axes() {
// 64 singleton axes and one interpolating one: two corners, not 2^65.
let mut grid = vec![1; 64];
grid.push(2);
let c = Clut { grid, outputs: 1, data: vec![0.0, 1.0] };
let mut x = vec![0.5; 64];
x.push(0.25);
assert!((c.eval(&x)[0] - 0.25).abs() < 1e-9);
// On grid points nothing interpolates.
let c = Clut { grid: vec![2; 40], outputs: 1, data: vec![] };
assert_eq!(c.eval(&[1.0; 40]), vec![0.0]);
// More interpolating axes than ICC allows: zero, at once.
assert_eq!(c.eval(&[0.5; 40]), vec![0.0]);
// The last grid point of every axis is that table entry, however many axes there are.
let mut data = vec![0.0; 1 << 17];
data[(1 << 17) - 1] = 0.75;
let c = Clut { grid: vec![2; 17], outputs: 1, data };
assert!((c.eval(&[1.0; 17])[0] - 0.75).abs() < 1e-9);
// Strides that overflow usize (64 two-point axes) read nothing instead of panicking.
let c = Clut { grid: vec![2; 64], outputs: 1, data: vec![] };
assert_eq!(c.eval(&[1.0; 64]), vec![0.0]);
assert_eq!(c.eval(&[0.5; 64]), vec![0.0]);
}
#[test]
fn clut_and_lut3d_interpolate() {
// A 2-point identity CLUT on 3 inputs is exact for every point.
+18 -1
View File
@@ -394,7 +394,7 @@ pub fn specs() -> Vec<EffectSpec> {
};
let render = || popup(&["Full", "Outside", "Inside"]);
let mut cyl = vec![
p("radius", "Radius", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)),
p("radius", "Radius", num(100.0), slider(0.0, 30000.0, 0.0, 200.0, 1)),
p("position", "Position", pt(0.5, 0.5), ParamUi::Point),
p("rotation", "Rotation", num(0.0), ParamUi::Angle),
p("rotationX", "Rotation X", num(0.0), ParamUi::Angle),
@@ -565,6 +565,23 @@ mod tests {
assert_eq!(out.get(50 + 18, 10)[3], 0.0);
}
/// #3368: Radius takes the range After Effects' CC Cylinder has, up to 30000 %, and a cylinder
/// that large still renders (nearly flat, fully opaque) instead of clamping to 400 %.
#[test]
fn cylinder_radius_goes_up_to_thirty_thousand_percent() {
let spec = find("ec.perspective.cccylinder").unwrap();
let radius = spec.params.iter().find(|p| p.id == "radius").unwrap();
assert!(matches!(radius.ui, ParamUi::Slider { min, max, .. } if min == 0.0 && max == 30000.0), "{:?}", radius.ui);
// The cylinder's axis on the left edge: at 400 % its radius is 400 / 100 * 100 / 2π ≈ 63.7 px,
// so a pixel 70 px along is past it there and inside the 30000 % one.
let img = Image::filled(100, 20, [0.5, 0.5, 0.5, 1.0]);
let at_edge = |radius: f64| run("ec.perspective.cccylinder", &img, &[("radius", num(radius)), ("position", Value::Vec2([0.0, 10.0]))]).img;
let clamped = at_edge(400.0).get(70, 10);
assert_eq!(clamped[3], 0.0, "beyond a 400 % cylinder: {clamped:?}");
let px = at_edge(30000.0).get(70, 10);
assert!(px.iter().all(|v| v.is_finite()) && px[3] > 0.99, "{px:?}");
}
#[test]
fn cylinder_axis_rotations_affect_geometry_and_texture() {
let img = Image::filled(100, 100, [0.5, 0.5, 0.5, 1.0]);
+2 -1
View File
@@ -565,7 +565,8 @@ impl PixelMotion {
let px = |id: &str| (g(id, 0.0).max(0.0) * scale).round() as u32;
PixelMotion {
tile,
window: ((g("tuning/blockSize", 8.0).max(4.0)) * scale).round().max(4.0) as u32,
// Block Size is declared 4..64; a project can store any number.
window: ((g("tuning/blockSize", 8.0).clamp(4.0, 64.0)) * scale).round().max(4.0) as u32,
global_smoothness: g("tuning/smoothing/globalSmoothness", 0.5).clamp(0.0, 1.0) as f32,
local_smoothness: g("tuning/smoothing/localSmoothness", 0.5).clamp(0.0, 1.0) as f32,
iterations: g("tuning/smoothing/smoothingIterations", 20.0).clamp(0.0, 200.0) as u32,
+58 -8
View File
@@ -115,18 +115,34 @@ fn box_row(src: &[f32], dst: &mut [f32], r: usize) {
dst.copy_from_slice(src);
return;
}
let get = |i: isize| src[i.clamp(0, n as isize - 1) as usize];
let norm = 1.0 / (2 * r + 1) as f32;
let mut acc = 0.0f64;
for i in -(r as isize)..=(r as isize) {
acc += get(i) as f64;
// Past the row the window only adds more copies of the edge pixels; the radius is clamped
// so the index arithmetic below cannot overflow (the means are unchanged in practice).
let r = r.min(MAX_BOX_RADIUS);
let (last, ri) = (n - 1, r as i64);
let get = |i: i64| src[i.clamp(0, last as i64) as usize];
let norm = 1.0 / (2.0 * r as f64 + 1.0);
// The first window, -r..=r: the edge pixels it repeats are counted, not visited, so the
// work depends on the row length and not on the radius.
let inside = r.min(last);
let mut acc = (r as f64 + 1.0) * src[0] as f64 + src[1..=inside].iter().map(|v| *v as f64).sum::<f64>();
// Only when the window actually repeats the last pixel: 0 * inf would be NaN otherwise.
if r > inside {
acc += (r - inside) as f64 * src[last] as f64;
}
for x in 0..n {
dst[x] = acc as f32 * norm;
acc += get(x as isize + r as isize + 1) as f64 - get(x as isize - r as isize) as f64;
dst[x] = (acc * norm) as f32;
let xi = x as i64;
acc += get(xi + ri + 1) as f64 - get(xi - ri) as f64;
}
}
/// Largest box radius [`box_row`] uses (larger radii come from absurd blur amounts in a
/// project, such as a Mocha shape's feather).
const MAX_BOX_RADIUS: usize = 1 << 30;
/// Largest sigma [`box_radii`] spreads into boxes (see [`MAX_BOX_RADIUS`]).
const MAX_BOX_SIGMA: f64 = 1e8;
/// Separable box mean with radii `rx`, `ry` (edges repeated).
pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
let mut cur = if rx > 0 { p.map_rows(|s, d| box_row(s, d, rx)) } else { p.clone() };
@@ -138,9 +154,10 @@ pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
/// Box radii for `n` passes approximating a Gaussian of `sigma`.
pub fn box_radii(sigma: f64, n: usize) -> Vec<usize> {
if sigma <= 0.0 {
if sigma.is_nan() || sigma <= 0.0 || n == 0 {
return vec![0; n];
}
let sigma = sigma.min(MAX_BOX_SIGMA);
let w_ideal = (12.0 * sigma * sigma / n as f64 + 1.0).sqrt();
let mut wl = w_ideal.floor() as i64;
if wl % 2 == 0 {
@@ -841,4 +858,37 @@ mod tests {
let o = guided_filter(&g, &p, 2, 1e-3);
assert!(o.data.iter().all(|v| (v - 0.7).abs() < 1e-3));
}
/// The box mean counts repeated edge pixels instead of visiting them: the same means as a
/// direct sum, and a huge radius (a Mocha feather of 1e300) costs a row's worth of work.
#[test]
fn box_row_matches_a_direct_sum_and_bounds_the_radius() {
let src = [1.0f32, 2.0, 3.0, 5.0, 8.0];
for r in [0usize, 1, 2, 4, 5, 9, 40] {
let mut dst = [0.0f32; 5];
box_row(&src, &mut dst, r);
for (x, d) in dst.iter().enumerate() {
let ri = r as i64;
let sum: f64 = (x as i64 - ri..=x as i64 + ri).map(|i| src[i.clamp(0, 4) as usize] as f64).sum();
let want = (sum / (2 * r + 1) as f64) as f32;
assert!((d - want).abs() < 1e-5, "r={r} x={x}: {d} vs {want}");
}
}
// An infinite last pixel inside the window still yields a finite first mean.
let mut inf = [0.0f32; 3];
box_row(&[1.0, 2.0, f32::INFINITY], &mut inf, 1);
assert!((inf[0] - 4.0 / 3.0).abs() < 1e-6, "{inf:?}");
let mut one = [0.0f32];
box_row(&[0.25], &mut one, usize::MAX);
assert!((one[0] - 0.25).abs() < 1e-6);
let mut dst = [0.0f32; 5];
box_row(&src, &mut dst, usize::MAX);
assert!(dst.iter().all(|v| v.is_finite() && (1.0..=8.0).contains(v)), "{dst:?}");
for sigma in [1e300, f64::MAX, f64::INFINITY, f64::NAN, -1.0] {
assert_eq!(box_radii(sigma, 3).len(), 3);
}
let p = Plane { w: 4, h: 3, data: vec![0.5; 12] };
let g = gauss_plane(&p, 1e300, 1e300);
assert!(g.data.iter().all(|v| (v - 0.5).abs() < 1e-4), "{:?}", g.data);
}
}
+17 -1
View File
@@ -344,7 +344,9 @@ pub fn load_lut(src: &str) -> Option<Arc<Lut>> {
{
return v.clone();
}
let inline = src.contains('\n') || src.contains("LUT_");
// `.cube` text always spans lines (a size header, then data rows); a path never does, even one
// whose file name contains `LUT_` (#3454). Trimmed, as the path is, so a trailing newline is a path.
let inline = src.trim().contains('\n');
let parsed = if inline { parse_cube(src) } else { std::fs::read_to_string(src.trim()).ok().and_then(|t| parse_cube(&t)) }.map(Arc::new);
if let Ok(mut m) = cache.lock() {
m.insert(src.to_string(), parsed.clone());
@@ -484,6 +486,20 @@ mod tests {
assert_eq!(out.img, img);
}
/// #3454: a path containing `LUT_` was taken for `.cube` text and the file never loaded.
#[test]
fn a_path_whose_name_contains_lut_loads_the_file() {
let dir = std::env::temp_dir().join(format!("ec-fx-3454-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("LUT_x.cube");
std::fs::write(&path, identity_cube(2)).unwrap();
let path = path.to_string_lossy().into_owned();
assert!(load_lut(&path).is_some(), "{path}");
assert!(load_lut(&format!("{path}\r\n")).is_some(), "{path} with a trailing newline");
assert!(load_lut(&identity_cube(2)).is_some(), "inline text");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn one_d_lut_with_domain() {
let lut = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 2 2 2\n1 1 1\n0 0 0\n").unwrap();
+44 -44
View File
@@ -413,46 +413,46 @@ fn plan_effects_golden_hashes() {
const GOLDEN: &[(&str, u64)] = &[
("ec.obsolete.lightning/0/8bpc/t0.4", 0xd829bb3a79ec2d61),
("ec.obsolete.lightning/0/8bpc/t1.3", 0xb2221364c0defe9b),
("ec.obsolete.lightning/0/32bpc/t0.4", 0x828cc4e53d579af7),
("ec.obsolete.lightning/0/32bpc/t1.3", 0x15770238cde9e766),
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0xf64f2b87fa9a09d3),
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0x625a00bd52fde5aa),
("ec.obsolete.lightning/0/32bpc/t0.4", 0x38c8b3ca5d03eb81),
("ec.obsolete.lightning/0/32bpc/t1.3", 0x552241c6035fa660),
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0x6158941311ce44d9),
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0xc7c2a804de6140a3),
("ec.obsolete.lightning/1/8bpc/t0.4", 0xa7c8cb94beebca84),
("ec.obsolete.lightning/1/8bpc/t1.3", 0x6e583b1d1f455099),
("ec.obsolete.lightning/1/32bpc/t0.4", 0x2ffd973cc7de98f7),
("ec.obsolete.lightning/1/32bpc/t1.3", 0x8f35e6018cac57d7),
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x96c14e911783f8d6),
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x620ee605f56c2068),
("ec.obsolete.lightning/1/32bpc/t0.4", 0xe34f30bf669e94d5),
("ec.obsolete.lightning/1/32bpc/t1.3", 0xb1ec8fad75bbbc32),
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x9e2934559f25d0d9),
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x8d2f6967b73bbf54),
("ec.obsolete.lightning/2/8bpc/t0.4", 0x119e5cedac21cd9b),
("ec.obsolete.lightning/2/8bpc/t1.3", 0x04b775fba837ddce),
("ec.obsolete.lightning/2/32bpc/t0.4", 0xfee767091adac4ce),
("ec.obsolete.lightning/2/32bpc/t1.3", 0x2ae26f9670c93751),
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x99111f77bf26704f),
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0x8eb781efc14183c3),
("ec.obsolete.lightning/2/32bpc/t0.4", 0x551415572abb270a),
("ec.obsolete.lightning/2/32bpc/t1.3", 0x3e1b68b5b9cf2e8c),
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x69b347a75e8ac280),
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0xe89cc144b611a083),
("ec.generate.advancedlightning/0/8bpc/t0.4", 0x5a53bf1e0236225d),
("ec.generate.advancedlightning/0/8bpc/t1.3", 0x5a53bf1e0236225d),
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x6817bf5b3583e0a0),
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x6817bf5b3583e0a0),
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x5dcc055f6cca9922),
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x5dcc055f6cca9922),
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x8ae323866971eaf4),
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x8ae323866971eaf4),
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x74b4ba806ab8e78b),
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x74b4ba806ab8e78b),
("ec.generate.advancedlightning/1/8bpc/t0.4", 0xf07f11457fa683d0),
("ec.generate.advancedlightning/1/8bpc/t1.3", 0xf07f11457fa683d0),
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1b21531df9187431),
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1b21531df9187431),
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0xf1032a0a9345edf1),
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0xf1032a0a9345edf1),
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1474a59cbdbcb6aa),
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1474a59cbdbcb6aa),
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0x4808561ce9d8a4a6),
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0x4808561ce9d8a4a6),
("ec.generate.advancedlightning/2/8bpc/t0.4", 0x62b5d179f05c6a4c),
("ec.generate.advancedlightning/2/8bpc/t1.3", 0x62b5d179f05c6a4c),
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x192256fe81f73975),
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x192256fe81f73975),
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x953dd51008c3ce31),
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x953dd51008c3ce31),
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x73c4b44db599a5c4),
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x73c4b44db599a5c4),
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x3085cf1b5f6e8311),
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x3085cf1b5f6e8311),
("ec.generate.advancedlightning/3/8bpc/t0.4", 0x57b6ad61bd3d89e0),
("ec.generate.advancedlightning/3/8bpc/t1.3", 0x57b6ad61bd3d89e0),
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x7e7c9d20e94c8b6c),
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x7e7c9d20e94c8b6c),
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xa37940f5a490a0fe),
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xa37940f5a490a0fe),
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x86eee3d36bfd41f2),
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x86eee3d36bfd41f2),
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xb24444985859d820),
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xb24444985859d820),
("ec.generate.radiowaves/0/8bpc/t0.4", 0xbc3643b8af12c44b),
("ec.generate.radiowaves/0/8bpc/t1.3", 0x703e948dc562e132),
("ec.generate.radiowaves/0/32bpc/t0.4", 0x2b0db208ee7517c1),
@@ -479,8 +479,8 @@ const GOLDEN: &[(&str, u64)] = &[
("ec.generate.radiowaves/3/32bpc/half/t1.3", 0xcf62c191e2aa8cc4),
("ec.generate.vegas/0/8bpc/t0.4", 0x5e58d44254e9101b),
("ec.generate.vegas/0/8bpc/t1.3", 0x5e58d44254e9101b),
("ec.generate.vegas/0/32bpc/t0.4", 0xe18712686e402759),
("ec.generate.vegas/0/32bpc/t1.3", 0xe18712686e402759),
("ec.generate.vegas/0/32bpc/t0.4", 0x2c6093dc655c6dbe),
("ec.generate.vegas/0/32bpc/t1.3", 0x2c6093dc655c6dbe),
("ec.generate.vegas/0/32bpc/half/t0.4", 0xb037c97234bbd936),
("ec.generate.vegas/0/32bpc/half/t1.3", 0xb037c97234bbd936),
("ec.generate.vegas/1/8bpc/t0.4", 0x68566eb5dc411cd3),
@@ -491,8 +491,8 @@ const GOLDEN: &[(&str, u64)] = &[
("ec.generate.vegas/1/32bpc/half/t1.3", 0xe2f8f5f4a12628d6),
("ec.generate.vegas/2/8bpc/t0.4", 0x002c48a91aea2dcf),
("ec.generate.vegas/2/8bpc/t1.3", 0x002c48a91aea2dcf),
("ec.generate.vegas/2/32bpc/t0.4", 0x4f550bbbf8b8316b),
("ec.generate.vegas/2/32bpc/t1.3", 0x4f550bbbf8b8316b),
("ec.generate.vegas/2/32bpc/t0.4", 0xd703631844687600),
("ec.generate.vegas/2/32bpc/t1.3", 0xd703631844687600),
("ec.generate.vegas/2/32bpc/half/t0.4", 0x30036df3e56a01ed),
("ec.generate.vegas/2/32bpc/half/t1.3", 0x30036df3e56a01ed),
("ec.generate.stroke/0/8bpc/t0.4", 0xa580fbfb806d655c),
@@ -557,10 +557,10 @@ const GOLDEN: &[(&str, u64)] = &[
("ec.generate.paintbucket/1/32bpc/half/t1.3", 0xf5ea3b0a7ac224ed),
("ec.generate.paintbucket/2/8bpc/t0.4", 0x530d0a248dc22c72),
("ec.generate.paintbucket/2/8bpc/t1.3", 0x530d0a248dc22c72),
("ec.generate.paintbucket/2/32bpc/t0.4", 0xe1b655f8815e1357),
("ec.generate.paintbucket/2/32bpc/t1.3", 0xe1b655f8815e1357),
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0xb95b3b205255cd7e),
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0xb95b3b205255cd7e),
("ec.generate.paintbucket/2/32bpc/t0.4", 0xf05e50b4261103a3),
("ec.generate.paintbucket/2/32bpc/t1.3", 0xf05e50b4261103a3),
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0x7e74e09fcc83615c),
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0x7e74e09fcc83615c),
("ec.generate.paintbucket/3/8bpc/t0.4", 0xa7485b7277e03e66),
("ec.generate.paintbucket/3/8bpc/t1.3", 0xa7485b7277e03e66),
("ec.generate.paintbucket/3/32bpc/t0.4", 0xc8e06c6eed30448b),
@@ -666,16 +666,16 @@ const GOLDEN: &[(&str, u64)] = &[
("ec.key.innerouter/0/32bpc/half/t1.3", 0x003941eb2cd18339),
("ec.key.innerouter/1/8bpc/t0.4", 0xd7bfea3d9550db56),
("ec.key.innerouter/1/8bpc/t1.3", 0xd7bfea3d9550db56),
("ec.key.innerouter/1/32bpc/t0.4", 0x62f7705e43cc6a71),
("ec.key.innerouter/1/32bpc/t1.3", 0x62f7705e43cc6a71),
("ec.key.innerouter/1/32bpc/half/t0.4", 0x22f15f770f22f072),
("ec.key.innerouter/1/32bpc/half/t1.3", 0x22f15f770f22f072),
("ec.key.innerouter/1/32bpc/t0.4", 0x36661c78c393b52a),
("ec.key.innerouter/1/32bpc/t1.3", 0x36661c78c393b52a),
("ec.key.innerouter/1/32bpc/half/t0.4", 0x73f19ba1aea7e496),
("ec.key.innerouter/1/32bpc/half/t1.3", 0x73f19ba1aea7e496),
("ec.key.innerouter/2/8bpc/t0.4", 0x7bdece44f841bb11),
("ec.key.innerouter/2/8bpc/t1.3", 0x7bdece44f841bb11),
("ec.key.innerouter/2/32bpc/t0.4", 0x5130a1fc09c42120),
("ec.key.innerouter/2/32bpc/t1.3", 0x5130a1fc09c42120),
("ec.key.innerouter/2/32bpc/half/t0.4", 0x46c0880853618fc8),
("ec.key.innerouter/2/32bpc/half/t1.3", 0x46c0880853618fc8),
("ec.key.innerouter/2/32bpc/t0.4", 0x093157401c50396e),
("ec.key.innerouter/2/32bpc/t1.3", 0x093157401c50396e),
("ec.key.innerouter/2/32bpc/half/t0.4", 0xbde58c0668a48529),
("ec.key.innerouter/2/32bpc/half/t1.3", 0xbde58c0668a48529),
("ec.color.colorlink/0/8bpc/t0.4", 0x539d1da6b2ed744a),
("ec.color.colorlink/0/8bpc/t1.3", 0x539d1da6b2ed744a),
("ec.color.colorlink/0/32bpc/t0.4", 0x67ed14da67ce1a97),
+8 -8
View File
@@ -537,18 +537,18 @@ const GOLDEN: &[(&str, u64)] = &[
("ec.sim.caustics/1/8bpc/t1.5", 0x282968317c36ccdf),
("ec.sim.caustics/1/8bpc/t0.8", 0x282968317c36ccdf),
("ec.sim.caustics/1/8bpc/t2.2", 0x282968317c36ccdf),
("ec.sim.caustics/1/32bpc/t0.5", 0xc12df8561f396d45),
("ec.sim.caustics/1/32bpc/t1.5", 0xc12df8561f396d45),
("ec.sim.caustics/1/32bpc/t0.8", 0xc12df8561f396d45),
("ec.sim.caustics/1/32bpc/t2.2", 0xc12df8561f396d45),
("ec.sim.caustics/1/32bpc/t0.5", 0xaa63d15038f75de2),
("ec.sim.caustics/1/32bpc/t1.5", 0xaa63d15038f75de2),
("ec.sim.caustics/1/32bpc/t0.8", 0xaa63d15038f75de2),
("ec.sim.caustics/1/32bpc/t2.2", 0xaa63d15038f75de2),
("ec.sim.caustics/2/8bpc/t0.5", 0x52ab258ae56e9b15),
("ec.sim.caustics/2/8bpc/t1.5", 0x52ab258ae56e9b15),
("ec.sim.caustics/2/8bpc/t0.8", 0x52ab258ae56e9b15),
("ec.sim.caustics/2/8bpc/t2.2", 0x52ab258ae56e9b15),
("ec.sim.caustics/2/32bpc/t0.5", 0x9a820d0e7c6d11ea),
("ec.sim.caustics/2/32bpc/t1.5", 0x9a820d0e7c6d11ea),
("ec.sim.caustics/2/32bpc/t0.8", 0x9a820d0e7c6d11ea),
("ec.sim.caustics/2/32bpc/t2.2", 0x9a820d0e7c6d11ea),
("ec.sim.caustics/2/32bpc/t0.5", 0x51d4304b505b5462),
("ec.sim.caustics/2/32bpc/t1.5", 0x51d4304b505b5462),
("ec.sim.caustics/2/32bpc/t0.8", 0x51d4304b505b5462),
("ec.sim.caustics/2/32bpc/t2.2", 0x51d4304b505b5462),
("ec.sim.waveworld/0/8bpc/t0.5", 0x425d5d639d2a6495),
("ec.sim.waveworld/0/8bpc/t1.5", 0x233d7370e580804a),
("ec.sim.waveworld/0/8bpc/t0.8", 0x866da86ce5e78eb2),
+13 -2
View File
@@ -219,7 +219,16 @@ fn fnv(s: &str) -> u64 {
/// What the effect's input frames are made of: the layer's source and timing, its masks and the
/// effects above the Roto Brush.
pub fn input_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String {
let mut s = crate::warp::signature(p, comp, layer);
input_key_with_signature(crate::warp::signature(p, comp, layer), layer, uid)
}
/// Recognize the input key serialized before 0.23 without re-keying its frozen matte.
fn input_key_before_placed_page(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> Option<String> {
let signature = crate::warp::signature_before_placed_page(p, comp, layer)?;
Some(input_key_with_signature(signature, layer, uid))
}
fn input_key_with_signature(mut s: String, layer: &Layer, uid: Uid) -> String {
if let Some(m) = layer.masks() {
s.push_str(&format!("{m:?}"));
}
@@ -247,7 +256,9 @@ pub(crate) fn sync(before: &Project, after: &mut Project) -> Vec<(ItemId, LayerI
for g in fxg.groups().filter(|g| is_roto(g) || is_object_matte(g)) {
let key = input_key(after, *cid, l, g.uid);
let old = before.comp(*cid).and_then(|bc| bc.layer(l.id)).and_then(|ol| ol.props.find_group(g.uid));
if str_of(g, INPUT_KEY) != key {
let saved_key = str_of(g, INPUT_KEY);
// Keep a matching old key: the frozen cache itself also hashes that key.
if saved_key != key && input_key_before_placed_page(after, *cid, l, g.uid).as_deref() != Some(saved_key.as_str()) {
set.push((*cid, l.id, g.uid, key));
todo.push((*cid, l.id, g.uid));
} else if old.is_none_or(|o| guard_of(o) != guard_of(g)) {
+14
View File
@@ -119,6 +119,20 @@ pub fn signature(p: &Project, comp: ItemId, layer: &Layer) -> String {
},
_ => String::new(),
};
signature_with_source(p, comp, layer, &src)
}
/// Before `Footage::placed_page` was added in 0.23, the persisted signature hashed the
/// same Debug representation without that last field. Only ordinary footage can match it.
pub(crate) fn signature_before_placed_page(p: &Project, comp: ItemId, layer: &Layer) -> Option<String> {
let LayerSource::Footage { item } = &layer.source else { return None };
let ItemKind::Footage(footage) = &p.item(*item)?.kind else { return None };
let src = format!("{footage:?}");
let old = src.strip_suffix(", placed_page: false }")?;
Some(signature_with_source(p, comp, layer, &format!("{old} }}")))
}
fn signature_with_source(p: &Project, comp: ItemId, layer: &Layer, src: &str) -> String {
let rate = p.comp(comp).map(|c| format!("{:?}", c.frame_rate)).unwrap_or_default();
let s = format!(
"{:?}|{src}|{:?}|{:?}|{:?}|{}|{:?}|{rate}",
+4 -1
View File
@@ -430,7 +430,10 @@ impl FrameServer {
}
}
// A frame still waiting for readbacks is replaced: end it, so every `frame_begin` has its `frame_end`.
if self.pending.take().is_some() {
if let Some(old) = self.pending.take() {
if let (Some((key, chose)), Some(pick)) = (old.auto, a.auto_pick()) {
pick.abandoned(key, chose);
}
a.frame_end();
}
a.frame_begin();
+3
View File
@@ -31,6 +31,7 @@ pub(crate) mod frame_export;
mod frontend;
mod help;
mod import_project;
mod journal;
mod key_labels;
mod key_transform;
mod keys_more;
@@ -161,6 +162,7 @@ mod params;
mod schema;
use super::commands;
pub use aep::is_aep_path;
pub use enabled::always;
pub(crate) use enabled::{has_comp, has_keys, has_layers, has_project_selection, not_yet, not_yet_run};
pub use params::match_path_of;
@@ -201,6 +203,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(nav_more::specs());
v.extend(render_queue::specs());
v.extend(help::specs());
v.extend(journal::specs());
v.extend(query::specs());
v.extend(batch::specs());
v.extend(layer_menu::specs());
+21 -9
View File
@@ -1,5 +1,5 @@
//! File ▸ Import ▸ After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸ After Effects
//! Project… (`effectcraft_aep::export`).
//! File ▸ Import ▸ Adobe After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸
//! Adobe After Effects Project… (`effectcraft_aep::export`).
//!
//! Adds the items of an `.aep` (or its XML form, `.aepx`) to the project in a folder named after
//! the file (`docs/aep-import.md` lists what arrives). What the importer cannot bring over (effects
@@ -16,6 +16,14 @@ use serde_json::{Value, json};
use super::{CommandSpec, always, b_p, bad, str_p};
use crate::{EngineError, Result, Session, cmd};
/// Whether `path` is an After Effects project (`.aep`, or its XML form `.aepx`): imported as a
/// project (`file.importAep`), not as footage.
pub fn is_aep_path(path: &str) -> bool {
std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|e| e.eq_ignore_ascii_case("aep") || e.eq_ignore_ascii_case("aepx"))
}
/// `file.importAep`: the `.aep` / `.aepx` at `path` into the open project, in a folder named after
/// the file; refused ([`EngineError::Refused`]) when parts would be lost and `allowLosses` is not set.
fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
let path = str_p(p, "path").ok_or_else(|| bad("file.importAep", "missing `path`"))?.to_string();
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
@@ -40,11 +48,12 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(json!({"dryRun": true, "report": report}));
}
let importer = s.importer.clone();
let res = s.edit("Import After Effects Project", None, |proj, st| {
let res = s.edit("Import Adobe After Effects Project", None, |proj, st| {
let r = effectcraft_aep::import::import(proj, &bytes, &options).map_err(|e| match e {
AepImportError::Refused(_) => {
EngineError::Other(format!("{e} (nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report)"))
}
AepImportError::Refused(_) => EngineError::Refused {
losses: e.to_string(),
advice: "nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report",
},
other => EngineError::Other(format!("cannot import {path}: {other}")),
})?;
if apply {
@@ -90,6 +99,8 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
}))
}
/// `file.exportAep`: the open project as an `.aep` at `path`; refused ([`EngineError::Refused`])
/// when parts would be lost and `allowLosses` is not set.
fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
let path = str_p(p, "path").ok_or_else(|| bad("file.exportAep", "missing `path`"))?.to_string();
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
@@ -112,7 +123,7 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
};
let res = effectcraft_aep::export::export_with(&s.project, &ExportOptions { allow_losses }, &folder_time).map_err(|e| match e {
AepExportError::Refused(_) => {
EngineError::Other(format!("{e} (nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report)"))
EngineError::Refused { losses: e.to_string(), advice: "nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report" }
}
other => EngineError::Other(format!("cannot export {path}: {other}")),
})?;
@@ -126,11 +137,12 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"path": path, "bytes": res.bytes.len(), "report": res.report}))
}
/// File ▸ Import / Export ▸ Adobe After Effects Project….
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
"file.importAep",
"After Effects Project...",
"Adobe After Effects Project...",
["File", "Import"],
None,
"{path (.aep or .aepx), allowLosses?: bool (import although parts cannot be imported), dryRun?: bool (only return the report), applySettings?: bool (take over the file's project settings; default: when the project has no compositions)}",
@@ -139,7 +151,7 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"file.exportAep",
"After Effects Project...",
"Adobe After Effects Project...",
["File", "Export"],
None,
"{path (.aep), allowLosses?: bool (export although parts cannot be exported), dryRun?: bool (only return the report)}",
+16 -2
View File
@@ -404,8 +404,11 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
let mut comps = vec![];
let mut items = vec![];
let mut errors = vec![];
// EffectCraft projects import as projects, as After Effects imports an .aep.
let (project_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
// EffectCraft projects import as projects, as After Effects imports an .aep; so do After
// Effects projects, refused when parts of them cannot come over. The app asks about those
// before it gets here (`panels::aep_losses`); a script or agent gets the refusal.
let (project_paths, paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
let (aep_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::is_aep_path(x));
let mut projects = vec![];
for path in project_paths {
match s.execute("file.importProject", json!({"path": path})) {
@@ -413,6 +416,17 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
Err(e) => errors.push(format!("{path}: {e}")),
}
}
for path in aep_paths {
match s.execute("file.importAep", json!({"path": path})) {
Ok(r) => projects.push(r),
// `file.import` has no `allowLosses`: point at the entry that lists the losses, and at
// the command that takes the consent.
Err(EngineError::Refused { losses, .. }) => errors.push(format!(
"{path}: {losses}; File ▸ Import ▸ Adobe After Effects Project… lists them and can import the rest (`file.importAep` with `allowLosses`)"
)),
Err(e) => errors.push(format!("{path}: {e}")),
}
}
if let Some(retain) = retain {
let mut rest = vec![];
for path in paths {
+53
View File
@@ -0,0 +1,53 @@
//! The command journal as a replay script (see [`crate::replay`]): `journal.export` for agents
//! and `help.exportRepro` for bug reports. Neither is journaled.
use serde_json::{Value, json};
use super::{CommandSpec, always, str_p};
use crate::{Result, Session, replay};
/// `journal.export {path?}`: the script itself, or written to `path`.
fn export(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(path) = str_p(p, "path") {
let (path, steps) = replay::save_repro(s, Some(path))?;
return Ok(json!({"path": path, "steps": steps}));
}
let script = replay::export(s, None);
Ok(json!({"steps": script.lines().count().saturating_sub(1), "script": script}))
}
/// `help.exportRepro {path?}`: save the steps so far for a bug report (default: the settings
/// folder's `Repros`).
fn export_repro(s: &mut Session, p: &Value) -> Result<Value> {
let (path, steps) = replay::save_repro(s, str_p(p, "path"))?;
Ok(json!({"path": path, "steps": steps}))
}
const fn spec(id: &'static str, label: &'static str, params: &'static str, run: super::Run) -> CommandSpec {
CommandSpec { id, label, menu: &[], shortcut: None, alt_shortcuts: &[], params, enabled: always, run, journal: false }
}
pub fn specs() -> Vec<CommandSpec> {
#[allow(unused_mut)]
let mut v = vec![
spec(
"journal.export",
"Export Command Journal",
"{path? (write the script there)} → {script, steps} or {path, steps}: the commands run since the project was last opened, as a replay script (docs/agents.md, Repro scripts)",
export,
),
spec(
"help.exportRepro",
"Save Bug Repro...",
"{path? (default: Repros in the settings folder)} → {path, steps}: the steps since the project was opened, for a bug report (commands and parameters only)",
export_repro,
),
];
// A command with a bug, for the crash-repro tests.
#[cfg(test)]
v.push(spec("test.panic", "Panic (test)", "{why?}", |_, p| {
let why = str_p(p, "why").unwrap_or("test").to_string();
std::panic::panic_any(why)
}));
v
}
+1 -1
View File
@@ -24,7 +24,7 @@ mod transform;
use contents::{add_mask, add_shape_item, mask_props, set_text};
use create::{add_item, new_adjustment, new_null, new_shape, new_solid, new_text};
pub(crate) use create::{color_p, index_p, insert_layer, insert_layer_at, place, position_p};
pub(crate) use create::{color_p, fit_span, index_p, insert_layer, insert_layer_at, new_layer_span, place, position_p, show_span};
pub(crate) use parent::parent_fixes;
use parent::set_parent;
use replace::{has_replace_source, replace_source};
+45 -2
View File
@@ -14,6 +14,39 @@ pub(crate) fn color_p(p: &Value, k: &str) -> Option<[f32; 3]> {
}
}
/// Settings ▸ General ▸ Fit New Layers to Work Area: the `(start, end)` that new text, solid,
/// shape, null and adjustment layers in `comp` take, or `None` for the whole composition.
pub(crate) fn new_layer_span(s: &Session, comp: &effectcraft_project::Comp) -> Option<(Tick, Tick)> {
let (start, end) = (comp.work_area.0, comp.work_area.1.min(comp.duration));
(s.prefs.general.fit_new_layers_to_work_area && start >= Tick::ZERO && start < end).then_some((start, end))
}
/// Start a new layer at `span`'s start and end it at `span`'s end, when there is a span.
pub(crate) fn fit_span(l: &mut Layer, span: Option<(Tick, Tick)>) {
if let Some((start, end)) = span {
l.start_time = start;
l.in_point = start;
l.out_point = end;
}
}
/// Tools draw on the layer they create: when it was fitted to a span the playhead is outside,
/// move `comp`'s playhead to the first frame in the span so the new layer shows. The layer's timing
/// is kept. The work area need not sit on frames (a typed start, a frame-rate change), so the
/// frame is rounded up from the start, not to the nearest; a span holding no frame moves nothing.
pub(crate) fn show_span(s: &mut Session, comp: ItemId, span: Option<(Tick, Tick)>) {
let Some(fr) = s.project.comp(comp).map(|c| c.frame_rate) else { return };
if let Some((start, end)) = span
&& !(start..end).contains(&s.time_of(comp))
{
let first = fr.snap(start);
let first = if first < start { fr.tick_of(fr.frame_at(start).saturating_add(1)) } else { first };
if first < end {
s.set_time_of(comp, first);
}
}
}
/// Insert a new layer above the selection (or at the top) and select it.
pub(crate) fn insert_layer(proj: &mut Project, st: &mut crate::EditorState, cid: ItemId, layer: Layer) -> Result<LayerId> {
insert_layer_at(proj, st, cid, layer, None)
@@ -89,10 +122,12 @@ fn new_solid_like(s: &mut Session, p: &Value, adjustment: bool) -> Result<Value>
let pixel_aspect = pixel_aspect_p(p)?.unwrap_or(comp.pixel_aspect);
let default_name = if adjustment { "Adjustment Layer 1".to_string() } else { "Solid 1".to_string() };
let name = str_p(p, "name").map(str::to_string).unwrap_or(default_name);
let span = new_layer_span(s, &comp);
let id = s.edit(if adjustment { "New Adjustment Layer" } else { "New Solid" }, None, |proj, st| {
let folder = solids_folder(proj);
let sid = proj.add_item(&name, Label::Red, Some(folder), ItemKind::Solid(Solid { color, width: w, height: h, pixel_aspect }));
let mut l = build::layer(proj, &comp, &name, LayerSource::Solid { item: sid }, (w, h), None);
fit_span(&mut l, span);
if adjustment {
l.switches.adjustment = true;
l.label = Label::Purple;
@@ -186,9 +221,11 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
}
}
let edit = b_p(p, "edit").unwrap_or(false);
let span = new_layer_span(s, &comp);
let id = s.edit("New Text Layer", None, |proj, st| {
let name: String = layer_name.clone().unwrap_or_else(|| effectcraft_project::text_layer_name(&doc.text));
let mut l = build::layer(proj, &comp, if name.is_empty() { "Text" } else { &name }, LayerSource::Text, (comp.width, comp.height), None);
fit_span(&mut l, span);
// Named after its text, the name follows the text until renamed; a given name does not.
l.name_follows_text = Some(layer_name.as_deref().is_none_or(str::is_empty));
if let Some(pr) = l.props.prop_mut("text/sourceText") {
@@ -213,6 +250,9 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
}
Ok(id)
})?;
if edit {
show_span(s, cid, span);
}
Ok(json!({"layer": id.0}))
}
@@ -232,8 +272,9 @@ pub(super) fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
.and_then(|v| v.as_array())
.map(|a| [a.first().and_then(Value::as_f64).unwrap_or(0.0), a.get(1).and_then(Value::as_f64).unwrap_or(0.0)]);
let name = str_p(p, "name").unwrap_or("Shape Layer 1").to_string();
let span = new_layer_span(s, &comp);
let id = s.edit("New Shape Layer", None, |proj, st| {
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos)?;
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos, span)?;
let mut next = proj.next_id;
let g = crate::cmd::shape_tool::shape_group(&mut Ids(&mut next), &kind, size, &paint);
proj.next_id = next;
@@ -249,8 +290,10 @@ fn new_simple(s: &mut Session, p: &Value, src: LayerSource, name: &str, label: &
let cid = comp_id(s, p)?;
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
let name = str_p(p, "name").unwrap_or(name).to_string();
let span = new_layer_span(s, &comp);
let id = s.edit(label, None, |proj, st| {
let l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
let mut l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
fit_span(&mut l, span);
insert_layer(proj, st, cid, l)
})?;
Ok(json!({"layer": id.0}))
+10 -2
View File
@@ -8,6 +8,7 @@ use effectcraft_geom::vec2;
use effectcraft_keyframe::{Gradient, ShapePath, Value as KV};
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{Comp, ItemId, LayerId, LayerSource, Project, PropGroup};
use effectcraft_time::Tick;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -263,7 +264,8 @@ pub(crate) fn draw_target(s: &Session, comp: &Comp, p: &Value, cmd: &str) -> Res
}
/// A new empty shape layer ("Shape Layer n" unless `name`) with its Position at `position` (comp
/// pixels; else the comp centre), above the selected layer and selected.
/// pixels; else the comp centre), above the selected layer and selected. `span` is the
/// [`new_layer_span`](super::layer::new_layer_span) the layer takes, if any.
pub(crate) fn new_shape_layer(
proj: &mut Project,
st: &mut EditorState,
@@ -271,11 +273,13 @@ pub(crate) fn new_shape_layer(
cid: ItemId,
name: Option<&str>,
position: Option<[f64; 2]>,
span: Option<(Tick, Tick)>,
) -> Result<LayerId> {
let count = comp.layers.iter().filter(|l| matches!(l.source, LayerSource::Shape)).count();
let name = name.map(str::to_string).unwrap_or_else(|| format!("Shape Layer {}", count + 1));
let mut l = build::layer(proj, comp, &name, LayerSource::Shape, (comp.width, comp.height), None);
place(&mut l, position);
super::layer::fit_span(&mut l, span);
insert_layer(proj, st, cid, l)
}
@@ -346,10 +350,11 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
_ => "Rectangle Tool",
};
let name = str_p(p, "name");
let span = super::layer::new_layer_span(s, &comp);
let (lid, group) = s.edit(label, None, |proj, st| {
let lid = match target {
Some(l) => l,
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos))?,
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos), span)?,
};
let mut next = proj.next_id;
let g = shape_group(&mut Ids(&mut next), kind, size, &paint).ok_or_else(|| bad(c, "unknown kind"))?;
@@ -358,6 +363,9 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
st.selected_layers = vec![lid];
Ok((lid, group))
})?;
if target.is_none() {
super::layer::show_span(s, cid, span);
}
Ok(json!({"layer": lid.0, "group": group}))
}
+17 -1
View File
@@ -43,7 +43,9 @@ pub fn display_duration(s: &Session, comp: &Comp, len: Tick) -> String {
}
/// Parse a time typed in the project's Time Display Style (relative `+n`/`-n` from frame
/// `current`) into a comp frame.
/// `current`) into a comp frame. In Timecode style, in a comp whose Start Timecode is negative, a
/// `-` before a timecode with separators (`-0:00:00:12`, `-1.00`) is the sign of a displayed time,
/// not a step back; a bare `-n` still steps back.
pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) -> std::result::Result<i64, String> {
let fr = comp.frame_rate;
let st = &s.project.settings;
@@ -53,6 +55,20 @@ pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) ->
effectcraft_time::parse_feet_frames(text, pf, current + start).map(|f| f - start).map_err(|e| e.0)
}
None => {
// A comp whose Start Timecode is negative shows the times before 0 with a `-`
// (`-0:00:00:12`), so there a `-` before a timecode with separators is the sign of a
// displayed time and typing what the display shows lands on the frame it shows. A bare
// `-n` still moves n frames back.
if st.time_display == TimeDisplayStyle::Timecode
&& comp.display_start < Tick::ZERO
&& let Some(body) = text.trim().strip_prefix('-').map(str::trim_start)
&& body.contains([':', ';', '.', ','])
&& !body.starts_with(['+', '-'])
{
let drop_frame = fr.supports_drop_frame() && body.contains(';');
let shown = effectcraft_time::parse_timecode(body, fr, drop_frame, current).map_err(|e| e.0)?;
return Ok(shown.saturating_neg().saturating_sub(fr.frame_at(comp.display_start)));
}
// The Timeline shows non-drop timecode (`:`), so typed `:`/`.` text is read as non-drop
// too; only explicit `;` fields mean drop-frame (#890).
let drop_frame = fr.supports_drop_frame() && text.contains(';');
+5 -1
View File
@@ -282,10 +282,11 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
path.vertices.iter().fold(([f64::MAX; 2], [f64::MIN; 2]), |(lo, hi), v| ([lo[0].min(v[0]), lo[1].min(v[1])], [hi[0].max(v[0]), hi[1].max(v[1])]));
let bounds = [lo[0], lo[1], hi[0] - lo[0], hi[1] - lo[1]];
let name = str_p(p, "name").map(str::to_string);
let span = super::layer::new_layer_span(s, &comp);
let (lid, group, path_uid) = s.edit("Pen Tool", None, |proj, st| {
let lid = match target {
Some(l) => l,
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None)?,
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None, span)?,
};
let mut next = proj.next_id;
let mut ids = Ids(&mut next);
@@ -300,6 +301,9 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
st.selected_vertices = vec![VertexRef { layer: lid, mask: path_uid, index: n - 1 }];
Ok((lid, group, path_uid))
})?;
if target.is_none() {
super::layer::show_span(s, cid, span);
}
Ok(json!({"layer": lid.0, "group": group, "path": path_uid}))
}
+4
View File
@@ -15,6 +15,10 @@ pub enum EngineError {
Project(#[from] effectcraft_project::ProjectError),
#[error("{0}")]
Other(String),
/// Refused because parts would be lost in a conversion; nothing changed. `losses` names them,
/// `advice` how to go on (`file.importAep` / `file.exportAep` without `allowLosses`).
#[error("{losses} ({advice})")]
Refused { losses: String, advice: &'static str },
}
pub type Result<T> = std::result::Result<T, EngineError>;
+1
View File
@@ -21,6 +21,7 @@ pub mod menus;
mod output_guard;
pub mod preview;
pub mod render_queue;
pub mod replay;
pub mod scriptui;
mod settings;
mod system;
+11 -4
View File
@@ -23,6 +23,12 @@
//! Libraries, Bridge, Media Encoder, Behance, Creative Cloud…) are intentionally absent; the
//! Essential Graphics workspace uses the Properties panel and the Essential Graphics panel, whose
//! templates use EffectCraft's own open `.ectemplate` format.
//!
//! Where EffectCraft has entries After Effects lacks, After Effects' entries keep their order and
//! the others get a fixed place. File ▸ Import: the project imports sit together where After
//! Effects has Adobe Premiere Pro Project…, in alphabetical order and named in full (Adobe After
//! Effects Project…, Adobe Premiere Pro Project…, EffectCraft Project…); the formats only
//! EffectCraft imports follow a separator at the end, in alphabetical order (#3103).
use std::sync::OnceLock;
@@ -577,14 +583,15 @@ File
Import
File... | file.import
Multiple Files... | file.importMultiple
Adobe After Effects Project... | file.importAep
Adobe Premiere Pro Project... | file.importTimeline
EffectCraft Project... | file.importProject
Vanishing Point (.vpe)... | file.importVanishingPoint
Placeholder... | file.importPlaceholder
Solid... | file.importSolid
After Effects Project... | file.importAep
Lottie... | file.importLottie
Vanishing Point (.vpe)... | file.importVanishingPoint
---
Essential Graphics Template... | essential.importTemplate
Lottie... | file.importLottie
Import Recent Footage
@dynamic:recentFootage
---
@@ -592,7 +599,7 @@ File
Export
Add to Render Queue | renderQueue.add
Adobe Premiere Pro Project... | file.exportTimeline
After Effects Project... | file.exportAep
Adobe After Effects Project... | file.exportAep
Lottie JSON... | file.exportLottie
Essential Graphics Template... | essential.exportTemplate
---
+16
View File
@@ -509,12 +509,27 @@ impl Session {
/// is not for an export to replace. For callers that write the file themselves; call it
/// before the work, not after it.
pub fn refuse_export(&self, path: &str) -> crate::Result<()> {
self.refuse_script_write(path)?;
match self.blocking(path) {
Some(source) => Err(crate::EngineError::Other(source.refusal(path, Use::Export))),
None => Ok(()),
}
}
/// `Err` when a script issued the running command ([`Session::running_for_script`]) and
/// Settings ▸ Scripting & Expressions ▸ Allow Scripts to Write Files is off. The scripting
/// wrappers check this too, but a script can run any command directly (`app.run`, `__exec`),
/// so the commands that write a path they are given check here, through
/// [`Session::refuse_export`] and [`Session::write_output`].
pub fn refuse_script_write(&self, path: &str) -> crate::Result<()> {
if self.running_for_script() && !self.prefs.scripting.allow_scripts_write_files {
return Err(crate::EngineError::Other(format!(
"Unable to write {path}: scripts can't write files unless Settings ▸ Scripting & Expressions ▸ Allow Scripts to Write Files and Access Network is on"
)));
}
Ok(())
}
/// Write `bytes` to `path` through the host's services, unless the project reads the file
/// there ([`Session::refuse_export`]). Every command that writes to a path it is given
/// writes through this; Save and Save As (the project's own file) do not.
@@ -525,6 +540,7 @@ impl Session {
/// [`Session::write_output`] against files already found ([`Session::protected_files`]),
/// for a command that writes many.
pub(crate) fn write_output_with(&self, protected: &Protected, path: &str, bytes: &[u8]) -> crate::Result<()> {
self.refuse_script_write(path)?;
if let Some(source) = protected.find(Path::new(path)) {
return Err(crate::EngineError::Other(source.refusal(path, Use::Export)));
}
+2 -1
View File
@@ -159,7 +159,8 @@ pub struct PreviewPreset {
pub pre_roll: f64,
pub post_roll: f64,
pub play_from: PlayFrom,
/// Frames per second; `None` = Auto (the comp's rate).
/// Composition frames played per second; `None` = Auto (the comp's rate).
/// An override changes preview speed, including the audio's speed and pitch.
pub frame_rate: Option<f64>,
/// Frames skipped between the frames played (0 = every frame).
pub skip: u32,
+748
View File
@@ -0,0 +1,748 @@
//! Replay scripts: a bug or crash as a list of commands an agent (or a test) can run again.
//!
//! The session's command journal ([`Session::journal`]) becomes a JSON-lines script
//! (`journal.export`, `help.exportRepro`, and automatically when the last-resort guard catches a
//! panic, see [`after_command`]); `effectcraft-cli replay script.jsonl` runs one headless and
//! `crates/host/tests/it/repros.rs` runs every `tests/repros/*.jsonl` as a regression test.
//!
//! Format (one JSON object per line; blank lines and lines starting with `//` or `#` are
//! comments):
//!
//! ```text
//! {"start": "new" | "demo" | {"project": "path.ecproj"}, "allow_errors"?: bool} optional, first
//! {"command": "comp.new", "params": {...}, "expect_error"?: true | "substring"}
//! {"expect": {"query": "prop.get", "params": {...}, "pointer"?: "/value", "equals"?: ...}}
//! {"expect_no_error": true}
//! ```
//!
//! A command must succeed unless it says `expect_error` (then it must fail, with that text in
//! the message when it is a string), or the start line says `allow_errors` (errors are reported
//! but tolerated; crash repros say so, since after the fix the crashing step may fail cleanly).
//! A panic always fails. `expect` runs a command (normally a query) and compares the value at
//! the JSON `pointer` of its result with `equals` (numbers within 1e-6); `expect_no_error`
//! checks that the previous command succeeded. Other keys (`version`, `app`, `note`, `crash`…)
//! are information for people. Relative `project` paths are relative to the script.
//!
//! Privacy: a repro holds command ids and their parameters only, never project or media file
//! contents. Parameters can include file paths, layer names and text typed into text layers.
use std::cell::Cell;
use std::path::{Path, PathBuf};
use serde::Serialize;
use serde_json::{Map, Value, json};
use crate::{EngineError, Result, Session, guard};
/// The format version written into exported headers.
pub const VERSION: u64 = 1;
/// Largest script [`run`] accepts (bytes).
pub const MAX_SCRIPT_BYTES: usize = 64 << 20;
/// Most lines [`run`] reads.
pub const MAX_LINES: usize = 100_000;
/// Exported scripts stay below this size: the oldest steps are dropped (and counted).
const MAX_EXPORT_BYTES: usize = 32 << 20;
/// Folder of saved repros in the settings store (next to `Logs`).
pub const REPRO_DIR: &str = "Repros";
/// Automatic crash repros kept (the oldest are removed).
const KEEP_CRASH_REPROS: usize = 20;
/// Commands that replace the whole project: an export starts at the last one.
const PROJECT_STARTS: &[&str] = &["file.newProject", "file.open", "file.openDemoProject"];
/// Error and value texts in reports are cut to this many characters.
const MAX_TEXT: usize = 600;
// ---------- nesting and crash capture (Session::execute) ----------
thread_local! {
/// Commands running on this thread: (all, journaled ones).
static DEPTH: Cell<(u32, u32)> = const { Cell::new((0, 0)) };
/// A replay is running on this thread (its panics are reported, not saved as repros).
static REPLAYING: Cell<bool> = const { Cell::new(false) };
}
/// One running command (see [`Session::execute`]): commands it runs itself (a script's steps)
/// aren't journaled again, and only the outermost command saves a crash repro.
pub struct Nesting {
outer: (u32, u32),
}
impl Nesting {
/// Record nesting so commands dispatched by a script are not exported twice.
pub fn enter(journaled: bool) -> Nesting {
let outer = DEPTH.with(Cell::get);
DEPTH.with(|d| d.set((outer.0.saturating_add(1), outer.1.saturating_add(u32::from(journaled)))));
Nesting { outer }
}
/// Run by a journaled command (which replays it).
pub fn inside_journaled(&self) -> bool {
self.outer.1 > 0
}
/// Whether this command is responsible for capturing a nested panic.
pub fn outermost(&self) -> bool {
self.outer.0 == 0
}
}
impl Drop for Nesting {
fn drop(&mut self) {
DEPTH.with(|d| d.set(self.outer));
}
}
/// After the outermost command: when the guard caught a panic while it ran (`caught` is
/// [`guard::caught`] from before), save the journal plus this command as a crash repro in the
/// settings store and name it in the error.
pub fn after_command(s: &Session, id: &str, params: &Value, nesting: &Nesting, caught: u64, r: Result<Value>) -> Result<Value> {
if !nesting.outermost() || guard::caught() == caught || REPLAYING.with(Cell::get) {
return r;
}
let error = match &r {
Err(e) => e.to_string(),
Ok(_) => "a step it ran failed with an internal error".to_string(),
};
let saved = save_crash_repro(s, id, params, &error);
match (r, saved) {
(Err(EngineError::Other(m)), Some(path)) => Err(EngineError::Other(format!("{m} (the steps that led here were saved to {path})"))),
(r, _) => r,
}
}
/// Wall-clock seconds used only to label exported repros.
fn unix_secs() -> u64 {
web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
}
/// `Repros/<prefix>-<seconds>-<command>.jsonl`.
fn repro_name(prefix: &str, id: &str) -> String {
let id: String = id.chars().map(|c| if c.is_ascii_alphanumeric() || c == '.' { c } else { '_' }).take(60).collect();
format!("{REPRO_DIR}/{prefix}-{:012}-{id}.jsonl", unix_secs())
}
/// Where a store entry is on disk (or its name in stores without a folder).
fn store_path(store: &dyn crate::config::ConfigStore, name: &str) -> String {
store.dir().map(|d| d.join(name).display().to_string()).unwrap_or_else(|| name.to_string())
}
/// Persist the failing command with its preceding journal and prune older crash repros.
fn save_crash_repro(s: &Session, id: &str, params: &Value, error: &str) -> Option<String> {
let store = s.config.clone()?;
let mut text = export(s, Some(json!({"command": id, "error": cut(error)})));
text.push_str(&command_line(id, params));
text.push('\n');
let name = repro_name("crash", id);
if let Err(e) = store.write(&name, &text) {
log::error!("cannot save the crash repro {name}: {e}");
return None;
}
// Keep the newest few (names sort by time).
let mut old: Vec<String> = store.list(REPRO_DIR).into_iter().filter(|n| n.starts_with("crash-")).collect();
old.sort();
let extra = old.len().saturating_sub(KEEP_CRASH_REPROS);
for n in old.into_iter().take(extra) {
let _ = store.remove(&format!("{REPRO_DIR}/{n}"));
}
let path = store_path(store.as_ref(), &name);
log::error!("crash repro saved to {path}");
Some(path)
}
/// Save the journal as a repro: at `path` (through the session's file services), else in the
/// settings store's `Repros` folder. Returns where it went and how many steps it holds.
pub fn save_repro(s: &Session, path: Option<&str>) -> Result<(String, usize)> {
let text = export(s, None);
let steps = text.lines().count().saturating_sub(1);
if let Some(path) = path {
s.services.write_file(path, text.as_bytes()).map_err(|e| EngineError::Other(format!("cannot write {path}: {e}")))?;
return Ok((path.to_string(), steps));
}
let store = s.config.clone().ok_or_else(|| EngineError::Other("there is no settings folder to save the repro in; pass `path`".into()))?;
let name = repro_name("repro", "session");
store.write(&name, &text).map_err(|e| EngineError::Other(format!("cannot save {name}: {e}")))?;
Ok((store_path(store.as_ref(), &name), steps))
}
// ---------- export ----------
/// Serialize one command without including an empty parameter object.
fn command_line(id: &str, params: &Value) -> String {
let mut o = Map::new();
o.insert("command".into(), json!(id));
if !(params.is_null() || params.as_object().is_some_and(Map::is_empty)) {
o.insert("params".into(), params.clone());
}
Value::Object(o).to_string()
}
/// Saving writes the user's files and doesn't change the project: left out of repros.
fn skipped_in_export(id: &str) -> bool {
id.starts_with("file.save") || id.starts_with("file.increment")
}
/// The journal as a replay script: a start line, then the steps since the project was last
/// replaced (`file.newProject` / `file.open` / `file.openDemoProject`; successful Revert records a resolved `file.open`). `crash` (the failing
/// command and its error) marks a crash repro, which tolerates errors on replay.
pub fn export(s: &Session, crash: Option<Value>) -> String {
let from = s.journal.iter().rposition(|(id, _)| PROJECT_STARTS.contains(&id.as_str())).unwrap_or(0);
let mut lines: Vec<String> =
s.journal.get(from..).unwrap_or_default().iter().filter(|(id, _)| !skipped_in_export(id)).map(|(id, p)| command_line(id, p)).collect();
let mut size: usize = lines.iter().map(|l| l.len().saturating_add(1)).sum();
let mut dropped = 0usize;
while size > MAX_EXPORT_BYTES && !lines.is_empty() {
size = size.saturating_sub(lines.remove(0).len().saturating_add(1));
dropped += 1;
}
let mut head = Map::new();
head.insert("start".into(), json!("new"));
head.insert("version".into(), json!(VERSION));
head.insert("app".into(), json!(format!("EffectCraft {}", env!("CARGO_PKG_VERSION"))));
head.insert("os".into(), json!(std::env::consts::OS));
head.insert("exported".into(), json!(unix_secs()));
if let Some(c) = crash {
head.insert("allow_errors".into(), json!(true));
head.insert("crash".into(), c);
}
if dropped > 0 {
head.insert("dropped".into(), json!(dropped));
}
head.insert("note".into(), json!("commands and parameters only (no file contents); parameters can include file paths and text"));
let mut out = Value::Object(head).to_string();
out.push('\n');
for l in lines {
out.push_str(&l);
out.push('\n');
}
out
}
// ---------- parsing ----------
#[derive(Clone, Debug, PartialEq)]
enum Start {
New,
Demo,
Project(String),
}
#[derive(Clone, Debug)]
enum Step {
Start(Start),
Command {
id: String,
params: Value,
expect_error: Option<Option<String>>,
},
Expect {
query: String,
params: Value,
pointer: Option<String>,
equals: Option<Value>,
},
ExpectNoError,
/// A line that isn't a valid step (reported as a failure).
Invalid(String),
}
fn params_of(v: Option<&Value>) -> std::result::Result<Value, String> {
match v {
None | Some(Value::Null) => Ok(json!({})),
Some(p @ Value::Object(_)) => Ok(p.clone()),
Some(_) => Err("`params` must be an object".into()),
}
}
/// Decode the supported initial project states.
fn parse_start(v: &Value) -> std::result::Result<Start, String> {
match v {
Value::String(s) if s == "new" => Ok(Start::New),
Value::String(s) if s == "demo" => Ok(Start::Demo),
Value::Object(o) => match o.get("project").and_then(Value::as_str) {
Some(p) if !p.trim().is_empty() => Ok(Start::Project(p.to_string())),
_ => Err("`start` object needs a `project` path".into()),
},
_ => Err("`start` must be \"new\", \"demo\" or {\"project\": path}".into()),
}
}
/// One line → a step (`None`: a blank or comment line).
fn parse_line(text: &str, first: bool, allow_errors: &mut bool) -> Option<Step> {
let t = text.trim();
if t.is_empty() || t.starts_with("//") || t.starts_with('#') {
return None;
}
let v: Value = match serde_json::from_str(t) {
Ok(v) => v,
Err(e) => return Some(Step::Invalid(format!("not JSON: {e}"))),
};
let Some(o) = v.as_object() else { return Some(Step::Invalid("a line must be a JSON object".into())) };
let step = if let Some(st) = o.get("start") {
if !first {
return Some(Step::Invalid("the `start` line must come first".into()));
}
*allow_errors = o.get("allow_errors").and_then(Value::as_bool).unwrap_or(false);
parse_start(st).map(Step::Start)
} else if let Some(c) = o.get("command") {
parse_command(c, o)
} else if let Some(e) = o.get("expect") {
parse_expect(e)
} else if o.get("expect_no_error").and_then(Value::as_bool) == Some(true) {
Ok(Step::ExpectNoError)
} else if o.len() == 1 && o.contains_key("comment") {
return None;
} else {
Err("expected a `start`, `command`, `expect` or `expect_no_error` line".to_string())
};
Some(step.unwrap_or_else(Step::Invalid))
}
/// Parse one command and its optional expected error without executing it.
fn parse_command(c: &Value, o: &Map<String, Value>) -> std::result::Result<Step, String> {
let id = c.as_str().filter(|s| !s.is_empty()).ok_or("`command` must be a command id")?;
let expect_error = match o.get("expect_error") {
None | Some(Value::Null | Value::Bool(false)) => None,
Some(Value::Bool(true)) => Some(None),
Some(Value::String(s)) => Some(Some(s.clone())),
Some(_) => return Err("`expect_error` must be true or a text the error contains".into()),
};
Ok(Step::Command { id: id.to_string(), params: params_of(o.get("params"))?, expect_error })
}
/// Parse a query assertion, retaining the distinction between no expected value and JSON null.
fn parse_expect(e: &Value) -> std::result::Result<Step, String> {
let query = e.get("query").and_then(Value::as_str).filter(|s| !s.is_empty()).ok_or("`expect` needs a `query` command id")?;
let pointer = e.get("pointer").map(|p| p.as_str().map(str::to_string).ok_or("`pointer` must be a JSON pointer such as /value")).transpose()?;
Ok(Step::Expect { query: query.to_string(), params: params_of(e.get("params"))?, pointer, equals: e.get("equals").cloned() })
}
// ---------- running ----------
/// How to run a script.
#[derive(Clone, Debug, Default)]
pub struct Options {
/// Keep running after a failed step (default: stop at the first failure).
pub keep_going: bool,
/// Folder relative `start.project` paths are resolved against (the script's).
pub base_dir: Option<PathBuf>,
}
/// What happened to one line.
#[derive(Clone, Debug, Serialize)]
pub struct StepReport {
/// 1-based line number (0: the implicit start).
pub line: usize,
/// `start`, `command`, `expect`, `expect_no_error` or `invalid`.
pub kind: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub command: Option<String>,
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub panicked: bool,
}
/// The outcome of [`run`].
#[derive(Clone, Debug, Default, Serialize)]
pub struct Report {
pub ok: bool,
/// A step panicked (caught by the last-resort guard): always a failure.
pub panicked: bool,
pub steps: Vec<StepReport>,
#[serde(skip_serializing_if = "Option::is_none")]
pub first_failure: Option<StepReport>,
/// Steps not run after the first failure.
pub skipped: usize,
}
/// Cut a message to [`MAX_TEXT`] characters.
fn cut(s: &str) -> String {
match s.char_indices().nth(MAX_TEXT) {
Some((i, _)) => format!("{}…", s.get(..i).unwrap_or(s)),
None => s.to_string(),
}
}
/// Run one command the way the app does, noting whether it panicked.
fn exec(s: &mut Session, id: &str, params: Value) -> (Result<Value>, bool) {
let before = guard::caught();
let r = guard::guarded(&format!("replay of `{id}`"), || s.execute(id, params));
s.events.clear();
(r, guard::caught() != before)
}
/// Numbers within 1e-6 (relative above 1), everything else exactly.
fn approx_eq(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => match (x.as_f64(), y.as_f64()) {
(Some(x), Some(y)) => (x - y).abs() <= 1e-6 * x.abs().max(y.abs()).max(1.0),
_ => x == y,
},
(Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(a, b)| approx_eq(a, b)),
(Value::Object(x), Value::Object(y)) => x.len() == y.len() && x.iter().all(|(k, a)| y.get(k).is_some_and(|b| approx_eq(a, b))),
_ => a == b,
}
}
/// Marks this thread as replaying while alive.
struct Replaying(bool);
impl Replaying {
/// Suppress automatic crash exports for the duration of a replay.
fn enter() -> Replaying {
Replaying(REPLAYING.with(|r| r.replace(true)))
}
}
impl Drop for Replaying {
fn drop(&mut self) {
REPLAYING.with(|r| r.set(self.0));
}
}
/// Create or open the initial project and retain whether its command panicked.
fn start_step(s: &mut Session, start: &Start, base: Option<&Path>) -> (String, Result<Value>, bool) {
match start {
Start::New => {
let (r, p) = exec(s, "file.newProject", json!({}));
("file.newProject".into(), r, p)
}
Start::Demo => {
let (r, p) = exec(s, "file.openDemoProject", json!({}));
("file.openDemoProject".into(), r, p)
}
Start::Project(path) => {
let full = match base {
Some(b) if Path::new(path).is_relative() => b.join(path).display().to_string(),
_ => path.clone(),
};
let (r, p) = exec(s, "file.open", json!({"path": full}));
("file.open".into(), r, p)
}
}
}
/// Compare a query result and produce a bounded explanation when it fails.
fn expectation_error(r: Result<Value>, panicked: bool, pointer: Option<&str>, equals: Option<&Value>) -> Option<String> {
let value = match r {
Ok(v) => v,
Err(e) => return Some(cut(&format!("{}{e}", if panicked { "panicked: " } else { "" }))),
};
let got = match pointer {
Some(p) => value.pointer(p),
None => Some(&value),
};
match (got, equals) {
(None, _) => Some(cut(&format!("no value at {} in {value}", pointer.unwrap_or("")))),
(Some(got), Some(want)) if !approx_eq(got, want) => Some(cut(&format!("expected {want}, got {got}"))),
_ => None,
}
}
/// Execute one parsed line and remember whether the last command failed for `expect_no_error`.
fn run_step(s: &mut Session, line: usize, step: Step, opts: &Options, allow_errors: bool, last_command_failed: &mut Option<bool>) -> StepReport {
let mut sr = StepReport { line, kind: "command", command: None, ok: true, error: None, panicked: false };
match step {
Step::Invalid(m) => {
sr.kind = "invalid";
sr.ok = false;
sr.error = Some(m);
}
Step::Start(start) => {
sr.kind = "start";
let (id, r, panicked) = start_step(s, &start, opts.base_dir.as_deref());
sr.command = Some(id);
sr.panicked = panicked;
sr.ok = !panicked;
if let Err(e) = r {
sr.ok = false;
sr.error = Some(cut(&e.to_string()));
}
}
Step::Command { id, params, expect_error } => {
let (r, panicked) = exec(s, &id, params);
sr.command = Some(id);
sr.panicked = panicked;
*last_command_failed = Some(r.is_err());
let err = r.err().map(|e| e.to_string());
let (ok, error) = match (err, expect_error) {
(e, _) if panicked => (false, Some(format!("panicked: {}", e.unwrap_or_else(|| "a step failed with an internal error".into())))),
(None, None) => (true, None),
(None, Some(_)) => (false, Some("expected an error, but the command succeeded".into())),
(Some(e), Some(Some(want))) if !e.contains(want.as_str()) => (false, Some(format!("expected an error containing {want:?}, got: {e}"))),
(Some(_), Some(_)) => (true, None),
(Some(e), None) => (allow_errors, Some(e)),
};
sr.ok = ok;
sr.error = error.as_deref().map(cut);
}
Step::Expect { query, params, pointer, equals } => {
sr.kind = "expect";
let (r, panicked) = exec(s, &query, params);
sr.command = Some(query);
sr.panicked = panicked;
sr.error = expectation_error(r, panicked, pointer.as_deref(), equals.as_ref());
sr.ok = sr.error.is_none() && !panicked;
}
Step::ExpectNoError => {
sr.kind = "expect_no_error";
match last_command_failed {
Some(false) => {}
Some(true) => {
sr.ok = false;
sr.error = Some("the previous command failed".into());
}
None => {
sr.ok = false;
sr.error = Some("no command before this line".into());
}
}
}
}
sr
}
/// Run a replay script against `s`. Never panics: malformed lines are failed steps.
pub fn run(s: &mut Session, script: &str, opts: &Options) -> Report {
let _replaying = Replaying::enter();
let mut report = Report { ok: true, ..Default::default() };
let fail = |report: &mut Report, sr: StepReport| {
report.ok = false;
report.panicked |= sr.panicked;
if report.first_failure.is_none() {
report.first_failure = Some(sr.clone());
}
report.steps.push(sr);
};
if script.len() > MAX_SCRIPT_BYTES {
let msg = format!("the script is larger than {} MB", MAX_SCRIPT_BYTES >> 20);
fail(&mut report, StepReport { line: 0, kind: "invalid", command: None, ok: false, error: Some(msg), panicked: false });
return report;
}
// Hand-edited UTF-8 scripts may start with a BOM, especially on Windows.
let script = script.strip_prefix('\u{feff}').unwrap_or(script);
let mut allow_errors = false;
let mut steps: Vec<(usize, Step)> = vec![];
for (i, line) in script.lines().enumerate().take(MAX_LINES) {
if let Some(step) = parse_line(line, steps.is_empty(), &mut allow_errors) {
steps.push((i + 1, step));
}
}
if script.lines().nth(MAX_LINES).is_some() {
steps.push((MAX_LINES + 1, Step::Invalid(format!("the script has more than {MAX_LINES} lines; the rest was not read"))));
}
if !matches!(steps.first(), Some((_, Step::Start(_)) | (_, Step::Invalid(_)))) {
steps.insert(0, (0, Step::Start(Start::New)));
}
let mut last_command_failed: Option<bool> = None;
let mut stopped = false;
for (line, step) in steps {
if stopped {
report.skipped += 1;
continue;
}
let sr = run_step(s, line, step, opts, allow_errors, &mut last_command_failed);
if sr.ok {
report.steps.push(sr);
} else {
fail(&mut report, sr);
stopped = !opts.keep_going;
}
}
report
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use serde_json::json;
use super::{Options, run};
use crate::Session;
use crate::config::{ConfigStore, MemoryConfig};
fn replay(script: &str) -> super::Report {
run(&mut Session::new(), script, &Options::default())
}
#[test]
fn steps_expectations_and_failures_are_reported() {
let r = replay(
r#"{"start": "new"}
// a comment
{"command": "comp.new", "params": {"name": "A", "width": 320, "height": 180, "frameRate": 30, "duration": 2}}
{"expect_no_error": true}
{"command": "comp.new", "params": {"frameRate": 0}, "expect_error": "frame"}
{"expect": {"query": "comp.info", "pointer": "/width", "equals": 320}}
"#,
);
assert!(r.ok, "{r:?}");
assert_eq!(r.steps.len(), 5);
let r = replay(
"{\"command\": \"comp.new\"}\n{\"expect\": {\"query\": \"comp.info\", \"pointer\": \"/width\", \"equals\": 1}}\n{\"command\": \"comp.new\"}",
);
assert!(!r.ok && !r.panicked && r.skipped == 1, "{r:?}");
assert_eq!(r.first_failure.as_ref().map(|f| f.line), Some(2));
// An unexpected success, an unknown command.
assert!(!replay(r#"{"command": "comp.new", "expect_error": true}"#).ok);
assert!(!replay(r#"{"command": "no.such.command"}"#).ok);
// Tolerated errors.
let r = replay("{\"start\": \"new\", \"allow_errors\": true}\n{\"command\": \"no.such.command\"}\n{\"expect_no_error\": true}");
assert!(!r.ok && r.first_failure.as_ref().map(|f| f.kind) == Some("expect_no_error"), "{r:?}");
}
/// A UTF-8 BOM belongs to the text file, not the first JSON step.
#[test]
fn utf8_bom_preserves_explicit_and_implicit_start_lines() {
for body in ["{\"start\":\"new\"}\n{\"command\":\"comp.new\"}", "{\"command\":\"comp.new\"}"] {
let script = format!("\u{feff}{body}");
let report = replay(&script);
assert!(report.ok, "{report:?}");
assert_eq!(report.steps.last().unwrap().line, body.lines().count());
}
}
#[test]
fn malformed_lines_are_failures_not_crashes() {
for bad in [
"not json",
"[1, 2]",
"{\"command\": 5}",
"{\"command\": \"comp.new\", \"params\": [1]}",
"{\"command\": \"comp.new\", \"expect_error\": 3}",
"{\"expect\": {}}",
"{\"expect\": {\"query\": \"comp.info\", \"pointer\": 4}}",
"{\"expect\": {\"query\": \"comp.info\", \"pointer\": \"/nope\"}}",
"{\"start\": 7}",
"{\"start\": {\"project\": \"\"}}",
"{\"command\": \"comp.new\"}\n{\"start\": \"new\"}",
"{\"what\": 1}",
"{\"expect_no_error\": true}",
"{\"start\": {\"project\": \"/no/such/file.ecproj\"}}",
] {
let r = replay(bad);
assert!(!r.ok && !r.panicked, "{bad}: {r:?}");
}
let r = replay(&format!("{{\"command\": \"{}\"}}", "\u{1F600}".repeat(1000)));
assert!(!r.ok && r.first_failure.and_then(|f| f.error).is_some_and(|e| e.ends_with('…')));
assert!(replay("").ok, "an empty script is a new project");
}
#[test]
fn the_journal_exports_and_replays() {
let mut s = Session::new();
s.execute("comp.new", json!({"name": "Old"})).unwrap();
s.execute("file.openDemoProject", json!({})).unwrap();
s.execute("layer.newSolid", json!({"color": "#ff0000", "name": "Red"})).unwrap();
s.execute("layer.newSolid", json!({"color": "#00ff00", "name": "Green"})).unwrap();
// A query isn't journaled; neither is saving.
s.execute("comp.info", json!({})).unwrap();
let text = super::export(&s, None);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines.len(), 4, "{text}");
assert!(lines[0].contains("\"start\":\"new\"") && lines[1].contains("file.openDemoProject"), "starts at the last project open: {text}");
let mut again = Session::new();
let r = run(&mut again, &text, &Options::default());
assert!(r.ok, "{r:?}");
let layers = |s: &Session| s.active_comp().map(|c| c.layers.len());
assert_eq!(layers(&again), layers(&s));
// journal.export returns it; with `path` it writes the file.
let v = s.execute("journal.export", json!({})).unwrap();
assert_eq!(v["steps"], 3);
assert_eq!(v["script"].as_str().unwrap().lines().skip(1).collect::<Vec<_>>(), lines[1..]);
let dir = std::env::temp_dir().join(format!("ec-replay-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("j.jsonl");
s.execute("journal.export", json!({"path": path.to_str().unwrap()})).unwrap();
assert!(std::fs::read_to_string(&path).unwrap().contains("layer.newSolid"));
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(s.journal.iter().filter(|(id, _)| id.starts_with("journal.")).count(), 0);
}
#[test]
fn help_export_repro_saves_into_the_settings_store() {
let mut s = Session::new();
assert!(s.execute("help.exportRepro", json!({})).is_err(), "no settings folder and no path");
let store = Arc::new(MemoryConfig::default());
s.config = Some(store.clone());
s.execute("comp.new", json!({})).unwrap();
let v = s.execute("help.exportRepro", json!({})).unwrap();
assert_eq!(v["steps"], 1);
let saved = store.list(super::REPRO_DIR);
assert_eq!(saved.len(), 1, "{saved:?}");
assert!(store.read(v["path"].as_str().unwrap()).unwrap().contains("comp.new"));
}
#[test]
fn a_panicking_command_saves_a_crash_repro_and_replays_as_a_failure() {
let mut s = Session::new();
let store = Arc::new(MemoryConfig::default());
s.config = Some(store.clone());
s.execute("comp.new", json!({"name": "C"})).unwrap();
// `test.panic` stands in for a command with a bug (see `commands::journal`).
let e = s.execute("test.panic", json!({"why": "boom"})).unwrap_err().to_string();
assert!(e.contains("boom") && e.contains("Repros/crash-"), "{e}");
let names = store.list(super::REPRO_DIR);
assert_eq!(names.len(), 1);
let text = store.read(&format!("{}/{}", super::REPRO_DIR, names[0])).unwrap();
assert!(text.lines().next().unwrap().contains("\"crash\""), "{text}");
assert!(text.lines().last().unwrap().contains("test.panic"), "{text}");
// The replay fails on the panic (and doesn't save another repro).
let r = run(&mut Session::new(), &text, &Options::default());
assert!(!r.ok && r.panicked, "{r:?}");
assert_eq!(r.first_failure.as_ref().and_then(|f| f.command.as_deref()), Some("test.panic"));
// A failing replay inside a session with a store saves nothing more.
let mut t = Session::new();
t.config = Some(store.clone());
assert!(!run(&mut t, &text, &Options::default()).ok);
assert_eq!(store.list(super::REPRO_DIR).len(), 1);
// Only the newest crash repros are kept.
for i in 0..30 {
store.write(&format!("{}/crash-{i:012}-x.jsonl", super::REPRO_DIR), "{}").unwrap();
}
let _ = s.execute("test.panic", json!({}));
assert_eq!(store.list(super::REPRO_DIR).len(), super::KEEP_CRASH_REPROS);
}
#[test]
fn commands_run_by_a_journaled_command_are_not_journaled_twice() {
let mut s = Session::new();
s.execute("comp.new", json!({})).unwrap();
// engine.batch isn't journaled: its steps are.
s.execute("engine.batch", json!({"steps": [{"command": "layer.newNull"}, {"command": "layer.newNull"}]})).unwrap();
assert_eq!(s.journal.iter().filter(|(id, _)| id == "layer.newNull").count(), 2);
// A journaled command's own steps replay with it.
s.execute("file.runScript", json!({"steps": [{"command": "layer.newNull"}]})).unwrap();
let ids: Vec<&str> = s.journal.iter().map(|(id, _)| id.as_str()).collect();
assert_eq!(ids, ["comp.new", "layer.newNull", "layer.newNull", "file.runScript"]);
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod revert_tests {
use super::{Options, export, run};
use crate::Session;
use serde_json::json;
/// Saving is omitted, so a successful Revert must keep the file it read as an open boundary.
#[test]
fn save_as_then_revert_exports_a_replayable_open() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("saved.ecproj");
let mut s = Session::new();
s.execute("comp.new", json!({"name": "Saved"})).unwrap();
s.execute("file.saveAs", json!({"path": path})).unwrap();
s.execute("layer.newNull", json!({})).unwrap();
s.execute("file.revert", json!({})).unwrap();
s.execute("layer.newNull", json!({"name": "After Revert"})).unwrap();
let script = export(&s, None);
assert!(!script.contains("file.save") && !script.contains("file.revert"), "{script}");
assert!(script.lines().nth(1).unwrap().contains("file.open"), "{script}");
let saved = std::fs::read(&path).unwrap();
let mut again = Session::new();
let report = run(&mut again, &script, &Options::default());
assert!(report.ok, "{report:?}");
assert_eq!(again.path, s.path);
assert_eq!(again.active_comp().unwrap().layers.len(), s.active_comp().unwrap().layers.len());
assert_eq!(saved, std::fs::read(path).unwrap(), "replay must not rewrite the saved project");
}
}
+22 -14
View File
@@ -1,8 +1,11 @@
use super::{Comp, CompSelection, Event, ItemId, LayerId, Result, Session, Tick, Value, commands};
/// `t` as a current time of `comp`: a frame within its duration.
/// `t` as a current time of `comp`: a frame within its duration. The last frame is the last one
/// that starts before the end, so a final partial frame (a duration off the frame grid after a
/// frame-rate change) stays reachable; on the grid it is `duration - frame_duration`.
fn comp_frame_at(comp: &Comp, t: Tick) -> Tick {
comp.frame_rate.snap_nearest(t.min(comp.duration - comp.frame_duration()).max(Tick::ZERO))
let last = comp.frame_rate.snap(comp.duration - Tick(1)).max(Tick::ZERO);
comp.frame_rate.snap_nearest(t.max(Tick::ZERO)).min(last)
}
impl Session {
@@ -114,18 +117,23 @@ impl Session {
/// editor is open, so playback with no text editor does no text copying.
pub fn set_time(&mut self, t: Tick) {
if let Some(c) = self.state.active_comp {
let t = self.project.comp(c).map_or(t, |comp| comp_frame_at(comp, t));
let edited = commands::text_edit::active_editor_snapshot(self);
self.state.times.insert(c, t);
if self.prefs.general.sync_time_related_items {
// Direct call: this `set_time` owns the editor snapshot for both time changes,
// so a synced move does not take a second snapshot or start a second epoch.
self.sync_related_times_inner(c, t);
}
commands::text_edit::after_external_change(self, edited.as_ref());
self.set_time_of(c, t);
}
}
/// Move `comp`'s current time, as [`Session::set_time`] does for the active comp.
pub fn set_time_of(&mut self, c: ItemId, t: Tick) {
let t = self.project.comp(c).map_or(t, |comp| comp_frame_at(comp, t));
let edited = commands::text_edit::active_editor_snapshot(self);
self.state.times.insert(c, t);
if self.prefs.general.sync_time_related_items {
// Direct call: this `set_time_of` owns the editor snapshot for both time changes,
// so a synced move does not take a second snapshot or start a second epoch.
self.sync_related_times_inner(c, t);
}
commands::text_edit::after_external_change(self, edited.as_ref());
}
/// Settings ▸ General ▸ Synchronize Time of All Related Items: moving the current time of
/// `comp` moves the current time of the comps nested in it (through their precomp layers'
/// timing) and of the comps it is nested in, so every related viewer shows the same moment.
@@ -139,8 +147,8 @@ impl Session {
/// Sets the time of every comp related to `comp` through precomp layers: down into the comps
/// it nests (through each layer's timing) and up into the comps nesting it. A seen-set keeps
/// cycles and diamonds finite; each found time is clamped to its comp's last frame, snapped to
/// the nearest frame and stored in `state.times`. The caller owns the text-editor boundary.
/// cycles and diamonds finite; each found time goes through [`comp_frame_at`] (as a direct move
/// does) and is stored in `state.times`. The caller owns the text-editor boundary.
fn sync_related_times_inner(&mut self, comp: ItemId, t: Tick) {
let mut seen = std::collections::BTreeSet::from([comp]);
let mut todo = vec![(comp, t)];
@@ -170,7 +178,7 @@ impl Session {
}
for (cid, ct) in found {
if let Some(c) = self.project.comp(cid) {
let ct = c.frame_rate.snap_nearest(ct.clamp(Tick::ZERO, (c.duration - c.frame_duration()).max(Tick::ZERO)));
let ct = comp_frame_at(c, ct);
self.state.times.insert(cid, ct);
}
}
+13 -3
View File
@@ -22,10 +22,20 @@ impl Session {
}
}
// Last-resort guard: a panicking command reports an error instead of crashing the app.
let r = guard::guarded(&format!("command `{id}`"), || (spec.run)(self, &params))?;
let nesting = crate::replay::Nesting::enter(spec.journal);
let caught = guard::caught();
let r = guard::guarded(&format!("command `{id}`"), || (spec.run)(self, &params));
let r = crate::replay::after_command(self, id, &params, &nesting, caught, r)?;
self.learn_observe(id, &params);
if spec.journal {
self.journal.push((id.to_string(), params));
if spec.journal && !nesting.inside_journaled() {
// Revert and Open Recent resolve a file the parameters may not name. Store
// the successful read as an explicit open boundary while its resolved path is known.
let entry = if matches!(id, "file.revert" | "file.openRecent") {
self.path.as_ref().map(|path| ("file.open".to_string(), serde_json::json!({"path": path})))
} else {
None
};
self.journal.push(entry.unwrap_or_else(|| (id.to_string(), params)));
if self.journal.len() > 10_000 {
self.journal.drain(..1000);
}
@@ -92,6 +92,7 @@ pub fn pages() -> Vec<Page> {
s("general.showToolTips", "Show Tool Tips", B, true),
s("general.viewerWheelScrubbing", "Scrub Composition Frames with Mouse Wheel", B, true),
s("general.createLayersAtCompStart", "Create Layers at Composition Start Time", B, true),
s("general.fitNewLayersToWorkArea", "Fit New Layers to Work Area", B, true),
s("general.switchesAffectNestedComps", "Switches Affect Nested Comps", B, true),
s("general.defaultSpatialLinear", "Default Spatial Interpolation to Linear", B, true),
s("general.preserveConstantVertexCount", "Preserve Constant Vertex and Feather Point Count when Editing Masks", B, true),
@@ -170,6 +171,7 @@ pub fn pages() -> Vec<Page> {
title: "Previews",
items: vec![
s("previews.adaptiveResolutionLimit", "Adaptive Resolution Limit", Kind::Choice(ON_OFF_RES), true),
s("previews.adaptivePlayback", "Lower Resolution When Playback Can't Keep Up", B, true),
s("previews.showInternalWireframes", "Show Internal Wireframes", B, true),
s("previews.cacheFramesWhenIdle", "Cache Frames When Idle", B, true),
s("previews.fastPreviews", "Fast Previews (Draft 3D, Faster Effects)", B, true),
+10 -1
View File
@@ -36,6 +36,9 @@ page!(General {
viewer_wheel_scrubbing: bool = false,
/// Create New Layers at Composition Start Time (off: at the current time).
create_layers_at_comp_start: bool = true,
/// Fit New Layers to Work Area: new text, solid, shape, null and adjustment layers span the
/// work area instead of the whole composition (off, as in After Effects).
fit_new_layers_to_work_area: bool = false,
switches_affect_nested_comps: bool = true,
/// Default Spatial Interpolation to Linear.
default_spatial_linear: bool = false,
@@ -92,8 +95,14 @@ page!(CompositionPrefs {
page!(Previews {
/// Largest downsampling while playing back: `1/2`, `1/4`, `1/8`.
adaptive_resolution_limit: String = "1/8".into(),
/// A preview at Resolution Auto that renders slower than real time steps down a resolution
/// (½, then ¼, no further than the Adaptive Resolution Limit) for the rest of that play. Off
/// by default: it never steps back up during a play, so a looping preview kept playing at ¼
/// after its first loop, where without it the first loop caches Full and the next ones play
/// in real time at Full, as in After Effects.
adaptive_playback: bool = false,
show_internal_wireframes: bool = false,
cache_frames_when_idle: bool = false,
cache_frames_when_idle: bool = true,
fast_previews: bool = false,
/// `faster` or `moreAccurate`.
zoom_quality: String = "moreAccurate".into(),
+14
View File
@@ -4,14 +4,28 @@
//! panics leaves the project as it was. This is a safety net, not a substitute for returning
//! errors (AGENTS.md, "Never crash").
use std::cell::Cell;
use crate::{EngineError, Result};
thread_local! {
/// Panics [`guarded`] caught on this thread (see [`caught`]).
static CAUGHT: Cell<u64> = const { Cell::new(0) };
}
/// How many panics [`guarded`] has caught on this thread so far: compare two readings to learn
/// whether a call panicked (crash repros, `effectcraft-cli replay`).
pub fn caught() -> u64 {
CAUGHT.with(Cell::get)
}
/// Run `f`; a panic inside it becomes an error naming `what`. The panic itself is logged by
/// the panic hook ([`crate::logging::install_panic_hook`]).
pub fn guarded<T>(what: &str, f: impl FnOnce() -> Result<T>) -> Result<T> {
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(r) => r,
Err(payload) => {
CAUGHT.with(|c| c.set(c.get().saturating_add(1)));
let msg = payload
.downcast_ref::<&str>()
.map(|s| s.to_string())
+3 -2
View File
@@ -1,4 +1,4 @@
//! File ▸ Export ▸ After Effects Project… through the command registry.
//! File ▸ Export ▸ Adobe After Effects Project… through the command registry.
use serde_json::json;
@@ -73,11 +73,12 @@ fn bad_input_is_an_error_not_a_crash() {
assert!(empty.execute_checked("file.exportAep", json!({"path": path})).is_ok());
}
/// File ▸ Export ▸ Adobe After Effects Project… is in the menu under that label.
#[test]
fn aep_export_menu_entry_exists() {
let entries = crate::menus::entries();
let find = |cmd: &str| entries.iter().find(|(_, e)| e.command == cmd).map(|(p, e)| (p.join(" > "), e.label.clone()));
assert_eq!(find("file.exportAep"), Some(("File > Export".into(), "After Effects Project...".into())));
assert_eq!(find("file.exportAep"), Some(("File > Export".into(), "Adobe After Effects Project...".into())));
}
#[test]
+87 -2
View File
@@ -1,4 +1,4 @@
//! File ▸ Import ▸ After Effects Project… through the command registry, with the synthetic
//! File ▸ Import ▸ Adobe After Effects Project… through the command registry, with the synthetic
//! fixtures of `effectcraft-aep`.
use serde_json::json;
@@ -147,11 +147,96 @@ fn bad_input_is_an_error_not_a_crash() {
assert!(s.project.items.is_empty());
}
/// File ▸ Import: After Effects' own entries in its order, the project imports together where
/// After Effects has Adobe Premiere Pro Project… (alphabetical, named in full), then a separator
/// and the formats only EffectCraft imports, alphabetical (#3103).
#[test]
fn the_import_menu_groups_the_project_imports() {
use crate::menus::MenuNode;
let file = crate::menus::menu_bar().iter().find_map(|n| match n {
MenuNode::Submenu { label, children } if label == "File" => Some(children),
_ => None,
});
let import = file.into_iter().flatten().find_map(|n| match n {
MenuNode::Submenu { label, children } if label == "Import" => Some(children),
_ => None,
});
let labels: Vec<&str> = import
.into_iter()
.flatten()
.map(|n| match n {
MenuNode::Item(e) => e.label.as_str(),
MenuNode::Separator => "---",
MenuNode::Submenu { label, .. } => label.as_str(),
MenuNode::Dynamic { .. } => "@dynamic",
})
.collect();
assert_eq!(
labels,
[
"File...",
"Multiple Files...",
"Adobe After Effects Project...",
"Adobe Premiere Pro Project...",
"EffectCraft Project...",
"Vanishing Point (.vpe)...",
"Placeholder...",
"Solid...",
"---",
"Essential Graphics Template...",
"Lottie...",
]
);
}
/// `file.import` takes After Effects projects as After Effects' File… does, for scripts and
/// agents: one without losses imports as `file.importAep` does; one with losses is refused and
/// changes nothing, naming the menu entry that lists the losses; a file that is not one says why.
#[test]
fn file_import_takes_after_effects_projects() {
assert!(crate::cmd::is_aep_path("/a/Intro.aep") && crate::cmd::is_aep_path("/a/Intro.AEPX"));
assert!(!crate::cmd::is_aep_path("/a/Intro.ecproj") && !crate::cmd::is_aep_path("/a/aep") && !crate::cmd::is_aep_path("/a/clip.aep.mp4"));
let dir = std::env::temp_dir().join(format!("ec-file-import-aep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let message = |r: crate::Result<serde_json::Value>| match r {
Ok(v) => v["errors"].to_string(),
Err(e) => e.to_string(),
};
let intro = dir.join("Intro.aep").to_string_lossy().to_string();
let mut source = Session::default();
source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap();
source.execute("file.exportAep", json!({"path": intro, "allowLosses": true})).unwrap();
let mut s = Session::default();
let r = s.execute("file.import", json!({"paths": [intro]})).unwrap();
assert_eq!(r["projects"].as_array().map(Vec::len), Some(1), "{r}");
let folder = s.project.items.values().find(|i| i.name == "Intro.aep").map(|i| i.id);
assert!(folder.is_some(), "the project's folder");
assert!(s.project.items.values().any(|i| i.name == "Intro" && i.parent == folder), "its composition in the folder");
let lossy = concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep");
let mut s = Session::default();
let refused = message(s.execute("file.import", json!({"paths": [lossy]})));
assert!(refused.contains("synthetic.aep") && refused.contains("File ▸ Import ▸ Adobe After Effects Project… lists them"), "{refused}");
assert!(refused.contains("`file.importAep` with `allowLosses`"), "and the command that takes the consent: {refused}");
assert!(s.project.items.is_empty(), "nothing imported");
let junk = dir.join("Junk.AEPX");
std::fs::write(&junk, b"not a project").unwrap();
let mut s = Session::default();
let error = message(s.execute("file.import", json!({"paths": [junk.to_string_lossy()]})));
assert!(error.contains("Junk.AEPX"), "{error}");
assert!(s.project.items.is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
/// File ▸ Import ▸ Adobe After Effects Project… is in the menu under that label.
#[test]
fn aep_menu_entry_exists() {
let entries = crate::menus::entries();
let find = |cmd: &str| entries.iter().find(|(_, e)| e.command == cmd).map(|(p, e)| (p.join(" > "), e.label.clone()));
assert_eq!(find("file.importAep"), Some(("File > Import".into(), "After Effects Project...".into())));
assert_eq!(find("file.importAep"), Some(("File > Import".into(), "Adobe After Effects Project...".into())));
}
#[test]
@@ -0,0 +1,947 @@
{
"savedBy": "0.22.0",
"activeComp": 1,
"openComps": [
1
],
"schema": 2,
"settings": {
"bit_depth": "Bpc8",
"linearize": false,
"working_space": null,
"blend_linear": false,
"time_display": "Timecode",
"frame_start": 0,
"audio_sample_rate": 48000,
"gpu_acceleration": true,
"color_engine": "Adobe",
"hdr": "Clip",
"output_space": null,
"working_gamma": 2.2
},
"items": {
"1": {
"id": 1,
"name": "Main",
"label": "Sandstone",
"comment": "",
"parent": null,
"kind": {
"type": "Comp",
"width": 64,
"height": 48,
"pixel_aspect": 1.0,
"frame_rate": {
"num": 30,
"den": 1
},
"duration": 25401600000,
"display_start": 0,
"background": [
0.0,
0.0,
0.0
],
"work_area": [
0,
25401600000
],
"layers": [
{
"id": 3,
"name": "effectcraft-3430-subject.mp4",
"source": {
"type": "Footage",
"item": 2
},
"label": "Aqua",
"comment": "",
"start_time": 0,
"in_point": 0,
"out_point": 25401600000,
"stretch": 100.0,
"switches": {
"video": true,
"audio": true,
"solo": false,
"locked": false,
"shy": false,
"collapse": false,
"quality": "Best",
"sampling": "Bilinear",
"effects": true,
"frame_blend": "Off",
"motion_blur": false,
"adjustment": false,
"three_d": false,
"guide": false
},
"blend_mode": "Normal",
"preserve_transparency": false,
"track_matte": null,
"parent": null,
"markers": [],
"auto_orient": "Off",
"props": {
"uid": 4,
"match": "layer",
"name": "effectcraft-3430-subject.mp4",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Group",
"uid": 5,
"match": "masks",
"name": "Masks",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": []
},
{
"node": "Group",
"uid": 6,
"match": "effects",
"name": "Effects",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Group",
"uid": 26,
"match": "ec.matte.objectmatte",
"name": "Object Matte",
"kind": {
"kind": "Effect",
"effect": "ec.matte.objectmatte"
},
"enabled": true,
"children": [
{
"node": "Prop",
"uid": 27,
"match": "objectPoint",
"name": "Object Point",
"value": {
"t": "Vec2",
"v": [
32.0,
24.0
]
},
"ui": {
"kind": "Point"
},
"spatial": true,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 28,
"match": "useBox",
"name": "Use Box",
"value": {
"t": "Bool",
"v": true
},
"ui": {
"kind": "Checkbox"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 29,
"match": "boxTopLeft",
"name": "Box Top Left",
"value": {
"t": "Vec2",
"v": [
14.0,
8.0
]
},
"ui": {
"kind": "Point"
},
"spatial": true,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 30,
"match": "boxBottomRight",
"name": "Box Bottom Right",
"value": {
"t": "Vec2",
"v": [
50.0,
40.0
]
},
"ui": {
"kind": "Point"
},
"spatial": true,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Group",
"uid": 32,
"match": "objectTracking",
"name": "Tracking",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Prop",
"uid": 31,
"match": "trackObject",
"name": "Track Object",
"value": {
"t": "Bool",
"v": true
},
"ui": {
"kind": "Checkbox"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 33,
"match": "referenceFrame",
"name": "Reference Frame",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Number"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 34,
"match": "framesBefore",
"name": "Frames Before",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Slider",
"min": 0.0,
"max": 600.0,
"slider_min": 0.0,
"slider_max": 120.0,
"decimals": 0
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 35,
"match": "framesAfter",
"name": "Frames After",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Slider",
"min": 0.0,
"max": 600.0,
"slider_min": 0.0,
"slider_max": 120.0,
"decimals": 0
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
]
},
{
"node": "Group",
"uid": 37,
"match": "objectMatte",
"name": "Matte",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Prop",
"uid": 36,
"match": "feather",
"name": "Feather",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Slider",
"min": 0.0,
"max": 100.0,
"slider_min": 0.0,
"slider_max": 20.0,
"decimals": 1
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 38,
"match": "contrast",
"name": "Contrast",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Slider",
"min": 0.0,
"max": 100.0,
"slider_min": 0.0,
"slider_max": 100.0,
"decimals": 0
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 39,
"match": "shiftEdge",
"name": "Shift Edge",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Slider",
"min": -100.0,
"max": 100.0,
"slider_min": -100.0,
"slider_max": 100.0,
"decimals": 0
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
]
},
{
"node": "Prop",
"uid": 40,
"match": "invert",
"name": "Invert",
"value": {
"t": "Bool",
"v": false
},
"ui": {
"kind": "Checkbox"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 41,
"match": "view",
"name": "View",
"value": {
"t": "Enum",
"v": 0
},
"ui": {
"kind": "Popup",
"options": [
"Final Output",
"Matte"
]
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 42,
"match": "inputKey",
"name": "Input Key",
"value": {
"t": "Str",
"v": "cbefe0c85977c4aa"
},
"ui": {
"kind": "Hidden"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 43,
"match": "frozen",
"name": "Frozen",
"value": {
"t": "Str",
"v": "{\"key\":\"1a097b69366afd7f\",\"size\":[64,48],\"frames\":{\"0\":{\"a\":\"gBgAkAX/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IAAg/yAAIP8gACD/IACQBQ==\",\"b\":\"AA==\"}}}"
},
"ui": {
"kind": "Hidden"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
]
}
]
},
{
"node": "Group",
"uid": 15,
"match": "transform",
"name": "Transform",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Prop",
"uid": 7,
"match": "anchor",
"name": "Anchor Point",
"value": {
"t": "Vec3",
"v": [
32.0,
24.0,
0.0
]
},
"ui": {
"kind": "Point"
},
"spatial": true,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 2,
"static_only": false
},
{
"node": "Prop",
"uid": 8,
"match": "position",
"name": "Position",
"value": {
"t": "Vec3",
"v": [
32.0,
24.0,
0.0
]
},
"ui": {
"kind": "Point"
},
"spatial": true,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 2,
"static_only": false
},
{
"node": "Prop",
"uid": 9,
"match": "scale",
"name": "Scale",
"value": {
"t": "Vec3",
"v": [
100.0,
100.0,
100.0
]
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 2,
"static_only": false
},
{
"node": "Prop",
"uid": 10,
"match": "orientation",
"name": "Orientation",
"value": {
"t": "Vec3",
"v": [
0.0,
0.0,
0.0
]
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": true,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 11,
"match": "rotationX",
"name": "X Rotation",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": true,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 12,
"match": "rotationY",
"name": "Y Rotation",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": true,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 13,
"match": "rotation",
"name": "Rotation",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 14,
"match": "opacity",
"name": "Opacity",
"value": {
"t": "Scalar",
"v": 100.0
},
"ui": {
"kind": "Slider",
"min": 0.0,
"max": 100.0,
"slider_min": 0.0,
"slider_max": 100.0,
"decimals": 0
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
]
},
{
"node": "Group",
"uid": 16,
"match": "materialOptions",
"name": "Material Options",
"kind": {
"kind": "Plain"
},
"enabled": true,
"children": [
{
"node": "Prop",
"uid": 17,
"match": "castsShadows",
"name": "Casts Shadows",
"value": {
"t": "Enum",
"v": 0
},
"ui": {
"kind": "Popup",
"options": [
"Off",
"On",
"Only"
]
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 18,
"match": "lightTransmission",
"name": "Light Transmission",
"value": {
"t": "Scalar",
"v": 0.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 19,
"match": "acceptsShadows",
"name": "Accepts Shadows",
"value": {
"t": "Enum",
"v": 1
},
"ui": {
"kind": "Popup",
"options": [
"Off",
"On",
"Only"
]
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 20,
"match": "acceptsLights",
"name": "Accepts Lights",
"value": {
"t": "Bool",
"v": true
},
"ui": {
"kind": "Checkbox"
},
"spatial": false,
"hold_only": true,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 21,
"match": "ambient",
"name": "Ambient",
"value": {
"t": "Scalar",
"v": 100.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 22,
"match": "diffuse",
"name": "Diffuse",
"value": {
"t": "Scalar",
"v": 50.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 23,
"match": "specularIntensity",
"name": "Specular Intensity",
"value": {
"t": "Scalar",
"v": 50.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 24,
"match": "specularShininess",
"name": "Specular Shininess",
"value": {
"t": "Scalar",
"v": 5.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
{
"node": "Prop",
"uid": 25,
"match": "metal",
"name": "Metal",
"value": {
"t": "Scalar",
"v": 100.0
},
"ui": {
"kind": "Percent"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
]
}
]
}
}
],
"markers": [],
"shutter_angle": 180.0,
"shutter_phase": -90.0,
"motion_blur_samples": 16,
"motion_blur_adaptive_limit": 128,
"renderer": "Classic3D",
"hide_shy": false,
"enable_motion_blur": true,
"enable_frame_blending": true,
"draft_3d": false,
"preserve_frame_rate": false,
"preserve_resolution": false,
"poster_time": 0,
"global_light": {
"angle": {
"uid": 0,
"match": "globalLightAngle",
"name": "Global Light Angle",
"value": {
"t": "Scalar",
"v": 120.0
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
},
"altitude": {
"uid": 0,
"match": "globalLightAltitude",
"name": "Global Light Altitude",
"value": {
"t": "Scalar",
"v": 30.0
},
"ui": {
"kind": "Angle"
},
"spatial": false,
"hold_only": false,
"three_d_only": false,
"two_d_only": false,
"shown_dims": 0,
"static_only": false
}
}
}
},
"2": {
"id": 2,
"name": "effectcraft-3430-subject.mp4",
"label": "Aqua",
"comment": "",
"parent": null,
"kind": {
"type": "Footage",
"path": "/tmp/effectcraft-3430-subject.mp4",
"kind": "Video",
"width": 64,
"height": 48,
"pixel_aspect": 1.0,
"frame_rate": {
"num": 30,
"den": 1
},
"native_rate": null,
"duration": 25401600000,
"has_video": true,
"has_audio": false,
"alpha": "Ignore",
"premul_color": [
0.0,
0.0,
0.0
],
"loop_count": 1,
"codec": "H.264 (High)",
"missing": false,
"color_profile": "Rec709"
}
}
},
"next_id": 44
}
@@ -0,0 +1,8 @@
asset: object-matte-0.22.ecproj
title: One frozen Object Matte from EffectCraft 0.22.0
author: lisn0
source: original work
license: MIT OR Apache-2.0
license-file: ../../../../../LICENSE-MIT
added: 2026-11-11 by lisn0
notes: Saved by the released 0.22.0 CLI using a generated 64x48 orange rectangle on a blue background. Contains one frozen alpha plane. The source path is a generic test path, and the source video is unnecessary for the state-preservation regression test.
+29
View File
@@ -603,6 +603,35 @@ fn object_matte_background_job_cancel_and_freeze() {
assert_eq!(status(&mut s)["frozen"], true);
}
/// A queue edit preserves a frozen matte made before the footage signature gained placed_page.
#[test]
fn render_queue_keeps_frozen_object_matte_with_legacy_footage_key() {
use effectcraft_effects::roto::{FROZEN, INPUT_KEY};
use effectcraft_keyframe::Value::Str;
hold_roto_cache_test_lock();
// Saved by the released 0.22.0 CLI, independently of the compatibility helper.
let project = effectcraft_project::Project::from_json(include_str!("fixtures/object-matte-0.22.ecproj")).unwrap();
let mut s = Session::default();
s.replace_project(project, None);
let cid = effectcraft_project::ItemId(1);
let clip = LayerId(3);
let uid = 26;
s.open_comp(cid);
let params = |s: &Session| crate::roto::params_static(s.project.comp(cid).unwrap().layer(clip).unwrap().props.find_group(uid).unwrap());
let legacy = params(&s).get(INPUT_KEY).unwrap().clone();
let saved = params(&s).get(FROZEN).unwrap().clone();
let layer = s.project.comp(cid).unwrap().layer(clip).unwrap();
assert_ne!(legacy, Str(crate::roto::input_key(&s.project, cid, layer, uid)));
s.execute_checked("renderQueue.add", json!({"comp": cid.0, "format": "png", "output": "matte.png"})).unwrap();
assert_eq!(params(&s).get(INPUT_KEY), Some(&legacy));
assert_eq!(params(&s).get(FROZEN), Some(&saved));
s.execute_checked("layer.timing", json!({"layer": clip.0, "out": 0.05})).unwrap();
assert_eq!(params(&s).get(FROZEN), Some(&Str(String::new())));
}
/// Computed Object Matte segmentations go to the persistent disk cache: a second session on the
/// same folder and project gets them back without segmenting (its frames cannot even be read
/// here), and another prompt (other keys) finds nothing.
+121
View File
@@ -382,6 +382,127 @@ fn new_layers_start_at_the_current_time_when_asked() {
assert_eq!(s.active_comp().unwrap().layers[0].start_time.seconds().round(), 2.0);
}
/// Settings ▸ General ▸ Fit New Layers to Work Area: new text, solid, shape, null and adjustment
/// layers span the work area, ahead of the start-time setting; footage keeps its own length.
#[test]
fn new_layers_fit_the_work_area_when_asked() {
let mut s = session_with_comp();
s.execute("comp.workArea", json!({"start": 2, "end": 5})).unwrap();
let span = |s: &Session| {
let l = &s.active_comp().unwrap().layers[0];
let r = |t: effectcraft_time::Tick| (t.seconds() * 10.0).round() / 10.0;
(r(l.start_time), r(l.in_point), r(l.out_point))
};
s.execute("layer.newSolid", json!({})).unwrap();
assert_eq!(span(&s), (0.0, 0.0, 10.0), "off by default");
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
s.execute("prefs.set", json!({"key": "general.createLayersAtCompStart", "value": false})).unwrap();
// The tools that draw on their new layer (Type, shape tools, Pen) move the playhead into it;
// the menu commands leave it.
for (cmd, p, draws) in [
("layer.newSolid", json!({}), false),
("layer.newAdjustment", json!({}), false),
("layer.newNull", json!({}), false),
("layer.newText", json!({"text": "a"}), false),
("layer.newText", json!({"text": "a", "edit": true}), true),
("layer.newShape", json!({"kind": "rect"}), false),
("shape.newShape", json!({"kind": "ellipse"}), true),
("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]}), true),
] {
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
s.state.selected_layers.clear();
s.state.text_edit = None;
s.execute(cmd, p).unwrap();
assert_eq!(span(&s), (2.0, 2.0, 5.0), "{cmd}");
assert_eq!(s.time().seconds().round(), if draws { 2.0 } else { 7.0 }, "{cmd} playhead");
}
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
let nested = s.execute("comp.new", json!({"name": "Nested", "duration": 3, "open": false})).unwrap()["comp"].as_u64().unwrap();
s.execute("layer.addItem", json!({"item": nested})).unwrap();
assert_eq!(span(&s), (7.0, 7.0, 10.0), "footage and comps keep their own timing");
}
/// A tool command aimed at another comp moves that comp's playhead into its work area and
/// leaves the active comp's playhead where it was.
#[test]
fn fitted_layers_in_another_comp_move_that_comps_playhead() {
let mut s = session_with_comp();
let active = s.active_comp_id().unwrap();
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
let other = effectcraft_project::ItemId(s.execute("comp.new", json!({"name": "Other", "duration": 10, "open": false})).unwrap()["comp"].as_u64().unwrap());
s.execute("comp.workArea", json!({"comp": other.0, "start": 2, "end": 5})).unwrap();
s.execute("comp.workArea", json!({"start": 8, "end": 9})).unwrap();
s.set_time(effectcraft_time::Tick::from_seconds_f64(7.0));
for (cmd, p) in [
("layer.newText", json!({"comp": other.0, "text": "a", "edit": true})),
("shape.newShape", json!({"comp": other.0, "kind": "ellipse"})),
("shape.newPath", json!({"comp": other.0, "vertices": [[0, 0], [10, 0], [10, 10]]})),
] {
s.set_time_of(other, effectcraft_time::Tick::from_seconds_f64(7.0));
s.state.selected_layers.clear();
s.state.text_edit = None;
s.execute(cmd, p).unwrap();
assert_eq!(s.time_of(other).seconds().round(), 2.0, "{cmd}: the target comp's playhead");
assert_eq!(s.time_of(active).seconds().round(), 7.0, "{cmd}: the active comp's playhead");
}
}
/// A work area off the frame grid (set at 25 fps, then the comp went to 30 fps): the playhead
/// goes to the first frame inside it, not the nearest frame before its start.
#[test]
fn fitted_layers_off_the_frame_grid_move_the_playhead_inside_them() {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Grid", "duration": 2, "frameRate": 25})).unwrap();
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
s.execute("comp.workArea", json!({"start": 0.04, "end": 0.08})).unwrap();
s.execute("comp.settings", json!({"frameRate": 30})).unwrap();
for (cmd, p) in [("layer.newText", json!({"text": "a", "edit": true})), ("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]}))] {
s.set_time(effectcraft_time::Tick::from_seconds_f64(1.0));
s.state.selected_layers.clear();
s.state.text_edit = None;
s.execute(cmd, p).unwrap();
let l = &s.active_comp().unwrap().layers[0];
let t = s.time();
assert!(l.in_point <= t && t < l.out_point, "{cmd}: {t:?} in {:?}..{:?}", l.in_point, l.out_point);
assert_eq!(t, effectcraft_time::Tick::from_seconds_f64(2.0 / 30.0), "{cmd}: the first frame inside");
}
}
/// A duration off the frame grid (2.01 s set at 100 fps, then 24 fps) keeps its final partial
/// frame reachable: a layer fitted to a work area inside it gets the playhead on that frame.
#[test]
fn fitted_layers_in_the_final_partial_frame_get_the_playhead() {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Tail", "duration": 2.01, "frameRate": 100})).unwrap();
s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
s.execute("comp.workArea", json!({"start": 2.0, "end": 2.01})).unwrap();
s.execute("comp.settings", json!({"frameRate": 24})).unwrap();
s.set_time(effectcraft_time::Tick::ZERO);
s.execute("shape.newPath", json!({"vertices": [[0, 0], [10, 0], [10, 10]]})).unwrap();
let l = &s.active_comp().unwrap().layers[0];
let t = s.time();
assert!(l.in_point <= t && t < l.out_point, "{t:?} in {:?}..{:?}", l.in_point, l.out_point);
assert_eq!(t, effectcraft_time::Tick::from_seconds_f64(2.0), "frame 48 of 24 fps");
// The playhead can't go past that last frame.
s.set_time(effectcraft_time::Tick::from_seconds_f64(5.0));
assert_eq!(s.time(), effectcraft_time::Tick::from_seconds_f64(2.0));
}
/// Synchronize Time of All Related Items reaches a nested comp's final partial frame as a direct
/// move does: the inner comp is 2.01 s (set at 100 fps, then 24 fps), the outer sits at 2.0 s.
#[test]
fn synced_time_reaches_a_nested_final_partial_frame() {
let mut s = Session::default();
let inner = s.execute("comp.new", json!({"name": "Inner", "duration": 2.01, "frameRate": 100, "open": false})).unwrap()["comp"].as_u64().unwrap();
s.execute("comp.settings", json!({"comp": inner, "frameRate": 24})).unwrap();
s.execute("comp.new", json!({"name": "Outer", "duration": 4, "frameRate": 24})).unwrap();
s.execute("layer.addItem", json!({"item": inner, "time": 0})).unwrap();
s.set_time(effectcraft_time::Tick::from_seconds_f64(2.0));
assert_eq!(s.time_of(effectcraft_project::ItemId(inner)), effectcraft_time::Tick::from_seconds_f64(2.0));
}
#[test]
fn default_spatial_interpolation_linear() {
let mut s = session_with_comp();
+25
View File
@@ -649,6 +649,31 @@ fn time_set_keeps_the_frame_shown_in_the_current_time_field() {
assert_eq!(r["frame"], 13);
}
/// A comp that starts before 0 shows its first frames as negative timecode (`-0:00:00:12`).
/// Typing back what the current-time display shows (Go to Time opens with it) stays on that frame;
/// the leading `-` used to move back from the current frame instead. A bare `-n` still steps back,
/// and in a comp that starts at 0 a `-` stays relative.
#[test]
fn time_set_round_trips_negative_displayed_timecode() {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Pre-roll", "width": 64, "height": 64, "frameRate": 24, "duration": 4, "startTime": -1})).unwrap();
for frame in [0_i64, 1, 12, 23, 24, 25, 60] {
let r = s.execute("time.set", json!({"frame": frame})).unwrap();
let display = r["display"].as_str().unwrap().to_string();
let r = s.execute("time.set", json!({"timecode": display})).unwrap();
assert_eq!(r["frame"], frame, "displayed {display}");
}
s.execute("time.set", json!({"frame": 12})).unwrap();
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:12"})).unwrap()["display"], "-0:00:00:12");
assert_eq!(s.execute("time.set", json!({"timecode": "-5"})).unwrap()["frame"], 7, "a bare -n steps back");
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:20"})).unwrap()["frame"], 4);
assert!(s.execute("time.set", json!({"timecode": "- -1:00"})).is_err(), "a second sign is still an error");
s.execute("comp.settings", json!({"startTime": 0})).unwrap();
s.execute("time.set", json!({"frame": 12})).unwrap();
assert_eq!(s.execute("time.set", json!({"timecode": "-0:00:00:02"})).unwrap()["frame"], 10, "relative when the comp starts at 0");
}
/// Frames display style: typed digits are a frame count, so 00100 is frame 100, not 1 s (#530).
#[test]
fn time_set_reads_frames_style_digits_as_a_frame_count() {
+5
View File
@@ -122,6 +122,11 @@ pub struct JobOptions<'a> {
/// Settings ▸ Memory & CPU ▸ GPU Frames Rendered at Once: 1 to 3, or `None` for Auto (from
/// the device memory a frame measured, #3335). `EFFECTCRAFT_GPU_INFLIGHT` overrides it.
pub gpu_frames: Option<usize>,
/// Reads the bytes of memory the system can hand out now (`None`: unknown), for Auto's CPU
/// frames in flight (#1615): read at most once per job, when a job renders on the CPU after
/// a GPU frame measured a frame's working memory. `None`: CPU frames are capped only by
/// `EFFECTCRAFT_CPU_INFLIGHT`.
pub free_memory: Option<&'a (dyn Fn() -> Option<u64> + Sync)>,
}
/// The layer caches of a Render Queue run (see [`JobOptions::caches`]): one per compositor,
+129 -2
View File
@@ -137,6 +137,17 @@ pub(crate) fn cpu_inflight(var: Option<&str>) -> Option<usize> {
}
}
/// Auto's CPU frames in flight (#1615): as many frames as half of the `free` bytes the system
/// can hand out hold at `frame` bytes each, 1 to `threads`. `frame` is a GPU frame's measured
/// working memory: both compositors keep 16-byte RGBA pixels (`[f32; 4]`, `Rgba32Float`), and
/// the other half is headroom for the layer caches, decoded footage, the encoder and the
/// estimate being low. `None` (not capped) when either is unknown or `frame` is 0.
pub(crate) fn auto_cpu_frames(frame: Option<u64>, free: Option<u64>, threads: usize) -> Option<usize> {
let (frame, free) = (frame.filter(|&f| f > 0)?, free?);
let n = free / 2 / frame;
Some(usize::try_from(n).unwrap_or(usize::MAX).clamp(1, threads.max(1)))
}
/// The frames-in-flight caps of a batch: `gpu` (#879) for the GPU's memory, `cpu` (#1615) for
/// the process's.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -253,6 +264,9 @@ pub(crate) struct Cx<'a> {
gpu_frames: Mutex<Option<(usize, String)>>,
/// A frame has rendered on the GPU compositor: its memory can be measured.
gpu_rendered: std::sync::atomic::AtomicBool,
/// Auto's CPU frames in flight once settled ([`Cx::auto_cpu_frames`]; `Some(None)`: not
/// capped).
cpu_frames: Mutex<Option<Option<usize>>>,
/// (output frame, seconds) of every rendered frame (Plus Per Frame Info logs).
pub frame_times: Option<Mutex<Vec<(u64, f64)>>>,
/// Files written to an overflow folder.
@@ -337,6 +351,7 @@ impl<'a> Cx<'a> {
choice: Mutex::new(None),
gpu_frames: Mutex::new(None),
gpu_rendered: Default::default(),
cpu_frames: Mutex::new(None),
frame_times,
overflowed: Mutex::new(vec![]),
written: Mutex::new(vec![]),
@@ -555,9 +570,11 @@ impl<'a> Cx<'a> {
let timed = |on: crate::JobRenderer, frames: &[u64]| {
let warm = render(first, on);
// The GPU's frames in flight settle with its warm-up frame (Auto measures it).
// The CPU's frames are held to what the job would render at once on it, from the
// GPU frames just measured (#1615): uncapped, they could exhaust RAM here already.
let caps = match on {
crate::JobRenderer::Gpu => Inflight { gpu: Some(self.gpu_inflight()), cpu: cpu_cap },
crate::JobRenderer::Cpu => Inflight { gpu: None, cpu: cpu_cap },
crate::JobRenderer::Cpu => Inflight { gpu: None, cpu: cpu_cap.or_else(|| self.auto_cpu_frames()) },
};
let t0 = Instant::now();
let gpu_live = || on == crate::JobRenderer::Cpu || self.job.accel.is_some_and(|a| a.available());
@@ -660,7 +677,48 @@ impl<'a> Cx<'a> {
/// The GPU and CPU frames-in-flight caps every frame loop shares (#1615).
fn inflight(&self) -> Inflight {
Inflight { gpu: self.gpu_cap(), cpu: cpu_inflight(std::env::var(CPU_INFLIGHT_ENV).ok().as_deref()) }
Inflight { gpu: self.gpu_cap(), cpu: self.cpu_cap() }
}
/// CPU frames in flight (#1615): `EFFECTCRAFT_CPU_INFLIGHT`, else Auto's
/// ([`Cx::auto_cpu_frames`]) while the job renders on the CPU (it chose the CPU's
/// compositor, or the GPU stopped for good). `None`: not capped.
fn cpu_cap(&self) -> Option<usize> {
if let Some(n) = cpu_inflight(std::env::var(CPU_INFLIGHT_ENV).ok().as_deref()) {
return Some(n);
}
let on_cpu = self.backend() == crate::JobRenderer::Cpu || self.job.accel.is_some_and(|a| !a.available());
if on_cpu { self.auto_cpu_frames() } else { None }
}
/// Auto's CPU frames in flight ([`auto_cpu_frames`]), settled once per job from the working
/// memory a GPU frame measured and the memory the system has free
/// ([`crate::JobOptions::free_memory`]). `None` (not capped, as before #1615) until a GPU
/// frame has rendered, and in a job that never renders one (a Software Rendering project):
/// there is no per-frame estimate then.
fn auto_cpu_frames(&self) -> Option<usize> {
if let Some(n) = *self.cpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner) {
return n;
}
if !self.gpu_rendered.load(std::sync::atomic::Ordering::Relaxed) {
return None;
}
// Read outside the lock: the allocator report walks every allocation, and reading the
// system's memory can take a second (PowerShell on Windows).
let frame = self.job.accel.and_then(|a| a.frame_memory()).map(|m| m.frame);
let free = self.job.options.free_memory.and_then(|read| read());
let n = auto_cpu_frames(frame, free, rayon::current_num_threads());
let mut slot = self.cpu_frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
*slot.get_or_insert_with(|| {
let mib = |b: Option<u64>| b.map_or_else(|| "unknown".to_string(), |b| format!("{} MiB", b >> 20));
let (frame, free) = (mib(frame), mib(free));
let label = &self.job.options.label;
match n {
Some(n) => log::info!(target: "effectcraft_export", "{label}: {n} CPU frame(s) at once (Auto: {frame} per frame on the GPU, {free} free)"),
None => log::info!(target: "effectcraft_export", "{label}: CPU frames not capped (Auto: {frame} per frame on the GPU, {free} free)"),
}
n
})
}
/// Output frame `i`: sampled (fields, pulldown), at the colour depth, cropped and resized.
@@ -1285,6 +1343,75 @@ mod tests {
assert_eq!(describe_choice(&cx.choice().unwrap()), "Software Rendering (measured on the first frames: GPU 117.0 ms/frame, CPU 68.0 ms/frame)");
}
#[test]
fn auto_cpu_frames_fit_half_the_free_memory() {
// #1615: 16 threads at 1378 MiB a frame (a GTX 1050 Ti user's comp) with 10 GiB free
// render 3 frames at once, not 16.
assert_eq!(auto_cpu_frames(Some(1378 << 20), Some(10 << 30), 16), Some(3));
assert_eq!(auto_cpu_frames(Some(1 << 28), Some(64 << 30), 16), Some(16), "at most one per thread");
assert_eq!(auto_cpu_frames(Some(4 << 30), Some(2 << 30), 16), Some(1), "at least one");
assert_eq!(auto_cpu_frames(Some(1 << 30), Some(0), 8), Some(1));
assert_eq!(auto_cpu_frames(Some(1 << 20), Some(u64::MAX), 0), Some(1));
// Unknown: not capped, as before.
assert_eq!(auto_cpu_frames(None, Some(8 << 30), 16), None);
assert_eq!(auto_cpu_frames(Some(1 << 30), None, 16), None);
assert_eq!(auto_cpu_frames(Some(0), Some(8 << 30), 16), None);
}
#[test]
fn a_job_that_chose_the_cpu_after_measuring_the_gpu_caps_its_frames() {
// #1615: a GPU project that measured the CPU faster rendered one frame per thread at
// once, so a heavy comp ran out of RAM (an exit without an error, 0.23.1 on Windows).
// Its frames now fit half the free memory at the GPU's measured size per frame.
use effectcraft_project::render_queue::{OutputFormat, OutputModule, RenderSettings};
if std::env::var_os(CPU_INFLIGHT_ENV).is_some() || std::env::var_os(RENDERER_ENV).is_some() {
return;
}
let (settings, output) = (RenderSettings::default(), OutputModule::for_format(OutputFormat::PngSequence));
let mut p = Project::default();
p.settings.gpu_acceleration = true;
let comp = Comp::new(16, 8, effectcraft_time::FrameRate::FPS_25, Tick::from_seconds_f64(1.0));
let cid = p.add_item("Comp", effectcraft_color::Label::Sandstone, None, effectcraft_project::ItemKind::Comp(comp.into()));
let accel = MemAccel(effectcraft_render::FrameMemory { budget: 3 << 30, peak: 2 << 30, frame: 1 << 30, reserve: 0 });
let reads = AtomicUsize::new(0);
let free = || {
reads.fetch_add(1, Ordering::SeqCst);
Some(6u64 << 30)
};
let job = Job {
project: &p,
footage: &effectcraft_render::NoFootage,
expr: None,
accel: Some(&accel),
comp: cid,
settings: &settings,
output: &output,
path: "/c/f_[#####].png",
sink: None,
nested_switches: true,
options: crate::JobOptions { free_memory: Some(&free), ..Default::default() },
};
let pool = rayon::ThreadPoolBuilder::new().num_threads(16).build().unwrap();
pool.install(|| {
let cx = Cx::new(&job);
assert_eq!(cx.inflight().cpu, None, "nothing measured yet");
let c = cx.comp().unwrap();
cx.frame_on(c, 0, crate::JobRenderer::Gpu);
assert_eq!(cx.inflight().cpu, None, "the GPU renders: its own cap holds");
assert_eq!(reads.load(Ordering::SeqCst), 0, "the free memory is read only for CPU frames");
cx.decide(Choice { renderer: crate::JobRenderer::Cpu, gpu_ms: Some(1417.3), cpu_ms: Some(655.8), why: "measured on the first frames" });
assert_eq!(cx.inflight(), Inflight { gpu: None, cpu: Some(3) }, "3 GiB of 6 GiB free at 1 GiB a frame");
assert_eq!(cx.inflight().cpu, Some(3));
assert_eq!(reads.load(Ordering::SeqCst), 1, "settled once per job");
});
// Without a free-memory reading the job renders uncapped, as before.
let job = Job { options: Default::default(), ..job };
let cx = Cx::new(&job);
cx.frame_on(cx.comp().unwrap(), 0, crate::JobRenderer::Gpu);
cx.decide(Choice { renderer: crate::JobRenderer::Cpu, gpu_ms: Some(2.0), cpu_ms: Some(1.0), why: "measured on the first frames" });
assert_eq!(cx.inflight().cpu, None);
}
#[test]
fn cpu_inflight_is_off_unless_a_positive_number() {
assert_eq!(cpu_inflight(None), None);
+5 -3
View File
@@ -244,12 +244,14 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
let pad = (fs - pcm_left.len() % fs) % fs + fs;
pcm_left.extend(std::iter::repeat_n(0.0, pad));
}
while pcm_left.len() >= fs {
let pkt = enc.encode_float(&pcm_left[..fs]);
pcm_left.drain(..fs);
// Packets are read in place and the encoded samples dropped once: draining each packet
// from the front would shift the rest of the batch's audio every 20 ms.
for packet in pcm_left.chunks_exact(fs) {
let pkt = enc.encode_float(packet);
frames.push(Frame { ms: packets as i64 * 20, track: 2, key: true, prev_ms: None, data: pkt, alpha: None });
packets += 1;
}
pcm_left.drain(..pcm_left.len() / fs * fs);
};
let batch = batch_size();
+5 -3
View File
@@ -125,12 +125,14 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
let pad = (fs - pcm_left.len() % fs) % fs + fs;
pcm_left.extend(std::iter::repeat_n(0.0, pad));
}
while pcm_left.len() >= fs {
let pkt = enc.encode_float(&pcm_left[..fs]);
pcm_left.drain(..fs);
// Packets are read in place and the encoded samples dropped once: draining each packet
// from the front would shift the rest of the batch's audio every 20 ms.
for packet in pcm_left.chunks_exact(fs) {
let pkt = enc.encode_float(packet);
blocks.push(Block { ms: packets as i64 * 20, track: 2, key: true, data: pkt });
packets += 1;
}
pcm_left.drain(..pcm_left.len() / fs * fs);
};
let batch = batch_size();
+4 -4
View File
@@ -234,7 +234,7 @@ function __cubicBezier(curve) {
x1 = curve[0]; y1 = curve[1]; x2 = curve[2]; y2 = curve[3];
}
if (typeof x1 !== 'number' || typeof y1 !== 'number' || typeof x2 !== 'number' || typeof y2 !== 'number' ||
!isFinite(x1) || !isFinite(y1) || !isFinite(x2) || !isFinite(y2)) {
!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2)) {
return __lin;
}
x1 = Math.max(0, Math.min(1, x1));
@@ -418,8 +418,8 @@ function hslToRgb(c) {
function hexToRgb(hex) {
var s = String(hex).replace(/^#|^0x/i, '');
if (s.length === 3) s = s[0] + s[0] + s[1] + s[1] + s[2] + s[2];
var n = parseInt(s.slice(0, 6), 16);
var a = s.length >= 8 ? parseInt(s.slice(6, 8), 16) / 255 : 1;
var n = Number.parseInt(s.slice(0, 6), 16);
var a = s.length >= 8 ? Number.parseInt(s.slice(6, 8), 16) / 255 : 1;
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255, a];
}
@@ -502,7 +502,7 @@ function __child(c, l, group, key) {
// it (default 1), the layer at the top (#1619).
function __ancestor(c, l, path, countUp) {
var n = countUp === undefined ? 1 : __v(countUp);
if (typeof n !== 'number' || !(n >= 1) || !isFinite(n)) __err('propertyGroup(' + countUp + '): the count must be a number of 1 or more');
if (typeof n !== 'number' || !(n >= 1) || !Number.isFinite(n)) __err('propertyGroup(' + countUp + '): the count must be a number of 1 or more');
n = Math.floor(n);
var info = __h(__ANCESTOR, c, l, path, n);
if (info === 'layer') return __layer(c, l);
+102
View File
@@ -0,0 +1,102 @@
//! Per-compositor, fixed-category observations from error scopes. No driver strings escape.
#[cfg(any(test, not(target_arch = "wasm32")))]
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, OnceLock};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScopedError {
OutOfMemory,
Internal,
Validation,
}
pub type ScopedErrorObserver = Arc<dyn Fn(ScopedError) + Send + Sync>;
#[derive(Default)]
pub(crate) struct ScopedObservations {
observer: OnceLock<ScopedErrorObserver>,
#[cfg(any(test, not(target_arch = "wasm32")))]
seen: AtomicU8,
}
impl ScopedObservations {
/// Register before rendering. Shared by all clones of the compositor, never global.
pub(crate) fn install(&self, observer: ScopedErrorObserver) -> bool {
self.observer.set(observer).is_ok()
}
#[cfg(any(test, not(target_arch = "wasm32")))]
pub(crate) fn report_scopes(&self, oom: bool, internal: bool, validation: bool) {
for (present, category) in [(oom, ScopedError::OutOfMemory), (internal, ScopedError::Internal), (validation, ScopedError::Validation)] {
if present {
self.report(category);
}
}
}
#[cfg(any(test, not(target_arch = "wasm32")))]
fn report(&self, error: ScopedError) {
let Some(observer) = self.observer.get() else { return };
let bit = match error {
ScopedError::OutOfMemory => 1,
ScopedError::Internal => 2,
ScopedError::Validation => 4,
};
if self.seen.fetch_or(bit, Ordering::Relaxed) & bit != 0 {
return;
}
// Diagnostics must not turn a recoverable GPU frame error into an application panic.
#[cfg(not(target_arch = "wasm32"))]
if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| observer(error))).is_err() {
log::warn!("GPU scoped-error observer panicked");
}
#[cfg(target_arch = "wasm32")]
observer(error);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn categories_are_independent_bounded_and_not_global() {
let observations = ScopedObservations::default();
let seen = Arc::new(std::sync::Mutex::new(Vec::new()));
let copy = seen.clone();
assert!(observations.install(Arc::new(move |error| copy.lock().unwrap().push(error))));
assert!(!observations.install(Arc::new(|_| panic!("must not replace observer"))));
for _ in 0..100 {
observations.report(ScopedError::Internal);
observations.report(ScopedError::Validation);
observations.report(ScopedError::OutOfMemory);
}
ScopedObservations::default().report(ScopedError::OutOfMemory);
assert_eq!(*seen.lock().unwrap(), [ScopedError::Internal, ScopedError::Validation, ScopedError::OutOfMemory]);
}
#[test]
fn internal_and_validation_scopes_do_not_claim_out_of_memory() {
let observations = ScopedObservations::default();
let seen = Arc::new(std::sync::Mutex::new(Vec::new()));
let copy = seen.clone();
observations.install(Arc::new(move |error| copy.lock().unwrap().push(error)));
observations.report_scopes(false, false, false);
assert!(seen.lock().unwrap().is_empty());
observations.report_scopes(false, true, true);
assert_eq!(*seen.lock().unwrap(), [ScopedError::Internal, ScopedError::Validation]);
observations.report_scopes(true, true, true);
assert_eq!(*seen.lock().unwrap(), [ScopedError::Internal, ScopedError::Validation, ScopedError::OutOfMemory]);
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn observer_panic_does_not_escape_frame_recovery() {
let observations = ScopedObservations::default();
observations.install(Arc::new(|_| panic!("synthetic observer panic")));
observations.report(ScopedError::OutOfMemory);
observations.report(ScopedError::Internal);
assert_eq!(observations.seen.load(Ordering::Relaxed), 3);
}
}
+3 -1
View File
@@ -274,7 +274,9 @@ pub(crate) fn box_passes(e: &mut Enc, img: &GpuImage, rx: &[usize], ry: &[usize]
let mut cur = img.clone();
let passes = rx.iter().map(|&r| (false, r)).chain(ry.iter().map(|&r| (true, r))).filter(|p| p.1 > 0);
for (vertical, r) in passes {
let block = if vertical { (4 * r).clamp(32, 256) } else { 256 } as u32;
// Vertical sums restart every `block` rows as the CPU's do; a row is one running sum, as
// on the CPU (restarting it rounds differently).
let block = if vertical { (4 * r).clamp(32, 256) as u32 } else { cur.width.max(1) };
let (lines, n) = if vertical { (cur.width, cur.height) } else { (cur.height, cur.width) };
let mut p = Params::default();
p.u[0] = [r as u32, repeat as u32, block, 0];
+14 -1
View File
@@ -58,6 +58,7 @@ mod bokeh;
mod classic3d;
mod context;
pub mod deferred;
mod diagnostics;
mod effects;
mod fx_color;
mod fx_depth;
@@ -91,6 +92,7 @@ mod yuv;
use std::sync::Arc;
pub use context::{GpuContext, GpuImage, TransferStats};
pub use diagnostics::{ScopedError, ScopedErrorObserver};
use effectcraft_effects::Buf;
use effectcraft_project::ItemId;
use effectcraft_raster::Image;
@@ -126,6 +128,7 @@ impl std::error::Error for FrameError {}
/// The GPU compositor (cheap to clone; one device shared by all clones).
#[derive(Clone)]
pub struct Gpu {
scoped_observations: Arc<diagnostics::ScopedObservations>,
ctx: Arc<GpuContext>,
/// Backend::Auto's per-comp CPU / GPU timings.
auto: Arc<effectcraft_render::AutoPick>,
@@ -162,7 +165,7 @@ impl Gpu {
}
pub fn from_context(ctx: GpuContext) -> Gpu {
Gpu { ctx: Arc::new(ctx), auto: Default::default(), declined: Default::default() }
Gpu { ctx: Arc::new(ctx), auto: Default::default(), declined: Default::default(), scoped_observations: Default::default() }
}
/// A device of its own without blocking, with deferred readbacks (a browser worker, see
@@ -173,6 +176,13 @@ impl Gpu {
Ok(Gpu::from_context(ctx))
}
/// Install a host's fixed-category observer before rendering. It is called at most once
/// per category across all clones, outside device/renderer locks. Returns false if already
/// registered. The observer must remain brief and must not perform GPU work.
pub fn set_scoped_error_observer(&self, observer: ScopedErrorObserver) -> bool {
self.scoped_observations.install(observer)
}
/// [`Gpu::request_deferred`] blocking (tests of the browser worker's path, natively).
#[cfg(not(target_arch = "wasm32"))]
pub fn headless_deferred() -> Option<Gpu> {
@@ -316,6 +326,9 @@ impl Gpu {
let validation = pollster::block_on(validation.pop());
let internal = pollster::block_on(internal.pop());
let oom = pollster::block_on(oom.pop());
// Preserve the typed scope observations before FrameError::Memory combines OOM
// and internal errors. An internal error alone is not evidence of actual OOM.
self.scoped_observations.report_scopes(oom.is_some(), internal.is_some(), validation.is_some());
let memory = oom.is_some() || internal.is_some();
// The allocation failure first: a validation error is often its consequence.
let errors: Vec<String> = [oom, internal, validation].into_iter().flatten().map(|e| e.to_string()).collect();
+1 -1
View File
@@ -270,7 +270,7 @@ fn line_get(horizontal: bool, line: i32, i: i32, n: i32, repeat: bool) -> vec4<f
}
// One box pass of radius r along rows (horizontal) or columns, as running sums over blocks of
// `block` pixels (each block restarts its sum, as the CPU's vertical pass does).
// `block` pixels (each block restarts its sum, as the CPU's vertical pass does; rows are one block).
fn box_pass(gid: vec3<u32>, horizontal: bool) {
let dims = out_dims();
let r = i32(P.u[0].x);
+29
View File
@@ -731,6 +731,35 @@ fn small_blurs_fall_back_to_the_cpu() {
}
}
/// Horizontal GPU box passes used to restart the running sum every 256 px; the CPU keeps one sum
/// per row, so images wider than 256 disagreed in f32 rounding. Width 320 forces that path.
/// Exact on llvmpipe (CI lavapipe), which fails without the fix; other adapters use the usual
/// Bpc32 tolerance because a tighter bound can fail on unmeasured hardware and does not catch
/// the regression (Metal still differs by ~1e-6 with or without the fix).
#[test]
fn box_blur_horizontal_rows_match_cpu_beyond_256() {
let Some(g) = gpu() else { return };
let img = pattern(11, 320, 48);
let up = g.ctx.upload_image(&img).expect("upload");
let tol = if on_llvmpipe() { 0.0 } else { tolerance(BitDepth::Bpc32) };
let check_case = |name: &str, want: Image, got: Image| {
assert_eq!((got.width, got.height), (want.width, want.height), "{name}: size");
let d = diff(&want, &got, tol);
assert_eq!(d.over, 0, "{name}: max {} at {:?} on {}", d.max, d.worst, g.ctx.name);
};
for (name, r) in [("box r5×3", 5usize), ("box r25×3", 25)] {
let mut e = crate::context::Enc::new(&g.ctx);
let rx = [r, r, r];
let out = crate::effects::box_passes(&mut e, &up, &rx, &rx, false);
check_case(name, effectcraft_raster::box_blur(&img, r, r, 3, false), e.download(&out).expect("download"));
}
for (name, sigma) in [("gauss σ10", 10.0f64), ("gauss σ40", 40.0)] {
let mut e = crate::context::Enc::new(&g.ctx);
let out = crate::effects::gaussian_blur(&mut e, &up, sigma, sigma, false);
check_case(name, effectcraft_raster::gaussian_blur(&img, sigma, sigma, false), e.download(&out).expect("download"));
}
}
#[test]
fn gpu_effects_match_cpu() {
// Sigma 3 px and up and even radii, so the half-scale pass stays on the GPU too (#1939).
+6
View File
@@ -65,6 +65,7 @@ impl Exporter for FileExporter {
caches: job.caches.map(|c| effectcraft_export::JobCaches { gpu: &c.gpu, cpu: &c.cpu }),
renderer: job.renderer,
gpu_frames: job.gpu_frames,
free_memory: Some(&free_memory),
},
nested_switches: job.nested_switches,
};
@@ -87,6 +88,11 @@ impl Exporter for FileExporter {
}
}
/// The memory the system can hand out now, for an export's Auto CPU frames in flight (#1615).
fn free_memory() -> Option<u64> {
effectcraft_engine::sysinfo::memory().map(|m| m.available)
}
/// A new session with media, import, expressions, scripting and export enabled.
pub fn session() -> Session {
let mut s = Session {
+1
View File
@@ -547,6 +547,7 @@ fn script_imports_footage_and_saves() {
std::fs::create_dir_all(&dir).unwrap();
let q = |n: &str| serde_json::to_string(&dir.join(n).to_string_lossy()).unwrap();
let mut s = super::session();
s.execute("prefs.set", json!({"key": "scripting.allowScriptsWriteFiles", "value": true})).unwrap();
let code = format!(
r#"
var c = app.project.items.addComp("Src", 32, 32, 1, 1, 10);
+2
View File
@@ -15,3 +15,5 @@ mod timeline_interop;
mod vector_import;
mod wav_markers;
mod webm_metadata;
mod repros;
+31
View File
@@ -0,0 +1,31 @@
//! Regression tests as data: every `tests/repros/*.jsonl` (repository root) is a replay script
//! (`effectcraft_engine::replay`, docs/agents.md "Repro scripts") run against a fully wired
//! headless session, like `effectcraft-cli replay`. A crash fix's regression test is a `.jsonl`
//! dropped there: it must replay without a panic and meet its expectations.
use std::path::{Path, PathBuf};
use effectcraft_engine::replay;
fn repros_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/repros")
}
#[test]
fn every_repro_replays_cleanly() {
let mut files: Vec<PathBuf> =
std::fs::read_dir(repros_dir()).unwrap().map(|e| e.unwrap().path()).filter(|p| p.extension().is_some_and(|x| x == "jsonl")).collect();
files.sort();
assert!(!files.is_empty(), "no repros in {}", repros_dir().display());
let mut failures = vec![];
for f in &files {
let script = std::fs::read_to_string(f).unwrap();
let mut s = effectcraft_host::session();
let opts = replay::Options { keep_going: false, base_dir: f.parent().map(Path::to_path_buf) };
let r = replay::run(&mut s, &script, &opts);
if !r.ok {
failures.push(format!("{}: {}", f.display(), serde_json::to_string(&r.first_failure).unwrap()));
}
}
assert!(failures.is_empty(), "{} of {} repros failed:\n{}", failures.len(), files.len(), failures.join("\n"));
}
+21
View File
@@ -165,6 +165,27 @@ fn gradient_tracks_sample_separately() {
assert_eq!((empty.color_at(0.3), empty.opacity_at(0.3)), ([1.0; 4], 1.0));
}
/// A gradient's stop count multiplies every painted pixel's sampling: plain JSON with more than
/// `MAX_GRADIENT_STOPS` is refused, and a longer stored list is sampled through its first ones.
#[test]
fn gradient_stop_count_is_bounded() {
use crate::value::MAX_GRADIENT_STOPS;
let g = Value::Gradient(Gradient::default());
let many: Vec<serde_json::Value> = (0..=MAX_GRADIENT_STOPS).map(|_| serde_json::json!("#ff0000")).collect();
assert_eq!(g.coerce_json(&serde_json::Value::Array(many.clone())), None);
assert!(g.coerce_json(&serde_json::Value::Array(many[..MAX_GRADIENT_STOPS].to_vec())).is_some());
let ops: Vec<serde_json::Value> = (0..=MAX_GRADIENT_STOPS).map(|i| serde_json::json!([i as f64 / MAX_GRADIENT_STOPS as f64, 1.0])).collect();
assert_eq!(g.coerce_json(&serde_json::json!({"colors": ["#000000", "#ffffff"], "opacities": ops})), None);
// The tagged (typed) form is bounded the same way.
let tagged = Value::Gradient(Gradient { colors: (0..=MAX_GRADIENT_STOPS).map(|i| (i as f64, [0.0; 4])).collect(), opacities: vec![(0.0, 1.0)] });
assert_eq!(g.coerce_json(&tagged.to_json()), None);
// A stored gradient with a huge tail: stops past the limit don't take part in sampling.
let mut colors: Vec<(f64, [f32; 4])> = (0..MAX_GRADIENT_STOPS).map(|i| (i as f64 / MAX_GRADIENT_STOPS as f64 * 0.5, [0.0, 0.0, 0.0, 1.0])).collect();
colors.extend((0..100_000).map(|_| (1.0, [1.0, 1.0, 1.0, 1.0])));
let big = Gradient { colors, opacities: vec![(0.0, 1.0)] };
assert_eq!(big.sample(1.0), [0.0, 0.0, 0.0, 1.0]);
}
/// #612: the editor saves the typed form, so one colour stop (which the plain JSON parser
/// rejects) and each stop's RGBA come back exactly.
#[test]
+70 -7
View File
@@ -977,7 +977,19 @@ impl TextDoc {
if r.start >= r.end && n > 0 {
return Ok(true);
}
self.apply_style(r.start..r.end.max(r.start + usize::from(n == 0)), |s| {
// Empty text: apply_style ignores the bounds, so a fixed range avoids `start + 1` overflow.
let range = if n == 0 { 0..1 } else { r.start..r.end };
// Check every affected run first, so a value one run refuses changes none of them.
let mut err = None;
self.apply_style(range.clone(), |s| {
if let Err(e) = apply_char_attr(&mut s.clone(), key, v) {
err.get_or_insert(e);
}
});
if let Some(e) = err {
return Err(e);
}
self.apply_style(range, |s| {
let _ = apply_char_attr(s, key, v);
});
return Ok(true);
@@ -1057,6 +1069,11 @@ pub const CHAR_ATTRS: &[&str] = &[
pub const PARA_ATTRS: &[&str] =
&["justify", "indentLeft", "indentRight", "indentFirst", "spaceBefore", "spaceAfter", "direction", "composer", "hangingPunctuation"];
/// Most variable font axes one style can set. Real fonts have a handful (OpenType registers
/// five; custom axes rarely bring the total past twenty); a `variations` object with more is
/// rejected before any of it is applied.
pub const MAX_VARIATION_AXES: usize = 64;
/// Set one character attribute; Ok(false) for keys that aren't character attributes.
pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result<bool, String> {
match key {
@@ -1173,20 +1190,28 @@ pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result<bool, Stri
"variations" => match v {
J::Null => s.variations.clear(),
J::Object(m) => {
if m.is_empty() {
s.variations.clear();
if m.len() > MAX_VARIATION_AXES {
return Err(format!("variations: at most {MAX_VARIATION_AXES} axes"));
}
// One keyed merge (the result sorted by tag), applied only when every entry is valid.
let mut axes: std::collections::BTreeMap<String, f32> = if m.is_empty() { Default::default() } else { s.variations.iter().cloned().collect() };
for (tag, val) in m {
if tag.is_empty() || tag.len() > 4 {
return Err(format!("variations: `{tag}` is not an axis tag"));
}
s.variations.retain(|(t, _)| t != tag);
match val {
J::Null => {}
_ => s.variations.push((tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32)),
J::Null => {
axes.remove(tag);
}
_ => {
axes.insert(tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32);
}
}
}
s.variations.sort_by(|a, b| a.0.cmp(&b.0));
if axes.len() > MAX_VARIATION_AXES {
return Err(format!("variations: at most {MAX_VARIATION_AXES} axes"));
}
s.variations = axes.into_iter().collect();
}
_ => return Err("variations: expected {tag: number, …}".into()),
},
@@ -1321,6 +1346,44 @@ pub fn word_at(text: &str, ci: usize) -> Range<usize> {
mod tests {
use super::*;
#[test]
fn variations_merge_by_tag_and_reject_too_many_axes() {
let mut doc = TextDoc::plain("ab");
assert!(doc.set_attr("variations", &serde_json::json!({"wght": 650, "wdth": 90}), None).unwrap());
assert!(doc.set_attr("variations", &serde_json::json!({"wght": 700, "wdth": null, "opsz": 12}), None).unwrap());
assert_eq!(doc.style_at(0).variations, vec![("opsz".to_string(), 12.0), ("wght".to_string(), 700.0)]);
// More axes than any font has: refused up front, the style untouched.
let many: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES + 1).map(|i| (format!("a{i:03}"), J::from(1.0))).collect();
assert!(doc.set_attr("variations", &J::Object(many), None).is_err());
assert_eq!(doc.style_at(0).variations.len(), 2);
// A merge that would grow past the limit is refused too.
let fill: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES - 2).map(|i| (format!("b{i:03}"), J::from(1.0))).collect();
assert!(doc.set_attr("variations", &J::Object(fill), None).unwrap());
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
// (The probe on an empty style passes; the styles already at the limit keep their axes.)
let _ = doc.set_attr("variations", &serde_json::json!({"zzzz": 1}), None);
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
assert!(!doc.style_at(0).variations.iter().any(|(t, _)| t == "zzzz"));
}
#[test]
fn refused_variation_leaves_every_run_unchanged() {
// "ab" is at the axis limit; "cd" has none. Three new axes over both runs must be refused
// for both, not applied to "cd" only.
let mut doc = TextDoc::plain("abcd");
let fill: serde_json::Map<String, J> = (0..MAX_VARIATION_AXES).map(|i| (format!("b{i:03}"), J::from(1.0))).collect();
assert!(doc.set_attr("variations", &J::Object(fill), Some(0..2)).unwrap());
assert!(doc.set_attr("variations", &serde_json::json!({"x1": 1, "x2": 1, "x3": 1}), None).is_err());
assert_eq!(doc.style_at(3).variations.len(), 0);
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
}
#[test]
fn empty_document_accepts_an_extreme_range_without_overflow() {
let mut doc = TextDoc::plain("");
assert!(doc.set_attr("size", &J::from(20), Some(usize::MAX..usize::MAX)).unwrap());
}
#[test]
fn mixed_line_breaks_keep_original_byte_and_character_ranges() {
let text = "é\r\nβ\rx\n\n";
+12 -4
View File
@@ -210,7 +210,13 @@ impl Gradient {
}
}
/// Most stops (colour or opacity) a gradient is sampled through. Gradients are sampled per
/// pixel, so the stop count multiplies every painted pixel's work: stops past this are ignored
/// when sampling, and a gradient written as JSON with more is refused.
pub const MAX_GRADIENT_STOPS: usize = 256;
fn sample_stops<T: Copy>(stops: &[(f64, T)], t: f64, lerp: impl Fn(T, T, f64) -> T) -> Option<T> {
let stops = &stops[..stops.len().min(MAX_GRADIENT_STOPS)];
let first = stops.first()?;
if t <= first.0 {
return Some(first.1);
@@ -243,7 +249,7 @@ fn gradient_json(j: &serde_json::Value) -> Option<Gradient> {
}
fn stops(a: &[J]) -> Option<Vec<(f64, [f32; 4])>> {
let n = a.len();
if n < 2 {
if !(2..=MAX_GRADIENT_STOPS).contains(&n) {
return None;
}
a.iter()
@@ -259,6 +265,7 @@ fn gradient_json(j: &serde_json::Value) -> Option<Gradient> {
J::Object(o) => {
let colors = stops(o.get("colors")?.as_array()?)?;
let op = match o.get("opacities") {
Some(J::Array(a)) if a.len() > MAX_GRADIENT_STOPS => return None,
Some(J::Array(a)) => Some(
a.iter()
.map(|s| {
@@ -469,9 +476,10 @@ impl Value {
}
Value::Layer(_) => Some(Value::Layer(j.as_u64())),
Value::Path(_) => serde_json::from_value::<ShapePath>(j.clone()).ok().map(Value::Path).or_else(|| serde_json::from_value::<Value>(j.clone()).ok()),
Value::Gradient(_) => {
serde_json::from_value::<Value>(j.clone()).ok().filter(|v| matches!(v, Value::Gradient(_))).or_else(|| gradient_json(j).map(Value::Gradient))
}
Value::Gradient(_) => serde_json::from_value::<Value>(j.clone())
.ok()
.filter(|v| matches!(v, Value::Gradient(g) if g.colors.len() <= MAX_GRADIENT_STOPS && g.opacities.len() <= MAX_GRADIENT_STOPS))
.or_else(|| gradient_json(j).map(Value::Gradient)),
}
}
/// JSON for display/automation (numbers/arrays for numeric kinds).
+18 -10
View File
@@ -145,16 +145,18 @@ fn hex(c: [f32; 3]) -> String {
format!("#{:02x}{:02x}{:02x}", b(c[0]), b(c[1]), b(c[2]))
}
fn mime_of(path: &str) -> &'static str {
let lower = path.to_ascii_lowercase();
if lower.ends_with(".jpg") || lower.ends_with(".jpeg") {
"image/jpeg"
} else if lower.ends_with(".webp") {
"image/webp"
} else if lower.ends_with(".gif") {
"image/gif"
/// The image type `bytes` hold, by their signature; `None` for anything else.
pub(crate) fn image_mime(bytes: &[u8]) -> Option<&'static str> {
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
Some("image/png")
} else if bytes.starts_with(&[0xff, 0xd8, 0xff]) {
Some("image/jpeg")
} else if bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") {
Some("image/gif")
} else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WEBP") {
Some("image/webp")
} else {
"image/png"
None
}
}
@@ -286,7 +288,13 @@ impl Doc<'_> {
self.ex.warn(format!("{}: cannot read image `{}` (layer exported as a null)", l.name, f.path));
return None;
};
let uri = format!("data:{};base64,{}", mime_of(&f.path), crate::base64::encode(&bytes));
// What a project calls a still is embedded only when it is one: the path can name any
// file (an imported animation's asset), and the export would carry its bytes verbatim.
let Some(mime) = image_mime(&bytes) else {
self.ex.warn(format!("{}: `{}` is not a PNG, JPEG, GIF or WebP image (layer exported as a null)", l.name, f.path));
return None;
};
let uri = format!("data:{mime};base64,{}", crate::base64::encode(&bytes));
self.assets.push(json!({"id": id, "nm": it.name, "w": f.width, "h": f.height, "u": "", "p": uri, "e": 1}));
self.done.insert(id.clone());
Some(id)
+31 -1
View File
@@ -725,7 +725,7 @@ fn effects_expressions_and_warnings() {
#[test]
fn images_embed_as_base64_and_come_back() {
let (mut p, cid) = setup();
let png: Vec<u8> = vec![0x89, b'P', b'N', b'G', 1, 2, 3, 4, 5];
let png: Vec<u8> = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3, 4, 5];
let fid = p.add_item(
"pic.png",
Label::Lavender,
@@ -775,6 +775,17 @@ fn images_embed_as_base64_and_come_back() {
assert!(f.path.starts_with("/tmp/image_") && f.path.ends_with(".png"), "{}", f.path);
}
#[test]
fn image_signatures_are_matched_in_full() {
use crate::export::image_mime;
assert_eq!(image_mime(b"\x89PNG\r\n\x1a\n\0"), Some("image/png"));
assert_eq!(image_mime(b"GIF89a\0"), Some("image/gif"));
assert_eq!(image_mime(b"GIF87a\0"), Some("image/gif"));
// Near matches (a prefix of the signature, or text that starts with it) are not images.
assert_eq!(image_mime(b"\x89PNG\x01\x02\x03\x04"), None);
assert_eq!(image_mime(b"GIF8 is a text file"), None);
}
#[test]
fn dotlottie_archive_roundtrip() {
let (mut p, cid) = setup();
@@ -985,3 +996,22 @@ fn per_corner_roundness_exports_equal_radii_and_warns_when_mixed() {
assert_eq!(rc(&json)["r"]["k"], json!(5.0));
assert!(!warnings.iter().any(|w| w.contains("per-corner")), "{warnings:?}");
}
/// A footage item called a still whose file is not an image is not embedded.
#[test]
fn only_image_bytes_are_embedded() {
let (mut p, cid) = setup();
let fid = p.add_item(
"secret.png",
Label::Lavender,
None,
ItemKind::Footage(Footage { path: "/virtual/secret.png".into(), kind: FootageKind::Still, width: 8, height: 8, has_video: true, ..Default::default() }),
);
let c = comp(&p, cid);
let l = build::layer(&mut p, &c, "secret.png", LayerSource::Footage { item: fid }, (8, 8), None);
push(&mut p, cid, l);
let read = |path: &str| (path == "/virtual/secret.png").then(|| b"root:x:0:0:root:/root:/bin/sh\n".to_vec());
let res = export_comp(&p, cid, &ExportOptions::default(), &read).unwrap();
assert!(res.json["assets"].as_array().is_none_or(|a| a.is_empty()), "{}", res.json["assets"]);
assert!(res.warnings.iter().any(|w| w.contains("not a PNG")), "{:?}", res.warnings);
}
+88 -15
View File
@@ -7,8 +7,9 @@
//! are encoded in the file. Alpha follows the footage's [`AlphaMode`] interpretation.
use effectcraft_project::{AlphaMode, Footage};
use effectcraft_raster::{Image, Px};
use effectcraft_raster::{Image, Px, quantize_px_u8};
use effectcraft_render::FloatCurve;
use effectcraft_render::disk_cache::Frame8;
use filmcraft_color::{Matrix, Range};
use filmcraft_frame::{Chroma, PixelData, VideoFrame};
use rayon::prelude::*;
@@ -80,14 +81,7 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
return img;
}
match &f.data {
PixelData::Rgba8(d) => {
img.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
let src = &d[y * w * 4..(y + 1) * w * 4];
for (o, s) in row.iter_mut().zip(src.as_chunks::<4>().0.iter()) {
*o = op.apply([U8[s[0] as usize], U8[s[1] as usize], U8[s[2] as usize]], U8[s[3] as usize]);
}
});
}
PixelData::Rgba8(d) => convert_rgba8(&mut img.data, d, w, op, |p| p),
PixelData::RgbaF32(d) => {
// linear premultiplied → encoded premultiplied
img.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
@@ -104,7 +98,7 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
let yuv = Yuv::new(f.color.matrix, f.color.range, 8);
let amax = 255.0;
convert_yuv(
&mut img,
&mut img.data,
YuvSource {
size: (w, h),
chroma: *chroma,
@@ -114,13 +108,14 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
},
&yuv,
op,
|p| p,
);
}
PixelData::Yuv16 { planes, chroma, bits, alpha } => {
let yuv = Yuv::new(f.color.matrix, f.color.range, *bits);
let amax = ((1u32 << *bits) - 1) as f32;
convert_yuv(
&mut img,
&mut img.data,
YuvSource {
size: (w, h),
chroma: *chroma,
@@ -130,12 +125,52 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
},
&yuv,
op,
|p| p,
);
}
}
img
}
/// An 8-bit frame as [`frame_to_image_in`] converts it and an 8 bpc render then quantises it
/// ([`effectcraft_raster::quantize_px`]), kept as the 8-bit levels: expanded with
/// [`effectcraft_raster::expand_u8`] it is that quantised image to the bit, in a quarter of
/// the memory. `None` for frames with more than 8 bits (10/12-bit Y'CbCr, float).
pub(crate) fn frame_to_rgba8(f: &VideoFrame, op: AlphaOp) -> Option<Frame8> {
let (w, h) = (f.width as usize, f.height as usize);
let mut px = vec![[0u8; 4]; w * h];
if w > 0 && h > 0 {
match &f.data {
PixelData::Rgba8(d) => convert_rgba8(&mut px, d, w, op, quantize_px_u8),
PixelData::Yuv8 { planes, chroma, alpha } => convert_yuv(
&mut px,
YuvSource {
size: (w, h),
chroma: *chroma,
planes: [&planes[0][..], &planes[1][..], &planes[2][..]],
alpha: alpha.as_deref().map(|a| &a[..]),
amax: 255.0,
},
&Yuv::new(f.color.matrix, f.color.range, 8),
op,
quantize_px_u8,
),
PixelData::RgbaF32(_) | PixelData::Yuv16 { .. } => return None,
}
}
Some(Frame8 { width: f.width, height: f.height, rgba: px.into_flattened() })
}
/// Straight or premultiplied 8-bit RGBA into `out` (one `O` per pixel, made by `store`).
fn convert_rgba8<O: Send>(out: &mut [O], d: &[u8], w: usize, op: AlphaOp, store: impl Fn(Px) -> O + Sync) {
out.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
let Some(src) = d.get(y * w * 4..(y + 1) * w * 4) else { return };
for (o, s) in row.iter_mut().zip(src.as_chunks::<4>().0.iter()) {
*o = store(op.apply([U8[s[0] as usize], U8[s[1] as usize], U8[s[2] as usize]], U8[s[3] as usize]));
}
});
}
/// A movie frame turned the way its file says it is shown ([`crate::probe::video_turn`]:
/// clockwise quarter turns; anything else leaves it as decoded).
pub(crate) fn turned(img: Image, turn: image::metadata::Orientation) -> Image {
@@ -311,13 +346,14 @@ struct YuvSource<'a, T> {
amax: f32,
}
fn convert_yuv<T: Sample>(img: &mut Image, source: YuvSource<'_, T>, k: &Yuv, op: AlphaOp) {
/// Y'CbCr into `out` (one `O` per pixel, made by `store` from the converted pixel).
fn convert_yuv<T: Sample, O: Send>(out: &mut [O], source: YuvSource<'_, T>, k: &Yuv, op: AlphaOp, store: impl Fn(Px) -> O + Sync) {
let YuvSource { size: (w, h), chroma, planes, alpha, amax } = source;
let (sx, sy) = chroma.shifts();
let cw = w.div_ceil(1 << sx);
let ch = h.div_ceil(1 << sy);
let inv_amax = 1.0 / amax;
img.data.par_chunks_mut(w).enumerate().for_each_init(
out.par_chunks_mut(w).enumerate().for_each_init(
|| (vec![0f32; cw], vec![0f32; cw], vec![0f32; w], vec![0f32; w]),
|(uc, vc, uf, vf), (y, row)| {
// vertical chroma interpolation into normalised rows (Cb, Cr in −0.5..0.5)
@@ -357,13 +393,13 @@ fn convert_yuv<T: Sample>(img: &mut Image, source: YuvSource<'_, T>, k: &Yuv, op
None => {
for (x, o) in row.iter_mut().enumerate() {
let c = rgb(x);
*o = [c[0], c[1], c[2], 1.0];
*o = store([c[0], c[1], c[2], 1.0]);
}
}
Some(al) => {
let arow = &al[y * w..y * w + w];
for (x, o) in row.iter_mut().enumerate() {
*o = op.apply(rgb(x), (arow[x].f() * inv_amax).clamp(0.0, 1.0));
*o = store(op.apply(rgb(x), (arow[x].f() * inv_amax).clamp(0.0, 1.0)));
}
}
}
@@ -552,6 +588,43 @@ mod tests {
assert!((p[0] - 0.5).abs() < 1e-3 && (p[1] - 1.0).abs() < 1e-5 && p[2] > 1.5, "{p:?}");
}
/// The 8-bit levels of a frame expand to the float frame quantised to 8 bpc, bit for bit:
/// 4:2:0 and 4:4:4 Y'CbCr with and without alpha, and RGBA, under every alpha interpretation.
#[test]
fn rgba8_levels_are_the_quantised_float_frame() {
let (w, h) = (37usize, 18usize);
let noise = |n: usize, seed: usize| Arc::new((0..n).map(|i| ((i * 7919 + seed * 104_729) % 256) as u8).collect::<Vec<u8>>());
let frame = |data| VideoFrame {
width: w as u32,
height: h as u32,
data,
color: filmcraft_color::ColorInfo::REC709,
par: (1, 1),
pts: filmcraft_time::Tick::ZERO,
};
let yuv = |chroma: Chroma, alpha: bool| {
let n = w.div_ceil(1 << chroma.shifts().0) * h.div_ceil(1 << chroma.shifts().1);
frame(PixelData::Yuv8 { planes: [noise(w * h, 1), noise(n, 2), noise(n, 3)], chroma, alpha: alpha.then(|| noise(w * h, 4)) })
};
let frames = [yuv(Chroma::C420, false), yuv(Chroma::C420, true), yuv(Chroma::C444, true), frame(PixelData::Rgba8(noise(w * h * 4, 5)))];
for f in &frames {
for mode in [AlphaMode::Straight, AlphaMode::Premultiplied, AlphaMode::Ignore] {
let op = AlphaOp::new(mode, [0.2, 0.5, 0.9]);
let q8 = frame_to_rgba8(f, op).expect("8-bit");
let mut want = frame_to_image(f, op);
for p in &mut want.data {
effectcraft_raster::quantize_px(p, 255.0, 1.0 / 255.0);
}
assert_eq!((q8.width, q8.height), (want.width, want.height));
for (got, want) in q8.rgba.as_chunks::<4>().0.iter().zip(&want.data) {
assert_eq!(effectcraft_raster::expand_u8(*got).map(f32::to_bits), want.map(f32::to_bits));
}
}
}
let wide = frame(PixelData::RgbaF32(Arc::new(vec![0.5; w * h * 4])));
assert!(frame_to_rgba8(&wide, AlphaOp::new(AlphaMode::Straight, [0.0; 3])).is_none());
}
/// `cargo test -p effectcraft-media --lib bench -- --ignored --nocapture`
#[test]
#[ignore]
+105 -25
View File
@@ -6,13 +6,14 @@ use std::sync::{Arc, Condvar, Mutex, OnceLock};
use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId};
use effectcraft_raster::{Image, Px};
use effectcraft_render::disk_cache::Frame8;
use effectcraft_render::reads::{FileStamp, ReadFile, ReadFiles, ReadLog};
use effectcraft_render::{FloatCurve, FootageSource};
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
use filmcraft_media::{FrameRequest, SharedSource};
use image::metadata::Orientation;
use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, turned};
use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, frame_to_rgba8, turned};
use crate::downmix::Order;
use crate::gif_anim::{self, Anim};
use crate::reads;
@@ -53,10 +54,28 @@ struct Key {
/// resampled (see `Inner::still_at_size`); also at its own size for a placed PDF /
/// Illustrator page, which is drawn to fill it ([`Footage::placed_page`]); (0, 0) otherwise.
nominal: (u32, u32),
/// Kept as 8-bit levels ([`FootageSource::frame8`]), not float.
q8: bool,
}
/// A cached frame's pixels: float, or the 8-bit levels an 8 bpc render quantises it to.
#[derive(Clone)]
enum Pixels {
F32(Arc<Image>),
Q8(Arc<Frame8>),
}
impl Pixels {
fn bytes(&self) -> usize {
match self {
Pixels::F32(img) => img.data.len() * std::mem::size_of::<Px>(),
Pixels::Q8(f) => f.rgba.len(),
}
}
}
struct Entry {
img: Arc<Image>,
img: Pixels,
/// The files the frame was decoded from (see [`crate::reads`]).
files: Arc<ReadFiles>,
stamp: u64,
@@ -112,8 +131,8 @@ struct Lru {
}
impl Lru {
/// The cached image and its read files for `k`, marking the entry as most recently used.
fn get(&mut self, k: &Key) -> Option<(Arc<Image>, Arc<ReadFiles>)> {
/// The cached frame and its read files for `k`, marking the entry as most recently used.
fn get(&mut self, k: &Key) -> Option<(Pixels, Arc<ReadFiles>)> {
let e = self.map.get_mut(k)?;
self.order.remove(&e.stamp);
self.clock += 1;
@@ -121,9 +140,9 @@ impl Lru {
self.order.insert(e.stamp, k.clone());
Some((e.img.clone(), e.files.clone()))
}
/// Stores an image with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes.
fn insert(&mut self, k: Key, img: Arc<Image>, files: Arc<ReadFiles>, budget: usize) {
let bytes = img.data.len() * std::mem::size_of::<Px>() + 64;
/// Stores a frame with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes.
fn insert(&mut self, k: Key, img: Pixels, files: Arc<ReadFiles>, budget: usize) {
let bytes = img.bytes() + 64;
self.clock += 1;
if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) {
self.order.remove(&old.stamp);
@@ -145,8 +164,10 @@ impl Lru {
let Some((_, k)) = self.order.pop_first() else { break };
if let Some(e) = self.map.remove(&k) {
self.bytes -= e.bytes;
// ponytail: float buffers only; add 8-bit spares if their allocation shows up in a profile
if self.spare.len() < MAX_SPARE
&& let Ok(img) = Arc::try_unwrap(e.img)
&& let Pixels::F32(img) = e.img
&& let Ok(img) = Arc::try_unwrap(img)
{
self.spare.push(img.data);
}
@@ -185,6 +206,8 @@ struct CacheState {
inflight: HashSet<Key>,
/// Last frame index requested per movie (sequential-playback detection).
last: HashMap<Arc<str>, i64>,
/// Movies whose frames can't be kept as 8-bit levels (more bits, or shown turned).
wide: HashSet<Arc<str>>,
}
/// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait
@@ -407,6 +430,7 @@ impl MediaPool {
c.lru.remove(&k);
}
c.last.remove(path);
c.wide.remove(path);
drop(c);
crate::exr_channels::forget(path);
}
@@ -422,6 +446,21 @@ impl MediaPool {
Inner::frame_at(&self.inner, footage, t, true, None, FloatCurve::Srgb)
}
/// [`FootageSource::frame8`]: `None` (the renderer then asks for the float frame) for
/// anything but an 8-bit movie frame shown as decoded, and on errors.
fn frame8_of(&self, footage: &Footage, t: Tick, float: FloatCurve) -> Option<Arc<Frame8>> {
if footage.missing || footage.kind != FootageKind::Video || gif_anim::is_animated(footage) {
return None;
}
if lock(&self.inner.cache).wide.contains(footage.path.as_str()) {
return None;
}
match Inner::frame_px(&self.inner, footage, t, true, None, float, true) {
Ok(Pixels::Q8(f)) => Some(f),
_ => None,
}
}
/// [`FootageSource::frame`] / [`FootageSource::frame_at_size`]: `None` (logged) on errors.
fn frame_sized(&self, _item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
if footage.missing {
@@ -775,7 +814,7 @@ impl Inner {
} << 2)
| (u8::from(footage.placed_page) << 4);
let matte = if footage.alpha == AlphaMode::Premultiplied { footage.premul_color.map(f32::to_bits) } else { [0; 3] };
let key = |path: &str, frame| Key { path: path.into(), frame, interp, matte, size: (0, 0), nominal: (0, 0) };
let key = |path: &str, frame| Key { path: path.into(), frame, interp, matte, size: (0, 0), nominal: (0, 0), q8: false };
match footage.kind {
FootageKind::Sequence if !footage.sequence.is_empty() => {
let i = Self::frame_index(footage.frame_rate, t, footage.sequence_frames(), footage.loop_count);
@@ -811,10 +850,22 @@ impl Inner {
/// The frame of `footage` at `t`, resampled to `size` (see [`FootageSource::frame_at_size`]):
/// a movie's made at that size directly, a still's resampled once and kept at that size.
fn frame_at(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve) -> Result<Arc<Image>> {
match Self::frame_px(self, footage, t, playback, size, float, false)? {
Pixels::F32(img) => Ok(img),
// (a float request's key never holds 8-bit levels)
Pixels::Q8(_) => Err(MediaError::Decode(format!("{}: 8-bit frame for a float request", footage.path))),
}
}
/// [`Self::frame_at`], kept as 8-bit levels when `q8` is set and the frame has 8 bits (a
/// float frame under the float key otherwise, and the movie is remembered as `wide`).
#[allow(clippy::too_many_arguments)]
fn frame_px(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) -> Result<Pixels> {
if !footage.has_video {
return Err(MediaError::Unsupported(format!("{}: no video", footage.path)));
}
let mut loc = Self::locate(footage, t, float);
loc.key.q8 = q8;
let movie = loc.media_t.is_some();
// A still asked for at its own size is the unsized frame (one decode, one entry).
if let Some(size) = size.filter(|s| movie || *s != (footage.width, footage.height)) {
@@ -854,7 +905,7 @@ impl Inner {
drop(c);
let (img, files) = reads::captured(|| self.decode(&loc, footage));
reads::note_all(&files);
return Ok(Arc::new(img?));
return img;
}
c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner);
}
@@ -864,8 +915,14 @@ impl Inner {
let _guard = Inflight { inner: self, key: loc.key.clone() };
let (img, files) = reads::captured(|| self.decode(&loc, footage));
reads::note_all(&files);
let img = Arc::new(img?);
lock(&self.cache).lru.insert(loc.key, img.clone(), Arc::new(files), self.budget());
let img = img?;
let mut c = lock(&self.cache);
if q8 && let Pixels::F32(_) = img {
// not 8-bit: kept as the float frame it is, which the renderer asks for next
c.wide.insert(loc.key.path.clone());
loc.key.q8 = false;
}
c.lru.insert(loc.key, img.clone(), Arc::new(files), self.budget());
Ok(img)
}
@@ -876,24 +933,25 @@ impl Inner {
let next_t = t + footage.frame_rate.frame_duration();
let mut next = Self::locate(footage, next_t, float);
next.key.size = loc.key.size;
next.key.q8 = loc.key.q8;
if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) {
self.prefetch(footage, next_t, size, float);
self.prefetch(footage, next_t, size, float, loc.key.q8);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) {
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) {
let (me, f) = (self.clone(), footage.clone());
self.prefetched.fetch_add(1, Ordering::Relaxed);
// A plain thread, not a rayon job: the decode below parallelises with rayon itself.
std::thread::spawn(move || {
let _ = me.frame_at(&f, t, false, size, float);
let _ = me.frame_px(&f, t, false, size, float, q8);
});
}
#[cfg(target_arch = "wasm32")]
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve) {}
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool) {}
/// A still (image file or sequence frame) at `loc.key.size` when one is set: resampled as the
/// renderer would ([`FootageSource::frame_at_size`]), so the pool keeps the reduced frame and a
@@ -910,9 +968,9 @@ impl Inner {
effectcraft_raster::resample(&img, w, h)
}
fn decode(&self, loc: &Loc, footage: &Footage) -> Result<Image> {
fn decode(&self, loc: &Loc, footage: &Footage) -> Result<Pixels> {
match loc.media_t {
None => self.decode_still(loc, footage).map(|img| Self::still_at_size(img, loc, footage)),
None => self.decode_still(loc, footage).map(|img| Pixels::F32(Arc::new(Self::still_at_size(img, loc, footage)))),
Some(_) => self.decode_media(loc, footage),
}
}
@@ -959,15 +1017,16 @@ impl Inner {
}
}
fn decode_media(&self, loc: &Loc, footage: &Footage) -> Result<Image> {
fn decode_media(&self, loc: &Loc, footage: &Footage) -> Result<Pixels> {
let op = AlphaOp::of(footage, loc.float);
let path = &loc.key.path;
let f32 = |img| Pixels::F32(Arc::new(img));
match loc.media_t {
None => self.decode_still(loc, footage),
None => self.decode_still(loc, footage).map(f32),
Some(mt) if gif_anim::is_animated(footage) => {
let anim = self.gif(path)?;
let frame = anim.frame_at(Tick(mt.0)).ok_or_else(|| MediaError::Decode(format!("{path}: no frames")))?;
Ok(dynamic_to_image(frame, op))
Ok(f32(dynamic_to_image(frame, op)))
}
Some(mt) => {
let Opened { src, turn, .. } = self.opened(path).ok_or_else(|| MediaError::Io(format!("{path}: cannot open")))?;
@@ -985,10 +1044,16 @@ impl Inner {
&& (vf.width, vf.height) == side((footage.width, footage.height))
&& let Some(img) = frame_to_image_scaled(&vf, w, h)
{
return Ok(turned(img, turn));
return Ok(f32(turned(img, turn)));
}
if loc.key.q8
&& turn == Orientation::NoTransforms
&& let Some(f) = frame_to_rgba8(&vf, op)
{
return Ok(Pixels::Q8(Arc::new(f)));
}
let buf = lock(&self.cache).lru.take_spare(vf.width as usize * vf.height as usize);
Ok(turned(frame_to_image_in(&vf, op, buf), turn))
Ok(f32(turned(frame_to_image_in(&vf, op, buf), turn)))
}
}
}
@@ -1038,6 +1103,9 @@ impl FootageSource for MediaPool {
fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
self.frame_sized(item, footage, t, size, float)
}
fn frame8(&self, _item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option<Arc<Frame8>> {
self.frame8_of(footage, t, float)
}
fn problem(&self, footage: &Footage) -> Option<String> {
if footage.missing || footage.kind != FootageKind::Video {
@@ -1275,8 +1343,8 @@ mod tests {
#[test]
fn lru_respects_budget_and_recycles() {
let mut c = Lru::default();
let img = || Arc::new(Image::new(16, 16)); // 4 KiB + overhead
let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0) };
let img = || Pixels::F32(Arc::new(Image::new(16, 16))); // 4 KiB + overhead
let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), q8: false };
let budget = 3 * (4096 + 64);
for i in 0..10 {
c.insert(k(i), img(), Arc::default(), budget);
@@ -1292,6 +1360,18 @@ mod tests {
assert!(c.take_spare(17).is_empty());
}
/// A frame kept as 8-bit levels is its own entry beside the float one, counted at 4 bytes
/// a pixel.
#[test]
fn lru_keeps_8_bit_frames_apart_at_a_quarter_of_the_bytes() {
let mut c = Lru::default();
let k = |q8| Key { path: "a".into(), frame: 0, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), q8 };
c.insert(k(false), Pixels::F32(Arc::new(Image::new(16, 16))), Arc::default(), usize::MAX);
c.insert(k(true), Pixels::Q8(Arc::new(Frame8 { width: 16, height: 16, rgba: vec![0; 16 * 16 * 4] })), Arc::default(), usize::MAX);
assert!(matches!(c.get(&k(false)), Some((Pixels::F32(_), _))) && matches!(c.get(&k(true)), Some((Pixels::Q8(_), _))));
assert_eq!(c.bytes, (16 * 16 * 16 + 64) + (16 * 16 * 4 + 64));
}
/// #482: an OpenEXR file with HTJ2K compression (which the decoder lacks) says so on import
/// and when its frame is read, instead of "no non-deep rgb channels" or a transparent frame.
#[test]
+176 -38
View File
@@ -55,37 +55,85 @@ pub fn trace(field: &[f32], w: usize, h: usize, o: &TraceOpts) -> Vec<Traced> {
return vec![];
}
let loops = contours(field, w, h, o.threshold);
let mut polys: Vec<(Vec<[f64; 2]>, Vec<[f64; 2]>, f64)> = loops
// (raw loop, kept raw indices, simplified loop, area)
let mut polys: Vec<(Vec<[f64; 2]>, Vec<usize>, Vec<[f64; 2]>, f64)> = loops
.into_iter()
.filter_map(|raw| {
let a = signed_area(&raw);
if a.abs() < o.min_area.max(0.0) || raw.len() < 3 {
return None;
}
let simp = simplify_closed(&raw, o.tolerance.max(0.01));
if simp.len() < 3 {
let idx = simplify_idx(&raw, o.tolerance.max(0.01));
if idx.len() < 3 {
return None;
}
Some((raw, simp, a))
})
.collect();
// Depth: how many other loops contain a point of this loop.
let depths: Vec<usize> = (0..polys.len())
.map(|i| {
let p = polys[i].1[0];
(0..polys.len()).filter(|&j| j != i && polys[j].2.abs() > polys[i].2.abs() && point_in_poly(p, &polys[j].1)).count()
let simp = idx.iter().map(|&i| raw[i]).collect();
Some((raw, idx, simp, a))
})
.collect();
let depths = nesting_depths(&polys.iter().map(|p| (p.2.as_slice(), p.3.abs())).collect::<Vec<_>>(), w, h);
let mut out: Vec<Traced> = polys
.drain(..)
.zip(depths)
.map(|((raw, simp, a), depth)| Traced { path: fit_loop(&raw, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a })
.map(|((raw, idx, simp, a), depth)| Traced { path: fit_loop(&raw, &idx, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a })
.collect();
// Outer outlines first, then holes, islands…; larger first within a depth.
out.sort_by(|a, b| a.depth.cmp(&b.depth).then(b.area.abs().total_cmp(&a.area.abs())));
out
}
/// Grid cell (pixels) of the index [`nesting_depths`] finds candidate containers with.
const DEPTH_CELL: f64 = 32.0;
/// Most grid cells one loop's box is entered into; a box covering more is kept in a short list
/// every point is tested against, so nested full-image contours can't fill the grid.
const MAX_BOX_CELLS: usize = 64;
/// Per loop (simplified points, |area|): how many other, larger loops contain its first point.
/// Only loops whose bounding box holds that point can contain it, so each loop's box is entered
/// into a coarse grid over the `w × h` field and a point is tested against the loops of its
/// cell alone: many separate regions no longer test every pair.
fn nesting_depths(polys: &[(&[[f64; 2]], f64)], w: usize, h: usize) -> Vec<usize> {
// Loops lie within the field padded by half a pixel each side.
let (gw, gh) = (((w as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1, ((h as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1);
let cell = |v: f64, n: usize| (((v + 0.5) / DEPTH_CELL).floor().max(0.0) as usize).min(n - 1);
let boxes: Vec<[f64; 4]> = polys
.iter()
.map(|(p, _)| {
p.iter().fold([f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY], |b, q| {
[b[0].min(q[0]), b[1].min(q[1]), b[2].max(q[0]), b[3].max(q[1])]
})
})
.collect();
let mut grid: Vec<Vec<usize>> = vec![Vec::new(); gw * gh];
let mut wide: Vec<usize> = Vec::new();
for (j, b) in boxes.iter().enumerate() {
let (x0, x1, y0, y1) = (cell(b[0], gw), cell(b[2], gw), cell(b[1], gh), cell(b[3], gh));
if (x1 - x0 + 1).saturating_mul(y1 - y0 + 1) > MAX_BOX_CELLS {
wide.push(j);
continue;
}
for cy in y0..=y1 {
for cx in x0..=x1 {
grid[cy * gw + cx].push(j);
}
}
}
(0..polys.len())
.map(|i| {
let p = polys[i].0[0];
grid[cell(p[1], gh) * gw + cell(p[0], gw)]
.iter()
.chain(&wide)
.filter(|&&j| {
let b = boxes[j];
j != i && polys[j].1 > polys[i].1 && p[0] >= b[0] && p[0] <= b[2] && p[1] >= b[1] && p[1] <= b[3] && point_in_poly(p, polys[j].0)
})
.count()
})
.collect()
}
/// Marching squares: closed polylines (pixel coordinates).
fn contours(field: &[f32], w: usize, h: usize, thr: f32) -> Vec<Vec<[f64; 2]>> {
// Padded grid: sample (i, j) of the padded grid is pixel (i - 1, j - 1), outside = -inf.
@@ -233,23 +281,37 @@ fn seg_dist(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> f64 {
((p[0] - a[0] - t * dx).powi(2) + (p[1] - a[1] - t * dy).powi(2)).sqrt()
}
/// Douglas–Peucker on an open polyline: kept indices.
fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, keep: &mut Vec<usize>) {
if hi <= lo + 1 {
return;
}
let (mut best, mut bi) = (0.0, lo);
for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) {
let d = seg_dist(*q, p[lo], p[hi]);
if d > best {
best = d;
bi = k;
/// Distance evaluations Douglas–Peucker may spend per point of a loop. Balanced splits need
/// about log2(n); splits that keep landing next to an end would need up to n.
const DP_WORK_PER_POINT: usize = 64;
/// Douglas–Peucker on an open polyline: kept indices (unordered). Spans wait on an explicit
/// stack (no recursion depth to overflow), and once `budget` distance evaluations are spent the
/// spans still waiting keep all their points: the outline stays exact, just less simplified.
fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, budget: &mut usize, keep: &mut Vec<usize>) {
let mut spans = vec![(lo, hi)];
while let Some((lo, hi)) = spans.pop() {
if hi <= lo + 1 {
continue;
}
if *budget < hi - lo - 1 {
keep.extend(lo + 1..hi);
continue;
}
*budget -= hi - lo - 1;
let (mut best, mut bi) = (0.0, lo);
for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) {
let d = seg_dist(*q, p[lo], p[hi]);
if d > best {
best = d;
bi = k;
}
}
if best > tol {
keep.push(bi);
spans.push((lo, bi));
spans.push((bi, hi));
}
}
if best > tol {
dp(p, lo, bi, tol, keep);
keep.push(bi);
dp(p, bi, hi, tol, keep);
}
}
@@ -265,27 +327,23 @@ fn simplify_idx(p: &[[f64; 2]], tol: f64) -> Vec<usize> {
let mut ext: Vec<[f64; 2]> = p.to_vec();
ext.push(p[0]);
let mut keep = vec![0];
dp(&ext, 0, far, tol, &mut keep);
let mut budget = n.saturating_mul(DP_WORK_PER_POINT);
dp(&ext, 0, far, tol, &mut budget, &mut keep);
keep.push(far);
dp(&ext, far, n, tol, &mut keep);
dp(&ext, far, n, tol, &mut budget, &mut keep);
keep.sort_unstable();
keep.dedup();
keep.retain(|&i| i < n);
keep
}
fn simplify_closed(p: &[[f64; 2]], tol: f64) -> Vec<[f64; 2]> {
simplify_idx(p, tol).into_iter().map(|i| p[i]).collect()
}
/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour).
fn fit_loop(raw: &[[f64; 2]], simp: &[[f64; 2]], round: f64) -> ShapePath {
/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour, `idx`
/// the raw index of each simplified vertex, as [`simplify_idx`] kept them).
fn fit_loop(raw: &[[f64; 2]], idx: &[usize], simp: &[[f64; 2]], round: f64) -> ShapePath {
let n = simp.len();
if round <= 1e-6 {
return ShapePath::polygon(simp, true);
}
// Raw index of each simplified vertex (they are raw points).
let idx: Vec<usize> = simp.iter().map(|s| raw.iter().position(|r| r == s).unwrap_or(0)).collect();
let pt = |a: [f64; 2]| Point::new(a[0], a[1]);
let v = |a: [f64; 2], b: [f64; 2]| Vec2::new(b[0] - a[0], b[1] - a[1]);
// Per vertex: (direction leaving, direction arriving), blended between chord and smooth.
@@ -349,6 +407,86 @@ mod tests {
fill_coverage(&paths, &Mat3::IDENTITY, w as u32, h as u32, FillRule::EvenOdd).data
}
#[test]
fn nesting_depths_match_every_pair() {
// Rings within rings beside many separate dots: the grid lookup gives the same depths
// as testing every pair.
let (w, h) = (200, 120);
let mut field = vec![0.0f32; w * h];
for y in 0..h {
for x in 0..w {
let (fx, fy) = (x as f64 + 0.5 - 60.0, y as f64 + 0.5 - 60.0);
let r = (fx * fx + fy * fy).sqrt();
let ring = r < 56.0 && ((r / 8.0) as usize).is_multiple_of(2);
let dot = x >= 130 && x % 6 < 3 && y % 6 < 3;
field[y * w + x] = if ring || dot { 1.0 } else { 0.0 };
}
}
let opts = TraceOpts { min_area: 1.0, ..Default::default() };
let t = trace(&field, w, h, &opts);
assert!(t.len() > 200, "{}", t.len());
let polys: Vec<(Vec<[f64; 2]>, f64)> = t.iter().map(|t| (t.path.vertices.clone(), t.area.abs())).collect();
let refs: Vec<(&[[f64; 2]], f64)> = polys.iter().map(|(p, a)| (p.as_slice(), *a)).collect();
let fast = nesting_depths(&refs, w, h);
let pairs: Vec<usize> =
(0..refs.len()).map(|i| (0..refs.len()).filter(|&j| j != i && refs[j].1 > refs[i].1 && point_in_poly(refs[i].0[0], refs[j].0)).count()).collect();
assert_eq!(fast, pairs);
assert_eq!(fast, t.iter().map(|t| t.depth).collect::<Vec<_>>());
assert!(fast.iter().max() >= Some(&5));
}
#[test]
fn nesting_depths_count_wide_boxes_kept_out_of_the_grid() {
// Concentric squares whose boxes each cover far more than MAX_BOX_CELLS cells.
let (w, h) = (4000, 4000);
let squares: Vec<Vec<[f64; 2]>> = (0..40)
.map(|k| {
let (a, b) = (10.0 + 40.0 * k as f64, 3990.0 - 40.0 * k as f64);
vec![[a, a], [b, a], [b, b], [a, b]]
})
.collect();
let refs: Vec<(&[[f64; 2]], f64)> = squares.iter().map(|p| (p.as_slice(), (p[1][0] - p[0][0]).powi(2))).collect();
assert_eq!(nesting_depths(&refs, w, h), (0..40).collect::<Vec<_>>());
}
#[test]
fn simplification_work_is_bounded_and_keeps_the_outline() {
// Out of budget, the spans still waiting keep every point: nothing is lost, the outline
// is just less simplified.
let p: Vec<[f64; 2]> = (0..12).map(|i| [i as f64, if i % 2 == 0 { 0.0 } else { 5.0 }]).collect();
let (mut budget, mut keep) = (0, vec![]);
dp(&p, 0, 11, 0.01, &mut budget, &mut keep);
keep.sort_unstable();
assert_eq!(keep, (1..11).collect::<Vec<_>>());
// With a budget, the result matches the plain recursion's on an ordinary loop.
let (mut budget, mut keep) = (usize::MAX, vec![]);
dp(&p, 0, 11, 0.01, &mut budget, &mut keep);
keep.sort_unstable();
assert_eq!(keep, (1..11).collect::<Vec<_>>());
// A long, finely zig-zagging loop: simplified within a budget linear in its length.
let n = 200_000;
let ring: Vec<[f64; 2]> = (0..n)
.map(|i| {
let a = i as f64 / n as f64 * std::f64::consts::TAU;
let r = 1000.0 + if i % 2 == 0 { 0.0 } else { 3.0 };
[r * a.cos(), r * a.sin()]
})
.collect();
let idx = simplify_idx(&ring, 0.5);
assert!(idx.windows(2).all(|w| w[0] < w[1]) && idx.len() >= 3 && *idx.last().unwrap() < n);
}
#[test]
fn fit_uses_the_kept_indices() {
// A contour that passes the same point twice: the fit follows the simplifier's index,
// not the first raw point with those coordinates.
let raw = vec![[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 0.0], [-10.0, 10.0], [-10.0, 0.0]];
let idx = vec![0, 2, 3, 4];
let simp: Vec<[f64; 2]> = idx.iter().map(|&i| raw[i]).collect();
let path = fit_loop(&raw, &idx, &simp, 0.5);
assert_eq!(path.vertices, simp);
}
#[test]
fn traces_a_disc_and_a_square_with_a_hole() {
let (w, h) = (120, 80);
+1 -1
View File
@@ -144,7 +144,7 @@ ffmpeg elsewhere) to check the code tables against an independent implementation
## Shared parser setup and bounds
The workspace pins `craft-pdf` to `craft-libs` Git revision
`92d392cff132eaf53e13afff77f38b5c11b73aa0`, with `default-features = false` and only the explicit `read`
`e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545`, with `default-features = false` and only the explicit `read`
feature. This excludes the backend's bundled fonts and Adobe CMaps and preserves EffectCraft's
font and asset policies. The shared parser does not change the supported drawing operators,
image codecs, continuously rasterized SVG tree, editable shapes, or EPS interpreter.
+24 -2
View File
@@ -401,8 +401,10 @@ fn mode(r: &mut Reader) -> Option<Mode> {
/// (index parity: even indices start black runs), and the one after it.
fn b1b2(refl: &[usize], a0: i64, white: bool, columns: usize) -> (usize, usize) {
let want = if white { 0 } else { 1 };
let mut i = 0;
while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) {
// The changing elements are in order, so the first past `a0` is found by bisection (a scan
// from the start made every row quadratic in its transitions).
let mut i = refl.partition_point(|&c| c as i64 <= a0);
if i % 2 != want {
i += 1;
}
let b1 = refl.get(i).copied().unwrap_or(columns);
@@ -804,4 +806,24 @@ pub(crate) mod tests {
let _ = decode(&garbage, &Params { k, columns: 1, rows: 0, ..Params::default() }, 1 << 20);
}
}
/// `b1b2` by bisection agrees with the scan it replaced, for every position and colour
/// over a dense reference row.
#[test]
fn b1b2_by_bisection_matches_the_scan() {
let scan = |refl: &[usize], a0: i64, white: bool, columns: usize| {
let want = if white { 0 } else { 1 };
let mut i = 0;
while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) {
i += 1;
}
(refl.get(i).copied().unwrap_or(columns), refl.get(i + 1).copied().unwrap_or(columns))
};
let refl: Vec<usize> = (0..64).map(|i| i * 3 / 2).collect();
for a0 in -1..100 {
for white in [true, false] {
assert_eq!(b1b2(&refl, a0, white, 100), scan(&refl, a0, white, 100), "a0 {a0} white {white}");
}
}
}
}
+77 -7
View File
@@ -538,16 +538,31 @@ impl Cff {
/// A glyph's outline in glyph space (`font_matrix` maps it to text space) and its advance.
pub fn outline(&self, gid: u16) -> Option<(BezPath, f64)> {
self.outline_depth(gid, 0)
self.outline_depth(gid, 0, &mut { MAX_GLYPH_OPS })
}
fn outline_depth(&self, gid: u16, depth: usize) -> Option<(BezPath, f64)> {
/// `ops` is the glyph's instruction budget, shared by the base glyph and its seac components.
fn outline_depth(&self, gid: u16, depth: usize, ops: &mut usize) -> Option<(BezPath, f64)> {
let r = self.charstrings.get(gid as usize)?.clone();
let fd = self.fdselect.get(gid as usize).copied().unwrap_or(0) as usize;
let fd = fd.min(self.lsubrs.len() - 1);
let mut st =
T2 { cff: self, fd, path: BezPath::new(), x: 0.0, y: 0.0, stack: vec![], stems: 0, width: None, open: false, done: false, depth: 0, seac: None };
let mut st = T2 {
cff: self,
fd,
path: BezPath::new(),
x: 0.0,
y: 0.0,
stack: vec![],
stems: 0,
width: None,
open: false,
done: false,
depth: 0,
ops: *ops,
seac: None,
};
st.run(&self.data[r]);
*ops = st.ops;
if st.open {
st.path.close_path();
}
@@ -563,10 +578,10 @@ impl Cff {
&& depth < 2
{
let find = |code: u8| crate::encoding::Base::Standard.name(code).and_then(|n| self.gid_by_name(&n));
if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1)) {
if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1, ops)) {
path.extend(bp);
}
if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1)) {
if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1, ops)) {
path.extend(kurbo::Affine::translate((adx, ady)) * ap);
}
}
@@ -646,9 +661,16 @@ struct T2<'a> {
open: bool,
done: bool,
depth: usize,
/// Instructions this glyph may still run, across its subroutine calls.
ops: usize,
seac: Option<(f64, f64, u8, u8)>,
}
/// The most charstring instructions one glyph runs: subroutines that branch (two calls each,
/// ten levels deep) would otherwise multiply a small font into unbounded work and geometry.
/// A real glyph runs a few hundred.
pub(crate) const MAX_GLYPH_OPS: usize = 100_000;
impl T2<'_> {
fn move_to(&mut self, dx: f64, dy: f64) {
if self.open {
@@ -698,6 +720,11 @@ impl T2<'_> {
}
let mut i = 0;
while i < cs.len() && !self.done {
let Some(left) = self.ops.checked_sub(1) else {
self.done = true;
return;
};
self.ops = left;
let b0 = cs[i];
i += 1;
match b0 {
@@ -958,6 +985,12 @@ impl T2<'_> {
/// named by custom strings.
#[cfg(test)]
pub(crate) fn write_test_cff(glyphs: &[(&str, Vec<u8>)]) -> Vec<u8> {
write_test_cff_with_gsubrs(glyphs, &[])
}
/// [`write_test_cff`] with global subroutines.
#[cfg(test)]
pub(crate) fn write_test_cff_with_gsubrs(glyphs: &[(&str, Vec<u8>)], gsubrs: &[Vec<u8>]) -> Vec<u8> {
fn idx(items: &[Vec<u8>]) -> Vec<u8> {
let mut out = (items.len() as u16).to_be_bytes().to_vec();
if items.is_empty() {
@@ -983,7 +1016,7 @@ pub(crate) fn write_test_cff(glyphs: &[(&str, Vec<u8>)]) -> Vec<u8> {
let names = idx(&[b"Test".to_vec()]);
let strings: Vec<Vec<u8>> = glyphs.iter().skip(1).map(|(n, _)| n.as_bytes().to_vec()).collect();
let strings_idx = idx(&strings);
let gsubrs = idx(&[]);
let gsubrs = idx(gsubrs);
let cs = idx(&glyphs.iter().map(|(_, c)| c.clone()).collect::<Vec<_>>());
// charset format 0: SIDs of glyphs 1.. (custom strings from 391).
let mut charset = vec![0u8];
@@ -1090,6 +1123,43 @@ mod tests {
assert!(p.elements().is_empty());
}
/// A global subroutine that calls itself ten times: 10^10 calls within the depth limit of
/// ten. The glyph stops at its instruction budget instead.
#[test]
fn branching_subroutines_stop_at_the_instruction_budget() {
// `-107 callgsubr` (bias 107) is subroutine 0.
let call = [32u8, 29];
let mut subr = call.repeat(10);
subr.push(11);
let mut cs = call.to_vec();
cs.push(14);
let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", cs)], &[subr]);
let cff = Cff::parse(&font).unwrap();
let (p, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();
assert!(p.elements().is_empty());
}
/// The instruction budget is one per glyph: a seac whose base glyph spends all of it leaves
/// nothing for the accent, instead of each component getting a fresh budget.
#[test]
fn seac_components_share_the_instruction_budget() {
let call = [32u8, 29];
let mut subr = call.repeat(10);
subr.push(11);
// A square, then the branching subroutine that exhausts the budget.
let mut a = vec![139 + 100, 139 + 100, 139 - 100, 6];
a.extend(call);
a.push(14);
// `adx ady bchar achar endchar` with both components 'A' (standard code 65).
let seac = vec![139 + 10, 139 + 20, 139 + 65, 139 + 65, 14];
let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", a), ("Aacute", seac)], &[subr]);
let cff = Cff::parse(&font).unwrap();
let (alone, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap();
let (both, _) = cff.outline(cff.gid_by_name("Aacute").unwrap()).unwrap();
assert!(!alone.elements().is_empty());
assert_eq!(both.elements().len(), alone.elements().len(), "the accent gets no budget after the base spent it");
}
/// A CFF whose top DICT points its Private DICT at offsets near `usize::MAX`: `size` and `off`
/// come from the file, and reading the Private DICT must not overflow `off + size`.
#[test]
+16 -1
View File
@@ -211,6 +211,12 @@ fn utf16(b: &[u8]) -> String {
String::from_utf16_lossy(&u)
}
/// The most code → text mappings a CMap keeps (more than Unicode has code points), and the
/// longest destination string a `bfrange` is expanded with: a range of 65,536 codes would
/// otherwise multiply a long destination into gigabytes of text.
const MAX_UNICODE_MAPPINGS: usize = 1 << 20;
const MAX_RANGE_DST: usize = 256;
fn parse_cmap(data: &[u8]) -> CMap {
let mut m = CMap::default();
let mut lx = Lexer::new(data, 0);
@@ -239,6 +245,9 @@ fn parse_cmap(data: &[u8]) -> CMap {
}
"bfchar" => {
for p in a.as_chunks::<2>().0 {
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
break;
}
if let (Obj::Str(src), Obj::Str(dst)) = (&p[0], &p[1]) {
m.unicode.insert(be(src), utf16(dst));
}
@@ -252,9 +261,12 @@ fn parse_cmap(data: &[u8]) -> CMap {
continue;
}
match &p[2] {
Obj::Str(dst) => {
Obj::Str(dst) if dst.len() <= MAX_RANGE_DST => {
let mut d = dst.clone();
for c in lo..=hi {
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
break;
}
m.unicode.insert(c, utf16(&d));
if let Some(last) = d.last_mut() {
*last = last.wrapping_add(1);
@@ -263,6 +275,9 @@ fn parse_cmap(data: &[u8]) -> CMap {
}
Obj::Array(list) => {
for (k, s) in list.iter().enumerate() {
if m.unicode.len() >= MAX_UNICODE_MAPPINGS {
break;
}
if let (Obj::Str(s), Some(c)) = (s, lo.checked_add(k as u32)) {
m.unicode.insert(c, utf16(s));
}
+36 -5
View File
@@ -148,8 +148,9 @@ impl Type1 {
};
if let Some(p) = find(&priv_, b"/Subrs", 0) {
let mut q = p + 6;
// The section ends at `/CharStrings`, found once rather than for every entry.
let lim = find(&priv_, b"/CharStrings", q).unwrap_or(priv_.len());
while let Some(d) = find(&priv_, b"dup", q) {
let lim = find(&priv_, b"/CharStrings", q).unwrap_or(priv_.len());
if d > lim {
break;
}
@@ -205,10 +206,11 @@ impl Type1 {
/// A glyph's outline in glyph space and its advance width.
pub fn outline(&self, name: &str) -> Option<(BezPath, f64)> {
self.outline_depth(name, 0)
self.outline_depth(name, 0, &mut { crate::cff::MAX_GLYPH_OPS })
}
fn outline_depth(&self, name: &str, depth: usize) -> Option<(BezPath, f64)> {
/// `ops` is the glyph's instruction budget, shared by the base glyph and its seac components.
fn outline_depth(&self, name: &str, depth: usize, ops: &mut usize) -> Option<(BezPath, f64)> {
let cs = self.charstrings.get(name)?;
let mut st = T1 {
font: self,
@@ -223,9 +225,11 @@ impl Type1 {
flex: None,
done: false,
depth: 0,
ops: *ops,
seac: None,
};
st.run(cs);
*ops = st.ops;
if st.open {
st.path.close_path();
}
@@ -234,10 +238,10 @@ impl Type1 {
&& depth < 2
{
let name_of = |c: u8| crate::encoding::Base::Standard.name(c);
if let Some((bp, _)) = name_of(b).and_then(|n| self.outline_depth(&n, depth + 1)) {
if let Some((bp, _)) = name_of(b).and_then(|n| self.outline_depth(&n, depth + 1, ops)) {
path.extend(bp);
}
if let Some((ap, _)) = name_of(a).and_then(|n| self.outline_depth(&n, depth + 1)) {
if let Some((ap, _)) = name_of(a).and_then(|n| self.outline_depth(&n, depth + 1, ops)) {
// The accent's origin: its own sidebearing is replaced by `asb`.
path.extend(kurbo::Affine::translate((adx - asb + st.sbx, ady)) * ap);
}
@@ -270,6 +274,8 @@ struct T1<'a> {
flex: Option<Vec<(f64, f64)>>,
done: bool,
depth: usize,
/// Instructions this glyph may still run, across its subroutine calls.
ops: usize,
seac: Option<(f64, f64, f64, u8, u8)>,
}
@@ -314,6 +320,12 @@ impl T1<'_> {
}
let mut i = 0;
while i < cs.len() && !self.done {
// Subroutines that branch would multiply the work: the budget is shared.
let Some(left) = self.ops.checked_sub(1) else {
self.done = true;
return;
};
self.ops = left;
let b0 = cs[i];
i += 1;
let s = |st: &Self, k: usize| st.stack.get(k).copied().unwrap_or(0.0);
@@ -533,6 +545,25 @@ mod tests {
assert_eq!(p.bounding_box(), kurbo::Rect::new(50.0, 0.0, 550.0, 500.0));
}
/// A subroutine that calls itself ten times: 10^10 calls within the depth limit of ten. The
/// glyph stops at its instruction budget instead.
#[test]
fn branching_subroutines_stop_at_the_instruction_budget() {
let mut out = b"%!PS-AdobeFont-1.0: Test 001\n/FontMatrix [0.001 0 0 0.001 0 0] readonly def\ncurrentfile eexec\n".to_vec();
// `0 callsubr` is `139 10`; `return` is 11, `endchar` 14 (lenIV -1: charstrings in clear).
let mut subr = [139u8, 10].repeat(10);
subr.push(11);
let mut priv_ = b"dup /Private 8 dict dup begin /lenIV -1 def\n/Subrs 1 array\n".to_vec();
priv_.extend_from_slice(format!("dup 0 {} RD ", subr.len()).as_bytes());
priv_.extend_from_slice(&subr);
priv_.extend_from_slice(b" NP\nend\n/CharStrings 1 dict dup begin\n/A 3 RD \x8b\x0a\x0e ND\nend\nend\nmark currentfile closefile\n");
out.extend(encrypt(&priv_, EEXEC_R, 4));
out.extend_from_slice(b"\n0000000000000000\ncleartomark\n");
let t = Type1::parse(&out).unwrap();
let (p, _) = t.outline("A").unwrap();
assert!(p.elements().is_empty());
}
/// A `/Subrs` entry whose `RD` length is `usize::MAX`: the bytes are not in the file, and
/// `q + n` must not overflow.
#[test]
+46 -3
View File
@@ -779,6 +779,27 @@ pub struct Crop {
pub right: i32,
}
/// The largest side of a frame an output module writes (Resize is capped there too).
pub const MAX_OUTPUT_SIDE: i32 = 30_000;
/// Crop edges `a`/`b` of one axis of `side` pixels: padding is taken back only as far as keeps
/// the result within [`MAX_OUTPUT_SIDE`], so a mixed pair that stays inside the cap is kept as
/// saved. Each edge is also bounded (what the larger of the side and the cap allows), so the
/// arithmetic on them cannot overflow.
fn bound_edges(a: i32, b: i32, side: i32) -> (i32, i32) {
let hi = side.max(MAX_OUTPUT_SIDE);
let (mut a, mut b) = (a.clamp(-MAX_OUTPUT_SIDE, hi), b.clamp(-MAX_OUTPUT_SIDE, hi));
let excess = i64::from(side) - i64::from(a) - i64::from(b) - i64::from(MAX_OUTPUT_SIDE);
if excess > 0 {
// Only padding (negative edges) grows the side: take back as much of it as needed.
let excess = i32::try_from(excess).unwrap_or(i32::MAX);
let from_a = excess.min(-a.min(0));
a += from_a;
b += (excess - from_a).min(-b.min(0));
}
(a, b)
}
impl Crop {
/// Edges `(top, left, bottom, right)` in pixels of a `w`×`h` frame rendered at `scale`.
pub fn edges(&self, w: u32, h: u32, scale: f64) -> (i32, i32, i32, i32) {
@@ -801,7 +822,12 @@ impl Crop {
let b = conv((y + rh) * scale, hi).max(t);
return (t, l, hi - b, wi - r);
}
(self.top, self.left, self.bottom, self.right)
// Negative edges pad the frame: keep each padded side within the largest frame an
// output module writes, or a few saved integers ask for a canvas of gigapixels.
let (wi, hi) = (i32::try_from(w).unwrap_or(i32::MAX), i32::try_from(h).unwrap_or(i32::MAX));
let (t, b) = bound_edges(self.top, self.bottom, hi);
let (l, r) = bound_edges(self.left, self.right, wi);
(t, l, b, r)
}
}
@@ -1271,8 +1297,8 @@ impl OutputModule {
// Width rules; height follows the cropped frame's aspect.
rh = ((rw as f64 * ch as f64 / cw as f64).round() as u32).max(1);
}
rw = rw.min(30_000);
rh = rh.min(30_000);
rw = rw.min(MAX_OUTPUT_SIDE as u32);
rh = rh.min(MAX_OUTPUT_SIDE as u32);
(rw, rh)
}
/// The written frame size of an item (Render Settings resolution, Crop, Resize, codec
@@ -1692,6 +1718,23 @@ pub fn sanitize(name: &str) -> String {
#[cfg(test)]
mod tests {
#[test]
fn crop_padding_stays_within_the_largest_output_frame() {
use super::*;
let crop = |top, left, bottom, right| Crop { enabled: true, top, left, bottom, right, ..Default::default() };
let m = |c: Crop| OutputModule { crop: c, ..Default::default() };
// Saved edges padding by billions of pixels: the frame stops at the cap.
assert_eq!(m(crop(i32::MIN, i32::MIN, i32::MIN, i32::MIN)).frame_size(100, 50, 1.0), (30_000, 30_000));
assert_eq!(m(crop(-20_000, 0, -20_000, -1)).frame_size(100, 50, 1.0), (101, 30_000));
// Ordinary crops and pads are unchanged.
assert_eq!(crop(1, -2, 3, 4).edges(100, 50, 1.0), (1, -2, 3, 4));
// A mixed pair beyond the side that still ends within the cap is kept as saved (50 px).
assert_eq!(crop(0, 150, 0, -100).edges(100, 50, 1.0), (0, 150, 0, -100));
assert_eq!(m(crop(0, 150, 0, -100)).frame_size(100, 50, 1.0), (50, 50));
assert_eq!(m(crop(10, 10, 10, 10)).frame_size(100, 50, 1.0), (80, 30));
assert_eq!(m(crop(i32::MAX, 0, 0, 0)).frame_size(100, 50, 1.0), (100, 1));
}
use super::*;
#[test]
+107 -11
View File
@@ -166,6 +166,9 @@ pub fn gaussian_is_exact(sigma_x: f64, sigma_y: f64) -> bool {
sigma_x.max(sigma_y) < EXACT_SIGMA_BELOW
}
/// Pixels per tile of [`gauss_pass`]'s vertical pass.
const TILE: usize = 128;
/// One separable pass of a sampled Gaussian (radius 3 sigma) along x or y.
fn gauss_pass(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image {
// Below 1e-3 the kernel is the identity in f64; tiny sigmas would also make 2σ² underflow to 0 (NaN taps).
@@ -176,20 +179,65 @@ fn gauss_pass(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image {
let mut k: Vec<f64> = (-r..=r).map(|i| (-((i * i) as f64) / (2.0 * sigma * sigma)).exp()).collect();
let total: f64 = k.iter().sum();
k.iter_mut().for_each(|v| *v /= total);
let w = img.width as usize;
let (w, h) = (img.width as usize, img.height as usize);
let mut out = Image::new(img.width, img.height);
out.rows_mut().for_each(|(y, row)| {
for x in 0..w {
let mut acc = [0.0f64; 4];
for (i, wt) in k.iter().enumerate() {
let d = i as i64 - r;
let (sx, sy) = if vertical { (x as i64, y as i64 + d) } else { (x as i64 + d, y as i64) };
let p = if repeat { img.get_clamped(sx, sy) } else { img.get(sx, sy) };
for c in 0..4 {
acc[c] += p[c] as f64 * wt;
// Every output pixel adds its taps in kernel order, in f64, as a per-pixel loop would: the
// result is the same to the bit. Taps outside a transparent edge are skipped (adding `+0.0`
// leaves a sum that started at `+0.0` unchanged).
if vertical {
// Row by row: each output row adds whole input rows (contiguous, vectorises), instead of
// walking a column per pixel (a strided read per tap).
out.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
// 128 pixels at a time: the f64 sums (4 KB) stay in L1 across the taps.
for (t, out_tile) in row.chunks_mut(TILE).enumerate() {
let x0 = t * TILE;
let mut acc = [[0.0f64; 4]; TILE];
for (i, &wt) in k.iter().enumerate() {
let sy = y as i64 + i as i64 - r;
let sy = if repeat {
sy.clamp(0, h as i64 - 1)
} else if sy < 0 || sy >= h as i64 {
continue;
} else {
sy
};
let start = sy as usize * w + x0;
let Some(src) = img.data.get(start..start + out_tile.len()) else { continue };
for (a, p) in acc.iter_mut().zip(src) {
for c in 0..4 {
a[c] += p[c] as f64 * wt;
}
}
}
for (o, a) in out_tile.iter_mut().zip(acc.iter()) {
*o = a.map(|v| v as f32);
}
}
row[x] = acc.map(|v| v as f32);
});
return out;
}
let n = k.len();
out.data.par_chunks_mut(w).zip(img.data.par_chunks(w)).for_each(|(row, src)| {
for (x, o) in row.iter_mut().enumerate() {
let mut acc = [0.0f64; 4];
// Inside the row: the taps are one slice (no per-tap edge checks).
if let Some(taps) = x.checked_sub(r as usize).and_then(|x0| src.get(x0..x0 + n)) {
for (p, wt) in taps.iter().zip(&k) {
for c in 0..4 {
acc[c] += p[c] as f64 * wt;
}
}
} else {
for (i, wt) in k.iter().enumerate() {
let sx = x as i64 + i as i64 - r;
let at = if repeat { Some(sx.clamp(0, w as i64 - 1)) } else { Some(sx) };
let Some(p) = at.and_then(|i| usize::try_from(i).ok()).and_then(|i| src.get(i)) else { continue };
for c in 0..4 {
acc[c] += p[c] as f64 * wt;
}
}
}
*o = acc.map(|v| v as f32);
}
});
out
@@ -264,6 +312,54 @@ pub fn radial_blur(img: &Image, center: (f64, f64), amount: f64, zoom: bool) ->
mod tests {
use super::*;
/// The sampled Gaussian pass as it was (one pixel at a time, bounds-checked taps): the
/// row-wise passes must match it to the bit.
fn gauss_pass_per_pixel(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image {
let r = (sigma * 3.0).ceil() as i64;
let mut k: Vec<f64> = (-r..=r).map(|i| (-((i * i) as f64) / (2.0 * sigma * sigma)).exp()).collect();
let total: f64 = k.iter().sum();
k.iter_mut().for_each(|v| *v /= total);
let mut out = Image::new(img.width, img.height);
for y in 0..img.height as i64 {
for x in 0..img.width as i64 {
let mut acc = [0.0f64; 4];
for (i, wt) in k.iter().enumerate() {
let d = i as i64 - r;
let (sx, sy) = if vertical { (x, y + d) } else { (x + d, y) };
let p = if repeat { img.get_clamped(sx, sy) } else { img.get(sx, sy) };
for c in 0..4 {
acc[c] += p[c] as f64 * wt;
}
}
out.set(x as u32, y as u32, acc.map(|v| v as f32));
}
}
out
}
#[test]
fn gauss_pass_is_bit_identical_to_the_per_pixel_kernel() {
// Sizes narrower and shorter than the kernel too (every tap near an edge), one wider
// than three tiles (tiles past the first, and a partial last one), with negative, zero
// and HDR values.
for (w, h) in [(37, 23), (3, 2), (1, 9), (64, 1), (300, 5)] {
let mut img = Image::new(w, h);
for (i, p) in img.data.iter_mut().enumerate() {
let v = ((i * 7919) % 101) as f32 / 37.0 - 0.6;
*p = [v, -v * 0.5, if i % 5 == 0 { 0.0 } else { v * 3.0 }, (v + 0.6).min(1.0)];
}
for sigma in [0.4, 1.3, 2.9] {
for vertical in [false, true] {
for repeat in [false, true] {
let (a, b) = (gauss_pass(&img, sigma, vertical, repeat), gauss_pass_per_pixel(&img, sigma, vertical, repeat));
let bits = |i: &Image| i.data.iter().flat_map(|p| p.map(f32::to_bits)).collect::<Vec<_>>();
assert!(bits(&a) == bits(&b), "{w}×{h} sigma {sigma} vertical {vertical} repeat {repeat}");
}
}
}
}
}
#[test]
fn gaussian_preserves_mass() {
let mut img = Image::new(64, 64);
+9 -1
View File
@@ -118,7 +118,8 @@ pub fn block_flow(a: &Image, b: &Image, block: u32, radius: i32) -> Flow {
/// (Timewarp's Vector Detail and Block Size).
pub fn block_flow_window(a: &Image, b: &Image, tile: u32, window: u32, radius: i32) -> Flow {
let bs = tile.max(2) as usize;
let win = window.max(4) as i64;
// The matching cost per vector grows with the window's area; 512 is far above any block size the effects offer.
let win = i64::from(window.clamp(4, 512));
// Window origin relative to the tile origin.
let wo = bs as i64 / 2 - win / 2;
let (w, h) = (a.width as usize, a.height as usize);
@@ -295,6 +296,13 @@ mod tests {
s / n as f32
}
#[test]
fn a_huge_matching_window_is_held_to_the_cap() {
let (a, b) = (texture(520, 520, 0.0), texture(520, 520, 2.0));
let capped = block_flow_window(&a, &b, 260, 512, 2);
assert_eq!(block_flow_window(&a, &b, 260, u32::MAX, 2).v, capped.v);
}
#[test]
fn flow_finds_translation() {
let a = texture(128, 96, 0.0);
+42 -4
View File
@@ -31,15 +31,36 @@ pub const MAX_PADDED_SIDE: u32 = 30_000;
/// Clamp (colour ≤ alpha ≤ 1, NaN → 0) and round half up to `levels` steps (`inv` = 1 / levels):
/// one pixel of an 8/16 bpc buffer. Values are non-negative after the clamp, so the integer
/// conversion rounds like `round()` and vectorises.
#[allow(clippy::manual_clamp)] // max/min map NaN to 0; clamp would keep NaN
#[inline(always)]
pub fn quantize_px(p: &mut Px, levels: f32, inv: f32) {
let a = round_half_up(p[3].max(0.0).min(1.0) * levels) * inv;
*p = quantize_levels(*p, levels, inv).map(|n| n * inv);
}
/// [`quantize_px`]'s steps: the whole number of `levels` each channel rounds to (0..=levels).
#[allow(clippy::manual_clamp)] // max/min map NaN to 0; clamp would keep NaN
#[inline(always)]
pub fn quantize_levels(p: Px, levels: f32, inv: f32) -> [f32; 4] {
let na = round_half_up(p[3].max(0.0).min(1.0) * levels);
let a = na * inv;
let hi = [a, a, a, a];
let mut n = [0.0, 0.0, 0.0, na];
for c in 0..3 {
p[c] = round_half_up(p[c].max(0.0).min(hi[c]) * levels) * inv;
n[c] = round_half_up(p[c].max(0.0).min(hi[c]) * levels);
}
p[3] = a;
n
}
/// [`quantize_px`] at 8 bpc, as bytes: [`expand_u8`] gives back exactly the pixel
/// [`quantize_px`] writes, so an 8 bpc buffer can be kept in a quarter of the memory.
#[inline(always)]
pub fn quantize_px_u8(p: Px) -> [u8; 4] {
quantize_levels(p, 255.0, 1.0 / 255.0).map(|n| n as u8)
}
/// The 8 bpc pixel of [`quantize_px_u8`]'s bytes.
#[inline(always)]
pub fn expand_u8(n: [u8; 4]) -> Px {
n.map(|v| f32::from(v) * (1.0 / 255.0))
}
/// `v` (0..=65 535) rounded half up. The same either way; the fastest on each target: x86-64's
@@ -403,6 +424,23 @@ impl Mask {
mod tests {
use super::*;
/// Pixels kept as `quantize_px_u8` bytes expand to the 8 bpc pixel `quantize_px` writes, bit
/// for bit: every level, exact halves, out-of-range, negative, NaN and premultiplied colour
/// brighter than its alpha.
#[test]
fn u8_levels_expand_to_the_quantised_pixel() {
let mut values: Vec<f32> = (0..=255 * 4).map(|i| i as f32 / (255.0 * 4.0)).collect();
values.extend([-0.0, -1.0, 1.0, 1.5, f32::NAN, f32::INFINITY, 0.5 / 255.0, 254.5 / 255.0]);
for &a in &[1.0, 0.5, 0.2, 0.0, 2.0, f32::NAN] {
for &v in &values {
let p = [v, 1.0 - v, v * 0.5, a];
let mut want = p;
quantize_px(&mut want, 255.0, 1.0 / 255.0);
assert_eq!(expand_u8(quantize_px_u8(p)).map(f32::to_bits), want.map(f32::to_bits), "{p:?}");
}
}
}
#[test]
fn dither_keeps_exact_codes_and_averages_the_rest() {
// A value on an 8-bit code is that code at every position.
+139 -14
View File
@@ -10,6 +10,13 @@
//! (its first frame, with layer cache misses and texture uploads, is not counted; the fastest of
//! the next ones starts the average), and the slower side is re-measured every
//! [`AutoPick::REPROBE`] frames so the choice follows the comp as it changes.
//!
//! Frames render several at a time (a preview's lanes), and a frame is only measured once it
//! is done: so at most one GPU warm-up frame, or re-probe frame on either side, runs at once,
//! and the frames asked for meanwhile go to the other side. And a frame's time counts per
//! frame running on its side at once: twelve CPU frames that each take 200 ms side by side cost
//! about 17 ms a frame, against a GPU frame of 175 ms on its own. Otherwise every lane of a starting preview took a
//! warm-up frame on a GPU that turned out 2–4× slower (an integrated GPU), all at once.
use std::collections::HashMap;
use std::sync::Mutex;
@@ -51,7 +58,36 @@ pub struct AutoStats {
/// accelerator, see [`Accelerator::auto_pick`](crate::Accelerator::auto_pick)).
#[derive(Default)]
pub struct AutoPick {
stats: Mutex<HashMap<AutoKey, AutoStats>>,
stats: Mutex<HashMap<AutoKey, Entry>>,
}
/// One key's history, when its running warm-up or re-probe frame started on each side, and
/// how many of its frames are running on each side (`[CPU, GPU]`).
#[derive(Default)]
struct Entry {
stats: AutoStats,
measuring: [Option<web_time::Instant>; 2],
// Lowered by `record`, `declined` and `abandoned`: every frame asked for ends in one (an
// [`AutoTicket`] makes sure of it).
running: [u32; 2],
}
impl Entry {
/// A warm-up or re-probe frame is running on `gpu`'s side. It counts as done once
/// [`AutoPick::MEASURING_FOR`] has passed (a frame that failed without reporting back).
fn measuring(&self, gpu: bool) -> bool {
self.measuring[usize::from(gpu)].is_some_and(|t| t.elapsed() < AutoPick::MEASURING_FOR)
}
/// Render the next warm-up or re-probe frame on `gpu`'s side, unless one is running there:
/// then the other side's (`!gpu`).
fn measure(&mut self, gpu: bool) -> bool {
if self.measuring(gpu) {
return !gpu;
}
self.measuring[usize::from(gpu)] = Some(web_time::Instant::now());
gpu
}
}
impl AutoPick {
@@ -66,19 +102,43 @@ impl AutoPick {
pub const MARGIN: f64 = 0.85;
/// Keys remembered before the history is reset.
const MAX_KEYS: usize = 1024;
/// Frames counted as running on one side at most (see `Entry::running`).
const MAX_RUNNING: u32 = 256;
/// A warm-up or re-probe frame not reported back by then no longer holds its side.
const MEASURING_FOR: std::time::Duration = std::time::Duration::from_secs(10);
/// Render the frame for `key` on the GPU?
/// [`AutoPick::choose`] for a frame that ends where it started: the ticket reports back, and
/// a ticket dropped without a time (an error, a retry elsewhere, a panic, a cancelled future)
/// ends the frame with [`AutoPick::abandoned`].
pub fn ticket(&self, key: AutoKey) -> AutoTicket<'_> {
AutoTicket { pick: self, key, gpu: self.choose(key), open: true }
}
/// Render the frame for `key` on the GPU? Every frame asked for reports back with
/// [`AutoPick::record`] (or [`AutoPick::declined`]), or ends with [`AutoPick::abandoned`]
/// ([`AutoPick::ticket`] does that on its own).
pub fn choose(&self, key: AutoKey) -> bool {
let Ok(mut m) = self.stats.lock() else { return true };
if m.len() > Self::MAX_KEYS && !m.contains_key(&key) {
m.clear();
}
let s = m.entry(key).or_default();
// Warm up both sides, alternating, GPU first.
let e = m.entry(key).or_default();
let gpu = Self::pick(e);
let n = &mut e.running[usize::from(gpu)];
*n = n.saturating_add(1).min(Self::MAX_RUNNING);
gpu
}
fn pick(e: &mut Entry) -> bool {
let s = e.stats;
// Warm up both sides, alternating, CPU first: the first frames asked for are the
// ones on screen, and the CPU is the side that is never slow to start (no pipelines
// to build; an integrated GPU took 2 s for the frame at the playhead).
let gpu_warm = s.declined || s.gpu_frames >= Self::WARMUP;
if !gpu_warm && s.gpu_frames <= s.cpu_frames {
return true;
if !gpu_warm && s.gpu_frames < s.cpu_frames {
return e.measure(true);
}
// (CPU frames already render side by side, on lanes of their own.)
if s.cpu_frames < Self::WARMUP {
return false;
}
@@ -86,12 +146,12 @@ impl AutoPick {
(Some(g), Some(c)) => c >= g * Self::MARGIN,
(g, _) => g.is_some(),
};
// Periodically re-measure the slower side.
if s.since_probe >= Self::REPROBE {
s.since_probe = 0;
return !gpu_faster;
// Periodically re-measure the slower side (one frame at a time).
if s.since_probe >= Self::REPROBE && !e.measuring(!gpu_faster) {
e.stats.since_probe = 0;
return e.measure(!gpu_faster);
}
s.since_probe += 1;
e.stats.since_probe = s.since_probe.saturating_add(1);
gpu_faster
}
@@ -103,7 +163,13 @@ impl AutoPick {
/// period).
pub fn record(&self, key: AutoKey, gpu: bool, ms: f64) {
let Ok(mut m) = self.stats.lock() else { return };
let s = m.entry(key).or_default();
let e = m.entry(key).or_default();
e.measuring[usize::from(gpu)] = None;
// Per frame running on this side alongside it (itself included).
let n = &mut e.running[usize::from(gpu)];
let ms = ms / f64::from((*n).max(1));
*n = n.saturating_sub(1);
let s = &mut e.stats;
let (avg, frames) = if gpu { (&mut s.gpu_ms, &mut s.gpu_frames) } else { (&mut s.cpu_ms, &mut s.cpu_frames) };
*frames = frames.saturating_add(1);
if gpu {
@@ -123,16 +189,36 @@ impl AutoPick {
/// The GPU did not handle a frame for `key` (it rendered on the CPU instead).
pub fn declined(&self, key: AutoKey) {
if let Ok(mut m) = self.stats.lock() {
let s = m.entry(key).or_default();
let e = m.entry(key).or_default();
e.measuring[1] = None;
// The frame renders on the CPU instead (and reports back from there).
e.running[1] = e.running[1].saturating_sub(1);
e.running[0] = e.running[0].saturating_add(1).min(Self::MAX_RUNNING);
let s = &mut e.stats;
s.declined = true;
s.gpu_ms = None;
s.since_probe = 0;
}
}
/// A frame for `key` asked of `gpu`'s side ends without a time (it failed, or was replaced
/// before it finished). Without this its side stays counted as running, and every later
/// frame there is timed as one of several running side by side: that side looks cheaper.
// ponytail: clears the side's warm-up/probe mark even when another frame holds it; that
// only lets the next probe start early.
pub fn abandoned(&self, key: AutoKey, gpu: bool) {
if let Ok(mut m) = self.stats.lock()
&& let Some(e) = m.get_mut(&key)
{
e.measuring[usize::from(gpu)] = None;
let n = &mut e.running[usize::from(gpu)];
*n = n.saturating_sub(1);
}
}
/// The history of `key`.
pub fn stats(&self, key: AutoKey) -> Option<AutoStats> {
self.stats.lock().ok()?.get(&key).copied()
self.stats.lock().ok()?.get(&key).map(|e| e.stats)
}
/// Forget all history (settings that change both paths' costs).
@@ -142,3 +228,42 @@ impl AutoPick {
}
}
}
/// One frame [`AutoPick`] chose a side for ([`AutoPick::ticket`]): it reports the frame's time
/// once, and ends the frame unmeasured when dropped without one.
#[must_use = "a ticket dropped at once ends its frame unmeasured"]
pub struct AutoTicket<'a> {
pick: &'a AutoPick,
key: AutoKey,
gpu: bool,
open: bool,
}
impl AutoTicket<'_> {
/// The frame renders on the GPU.
pub fn gpu(&self) -> bool {
self.gpu
}
/// The frame took `ms` on its side.
pub fn record(mut self, ms: f64) {
self.open = false;
self.pick.record(self.key, self.gpu, ms);
}
/// The GPU did not take the frame: it renders on the CPU, and the ticket reports from there.
pub fn declined(&mut self) {
if self.gpu {
self.pick.declined(self.key);
self.gpu = false;
}
}
}
impl Drop for AutoTicket<'_> {
fn drop(&mut self) {
if self.open {
self.pick.abandoned(self.key, self.gpu);
}
}
}
+6 -5
View File
@@ -361,10 +361,11 @@ pub trait LayerStore: Send + Sync {
/// mid-render). An entry whose files changed, or damaged, is a miss and is dropped, so the
/// buffer rendered again replaces it. `virtual_id` names files served from memory.
fn get_layer(&self, key: u128, virtual_id: Option<VirtualFileId<'_>>) -> Option<(Arc<Buf>, ReadFiles)>;
/// Keep a buffer that was slow to render, with the source files it was made of. `since`: the
/// Keep a buffer that was slow to render, with the source files it was made of (shared, so
/// a store can encode it off the render thread). `since`: the
/// [`LayerStore::drop_generation`] when its render started (a buffer made of a file dropped
/// after that is refused).
fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64);
fn put_layer(&self, key: u128, buf: &Arc<Buf>, files: &EntryFiles, since: u64);
/// How many times files were dropped from the store (File ▸ Reload Footage,
/// [`crate::disk_cache::DiskCache::drop_files`]); `0` for a store that never drops.
fn drop_generation(&self) -> u64 {
@@ -540,7 +541,7 @@ impl LayerStore for PrefetchStore {
}
/// Queues the layer for writing to the persistent store the first time `key` is seen.
fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, _since: u64) {
fn put_layer(&self, key: u128, buf: &Arc<Buf>, files: &EntryFiles, _since: u64) {
let Ok(mut s) = self.st.lock() else { return };
if s.known.insert(key) {
s.writes.push((key, crate::disk_cache::layer_entry(buf, files)));
@@ -983,7 +984,7 @@ mod shared_insert_tests {
assert!(store.get_layer(5, Some(&two)).is_none());
assert!(!store.has(5));
assert_eq!(store.take_misses(), [5]);
store.put_layer(5, &buf(), &EntryFiles::of(&files, Some(&two)).unwrap(), 0);
store.put_layer(5, &Arc::new(buf()), &EntryFiles::of(&files, Some(&two)).unwrap(), 0);
assert_eq!(store.take_writes().len(), 1, "written over the changed entry");
// Through the layer cache: a hit reports its files; incomplete lists are not queued.
let cache = LayerCache::default();
@@ -1065,7 +1066,7 @@ mod shared_insert_tests {
fn get_layer(&self, key: u128, virtual_id: Option<crate::disk_cache::VirtualFileId<'_>>) -> Option<(Arc<Buf>, crate::reads::ReadFiles)> {
super::LayerStore::get_layer(&*self.dc, key, virtual_id)
}
fn put_layer(&self, key: u128, buf: &Buf, files: &crate::disk_cache::EntryFiles, since: u64) {
fn put_layer(&self, key: u128, buf: &Arc<Buf>, files: &crate::disk_cache::EntryFiles, since: u64) {
self.arrived.wait();
self.go_on.wait();
super::LayerStore::put_layer(&*self.dc, key, buf, files, since);
+61 -8
View File
@@ -153,6 +153,26 @@ impl Pipe {
p
}
/// A copy of `src` converted by `c` and quantised to the bit depth, in one pass: one read and
/// one write of the frame instead of a copy, a conversion and a quantise pass (a 1080p float
/// frame is 33 MB, and these passes are limited by memory speed). The same pixels as
/// [`convert`] then [`Pipe::quantize`].
pub fn copy_converted(&self, src: &Image, c: Option<&Conversion>) -> Image {
let levels = self.levels;
let data = src
.data
.par_iter()
.map(|&p| {
let mut p = c.map_or(p, |c| convert_px(p, c));
if let Some(l) = levels {
quantize_px(&mut p, l, 1.0 / l);
}
p
})
.collect();
Image { width: src.width, height: src.height, data }
}
/// Clamp and quantise to the bit depth (no-op in 32 bpc).
pub fn quantize(&self, img: &mut Image) {
self.quantize_region(img, Region::Full);
@@ -265,14 +285,18 @@ fn quantize_rect(img: &mut Image, r: PxRect, levels: f32) {
/// Convert premultiplied pixels (on straight colour).
pub fn convert(img: &mut Image, c: &Conversion) {
img.data.par_iter_mut().for_each(|p| {
let a = p[3];
if a <= 0.0 {
return;
}
let o = c.apply([p[0] / a, p[1] / a, p[2] / a]);
*p = [o[0] * a, o[1] * a, o[2] * a, a];
});
img.data.par_iter_mut().for_each(|p| *p = convert_px(*p, c));
}
/// [`convert`] of one pixel.
#[inline]
fn convert_px(p: [f32; 4], c: &Conversion) -> [f32; 4] {
let a = p[3];
if a <= 0.0 {
return p;
}
let o = c.apply([p[0] / a, p[1] / a, p[2] / a]);
[o[0] * a, o[1] * a, o[2] * a, a]
}
/// Compand: an exponential knee from 80%, approaching 1.0 (linear light, per channel).
@@ -349,6 +373,35 @@ mod tests {
}
}
/// The fused footage pass gives the pixels of a copy, a conversion and a quantise pass, bit
/// for bit, at every depth, with and without a conversion.
#[test]
fn copy_converted_is_convert_then_quantize() {
let mut img = Image::new(17, 5);
for (i, p) in img.data.iter_mut().enumerate() {
*p = match i % 7 {
0 => [0.3, -0.2, 0.71, 0.9],
1 => [-0.0, 0.0, 0.0, 0.0],
2 => [1.7, 0.4, f32::NAN, 0.5],
3 => [0.0; 4],
_ => [i as f32 / 90.0, 0.5, 0.25, 1.0],
};
}
for depth in [effectcraft_project::BitDepth::Bpc8, effectcraft_project::BitDepth::Bpc16, effectcraft_project::BitDepth::Bpc32] {
let pipe = Pipe::of(&ProjectSettings { bit_depth: depth, ..Default::default() });
for c in [None, Some(Conversion::linearize(ColorSpace::Srgb))] {
let mut want = img.clone();
if let Some(c) = &c {
convert(&mut want, c);
}
pipe.quantize(&mut want);
let got = pipe.copy_converted(&img, c.as_ref());
let bits = |i: &Image| i.data.iter().map(|p| p.map(f32::to_bits)).collect::<Vec<_>>();
assert_eq!(bits(&got), bits(&want), "{depth:?} conversion {}", c.is_some());
}
}
}
/// Both forms `round_half_up` picks between agree on everything quantising feeds it:
/// 0..=levels, exact halves included.
#[test]
+130 -30
View File
@@ -58,6 +58,10 @@ const QUEUE_LIMIT: usize = 64;
/// frames, so a fully cached comp came back with gaps in its blue bar.
#[cfg(not(target_arch = "wasm32"))]
const FRAME_QUEUE_BYTES: usize = 1 << 30;
/// Pending layer and matte bytes before new ones are dropped: a layer job holds its float
/// buffer until the writer compresses it (64 of them at 4K were ~8.5 GB).
#[cfg(not(target_arch = "wasm32"))]
const LAYER_QUEUE_BYTES: usize = 1 << 30;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Kind {
@@ -264,18 +268,50 @@ impl LayerPrefetch {
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
enum Job {
/// Kind, key, header format byte, payload.
Write(Kind, u128, u8, Vec<u8>),
Write(Kind, u128, u8, Payload),
/// Delete the frame and layer entries made of these files ([`DiskCache::drop_files`]).
Drop(Vec<Arc<str>>),
}
/// What a queued write holds. 8-bit frames and layer buffers are encoded (LZ4) on the writer
/// thread, not on the render thread that queued them: encoding a 1080p frame took ~9 ms of every
/// frame's render, and a full queue no longer throws away encoding work.
enum Payload {
Encoded(Vec<u8>),
/// 8-bit premultiplied RGBA, `width * height * 4` bytes, and the files it was made of.
Frame(u32, u32, Vec<u8>, EntryFiles),
Layer(Arc<Buf>, EntryFiles),
}
impl Payload {
/// Bytes it holds (the frame queue's budget).
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
fn len(&self) -> usize {
match self {
Payload::Encoded(p) | Payload::Frame(_, _, p, _) => p.len(),
Payload::Layer(b, _) => b.img.data.len().saturating_mul(std::mem::size_of::<effectcraft_raster::Px>()),
}
}
/// The entry's payload: its source files, then its pixels.
fn encode(self) -> Vec<u8> {
match self {
Payload::Encoded(p) => p,
Payload::Frame(w, h, rgba, files) => with_files(&files, encode_frame(w, h, &rgba)),
Payload::Layer(b, files) => with_files(&files, encode_layer(&b)),
}
}
}
struct Queue {
/// Frame writes (taken first) and their payload bytes.
frames: std::collections::VecDeque<Job>,
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
frame_bytes: usize,
/// Layer and matte writes.
/// Layer and matte writes, and their payload bytes.
jobs: std::collections::VecDeque<Job>,
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
job_bytes: usize,
busy: bool,
/// Keys of frames queued or being written.
pending_frames: std::collections::HashSet<u128>,
@@ -330,7 +366,21 @@ impl Queue {
}
return Some(j);
}
self.jobs.pop_front()
let j = self.jobs.pop_front()?;
if let Job::Write(_, _, _, p) = &j {
self.job_bytes = self.job_bytes.saturating_sub(p.len());
}
Some(j)
}
/// No room for a `kind` write of `len` bytes. A layer bigger than the whole budget is still
/// taken when nothing else is queued.
#[cfg(not(target_arch = "wasm32"))]
fn full(&self, kind: Kind, len: usize) -> bool {
match kind {
Kind::Frame => self.frame_bytes.saturating_add(len) > FRAME_QUEUE_BYTES,
_ => self.jobs.len() >= QUEUE_LIMIT || (self.job_bytes > 0 && self.job_bytes.saturating_add(len) > LAYER_QUEUE_BYTES),
}
}
fn is_empty(&self) -> bool {
self.frames.is_empty() && self.jobs.is_empty()
@@ -462,6 +512,7 @@ impl DiskCache {
frames: Default::default(),
frame_bytes: 0,
jobs: Default::default(),
job_bytes: 0,
busy: cfg!(not(target_arch = "wasm32")),
pending_frames: Default::default(),
drops: Drops::default(),
@@ -650,6 +701,7 @@ impl DiskCache {
#[cfg(target_arch = "wasm32")]
{
q.jobs.clear();
q.job_bytes = 0;
q.frames.clear();
q.pending_frames.clear();
}
@@ -771,20 +823,20 @@ impl DiskCache {
/// Queue a write; `false` when it was dropped (a full queue).
fn enqueue(&self, kind: Kind, key: u128, payload: Vec<u8>) -> bool {
self.enqueue_format(kind, key, FORMAT_RGBA8, payload, None)
self.enqueue_format(kind, key, FORMAT_RGBA8, Payload::Encoded(payload), None)
}
/// [`DiskCache::enqueue`] with the entry's header format byte. `made`: the source files of a
/// frame or layer entry and the drop generation its reads started at
/// ([`DiskCache::drop_generation`]); refused (`false`) when one of them was dropped since.
fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Vec<u8>, made: Option<(&EntryFiles, u64)>) -> bool {
fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Payload, made: Option<(&EntryFiles, u64)>) -> bool {
let stale = |q: &Queue| made.is_some_and(|(files, since)| q.drops.stale(files.files().iter().map(|f| &f.path), since));
#[cfg(target_arch = "wasm32")]
{
if self.queue.0.lock().is_ok_and(|q| stale(&q)) {
return false;
}
self.write_format(kind, key, format, &payload);
self.write_format(kind, key, format, &payload.encode());
true
}
#[cfg(not(target_arch = "wasm32"))]
@@ -794,11 +846,7 @@ impl DiskCache {
if stale(&q) {
return false;
}
let full = match kind {
Kind::Frame => q.frame_bytes.saturating_add(payload.len()) > FRAME_QUEUE_BYTES,
_ => q.jobs.len() >= QUEUE_LIMIT,
};
if full {
if q.full(kind, payload.len()) {
self.dropped.fetch_add(1, Ordering::Relaxed);
return false;
}
@@ -807,6 +855,7 @@ impl DiskCache {
q.frame_bytes = q.frame_bytes.saturating_add(payload.len());
q.frames.push_back(Job::Write(kind, key, format, payload));
} else {
q.job_bytes = q.job_bytes.saturating_add(payload.len());
q.jobs.push_back(Job::Write(kind, key, format, payload));
}
cv.notify_all();
@@ -1033,7 +1082,7 @@ impl DiskCache {
}
self.count_encode();
let payload = with_files(&files, encode_frame_f16(width, height, rgba));
self.enqueue_format(Kind::Frame, key, FORMAT_F16_FILES, payload, Some((&files, since)))
self.enqueue_format(Kind::Frame, key, FORMAT_F16_FILES, Payload::Encoded(payload), Some((&files, since)))
}
/// Write a half-float frame synchronously (tests); a list that can't be stored is not.
@@ -1050,6 +1099,22 @@ impl DiskCache {
/// render started at).
#[allow(clippy::too_many_arguments)]
pub fn put_frame(&self, key: u128, width: u32, height: u32, rgba: &[u8], files: &ReadFiles, virtual_id: Option<VirtualFileId<'_>>, since: u64) -> bool {
self.put_frame_owned(key, width, height, rgba.to_vec(), files, virtual_id, since)
}
/// [`DiskCache::put_frame`] of a frame the caller hands over (no copy; compressed on the
/// writer thread).
#[allow(clippy::too_many_arguments)]
pub fn put_frame_owned(
&self,
key: u128,
width: u32,
height: u32,
rgba: Vec<u8>,
files: &ReadFiles,
virtual_id: Option<VirtualFileId<'_>>,
since: u64,
) -> bool {
if rgba.len() as u64 != width as u64 * height as u64 * 4 {
return false;
}
@@ -1057,13 +1122,13 @@ impl DiskCache {
if self.contains(Kind::Frame, key) {
return true;
}
// (no room on the disk: refused before the pixels are compressed)
// (no room on the disk: refused before the pixels are queued and compressed)
if !self.accepts_writes() {
return false;
}
self.count_encode();
let payload = with_files(&files, encode_frame(width, height, rgba));
self.enqueue_format(Kind::Frame, key, FORMAT_RGBA8_FILES, payload, Some((&files, since)))
let made = files.clone();
self.enqueue_format(Kind::Frame, key, FORMAT_RGBA8_FILES, Payload::Frame(width, height, rgba, files), Some((&made, since)))
}
/// A layer buffer (key: [`layer_disk_key`]) whose source files are unchanged, with those
@@ -1078,13 +1143,14 @@ impl DiskCache {
/// [`EntryFiles::of`]: a buffer whose files can't all be named must not be stored). Refused
/// when one of them was dropped after `since`, the [`DiskCache::drop_generation`] its render
/// started at.
pub fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) {
// (no room on the disk: refused before the buffer is compressed)
/// The buffer is shared, and encoded on the writer thread.
pub fn put_layer(&self, key: u128, buf: &Arc<Buf>, files: &EntryFiles, since: u64) {
// (no room on the disk: refused before the buffer is queued and compressed)
if self.contains(Kind::Layer, key) || !self.accepts_writes() {
return;
}
self.count_encode();
let _ = self.enqueue_format(Kind::Layer, key, FORMAT_LAYER_FILES, with_files(files, encode_layer(buf)), Some((files, since)));
let _ = self.enqueue_format(Kind::Layer, key, FORMAT_LAYER_FILES, Payload::Layer(buf.clone(), files.clone()), Some((files, since)));
}
/// Write a layer buffer synchronously (tests); a list that can't be stored is not.
@@ -1192,7 +1258,7 @@ fn writer(dc: std::sync::Weak<DiskCache>, q: Arc<(Mutex<Queue>, Condvar)>) {
let Some(cache) = dc.upgrade() else { return };
let written = match job {
Job::Write(kind, key, format, payload) => {
cache.write_format(kind, key, format, &payload);
cache.write_format(kind, key, format, &payload.encode());
(kind == Kind::Frame).then_some(key)
}
Job::Drop(paths) => {
@@ -1292,7 +1358,7 @@ impl crate::cache::LayerStore for DiskCache {
DiskCache::get_layer(self, key, virtual_id).map(|(b, f)| (Arc::new(b), f))
}
/// Queues a layer write on the disk cache.
fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) {
fn put_layer(&self, key: u128, buf: &Arc<Buf>, files: &EntryFiles, since: u64) {
DiskCache::put_layer(self, key, buf, files, since);
}
/// The current drop generation of the disk cache.
@@ -1706,7 +1772,7 @@ mod tests {
let dc = DiskCache::open(&dir, 1 << 30).unwrap();
assert!(dc.get_layer(42, None).is_none());
let b = buf(1, 33, 17);
dc.put_layer(42, &b, &EntryFiles::default(), 0);
dc.put_layer(42, &Arc::new(b.clone()), &EntryFiles::default(), 0);
dc.flush();
let (back, files) = dc.get_layer(42, None).unwrap();
assert!(files.is_empty() && files.is_complete());
@@ -1715,10 +1781,14 @@ mod tests {
let rgba: Vec<u8> = (0..20 * 10 * 4).map(|i| (i % 251) as u8).collect();
assert!(dc.put_frame(7, 20, 10, &rgba, &ReadFiles::default(), None, 0));
assert!(!dc.put_frame(8, 2, 2, &[0; 3], &ReadFiles::default(), None, 0), "a wrong-size frame is refused");
// Handed over and compressed on the writer thread: the same pixels come back.
assert!(dc.put_frame_owned(9, 20, 10, rgba.clone(), &ReadFiles::default(), None, 0));
assert!(!dc.put_frame_owned(10, 2, 2, vec![0; 3], &ReadFiles::default(), None, 0), "a wrong-size frame is refused");
dc.flush();
assert_eq!(dc.get_frame(7, None).map(|f| f.0).unwrap().rgba, rgba);
assert_eq!(dc.get_frame(9, None).map(|f| f.0).unwrap().rgba, rgba);
let st = dc.stats();
assert_eq!((st.entries, st.hits, st.misses, st.writes), (2, 2, 1, 2));
assert_eq!((st.entries, st.hits, st.misses, st.writes), (3, 3, 1, 3));
// Wrong kind is a miss.
assert!(dc.get_frame(42, None).map(|f| f.0).is_none());
}
@@ -1773,6 +1843,36 @@ mod tests {
assert_ne!(comp_content_key(&p, cid), key, "the expression reads the model's name");
}
/// Layer jobs hold float buffers until they are compressed: the queue holds at most
/// [`LAYER_QUEUE_BYTES`] of them (64 at 4K held ~8.5 GB), and a single bigger one only alone.
#[test]
fn layer_writes_queue_within_a_byte_budget() {
let mut q = Queue {
frames: Default::default(),
frame_bytes: 0,
jobs: Default::default(),
job_bytes: 0,
busy: true,
pending_frames: Default::default(),
drops: Drops::default(),
};
let layer = LAYER_QUEUE_BYTES / 4 + 1;
for _ in 0..3 {
assert!(!q.full(Kind::Layer, layer));
q.jobs.push_back(Job::Write(Kind::Layer, 0, 0, Payload::Encoded(vec![])));
q.job_bytes += layer;
}
assert!(q.full(Kind::Layer, layer), "a fourth would pass the budget");
assert!(!q.full(Kind::Frame, 1024), "frames have their own budget");
let mut empty = Queue { jobs: Default::default(), job_bytes: 0, ..q };
assert!(!empty.full(Kind::Layer, 2 * LAYER_QUEUE_BYTES), "a single big layer, alone");
// Taking a job gives its bytes back.
empty.job_bytes = 3;
empty.jobs.push_back(Job::Write(Kind::Layer, 0, 0, Payload::Encoded(vec![0; 3])));
assert!(empty.pop().is_some());
assert_eq!(empty.job_bytes, 0);
}
/// A burst of layer writes never crowds out frames: every frame of a fully cached comp is
/// on disk afterwards (the blue bar came back with gaps).
#[test]
@@ -1780,7 +1880,7 @@ mod tests {
let dir = tmpdir("burst");
let dc = DiskCache::open(&dir, 1 << 30).unwrap();
for k in 0..300 {
dc.put_layer(10_000 + k, &buf(1, 64, 64), &EntryFiles::default(), 0);
dc.put_layer(10_000 + k, &Arc::new(buf(1, 64, 64)), &EntryFiles::default(), 0);
}
let rgba = vec![7u8; 32 * 32 * 4];
for k in 0..300 {
@@ -1950,7 +2050,7 @@ mod tests {
let rgba = vec![7u8; 16 * 16 * 4];
assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused");
assert!(!dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0), "refused");
dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0);
dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0);
dc.flush();
assert_eq!(dc.encodes(), 0, "nothing compressed");
assert_eq!(dc.stats().entries, 0);
@@ -1960,7 +2060,7 @@ mod tests {
assert_eq!(dc.stats().limit_bytes, 32 << 20);
assert!(!dc.accepts_writes());
assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused");
dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0);
dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0);
assert_eq!(dc.encodes(), 0, "nothing compressed");
// A small limit the setting asks for (not free space) is used.
dc.set_disk_space(None);
@@ -1971,7 +2071,7 @@ mod tests {
dc.set_disk_space(Some((4096 * GB, 500 * GB)));
assert!(dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0));
assert!(dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0));
dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0);
dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0);
dc.flush();
assert_eq!(dc.encodes(), 3);
assert_eq!(dc.stats().entries, 3);
@@ -2076,11 +2176,11 @@ mod tests {
assert!(dc.get_frame(k, Some(&two)).is_none() && !dc.contains(Kind::Frame, k), "replaced: a miss, deleted");
// A layer buffer made of it.
let lk = layer_disk_key(7);
dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0);
dc.put_layer(lk, &Arc::new(buf(1, 4, 4)), &EntryFiles::of(&files, Some(&one)).unwrap(), 0);
dc.flush();
assert!(dc.get_layer(lk, Some(&one)).is_some_and(|(b, f)| b.img == buf(1, 4, 4).img && f == files && f.is_complete()));
assert!(dc.get_layer(lk, None).is_none() && !dc.contains(Kind::Layer, lk), "no identity: a miss, deleted");
dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0);
dc.put_layer(lk, &Arc::new(buf(1, 4, 4)), &EntryFiles::of(&files, Some(&one)).unwrap(), 0);
dc.flush();
assert!(dc.get_layer(lk, Some(&two)).is_none() && !dc.contains(Kind::Layer, lk), "replaced: a miss, deleted");
// The browser's sealed layer entries carry the same list.
@@ -2324,7 +2424,7 @@ mod tests {
assert!(dc.dropped_since(&read(&[&a]), before) && !dc.dropped_since(&read(&[&b]), before));
assert!(!dc.put_frame(1, 1, 1, &[1; 4], &read(&[&a, &b]), None, before), "a frame from before the drop");
assert!(!dc.put_frame_f16(2, 1, 1, &[1; 4], &read(&[&a]), None, before));
dc.put_layer(3, &buf(3, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), before);
dc.put_layer(3, &Arc::new(buf(3, 2, 2)), &EntryFiles::of(&read(&[&a]), None).unwrap(), before);
assert!(dc.put_frame(4, 1, 1, &[4; 4], &read(&[&b]), None, before), "b.png wasn't dropped");
dc.flush();
assert!(!dc.contains(Kind::Frame, 1) && !dc.contains(Kind::Frame, 2) && !dc.contains(Kind::Layer, 3));
@@ -2334,7 +2434,7 @@ mod tests {
let after = dc.drop_generation();
assert!(after > before && !dc.dropped_since(&read(&[&a]), after));
assert!(dc.put_frame(5, 1, 1, &[5; 4], &read(&[&a]), None, after));
dc.put_layer(6, &buf(6, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), after);
dc.put_layer(6, &Arc::new(buf(6, 2, 2)), &EntryFiles::of(&read(&[&a]), None).unwrap(), after);
dc.flush();
assert_eq!(dc.get_frame(5, None).map(|f| f.0.rgba), Some(vec![5; 4]));
assert!(dc.get_layer(6, None).is_some());
+11
View File
@@ -37,6 +37,14 @@ pub trait FootageSource: Send + Sync {
None => self.frame(item, footage, t),
}
}
/// [`FootageSource::frame_with`]'s full-size frame as 8-bit levels: each pixel is
/// [`effectcraft_raster::quantize_px_u8`] of the float pixel `frame_with` would give, so
/// [`effectcraft_raster::expand_u8`] makes that frame quantised to 8 bpc, bit for bit, from a
/// quarter of the memory (an 8 bpc render of 8-bit footage). `None` when the source has no
/// cheaper way than the float frame; the renderer then takes that.
fn frame8(&self, _item: ItemId, _footage: &Footage, _t: Tick, _float: FloatCurve) -> Option<Arc<crate::disk_cache::Frame8>> {
None
}
/// `frames` interleaved stereo sample frames of `item`'s audio from source time `t` at
/// `rate` Hz (`None` when unavailable). The first sample is `t.to_units_floor(rate)`;
/// a sample boundary uses the earliest representable tick that selects that sample.
@@ -215,6 +223,9 @@ impl FootageSource for CancellableFootage<'_> {
fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
self.read(|| self.inner.frame_with(item, footage, t, size, float))
}
fn frame8(&self, item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option<Arc<crate::disk_cache::Frame8>> {
self.read(|| self.inner.frame8(item, footage, t, float))
}
}
/// Footage that can be rasterised at any scale: SVG, PDF, Illustrator and EPS files, and a
+1 -1
View File
@@ -26,7 +26,7 @@ pub mod three_d;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
pub use auto::{AutoKey, AutoPick};
pub use auto::{AutoKey, AutoPick, AutoTicket};
pub use cache::{CacheStats, LayerCache, LayerStore, PrefetchStore};
use effectcraft_color::BlendMode;
use effectcraft_effects::{Buf, EffectCtx, EffectEnv, EffectHost, LayerPixels, Params};
+6 -6
View File
@@ -86,10 +86,10 @@ pub async fn comp_frame(r: &Renderer<'_>, comp: ItemId, t: Tick) -> Image {
} else if opts.roi.is_none()
&& let Some(pick) = a.auto_pick()
{
let key = AutoKey::new(comp, opts.scale, false);
let gpu = pick.choose(key);
opts.backend = if gpu { Backend::Gpu } else { Backend::Cpu };
auto = Some((key, gpu));
// (a frame cancelled or panicking between passes drops it: ended unmeasured)
let ticket = pick.ticket(AutoKey::new(comp, opts.scale, false));
opts.backend = if ticket.gpu() { Backend::Gpu } else { Backend::Cpu };
auto = Some(ticket);
}
}
let t0 = web_time::Instant::now();
@@ -98,8 +98,8 @@ pub async fn comp_frame(r: &Renderer<'_>, comp: ItemId, t: Tick) -> Image {
} else {
in_passes(Some(a), |acc| Renderer { opts, accel: acc, ..*r }.comp_frame(comp, t)).await.value
};
if let (Some((key, chose)), Some(pick)) = (auto, a.auto_pick()) {
pick.record(key, chose, t0.elapsed().as_secs_f64() * 1e3);
if let Some(t) = auto {
t.record(t0.elapsed().as_secs_f64() * 1e3);
}
img
}
+97 -25
View File
@@ -5,8 +5,9 @@ use super::fx_host::{FxHost, essential_overrides};
use super::opts::{Collapse3d, LayerTiming, MAX_THROUGH, RenderOpts, Through};
use super::reads::{ReadFiles, ReadLog};
use super::{
Arc, AutoKey, AutoPick, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image, ItemId,
ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d, vec2,
Arc, AutoKey, AutoPick, AutoTicket, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image,
ItemId, ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d,
vec2,
};
use effectcraft_keyframe::Value;
use effectcraft_project::LayerId;
@@ -584,23 +585,24 @@ impl<'a> Renderer<'a> {
if self.depth == 0
&& let Some(a) = self.active_accel()
{
let auto = self.frame_auto(a, comp_id, false);
if auto.is_none_or(|(p, key)| p.choose(key)) {
// (an error returns with the ticket, which ends the frame unmeasured)
let mut ticket = self.frame_auto(a, comp_id, false).map(|(p, key)| p.ticket(key));
if ticket.as_ref().is_none_or(AutoTicket::gpu) {
let t0 = web_time::Instant::now();
if let Some(img) = scope(a)? {
if let Some((p, key)) = auto {
p.record(key, true, t0.elapsed().as_secs_f64() * 1e3);
if let Some(t) = ticket {
t.record(t0.elapsed().as_secs_f64() * 1e3);
}
return Ok(img);
}
if let Some((p, key)) = auto {
p.declined(key);
if let Some(t) = &mut ticket {
t.declined();
}
}
let t0 = web_time::Instant::now();
let img = cpu.comp_frame_cpu(comp_id, t);
if let Some((p, key)) = auto {
p.record(key, false, t0.elapsed().as_secs_f64() * 1e3);
if let Some(t) = ticket {
t.record(t0.elapsed().as_secs_f64() * 1e3);
}
return Ok(img);
}
@@ -878,6 +880,12 @@ impl<'a> Renderer<'a> {
/// Source pixels of a layer in layer space (before masks/effects).
pub(crate) fn source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
self.source_with(ctx, layer, false)
}
/// [`Renderer::source`]; with `quantized`, footage that [`Renderer::prequantized_footage`]
/// counts comes quantised to the project depth.
fn source_with(&self, ctx: &EvalCtx, layer: &Layer, quantized: bool) -> Option<Buf> {
let s = self.opts.scale;
match &layer.source {
LayerSource::Solid { item } => {
@@ -910,7 +918,7 @@ impl<'a> Renderer<'a> {
&& let Some(nc) = self.project.comp(*item)
{
let img = self.footage_frame(ctx, layer, *item, pf, lt, None)?;
return Some(self.footage_buf(&img, pf, nc.width, nc.height));
return Some(self.footage_buf(&img, pf, nc.width, nc.height, false));
}
// Essential Properties overrides render the nested comp with this instance's
// values.
@@ -958,8 +966,12 @@ impl<'a> Renderer<'a> {
return Some(buf);
}
}
// 8 bpc plain footage: made from the footage's 8-bit levels, no float frame.
if quantized && let Some(buf) = self.footage_buf8(ctx, layer, item, self.proxy_for(item).unwrap_or(f), f, ctx.source_time(layer)) {
return Some(buf);
}
let (img, pf) = self.footage_picture_of(ctx, layer, item, f)?;
Some(self.footage_buf(&img, pf, f.width, f.height))
Some(self.footage_buf(&img, pf, f.width, f.height, quantized && Self::prequantized_footage(layer)))
}
// (the font files go to this render's log, which `ctx` may not carry: see
// [`Renderer::with_reads`])
@@ -992,15 +1004,74 @@ impl<'a> Renderer<'a> {
/// A decoded frame of `f` as a layer buffer covering `w`×`h` layer pixels (the footage's
/// nominal size; proxies are scaled up to it), converted from its colour profile.
fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32) -> Buf {
let mut buf = match self.footage_layout(img, w, h) {
FootageLayout::Native { scale } => Buf { img: img.clone(), offset: [0.0; 2], scale },
FootageLayout::Resample { width, height, scale } => Buf { img: effectcraft_raster::resample(img, width, height), offset: [0.0; 2], scale },
};
if let Some(c) = self.pipe.media_in(f) {
color::convert(&mut buf.img, &c);
///
/// `quantized`: also clamp and quantise it to the project depth, as [`Renderer::masked_source`]
/// would (fused with the copy of the cached frame: one pass over it instead of three).
fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32, quantized: bool) -> Buf {
let c = self.pipe.media_in(f);
match self.footage_layout(img, w, h) {
FootageLayout::Native { scale } => {
let img = match (quantized, &c) {
(true, _) => self.pipe.copy_converted(img, c.as_ref()),
(false, Some(c)) => {
let mut img = img.clone();
color::convert(&mut img, c);
img
}
(false, None) => img.clone(),
};
Buf { img, offset: [0.0; 2], scale }
}
FootageLayout::Resample { width, height, scale } => {
let mut img = effectcraft_raster::resample(img, width, height);
if let Some(c) = &c {
color::convert(&mut img, c);
}
if quantized {
self.pipe.quantize(&mut img);
}
Buf { img, offset: [0.0; 2], scale }
}
}
buf
}
/// [`Renderer::footage_buf`]'s quantised buffer made from the footage's 8-bit levels
/// ([`FootageSource::frame8`]), the same pixels to the bit, when nothing before the quantise
/// changes them: an 8 bpc render at Full size of plain footage (no masks, colour profile,
/// Separate Fields, Frame Blending, Cineon, Invert Alpha or linear light interpretation).
/// `None` otherwise, and when the source has no 8-bit frame.
// ponytail: the media pool keeps the 8-bit and the float frame as separate entries, so a
// movie shown by both a plain and a masked layer in one 8 bpc comp is decoded twice per
// frame; derive one from the other in the pool if a comp like that turns out common.
#[allow(clippy::too_many_arguments)]
fn footage_buf8(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, pf: &Footage, f: &Footage, t: Tick) -> Option<Buf> {
let plain = self.pipe.levels == Some(255.0)
&& Self::prequantized_footage(layer)
&& self.reduced_size(f.width, f.height).is_none()
&& self.pipe.media_in(pf).is_none()
&& pf.fields == effectcraft_project::FieldOrder::Off
&& !(pf.invert_alpha || pf.linear_light_applies() || pf.cineon_applies())
&& frame_blend_mode(ctx, layer) == FrameBlend::Off;
if !plain {
return None;
}
let fr = self.read(|| self.footage.frame8(item, pf, t, self.pipe.float_curve(pf)))?;
let n = (fr.width as usize).checked_mul(fr.height as usize)?;
let px = fr.rgba.as_chunks::<4>().0;
if px.len() != n {
return None;
}
use rayon::prelude::*;
let data = px.par_iter().map(|&p| effectcraft_raster::expand_u8(p)).collect();
let k = if fr.width > 0 { f.width.max(1) as f64 / fr.width as f64 } else { 1.0 };
Some(Buf { img: Image { width: fr.width, height: fr.height, data }, offset: [0.0; 2], scale: 1.0 / k })
}
/// The layer's source is footage that [`Renderer::source`] hands over already quantised
/// ([`Renderer::footage_buf`]): footage without masks (masks apply to unquantised pixels).
/// Collapsed layers may be vector footage, drawn elsewhere: not counted.
fn prequantized_footage(layer: &Layer) -> bool {
matches!(layer.source, LayerSource::Footage { .. }) && !layer.switches.collapse && layer.masks().is_none_or(|m| m.groups().next().is_none())
}
/// How [`Renderer::footage_buf`] lays out a decoded frame `img` of footage `w`×`h` layer
@@ -1159,13 +1230,14 @@ impl<'a> Renderer<'a> {
/// Source → masks, clamped/quantised to the project depth.
fn masked_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Buf> {
let mut buf = match self.content_times(ctx, layer) {
Some(times) => self.blurred_source(ctx, layer, &times)?,
None => self.source(ctx, layer)?,
let (mut buf, quantized) = match self.content_times(ctx, layer) {
Some(times) => (self.blurred_source(ctx, layer, &times)?, false),
None => (self.source_with(ctx, layer, true)?, Self::prequantized_footage(layer)),
};
// A solid is filled with a quantised colour: only masks can take it off the grid.
// A solid is filled with a quantised colour, and plain footage comes quantised: only
// masks can take them off the grid.
let solid = matches!(layer.source, LayerSource::Solid { .. });
if masks::apply(ctx, layer, &mut buf, self.mask_blur(ctx, layer)) || !solid {
if masks::apply(ctx, layer, &mut buf, self.mask_blur(ctx, layer)) || !(solid || quantized) {
self.pipe.quantize(&mut buf.img);
}
Some(buf)
+117 -3
View File
@@ -229,6 +229,12 @@ fn healthy_gpu_decline_renders_cpu_once_outside_scope() {
let src = ScopedFootage { in_scope: false.into(), reads: 0.into() };
let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Auto, ..Default::default() });
r.accel = Some(&a);
// Auto warms up CPU first: the first frame renders on the CPU without a GPU scope.
r.comp_frame_scoped(cid, Tick::ZERO, |_| -> Result<Option<crate::Image>, ()> { panic!("Auto's CPU warm-up frame must not enter GPU error scopes") })
.unwrap();
assert_eq!(src.reads.load(Ordering::SeqCst), 1);
assert_eq!(a.calls(), 0);
// The next one is the GPU's warm-up frame, which the accelerator declines.
let frame = r
.comp_frame_scoped(cid, Tick::ZERO, |a| {
src.in_scope.store(true, Ordering::SeqCst);
@@ -238,11 +244,119 @@ fn healthy_gpu_decline_renders_cpu_once_outside_scope() {
})
.unwrap();
assert!(frame.get(100, 50)[3] > 0.99);
assert_eq!(src.reads.load(Ordering::SeqCst), 1);
assert_eq!(src.reads.load(Ordering::SeqCst), 2);
assert_eq!(a.calls(), 1);
assert_eq!(a.pick.stats(AutoKey::new(cid, 1.0, false)).unwrap().cpu_frames, 1);
assert_eq!(a.pick.stats(AutoKey::new(cid, 1.0, false)).unwrap().cpu_frames, 2);
// Auto's next frame stays on CPU, without entering a GPU scope at all.
r.comp_frame_scoped(cid, Tick::ZERO, |_| -> Result<Option<crate::Image>, ()> { panic!("Auto CPU choice must not enter GPU error scopes") }).unwrap();
assert_eq!(src.reads.load(Ordering::SeqCst), 2);
assert_eq!(src.reads.load(Ordering::SeqCst), 3);
assert_eq!(a.effect_calls.load(Ordering::SeqCst), 0, "CPU effects must not use the GPU outside its scope");
}
/// A preview starts with every lane asking at once, before any frame is measured: the first
/// frames (the ones on screen) render on the CPU; once one is measured, one lane takes a GPU
/// warm-up frame, and the next waits for its time. (Every lane took a GPU warm-up frame on an
/// integrated GPU 2–4× slower than the CPU, and each first playback stalled at its start.)
#[test]
fn auto_pick_warms_the_gpu_up_one_frame_at_a_time() {
let pick = AutoPick::default();
let key = AutoKey::new(ItemId(3), 1.0, false);
let first: Vec<bool> = (0..12).map(|_| pick.choose(key)).collect();
assert!(!first.iter().any(|g| *g), "{first:?}");
pick.record(key, false, 10.0);
let next: Vec<bool> = (0..12).map(|_| pick.choose(key)).collect();
assert_eq!(next.iter().filter(|g| **g).count(), 1, "{next:?}");
// The CPU frames finish first: still no second GPU frame while the first runs.
for _ in 0..22 {
pick.record(key, false, 10.0);
}
assert!(!(0..12).any(|_| pick.choose(key)), "a second GPU warm-up frame while one runs");
// Measured: the next warm-up frame may go to the GPU.
pick.record(key, true, 40.0);
assert!(pick.choose(key));
// A re-probe of the slow side runs one frame at a time too.
let pick = AutoPick::default();
let mut gpu_asked = 0;
for _ in 0..(4 * AutoPick::REPROBE) {
// (sequential warm-up, then GPU frames held: as lanes that never report back)
if pick.choose(key) {
gpu_asked += 1;
if pick.stats(key).is_none_or(|s| s.gpu_frames < AutoPick::WARMUP) {
pick.record(key, true, 40.0);
}
} else {
pick.record(key, false, 10.0);
}
}
assert!(gpu_asked <= AutoPick::WARMUP as usize + 1, "asked the GPU {gpu_asked} times");
}
/// Frames render side by side: twelve CPU frames of 210 ms each at once cost ~18 ms a frame,
/// so a lone GPU frame of 175 ms is the slower side (it measured faster and took the comp).
#[test]
fn auto_pick_counts_frames_running_side_by_side() {
let pick = AutoPick::default();
let key = AutoKey::new(ItemId(4), 0.5, false);
// Twelve lanes: each starts its next frame as soon as one is done.
let mut lanes: std::collections::VecDeque<bool> = (0..12).map(|_| pick.choose(key)).collect();
for _ in 0..240 {
let Some(gpu) = lanes.pop_front() else { break };
pick.record(key, gpu, if gpu { 175.0 } else { 210.0 });
lanes.push_back(pick.choose(key));
}
let s = pick.stats(key).unwrap();
assert!(s.cpu_ms.unwrap() < 30.0 && s.gpu_ms.unwrap() > 100.0, "{s:?}");
assert!(lanes.iter().filter(|g| **g).count() <= 1, "{lanes:?}");
}
/// A frame that ends without a time (a GPU failure, a remote frame replaced before it finished)
/// no longer counts as running: the frames after it are timed as one at a time. (It stayed
/// counted, and every later frame on its side was timed as half its cost.)
#[test]
fn auto_pick_abandoned_frames_stop_counting_as_running() {
let pick = AutoPick::default();
let key = AutoKey::new(ItemId(5), 1.0, false);
let mut dropped = [false; 2];
for _ in 0..16 {
let gpu = pick.choose(key);
if !std::mem::replace(&mut dropped[usize::from(gpu)], true) {
pick.abandoned(key, gpu);
continue;
}
pick.record(key, gpu, if gpu { 40.0 } else { 10.0 });
}
let s = pick.stats(key).unwrap();
assert_eq!((s.cpu_ms, s.gpu_ms), (Some(10.0), Some(40.0)), "{s:?}");
}
/// A ticket that never reports (dropped on an error, a retry elsewhere, a cancelled export
/// future) ends its frame like [`AutoPick::abandoned`]; one the GPU declined reports from the
/// CPU. (Each exit that skipped `record` left its frame counted as running, and every later
/// frame on that side was timed as one of several side by side.)
#[test]
fn auto_tickets_end_their_frame_on_every_exit() {
let pick = AutoPick::default();
let key = AutoKey::new(ItemId(6), 1.0, false);
let mut dropped = [false; 2];
for _ in 0..16 {
let t = pick.ticket(key);
let gpu = t.gpu();
if !std::mem::replace(&mut dropped[usize::from(gpu)], true) {
drop(t);
continue;
}
t.record(if gpu { 40.0 } else { 10.0 });
}
let s = pick.stats(key).unwrap();
assert_eq!((s.cpu_ms, s.gpu_ms), (Some(10.0), Some(40.0)), "{s:?}");
// Declined: the frame renders on the CPU and reports there, one frame on it.
let key = AutoKey::new(ItemId(7), 1.0, false);
pick.ticket(key).record(10.0);
let mut t = pick.ticket(key);
assert!(t.gpu(), "the GPU's warm-up frame");
t.declined();
assert!(!t.gpu());
t.record(10.0);
pick.ticket(key).record(10.0);
assert_eq!(pick.stats(key).unwrap().cpu_ms, Some(10.0), "timed as one frame on the CPU");
}
+91
View File
@@ -106,3 +106,94 @@ fn still_layers_are_cached_across_times_and_movies_are_not() {
assert_eq!(src.sizes.unwrap().into_inner().unwrap().len(), fetches + 3, "{kind:?}");
}
}
/// [`Clip`] that also keeps its frames as 8-bit levels, counting the times it's asked for them.
struct Clip8 {
asked: std::sync::atomic::AtomicUsize,
}
impl FootageSource for Clip8 {
fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option<Arc<Image>> {
let mut img = (*Clip { sizes: None }.frame(item, f, t)?).clone();
// (partly transparent, premultiplied, as decoded with alpha)
img.data.iter_mut().enumerate().for_each(|(i, p)| {
let a = (i % 7) as f32 / 6.0;
*p = [p[0] * a, p[1] * a, p[2] * a, a];
});
Some(Arc::new(img))
}
fn frame8(&self, item: ItemId, f: &Footage, t: Tick, _: crate::FloatCurve) -> Option<Arc<crate::disk_cache::Frame8>> {
self.asked.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let img = self.frame(item, f, t)?;
let rgba = img.data.iter().flat_map(|p| effectcraft_raster::quantize_px_u8(*p)).collect();
Some(Arc::new(crate::disk_cache::Frame8 { width: img.width, height: img.height, rgba }))
}
}
/// An 8 bpc render at Full takes plain footage as 8-bit levels and renders the same pixels to
/// the bit; other depths, reduced renders and interpreted pixels never ask for them.
#[test]
fn footage_kept_as_8_bit_levels_renders_the_same_pixels() {
struct Float(Clip8);
impl FootageSource for Float {
fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option<Arc<Image>> {
self.0.frame(item, f, t)
}
}
for (depth, invert, scale, used) in [
(BitDepth::Bpc8, false, 1.0, true),
(BitDepth::Bpc8, false, 0.75, true),
(BitDepth::Bpc8, false, 0.5, false),
(BitDepth::Bpc8, true, 1.0, false),
(BitDepth::Bpc16, false, 1.0, false),
(BitDepth::Bpc32, false, 1.0, false),
] {
let (p, cid) = project(depth, invert);
let opts = RenderOpts { scale, ..Default::default() };
let q8 = Clip8 { asked: Default::default() };
let frame = Renderer::new(&p, &q8, opts).comp_frame(cid, Tick::ZERO);
let plain = Renderer::new(&p, &Float(Clip8 { asked: Default::default() }), opts).comp_frame(cid, Tick::ZERO);
assert!(frame.data.iter().zip(&plain.data).all(|(a, b)| a.map(f32::to_bits) == b.map(f32::to_bits)), "{depth:?} {invert} {scale}");
assert_eq!(q8.asked.into_inner() > 0, used, "{depth:?} {invert} {scale}");
}
}
/// The 8-bit levels are read while the render records its reads: the movie is in the files the
/// frame (and a cached layer buffer) was made of. (Read outside, a buffer of a movie listed no
/// files and was still marked complete: changed on disk, it was served stale.)
#[test]
fn footage_kept_as_8_bit_levels_is_noted_as_read() {
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
struct Noted {
clip: Clip8,
recording: AtomicBool,
outside: AtomicUsize,
}
impl FootageSource for Noted {
fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option<Arc<Image>> {
self.clip.frame(item, f, t)
}
fn frame8(&self, item: ItemId, f: &Footage, t: Tick, float: crate::FloatCurve) -> Option<Arc<crate::disk_cache::Frame8>> {
if !self.recording.load(Ordering::SeqCst) {
self.outside.fetch_add(1, Ordering::SeqCst);
}
self.clip.frame8(item, f, t, float)
}
// (as the media pool does: the files of what `f` reads go to `reads`)
fn recording(&self, reads: &crate::reads::ReadLog, f: &mut dyn FnMut()) {
reads.note(crate::reads::ReadFile::new("clip.mov", crate::reads::FileStamp::Native { len: 1, modified: Some(1) }));
self.recording.store(true, Ordering::SeqCst);
f();
self.recording.store(false, Ordering::SeqCst);
}
}
let (p, cid) = project(BitDepth::Bpc8, false);
let src = Noted { clip: Clip8 { asked: Default::default() }, recording: AtomicBool::new(false), outside: AtomicUsize::new(0) };
let log = crate::reads::ReadLog::new();
let mut r = Renderer::new(&p, &src, RenderOpts::default());
r.reads = Some(&log);
r.comp_frame(cid, Tick::ZERO);
assert!(src.clip.asked.load(Ordering::SeqCst) > 0, "the 8-bit levels were not asked for");
assert_eq!(src.outside.load(Ordering::SeqCst), 0, "8-bit levels read outside the render's recording");
assert!(log.files().files().iter().any(|f| &*f.path == "clip.mov"), "the movie is not in the frame's files");
}
+13 -7
View File
@@ -26,7 +26,7 @@ function __err(msg) {
}
function __num(v, what) {
var n = Number(v);
if (typeof v === "boolean" || isNaN(n)) throw __err((what || "value") + " must be a number");
if (typeof v === "boolean" || Number.isNaN(n)) throw __err((what || "value") + " must be a number");
return n;
}
function __str(v) {
@@ -43,11 +43,11 @@ if (typeof String.prototype.substr !== "function") {
if (this === null || this === undefined) throw new TypeError("String.substr called on null or undefined");
var text = String(this), n = text.length;
var from = Number(start);
if (isNaN(from)) from = 0;
if (Number.isNaN(from)) from = 0;
// ToIntegerOrInfinity, then clamp positive or negative offsets.
from = from < 0 ? Math.max(n + Math.ceil(from), 0) : Math.min(Math.floor(from), n);
var count = length === undefined ? n - from : Number(length);
if (isNaN(count) || count <= 0) return "";
if (Number.isNaN(count) || count <= 0) return "";
return text.slice(from, from + count);
},
writable: true, configurable: true
@@ -436,7 +436,7 @@ function __callable(o) {
// 1-based collections with `[n]` access.
var __coll = {
get: function (t, k, r) {
if (typeof k === "string" && /^[0-9]+$/.test(k)) return t.__at(parseInt(k, 10));
if (typeof k === "string" && /^[0-9]+$/.test(k)) return t.__at(Number.parseInt(k, 10));
return Reflect.get(t, k, r);
},
};
@@ -867,6 +867,12 @@ Property.prototype.property = function () { return null; };
if (!idx) return null;
return this.__layer.containingComp.layer(idx).__id;
}
// A string parameter (a LUT or look file, a text field) takes the text, or a File's path (#3395).
// A hidden one (Mesh Warp's mesh) stays refused by the key setters too, as by setValue.
if (info.type === "string") {
if (info.hidden) throw __err("\"" + info.name + "\" can not be set");
return __str(v);
}
if (info.type === "enum") return __num(v) - 1;
if (info.type === "bool") return typeof v === "boolean" ? v : __num(v) !== 0;
if (info.type === "color") {
@@ -1652,7 +1658,7 @@ CompItem.prototype.constructor = CompItem;
P.saveFrameToPng = function (time, file) {
var f = __str(file);
if (!f) throw __err("saveFrameToPng: missing file");
__file("write", f, "");
__file("checkWrite", f);
__call("comp.saveFrameAs", { comp: this.__id, path: f, time: __num(time, "time") });
};
P.openInEssentialGraphics = function () { __call("comp.openInEssentialGraphics", { comp: this.__id }); };
@@ -1662,8 +1668,8 @@ CompItem.prototype.constructor = CompItem;
if (!/\.[A-Za-z0-9]+$/.test(f)) f += ".ectemplate";
if (!overwrite && JSON.parse(__file("exists", f)) === true) return false;
if (this.__controllers().length === 0) return false;
// The file gate applies to templates too.
__file("write", f, "");
// The file gate applies to templates too (checked without touching the file).
__file("checkWrite", f);
__call("essential.exportTemplate", { comp: this.__id, path: f });
return true;
};
+2
View File
@@ -427,6 +427,8 @@ fn native_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsV
let p = p.to_string_lossy();
Ok(json!(if op == "exists" { s.services.exists(&p) } else { s.services.is_dir(&p) }))
}
// The write gate alone, with no side effect on the file (export wrappers).
"checkWrite" => check_access(s, &path, true).map(|_| json!(true)),
"read" | "readBinary" => {
let p = check_access(s, &path, false)?;
let bytes = s.services.read_file(&p.to_string_lossy()).map_err(|e| format!("Unable to read {path}: {e}"))?;
+1 -1
View File
@@ -750,7 +750,7 @@ function __uiMouseWanted(c, inherited) {
function __uiMouseEvent(type, target, v) {
var num = function (x, d) {
x = Number(x);
return isFinite(x) ? x : d;
return Number.isFinite(x) ? x : d;
};
var keys = { alt: !!v.altKey, control: !!v.ctrlKey, ctrl: !!v.ctrlKey, meta: !!v.metaKey, shift: !!v.shiftKey };
return {
+79
View File
@@ -700,6 +700,56 @@ fn save_frame_to_png_writes_behind_the_file_gate() {
let _ = std::fs::remove_dir_all(&dir);
}
/// A script that runs the export commands itself (`app.run`, `__exec`) meets the same file-write
/// preference as the wrappers; with the preference off nothing is created or replaced.
#[test]
fn export_commands_run_directly_by_a_script_respect_the_file_gate() {
let dir = std::env::temp_dir().join(format!("ec-direct-export-{}", std::process::id()));
// A leftover dir from an interrupted run (same pid) must not feed stale absence checks.
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let png = dir.join("frame.png");
let tpl = dir.join("card.ectemplate");
let kept = dir.join("kept.png");
std::fs::write(&kept, b"original").unwrap();
let mut s = session();
s.execute("comp.new", json!({"name": "Card", "width": 64, "height": 64, "duration": 1})).unwrap();
s.execute("layer.newText", json!({"text": "Hi"})).unwrap();
ok(&mut s, "var c = app.project.activeItem; c.layer(1).property('ADBE Transform Group').property('ADBE Opacity').addToMotionGraphicsTemplate(c);");
let q = |p: &std::path::Path| serde_json::to_string(&p.to_string_lossy()).unwrap();
let comp = s.active_comp_id().unwrap().0;
for (code, target) in [
(format!("app.run('comp.saveFrameAs', {{comp: {comp}, path: {}}})", q(&png)), &png),
(format!("__exec('comp.saveFrameAs', JSON.stringify({{comp: {comp}, path: {}}}))", q(&png)), &png),
(format!("app.run('essential.exportTemplate', {{comp: {comp}, path: {}}})", q(&tpl)), &tpl),
(format!("app.run('comp.saveFrameAs', {{comp: {comp}, path: {}}})", q(&kept)), &kept),
] {
let o = run_code(&mut s, &code, "direct.jsx");
let e = o.error.unwrap_or_else(|| panic!("`{code}` was not refused"));
assert!(e.message.contains("Allow Scripts to Write Files"), "{e:?}");
if target == &kept {
assert_eq!(std::fs::read(&kept).unwrap(), b"original");
} else {
assert!(!target.exists(), "{} was written", target.display());
}
}
// The wrappers check the gate without touching the file: an existing one is left as it was.
let o = run_code(&mut s, &format!("app.project.activeItem.saveFrameToPng(0, new File({}))", q(&kept)), "wrap.jsx");
assert!(o.error.is_some());
assert_eq!(std::fs::read(&kept).unwrap(), b"original");
let o = run_code(&mut s, &format!("app.project.activeItem.exportAsMotionGraphicsTemplate(true, {})", q(&tpl)), "wrap.jsx");
assert!(o.error.is_some());
assert!(!tpl.exists());
// The user, from the UI, is not a script: the same command writes.
s.execute("comp.saveFrameAs", json!({"comp": comp, "path": png.to_string_lossy()})).unwrap();
assert!(png.exists());
// With the preference on, the script's direct call writes too.
s.prefs.scripting.allow_scripts_write_files = true;
ok(&mut s, &format!("app.run('essential.exportTemplate', {{comp: {comp}, path: {}}})", q(&tpl)));
assert!(std::fs::metadata(&tpl).unwrap().len() > 100);
let _ = std::fs::remove_dir_all(&dir);
}
/// #1246: with Warn User When Executing Files on, a script that asserts `confirmed` itself still
/// gets the question; only the user's answer opens the file.
#[test]
@@ -1949,3 +1999,32 @@ fn setting_comp_width_and_height_leaves_the_layers_where_they_are() {
let (_, c) = comp_named(&s, "w");
assert_eq!(layer(c, "s").props.prop("transform/position").unwrap().value, KV::Vec3([100.0, 100.0, 0.0]));
}
/// #3395: setValue on a string effect parameter stores the text, or a File's path, as `prop.set`
/// does. Before, every string fell through to the number check ("value must be a number").
/// Hidden string parameters (Mesh Warp's Distortion Mesh) stay refused, keys included.
#[test]
fn set_value_stores_a_string_parameter() {
let mut s = session();
let o = ok(
&mut s,
r#"
var c = app.project.items.addComp("Str", 64, 36, 1, 1, 24);
var l = c.layers.addSolid([1, 0, 0], "s", 64, 36, 1);
var fx = l.property("ADBE Effect Parade");
var lut = fx.addProperty("Apply Color LUT").property("LUT");
lut.setValue("/luts/look.cube");
var fromString = lut.value;
lut.setValue(new File("/luts/other.cube"));
var mesh = fx.addProperty("Mesh Warp").property("Distortion Mesh");
function attempt(f) { try { f(); return "no error"; } catch (e) { return e.message; } }
var hidden = attempt(function () { mesh.setValue("x"); });
var keyed = attempt(function () { mesh.setValueAtTime(0, "x"); });
[lut.propertyValueType === PropertyValueType.CUSTOM_VALUE, fromString, lut.value, hidden, keyed, mesh.numKeys]
"#,
);
let refused = "\"Distortion Mesh\" can not be set";
assert_eq!(o.result, json!([true, "/luts/look.cube", "/luts/other.cube", refused, refused, 0]));
let (_, c) = comp_named(&s, "Str");
assert_eq!(layer(c, "s").props.prop("effects/#1/lut").unwrap().value, KV::Str("/luts/other.cube".into()));
}
Loaded 100 of 162 files, more files were not shown because too many files have changed in this diff. Show more