From dd53d04d8c19515223f86129ca043614e196840e Mon Sep 17 00:00:00 2001 From: Michael Butler Date: Wed, 24 Mar 2021 21:26:02 -0700 Subject: [PATCH] Improve the structure of NNAPI AIDL Memory Prior to this change, the NN AIDL HAL created a Memory struct analogous to hidl_memory, consisting of a name, size, and native handle. However, this Memory struct is not very structured, and requires both the client and server to agree on how the data should be interpreted. This CL tightens the structure of the Memory representation by introducing Ashmem and MappableFile structs to android.hardware.common in order to hold specific file descriptors representing memory regions. Further, this CL redefines android.hardwre.neuralnetworks's Memory object as a union of the Ashmem, MappableFile, and (existing) HardwareBuffer memory types. This change also adds "com.android.neuralnetworks" to the graphics AIDL HAL's apex_available build rule. Bug: 183118727 Test: mma Change-Id: I32322df676b83597c9e95f13662c322a6a80accc Merged-In: I32322df676b83597c9e95f13662c322a6a80accc (cherry picked from commit 1158c80ff6f24223b8add271945b66f34db78d60) --- .../android/hardware/common/Ashmem.aidl | 39 ++++++++++++++ .../android/hardware/common/MappableFile.aidl | 41 ++++++++++++++ .../android/hardware/common/NativeHandle.aidl | 28 +++++++--- .../aidl/android/hardware/common/Ashmem.aidl | 34 ++++++++++++ .../android/hardware/common/MappableFile.aidl | 53 +++++++++++++++++++ graphics/common/aidl/Android.bp | 1 + neuralnetworks/aidl/Android.bp | 1 + .../hardware/neuralnetworks/Memory.aidl | 8 +-- .../hardware/neuralnetworks/Memory.aidl | 26 ++++++--- 9 files changed, 213 insertions(+), 18 deletions(-) create mode 100644 common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/Ashmem.aidl create mode 100644 common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/MappableFile.aidl create mode 100644 common/aidl/android/hardware/common/Ashmem.aidl create mode 100644 common/aidl/android/hardware/common/MappableFile.aidl diff --git a/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/Ashmem.aidl b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/Ashmem.aidl new file mode 100644 index 0000000000..a4380315c9 --- /dev/null +++ b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/Ashmem.aidl @@ -0,0 +1,39 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/////////////////////////////////////////////////////////////////////////////// +// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. // +/////////////////////////////////////////////////////////////////////////////// + +// This file is a snapshot of an AIDL file. Do not edit it manually. There are +// two cases: +// 1). this is a frozen version file - do not edit this in any case. +// 2). this is a 'current' file. If you make a backwards compatible change to +// the interface (from the latest frozen version), the build system will +// prompt you to update this file with `m -update-api`. +// +// You must not make a backward incompatible change to any AIDL file built +// with the aidl_interface module type with versions property set. The module +// type is used to build AIDL files in a way that they can be used across +// independently updatable components of the system. If a device is shipped +// with such a backward incompatible change, it has a high risk of breaking +// later when a module using the interface is updated, e.g., Mainline modules. + +package android.hardware.common; +@VintfStability +parcelable Ashmem { + ParcelFileDescriptor fd; + long size; +} diff --git a/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/MappableFile.aidl b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/MappableFile.aidl new file mode 100644 index 0000000000..394ea8ff07 --- /dev/null +++ b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/MappableFile.aidl @@ -0,0 +1,41 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/////////////////////////////////////////////////////////////////////////////// +// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. // +/////////////////////////////////////////////////////////////////////////////// + +// This file is a snapshot of an AIDL file. Do not edit it manually. There are +// two cases: +// 1). this is a frozen version file - do not edit this in any case. +// 2). this is a 'current' file. If you make a backwards compatible change to +// the interface (from the latest frozen version), the build system will +// prompt you to update this file with `m -update-api`. +// +// You must not make a backward incompatible change to any AIDL file built +// with the aidl_interface module type with versions property set. The module +// type is used to build AIDL files in a way that they can be used across +// independently updatable components of the system. If a device is shipped +// with such a backward incompatible change, it has a high risk of breaking +// later when a module using the interface is updated, e.g., Mainline modules. + +package android.hardware.common; +@VintfStability +parcelable MappableFile { + long length; + int prot; + ParcelFileDescriptor fd; + long offset; +} diff --git a/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/NativeHandle.aidl b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/NativeHandle.aidl index f37b7d506f..2ed5c0b22d 100644 --- a/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/NativeHandle.aidl +++ b/common/aidl/aidl_api/android.hardware.common/current/android/hardware/common/NativeHandle.aidl @@ -1,14 +1,30 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /////////////////////////////////////////////////////////////////////////////// // THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. // /////////////////////////////////////////////////////////////////////////////// -// This file is a snapshot of an AIDL interface (or parcelable). Do not try to -// edit this file. It looks like you are doing that because you have modified -// an AIDL interface in a backward-incompatible way, e.g., deleting a function -// from an interface or a field from a parcelable and it broke the build. That -// breakage is intended. +// This file is a snapshot of an AIDL file. Do not edit it manually. There are +// two cases: +// 1). this is a frozen version file - do not edit this in any case. +// 2). this is a 'current' file. If you make a backwards compatible change to +// the interface (from the latest frozen version), the build system will +// prompt you to update this file with `m -update-api`. // -// You must not make a backward incompatible changes to the AIDL files built +// You must not make a backward incompatible change to any AIDL file built // with the aidl_interface module type with versions property set. The module // type is used to build AIDL files in a way that they can be used across // independently updatable components of the system. If a device is shipped diff --git a/common/aidl/android/hardware/common/Ashmem.aidl b/common/aidl/android/hardware/common/Ashmem.aidl new file mode 100644 index 0000000000..8e402668bf --- /dev/null +++ b/common/aidl/android/hardware/common/Ashmem.aidl @@ -0,0 +1,34 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.common; + +import android.os.ParcelFileDescriptor; + +/** + * Type that holds same memory as the "ashmem" hidl_memory type from HIDL. + */ +@VintfStability +parcelable Ashmem { + /** + * A handle to a memory region. + */ + ParcelFileDescriptor fd; + /** + * Size of the memory region in bytes. + */ + long size; +} diff --git a/common/aidl/android/hardware/common/MappableFile.aidl b/common/aidl/android/hardware/common/MappableFile.aidl new file mode 100644 index 0000000000..a7763eab37 --- /dev/null +++ b/common/aidl/android/hardware/common/MappableFile.aidl @@ -0,0 +1,53 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.common; + +import android.os.ParcelFileDescriptor; + +/** + * A region of a file that can be mapped into memory. + * + * In Linux, MappableFile may be used with mmap as `MAP_SHARED`. + * + * MappableFile is compatible with ::android::base::MappedFile. + */ +@VintfStability +parcelable MappableFile { + /** + * Length of the mapping region in bytes. + */ + long length; + /** + * The desired memory protection for the mapping. + * + * In Linux, prot is either `PROT_NONE` (indicating that mapped pages may not be accessed) or + * the bitwise OR of one or more of the following flags: + * - `PROT_READ` (indicating that the mapped pages may be read) + * - `PROT_WRITE` (indicating that the mapped pages may be written) + */ + int prot; + /** + * A handle to a mappable file. + */ + ParcelFileDescriptor fd; + /** + * The offset in the file to the beginning of the mapping region in number of bytes. + * + * Note: Some mapping functions require that the offset is aligned to the page size. + */ + long offset; +} diff --git a/graphics/common/aidl/Android.bp b/graphics/common/aidl/Android.bp index 2a46f9dc7f..cadd13cdde 100644 --- a/graphics/common/aidl/Android.bp +++ b/graphics/common/aidl/Android.bp @@ -34,6 +34,7 @@ aidl_interface { apex_available: [ "//apex_available:platform", "com.android.media.swcodec", + "com.android.neuralnetworks", ], min_sdk_version: "29", }, diff --git a/neuralnetworks/aidl/Android.bp b/neuralnetworks/aidl/Android.bp index b1860e2bd0..ebf4654885 100644 --- a/neuralnetworks/aidl/Android.bp +++ b/neuralnetworks/aidl/Android.bp @@ -16,6 +16,7 @@ aidl_interface { stability: "vintf", imports: [ "android.hardware.common", + "android.hardware.graphics.common", ], backend: { java: { diff --git a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/Memory.aidl b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/Memory.aidl index 8207b25570..37fa102cf4 100644 --- a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/Memory.aidl +++ b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/Memory.aidl @@ -33,8 +33,8 @@ package android.hardware.neuralnetworks; @VintfStability -parcelable Memory { - android.hardware.common.NativeHandle handle; - long size; - String name; +union Memory { + android.hardware.common.Ashmem ashmem; + android.hardware.common.MappableFile mappableFile; + android.hardware.graphics.common.HardwareBuffer hardwareBuffer; } diff --git a/neuralnetworks/aidl/android/hardware/neuralnetworks/Memory.aidl b/neuralnetworks/aidl/android/hardware/neuralnetworks/Memory.aidl index 870f0aeb08..244ac8752d 100644 --- a/neuralnetworks/aidl/android/hardware/neuralnetworks/Memory.aidl +++ b/neuralnetworks/aidl/android/hardware/neuralnetworks/Memory.aidl @@ -15,16 +15,26 @@ */ package android.hardware.neuralnetworks; -import android.hardware.common.NativeHandle; -import android.os.ParcelFileDescriptor; + +import android.hardware.common.Ashmem; +import android.hardware.common.MappableFile; +import android.hardware.graphics.common.HardwareBuffer; /** - * A type that is used to pass pieces of shared memory between processes. - * The type structure mimics hidl_memory type from HIDL. + * The different types of memory that can be shared across processes. */ @VintfStability -parcelable Memory { - NativeHandle handle; - long size; - String name; +union Memory { + /** + * Ashmem hidl_memory type from HIDL. + */ + Ashmem ashmem; + /** + * File that can be mapped. + */ + MappableFile mappableFile; + /** + * AIDL representation of AHardwareBuffer. + */ + HardwareBuffer hardwareBuffer; }