# me176c-dev/drmfb-composer architecture

> Architecture diagram of the GitHub repository me176c-dev/drmfb-composer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/me176c-dev/drmfb-composer
- Repository: https://github.com/me176c-dev/drmfb-composer

## Overview

##### Project type and purpose
- This is an Android HIDL HAL service (“drmfb-composer”) implementing android.hardware.graphics.composer@2.1.
- It sits between SurfaceFlinger (Android framework) and the Linux DRM/KMS subsystem, using legacy kernel mode setting to present GPU-composed buffers on one or more displays.

##### File structure
- Top-level build and config:
- Android.mk – build script for the HAL service
- android.hardware.graphics.composer@2.1-service.drmfb.rc – init script for Android
- sepolicy/… – SELinux policy for the service
- Core service code:
- service.cpp – HIDL registration and main loop
- DrmComposer*.cpp/.h – entry point implementing the HIDL IComposer interface
- DrmComposerHal.h – common HAL interface definitions
- DRM abstraction layer:
- DrmDevice.cpp/h – open /dev/dri, enumerate resources
- DrmDisplay.cpp/h – manage connectors, CRTCs, modes
- DrmFramebuffer*/.cpp/.h – import buffers (libdrm or minigbm), create framebuffers
- Threads for asynchronous events:
- DrmHotplugThread.cpp/h – monitor & signal display hotplug events
- DrmVsyncThread.cpp/h – wait on vblank and signal vsync to SurfaceFlinger
- GraphicsThread.cpp/h – serialize client requests, handle composition operations
- Utility:
- drm_unique_ptr.h – RAII wrappers for libdrm handles

##### README for architectural insights
- Confirms:
- Service is binderized HIDL HAL
- No atomic mode setting or hardware composition – all composition done by GPU + legacy FB presentation
- Supports multiple displays, modes, vsync, hotplug
- Dependencies: libdrm, minigbm (runtime selection)

a. Android Framework layer
- SurfaceFlinger (Client of HIDL Composer HAL)
- Binder driver
b. HIDL / Binder IPC
- HIDL runtime glue in service.cpp
c. drmfb-composer HAL process
- DrmComposer (implements IComposer)
- GraphicsThread (queues and dispatches composition requests)
- DrmDevice → DrmDisplay → DrmFramebuffer chain
- Importers: libdrm vs minigbm
- DrmHotplugThread & DrmVsyncThread for asynchronous events
d. Kernel / DRM subsystem
- GPU DRM driver (e.g. i915, radeon)
- Kernel Mode Setting (legacy KMS)
e. External libraries
- libdrm
- minigbm

##### Relationships and interactions
- SurfaceFlinger issues HIDL calls (setLayer, presentLayers, getDisplayConfigs, etc.) over Binder to DrmComposer
- DrmComposer delegates to GraphicsThread, which calls DrmDevice to open DRM fd
- DrmDevice enumerates resources and exposes DrmDisplay objects
- For each composition cycle:
- DrmFramebuffer importer loads buffers via PRIME fd (libdrm or minigbm)
- DrmDisplay programs CRTC and FB via legacy DRM/KMS ioctl
- DrmVsyncThread waits on UAPI DRM vblank ioctl → notifies SurfaceFlinger via callback
- DrmHotplugThread monitors hotplug uevents → triggers display add/remove to SurfaceFlinger

##### Architectural patterns & design principles
- Layered separation: HIDL interface → service orchestration → DRM abstraction → Kernel
- Thread-per-function: one thread each for hotplug, vsync, request dispatching
- RAII resource management (drm_unique_ptr)
- Runtime selection of buffer importer strategy

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Android Framework Layer
    subgraph "Android Framework"
        SF["SurfaceFlinger"]:::framework
        Binder["Binder Driver"]:::framework
    end

    %% HIDL/Binder IPC Layer
    subgraph "HIDL / Binder IPC"
        ServiceCPP["HIDL Runtime Glue\n(service.cpp)"]:::ipc
    end

    %% HAL Service Layer
    subgraph "HAL Service Components"
        DrmComposer["DrmComposer\n(IComposer impl)"]:::hal
        GraphicsThread["GraphicsThread"]:::hal
        DrmDevice["DrmDevice"]:::hal
        DrmDisplay["DrmDisplay"]:::hal
        DrmFBCommon["DrmFramebuffer\n(common)"]:::hal
        HotplugThread["DrmHotplugThread"]:::hal
        VsyncThread["DrmVsyncThread"]:::hal
        FBImporterInterface["Framebuffer Importer\n(Interface)"]:::hal
        FBImporterLib["DrmFramebufferLibDrm"]:::hal
        FBImporterMini["DrmFramebufferMinigbm"]:::hal
    end

    %% Native Libraries Layer
    subgraph "Native Libraries"
        libdrm["libdrm"]:::native
        minigbm["minigbm"]:::native
    end

    %% Kernel DRM Subsystem
    subgraph "Kernel DRM Subsystem"
        Kernel["GPU DRM Driver\n& Legacy KMS"]:::kernel
    end

    %% Connections
    SF -->|"HIDL calls\nsetLayer, presentLayers"| Binder
    Binder -->|"Binder IPC"| ServiceCPP
    ServiceCPP -->|"register service\n& dispatch calls"| DrmComposer
    DrmComposer -->|"enqueue requests"| GraphicsThread
    GraphicsThread -->|"open DRM fd\n& enumerate"| DrmDevice
    DrmDevice -->|"create displays"| DrmDisplay
    DrmDisplay -->|"program CRTC & FB\n(drmModeSetCrtc)"| Kernel
    DrmComposer -->|"import buffers"| FBImporterInterface
    FBImporterInterface -->|"libdrm path"| FBImporterLib
    FBImporterInterface -->|"minigbm path"| FBImporterMini
    FBImporterLib --> libdrm
    FBImporterMini --> minigbm
    Kernel -->|"vblank ioctl"| VsyncThread
    VsyncThread -->|"vsync callback"| SF
    Kernel -->|"hotplug uevent"| HotplugThread
    HotplugThread -->|"hotplug callback"| SF

    %% Click Events
    click ServiceCPP "https://github.com/me176c-dev/drmfb-composer/blob/master/service.cpp"
    click DrmComposer "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmComposer.cpp"
    click DrmDevice "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmDevice.cpp"
    click DrmDisplay "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmDisplay.cpp"
    click FBImporterLib "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmFramebufferLibDrm.cpp"
    click FBImporterMini "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmFramebufferMinigbm.cpp"
    click GraphicsThread "https://github.com/me176c-dev/drmfb-composer/blob/master/GraphicsThread.cpp"
    click HotplugThread "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmHotplugThread.cpp"
    click VsyncThread "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmVsyncThread.cpp"
    click FBImporterInterface "https://github.com/me176c-dev/drmfb-composer/blob/master/DrmFramebufferImporter.h"

    %% Styles
    classDef framework fill:#D0E6FF,stroke:#000;
    classDef ipc fill:#D0FFD8,stroke:#000;
    classDef hal fill:#FFF3B0,stroke:#000;
    classDef native fill:#FFE0B0,stroke:#000;
    classDef kernel fill:#E0E0E0,stroke:#000;
```
