Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 900c28a2 authored by Lev Proleev's avatar Lev Proleev Committed by Xusong Wang
Browse files

Add canonical types adapters for NNAPI AIDL interface

Also:
* Add missing AIDL<->CT conversions
* Add AIDL-specific info to neuralnetworks/utils/README.md
* Add mock classes and tests AIDL adapters

Bug: 179015258
Test: neuralnetworks_utils_hal_test
Change-Id: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4
Merged-In: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4
(cherry picked from commit 3b93b0b5)
parent 7b0b5469
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
    {
      "name": "neuralnetworks_utils_hal_1_3_test"
    },
    {
      "name": "neuralnetworks_utils_hal_aidl_test"
    },
    {
      "name": "VtsHalNeuralnetworksV1_0TargetTest",
      "options": [
+37 −0
Original line number Diff line number Diff line
@@ -29,10 +29,12 @@ cc_library_static {
    srcs: ["src/*"],
    local_include_dirs: ["include/nnapi/hal/aidl/"],
    export_include_dirs: ["include"],
    cflags: ["-Wthread-safety"],
    static_libs: [
        "libarect",
        "neuralnetworks_types",
        "neuralnetworks_utils_hal_common",
        "neuralnetworks_utils_hal_1_0",
    ],
    shared_libs: [
        "android.hardware.neuralnetworks-V1-ndk_platform",
@@ -41,3 +43,38 @@ cc_library_static {
        "libnativewindow",
    ],
}

cc_test {
    name: "neuralnetworks_utils_hal_aidl_test",
    defaults: ["neuralnetworks_utils_defaults"],
    srcs: [
        "test/*.cpp",
    ],
    static_libs: [
        "android.hardware.common-V2-ndk_platform",
        "android.hardware.neuralnetworks-V1-ndk_platform",
        "libgmock",
        "libneuralnetworks_common",
        "neuralnetworks_types",
        "neuralnetworks_utils_hal_aidl",
        "neuralnetworks_utils_hal_common",
    ],
    shared_libs: [
        "android.hidl.allocator@1.0",
        "libbase",
        "libbinder_ndk",
        "libcutils",
        "libhidlbase",
        "libhidlmemory",
        "liblog",
        "libnativewindow",
        "libutils",
    ],
    cflags: [
        /* GMOCK defines functions for printing all MOCK_DEVICE arguments and
         * MockDevice contains a string pointer which triggers a warning in the
         * base logging library. */
        "-Wno-user-defined-warnings",
    ],
    test_suites: ["general-tests"],
}
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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.
 */

#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H

#include <aidl/android/hardware/neuralnetworks/IBuffer.h>
#include <nnapi/IBuffer.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <memory>

// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes.

namespace aidl::android::hardware::neuralnetworks::utils {

// Class that adapts aidl_hal::IBuffer to  nn::IBuffer.
class Buffer final : public nn::IBuffer {
    struct PrivateConstructorTag {};

  public:
    static nn::GeneralResult<std::shared_ptr<const Buffer>> create(
            std::shared_ptr<aidl_hal::IBuffer> buffer, nn::Request::MemoryDomainToken token);

    Buffer(PrivateConstructorTag tag, std::shared_ptr<aidl_hal::IBuffer> buffer,
           nn::Request::MemoryDomainToken token);

    nn::Request::MemoryDomainToken getToken() const override;

    nn::GeneralResult<void> copyTo(const nn::SharedMemory& dst) const override;
    nn::GeneralResult<void> copyFrom(const nn::SharedMemory& src,
                                     const nn::Dimensions& dimensions) const override;

  private:
    const std::shared_ptr<aidl_hal::IBuffer> kBuffer;
    const nn::Request::MemoryDomainToken kToken;
};

}  // namespace aidl::android::hardware::neuralnetworks::utils

#endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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.
 */

#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H

#include <aidl/android/hardware/neuralnetworks/BnPreparedModelCallback.h>
#include <aidl/android/hardware/neuralnetworks/IDevice.h>
#include <nnapi/IPreparedModel.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
#include <nnapi/hal/TransferValue.h>
#include <nnapi/hal/aidl/ProtectCallback.h>

// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
// lifetimes across processes and for protecting asynchronous calls across AIDL.

namespace aidl::android::hardware::neuralnetworks::utils {

// An AIDL callback class to receive the results of IDevice::prepareModel* asynchronously.
class PreparedModelCallback final : public BnPreparedModelCallback,
                                    public hal::utils::IProtectedCallback {
  public:
    using Data = nn::GeneralResult<nn::SharedPreparedModel>;

    ndk::ScopedAStatus notify(ErrorStatus status,
                              const std::shared_ptr<IPreparedModel>& preparedModel) override;

    void notifyAsDeadObject() override;

    Data get();

  private:
    hal::utils::TransferValue<Data> mData;
};

}  // namespace aidl::android::hardware::neuralnetworks::utils

#endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H
+59 −1
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@
#include <aidl/android/hardware/neuralnetworks/SymmPerChannelQuantParams.h>
#include <aidl/android/hardware/neuralnetworks/Timing.h>

#include <android/binder_auto_utils.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
@@ -96,7 +97,11 @@ GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
        const aidl_hal::ExtensionOperandTypeInformation& operandTypeInformation);
GeneralResult<SharedHandle> unvalidatedConvert(
        const ::aidl::android::hardware::common::NativeHandle& handle);
GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence);

GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities);
GeneralResult<DeviceType> convert(const aidl_hal::DeviceType& deviceType);
GeneralResult<ErrorStatus> convert(const aidl_hal::ErrorStatus& errorStatus);
GeneralResult<ExecutionPreference> convert(
        const aidl_hal::ExecutionPreference& executionPreference);
GeneralResult<SharedMemory> convert(const aidl_hal::Memory& memory);
@@ -106,9 +111,14 @@ GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType);
GeneralResult<Priority> convert(const aidl_hal::Priority& priority);
GeneralResult<Request::MemoryPool> convert(const aidl_hal::RequestMemoryPool& memoryPool);
GeneralResult<Request> convert(const aidl_hal::Request& request);
GeneralResult<Timing> convert(const aidl_hal::Timing& timing);
GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence);

GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension);
GeneralResult<std::vector<Operation>> convert(const std::vector<aidl_hal::Operation>& outputShapes);
GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories);
GeneralResult<std::vector<OutputShape>> convert(
        const std::vector<aidl_hal::OutputShape>& outputShapes);

GeneralResult<std::vector<uint32_t>> toUnsigned(const std::vector<int32_t>& vec);

@@ -118,14 +128,62 @@ namespace aidl::android::hardware::neuralnetworks::utils {

namespace nn = ::android::nn;

nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(const nn::CacheToken& cacheToken);
nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc);
nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole);
nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTiming);
nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory);
nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputShape);
nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus);

nn::GeneralResult<ExecutionPreference> unvalidatedConvert(
        const nn::ExecutionPreference& executionPreference);
nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType);
nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& operandLifeTime);
nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location);
nn::GeneralResult<std::optional<OperandExtraParams>> unvalidatedConvert(
        const nn::Operand::ExtraParams& extraParams);
nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand);
nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType);
nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation);
nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph);
nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(
        const nn::Model::OperandValues& operandValues);
nn::GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert(
        const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix);
nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model);
nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority);
nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request);
nn::GeneralResult<RequestArgument> unvalidatedConvert(const nn::Request::Argument& requestArgument);
nn::GeneralResult<RequestMemoryPool> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool);
nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::Duration& duration);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalDuration& optionalDuration);
nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint);
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFence& syncFence);
nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle);
nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvertCache(
        const nn::SharedHandle& handle);

nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken);
nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc);
nn::GeneralResult<bool> convert(const nn::MeasureTiming& measureTiming);
nn::GeneralResult<Memory> convert(const nn::SharedMemory& memory);
nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus);
nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference);
nn::GeneralResult<Model> convert(const nn::Model& model);
nn::GeneralResult<Priority> convert(const nn::Priority& priority);
nn::GeneralResult<Request> convert(const nn::Request& request);
nn::GeneralResult<Timing> convert(const nn::Timing& timing);
nn::GeneralResult<int64_t> convert(const nn::OptionalDuration& optionalDuration);
nn::GeneralResult<int64_t> convert(const nn::OptionalTimePoint& optionalTimePoint);

nn::GeneralResult<std::vector<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles);
nn::GeneralResult<std::vector<OutputShape>> convert(
        const std::vector<nn::OutputShape>& outputShapes);
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
        const std::vector<nn::SharedHandle>& handles);
nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
        const std::vector<nn::SyncFence>& syncFences);

nn::GeneralResult<std::vector<int32_t>> toSigned(const std::vector<uint32_t>& vec);

Loading