在高通的OpenCL SDK中,其Android.mk文件中,有判斷當前kernel的版本,若是大於4.12,那麼就使用libion.so,不然則使用ion kernle uapi:linux
# 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調用.android
在網上也找到關於如何使用ION的回答:https://grokbase.com/t/gg/android-kernel/141renrvzj/where-i-can-find-example-of-using-ion-for-memory-managementshell
You can use libion.
You can find it in system http://androidxref.com/4.4.2_r1/xref/system//
core http://androidxref.com/4.4.2_r1/xref/system/core//libionhttp://androidxref.com/4.4.2_r1/xref/system/core/libion/api
根據這個答案連接,編譯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.c和ion_test.c編一個出來一個executable。ion_test.c裏面有一個main()
函數,主要是用來test alloc,map和share功能,也能夠說是提供了使用Demo;而ion.c則是libion的實現,其實也是對ion toctl的幾個功能的封裝而已。其實咱們若是把這個ion.c和ion.h文件拿出來,那麼咱們也能編譯出libion庫了。頭文件ion.h以下:code
#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 */