关于Android中ION的libion 2019-09-26

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

# Tries to determine whether to use libion or ion kernel uapiKERNEL_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") ""}")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/adrenoelse $(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/adrenoendif


从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

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/

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

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := ion.cLOCAL_MODULE := libionLOCAL_MODULE_TAGS := optionalLOCAL_SHARED_LIBRARIES := libloginclude $(BUILD_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_SRC_FILES := ion.c ion_test.cLOCAL_MODULE := iontestLOCAL_MODULE_TAGS := optional testsLOCAL_SHARED_LIBRARIES := libloginclude $(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如下:

#ifndef __SYS_CORE_ION_H#define __SYS_CORE_ION_H#include <linux/ion.h>__BEGIN_DECLSint 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 */

Copyright © 2019 公海娱乐555000aa All Rights Reserved
王先海
地址:北辰区京津路与九洲交道口西南侧久久大厦4-1907
全国统一热线:15321733176