1mod device;
8mod normalize;
9
10use std::cell::{Cell, RefCell};
11use std::collections::HashMap;
12use std::io::{Cursor, Read, Seek, SeekFrom};
13use std::rc::Rc;
14use std::sync::Arc;
15
16use alpha2_time::{FrameRate, MediaTime};
17use base64::Engine as _;
18use kurbo::{Affine, PathEl, Shape};
19use peniko::{BlendMode as PenikoBlend, Brush, Compose, Extend, GradientKind, Mix};
20use resvg::tiny_skia::{
21 self, BlendMode, Color, FillRule, GradientStop, IntRect, IntSize, LineCap, LineJoin,
22 LinearGradient, Mask, Paint, PathBuilder, PathStroker, Pixmap, PixmapPaint, Point,
23 RadialGradient, Shader, SpreadMode, Stroke, StrokeDash, SweepGradient, Transform,
24};
25use serde_json::Value;
26use velato::model::ImageAsset;
27use velato::{Composition, RenderSink, Renderer};
28
29use alpha2_model::{MAX_LOTTIE_DOCUMENT_BYTES, ProviderSource};
30
31use crate::api::{
32 Diagnostic, ExtensionError, Format, FramePixels, ProbeReport, ResourceResolver, Timing,
33 VisualFrame, VisualProvider, VisualSource,
34};
35use crate::budget::{MAX_HELD_PIXELS, check_canvas, check_document_size, check_image_pixels};
36
37const MAX_LOTTIE_FRAMES: f64 = 100_000.0;
39const MAX_LOTTIE_BYTES: u64 = 256 * 1024 * 1024;
42const MAX_DOTLOTTIE_FILES: usize = 1_024;
44const MAX_DOTLOTTIE_DIRECTORY_BYTES: u64 = 1_024 * 1_024;
46
47pub struct LottieProvider;
49
50impl VisualProvider for LottieProvider {
51 fn name(&self) -> &'static str {
52 "lottie"
53 }
54
55 fn probe(&self, bytes: &[u8]) -> Option<Result<ProbeReport, ExtensionError>> {
56 let size = check_document_size(
57 &ProviderSource::Lottie { animation: None },
58 bytes,
59 bytes.len() as u64,
60 );
61 if is_zip(bytes) {
62 return Some(
63 size.and_then(|()| Package::read(bytes))
64 .and_then(|package| package.probe()),
65 );
66 }
67 if is_json_object(bytes)
68 && let Err(error) = size
69 {
70 return Some(Err(error));
71 }
72 let json = parse_lottie(bytes)?;
73 Some(inspect(json, Format::Lottie).map(|(report, _, _)| report))
74 }
75
76 fn open(
77 &self,
78 bytes: Arc<[u8]>,
79 source: &ProviderSource,
80 resolver: &dyn ResourceResolver,
81 ) -> Result<Box<dyn VisualSource>, ExtensionError> {
82 Ok(Box::new(LottieSource::open(&bytes, source, resolver)?))
83 }
84}
85
86impl LottieSource {
87 fn open(
88 bytes: &[u8],
89 source: &ProviderSource,
90 resolver: &dyn ResourceResolver,
91 ) -> Result<Self, ExtensionError> {
92 let item = match source {
93 ProviderSource::Lottie { animation } => animation.as_deref(),
94 _ => return Err(ExtensionError::Mismatch("not a Lottie source".into())),
95 };
96 check_document_size(source, bytes, bytes.len() as u64)?;
97 let (json, package, format) = if is_zip(bytes) {
98 let package = Package::read(bytes)?;
99 (package.animation(item)?, Some(package), Format::DotLottie)
100 } else {
101 if item.is_some() {
102 return Err(ExtensionError::Mismatch(
103 "a Lottie JSON file holds one animation".into(),
104 ));
105 }
106 let json = parse_lottie(bytes)
107 .ok_or_else(|| ExtensionError::Mismatch("not a Lottie animation".into()))?;
108 (json, None, Format::Lottie)
109 };
110 let (mut report, depth, sanitized) = inspect(json, format)?;
111 if let Some(package) = &package {
112 report.items.clone_from(&package.ids);
113 report.item = Some(item.unwrap_or(&package.ids[0]).to_owned());
114 }
115 let text = serde_json::to_vec(&sanitized)
117 .map_err(|error| ExtensionError::Malformed(error.to_string()))?;
118 drop(sanitized);
119 let composition = Composition::from_slice(&text)
120 .map_err(|error| ExtensionError::Malformed(format!("{error:?}")))?;
121 let composition_bytes = text.len();
122 drop(text);
123 let mut images = HashMap::new();
126 let mut decoded = HashMap::<String, Option<Arc<Pixmap>>>::new();
127 let mut pixels = 0_u64;
128 for (id, asset) in &composition.images {
129 let key = asset_key(asset);
130 let pixmap = if let Some(known) = decoded.get(&key) {
131 known.clone()
132 } else {
133 let pixmap = match image_bytes(asset, package.as_ref(), resolver)? {
134 Some(bytes) => {
135 if let Some((width, height)) = crate::raster::png_size(&bytes)
136 && check_canvas(width, height).is_ok()
137 {
138 pixels = pixels.saturating_add(u64::from(width) * u64::from(height));
139 check_image_pixels(pixels)?;
140 }
141 decode_image(&bytes).map(Arc::new)
142 }
143 None => None,
144 };
145 decoded.insert(key, pixmap.clone());
146 pixmap
147 };
148 match pixmap {
149 Some(pixmap) => {
150 images.insert(id.clone(), pixmap);
151 }
152 None => report.diagnostics.push(Diagnostic::MissingImage {
153 href: format!(
154 "{}{}",
155 asset.directory.clone().unwrap_or_default(),
156 short(&asset.file_name)
157 ),
158 }),
159 }
160 }
161 Ok(Self {
162 composition,
163 composition_bytes,
164 depth,
165 renderer: Renderer::new(),
166 images,
167 report,
168 last: None,
169 })
170 }
171}
172
173fn is_zip(bytes: &[u8]) -> bool {
174 bytes.starts_with(b"PK\x03\x04")
175}
176
177fn is_json_object(bytes: &[u8]) -> bool {
178 bytes.iter().find(|byte| !byte.is_ascii_whitespace()) == Some(&b'{')
179}
180
181fn parse_lottie(bytes: &[u8]) -> Option<Value> {
183 if !is_json_object(bytes) {
184 return None;
185 }
186 let json: Value = serde_json::from_slice(bytes).ok()?;
187 let root = json.as_object()?;
188 ["w", "h", "fr", "ip", "op"]
189 .iter()
190 .all(|key| root.get(*key).is_some_and(Value::is_number))
191 .then_some(())
192 .filter(|()| root.get("layers").is_some_and(Value::is_array))
193 .map(|()| json)
194}
195
196fn check_directory(bytes: &[u8]) -> Result<(), ExtensionError> {
202 const END: &[u8] = b"PK\x05\x06";
203 const END64_LOCATOR: &[u8] = b"PK\x06\x07";
204 const END64: &[u8] = b"PK\x06\x06";
205 let malformed = |what: &str| ExtensionError::Malformed(format!("dotLottie: {what}"));
206 let field = |at: usize, size: usize| {
207 bytes.get(at..at + size).map(|field| {
208 field
209 .iter()
210 .rev()
211 .fold(0_u64, |value, byte| value << 8 | u64::from(*byte))
212 })
213 };
214 let from = bytes.len().saturating_sub(22 + usize::from(u16::MAX));
215 let end = (from..bytes.len().saturating_sub(21))
216 .rev()
217 .find(|&at| {
218 bytes[at..].starts_with(END)
219 && field(at + 20, 2)
220 .is_some_and(|comment| at as u64 + 22 + comment <= bytes.len() as u64)
221 })
222 .ok_or_else(|| malformed("no end of central directory record"))?;
223 let (mut files, mut size) = (field(end + 10, 2), field(end + 12, 4));
224 let zip64 =
225 files == Some(u64::from(u16::MAX)) || field(end + 16, 4) == Some(u64::from(u32::MAX));
226 if zip64
227 && let Some(locator) = end.checked_sub(20)
228 && bytes[locator..].starts_with(END64_LOCATOR)
229 {
230 let at = field(locator + 8, 8)
231 .and_then(|at| usize::try_from(at).ok())
232 .filter(|&at| at < locator)
233 .and_then(|start| {
234 bytes[start..locator]
235 .windows(END64.len())
236 .position(|window| window == END64)
237 .map(|found| start + found)
238 })
239 .ok_or_else(|| malformed("no ZIP64 end of central directory record"))?;
240 (files, size) = (field(at + 32, 8), field(at + 40, 8));
241 }
242 let (Some(files), Some(size)) = (files, size) else {
243 return Err(malformed("the end of central directory record is cut off"));
244 };
245 if files > MAX_DOTLOTTIE_FILES as u64 {
246 return Err(ExtensionError::Budget(format!(
247 "the dotLottie package holds more than {MAX_DOTLOTTIE_FILES} files"
248 )));
249 }
250 if size > MAX_DOTLOTTIE_DIRECTORY_BYTES {
251 return Err(ExtensionError::Budget(format!(
252 "the dotLottie package's central directory takes more than \
253 {MAX_DOTLOTTIE_DIRECTORY_BYTES} bytes"
254 )));
255 }
256 Ok(())
257}
258
259const MAX_DIRECTORY_SEARCH: u64 = 4 * 1_024 * 1_024;
261
262struct Metered<'a> {
265 bytes: Cursor<&'a [u8]>,
266 left: Rc<Cell<u64>>,
267}
268
269impl Read for Metered<'_> {
270 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
271 let left = self.left.get();
272 if left == 0 {
273 return Err(std::io::Error::other("the search for a directory is over"));
274 }
275 let take = usize::try_from(left).map_or(buf.len(), |left| left.min(buf.len()));
276 let read = self.bytes.read(&mut buf[..take])?;
277 self.left.set(left.saturating_sub((read as u64).max(1)));
279 Ok(read)
280 }
281}
282
283impl Seek for Metered<'_> {
284 fn seek(&mut self, to: SeekFrom) -> std::io::Result<u64> {
285 self.bytes.seek(to)
286 }
287}
288
289struct Package<'a> {
292 ids: Vec<String>,
293 archive: RefCell<zip::ZipArchive<Metered<'a>>>,
294 names: HashMap<String, usize>,
296 unpacked: Cell<u64>,
298}
299
300impl<'a> Package<'a> {
301 fn read(bytes: &'a [u8]) -> Result<Self, ExtensionError> {
302 check_directory(bytes)?;
303 let left = Rc::new(Cell::new(
304 MAX_DIRECTORY_SEARCH.saturating_add((bytes.len() as u64).saturating_mul(2)),
305 ));
306 let metered = Metered {
307 bytes: Cursor::new(bytes),
308 left: Rc::clone(&left),
309 };
310 let archive = zip::ZipArchive::new(metered).map_err(|error| {
311 if left.get() == 0 {
312 ExtensionError::Malformed(
313 "dotLottie: no end of central directory record describes a directory".into(),
314 )
315 } else {
316 ExtensionError::Malformed(format!("dotLottie: {error}"))
317 }
318 })?;
319 left.set(u64::MAX);
321 if archive.len() > MAX_DOTLOTTIE_FILES {
322 return Err(ExtensionError::Budget(format!(
323 "the dotLottie package holds more than {MAX_DOTLOTTIE_FILES} files"
324 )));
325 }
326 let mut names = HashMap::new();
327 for (index, name) in archive.file_names().enumerate() {
328 let name =
329 name.map_err(|error| ExtensionError::Malformed(format!("dotLottie: {error}")))?;
330 if !name.ends_with('/') {
331 names.insert(name.trim_start_matches('/').to_owned(), index);
332 }
333 }
334 let package = Self {
335 ids: Vec::new(),
336 archive: RefCell::new(archive),
337 names,
338 unpacked: Cell::new(0),
339 };
340 let manifest: Value = serde_json::from_slice(
341 &package
342 .file("manifest.json", Some(MAX_LOTTIE_DOCUMENT_BYTES))?
343 .ok_or_else(|| ExtensionError::Malformed("dotLottie: no manifest.json".into()))?,
344 )
345 .map_err(|error| ExtensionError::Malformed(format!("dotLottie: {error}")))?;
346 let ids = manifest
347 .get("animations")
348 .and_then(Value::as_array)
349 .map(|animations| {
350 animations
351 .iter()
352 .filter_map(|animation| animation.get("id")?.as_str().map(str::to_owned))
353 .collect::<Vec<_>>()
354 })
355 .unwrap_or_default();
356 if ids.is_empty() {
357 return Err(ExtensionError::Malformed(
358 "dotLottie: the manifest lists no animation".into(),
359 ));
360 }
361 Ok(Self { ids, ..package })
362 }
363
364 fn file(&self, name: &str, document: Option<usize>) -> Result<Option<Vec<u8>>, ExtensionError> {
370 let Some(&index) = self.names.get(name) else {
371 return Ok(None);
372 };
373 let malformed = |error: &dyn std::fmt::Display| {
374 ExtensionError::Malformed(format!("dotLottie: {error}"))
375 };
376 let total = self.unpacked.get();
377 let left = MAX_LOTTIE_BYTES - total;
378 let (within, too_large) = match document {
379 Some(most) if (most as u64) < left => (
380 most as u64,
381 format!(
382 "the dotLottie package's {name} unpacks to more than the {most} bytes a \
383 provider reads"
384 ),
385 ),
386 _ => (left, "the dotLottie package unpacks past 256 MB".to_owned()),
387 };
388 let too_large = || ExtensionError::Budget(too_large.clone());
389 let mut archive = self.archive.borrow_mut();
390 let mut file = archive.by_index(index).map_err(|error| malformed(&error))?;
391 if file.size() > within {
392 return Err(too_large());
393 }
394 let mut contents = Vec::new();
395 let read = file
396 .by_ref()
397 .take(within + 1)
398 .read_to_end(&mut contents)
399 .map_err(|error| malformed(&error))? as u64;
400 if read > within {
401 return Err(too_large());
402 }
403 self.unpacked.set(total + read);
404 Ok(Some(contents))
405 }
406
407 fn animation(&self, id: Option<&str>) -> Result<Value, ExtensionError> {
409 let id = match id {
410 Some(id) if self.ids.iter().any(|known| known == id) => id,
411 Some(id) => {
412 return Err(ExtensionError::Mismatch(format!(
413 "the dotLottie package has no animation {id}"
414 )));
415 }
416 None => &self.ids[0],
417 };
418 let bytes = self
419 .first_file(
420 &[format!("a/{id}.json"), format!("animations/{id}.json")],
421 Some(MAX_LOTTIE_DOCUMENT_BYTES),
422 )?
423 .ok_or_else(|| {
424 ExtensionError::Malformed(format!("dotLottie: animation {id} has no file"))
425 })?;
426 parse_lottie(&bytes).ok_or_else(|| {
427 ExtensionError::Malformed(format!("dotLottie: animation {id} is not Lottie"))
428 })
429 }
430
431 fn probe(&self) -> Result<ProbeReport, ExtensionError> {
434 let mut unreadable = Vec::new();
435 let mut first = None;
436 for id in &self.ids {
437 match self
438 .animation(Some(id))
439 .and_then(|json| inspect(json, Format::DotLottie))
440 {
441 Ok((report, _, _)) => {
442 unreadable.extend(report.diagnostics);
443 return Ok(ProbeReport {
444 items: self.ids.clone(),
445 item: Some(id.clone()),
446 diagnostics: unreadable,
447 ..report
448 });
449 }
450 Err(error) => {
451 unreadable.push(Diagnostic::UnreadableItem {
452 item: id.clone(),
453 reason: error.to_string(),
454 });
455 first.get_or_insert(error);
456 }
457 }
458 }
459 Err(first.unwrap_or_else(|| {
460 ExtensionError::Malformed("dotLottie: the manifest lists no animation".into())
461 }))
462 }
463
464 fn image(&self, name: &str) -> Result<Option<Vec<u8>>, ExtensionError> {
466 let name = name.trim_start_matches('/');
467 self.first_file(
468 &[
469 name.to_owned(),
470 format!("i/{name}"),
471 format!("images/{name}"),
472 ],
473 None,
474 )
475 }
476
477 fn first_file(
479 &self,
480 paths: &[String],
481 document: Option<usize>,
482 ) -> Result<Option<Vec<u8>>, ExtensionError> {
483 for path in paths {
484 if let Some(bytes) = self.file(path, document)? {
485 return Ok(Some(bytes));
486 }
487 }
488 Ok(None)
489 }
490}
491
492fn inspect(json: Value, format: Format) -> Result<(ProbeReport, u64, Value), ExtensionError> {
494 let root = json
495 .as_object()
496 .ok_or_else(|| ExtensionError::Malformed("the animation is not an object".into()))?;
497 let number = |key: &str| root.get(key).and_then(Value::as_f64).unwrap_or(0.0);
498 let (width, height) = (number("w"), number("h"));
499 let (rate, first, last) = (number("fr"), number("ip"), number("op"));
500 if !(width >= 1.0 && height >= 1.0 && rate > 0.0 && last > first)
501 || last - first > MAX_LOTTIE_FRAMES
502 {
503 return Err(ExtensionError::Malformed(
504 "the animation has no canvas, rate, or frames".into(),
505 ));
506 }
507 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
508 let (width, height, frames) = (
509 width.round() as u32,
510 height.round() as u32,
511 (last - first).ceil() as usize,
512 );
513 check_canvas(width, height)?;
514 let (normalized, sanitized) = normalize::normalize(json)?;
515 let normalize::Normalized { diagnostics, depth } = normalized;
516 check_held(depth, width, height)?;
517 Ok((
518 ProbeReport {
519 format,
520 width,
521 height,
522 timing: Timing::Rate {
523 rate: rational_rate(rate).ok_or_else(|| {
524 ExtensionError::Malformed(format!("the frame rate {rate} is not usable"))
525 })?,
526 frames,
527 },
528 loop_count: None,
529 items: Vec::new(),
530 item: None,
531 color: None,
532 diagnostics,
533 },
534 depth,
535 sanitized,
536 ))
537}
538
539fn check_held(depth: u64, width: u32, height: u32) -> Result<(), ExtensionError> {
542 #[allow(clippy::cast_precision_loss)]
543 let held = (depth.saturating_add(1)) as f64 * f64::from(width) * f64::from(height);
544 if held > MAX_HELD_PIXELS {
545 return Err(ExtensionError::Budget(format!(
546 "drawing the Lottie animation at {width}x{height} holds more than {MAX_HELD_PIXELS} \
547 pixels in its {depth} nested offscreen layers"
548 )));
549 }
550 Ok(())
551}
552
553fn rational_rate(rate: f64) -> Option<FrameRate> {
555 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
556 let (num, den) = [24_u32, 30, 48, 60, 120]
557 .into_iter()
558 .find(|base| (rate - f64::from(*base) * 1_000.0 / 1_001.0).abs() < 0.005)
559 .map(|base| (base * 1_000, 1_001))
560 .or_else(|| ((rate - rate.round()).abs() < 0.005).then(|| (rate.round() as u32, 1)))
561 .unwrap_or_else(|| ((rate * 1_000.0).round() as u32, 1_000));
562 FrameRate::new(num, den).ok()
563}
564
565fn asset_key(asset: &ImageAsset) -> String {
567 format!(
568 "{}\0{}",
569 asset.directory.clone().unwrap_or_default(),
570 asset.file_name
571 )
572}
573
574fn image_bytes(
577 asset: &ImageAsset,
578 package: Option<&Package>,
579 resolver: &dyn ResourceResolver,
580) -> Result<Option<Arc<[u8]>>, ExtensionError> {
581 if let Some(data) = asset.file_name.strip_prefix("data:") {
582 return Ok(data.split_once(";base64,").and_then(|(_, encoded)| {
583 base64::engine::general_purpose::STANDARD
584 .decode(encoded)
585 .ok()
586 .map(Arc::from)
587 }));
588 }
589 let name = format!(
590 "{}{}",
591 asset.directory.clone().unwrap_or_default(),
592 asset.file_name
593 );
594 if let Some(package) = package {
595 for candidate in [&name, &asset.file_name] {
596 if let Some(bytes) = package.image(candidate)? {
597 return Ok(Some(Arc::from(bytes)));
598 }
599 }
600 }
601 Ok(resolver.bytes(&name).ok())
602}
603
604fn decode_image(bytes: &[u8]) -> Option<Pixmap> {
606 let (width, height, mut pixels) = crate::raster::decode_png(bytes).ok()?;
607 crate::api::premultiply(&mut pixels);
608 Pixmap::from_vec(pixels, IntSize::from_wh(width, height)?)
609}
610
611fn short(name: &str) -> String {
612 if name.starts_with("data:") {
613 "an embedded image".into()
614 } else {
615 name.to_owned()
616 }
617}
618
619struct LottieSource {
621 composition: Composition,
622 composition_bytes: usize,
624 depth: u64,
626 renderer: Renderer,
627 images: HashMap<String, Arc<Pixmap>>,
629 report: ProbeReport,
630 last: Option<VisualFrame>,
631}
632
633impl VisualSource for LottieSource {
634 fn report(&self) -> &ProbeReport {
635 &self.report
636 }
637
638 fn render(&mut self, time: MediaTime, fit: (u32, u32)) -> Result<VisualFrame, ExtensionError> {
639 let (index, start, end) = self.report.timing.frame_at(time);
640 let (width, height) = fitted(self.report.width, self.report.height, fit);
641 check_held(self.depth, width, height)?;
642 if let Some(last) = self
643 .last
644 .as_ref()
645 .filter(|last| last.index == index && (last.width, last.height) == (width, height))
646 {
647 return Ok(last.clone());
648 }
649 let mut sink = SkiaSink::new(width, height, &self.images)?;
650 #[allow(clippy::cast_precision_loss)]
651 let frame = self.composition.frames.start + index as f64;
652 let transform = Affine::scale_non_uniform(
653 f64::from(width) / f64::from(self.report.width),
654 f64::from(height) / f64::from(self.report.height),
655 );
656 self.renderer
657 .append(&self.composition, frame, transform, 1.0, &mut sink);
658 let frame = VisualFrame {
659 width,
660 height,
661 pixels: FramePixels::Rgba8(sink.finish().into()),
662 index,
663 start,
664 end,
665 diagnostics: Vec::new(),
666 };
667 self.last = Some(frame.clone());
668 Ok(frame)
669 }
670
671 fn resident_bytes(&self) -> usize {
672 let mut seen = std::collections::HashSet::new();
673 self.images
674 .values()
675 .filter(|image| seen.insert(Arc::as_ptr(image)))
676 .map(|image| image.data().len())
677 .sum::<usize>()
678 + self.composition_bytes
679 + self.last.as_ref().map_or(0, |last| last.pixels.byte_len())
680 }
681}
682
683fn fitted(width: u32, height: u32, fit: (u32, u32)) -> (u32, u32) {
685 if fit.0 == 0 || fit.1 == 0 {
686 return (width, height);
687 }
688 let scale = (f64::from(fit.0) / f64::from(width)).min(f64::from(fit.1) / f64::from(height));
689 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
690 (
691 (f64::from(width) * scale)
692 .round()
693 .clamp(1.0, f64::from(fit.0)) as u32,
694 (f64::from(height) * scale)
695 .round()
696 .clamp(1.0, f64::from(fit.1)) as u32,
697 )
698}
699
700struct SkiaSink<'a> {
706 width: u32,
707 height: u32,
708 layers: Vec<Layer>,
709 images: &'a HashMap<String, Arc<Pixmap>>,
710 covering: Option<Rc<Mask>>,
712 pool: Vec<Pixmap>,
714 #[cfg(test)]
715 whole: bool,
716 #[cfg(test)]
717 composited: u64,
718 #[cfg(test)]
719 allocated: usize,
720}
721
722struct Layer {
723 pixmap: Option<Pixmap>,
725 blend: BlendMode,
726 alpha: f32,
727 clip: Option<Rc<Mask>>,
728 bounds: Option<IntRect>,
730 dirty: Option<IntRect>,
732}
733
734impl<'a> SkiaSink<'a> {
735 fn new(
736 width: u32,
737 height: u32,
738 images: &'a HashMap<String, Arc<Pixmap>>,
739 ) -> Result<Self, ExtensionError> {
740 let pixmap = Pixmap::new(width, height).ok_or_else(|| {
741 ExtensionError::Budget(format!("cannot rasterize at {width}x{height}"))
742 })?;
743 Ok(Self {
744 width,
745 height,
746 layers: vec![Layer {
747 pixmap: Some(pixmap),
748 blend: BlendMode::SourceOver,
749 alpha: 1.0,
750 clip: None,
751 bounds: IntRect::from_xywh(0, 0, width, height),
752 dirty: None,
753 }],
754 images,
755 covering: None,
756 pool: Vec::new(),
757 #[cfg(test)]
758 whole: false,
759 #[cfg(test)]
760 composited: 0,
761 #[cfg(test)]
762 allocated: 0,
763 })
764 }
765
766 fn finish(mut self) -> Vec<u8> {
767 while self.layers.len() > 1 {
768 self.pop_layer();
769 }
770 self.layers
771 .pop()
772 .and_then(|layer| layer.pixmap)
773 .map_or_else(Vec::new, Pixmap::take)
774 }
775
776 #[allow(clippy::unused_self)]
778 fn whole(&self) -> bool {
779 #[cfg(test)]
780 return self.whole;
781 #[cfg(not(test))]
782 false
783 }
784
785 fn target(&mut self) -> Option<&mut Layer> {
787 self.layers
788 .iter_mut()
789 .rev()
790 .find(|layer| layer.pixmap.is_some())
791 }
792
793 fn draw_on(&mut self, bounds: Option<IntRect>) -> &mut Pixmap {
795 let canvas = IntRect::from_xywh(0, 0, self.width, self.height);
796 let layer = self.target().expect("the canvas is a layer");
797 layer.dirty = match (layer.dirty, bounds.or(canvas)) {
798 (Some(dirty), Some(bounds)) => union(dirty, bounds),
799 (dirty, bounds) => dirty.or(bounds),
800 };
801 layer.pixmap.as_mut().expect("a layer with a pixmap")
802 }
803
804 fn reach(&self, bounds: tiny_skia::Rect, transform: Transform) -> Option<IntRect> {
807 let device = PathBuilder::from_rect(bounds)
808 .transform(transform)?
809 .bounds();
810 #[allow(clippy::cast_precision_loss)]
812 let (width, height) = (self.width as f32, self.height as f32);
813 #[allow(clippy::cast_possible_truncation)]
814 IntRect::from_ltrb(
815 device.left().floor().max(-2.0) as i32 - 2,
816 device.top().floor().max(-2.0) as i32 - 2,
817 device.right().ceil().min(width) as i32 + 2,
818 device.bottom().ceil().min(height) as i32 + 2,
819 )?
820 .intersect(&IntRect::from_xywh(0, 0, self.width, self.height)?)
821 }
822
823 fn push(&mut self, blend: BlendMode, alpha: f32, affine: Affine, shape: &impl Shape) {
824 let transform = skia_transform(affine);
825 let path = path_of(shape);
826 let canvas = IntRect::from_xywh(0, 0, self.width, self.height);
827 let device = path
829 .clone()
830 .map(|path| path.transform(transform).map(|path| path.bounds()));
831 #[allow(clippy::cast_precision_loss)]
833 let (width, height) = (self.width as f32, self.height as f32);
834 let clips_nothing = shape.as_rect().is_some()
835 && transform.kx == 0.0
836 && transform.ky == 0.0
837 && matches!(device, Some(Some(bounds)) if bounds.left() <= 0.0
838 && bounds.top() <= 0.0
839 && bounds.right() >= width
840 && bounds.bottom() >= height);
841 #[allow(clippy::float_cmp)]
842 let opaque = alpha == 1.0;
843 if clips_nothing
844 && blend == BlendMode::SourceOver
845 && opaque
846 && !self.whole()
847 && self.target().is_some_and(|layer| layer.dirty.is_none())
848 {
849 self.layers.push(Layer {
850 pixmap: None,
851 blend,
852 alpha,
853 clip: None,
854 bounds: None,
855 dirty: None,
856 });
857 return;
858 }
859 let bounds = match &path {
860 None => canvas,
862 _ if self.whole() => canvas,
863 Some(path) => self.reach(path.bounds(), transform),
865 };
866 let clip = if clips_nothing && !self.whole() {
867 if self.covering.is_none() {
868 self.covering = IntSize::from_wh(self.width, self.height)
869 .and_then(|size| {
870 let coverage = vec![255; size.width() as usize * size.height() as usize];
871 Mask::from_vec(coverage, size)
872 })
873 .map(Rc::new);
874 }
875 self.covering.clone()
876 } else {
877 path.and_then(|path| {
878 let mut mask = Mask::new(self.width, self.height)?;
879 if device::within(shape, affine, 0.0, (self.width, self.height)) {
880 mask.fill_path(&path, FillRule::Winding, true, transform);
881 } else if let Some(far) =
882 device::fill_path(shape, affine, (self.width, self.height))
883 {
884 mask.fill_path(&far, FillRule::Winding, true, Transform::identity());
885 }
886 Some(Rc::new(mask))
887 })
888 };
889 let pooled = if self.whole() { None } else { self.pool.pop() };
890 #[cfg(test)]
891 if pooled.is_none() {
892 self.allocated += 1;
893 }
894 let Some(pixmap) = pooled.or_else(|| Pixmap::new(self.width, self.height)) else {
895 return;
896 };
897 self.layers.push(Layer {
898 pixmap: Some(pixmap),
899 blend,
900 alpha,
901 clip,
902 bounds,
903 dirty: None,
904 });
905 }
906}
907
908impl SkiaSink<'_> {
909 fn fill_far(&mut self, shape: &impl Shape, transform: Affine, mut paint: Paint<'_>) {
912 let Some(path) = device::fill_path(shape, transform, (self.width, self.height)) else {
913 return;
914 };
915 paint.shader.transform(skia_transform(transform));
916 let bounds = self.reach(path.bounds(), Transform::identity());
917 self.draw_on(bounds).fill_path(
918 &path,
919 &paint,
920 FillRule::Winding,
921 Transform::identity(),
922 None,
923 );
924 }
925
926 fn composite(&mut self, pixmap: &Pixmap, layer: &Layer, bounds: IntRect) {
928 #[cfg(test)]
929 {
930 self.composited += u64::from(bounds.width()) * u64::from(bounds.height());
931 }
932 let paint = Paint {
933 shader: tiny_skia::Pattern::new(
934 pixmap.as_ref(),
935 SpreadMode::Pad,
936 tiny_skia::FilterQuality::Nearest,
937 layer.alpha,
938 Transform::identity(),
939 ),
940 blend_mode: layer.blend,
941 anti_alias: false,
942 ..Paint::default()
943 };
944 self.draw_on(Some(bounds)).fill_rect(
945 bounds.to_rect(),
946 &paint,
947 Transform::identity(),
948 layer.clip.as_deref(),
949 );
950 }
951}
952
953fn union(a: IntRect, b: IntRect) -> Option<IntRect> {
954 IntRect::from_ltrb(
955 a.left().min(b.left()),
956 a.top().min(b.top()),
957 a.right().max(b.right()),
958 a.bottom().max(b.bottom()),
959 )
960}
961
962impl RenderSink for SkiaSink<'_> {
963 fn push_layer(
964 &mut self,
965 blend: impl Into<PenikoBlend>,
966 alpha: f32,
967 transform: Affine,
968 shape: &impl Shape,
969 ) {
970 self.push(blend_mode(blend.into()), alpha, transform, shape);
971 }
972
973 fn push_clip_layer(&mut self, transform: Affine, shape: &impl Shape) {
974 self.push(BlendMode::SourceOver, 1.0, transform, shape);
975 }
976
977 fn pop_layer(&mut self) {
978 if self.layers.len() < 2 {
979 return;
980 }
981 let Some(layer) = self.layers.pop() else {
982 return;
983 };
984 let mut layer = layer;
985 let Some(mut pixmap) = layer.pixmap.take() else {
986 return;
987 };
988 let bounds = layer.bounds.and_then(|bounds| match layer.blend {
990 BlendMode::SourceOver if !self.whole() => layer.dirty?.intersect(&bounds),
991 _ => Some(bounds),
992 });
993 if let Some(bounds) = bounds {
994 self.composite(&pixmap, &layer, bounds);
995 }
996 if let Some(dirty) = layer.dirty {
998 let at = |value: i32| usize::try_from(value).unwrap_or(0);
999 let stride = pixmap.width() as usize * 4;
1000 let (left, right) = (at(dirty.left()) * 4, at(dirty.right()) * 4);
1001 for row in pixmap
1002 .data_mut()
1003 .chunks_exact_mut(stride)
1004 .skip(at(dirty.top()))
1005 .take(dirty.height() as usize)
1006 {
1007 row[left..right].fill(0);
1008 }
1009 }
1010 self.pool.push(pixmap);
1011 }
1012 fn draw(
1013 &mut self,
1014 stroke: Option<&kurbo::Stroke>,
1015 transform: Affine,
1016 brush: &Brush,
1017 shape: &impl Shape,
1018 ) {
1019 let Some(path) = path_of(shape) else {
1020 return;
1021 };
1022 let Some(shader) = shader_of(brush) else {
1023 return;
1024 };
1025 let paint = Paint {
1026 shader,
1027 anti_alias: true,
1028 ..Paint::default()
1029 };
1030 let affine = transform;
1031 let transform = skia_transform(affine);
1032 match stroke {
1033 Some(stroke) if stroke.width.is_nan() || stroke.width <= 0.0 => {}
1036 Some(stroke) => {
1037 let style = Stroke {
1038 #[allow(clippy::cast_possible_truncation)]
1039 width: stroke.width as f32,
1040 #[allow(clippy::cast_possible_truncation)]
1041 miter_limit: stroke.miter_limit as f32,
1042 line_cap: match stroke.start_cap {
1043 kurbo::Cap::Butt => LineCap::Butt,
1044 kurbo::Cap::Round => LineCap::Round,
1045 kurbo::Cap::Square => LineCap::Square,
1046 },
1047 line_join: match stroke.join {
1048 kurbo::Join::Bevel => LineJoin::Bevel,
1049 kurbo::Join::Miter => LineJoin::Miter,
1050 kurbo::Join::Round => LineJoin::Round,
1051 },
1052 #[allow(clippy::cast_possible_truncation)]
1053 dash: (!stroke.dash_pattern.is_empty())
1054 .then(|| {
1055 StrokeDash::new(
1056 stroke
1057 .dash_pattern
1058 .iter()
1059 .map(|value| *value as f32)
1060 .collect(),
1061 stroke.dash_offset as f32,
1062 )
1063 })
1064 .flatten(),
1065 };
1066 let reach = style.width / 2.0 * style.miter_limit.max(std::f32::consts::SQRT_2)
1069 + style.width;
1070 if !device::within(shape, affine, f64::from(reach), (self.width, self.height)) {
1071 let scale = PathStroker::compute_resolution_scale(&transform);
1073 let dashed = match &style.dash {
1074 Some(dash) => path.dash(dash, scale),
1075 None => Some(path),
1076 };
1077 if let Some(outline) = dashed.and_then(|path| path.stroke(&style, scale)) {
1078 self.fill_far(&device::kurbo_path(&outline), affine, paint);
1079 }
1080 return;
1081 }
1082 let bounds = tiny_skia::Rect::from_ltrb(
1083 path.bounds().left() - reach,
1084 path.bounds().top() - reach,
1085 path.bounds().right() + reach,
1086 path.bounds().bottom() + reach,
1087 )
1088 .and_then(|bounds| self.reach(bounds, transform));
1089 self.draw_on(bounds)
1090 .stroke_path(&path, &paint, &style, transform, None);
1091 }
1092 None if !device::within(shape, affine, 0.0, (self.width, self.height)) => {
1093 self.fill_far(shape, affine, paint);
1094 }
1095 None => {
1096 let bounds = self.reach(path.bounds(), transform);
1097 self.draw_on(bounds)
1098 .fill_path(&path, &paint, FillRule::Winding, transform, None);
1099 }
1100 }
1101 }
1102
1103 fn draw_image(&mut self, image: &ImageAsset, transform: Affine, alpha: f64) {
1104 let Some(pixmap) = self.images.get(&image.id).map(Arc::as_ref) else {
1105 return;
1106 };
1107 let picture = (f64::from(pixmap.width()), f64::from(pixmap.height()));
1109 let transform = match image.width.zip(image.height) {
1110 Some((width, height)) if width > 0.0 && height > 0.0 && (width, height) != picture => {
1111 let scale = (width / picture.0).min(height / picture.1);
1112 transform
1113 * Affine::translate((
1114 (width - picture.0 * scale) / 2.0,
1115 (height - picture.1 * scale) / 2.0,
1116 ))
1117 * Affine::scale(scale)
1118 }
1119 _ => transform,
1120 };
1121 let paint = PixmapPaint {
1122 #[allow(clippy::cast_possible_truncation)]
1123 opacity: alpha as f32,
1124 blend_mode: BlendMode::SourceOver,
1125 quality: tiny_skia::FilterQuality::Bilinear,
1126 };
1127 let picture = kurbo::Rect::new(0.0, 0.0, picture.0, picture.1);
1128 if !device::within(&picture, transform, 0.0, (self.width, self.height)) {
1129 let paint = Paint {
1131 shader: tiny_skia::Pattern::new(
1132 pixmap.as_ref(),
1133 SpreadMode::Pad,
1134 paint.quality,
1135 paint.opacity,
1136 Transform::identity(),
1137 ),
1138 blend_mode: paint.blend_mode,
1139 anti_alias: false,
1140 ..Paint::default()
1141 };
1142 self.fill_far(&picture, transform, paint);
1143 return;
1144 }
1145 let transform = skia_transform(transform);
1146 let bounds = IntRect::from_xywh(0, 0, pixmap.width(), pixmap.height())
1147 .and_then(|picture| self.reach(picture.to_rect(), transform));
1148 self.draw_on(bounds)
1149 .draw_pixmap(0, 0, pixmap.as_ref(), &paint, transform, None);
1150 }
1151}
1152
1153fn skia_transform(affine: Affine) -> Transform {
1154 #[allow(clippy::cast_possible_truncation)]
1155 let [sx, ky, kx, sy, tx, ty] = affine.as_coeffs().map(|value| value as f32);
1156 Transform::from_row(sx, ky, kx, sy, tx, ty)
1157}
1158
1159fn path_of(shape: &impl Shape) -> Option<tiny_skia::Path> {
1161 let mut builder = PathBuilder::new();
1162 #[allow(clippy::cast_possible_truncation)]
1163 let point = |p: kurbo::Point| (p.x as f32, p.y as f32);
1164 for element in shape.path_elements(0.1) {
1165 match element {
1166 PathEl::MoveTo(p) => {
1167 let (x, y) = point(p);
1168 builder.move_to(x, y);
1169 }
1170 PathEl::LineTo(p) => {
1171 let (x, y) = point(p);
1172 builder.line_to(x, y);
1173 }
1174 PathEl::QuadTo(p1, p2) => {
1175 let ((x1, y1), (x, y)) = (point(p1), point(p2));
1176 builder.quad_to(x1, y1, x, y);
1177 }
1178 PathEl::CurveTo(p1, p2, p3) => {
1179 let ((x1, y1), (x2, y2), (x, y)) = (point(p1), point(p2), point(p3));
1180 builder.cubic_to(x1, y1, x2, y2, x, y);
1181 }
1182 PathEl::ClosePath => builder.close(),
1183 }
1184 }
1185 builder.finish()
1186}
1187
1188fn shader_of(brush: &Brush) -> Option<Shader<'static>> {
1189 let color = |components: [f32; 4]| {
1190 Color::from_rgba(
1191 components[0].clamp(0.0, 1.0),
1192 components[1].clamp(0.0, 1.0),
1193 components[2].clamp(0.0, 1.0),
1194 components[3].clamp(0.0, 1.0),
1195 )
1196 };
1197 match brush {
1198 Brush::Solid(solid) => color(solid.components).map(Shader::SolidColor),
1199 Brush::Gradient(gradient) => {
1200 let stops = gradient
1201 .stops
1202 .iter()
1203 .filter_map(|stop| {
1204 let srgb = stop.color.to_alpha_color::<peniko::color::Srgb>();
1205 color(srgb.components).map(|c| GradientStop::new(stop.offset, c))
1206 })
1207 .collect::<Vec<_>>();
1208 let mode = match gradient.extend {
1209 Extend::Pad => SpreadMode::Pad,
1210 Extend::Repeat => SpreadMode::Repeat,
1211 Extend::Reflect => SpreadMode::Reflect,
1212 };
1213 #[allow(clippy::cast_possible_truncation)]
1214 let point = |p: kurbo::Point| Point::from_xy(p.x as f32, p.y as f32);
1215 match &gradient.kind {
1216 GradientKind::Linear(linear) => LinearGradient::new(
1217 point(linear.start),
1218 point(linear.end),
1219 stops,
1220 mode,
1221 Transform::identity(),
1222 ),
1223 GradientKind::Radial(radial) => RadialGradient::new(
1224 point(radial.start_center),
1225 radial.start_radius,
1226 point(radial.end_center),
1227 radial.end_radius,
1228 stops,
1229 mode,
1230 Transform::identity(),
1231 ),
1232 GradientKind::Sweep(sweep) => SweepGradient::new(
1233 point(sweep.center),
1234 sweep.start_angle.to_degrees(),
1235 sweep.end_angle.to_degrees(),
1236 stops,
1237 mode,
1238 Transform::identity(),
1239 ),
1240 }
1241 }
1242 Brush::Image(_) => None,
1243 }
1244}
1245
1246fn blend_mode(blend: PenikoBlend) -> BlendMode {
1249 match blend.mix {
1250 Mix::Multiply => BlendMode::Multiply,
1251 Mix::Screen => BlendMode::Screen,
1252 Mix::Overlay => BlendMode::Overlay,
1253 Mix::Darken => BlendMode::Darken,
1254 Mix::Lighten => BlendMode::Lighten,
1255 Mix::ColorDodge => BlendMode::ColorDodge,
1256 Mix::ColorBurn => BlendMode::ColorBurn,
1257 Mix::HardLight => BlendMode::HardLight,
1258 Mix::SoftLight => BlendMode::SoftLight,
1259 Mix::Difference => BlendMode::Difference,
1260 Mix::Exclusion => BlendMode::Exclusion,
1261 Mix::Hue => BlendMode::Hue,
1262 Mix::Saturation => BlendMode::Saturation,
1263 Mix::Color => BlendMode::Color,
1264 Mix::Luminosity => BlendMode::Luminosity,
1265 Mix::Normal => match blend.compose {
1266 Compose::Clear => BlendMode::Clear,
1267 Compose::Copy => BlendMode::Source,
1268 Compose::Dest => BlendMode::Destination,
1269 Compose::DestOver => BlendMode::DestinationOver,
1270 Compose::SrcIn => BlendMode::SourceIn,
1271 Compose::DestIn => BlendMode::DestinationIn,
1272 Compose::SrcOut => BlendMode::SourceOut,
1273 Compose::DestOut => BlendMode::DestinationOut,
1274 Compose::SrcAtop => BlendMode::SourceAtop,
1275 Compose::DestAtop => BlendMode::DestinationAtop,
1276 Compose::Xor => BlendMode::Xor,
1277 Compose::Plus | Compose::PlusLighter => BlendMode::Plus,
1278 Compose::SrcOver => BlendMode::SourceOver,
1279 },
1280 }
1281}
1282
1283#[cfg(test)]
1284mod tests;