libui: use HIDLized gralloc-mapper

Test: builds and boots
Change-Id: If42bea782e6dda5cab31c270085d934879fcdb54
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp
index 3328a92..c207a79 100644
--- a/libs/ui/Android.bp
+++ b/libs/ui/Android.bp
@@ -59,12 +59,9 @@
         "UiConfig.cpp",
     ],
 
-    static_libs: [
-        "android.hardware.graphics.mapper@2.0",
-    ],
-
     shared_libs: [
         "android.hardware.graphics.allocator@2.0",
+        "android.hardware.graphics.mapper@2.0",
         "libbinder",
         "libcutils",
         "libhardware",
diff --git a/libs/ui/GrallocMapper.cpp b/libs/ui/GrallocMapper.cpp
index d568b68..b444871 100644
--- a/libs/ui/GrallocMapper.cpp
+++ b/libs/ui/GrallocMapper.cpp
@@ -26,85 +26,144 @@
 
 namespace Gralloc2 {
 
-typedef const void*(*FetchInterface)(const char* name);
-
-static FetchInterface loadHalLib(const char* pkg_name)
-{
-    static const std::array<const char*, 3> sSearchDirs = {{
-        HAL_LIBRARY_PATH_ODM,
-        HAL_LIBRARY_PATH_VENDOR,
-        HAL_LIBRARY_PATH_SYSTEM,
-    }};
-    static const char sSymbolName[] = "HALLIB_FETCH_Interface";
-
-    void* handle = nullptr;
-    std::string path;
-    for (auto dir : sSearchDirs) {
-        path = dir;
-        path += pkg_name;
-        path += ".hallib.so";
-        handle = dlopen(path.c_str(), RTLD_LOCAL | RTLD_NOW);
-        if (handle) {
-            break;
-        }
-    }
-    if (!handle) {
-        return nullptr;
-    }
-
-    void* symbol = dlsym(handle, sSymbolName);
-    if (!symbol) {
-        ALOGE("%s is missing from %s", sSymbolName, path.c_str());
-        dlclose(handle);
-        return nullptr;
-    }
-
-    return reinterpret_cast<FetchInterface>(symbol);
-}
+static constexpr Error kDefaultError = Error::NO_RESOURCES;
 
 Mapper::Mapper()
-    : mMapper(nullptr), mDevice(nullptr)
 {
-    static const char sHalLibName[] = "android.hardware.graphics.mapper";
-    static const char sSupportedInterface[] =
-        "android.hardware.graphics.mapper@2.0::IMapper";
-
-    FetchInterface fetchInterface = loadHalLib(sHalLibName);
-    if (!fetchInterface) {
-        return;
-    }
-
-    mMapper = static_cast<const IMapper*>(
-            fetchInterface(sSupportedInterface));
-    if (!mMapper) {
-        ALOGE("%s is not supported", sSupportedInterface);
-        return;
-    }
-
-    if (mMapper->createDevice(&mDevice) != Error::NONE) {
-        ALOGE("failed to create mapper device");
-        mMapper = nullptr;
+    mMapper = IMapper::getService("gralloc-mapper");
+    if (mMapper != nullptr && mMapper->isRemote()) {
+        LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
     }
 }
 
-Mapper::~Mapper()
+Error Mapper::retain(buffer_handle_t handle) const
 {
-    if (mMapper) {
-        mMapper->destroyDevice(mDevice);
-    }
+    auto ret = mMapper->retain(handle);
+    return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
 }
 
 void Mapper::release(buffer_handle_t handle) const
 {
-    auto error = mMapper->release(mDevice, handle);
+    auto ret = mMapper->release(handle);
+
+    auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
     ALOGE_IF(error != Error::NONE,
             "release(%p) failed with %d", handle, error);
 }
 
+Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const
+{
+    Error error = kDefaultError;
+    mMapper->getStride(handle,
+            [&](const auto& tmpError, const auto& tmpStride)
+            {
+                error = tmpError;
+                if (error != Error::NONE) {
+                    return;
+                }
+
+                *outStride = tmpStride;
+            });
+
+    return error;
+}
+
+Error Mapper::lock(buffer_handle_t handle,
+        uint64_t producerUsageMask,
+        uint64_t consumerUsageMask,
+        const IMapper::Rect& accessRegion,
+        int acquireFence, void** outData) const
+{
+    hardware::hidl_handle acquireFenceHandle;
+
+    NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
+    if (acquireFence >= 0) {
+        auto h = native_handle_init(acquireFenceStorage, 1, 0);
+        h->data[0] = acquireFence;
+        acquireFenceHandle = h;
+    }
+
+    Error error = kDefaultError;
+    mMapper->lock(handle, producerUsageMask, consumerUsageMask,
+            accessRegion, acquireFenceHandle,
+            [&](const auto& tmpError, const auto& tmpData)
+            {
+                error = tmpError;
+                if (error != Error::NONE) {
+                    return;
+                }
+
+                *outData = tmpData;
+            });
+
+    if (error == Error::NONE && acquireFence >= 0) {
+        close(acquireFence);
+    }
+
+    return error;
+}
+
+Error Mapper::lock(buffer_handle_t handle,
+        uint64_t producerUsageMask,
+        uint64_t consumerUsageMask,
+        const IMapper::Rect& accessRegion,
+        int acquireFence, FlexLayout* outLayout) const
+{
+    hardware::hidl_handle acquireFenceHandle;
+
+    NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
+    if (acquireFence >= 0) {
+        auto h = native_handle_init(acquireFenceStorage, 1, 0);
+        h->data[0] = acquireFence;
+        acquireFenceHandle = h;
+    }
+
+    Error error = kDefaultError;
+    mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask,
+            accessRegion, acquireFenceHandle,
+            [&](const auto& tmpError, const auto& tmpLayout)
+            {
+                error = tmpError;
+                if (error != Error::NONE) {
+                    return;
+                }
+
+                *outLayout = tmpLayout;
+            });
+
+    if (error == Error::NONE && acquireFence >= 0) {
+        close(acquireFence);
+    }
+
+    return error;
+}
+
 int Mapper::unlock(buffer_handle_t handle) const
 {
     int releaseFence;
-    auto error = mMapper->unlock(mDevice, handle, &releaseFence);
+
+    Error error = kDefaultError;
+    mMapper->unlock(handle,
+            [&](const auto& tmpError, const auto& tmpReleaseFence)
+            {
+                error = tmpError;
+                if (error != Error::NONE) {
+                    return;
+                }
+
+                auto fenceHandle = tmpReleaseFence.getNativeHandle();
+                if (fenceHandle && fenceHandle->numFds == 1) {
+                    int fd = dup(fenceHandle->data[0]);
+                    if (fd >= 0) {
+                        releaseFence = fd;
+                    } else {
+                        error = Error::NO_RESOURCES;
+                    }
+                } else {
+                    releaseFence = -1;
+                }
+            });
+
     if (error != Error::NONE) {
         ALOGE("unlock(%p) failed with %d", handle, error);
         releaseFence = -1;
diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp
index 13ab38c..1ff934b 100644
--- a/libs/ui/GraphicBufferMapper.cpp
+++ b/libs/ui/GraphicBufferMapper.cpp
@@ -148,8 +148,8 @@
     gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
     gralloc1_error_t error;
     if (mMapper->valid()) {
-        const Gralloc2::Device::Rect& accessRect =
-            *reinterpret_cast<Gralloc2::Device::Rect*>(&accessRegion);
+        const Gralloc2::IMapper::Rect& accessRect =
+            *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
         error = static_cast<gralloc1_error_t>(mMapper->lock(
                     handle, usage, usage, accessRect, fenceFd, vaddr));
     } else {
@@ -196,10 +196,32 @@
 
     gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
 
-    if (!mMapper->valid()) {
+    std::vector<android_flex_plane_t> planes;
+    android_flex_layout_t flexLayout{};
+    gralloc1_error_t error;
+
+    if (mMapper->valid()) {
+        const Gralloc2::IMapper::Rect& accessRect =
+            *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
+        Gralloc2::FlexLayout layout{};
+        error = static_cast<gralloc1_error_t>(mMapper->lock(
+                    handle, usage, usage, accessRect, fenceFd, &layout));
+
+        if (error == GRALLOC1_ERROR_NONE) {
+            planes.resize(layout.planes.size());
+            memcpy(planes.data(), layout.planes.data(),
+                    sizeof(planes[0]) * planes.size());
+
+            flexLayout.format = static_cast<android_flex_format_t>(
+                    layout.format);
+            flexLayout.num_planes = static_cast<uint32_t>(planes.size());
+            flexLayout.planes = planes.data();
+        }
+    } else {
+        sp<Fence> fence = new Fence(fenceFd);
+
         if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
-            sp<Fence> fence = new Fence(fenceFd);
-            gralloc1_error_t error = mDevice->lockYCbCr(handle,
+            error = mDevice->lockYCbCr(handle,
                     static_cast<gralloc1_producer_usage_t>(usage),
                     static_cast<gralloc1_consumer_usage_t>(usage),
                     &accessRegion, ycbcr, fence);
@@ -207,40 +229,23 @@
                     "lockYCbCr(%p, ...) failed: %d", handle, error);
             return error;
         }
-    }
 
-    uint32_t numPlanes = 0;
-    gralloc1_error_t error;
-    if (mMapper->valid()) {
-        error = static_cast<gralloc1_error_t>(
-                mMapper->getNumFlexPlanes(handle, &numPlanes));
-    } else {
+        uint32_t numPlanes = 0;
         error = mDevice->getNumFlexPlanes(handle, &numPlanes);
-    }
 
-    if (error != GRALLOC1_ERROR_NONE) {
-        ALOGV("Failed to retrieve number of flex planes: %d", error);
-        return error;
-    }
-    if (numPlanes < 3) {
-        ALOGV("Not enough planes for YCbCr (%u found)", numPlanes);
-        return GRALLOC1_ERROR_UNSUPPORTED;
-    }
+        if (error != GRALLOC1_ERROR_NONE) {
+            ALOGV("Failed to retrieve number of flex planes: %d", error);
+            return error;
+        }
+        if (numPlanes < 3) {
+            ALOGV("Not enough planes for YCbCr (%u found)", numPlanes);
+            return GRALLOC1_ERROR_UNSUPPORTED;
+        }
 
-    std::vector<android_flex_plane_t> planes(numPlanes);
-    android_flex_layout_t flexLayout{};
-    flexLayout.num_planes = numPlanes;
-    flexLayout.planes = planes.data();
+        planes.resize(numPlanes);
+        flexLayout.num_planes = numPlanes;
+        flexLayout.planes = planes.data();
 
-    if (mMapper->valid()) {
-        const Gralloc2::Device::Rect& accessRect =
-            *reinterpret_cast<Gralloc2::Device::Rect*>(&accessRegion);
-        Gralloc2::FlexLayout& layout =
-            *reinterpret_cast<Gralloc2::FlexLayout*>(&flexLayout);
-        error = static_cast<gralloc1_error_t>(mMapper->lock(
-                    handle, usage, usage, accessRect, fenceFd, &layout));
-    } else {
-        sp<Fence> fence = new Fence(fenceFd);
         error = mDevice->lockFlex(handle,
                 static_cast<gralloc1_producer_usage_t>(usage),
                 static_cast<gralloc1_consumer_usage_t>(usage),