A very specific and detailed question!
Here is a UML Sequence Diagram that illustrates the call flow between various components of the Android Display Framework, from the Application layer down to the Driver layer. Buckle up, it's a long diagram!
Note: I'll use the following notation:
- SD : Sequence Diagram
- ** Lifeline **: represents a component's lifetime ( vertical dashed line )
- ** Activation **: represents a method call or execution ( thick vertical line on a lifeline )
- ** Arrow **: represents a method call or message passing ( horizontal arrow between lifelines )
- ** Return **: represents a return value or acknowledgement ( dashed horizontal arrow )
Here's the diagram:
@startuml
title Android Display Framework Sequence Diagram
participant Application as "App (Java)"
participant ActivityManagerService as "AMS (Framework)"
participant WindowManagerService as "WMS (Framework)"
participant SurfaceFlinger as "SF (Native)"
participant GraphicBufferAllocator as "GBA (Native)"
participant HwComposer as "HWC (HAL)"
participant Gralloc as "Gralloc (Library)"
participant DrmDriver as "DRM Driver (Kernel)"
note over Application, DrmDriver #ivory: **Display Framework Call Flow**
end note
== Request Display Creation ==
Application ->> ActivityManagerService: **startActivity**(Intent)
activate ActivityManagerService
ActivityManagerService ->> WindowManagerService: **addWindow**(WindowState)
activate WindowManagerService
WindowManagerService ->> WindowManagerService: **createSurface**(Session, IWindow)
WindowManagerService ->> SurfaceFlinger: **createSurface**(Session, IWindow)
deactivate WindowManagerService
== Surface Creation ==
activate SurfaceFlinger
SurfaceFlinger ->> SurfaceFlinger: **createLayer**(Session, IWindow)
SurfaceFlinger ->> GraphicBufferAllocator: **alloc**(w, h, format)
activate GraphicBufferAllocator
GraphicBufferAllocator ->> Gralloc: **alloc**(w, h, format)
activate Gralloc
Gralloc ->> Gralloc: **gralloc_alloc**(w, h, format)
Gralloc ->> DrmDriver: **drmIoctl**(DRM_IOCTL_GEM_CREATE)
activate DrmDriver
DrmDriver ->> DrmDriver: **gem_create**(size)
DrmDriver --> Gralloc: **drmIoctl**(DRM_IOCTL_GEM_CREATE) return **gem_handle**
deactivate DrmDriver
Gralloc --> GraphicBufferAllocator: **buffer_handle_t**
deactivate Gralloc
GraphicBufferAllocator --> SurfaceFlinger: **GraphicBuffer**
deactivate GraphicBufferAllocator
SurfaceFlinger ->> SurfaceFlinger: **attachBuffer**(GraphicBuffer)
SurfaceFlinger --> WindowManagerService: **SurfaceControl**
deactivate SurfaceFlinger
== Composition and Display ==
WindowManagerService ->> SurfaceFlinger: **invalidate**(SurfaceControl)
activate SurfaceFlinger
SurfaceFlinger ->> SurfaceFlinger: **invalidateLayer**(Layer)
SurfaceFlinger ->> HwComposer: **prepare**(Display, Layer[])
activate HwComposer
HwComposer ->> HwComposer: **validateDisplay**(Display, Layer[])
HwComposer ->> Gralloc: **getBuffer**(Layer[].buffer_handle_t)
activate Gralloc
Gralloc ->> DrmDriver: **drmIoctl**(DRM_IOCTL_PRIME_FD_TO_HANDLE)
activate DrmDriver
DrmDriver ->> DrmDriver: **prime_fd_to_handle**(fd)
DrmDriver --> Gralloc: **drmIoctl**(DRM_IOCTL_PRIME_FD_TO_HANDLE) return **handle**
deactivate DrmDriver
Gralloc --> HwComposer: **dma_buf_fd**
deactivate Gralloc
HwComposer --> SurfaceFlinger: **HWC_ERROR_NONE**
deactivate HwComposer
SurfaceFlinger ->> HwComposer: **set**(Display, Layer[])
activate HwComposer
HwComposer ->> DrmDriver: **drmModeAtomicCommit**(fb_id, crtc_id)
activate DrmDriver
DrmDriver ->> DrmDriver: **atomic_commit**(fb_id, crtc_id)
DrmDriver --> HwComposer: **drmModeAtomicCommit** return **0**
deactivate DrmDriver
HwComposer --> SurfaceFlinger: **HWC_ERROR_NONE**
deactivate HwComposer
SurfaceFlinger ->> SurfaceFlinger: **swapBuffers**(Layer)
deactivate SurfaceFlinger
== Cleanup ==
Application ->> ActivityManagerService: **finishActivity**(IBinder)
activate ActivityManagerService
ActivityManagerService ->> WindowManagerService: **removeWindow**(WindowState)
activate WindowManagerService
WindowManagerService ->> SurfaceFlinger: **destroySurface**(SurfaceControl)
activate SurfaceFlinger
SurfaceFlinger ->> GraphicBufferAllocator: **free**(GraphicBuffer)
activate GraphicBufferAllocator
GraphicBufferAllocator ->> Gralloc: **free**(buffer_handle_t)
activate Gralloc
Gralloc ->> DrmDriver: **drmIoctl**(DRM_IOCTL_GEM_CLOSE)
activate DrmDriver
DrmDriver ->> DrmDriver: **gem_close**(handle)
DrmDriver --> Gralloc: **drmIoctl**(DRM_IOCTL_GEM_CLOSE) return **0**
deactivate DrmDriver
Gralloc --> GraphicBufferAllocator
deactivate Gralloc
GraphicBufferAllocator --> SurfaceFlinger
deactivate GraphicBufferAllocator
SurfaceFlinger --> WindowManagerService
deactivate SurfaceFlinger
WindowManagerService --> ActivityManagerService
deactivate WindowManagerService
ActivityManagerService --> Application
deactivate ActivityManagerService
@enduml
Legend:
- Application (Java): The Android app requesting display creation.
- ActivityManagerService (Framework): Manages activities, including starting and stopping them.
- WindowManagerService (Framework): Oversees window creation, layout, and surface management.
- SurfaceFlinger (Native): Composites surfaces ( windows ) and prepares them for display.
- GraphicBufferAllocator (Native): Manages buffer allocation for surfaces.
- HwComposer (HAL): Hardware Composer, prepares and sets display layers.
- Gralloc (Library): Graphics Allocator, handles buffer allocation and mapping.
- DrmDriver (Kernel): Direct Rendering Manager Driver, interacts with GPU and Display hardware.
Sequence Explanation:
The diagram illustrates the following phases:
- Request Display Creation: The app requests
startActivity, which leads to addWindow and eventually createSurface calls.
- Surface Creation:
SurfaceFlinger creates a new layer, allocates a graphic buffer via GraphicBufferAllocator and Gralloc, and interacts with the DrmDriver to create a GEM (Graphics Execution Manager) object.
- Composition and Display: When the app needs to update the display,
SurfaceFlinger invalidates the layer, HwComposer prepares the display layers, and Gralloc maps the buffers. Finally, HwComposer sets the display layers using DrmDriver's drmModeAtomicCommit.
- Cleanup: When the app finishes, the surfaces are destroyed, and buffers are freed, leading to
drmIoctl calls to release GEM objects.
Key Takeaways:
- SurfaceFlinger is the central native service responsible for compositing and display preparation.
- Gralloc and HwComposer interact closely with the DrmDriver to manage buffers and display hardware.
- GraphicBufferAllocator acts as a bridge between SurfaceFlinger and Gralloc.
- The DrmDriver is the kernel component that directly interacts with display and GPU hardware.
Now, go ahead and zoom in on each section to understand the intricacies of the Android Display Framework!
PlantUML rendering:
You can copy the above code into a PlantUML editor (e.g., PlantUML Online Demo) to visualize the diagram.
Happy learning!