/**
* Keep track of animations and surface operations for a single WindowState.
**/
class WindowStateAnimator {
/** This is set when there is no Surface */
static final int NO_SURFACE = 0;
/** This is set after the Surface has been created but before the window has been drawn. During
* this time the surface is hidden. */
static final int DRAW_PENDING = 1;
/** This is set after the window has finished drawing for the first time but before its surface
* is shown. The surface will be displayed when the next layout is run. */
static final int COMMIT_DRAW_PENDING = 2;
/** This is set during the time after the window's drawing has been committed, and before its
* surface is actually shown. It is used to delay showing the surface until all windows in a
* token are ready to be shown. */
static final int READY_TO_SHOW = 3;
/** Set when the window has been shown in the screen the first time. */
static final int HAS_DRAWN = 4;
WindowStateAnimator 是 WMS 中每个窗口对应的 Surface 动画与绘制状态管理器。它维护了从 Surface 创建到最终显示到屏幕的完整生命周期,核心职责包括:
| 职责 | 说明 |
|---|---|
| Surface 生命周期管理 | 创建 (createSurfaceLocked)、销毁 (destroySurfaceLocked) 窗口的 SurfaceControl |
| 绘制状态机 | 管理 mDrawState 的 5 个状态流转(核心机制) |
| Surface 属性控制 | 控制透明度 (setAlpha)、可见性 (show/hide)、不透明度 (setOpaque)、颜色空间等 |
| 入场动画 | applyEnterAnimationLocked() 为窗口首次显示播放动画 |
| Transaction 提交 | finishDrawingLocked() 将客户端的 postDrawTransaction 合并到 getSyncTransaction() |
完整时序图
BUFFER 的 fence 机制
当 App 通过 eglSwapBuffers / queueBuffer 将 buffer 提交给 BLASTBufferQueue 时,buffer 带有一个 fence(GraphicBuffer 的 acquireFence):
当 App 通过 eglSwapBuffers / queueBuffer 将 buffer 提交给 BLASTBufferQueue 时,buffer 带有一个 fence(GraphicBuffer 的 acquireFence):
queueBuffer(buffer, fence) → BBQ
│
├── buffer 进入 BBQ 的待处理队列
├── fence 被附加到 Transaction 中
└── Transaction → SF
│
├── SF: t.show() → Surface 可见
├── SF: 尝试合成 buffer
├── SF: 发现 fence 未 signal
├── SF: 等待 fence ← GPU 仍在渲染
├── ... GPU 完成 → fence signal
└── SF: 合成并显示
绘制状态与屏显的关系
| 时间点 | 事件 | GPU 状态 | mDrawState |
|---|---|---|---|
| T1 | GPU 命令提交,queueBuffer | 渲染中 | DRAW_PENDING |
| T2 | finishDrawing() → WMS | 渲染中 | COMMIT_DRAW_PENDING |
| T3 | commitFinishDrawingLocked() | 渲染中 | READY_TO_SHOW |
| T4 | performShowLocked() | 渲染中 | HAS_DRAWN ✅ |
| T5 | showRobustly() → t.show() | 渲染中 | HAS_DRAWN |
| T6 | GPU fence signal | 完成 ✅ | HAS_DRAWN |
| T7 | SF 合成并显示 | 完成 | HAS_DRAWN |
HAS_DRAWN 是 WMS 侧的"决策状态",不是 GPU 侧的"完成状态"。从 HAS_DRAWN 设置到 GPU 真正完成渲染之间的时间差,由 buffer 的 acquire fence 机制来保证画面正确性——SF 会在合成前自动等待 fence,不会出现显示不完整帧的问题。

243

被折叠的 条评论
为什么被折叠?



