Mobile Development 11 min read

Flutter iOS GPU Background Crash Analysis and Solution

The article analyzes why Flutter crashes on iOS when accessing the GPU in the background, explains the official SyncSwitch fix for ImageDecoder, and details Xianyu’s additional patches for MultipleFrameCodec, EncodeImage, and Rasterizer::DrawToSurface that together, via PR #28383, fully resolve the GPU‑background crash.

Xianyu Technology
Xianyu Technology
Xianyu Technology
Flutter iOS GPU Background Crash Analysis and Solution

Flutter在iOS后台访问GPU导致Crash问题分析与解决

本文分析Flutter在iOS后台访问GPU导致Crash的原因,包括GPU后台Crash发生的原因、官方的修复方案(SyncSwitch机制)以及闲鱼在其他三个场景(MultipleFrameCodec、EncodeImage、Rasterizer::DrawToSurface)下的解决方案。

官方通过SyncSwitch机制解决ImageDecoder导致的GPU后台Crash,具体代码如下:

void SyncSwitch::SetSwitch(bool value) { ... }

闲鱼将Flutter引擎升级并打上官方修复Patch后,发现GPU后台Crash问题未完全解决,分析发现问题分布在3个地方:MultipleFrameCodec、EncodeImage以及DrawToSurface。

针对MultipleFrameCodec场景的Crash,官方通过SyncSwitch机制确保GPU可用时调用SkImage::MakeCrossContextFromPixmap生成SkImage,否则回退到SkImage::MakeFromBitmap生成SkImage。

针对EncodeImage场景的Crash,闲鱼在ConvertToRasterUsingResourceContext方法中使用SyncSwitch机制确保GPU可用时调用SkImage::MakeCrossContextFromPixmap,否则回退到SkImage::MakeFromBitmap。

针对Rasterizer::DrawToSurface场景的Crash,闲鱼同样使用SyncSwitch机制确保GPU可用时调用SkImage::MakeCrossContextFromPixmap,否则回退到SkImage::MakeFromBitmap。

最终,闲鱼和官方共同完成了PR #28383,将GPU sync switch提供给Rasterizer::DrawToSurface方法,解决Flutter在iOS后台访问GPU导致Crash的问题。

FlutteriOSGPUcrashImageDecoderMetalOpenGLESRasterizerSyncSwitch
Xianyu Technology
Written by

Xianyu Technology

Official account of the Xianyu technology team

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.