Make NNAPI Memory type more structured

This CL makes NNAPI memory more structured by replacing the pervasive
use of NativeHandle with the new, more specific memory types "Ashmem"
and "MmapFd". It changes the Memory type to be a std::variant of Ashmem
and MmapFd, in addition to (a cleaned up) HardwareBuffer. Finally, the
Memory type also continues to hold the alternative type "NativeHandle"
(now as an "Unknown" memory type) to ensure all previous types of
memory could still be held.

This change also updates the utility and testing code to account for the
changes to the Memory type.

Bug: 183118727
Test: mma
Test: NeuralNetworksTest_static
Change-Id: I7e2fea015a18b5e00d56bef924ebca4449d5b6dd
Merged-In: I7e2fea015a18b5e00d56bef924ebca4449d5b6dd
(cherry picked from commit d6496953292ba60c357417e7c9673ba31d9f84f5)
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 107124a..704aa99 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -115,6 +115,7 @@
     // this would remove half of dependencies here.
     static_libs: [
         "android.hardware.common-V2-ndk_platform",
+        "android.hardware.graphics.common-V2-ndk_platform",
         "android.hardware.neuralnetworks-V1-ndk_platform",
         "[email protected]",
         "[email protected]",
@@ -122,6 +123,7 @@
         "[email protected]",
         "[email protected]",
         "[email protected]",
+        "libaidlcommonsupport",
         "libbase",
         "libcrypto_static",
         "libcutils",
diff --git a/runtime/ExecutionBuilder.cpp b/runtime/ExecutionBuilder.cpp
index 3985d12..53aa464 100644
--- a/runtime/ExecutionBuilder.cpp
+++ b/runtime/ExecutionBuilder.cpp
@@ -226,7 +226,7 @@
     // length. For other memories that do not allow this semantic, it is checked in
     // MemoryValidatorBase::validate before reaching here.
     if (validate(memory->getMemory()).ok() && offset == 0 && length == 0) {
-        length = memory->getMemory()->size;
+        length = memory->getSize();
     }
     // TODO validate the rest
     uint32_t poolIndex = mMemories.add(memory);
@@ -307,7 +307,7 @@
     // length. For other memories that do not allow this semantic, it is checked in
     // MemoryValidatorBase::validate before reaching here.
     if (validate(memory->getMemory()).ok() && offset == 0 && length == 0) {
-        length = memory->getMemory()->size;
+        length = memory->getSize();
     }
     // TODO validate the rest
     uint32_t poolIndex = mMemories.add(memory);
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index 7b46cf1..33f3ab7 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -182,7 +182,7 @@
 
 RuntimeMemory::RuntimeMemory(SharedMemory memory) : kMemory(std::move(memory)) {
     CHECK(kMemory != nullptr);
-    mValidator = std::make_unique<SizedMemoryValidator>(kMemory->size);
+    mValidator = std::make_unique<SizedMemoryValidator>(nn::getSize(kMemory));
 }
 
 RuntimeMemory::RuntimeMemory(SharedMemory memory, std::unique_ptr<MemoryValidatorBase> validator)
@@ -548,8 +548,8 @@
     }
 
     std::unique_ptr<MemoryValidatorBase> validator;
-    if (memory.value()->name == "hardware_buffer_blob") {
-        validator = std::make_unique<SizedMemoryValidator>(memory.value()->size);
+    if (isAhwbBlob(memory.value())) {
+        validator = std::make_unique<SizedMemoryValidator>(nn::getSize(memory.value()));
     } else {
         validator = std::make_unique<AHardwareBufferNonBlobValidator>();
     }
diff --git a/runtime/Memory.h b/runtime/Memory.h
index 3ce32f5..850f70a 100644
--- a/runtime/Memory.h
+++ b/runtime/Memory.h
@@ -175,7 +175,7 @@
     Request::MemoryPool getMemoryPool() const;
     const SharedMemory& getMemory() const { return kMemory; }
     const SharedBuffer& getIBuffer() const { return kBuffer; }
-    virtual uint32_t getSize() const { return getMemory()->size; }
+    virtual uint32_t getSize() const { return nn::getSize(getMemory()); }
     virtual std::optional<RunTimePoolInfo> getRunTimePoolInfo() const;
 
     MemoryValidatorBase& getValidator() const {
@@ -278,7 +278,7 @@
     uint8_t* getPointer() const;
 
     std::optional<RunTimePoolInfo> getRunTimePoolInfo() const override {
-        return RunTimePoolInfo::createFromExistingBuffer(getPointer(), kMemory->size);
+        return RunTimePoolInfo::createFromExistingBuffer(getPointer(), nn::getSize(kMemory));
     }
 
     // prefer using MemoryAshmem::create
@@ -332,7 +332,7 @@
     uint8_t* getPointer() const;
 
     std::optional<RunTimePoolInfo> getRunTimePoolInfo() const override {
-        return RunTimePoolInfo::createFromExistingBuffer(getPointer(), kMemory->size);
+        return RunTimePoolInfo::createFromExistingBuffer(getPointer(), nn::getSize(kMemory));
     }
 
     // prefer using MemoryRuntimeAHWB::create
diff --git a/runtime/test/Android.bp b/runtime/test/Android.bp
index f25485f..66196c3 100644
--- a/runtime/test/Android.bp
+++ b/runtime/test/Android.bp
@@ -49,11 +49,13 @@
     ],
     static_libs: [
         "android.hardware.common-V2-ndk_platform",
+        "android.hardware.graphics.common-V2-ndk_platform",
         "android.hardware.neuralnetworks-V1-ndk_platform",
         "[email protected]",
         "[email protected]",
         "[email protected]",
         "[email protected]",
+        "libaidlcommonsupport",
         "libc++fs",
         "libneuralnetworks_generated_test_harness",
         "libtextclassifier_hash_static",
diff --git a/runtime/test/TestMemoryDomain.cpp b/runtime/test/TestMemoryDomain.cpp
index 1d11b44..0f462bc 100644
--- a/runtime/test/TestMemoryDomain.cpp
+++ b/runtime/test/TestMemoryDomain.cpp
@@ -39,6 +39,7 @@
 using WrapperResult = test_wrapper::Result;
 using Type = test_wrapper::Type;
 using android::sp;
+using android::nn::isAhwbBlob;
 
 namespace {
 
@@ -281,6 +282,10 @@
     const AllocateReturn kAllocateReturn = std::get<2>(GetParam());
 };
 
+bool isAshmem(const SharedMemory& memory) {
+    return memory != nullptr && std::holds_alternative<Memory::Ashmem>(memory->handle);
+}
+
 // Test device memory allocation on a compilation with only a single partition.
 TEST_P(MemoryDomainTest, SinglePartition) {
     createAndRegisterDriver(
@@ -310,9 +315,9 @@
             const auto& memory = m->getMemory();
             EXPECT_TRUE(validate(memory).ok());
             if (kUseV1_2Driver) {
-                EXPECT_EQ(memory->name, "ashmem");
+                EXPECT_TRUE(isAshmem(memory));
             } else {
-                EXPECT_EQ(memory->name, "hardware_buffer_blob");
+                EXPECT_TRUE(isAhwbBlob(memory));
             }
         }
     }
@@ -348,9 +353,9 @@
                 const auto& memory = m->getMemory();
                 EXPECT_TRUE(validate(memory).ok());
                 if (kUseV1_2Driver) {
-                    EXPECT_EQ(memory->name, "ashmem");
+                    EXPECT_TRUE(isAshmem(memory));
                 } else {
-                    EXPECT_EQ(memory->name, "hardware_buffer_blob");
+                    EXPECT_TRUE(isAhwbBlob(memory));
                 }
             }
         }
@@ -372,9 +377,9 @@
             const auto& memory = m->getMemory();
             EXPECT_TRUE(validate(memory).ok());
             if (kUseV1_2Driver) {
-                EXPECT_EQ(memory->name, "ashmem");
+                EXPECT_TRUE(isAshmem(memory));
             } else {
-                EXPECT_EQ(memory->name, "hardware_buffer_blob");
+                EXPECT_TRUE(isAhwbBlob(memory));
             }
         }
     }
@@ -395,9 +400,9 @@
             const auto& memory = m->getMemory();
             EXPECT_TRUE(validate(memory).ok());
             if (kUseV1_2Driver) {
-                EXPECT_EQ(memory->name, "ashmem");
+                EXPECT_TRUE(isAshmem(memory));
             } else {
-                EXPECT_EQ(memory->name, "hardware_buffer_blob");
+                EXPECT_TRUE(isAhwbBlob(memory));
             }
         }
     }