目录
Differences between ION and DMA-BUF heaps
Why replace ION with DMA-BUF heaps
CSLAlloc ->...-> dma_heap_buffer_alloc
Gralloc -> ... -> dma_heap_buffer_alloc
DMA-BUF
如这张图所示,这是一条简单的 pipeline 图。
Sensor 出一帧,送给高通的 IFE,然后在送给IPE,IPE会分为三路:
第一路:TARGET_BUFFER 有可能被CPU访问,或者可能被GPU访问。如果这个buffer 是要放回给应用层做显示用的,那么这个buffer 可能会被display 访问。
第二路:送给ASD node 这路,取决于ASD算法是CPU实现的,还是GPU实现的。
第三路:如果FDManager 是高通硬件做的,那么可能会被高通的 cvp 访问,如果是软件算法,那么会被cpu访问。所以,IPE输出的一个 buffer 可能被 CPU,GPU,display,cvp 所共享。
DMA-BUF主要解决CPU和各种外设之间的buffer共享。其次, DMA-BUF 设计之初就是为满足那些大内存访问需求的硬件而设计的,如GPU/DPU/NPU。在这种场景下,如果使用CPU直接去访memory,那么性能会大大降低。
ION
• ION是基于DMA-BUF的框架实现的一种内存分配器
Call flow
DMA-BUF heaps
- DMA-BUF heaps也是一种基于DMA-BUF框架实现的内存分配器。
- DMA-BUF heaps已经upstream到kernel的mainline,而ION待在
android的staging中好多年,到现在都没有mainline。 - 最新的kernel中还upstream了system和cma两个dma-buf heap的
实现。system_uncached heap正在走upstream的流程
Differences between ION and DMA-BUF heaps
Why replace ION with DMA-BUF heaps
- ION太复杂,很难upstream,⽽DMA-BUF heaps结构清晰,实现简单,已经 mainline,由kernel upstream进⾏维护, ABI兼容性可以得到保障。
- DMA-BUF heaps中的每个heap都会对应⼀个设备节点,⽅便通过sepolicy进⾏权限控制。
- 由于ION⽀持客制化的heap id和heap flag,很难开发统⼀的测试框架。⽽DMA-BUF heap向上接⼝统⼀,不⽀持heap flag,⽅便对heap框架和mainline的heap开发统⼀的测试程序。
Reference
相关代码
DMA-BUF: common/driver/dma-buf/
ION: common/driver/android/staging/ion/
DMA-BUF:heaps: common/driver/dma-buf/dma-heap.c
common/driver/dma-buf/heaps/
DMA-BUF heaps To Replace ION
How to in Kernel Space
How to in User Space
Ueventd
sepolicy
Transition Example
如下举例说明如何将一个ion heap(Heap ID: MY_HEAP, Heap Flag: ION_FLAG_MY_FLAG)转成DMA-BUF heap。并且以ClientA为例说明,如何调用libdmabufheap的接口从这个heap中分配buffer。
Transition Example(一)
Transition Example(二)
Transition Example(三)
Transition Example(四)
Transition Example(五)
Transition Example(六)
DMA-BUF heaps in 8450
- Qcom heaps and sepolicy item
- Qcom gralloc implementation
- Qcom camera’s driver
Qcom heaps and sepolicy item
Qcom gralloc
已经完成从libion到libdmabufheap的迁移
path: hardware/qcom/display/gralloc
Qcom camera driver
已经完成从ion到dma-buf heaps的迁移
path: camera-kernel/drivers/cam_req_mgr/cam_mem_mgr.c
DMA-BUF heaps调用堆栈分析
1.在kernel源代码中 include/trace/events/dma_buf.h 加上如下patch:
2.在kernel源代码中 drivers/dma-buf/dma-heap.c 加上如下patch:
CSLAlloc ->...-> dma_heap_buffer_alloc
Gralloc -> ... -> dma_heap_buffer_alloc
小结
- 可以看到在安卓13之后,无论是 CSLAlloc接口申请的 imagebuffer,还是 Gralloc接口申请的 imagebuffer ,底层都是调用 dma_buf heaps,进而去调用 dma_buf 去分配内存的。