About ION's Library in Android

Keywords: Android SDK shell Linux

In the Android.mk file of Qualcomm's OpenCL SDK, you can judge the current kernel version. If it is greater than 4.12, then use library.so. Otherwise, use ion kernel 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

Starting with Android P, Kernel 4.14 has been pushed to AOSP, and libion supports the new kernel ion interface on Android P. it is strongly recommended to use libion instead of using the ion ioctl call directly.

Answers on how to use ION are also found online: 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/

According to the answer link, compile the mk file As follows:

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)

This mk file is used, ion.c Compile the library shared library and use it, ion.c and ion_test.c Make up an executable. There is a main() function in ion Φ test. C, which is mainly used to test alloc, map and share functions, or to provide the use of Demo; while ion.c is the implementation of libion, which is also the encapsulation of several functions of ion toctl. In fact, if we take out the ion.c and ion.h files, then we can also compile the library. Header file ion.h As follows:

#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 */

Posted by whare on Mon, 02 Dec 2019 10:21:42 -0800