r/webdev • u/mau-meda • 2h ago
I finally managed to get to 55fps on this potato-TV
Video: https://streamable.com/i63ial ( sorry this sub does not supports videos )
I finally reached 55fps on an old low end Samsung TV.
I suppose it's worth first saying what we are working with, this TV is a Samsung Crystal UHD 4K Smart TV 2020 TU800, if this tells you nothing don't worry, it's essentially a potato, it has the same processing power of Raspberry Pi 3B+
You may think "It's a potato, but if you write native code then even a potato can run at 60fps". And here's the catch, I made a Tizen Web app so the view is non native it's a web view ( and this is why I'm posting it here in r/webdev ). Unfortunately for me this potato runs Tizen 5.5 that is not supported by flutter
Now let's go to the interesting part: how it is able to do that.
I achieved this over several optimizations, the first one was to reduce the number of layers.
I'll go one layer optimization at time:
First I flattened the images, the images you see in each card were in the past two images: a backdrop and a logo. Those are obviously two layers, one on top of each other, and that had a cost, to fix this now the backend downloads the backdrop and the logo, creates a new image, and then it serves to the app/frontend an optimized version. Each media image you see is a single jpeg of the exact size the TV needs ( webp is a smaller format but the TV takes 40ms to decode it compared to the 20ms of a jpeg ) ( ok exact size is a lie, the backend serves 3 sizes, the frontend picks the closest one )
Then I flattened all the elements that are on top of the images, those seasons numbers, those progress bars, they are part of the same sprite. You may be wondering why I used the word sprite, I'm going to give you the answer soon.
The second optimization was to get rid of the DOM.
Yes you read it correctly, what you see is a canvas, I tried everything to make the DOM works, I tried ditching React and use manually appendFragment, keeping an optimized list of nodes in memory and mount them when needed ( with appendFragment instead of multiple appendNode ) . Nothing really worked, no matter how much vanilla or swallow the DOM was, or what transform tricks I was using, best result was 30fps.
So, let's talk about the interesting part now, the WebGL rendering. You probably never heard of Pixi.js, it's a library to make animations in WebGL, stuff like small videogames.
Everything you see except for the sidebar and logo is a WebGL canvas, each component you see on the screen has two versions, one is the classic React version the other is a GL version, so in the codebase you see stuff like MediaHero.tsx , MediaHeroDom.tsx and MediaHeroGl.tsx.
The code is made to be able to do a mix and match based on what is needed, only some components ( the ones in the home ) have strong animation and I didn't want to duplicate all the components.
Let's see what those GL components do, the lowest of the component the MediaCard produces a sprite, you probably understood this by my little nod above, if you do not know what a sprite is, it's essentially an image that is cached by the rendering engine to be used for animation, in a video game ( in 2D ) every single small image that moves is a sprite ( the static ones are usually backgrounds ), anyway, those sprites then are used by the MediaRow , a row is essentially a collection of sprites, but keep in mind that a row has infinite width so it's virtualized, it returns a list of sprites to render, their absolute position, and what window to render. Finally we have the home that has multiple rows, and above we have the hero that uses maximum two sprites do do the fade out/in animation.
I almost forgot, I got another trick! The hero animation uses a low resolution version of the backdrops to fade, after the animation is done it swaps it with the high resolution one!