class Window {
getSurface() -> Surface
setWindowCallback(callback: WindowCallback)
//Posts a callback to run on the next frame.
//The callback runs once then is automatically removed.
postVsyncCallback(callback: VsyncCallback, type: VsyncCallbackType)
}
protocol WindowCallback {
onAttached()
onDetached()
onFocusChanged(hasFocus: Bool)
onInputEvent(event: InputEvent)
}
protocol VsyncCallback {
onVsync(frameTimeNanos: long)
}
enum VsyncCallbackType {
// Called before drawing
case animation
case drawing
}
class Surface {
isValid() -> Bool
setCallback(callback: SurfaceCallback)
}
protocol SurfaceCallback {
// Start up whatever rendering code you desire.
onSurfaceCreated()
//This method is always called at least once, after surfaceCreated.
onSurfaceChanged(format: PixelFormat, width: Int32, height: Int32)
//After returning from this call, you should no longer try to access this surface.
onSurfaceDestroyed()
//You should initiate the redraw, and return, later invoking drawingFinished when
//your redraw is complete.
onRedrawRequest(callback: DrawingFinishCallback)
}
protocol DrawingFinishCallback {
onDrawingFinished()
}
sequenceDiagram
box MindUI
participant v as Context
participant vc as VsyncCallback
participant wc as WindowCallback
participant sc as SurfaceCallback
end
participant s as Surface
participant w as Window
participant wm as WindowManager
participant c as Choreographer
v ->>+ wm: 1. addWindow(name, attrs)
wm ->> w: new
wm -->>- v: window
wc ->> w: 2. setWindowCallback(WindowCallback)
w ->> wc: onInputEvent
vc ->> w: 3. postVsyncCallback(VsyncCallback)
c ->> vc: onVsync()
vc ->> v: animating / drawing
sc ->> s: 4. setSurfaceCallback(SurfaceCallback)
c ->> w: performTraversal
w ->> wc: onAttached
w ->> wm: relayoutWindow
wm -->> w: Surface
alt surface created
w ->> sc: onSurfaceCreated
w ->> sc: onSurfaceChanged(format, width, height)
else surface destroyed
w ->> sc: onSurfaceDestroy
end
s ->> sc: onRedrawRequest(DrawingFinishCallback)
sc ->> w: onDrawingFinished
w ->> wm: reportDrawFinished
- 调用
addWindow()
创建一个窗口。
- 调用
setWindowCallback()
向 Window 设置 WindowCallback,接收 InputEvent、窗口焦点切换等回调。
- 调用
postVsyncCalback()
申请一次 VSYNC。一次 VSYNC 中 animation 类型的回调早于 drawing 类型。
- 调用
setSurfaceCallback()
向 Suface 设置 SurfaceCallback。它可以接收 Surface 变化。在 Window 的 performTraversal()
流程中:
- 调用
onAttached()
,表示 Window 已经添加到 WindowManager。
- 调用
relayoutWindow()
,向 WindowManagerService 申请/销毁/更新 Surface。
- Surface 创建时调用
onSurfaceCreated()
和 onSurfaceChanged()
。
- Surface 销毁时调用
onSurfaceDestroyed()
,之后无法访问 Surface。
- 调用
onRedrawRequest()
请求一次绘制,并设置 DrawingFinishCallback。
- 绘制完成之后回调
onDrawingFinished()
。
- 调用
reportDrawFinished()
通知 WindowManagerService 绘制一帧完成。