Fix browser source tearing with hardware acceleration #531

Open
hubert-blasttv wants to merge 1 commit from blastorg/fix/source_tearing into master
hubert-blasttv commented 2026-08-04 08:50:09 -05:00 (Migrated from github.com)

Description

Since the CEF M124 OSR rework, OnAcceleratedPaint hands out a texture from a small internal pool, with no keyed mutex. The handle "cannot be cached and cannot be accessed outside of this callback" and the contents "should be copied to a texture owned by the client application". "Resources will be released to the underlying pool for reuse when the callback returns" and that the texture "is instantiated without a keyed mutex".

The current implementation does the opposite: open the handle, keep the resource as bs->texture, and let the render thread sample it during composition at some later point. When the compositor falls behind under CPU load, CEF is already writing a later frame into that same pooled texture while we sample it, which shows up as the one-frame swaps and tears from the issue.

Fix

CopyAcceleratedTexture() now copies the frame into a texture we own, inside the callback, while the handle is guaranteed valid. The copy target is reused across frames and only reallocated when the size or format changes. gs_flush() submits the copy before the callback returns, since the pooled texture can be reused after that. The render thread then samples our private, stable copy.

The gs_texture_acquire_sync(shared, 1, 50) is a no-op right now but it cooperates, with a bounded wait, if a future producer exposes a keyed mutex.

One note: the flush submits the copy but cannot fence it, because CEF exposes no fence through this API. This is as close to the documented contract as a consumer can get. The exposure window shrinks from a full compositor frame of sampling to command submission granularity, and the pool depth gives the copy several frame intervals of slack in practice.

Motivation and Context

Recurrence of #488

How Has This Been Tested?

Retested multiple times against example from #488, and with python script to detect tear artifacts.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
### Description Since the CEF M124 OSR rework, `OnAcceleratedPaint` hands out a texture from a small internal pool, with no keyed mutex. The handle ["cannot be cached and cannot be accessed outside of this callback"](https://github.com/chromiumembedded/cef/blob/af8ada6a3ab9878669a3a6c46d9e0402279ac068/include/cef_render_handler.h#L163-L166) and the contents "should be copied to a texture owned by the client application". ["Resources will be released to the underlying pool for reuse when the callback returns"](https://github.com/chromiumembedded/cef/blob/af8ada6a3ab9878669a3a6c46d9e0402279ac068/include/internal/cef_types_win.h#L130-L131) and that the texture "is instantiated without a keyed mutex". The current implementation does the opposite: open the handle, keep the resource as `bs->texture`, and let the render thread sample it during composition at some later point. When the compositor falls behind under CPU load, CEF is already writing a later frame into that same pooled texture while we sample it, which shows up as the one-frame swaps and tears from the issue. #### Fix `CopyAcceleratedTexture()` now copies the frame into a texture we own, inside the callback, while the handle is guaranteed valid. The copy target is reused across frames and only reallocated when the size or format changes. `gs_flush()` submits the copy before the callback returns, since the pooled texture can be reused after that. The render thread then samples our private, stable copy. The `gs_texture_acquire_sync(shared, 1, 50)` is a no-op right now but it cooperates, with a bounded wait, if a future producer exposes a keyed mutex. One note: the flush submits the copy but cannot fence it, because CEF exposes no fence through this API. This is as close to the documented contract as a consumer can get. The exposure window shrinks from a full compositor frame of sampling to command submission granularity, and the pool depth gives the copy several frame intervals of slack in practice. ### Motivation and Context Recurrence of #488 ### How Has This Been Tested? Retested multiple times against example from #488, and with python script to detect tear artifacts. ### Types of changes - Bug fix (non-breaking change which fixes an issue) ### Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I have read the [**contributing** document](https://github.com/obsproject/obs-studio/blob/master/CONTRIBUTING.md). - [x] My code has been run through [clang-format](https://github.com/obsproject/obs-studio/blob/master/.clang-format). - [x] My code follows the project's [**style guidelines**](https://github.com/obsproject/obs-studio/blob/master/CODESTYLE.md) - [x] My code is not on the master branch. - [x] My code has been tested. - [x] All commit messages are properly formatted and commits squashed where appropriate. - [x] I have included updates to all appropriate documentation.
notr1ch (Migrated from github.com) reviewed 2026-08-04 08:50:09 -05:00
WizardCM (Migrated from github.com) reviewed 2026-08-04 08:50:09 -05:00
Dan-Shields commented 2026-08-04 08:58:22 -05:00 (Migrated from github.com)

I have independently tested the patch and this solves our issue!

I have independently tested the patch and this solves our issue!
This pull request is broken due to missing fork information.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'blastorg/fix/source_tearing:blastorg/fix/source_tearing'
git switch 'blastorg/fix/source_tearing'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'master'
git merge --no-ff 'blastorg/fix/source_tearing'
git switch 'blastorg/fix/source_tearing'
git rebase 'master'
git switch 'master'
git merge --ff-only 'blastorg/fix/source_tearing'
git switch 'blastorg/fix/source_tearing'
git rebase 'master'
git switch 'master'
git merge --no-ff 'blastorg/fix/source_tearing'
git switch 'master'
git merge --squash 'blastorg/fix/source_tearing'
git switch 'master'
git merge --ff-only 'blastorg/fix/source_tearing'
git switch 'master'
git merge 'blastorg/fix/source_tearing'
git push origin 'master'
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-obsproject/obs-browser!531
No description provided.