Native Buffers and the Heartbeat of a Frame
React Native Skia 3.0 came out last month, and it is all about WebGPU. Since then, we have refined the Native Buffer API and added a view that hands native buffers straight to the platform's compositor. We have also gone deeper into each platform's rendering APIs, so that every frame keeps pace with the display.
Native Buffers
Our design goal with React Native WebGPU is to stay symmetrical with the Web while enabling native platform capabilities. This has two consequences: code written for WebGPU in the browser runs as is, and native platform capabilities are available when you need them. These native extensions to the WebGPU API are documented here.
A native buffer is the platform's own GPU buffer: an AHardwareBuffer on Android, and an IOSurface on Apple platforms.
On the Web, external content comes into WebGPU in two ways: importExternalTexture samples a video frame, and copyExternalImageToTexture copies an image, a bitmap or a canvas into a texture.
In React Native WebGPU, both also accept a native buffer as their source.
Both are read-only, so we add a third entry point, from Dawn: importSharedTextureMemory, which gives the device a texture backed by the native buffer, to sample from or to render into.
This is what lets video stay on the GPU from end to end, without going through the CPU: import video and camera feeds, render 2D and 3D scenes, and encode the result into a video file.
React Native Skia Video does exactly that in its 1.0 release candidate, built on React Native Skia 3 and React Native WebGPU.
A composition is a timeline of clips and a drawFrame worklet, which receives the decoded frames as Skia images and draws whatever Skia can draw.
In practice, there are two ways in.
To sample a native buffer, use importExternalTexture as you would on the Web, with the buffer as its source.
To write into a native buffer, use importSharedTextureMemory to get a GPUTexture that you can render into.
Because the native buffer is owned outside of the GPU device, this access must be synchronized explicitly: beginAccess() and endAccess() bracket the work, and fences tell each side when the other is done with the buffer.
importExternalTexture takes care of this for you.
We introduced native buffers in React Native Skia before WebGPU came into the picture.
They let us draw frames from the camera or a video decoder into a Skia canvas, without copying them through the CPU.
In the pre-WebGPU era, this support was brittle (to say the least): few texture formats, YUV handled by hand, and no fences, so nothing kept a frame from being sampled while the decoder or the camera was still writing it.
Still, we already had in mind that native buffers would also be useful for content that doesn't change on every frame.
This brings us to NativeBufferView, which displays the content of a native buffer as is, composited directly by the platform.
In React Native Skia, this is useful for drawings that change now and then, but don't need to be animated continuously in a canvas.
A Canvas Without a Swap Chain
The very first question Christian Falch and I received when we started working on React Native Skia was: how expensive is a Canvas, compared to a regular View?
And that question kept coming back throughout the development of the library.
Our answer was that it cost about the same, and as a rule of thumb, that was fair.
Today, however, with WebGPU and modern Android graphics APIs, we can do better.
A Canvas comes with a swap chain: a few buffers it cycles through to show a new frame on every refresh of the display.
In that context, a NativeBufferView can be seen as a bare-metal canvas without the swap chain.
It holds one buffer, the last one it was given, and nothing else.
Because React Native Skia shares its GPU device with React Native WebGPU, it can draw straight into a native buffer, and NativeBufferView hands that buffer to the platform to display.
To see what this buys, we built a screen of 200 charts, like the watchlist of a finance app.
Each card is a Skia drawing of its own data: a ticker, a price, its change and a sparkline.
And the data is live: every quarter of a second, a few cards get a new point, and only those are drawn again while the rest of the screen stays as it is.
This is what NativeBufferView is made for: drawings that depend on data and change now and then.
200 live charts, drawn by Skia
Slowed down 4×
CanvasSwap chainBuilt for 120 fps animationsNativeBufferViewNo swap chainBuilt for infrequent updates5×less memory on 200 views
The canvases come up in batches and out of order, and the last card on screen shows 235 to 260 ms after the grid mounts. The buffers fill in row after row, in 160 to 190 ms. And the larger difference is memory. On Android, 200 canvases are 200 views with a swap chain each, whereas 200 native buffer views are 200 buffers the size of a card.
The Heartbeat of a Frame
A 120 Hz display shows a new frame every 8.3 ms. Smooth rendering means that each of these slots gets exactly one new frame: two frames for one slot waste the first, and a slot with no new frame reads as a stutter. Rendering each frame fast enough is only the first part of the recipe: it also has to start, and arrive, at the right time.
In a browser, requestAnimationFrame is a frame callback: the display starts a frame, and every callback queued for it runs once.
In React Native, requestAnimationFrame is completely disconnected from the display: it is a timer, and what that timer does depends on where the loop runs:
- JS thread, Android. The timer only catches a frame of the display if it was requested before that frame started. A loop gets about 6 ms of the 8.3 ms, and above that it runs at 60 fps.
- JS thread, iOS. The display link behind the timers runs at 60 Hz, also on a ProMotion display.
- Worklet runtimes. It is a 16 ms timer: 45 to 60 fps, never in step with the display.
With React Native WebGPU, the render loop usually runs on requestAnimationFrame.
This is the symmetry with the Web again: an example copied from the Web should just work.
Since version 0.23, React Native WebGPU has its own requestAnimationFrame and cancelAnimationFrame, connected to the vsync.
Measured on a cube, with busy work added to each frame:
The example below is that cube. The graph above it is drawn by the loop itself: one bar per frame, as tall as the time since the frame before.
We took the problem upstream to React Native for the JS thread, and to Reanimated for worklet runtimes.
The answer from Software Mansion came the next day: a pull request that adds an enableVSync option to createWorkletRuntime().
Once it is released, a worklet runtime created with enableVSync: true will have a requestAnimationFrame that follows the heartbeat of the display, and this will become our recommended approach there.
Without Skipping a Beat
A Skia canvas paces itself and has access to deeper platform primitives. Each canvas has a scheduler, a plain state machine with four inputs: the heartbeat of the display, the deadline of that frame, whether the GPU is done with the frame presented before, and the time the last recordings and submits took.
The simplest way to pace a canvas is one frame at a time: the next recording starts once the previous frame is on screen, so each frame is drawn with the freshest values. That works as long as a recording fits in a frame (less than 8.3 ms on a 120 Hz display). A recording of 10 ms starts at a frame of the display, misses the next one by 1.7 ms, and goes on screen at the one after. Only then does the next recording start: every frame costs two frames of the display, and the canvas runs at 60 fps, whether its recordings take 9 ms or 16 ms. So once recordings take longer than a frame, the scheduler chains them: the next one starts as soon as the previous one ends, while its frame waits for the display. At 10 ms, a new frame is ready every 10 ms, and the display shows a new one in five slots out of six: 100 fps.
The switch works both ways, so a canvas is never stuck in the slow mode. When the scene gets heavier, the scheduler chains recordings after two late ones out of four. When it gets lighter again, it goes back to one frame at a time, and to the freshest values, once recordings have fit in a frame for about a second. That delay is deliberate: a scene that hovers around 8.3 ms would otherwise switch every few frames, and each switch back costs a frame on screen.
Happy Hacking
There is a small paradox here. As always, we set out to stay symmetrical with the Web, and even the parts of the scheduler are named after the Web. But today, on iOS and Android, we ended up with a much deeper integration of frame pacing than what the Web offers. The questions are the same on every platform: which thread records a frame, when it is submitted, and when it reaches the screen. I am looking forward to bringing these rendering techniques back to the Web: closing the loop would make it a virtuous one.
Please get in touch if you have any questions or comments. And until next time: happy hacking!
Comments