perf(web): replace softbuffer canvas present with direct put_image_data#1374
perf(web): replace softbuffer canvas present with direct put_image_data#1374irvingouj@Devolutions (irvingoujAtDevolution) wants to merge 1 commit into
Conversation
The render path converted each dirty region RGBA -> u32 `0RGB`, then let softbuffer repack u32 -> RGBA into a freshly allocated buffer every frame — two pixel passes over the whole surface plus a per-frame allocation. Replace it with the canvas's own 2D context: one copy of the region into a reused RGBA scratch (alpha forced opaque) followed by put_image_data at the region origin. softbuffer is dropped from ironrdp-web (still used by ironrdp-viewer). Mirrors the same fix in IronVNC. Measured with a record/replay draw bench (dev wasm, headless Chromium), draw-stage median: 4K 1706ms -> 83ms (~20x), 1080p 705ms -> 14ms (~50x), with byte-identical canvas output and unchanged framebuffer checksums.
|
Same as VNC, remove softbuffer |
There was a problem hiding this comment.
Pull request overview
This PR updates the ironrdp-web rendering path to remove the softbuffer dependency and present updated regions by uploading RGBA buffers directly to an HTML canvas via CanvasRenderingContext2d::put_image_data, aiming to reduce per-frame work and allocations.
Changes:
- Replaces the softbuffer-based canvas present path with direct
ImageData+put_image_datablits for dirty regions. - Removes the
softbufferdependency fromironrdp-web. - Enables additional
web-sysfeatures needed for 2D canvas rendering (CanvasRenderingContext2d,ImageData).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/ironrdp-web/src/canvas.rs | Implements the new 2D-context put_image_data rendering path and removes the softbuffer surface logic. |
| crates/ironrdp-web/Cargo.toml | Drops softbuffer dependency; enables required web-sys features for 2D canvas + ImageData. |
| Cargo.lock | Updates lockfile dependency edges to reflect removal of softbuffer from ironrdp-web. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| }) | ||
| } | ||
|
|
||
| /// Setting width/height resets the canvas backing store; the 2D context persists. |
| pub(crate) fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) { | ||
| self.surface.resize(width, height).expect("surface resize"); | ||
| self.width = width; | ||
| self.canvas.set_width(width.get()); | ||
| self.canvas.set_height(height.get()); |
| if self.rgba.len() < len { | ||
| self.rgba.resize(len, 0); | ||
| } | ||
| let dst = &mut self.rgba[..len]; | ||
| dst.copy_from_slice(buffer); |
There was a problem hiding this comment.
We can do it faster without filling the buffer with zeros prior to copying, since we are initializing it with data from buffer after all.
if self.rgba.len() < len {
self.rgba.reserve(len);
// SAFETY: We later initialize the exact `len` of `self.rgba` by copying `len` bytes from `buffer`.
unsafe { self.rgba.set_len(len) };
}
let dst = &mut self.rgba[..len];
dst.copy_from_slice(buffer);But, I wonder how much of speed up it gives.
Also, we could even make it branchless:
let target_len = max(self.rgba.len(), len);
// In, case `target_len` == `self.rgba.len()`, `reserve` does nothing, and `self.rgba.set_len` sets a length, which it already contains.
// In, case ``target_len` == `len`, it's the same case as above.
self.rgba.reserve(target_len);
// SAFETY: We later initialize the exact `len` of `self.rgba` by copying `len` bytes from `buffer`.
unsafe { self.rgba.set_len(target_len ) };
let dst = &mut self.rgba[..target_len];
dst.copy_from_slice(buffer);But, we need to measure both versions. The first one should definitely be faster, than filling with zero.
As for the second version, I'm not sure - we don't have len comparing branch, which is great, but it means that a branch inside self.rgba.reserve(...); executes more often.
What
Removes the
softbufferdependency fromironrdp-weband presents canvas updates via the 2D context'sput_image_datadirectly.Why
The old render path did two full-surface pixel passes per frame (RGBA → u32
0RGB, then softbuffer repacks u32 → RGBA into a freshly allocated buffer) plus a per-frame allocation. The new path does a single copy of each dirty region into a reused RGBA scratch (alpha forced opaque) followed byput_image_dataat the region origin.Perf
Record/replay draw bench (dev wasm, headless Chromium), draw-stage median:
Byte-identical canvas output; unchanged framebuffer checksums. Mirrors the same fix already in IronVNC.
Notes
softbufferis only removed fromironrdp-web;ironrdp-viewer(native) still uses it, so it remains inCargo.lock.CanvasRenderingContext2d+ImageDatafeatures.