1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Image loading and cache.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]
use std::{
env,
future::{Future, IntoFuture},
mem,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
use parking_lot::Mutex;
use task::io::AsyncReadExt;
use zng_app::{
update::EventUpdate,
view_process::{
raw_events::{LOW_MEMORY_EVENT, RAW_IMAGE_LOADED_EVENT, RAW_IMAGE_LOAD_ERROR_EVENT, RAW_IMAGE_METADATA_LOADED_EVENT},
ViewImage, VIEW_PROCESS, VIEW_PROCESS_INITED_EVENT,
},
widget::UiTaskWidget,
AppExtension,
};
use zng_app_context::app_local;
use zng_clone_move::{async_clmv, clmv};
use zng_task as task;
mod types;
pub use types::*;
mod render;
#[doc(inline)]
pub use render::{render_retain, ImageRenderWindowRoot, ImageRenderWindowsService, IMAGES_WINDOW, IMAGE_RENDER};
use zng_layout::unit::{ByteLength, ByteUnits};
use zng_task::UiTask;
use zng_txt::{formatx, ToTxt, Txt};
use zng_unique_id::{IdEntry, IdMap};
use zng_var::{types::WeakArcVar, var, AnyVar, AnyWeakVar, ArcVar, Var, WeakVar};
use zng_view_api::{image::ImageRequest, ipc::IpcBytes, ViewProcessOffline};
/// Application extension that provides an image cache.
///
/// # Services
///
/// Services this extension provides.
///
/// * [`IMAGES`]
#[derive(Default)]
pub struct ImageManager {}
impl AppExtension for ImageManager {
fn event_preview(&mut self, update: &mut EventUpdate) {
if let Some(args) = RAW_IMAGE_METADATA_LOADED_EVENT.on(update) {
let images = IMAGES_SV.read();
if let Some(var) = images
.decoding
.iter()
.map(|t| &t.image)
.find(|v| v.with(|img| img.view.get().unwrap() == &args.image))
{
var.update();
}
} else if let Some(args) = RAW_IMAGE_LOADED_EVENT.on(update) {
let image = &args.image;
// image finished decoding, remove from `decoding`
// and notify image var value update.
let mut images = IMAGES_SV.write();
if let Some(i) = images
.decoding
.iter()
.position(|t| t.image.with(|img| img.view.get().unwrap() == image))
{
let ImageDecodingTask { image, .. } = images.decoding.swap_remove(i);
image.update();
image.with(|img| img.done_signal.set());
}
} else if let Some(args) = RAW_IMAGE_LOAD_ERROR_EVENT.on(update) {
let image = &args.image;
// image failed to decode, remove from `decoding`
// and notify image var value update.
let mut images = IMAGES_SV.write();
if let Some(i) = images
.decoding
.iter()
.position(|t| t.image.with(|img| img.view.get().unwrap() == image))
{
let ImageDecodingTask { image, .. } = images.decoding.swap_remove(i);
image.update();
image.with(|img| {
img.done_signal.set();
if let Some(k) = &img.cache_key {
if let Some(e) = images.cache.get(k) {
e.error.store(true, Ordering::Relaxed);
}
}
tracing::error!("decode error: {:?}", img.error().unwrap());
});
}
} else if let Some(args) = VIEW_PROCESS_INITED_EVENT.on(update) {
if !args.is_respawn {
return;
}
let mut images = IMAGES_SV.write();
let images = &mut *images;
images.cleanup_not_cached(true);
images.download_accept.clear();
let decoding_interrupted = mem::take(&mut images.decoding);
for (img_var, max_decoded_len, downscale, mask) in images
.cache
.values()
.map(|e| (e.image.clone(), e.max_decoded_len, e.downscale, e.mask))
.chain(
images
.not_cached
.iter()
.filter_map(|e| e.image.upgrade().map(|v| (v, e.max_decoded_len, e.downscale, e.mask))),
)
{
let img = img_var.get();
if let Some(view) = img.view.get() {
if view.generation() == args.generation {
continue; // already recovered, can this happen?
}
if let Some(e) = view.error() {
// respawned, but image was an error.
img_var.set(Img::dummy(Some(e.to_owned())));
} else if let Some(task) = decoding_interrupted.iter().find(|e| e.image.with(|img| img.view() == Some(view))) {
// respawned, but image was decoding, need to restart decode.
match VIEW_PROCESS.add_image(ImageRequest {
format: task.format.clone(),
data: task.data.clone(),
max_decoded_len: max_decoded_len.0 as u64,
downscale,
mask,
}) {
Ok(img) => {
img_var.set(Img::new(img));
}
Err(ViewProcessOffline) => { /*will receive another event.*/ }
}
images.decoding.push(ImageDecodingTask {
format: task.format.clone(),
data: task.data.clone(),
image: img_var,
});
} else {
// respawned and image was loaded.
let img_format = if view.is_mask() {
ImageDataFormat::A8 { size: view.size() }
} else {
ImageDataFormat::Bgra8 {
size: view.size(),
ppi: view.ppi(),
}
};
let data = view.pixels().unwrap();
let img = match VIEW_PROCESS.add_image(ImageRequest {
format: img_format.clone(),
data: data.clone(),
max_decoded_len: max_decoded_len.0 as u64,
downscale,
mask,
}) {
Ok(img) => img,
Err(ViewProcessOffline) => return, // we will receive another event.
};
img_var.set(Img::new(img));
images.decoding.push(ImageDecodingTask {
format: img_format,
data,
image: img_var,
});
}
} // else { *is loading, will continue normally in self.update_preview()* }
}
} else if LOW_MEMORY_EVENT.on(update).is_some() {
IMAGES.clean_all();
} else {
self.event_preview_render(update);
}
}
fn update_preview(&mut self) {
// update loading tasks:
let mut images = IMAGES_SV.write();
let mut loading = Vec::with_capacity(images.loading.len());
let loading_tasks = mem::take(&mut images.loading);
let mut proxies = mem::take(&mut images.proxies);
drop(images); // proxies can use IMAGES
'loading_tasks: for t in loading_tasks {
t.task.lock().update();
match t.task.into_inner().into_result() {
Ok(d) => {
match d.r {
Ok(data) => {
if let Some((key, mode)) = &t.is_data_proxy_source {
for proxy in &mut proxies {
if proxy.is_data_proxy() {
if let Some(replaced) = proxy.data(key, &data, &d.format, *mode, t.downscale, t.mask, true) {
replaced.set_bind(&t.image).perm();
t.image.hold(replaced).perm();
continue 'loading_tasks;
}
}
}
}
if VIEW_PROCESS.is_available() {
// success and we have a view-process.
match VIEW_PROCESS.add_image(ImageRequest {
format: d.format.clone(),
data: data.clone(),
max_decoded_len: t.max_decoded_len.0 as u64,
downscale: t.downscale,
mask: t.mask,
}) {
Ok(img) => {
// request sent, add to `decoding` will receive
// `RawImageLoadedEvent` or `RawImageLoadErrorEvent` event
// when done.
t.image.modify(move |v| {
v.to_mut().view.set(img).unwrap();
});
}
Err(ViewProcessOffline) => {
// will recover in ViewProcessInitedEvent
}
}
IMAGES_SV.write().decoding.push(ImageDecodingTask {
format: d.format,
data,
image: t.image,
});
} else {
// success, but we are only doing `load_in_headless` validation.
let img = ViewImage::dummy(None);
t.image.modify(move |v| {
let v = v.to_mut();
v.view.set(img).unwrap();
v.done_signal.set();
});
}
}
Err(e) => {
tracing::error!("load error: {e:?}");
// load error.
let img = ViewImage::dummy(Some(e));
t.image.modify(move |v| {
let v = v.to_mut();
v.view.set(img).unwrap();
v.done_signal.set();
});
// flag error for user retry
if let Some(k) = &t.image.with(|img| img.cache_key) {
if let Some(e) = IMAGES_SV.read().cache.get(k) {
e.error.store(true, Ordering::Relaxed);
}
}
}
}
}
Err(task) => {
loading.push(ImageLoadingTask {
task: Mutex::new(task),
image: t.image,
max_decoded_len: t.max_decoded_len,
downscale: t.downscale,
mask: t.mask,
is_data_proxy_source: t.is_data_proxy_source,
});
}
}
}
let mut images = IMAGES_SV.write();
images.loading = loading;
images.proxies = proxies;
}
fn update(&mut self) {
self.update_render();
}
}
app_local! {
static IMAGES_SV: ImagesService = ImagesService::new();
}
struct ImageLoadingTask {
task: Mutex<UiTask<ImageData>>,
image: ArcVar<Img>,
max_decoded_len: ByteLength,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
is_data_proxy_source: Option<(ImageHash, ImageCacheMode)>,
}
struct ImageDecodingTask {
format: ImageDataFormat,
data: IpcBytes,
image: ArcVar<Img>,
}
struct CacheEntry {
image: ArcVar<Img>,
error: AtomicBool,
max_decoded_len: ByteLength,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
}
struct NotCachedEntry {
image: WeakArcVar<Img>,
max_decoded_len: ByteLength,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
}
struct ImagesService {
load_in_headless: ArcVar<bool>,
limits: ArcVar<ImageLimits>,
download_accept: Txt,
proxies: Vec<Box<dyn ImageCacheProxy>>,
loading: Vec<ImageLoadingTask>,
decoding: Vec<ImageDecodingTask>,
cache: IdMap<ImageHash, CacheEntry>,
not_cached: Vec<NotCachedEntry>,
render: render::ImagesRender,
}
impl ImagesService {
fn new() -> Self {
Self {
load_in_headless: var(false),
limits: var(ImageLimits::default()),
proxies: vec![],
loading: vec![],
decoding: vec![],
download_accept: Txt::from_static(""),
cache: IdMap::new(),
not_cached: vec![],
render: render::ImagesRender::default(),
}
}
fn register(
&mut self,
key: ImageHash,
image: ViewImage,
downscale: Option<ImageDownscale>,
) -> std::result::Result<ImageVar, (ViewImage, ImageVar)> {
let limits = self.limits.get();
let limits = ImageLimits {
max_encoded_len: limits.max_encoded_len,
max_decoded_len: limits.max_decoded_len.max(image.pixels().map(|b| b.len()).unwrap_or(0).bytes()),
allow_path: PathFilter::BlockAll,
#[cfg(feature = "http")]
allow_uri: UriFilter::BlockAll,
};
match self.cache.entry(key) {
IdEntry::Occupied(e) => Err((image, e.get().image.read_only())),
IdEntry::Vacant(e) => {
let is_error = image.is_error();
let is_loading = !is_error && !image.is_loaded();
let is_mask = image.is_mask();
let format = if is_mask {
ImageDataFormat::A8 { size: image.size() }
} else {
ImageDataFormat::Bgra8 {
size: image.size(),
ppi: image.ppi(),
}
};
let img_var = var(Img::new(image));
if is_loading {
self.decoding.push(ImageDecodingTask {
format,
data: IpcBytes::from_vec(vec![]),
image: img_var.clone(),
});
}
Ok(e.insert(CacheEntry {
error: AtomicBool::new(is_error),
image: img_var,
max_decoded_len: limits.max_decoded_len,
downscale,
mask: if is_mask { Some(ImageMaskMode::A) } else { None },
})
.image
.read_only())
}
}
}
fn detach(&mut self, image: ImageVar) -> ImageVar {
if let Some(key) = &image.with(|i| i.cache_key) {
let decoded_size = image.with(|img| img.pixels().map(|b| b.len()).unwrap_or(0).bytes());
let mut max_decoded_len = self.limits.with(|l| l.max_decoded_len.max(decoded_size));
let mut downscale = None;
let mut mask = None;
if let Some(e) = self.cache.get(key) {
max_decoded_len = e.max_decoded_len;
downscale = e.downscale;
mask = e.mask;
// is cached, `clean` if is only external reference.
if image.strong_count() == 2 {
self.cache.remove(key);
}
}
// remove `cache_key` from image, this clones the `Img` only-if is still in cache.
let mut img = image.into_value();
img.cache_key = None;
let img = var(img);
self.not_cached.push(NotCachedEntry {
image: img.downgrade(),
max_decoded_len,
downscale,
mask,
});
img.read_only()
} else {
// already not cached
image
}
}
fn proxy_then_remove(mut proxies: Vec<Box<dyn ImageCacheProxy>>, key: &ImageHash, purge: bool) -> bool {
for proxy in &mut proxies {
let r = proxy.remove(key, purge);
match r {
ProxyRemoveResult::None => continue,
ProxyRemoveResult::Remove(r, p) => return IMAGES_SV.write().proxied_remove(proxies, &r, p),
ProxyRemoveResult::Removed => {
IMAGES_SV.write().proxies.append(&mut proxies);
return true;
}
}
}
IMAGES_SV.write().proxied_remove(proxies, key, purge)
}
fn proxied_remove(&mut self, mut proxies: Vec<Box<dyn ImageCacheProxy>>, key: &ImageHash, purge: bool) -> bool {
self.proxies.append(&mut proxies);
if purge || self.cache.get(key).map(|v| v.image.strong_count() > 1).unwrap_or(false) {
self.cache.remove(key).is_some()
} else {
false
}
}
fn proxy_then_get(
mut proxies: Vec<Box<dyn ImageCacheProxy>>,
source: ImageSource,
mode: ImageCacheMode,
limits: ImageLimits,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
) -> ImageVar {
let source = match source {
ImageSource::Read(path) => {
let path = crate::absolute_path(&path, || env::current_dir().expect("could not access current dir"), true);
if !limits.allow_path.allows(&path) {
let error = formatx!("limits filter blocked `{}`", path.display());
tracing::error!("{error}");
IMAGES_SV.write().proxies.append(&mut proxies);
return var(Img::dummy(Some(error))).read_only();
}
ImageSource::Read(path)
}
#[cfg(feature = "http")]
ImageSource::Download(uri, accepts) => {
if !limits.allow_uri.allows(&uri) {
let error = formatx!("limits filter blocked `{uri}`");
tracing::error!("{error}");
IMAGES_SV.write().proxies.append(&mut proxies);
return var(Img::dummy(Some(error))).read_only();
}
ImageSource::Download(uri, accepts)
}
ImageSource::Image(r) => {
IMAGES_SV.write().proxies.append(&mut proxies);
return r;
}
source => source,
};
let key = source.hash128(downscale, mask).unwrap();
for proxy in &mut proxies {
if proxy.is_data_proxy() && !matches!(source, ImageSource::Data(_, _, _) | ImageSource::Static(_, _, _)) {
continue;
}
let r = proxy.get(&key, &source, mode, downscale, mask);
match r {
ProxyGetResult::None => continue,
ProxyGetResult::Cache(source, mode, downscale, mask) => {
return IMAGES_SV.write().proxied_get(
proxies,
source.hash128(downscale, mask).unwrap(),
source,
mode,
limits,
downscale,
mask,
)
}
ProxyGetResult::Image(img) => {
IMAGES_SV.write().proxies.append(&mut proxies);
return img;
}
}
}
IMAGES_SV.write().proxied_get(proxies, key, source, mode, limits, downscale, mask)
}
#[allow(clippy::too_many_arguments)]
fn proxied_get(
&mut self,
mut proxies: Vec<Box<dyn ImageCacheProxy>>,
key: ImageHash,
source: ImageSource,
mode: ImageCacheMode,
limits: ImageLimits,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
) -> ImageVar {
self.proxies.append(&mut proxies);
match mode {
ImageCacheMode::Cache => {
if let Some(v) = self.cache.get(&key) {
return v.image.read_only();
}
}
ImageCacheMode::Retry => {
if let Some(e) = self.cache.get(&key) {
if !e.error.load(Ordering::Relaxed) {
return e.image.read_only();
}
}
}
ImageCacheMode::Ignore | ImageCacheMode::Reload => {}
}
if !VIEW_PROCESS.is_available() && !self.load_in_headless.get() {
tracing::warn!("loading dummy image, set `load_in_headless=true` to actually load without renderer");
let dummy = var(Img::new(ViewImage::dummy(None)));
self.cache.insert(
key,
CacheEntry {
image: dummy.clone(),
error: AtomicBool::new(false),
max_decoded_len: limits.max_decoded_len,
downscale,
mask,
},
);
return dummy.read_only();
}
let max_encoded_size = limits.max_encoded_len;
match source {
ImageSource::Read(path) => self.load_task(
key,
mode,
limits.max_decoded_len,
downscale,
mask,
true,
task::run(async move {
let mut r = ImageData {
format: path
.extension()
.and_then(|e| e.to_str())
.map(|s| ImageDataFormat::FileExtension(Txt::from_str(s)))
.unwrap_or(ImageDataFormat::Unknown),
r: Err(Txt::from_static("")),
};
let mut file = match task::fs::File::open(path).await {
Ok(f) => f,
Err(e) => {
r.r = Err(e.to_txt());
return r;
}
};
let len = match file.metadata().await {
Ok(m) => m.len() as usize,
Err(e) => {
r.r = Err(e.to_txt());
return r;
}
};
if len > max_encoded_size.0 {
r.r = Err(formatx!("file size `{}` exceeds the limit of `{max_encoded_size}`", len.bytes()));
return r;
}
let mut data = Vec::with_capacity(len);
r.r = match file.read_to_end(&mut data).await {
Ok(_) => Ok(IpcBytes::from_vec(data)),
Err(e) => Err(e.to_txt()),
};
r
}),
),
#[cfg(feature = "http")]
ImageSource::Download(uri, accept) => {
let accept = accept.unwrap_or_else(|| self.download_accept());
self.load_task(
key,
mode,
limits.max_decoded_len,
downscale,
mask,
true,
task::run(async move {
let mut r = ImageData {
format: ImageDataFormat::Unknown,
r: Err(Txt::from_static("")),
};
let request = task::http::Request::get(uri)
.unwrap()
.header(task::http::header::ACCEPT, accept.as_str())
.unwrap()
.max_length(max_encoded_size)
.build();
match task::http::send(request).await {
Ok(mut rsp) => {
if let Some(m) = rsp.headers().get(&task::http::header::CONTENT_TYPE).and_then(|v| v.to_str().ok()) {
let m = m.to_lowercase();
if m.starts_with("image/") {
r.format = ImageDataFormat::MimeType(Txt::from_str(&m));
}
}
match rsp.bytes().await {
Ok(d) => r.r = Ok(IpcBytes::from_vec(d)),
Err(e) => {
r.r = Err(formatx!("download error: {e}"));
}
}
let _ = rsp.consume().await;
}
Err(e) => {
r.r = Err(formatx!("request error: {e}"));
}
}
r
}),
)
}
ImageSource::Static(_, bytes, fmt) => {
let r = ImageData {
format: fmt,
r: Ok(IpcBytes::from_slice(bytes)),
};
self.load_task(key, mode, limits.max_decoded_len, downscale, mask, false, async { r })
}
ImageSource::Data(_, bytes, fmt) => {
let r = ImageData {
format: fmt,
r: Ok(IpcBytes::from_slice(&bytes)),
};
self.load_task(key, mode, limits.max_decoded_len, downscale, mask, false, async { r })
}
ImageSource::Render(rfn, args) => {
let img = self.new_cache_image(key, mode, limits.max_decoded_len, downscale, mask);
self.render_img(mask, clmv!(rfn, || rfn(&args.unwrap_or_default())), &img);
img.read_only()
}
ImageSource::Image(_) => unreachable!(),
}
}
#[cfg(feature = "http")]
fn download_accept(&mut self) -> Txt {
if self.download_accept.is_empty() {
if VIEW_PROCESS.is_available() {
let mut r = String::new();
let mut sep = "";
for fmt in VIEW_PROCESS.image_decoders().unwrap_or_default() {
r.push_str(sep);
r.push_str("image/");
r.push_str(&fmt);
sep = ",";
}
}
if self.download_accept.is_empty() {
self.download_accept = "image/*".into();
}
}
self.download_accept.clone()
}
fn cleanup_not_cached(&mut self, force: bool) {
if force || self.not_cached.len() > 1000 {
self.not_cached.retain(|c| c.image.strong_count() > 0);
}
}
fn new_cache_image(
&mut self,
key: ImageHash,
mode: ImageCacheMode,
max_decoded_len: ByteLength,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
) -> ArcVar<Img> {
self.cleanup_not_cached(false);
if let ImageCacheMode::Reload = mode {
self.cache
.entry(key)
.or_insert_with(|| CacheEntry {
image: var(Img::new_none(Some(key))),
error: AtomicBool::new(false),
max_decoded_len,
downscale,
mask,
})
.image
.clone()
} else if let ImageCacheMode::Ignore = mode {
let img = var(Img::new_none(None));
self.not_cached.push(NotCachedEntry {
image: img.downgrade(),
max_decoded_len,
downscale,
mask,
});
img
} else {
let img = var(Img::new_none(Some(key)));
self.cache.insert(
key,
CacheEntry {
image: img.clone(),
error: AtomicBool::new(false),
max_decoded_len,
downscale,
mask,
},
);
img
}
}
/// The `fetch_bytes` future is polled in the UI thread, use `task::run` for futures that poll a lot.
#[allow(clippy::too_many_arguments)]
fn load_task(
&mut self,
key: ImageHash,
mode: ImageCacheMode,
max_decoded_len: ByteLength,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
is_data_proxy_source: bool,
fetch_bytes: impl Future<Output = ImageData> + Send + 'static,
) -> ImageVar {
let img = self.new_cache_image(key, mode, max_decoded_len, downscale, mask);
let r = img.read_only();
self.loading.push(ImageLoadingTask {
task: Mutex::new(UiTask::new(None, fetch_bytes)),
image: img,
max_decoded_len,
downscale,
mask,
is_data_proxy_source: if is_data_proxy_source { Some((key, mode)) } else { None },
});
zng_app::update::UPDATES.update(None);
r
}
}
/// Image loading, cache and render service.
///
/// If the app is running without a [`VIEW_PROCESS`] all images are dummy, see [`load_in_headless`] for
/// details.
///
/// [`load_in_headless`]: IMAGES::load_in_headless
/// [`VIEW_PROCESS`]: zng_app::view_process::VIEW_PROCESS
pub struct IMAGES;
impl IMAGES {
/// If should still download/read image bytes in headless/renderless mode.
///
/// When an app is in headless mode without renderer no [`VIEW_PROCESS`] is available, so
/// images cannot be decoded, in this case all images are the [`dummy`] image and no attempt
/// to download/read the image files is made. You can enable loading in headless tests to detect
/// IO errors, in this case if there is an error acquiring the image file the image will be a
/// [`dummy`] with error.
///
/// [`dummy`]: IMAGES::dummy
/// [`VIEW_PROCESS`]: zng_app::view_process::VIEW_PROCESS
pub fn load_in_headless(&self) -> ArcVar<bool> {
IMAGES_SV.read().load_in_headless.clone()
}
/// Default loading and decoding limits for each image.
pub fn limits(&self) -> ArcVar<ImageLimits> {
IMAGES_SV.read().limits.clone()
}
/// Returns a dummy image that reports it is loaded or an error.
pub fn dummy(&self, error: Option<Txt>) -> ImageVar {
var(Img::dummy(error)).read_only()
}
/// Cache or load an image file from a file system `path`.
pub fn read(&self, path: impl Into<PathBuf>) -> ImageVar {
self.cache(path.into())
}
/// Get a cached `uri` or download it.
///
/// Optionally define the HTTP ACCEPT header, if not set all image formats supported by the view-process
/// backend are accepted.
#[cfg(feature = "http")]
pub fn download(&self, uri: impl task::http::TryUri, accept: Option<Txt>) -> ImageVar {
match uri.try_uri() {
Ok(uri) => self.cache(ImageSource::Download(uri, accept)),
Err(e) => self.dummy(Some(e.to_txt())),
}
}
/// Get a cached image from `&'static [u8]` data.
///
/// The data can be any of the formats described in [`ImageDataFormat`].
///
/// The image key is a [`ImageHash`] of the image data.
///
/// # Examples
///
/// Get an image from a PNG file embedded in the app executable using [`include_bytes!`].
///
/// ```
/// # use zng_ext_image::*;
/// # macro_rules! include_bytes { ($tt:tt) => { &[] } }
/// # fn demo() {
/// let image_var = IMAGES.from_static(include_bytes!("ico.png"), "png");
/// # }
pub fn from_static(&self, data: &'static [u8], format: impl Into<ImageDataFormat>) -> ImageVar {
self.cache((data, format.into()))
}
/// Get a cached image from shared data.
///
/// The image key is a [`ImageHash`] of the image data. The data reference is held only until the image is decoded.
///
/// The data can be any of the formats described in [`ImageDataFormat`].
pub fn from_data(&self, data: Arc<Vec<u8>>, format: impl Into<ImageDataFormat>) -> ImageVar {
self.cache((data, format.into()))
}
/// Get a cached image or add it to the cache.
pub fn cache(&self, source: impl Into<ImageSource>) -> ImageVar {
self.image(source, ImageCacheMode::Cache, None, None, None)
}
/// Get a cached image or add it to the cache or retry if the cached image is an error.
pub fn retry(&self, source: impl Into<ImageSource>) -> ImageVar {
self.image(source, ImageCacheMode::Retry, None, None, None)
}
/// Load an image, if it was already cached update the cached image with the reloaded data.
pub fn reload(&self, source: impl Into<ImageSource>) -> ImageVar {
self.image(source, ImageCacheMode::Reload, None, None, None)
}
/// Get or load an image.
///
/// If `limits` is `None` the [`IMAGES.limits`] is used.
///
/// [`IMAGES.limits`]: IMAGES::limits
pub fn image(
&self,
source: impl Into<ImageSource>,
cache_mode: impl Into<ImageCacheMode>,
limits: Option<ImageLimits>,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
) -> ImageVar {
let limits = limits.unwrap_or_else(|| IMAGES_SV.read().limits.get());
let proxies = mem::take(&mut IMAGES_SV.write().proxies);
ImagesService::proxy_then_get(proxies, source.into(), cache_mode.into(), limits, downscale, mask)
}
/// Await for an image source, then get or load the image.
///
/// If `limits` is `None` the [`IMAGES.limits`] is used.
///
/// This method returns immediately with a loading [`ImageVar`], when `source` is ready it
/// is used to get the actual [`ImageVar`] and binds it to the returned image.
///
/// Note that the `cache_mode` always applies to the inner image, and only to the return image if `cache_key` is set.
///
/// [`IMAGES.limits`]: IMAGES::limits
pub fn image_task<F>(
&self,
source: impl IntoFuture<IntoFuture = F>,
cache_mode: impl Into<ImageCacheMode>,
cache_key: Option<ImageHash>,
limits: Option<ImageLimits>,
downscale: Option<ImageDownscale>,
mask: Option<ImageMaskMode>,
) -> ImageVar
where
F: Future<Output = ImageSource> + Send + 'static,
{
let cache_mode = cache_mode.into();
if let Some(key) = cache_key {
match cache_mode {
ImageCacheMode::Cache => {
if let Some(v) = IMAGES_SV.read().cache.get(&key) {
return v.image.read_only();
}
}
ImageCacheMode::Retry => {
if let Some(e) = IMAGES_SV.read().cache.get(&key) {
if !e.error.load(Ordering::Relaxed) {
return e.image.read_only();
}
}
}
ImageCacheMode::Ignore | ImageCacheMode::Reload => {}
}
}
let source = source.into_future();
let img = var(Img::new_none(cache_key));
task::spawn(async_clmv!(img, {
let source = source.await;
let actual_img = IMAGES.image(source, cache_mode, limits, downscale, mask);
actual_img.set_bind(&img).perm();
img.hold(actual_img).perm();
}));
img.read_only()
}
/// Associate the `image` with the `key` in the cache.
///
/// Returns `Ok(ImageVar)` with the new image var that tracks `image`, or `Err(ViewImage, ImageVar)`
/// that returns the `image` and a clone of the var already associated with the `key`.
pub fn register(&self, key: ImageHash, image: ViewImage) -> std::result::Result<ImageVar, (ViewImage, ImageVar)> {
IMAGES_SV.write().register(key, image, None)
}
/// Remove the image from the cache, if it is only held by the cache.
///
/// You can use [`ImageSource::hash128_read`] and [`ImageSource::hash128_download`] to get the `key`
/// for files or downloads.
///
/// Returns `true` if the image was removed.
pub fn clean(&self, key: ImageHash) -> bool {
ImagesService::proxy_then_remove(mem::take(&mut IMAGES_SV.write().proxies), &key, false)
}
/// Remove the image from the cache, even if it is still referenced outside of the cache.
///
/// You can use [`ImageSource::hash128_read`] and [`ImageSource::hash128_download`] to get the `key`
/// for files or downloads.
///
/// Returns `true` if the image was cached.
pub fn purge(&self, key: &ImageHash) -> bool {
ImagesService::proxy_then_remove(mem::take(&mut IMAGES_SV.write().proxies), key, true)
}
/// Gets the cache key of an image.
pub fn cache_key(&self, image: &Img) -> Option<ImageHash> {
if let Some(key) = &image.cache_key {
if IMAGES_SV.read().cache.contains_key(key) {
return Some(*key);
}
}
None
}
/// If the image is cached.
pub fn is_cached(&self, image: &Img) -> bool {
image
.cache_key
.as_ref()
.map(|k| IMAGES_SV.read().cache.contains_key(k))
.unwrap_or(false)
}
/// Returns an image that is not cached.
///
/// If the `image` is the only reference returns it and removes it from the cache. If there are other
/// references a new [`ImageVar`] is generated from a clone of the image.
pub fn detach(&self, image: ImageVar) -> ImageVar {
IMAGES_SV.write().detach(image)
}
/// Clear cached images that are not referenced outside of the cache.
pub fn clean_all(&self) {
let mut img = IMAGES_SV.write();
img.proxies.iter_mut().for_each(|p| p.clear(false));
img.cache.retain(|_, v| v.image.strong_count() > 1);
}
/// Clear all cached images, including images that are still referenced outside of the cache.
///
/// Image memory only drops when all strong references are removed, so if an image is referenced
/// outside of the cache it will merely be disconnected from the cache by this method.
pub fn purge_all(&self) {
let mut img = IMAGES_SV.write();
img.cache.clear();
img.proxies.iter_mut().for_each(|p| p.clear(true));
}
/// Add a cache proxy.
///
/// Proxies can intercept cache requests and map to a different request or return an image directly.
pub fn install_proxy(&self, proxy: Box<dyn ImageCacheProxy>) {
IMAGES_SV.write().proxies.push(proxy);
}
/// Image format decoders implemented by the current view-process.
pub fn available_decoders(&self) -> Vec<Txt> {
VIEW_PROCESS.image_decoders().unwrap_or_default()
}
/// Image format encoders implemented by the current view-process.
pub fn available_encoders(&self) -> Vec<Txt> {
VIEW_PROCESS.image_encoders().unwrap_or_default()
}
}
struct ImageData {
format: ImageDataFormat,
r: std::result::Result<IpcBytes, Txt>,
}
fn absolute_path(path: &Path, base: impl FnOnce() -> PathBuf, allow_escape: bool) -> PathBuf {
if path.is_absolute() {
normalize_path(path)
} else {
let mut dir = base();
if allow_escape {
dir.push(path);
normalize_path(&dir)
} else {
dir.push(normalize_path(path));
dir
}
}
}
/// Resolves `..` components, without any system request.
///
/// Source: https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
fn normalize_path(path: &Path) -> PathBuf {
use std::path::Component;
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}