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

Commit 6fb63696 authored by Chia-I Wu's avatar Chia-I Wu Committed by Android (Google) Code Review
Browse files

Merge "libui: use HIDLized gralloc-mapper"

parents 064670ef 31669475
Loading
Loading
Loading
Loading
+7 −70
Original line number Diff line number Diff line
@@ -32,98 +32,35 @@ using hardware::graphics::allocator::V2_0::ConsumerUsage;
using hardware::graphics::common::V1_0::PixelFormat;
using hardware::graphics::mapper::V2_0::FlexLayout;
using hardware::graphics::mapper::V2_0::BackingStore;
using hardware::graphics::mapper::V2_0::Device;
using hardware::graphics::mapper::V2_0::IMapper;

// Mapper is a wrapper to IMapper, a client-side graphics buffer mapper.
class Mapper {
public:
    Mapper();
    ~Mapper();

    // this will be removed and Mapper will be always valid
    bool valid() const { return (mMapper != nullptr); }

    Error retain(buffer_handle_t handle) const
    {
        return mMapper->retain(mDevice, handle);
    }

    Error retain(buffer_handle_t handle) const;
    void release(buffer_handle_t handle) const;

    Error getDimensions(buffer_handle_t handle,
            uint32_t* outWidth, uint32_t* outHeight) const
    {
        return mMapper->getDimensions(mDevice, handle, outWidth, outHeight);
    }

    Error getFormat(buffer_handle_t handle,
            PixelFormat* outFormat) const
    {
        return mMapper->getFormat(mDevice, handle, outFormat);
    }

    Error getLayerCount(buffer_handle_t handle, uint32_t* outLayerCount) const
    {
        return mMapper->getLayerCount(mDevice, handle, outLayerCount);
    }

    Error getProducerUsageMask(buffer_handle_t handle,
            uint64_t* outUsageMask) const
    {
        return mMapper->getProducerUsageMask(mDevice, handle, outUsageMask);
    }

    Error getConsumerUsageMask(buffer_handle_t handle,
            uint64_t* outUsageMask) const
    {
        return mMapper->getConsumerUsageMask(mDevice, handle, outUsageMask);
    }

    Error getBackingStore(buffer_handle_t handle,
            BackingStore* outStore) const
    {
        return mMapper->getBackingStore(mDevice, handle, outStore);
    }

    Error getStride(buffer_handle_t handle, uint32_t* outStride) const
    {
        return mMapper->getStride(mDevice, handle, outStride);
    }

    Error getNumFlexPlanes(buffer_handle_t handle,
            uint32_t* outNumPlanes) const
    {
        return mMapper->getNumFlexPlanes(mDevice, handle, outNumPlanes);
    }
    Error getStride(buffer_handle_t handle, uint32_t* outStride) const;

    Error lock(buffer_handle_t handle,
            uint64_t producerUsageMask,
            uint64_t consumerUsageMask,
            const Device::Rect& accessRegion,
            int acquireFence, void** outData) const
    {
        return mMapper->lock(mDevice, handle,
                producerUsageMask, consumerUsageMask,
                &accessRegion, acquireFence, outData);
    }

            const IMapper::Rect& accessRegion,
            int acquireFence, void** outData) const;
    Error lock(buffer_handle_t handle,
            uint64_t producerUsageMask,
            uint64_t consumerUsageMask,
            const Device::Rect& accessRegion,
            int acquireFence, FlexLayout* outFlexLayout) const
    {
        return mMapper->lockFlex(mDevice, handle,
                producerUsageMask, consumerUsageMask,
                &accessRegion, acquireFence, outFlexLayout);
    }

            const IMapper::Rect& accessRegion,
            int acquireFence, FlexLayout* outLayout) const;
    int unlock(buffer_handle_t handle) const;

private:
    const IMapper* mMapper;
    Device* mDevice;
    sp<IMapper> mMapper;
};

} // namespace Gralloc2
+1 −4
Original line number Diff line number Diff line
@@ -59,12 +59,9 @@ cc_library_shared {
        "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",
+115 −56
Original line number Diff line number Diff line
@@ -26,85 +26,144 @@ namespace android {

namespace Gralloc2 {

typedef const void*(*FetchInterface)(const char* name);
static constexpr Error kDefaultError = Error::NO_RESOURCES;

static FetchInterface loadHalLib(const char* pkg_name)
Mapper::Mapper()
{
    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;
    mMapper = IMapper::getService("gralloc-mapper");
    if (mMapper != nullptr && mMapper->isRemote()) {
        LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
    }
}

Error Mapper::retain(buffer_handle_t handle) const
{
    auto ret = mMapper->retain(handle);
    return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
}
    if (!handle) {
        return nullptr;

void Mapper::release(buffer_handle_t handle) const
{
    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);
}

    void* symbol = dlsym(handle, sSymbolName);
    if (!symbol) {
        ALOGE("%s is missing from %s", sSymbolName, path.c_str());
        dlclose(handle);
        return nullptr;
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;
                }

    return reinterpret_cast<FetchInterface>(symbol);
                *outStride = tmpStride;
            });

    return error;
}

Mapper::Mapper()
    : mMapper(nullptr), mDevice(nullptr)
Error Mapper::lock(buffer_handle_t handle,
        uint64_t producerUsageMask,
        uint64_t consumerUsageMask,
        const IMapper::Rect& accessRegion,
        int acquireFence, void** outData) const
{
    static const char sHalLibName[] = "android.hardware.graphics.mapper";
    static const char sSupportedInterface[] =
        "android.hardware.graphics.mapper@2.0::IMapper";
    hardware::hidl_handle acquireFenceHandle;

    FetchInterface fetchInterface = loadHalLib(sHalLibName);
    if (!fetchInterface) {
        return;
    NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
    if (acquireFence >= 0) {
        auto h = native_handle_init(acquireFenceStorage, 1, 0);
        h->data[0] = acquireFence;
        acquireFenceHandle = h;
    }

    mMapper = static_cast<const IMapper*>(
            fetchInterface(sSupportedInterface));
    if (!mMapper) {
        ALOGE("%s is not supported", sSupportedInterface);
    Error error = kDefaultError;
    mMapper->lock(handle, producerUsageMask, consumerUsageMask,
            accessRegion, acquireFenceHandle,
            [&](const auto& tmpError, const auto& tmpData)
            {
                error = tmpError;
                if (error != Error::NONE) {
                    return;
                }

    if (mMapper->createDevice(&mDevice) != Error::NONE) {
        ALOGE("failed to create mapper device");
        mMapper = nullptr;
                *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;
    }

Mapper::~Mapper()
    Error error = kDefaultError;
    mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask,
            accessRegion, acquireFenceHandle,
            [&](const auto& tmpError, const auto& tmpLayout)
            {
    if (mMapper) {
        mMapper->destroyDevice(mDevice);
                error = tmpError;
                if (error != Error::NONE) {
                    return;
                }

                *outLayout = tmpLayout;
            });

    if (error == Error::NONE && acquireFence >= 0) {
        close(acquireFence);
    }

void Mapper::release(buffer_handle_t handle) const
{
    auto error = mMapper->release(mDevice, handle);
    ALOGE_IF(error != Error::NONE,
            "release(%p) failed with %d", handle, error);
    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;
+39 −34
Original line number Diff line number Diff line
@@ -148,8 +148,8 @@ status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
    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 @@ status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,

    gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);

    if (!mMapper->valid()) {
        if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
    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);
            gralloc1_error_t error = mDevice->lockYCbCr(handle,

        if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
            error = mDevice->lockYCbCr(handle,
                    static_cast<gralloc1_producer_usage_t>(usage),
                    static_cast<gralloc1_consumer_usage_t>(usage),
                    &accessRegion, ycbcr, fence);
@@ -207,16 +229,9 @@ status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
                    "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 {
        error = mDevice->getNumFlexPlanes(handle, &numPlanes);
    }

        if (error != GRALLOC1_ERROR_NONE) {
            ALOGV("Failed to retrieve number of flex planes: %d", error);
@@ -227,20 +242,10 @@ status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
            return GRALLOC1_ERROR_UNSUPPORTED;
        }

    std::vector<android_flex_plane_t> planes(numPlanes);
    android_flex_layout_t flexLayout{};
        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),