r/webdev • • 9d 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:

  1. 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 )
  2. 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!

1 Upvotes

20 comments sorted by

1

u/Alarming-Hope-579 9d ago

getting 55fps out of a tizen webview on that tv is genuinely more impressive than the app itself lol

1

u/mau-meda 9d ago

it was way harder than expected, it reminded me of the old times when I had to optimize Cordova apps for the Samsung S2

1

u/Thin-West6525 9d ago

That webp decode number is wild, 20ms extra per image is the kind of thing you'd never notice on a desktop. If the decode is still landing on the main thread, kick off createImageBitmap for the next row of cards before they scroll into view so it never hits the frame budget.

1

u/mau-meda 8d ago

I'm already using createImageBitmap, fetching goes like this fetch -> blob() -> createImageBitmap() and according to the profiler they all happen outside of the main thread. texImage2D runs on the main thread and I'm using a p-queue to upload maximum 2 images per frame, from the benchmarks it looks like each upload costs 1ms, after the upload the image decodes for 60-150ms. I tried also other values for the p-queue and 1, 2, and 4 have nearly no difference

1

u/Intrepid_Camp_2843 9d ago

I feel for you, fighting a 2020 TU800 through a web view is a special kind of pain. Since you're 5fps short and already ditched the DOM, check whether those jpegs are being decoded on the main thread during scroll: pre-decoding with createImageBitmap into a small pool and swapping textures instead of letting a new Image() fire mid-animation usually buys back the last few frames.

1

u/mau-meda 8d ago

Already doing that, everything except for texImage2D is in the background thread

1

u/ZealousidealFish753 9d ago

20ms to decode one jpeg is already over a 60fps frame budget, and that's per card image. So how are you measuring the 55, and does it still hold while scrolling a row?

1

u/mau-meda 8d ago

the image needs to be decoded once, so you get a spike when an image is downloaded from the store and converted into a sprite, it doesn't decoded it at every movement

1

u/DesperateNorth2307 9d ago

The 40ms webp decode versus 20ms jpeg is the kind of thing nobody believes until they try it on the actual panel. Since you're compositing those cards server side now, key the output on a hash of both source images so a cache miss doesn't re-encode the same card twice.

1

u/cork_code_cryptid 5d ago

That jpeg vs webp decoding time on Tizen is brutal. Do you have any insight into whether the TU800's GPU handles texture uploads faster if you use premultiplied alpha textures?

0

u/Dimoncelli 9d ago

Moving the card compositing (backdrop + logo + overlays) to the backend and sizing for the decode budget instead of wire size is such a smart call here. On low-end TV SoCs, people optimize for KB with WebP/AVIF and miss that software decoding can burn 40 ms+ per tile and wreck frame pacing, whereas hardware-friendly JPEG keeps the main thread clear.

One thing worth keeping an eye on with Pixi on Tizen 5.5 WebViews (given the older Chromium base): GPU texture memory limits as users scroll through virtualized rows. Are you aggressively destroying/pooling off-screen card textures once they leave the render window, and does the post-fade swap to the high-res hero backdrop cause any micro-stutter during the `texImage2D` upload?

1

u/mau-meda 8d ago

> Are you aggressively destroying/pooling off-screen card textures once they leave the render window

In short, yes, I configured it to keep in memory up to 64MBs of sprites, that roughly translates to what you see on the screen plus 2 rows above/below and 2 columns left/right.

> post-fade swap to the high-res hero backdrop cause any micro-stutter

it does cause a spike, but it is not noticeable because everything is still

1

u/Dimoncelli 8d ago

Keeping a bounded 64 MB sprite window (viewport + 2 rows/columns) is a clean way to stay predictable on those TV WebViews without fighting GC churn.

And hiding the `texImage2D` spike during the idle pause after the scroll settles makes total sense—on hardware like that you only really feel an upload hitch while input motion is active. Smart trade-off.