Close

Font Rendering Options (With Caching)

A project log for SilviaPiPID

Rebuilding an old Arduino Based PID for a Rancilio Silvia with a PI Zero and Rust

David P SmithDavid P Smith 10/24/2020 at 18:520 Comments

So after my analysis with perf, It looked like the biggest inefficiency was with the way I was using `rusttype` . The draw function was re-rasterizing each character in my onscreen clock for every clock tick. There are several ways to deal with that, but I didn't want to handle it entirely in more complicated app code. I wanted a way to push that kind of issue down into the framework.

With my initial use of rusttype I was having to rasterize the entire string of chars for the clock every second (ie every time the clock ticked forward by one second). This means that every character before the seconds was being rasterized when for most of the time we only needed to rasterize the last couple of chars in the string. 

At first I though I could just break down the string a bit into segments, and each segment would be updated only when that time char was updated.. ie render the hours number only when the hours changed. But as a general method this felt like it was putting too much logic into app side of things, when the framework itself should have a better way to do it.

Enter ab_glyph. One of the authors of rusttype was already working on some code that would help this situation out. While ab_glyph was intended to offload some work to a GPU, it had enough gpu independent code to create a local font cache. This means that I only have to rasterize characters when they differ enough from what's in the cache to require it. 

It took a bit of work to follow along with how the system works, but after awhile I got it all up and running. And in the process shaved about 1% of cpu usage off! (Hey 1% is important on a raspberry pi zero)

Discussions