在高通的OpenCL SDK中,其Android.mk文件中,有判断当前kernel的版本,如果大于4.12,那么就使用libion.so,否则则使用ion kernle uapi

# Tries to determine whether to use libion or ion kernel uapi
KERNEL_VERSION = $(shell ls kernel | sed -n 's/msm-\([0-9]\+\)\.\([0-9]\+\)/-v x0=\1 -v x1=\2/p')
USE_LIBION = $(shell awk $(KERNEL_VERSION) -v y0="4" -v y1="12" 'BEGIN {printf (x0>=y0 && x1>=y1?"true":"false") "\n"}')
ifeq ($(USE_LIBION), true)
$(info OpenCL SDK: Using libion) OPENCL_SDK_CPPFLAGS := -Wno-missing-braces -DUSES_LIBION OPENCL_SDK_SHARED_LIBS := libion libOpenCL OPENCL_SDK_COMMON_INCLUDES := \
$(LOCAL_PATH)/src \
kernel/msm-4.14/ \
$(TARGET_OUT_INTERMEDIATES)/include/adreno else
$(info OpenCL SDK: Using ion uapi) OPENCL_SDK_CPPFLAGS := -Wno-missing-braces OPENCL_SDK_SHARED_LIBS := libOpenCL OPENCL_SDK_COMMON_INCLUDES := \
$(LOCAL_PATH)/src \
$(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
$(TARGET_OUT_INTERMEDIATES)/include/adreno endif

从Andriod P开始,Kernel 4.14已推到AOSP, libion在Android P上已支持新的kernel ion接口,强烈建议 使用libion,而非直接使用ion ioctl调用.


在网上也找到关于如何使用ION的回答:https://grokbase.com/t/gg/android-kernel/141renrvzj/where-i-can-find-example-of-using-ion-for-memory-management

根据这个答案链接,编译libion的mk文件如下:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES := ion.c
LOCAL_MODULE := libion
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS)
LOCAL_SRC_FILES := ion.c ion_test.c
LOCAL_MODULE := iontest
LOCAL_MODULE_TAGS := optional tests
LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_EXECUTABLE)

这个mk文件使用,ion.c编译出来libion shared library ,以及使用,ion.cion_test.c编一个出来一个executable。ion_test.c里面有一个main()函数,主要是用来test alloc,map和share功能,也可以说是提供了使用Demo;而ion.c则是libion的实现,其实也是对ion toctl的几个功能的封装而已。其实我们如果把这个ion.c和ion.h文件拿出来,那么我们也能编译出libion库了。头文件ion.h如下:

#ifndef __SYS_CORE_ION_H
#define __SYS_CORE_ION_H #include <linux/ion.h> __BEGIN_DECLS int ion_open();
int ion_close(int fd);
int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask,
unsigned int flags, struct ion_handle **handle);
int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask,
unsigned int flags, int *handle_fd);
int ion_sync_fd(int fd, int handle_fd);
int ion_free(int fd, struct ion_handle *handle);
int ion_map(int fd, struct ion_handle *handle, size_t length, int prot,
int flags, off_t offset, unsigned char **ptr, int *map_fd);
int ion_share(int fd, struct ion_handle *handle, int *share_fd);
int ion_import(int fd, int share_fd, struct ion_handle **handle); __END_DECLS #endif /* __SYS_CORE_ION_H */
05-11 17:04