Skip to content

Fix EGL surface recreation after native handle changes - #21816

Merged
MrJul merged 2 commits into
AvaloniaUI:mainfrom
AB901:agent/fix-egl-window-handle-cache
Jul 24, 2026
Merged

Fix EGL surface recreation after native handle changes#21816
MrJul merged 2 commits into
AvaloniaUI:mainfrom
AB901:agent/fix-egl-window-handle-cache

Conversation

@AB901

@AB901 AB901 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

What changed

  • Cache the native window handle and pixel size once at the start of BeginDrawCore.
  • Update the cached handle after successfully recreating the EGLSurface.
  • Use the same captured size for recreation and the rendering session.

Root cause

Android can destroy a SurfaceView surface while an activity is in the background and provide a new ANativeWindow when the activity resumes. EglGlPlatformSurface.RenderTarget detects that IEglWindowGlPlatformSurfaceInfo.Handle changed and recreates its EGLSurface, but _handle was readonly and was never updated.

After the first handle change, the mismatch therefore remained true for every frame. The renderer repeatedly disposed and recreated the EGLSurface, continued hitting the abandoned BufferQueue associated with the previous native window, reduced frame rate, and could eventually leave the application surface black.

User impact

The cached handle now follows the window surface that was used to create the current EGLSurface. A handle or size change recreates the EGL surface once instead of once per rendered frame.

Reproduction and validation

Test device: OPPO PEDM00, Android 14, 120 Hz display.

Reproduction:

  1. Launch an Avalonia Android application.
  2. Press Home and reopen the application without terminating its process.
  3. Trigger an animation and repeat the Home/resume cycle.

Hardware A/B test using the same application and lifecycle code:

  • Unmodified Avalonia 12.1.0 assembly: three Home/resume cycles produced 156 BufferQueue has been abandoned messages and 157 dequeueBuffer failures, repeating at roughly 8 ms intervals.
  • Assembly with this patch: three equivalent cycles produced no BufferQueue has been abandoned messages and no abandoned-queue dequeueBuffer failures; the process remained alive and the surface stayed visible.

Build validation:

dotnet build .\src\Avalonia.OpenGL\Avalonia.OpenGL.csproj -c Release --no-restore
Build succeeded: 0 warnings, 0 errors (net8.0 and net10.0).

An automated regression test is not included because reproducing the failure requires replacing a real native EGL window during the Android SurfaceView lifecycle; the current OpenGL test infrastructure does not provide an injectable window-surface/EGL display boundary.

@AB901
AB901 marked this pull request as ready for review July 21, 2026 19:43
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0067599-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@cla-avalonia

cla-avalonia commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator
  • All contributors have signed the CLA.

@MrJul MrJul added bug os-android backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Jul 21, 2026
@AB901

AB901 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@cla-avalonia agree

@MrJul
MrJul enabled auto-merge July 24, 2026 14:29
@MrJul
MrJul added this pull request to the merge queue Jul 24, 2026
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0067705-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Merged via the queue into AvaloniaUI:main with commit 06fdb64 Jul 24, 2026
9 checks passed
MrJul added a commit that referenced this pull request Jul 29, 2026
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants