Lottie4J 1.5.0

A new version of Lottie4J, the Java and JavaFX library to render Lottie animations, is available: 1.5.0. It adds no new file features. It is all about speed!

One of the test animations played at 3 fps. It now plays at 60 fps!

How to measure

A player in a window is capped by the refresh rate of the display, so it shows 60 fps for anything that renders within 16.6 ms. That says nothing. So the numbers below are the render time per frame and the achievable fps (1000 / average render ms).

All numbers come from an Apple M2 Max, running OpenJDK 27 and JavaFX 21. The animations were played in a real window with the adaptive scaling of the player switched off, so everything is rendered at the full size.

Before touching any renderer code, it was important to know where the time actually goes. These three parts of the library and the file viewer help to investigate the playback speed:

  • RenderStats and RenderProfiler collect render times per frame and per stage.
  • The file viewer has a new --bench=<seconds> option that prints a summary.
  • PlaybackBenchmarkTest renders frames back to back without a window, so the display can’t limit the result.

The test file

winter-town is an animation of 1200×800 pixels with 1200 frames and 348 layers, created by Dirk Lemmermann with his AnimatorFX. Of those, 318 are shape layers and 30 are null layers. It also has one blur and one track matte.

The first measurement was 360.6 ms per frame. And the profiler made the problem clear: 95% of that time went to 100 full-canvas off-screen snapshots. The animation has 100 layers with an animated opacity. Each of them was drawn into its own 1200×800 buffer, rasterized, read back and composited. The other 246 layers and all the actual drawing together took about 8 ms.

Step 1: Apply opacity directly

A layer that paints exactly one shape with one fill or stroke has no overlapping parts. So there is no reason for a buffer: the opacity can be applied directly with the global alpha.

  • Before: 360.6 ms per frame, 2.8 fps
  • After: 7.2 ms per frame, 138 achievable fps, 59.8 fps delivered

Compare those two numbers. And what about the pixels? When comparing ten frames with the old output, only about 0.01% of the bytes were different, and never by more than 2 out of 255.

Step 2: Smaller track mattes

What was left was the one track matte, now 92% of the render time. It was processed on the full canvas.

Now the library first looks for the area the content layer paints in, using a probe at 1/8 of the size. Only that area is snapshotted, read, composed and drawn. If the content is empty, the matte is skipped.

  • Before: 7.2 ms per frame, 138 achievable fps
  • After: 3.2 ms per frame, 313 achievable fps

From 360 ms to 3.2 ms. That is about 100 times faster, and there is room left for bigger canvases or heavier animations.

Step 3: A harder test with 14 mattes

winter-town was now well within budget, so a heavier file was needed. animated_background_patterns has 134 layers and 14 track mattes. It’s a 1080×1080 animation meant to play at 30 fps, but it only delivered 18 fps.

The probe for the matte area was now a big part of the frame time. The fix: a matte reuses the area its content covered in the previous frame, plus a margin. The prediction is checked against what is drawn, and a miss falls back to the probe. Every fourth frame, and every time the playback jumps, a new probe is forced anyway. During the benchmark, 2819 predictions were used and only 4 of them missed.

  • Before: 45.8 ms per frame, 21.8 achievable fps
  • After: 34.9 ms per frame, 28.7 achievable fps

The similarity with the reference renders of thorvg is unchanged for this file, for collision and for winter-town.

What didn’t work

Two attempts to cut the number of snapshots per matte did not make it into the release:

  • Content and matte side by side in one canvas. 28.6 ms, a nice gain. But moving a layer to another offset changes the rounding of the rasterizer on a few pixels, and SequentialPlaybackTest failed. Rightly so: the output of a frame may not depend on the playback history.
  • One canvas for all mattes of a frame. Same pixels, but no faster. Snapshot time is dominated by rasterizing what is drawn, not by a fixed cost per snapshot. Fourteen small snapshots cost about as much as one large one.

The same rule needed one fix in the area prediction: the matted result is now cropped to the content bounds plus 1 px before it is drawn. A predicted area and a probed area now give the identical image.

What’s next

animated_background_patterns is just under its 30 fps target. Candidates to get there: reusing the result of a matte whose content didn’t change, drawing soft mattes at a lower resolution, and cheaper probes. The complete log with all measurements is in BENCHMARK-LOG.md in the repository.

There are also contribution guidelines now, for anyone who wants to help.

Upgrade

Upgrading to 1.5.0 is recommended, as you get these improvements without any code changes.

See the full changelog for the details, or the GitHub diff between 1.4.0 and 1.5.0.