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

Commit 8deb4da6 authored by Dan Stoza's avatar Dan Stoza
Browse files

libui: Convert Allocator and Mapper to gralloc1

Converts GraphicBufferAllocator and GraphicBufferMapper to speak
gralloc 1.0 (via the C++ shim and optionally the 1On0 adapter) instead
of gralloc 0.x.

Bug: 28401203
Change-Id: Ie1649f0ee72801579be2c2d3c47177b52962a825
parent 41b1261e
Loading
Loading
Loading
Loading
+19 −19
Original line number Original line Diff line number Diff line
@@ -27,42 +27,41 @@
#include <utils/threads.h>
#include <utils/threads.h>
#include <utils/Singleton.h>
#include <utils/Singleton.h>


#include <ui/Gralloc1.h>
#include <ui/PixelFormat.h>
#include <ui/PixelFormat.h>


#include <hardware/gralloc.h>


namespace android {
namespace android {
// ---------------------------------------------------------------------------


class Gralloc1Loader;
class String8;
class String8;


class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
{
{
public:
public:
    enum {
    enum {
        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
        USAGE_SW_READ_NEVER     = GRALLOC1_CONSUMER_USAGE_CPU_READ_NEVER,
        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
        USAGE_SW_READ_RARELY    = GRALLOC1_CONSUMER_USAGE_CPU_READ,
        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
        USAGE_SW_READ_OFTEN     = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
        USAGE_SW_READ_MASK      = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,


        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
        USAGE_SW_WRITE_NEVER    = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_NEVER,
        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
        USAGE_SW_WRITE_RARELY   = GRALLOC1_PRODUCER_USAGE_CPU_WRITE,
        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
        USAGE_SW_WRITE_OFTEN    = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
        USAGE_SW_WRITE_MASK     = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,


        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,


        USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
        USAGE_HW_TEXTURE        = GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE,
        USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
        USAGE_HW_RENDER         = GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET,
        USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
        USAGE_HW_2D             = 0x00000400, // Deprecated
        USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK
        USAGE_HW_MASK           = 0x00071F00, // Deprecated
    };
    };


    static inline GraphicBufferAllocator& get() { return getInstance(); }
    static inline GraphicBufferAllocator& get() { return getInstance(); }


    status_t alloc(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
    status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
            buffer_handle_t* handle, uint32_t* stride);
            uint32_t usage, buffer_handle_t* handle, uint32_t* stride,
            uint64_t graphicBufferId);


    status_t free(buffer_handle_t handle);
    status_t free(buffer_handle_t handle);


@@ -86,7 +85,8 @@ private:
    GraphicBufferAllocator();
    GraphicBufferAllocator();
    ~GraphicBufferAllocator();
    ~GraphicBufferAllocator();


    alloc_device_t  *mAllocDev;
    std::unique_ptr<Gralloc1::Loader> mLoader;
    std::unique_ptr<Gralloc1::Device> mDevice;
};
};


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
+7 −9
Original line number Original line Diff line number Diff line
@@ -20,12 +20,9 @@
#include <stdint.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/types.h>


#include <utils/Singleton.h>
#include <ui/Gralloc1.h>

#include <hardware/gralloc.h>



#include <utils/Singleton.h>
struct gralloc_module_t;


namespace android {
namespace android {


@@ -39,6 +36,7 @@ public:
    static inline GraphicBufferMapper& get() { return getInstance(); }
    static inline GraphicBufferMapper& get() { return getInstance(); }


    status_t registerBuffer(buffer_handle_t handle);
    status_t registerBuffer(buffer_handle_t handle);
    status_t registerBuffer(const GraphicBuffer* buffer);


    status_t unregisterBuffer(buffer_handle_t handle);
    status_t unregisterBuffer(buffer_handle_t handle);


@@ -59,13 +57,13 @@ public:


    status_t unlockAsync(buffer_handle_t handle, int *fenceFd);
    status_t unlockAsync(buffer_handle_t handle, int *fenceFd);


    // dumps information about the mapping of this handle
    void dump(buffer_handle_t handle);

private:
private:
    friend class Singleton<GraphicBufferMapper>;
    friend class Singleton<GraphicBufferMapper>;

    GraphicBufferMapper();
    GraphicBufferMapper();
    gralloc_module_t const *mAllocMod;

    std::unique_ptr<Gralloc1::Loader> mLoader;
    std::unique_ptr<Gralloc1::Device> mDevice;
};
};


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
+3 −3
Original line number Original line Diff line number Diff line
@@ -169,8 +169,8 @@ status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
{
{
    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
    GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
    uint32_t outStride = 0;
    uint32_t outStride = 0;
    status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
    status_t err = allocator.allocate(inWidth, inHeight, inFormat, inUsage,
            &handle, &outStride);
            &handle, &outStride, mId);
    if (err == NO_ERROR) {
    if (err == NO_ERROR) {
        width = static_cast<int>(inWidth);
        width = static_cast<int>(inWidth);
        height = static_cast<int>(inHeight);
        height = static_cast<int>(inHeight);
@@ -390,7 +390,7 @@ status_t GraphicBuffer::unflatten(
    mOwner = ownHandle;
    mOwner = ownHandle;


    if (handle != 0) {
    if (handle != 0) {
        status_t err = mBufferMapper.registerBuffer(handle);
        status_t err = mBufferMapper.registerBuffer(this);
        if (err != NO_ERROR) {
        if (err != NO_ERROR) {
            width = height = stride = format = usage = 0;
            width = height = stride = format = usage = 0;
            handle = NULL;
            handle = NULL;
+52 −40
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@
#include <utils/Trace.h>
#include <utils/Trace.h>


#include <ui/GraphicBufferAllocator.h>
#include <ui/GraphicBufferAllocator.h>
#include <ui/Gralloc1On0Adapter.h>


namespace android {
namespace android {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
@@ -36,20 +37,10 @@ KeyedVector<buffer_handle_t,
    GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
    GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;


GraphicBufferAllocator::GraphicBufferAllocator()
GraphicBufferAllocator::GraphicBufferAllocator()
    : mAllocDev(0)
  : mLoader(std::make_unique<Gralloc1::Loader>()),
{
    mDevice(mLoader->getDevice()) {}
    hw_module_t const* module;
    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
    ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
    if (err == 0) {
        gralloc_open(module, &mAllocDev);
    }
}


GraphicBufferAllocator::~GraphicBufferAllocator()
GraphicBufferAllocator::~GraphicBufferAllocator() {}
{
    gralloc_close(mAllocDev);
}


void GraphicBufferAllocator::dump(String8& result) const
void GraphicBufferAllocator::dump(String8& result) const
{
{
@@ -77,10 +68,8 @@ void GraphicBufferAllocator::dump(String8& result) const
    }
    }
    snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
    snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
    result.append(buffer);
    result.append(buffer);
    if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
    std::string deviceDump = mDevice->dump();
        mAllocDev->dump(mAllocDev, buffer, SIZE);
    result.append(deviceDump.c_str(), deviceDump.size());
        result.append(buffer);
    }
}
}


void GraphicBufferAllocator::dumpToSystemLog()
void GraphicBufferAllocator::dumpToSystemLog()
@@ -90,9 +79,9 @@ void GraphicBufferAllocator::dumpToSystemLog()
    ALOGD("%s", s.string());
    ALOGD("%s", s.string());
}
}


status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
        PixelFormat format, uint32_t usage, buffer_handle_t* handle,
        PixelFormat format, uint32_t usage, buffer_handle_t* handle,
        uint32_t* stride)
        uint32_t* stride, uint64_t graphicBufferId)
{
{
    ATRACE_CALL();
    ATRACE_CALL();


@@ -101,22 +90,46 @@ status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
    if (!width || !height)
    if (!width || !height)
        width = height = 1;
        width = height = 1;


    // we have a h/w allocator and h/w buffer is requested
    status_t err;

    // Filter out any usage bits that should not be passed to the gralloc module
    // Filter out any usage bits that should not be passed to the gralloc module
    usage &= GRALLOC_USAGE_ALLOC_MASK;
    usage &= GRALLOC_USAGE_ALLOC_MASK;


    int outStride = 0;
    auto descriptor = mDevice->createDescriptor();
    err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
    auto error = descriptor->setDimensions(width, height);
            static_cast<int>(height), format, static_cast<int>(usage), handle,
    if (error != GRALLOC1_ERROR_NONE) {
            &outStride);
        ALOGE("Failed to set dimensions to (%u, %u): %d", width, height, error);
    *stride = static_cast<uint32_t>(outStride);
        return BAD_VALUE;
    }
    error = descriptor->setFormat(static_cast<android_pixel_format_t>(format));
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGE("Failed to set format to %d: %d", format, error);
        return BAD_VALUE;
    }
    error = descriptor->setProducerUsage(
            static_cast<gralloc1_producer_usage_t>(usage));
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGE("Failed to set producer usage to %u: %d", usage, error);
        return BAD_VALUE;
    }
    error = descriptor->setConsumerUsage(
            static_cast<gralloc1_consumer_usage_t>(usage));
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGE("Failed to set consumer usage to %u: %d", usage, error);
        return BAD_VALUE;
    }

    error = mDevice->allocate(descriptor, graphicBufferId, handle);
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGE("Failed to allocate (%u x %u) format %d usage %u: %d",
                width, height, format, usage, error);
        return NO_MEMORY;
    }


    ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
    error = mDevice->getStride(*handle, stride);
            width, height, format, usage, err, strerror(-err));
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGW("Failed to get stride from buffer: %d", error);
    }


    if (err == NO_ERROR) {
    if (error == NO_ERROR) {
        Mutex::Autolock _l(sLock);
        Mutex::Autolock _l(sLock);
        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
        uint32_t bpp = bytesPerPixel(format);
        uint32_t bpp = bytesPerPixel(format);
@@ -130,24 +143,23 @@ status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
        list.add(*handle, rec);
        list.add(*handle, rec);
    }
    }


    return err;
    return NO_ERROR;
}
}


status_t GraphicBufferAllocator::free(buffer_handle_t handle)
status_t GraphicBufferAllocator::free(buffer_handle_t handle)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    err = mAllocDev->free(mAllocDev, handle);
    auto error = mDevice->release(handle);
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGE("Failed to free buffer: %d", error);
    }


    ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
    if (err == NO_ERROR) {
    Mutex::Autolock _l(sLock);
    Mutex::Autolock _l(sLock);
    KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
    KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
    list.removeItem(handle);
    list.removeItem(handle);
    }


    return err;
    return NO_ERROR;
}
}


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
+193 −96
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


#define LOG_TAG "GraphicBufferMapper"
#define LOG_TAG "GraphicBufferMapper"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0


#include <stdint.h>
#include <stdint.h>
#include <errno.h>
#include <errno.h>
@@ -31,11 +32,11 @@
#include <utils/Log.h>
#include <utils/Log.h>
#include <utils/Trace.h>
#include <utils/Trace.h>


#include <ui/Gralloc1On0Adapter.h>
#include <ui/GraphicBufferMapper.h>
#include <ui/GraphicBufferMapper.h>
#include <ui/Rect.h>
#include <ui/Rect.h>


#include <hardware/gralloc.h>
#include <system/graphics.h>



namespace android {
namespace android {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
@@ -43,151 +44,247 @@ namespace android {
ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )


GraphicBufferMapper::GraphicBufferMapper()
GraphicBufferMapper::GraphicBufferMapper()
    : mAllocMod(0)
  : mLoader(std::make_unique<Gralloc1::Loader>()),
{
    mDevice(mLoader->getDevice()) {}
    hw_module_t const* module;

    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);

    ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
    if (err == 0) {
        mAllocMod = reinterpret_cast<gralloc_module_t const *>(module);
    }
}


status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    err = mAllocMod->registerBuffer(mAllocMod, handle);
    gralloc1_error_t error = mDevice->retain(handle);
    ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
            handle, error);


    ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)",
    return error;
            handle, err, strerror(-err));
    return err;
}
}


status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
status_t GraphicBufferMapper::registerBuffer(const GraphicBuffer* buffer)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    err = mAllocMod->unregisterBuffer(mAllocMod, handle);
    gralloc1_error_t error = mDevice->retain(buffer);
    ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
            buffer->getNativeBuffer()->handle, error);


    ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)",
    return error;
            handle, err, strerror(-err));
    return err;
}
}


status_t GraphicBufferMapper::lock(buffer_handle_t handle,
status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
        uint32_t usage, const Rect& bounds, void** vaddr)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
    gralloc1_error_t error = mDevice->release(handle);
            bounds.left, bounds.top, bounds.width(), bounds.height(),
    ALOGW_IF(error != GRALLOC1_ERROR_NONE, "unregisterBuffer(%p): failed %d",
            vaddr);
            handle, error);


    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
    return error;
    return err;
}
}


status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle,
static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) {
        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr)
    gralloc1_rect_t outRect{};
{
    outRect.left = rect.left;
    ATRACE_CALL();
    outRect.top = rect.top;
    status_t err;
    outRect.width = rect.width();

    outRect.height = rect.height();
    if (mAllocMod->lock_ycbcr == NULL) {
    return outRect;
        return -EINVAL; // do not log failure
}
}


    err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
            bounds.left, bounds.top, bounds.width(), bounds.height(),
        const Rect& bounds, void** vaddr)
            ycbcr);
{
    return lockAsync(handle, usage, bounds, vaddr, -1);
}


    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
    return err;
        const Rect& bounds, android_ycbcr *ycbcr)
{
    return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
}
}


status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
{
{
    ATRACE_CALL();
    int32_t fenceFd = -1;
    status_t err;
    status_t error = unlockAsync(handle, &fenceFd);

    if (error == NO_ERROR) {
    err = mAllocMod->unlock(mAllocMod, handle);
        sync_wait(fenceFd, -1);

        close(fenceFd);
    ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
    }
    return err;
    return error;
}
}


status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
        uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
        uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
    sp<Fence> fence = new Fence(fenceFd);
        err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage),
    gralloc1_error_t error = mDevice->lock(handle,
                bounds.left, bounds.top, bounds.width(), bounds.height(),
            static_cast<gralloc1_producer_usage_t>(usage),
                vaddr, fenceFd);
            static_cast<gralloc1_consumer_usage_t>(usage),
    } else {
            &accessRegion, vaddr, fence);
        if (fenceFd >= 0) {
    ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle,
            sync_wait(fenceFd, -1);
            error);
            close(fenceFd);

    return error;
}
}
        err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),

                bounds.left, bounds.top, bounds.width(), bounds.height(),
static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
                vaddr);
    if (plane.bits_per_component != 8) {
        ALOGV("Invalid number of bits per component: %d",
                plane.bits_per_component);
        return false;
    }
    if (plane.bits_used != 8) {
        ALOGV("Invalid number of bits used: %d", plane.bits_used);
        return false;
    }

    bool hasValidIncrement = plane.h_increment == 1 ||
            (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
    hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
    if (!hasValidIncrement) {
        ALOGV("Invalid increment: h %d v %d", plane.h_increment,
                plane.v_increment);
        return false;
    }
    }


    ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
    return true;
    return err;
}
}


status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3
    sp<Fence> fence = new Fence(fenceFd);
            && mAllocMod->lockAsync_ycbcr != NULL) {

        err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
    if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
                static_cast<int>(usage), bounds.left, bounds.top,
        gralloc1_error_t error = mDevice->lockYCbCr(handle,
                bounds.width(), bounds.height(), ycbcr, fenceFd);
                static_cast<gralloc1_producer_usage_t>(usage),
    } else if (mAllocMod->lock_ycbcr != NULL) {
                static_cast<gralloc1_consumer_usage_t>(usage),
        if (fenceFd >= 0) {
                &accessRegion, ycbcr, fence);
            sync_wait(fenceFd, -1);
        ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lockYCbCr(%p, ...) failed: %d",
            close(fenceFd);
                handle, error);
        return error;
    }
    }
        err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),

                bounds.left, bounds.top, bounds.width(), bounds.height(),
    uint32_t numPlanes = 0;
                ycbcr);
    gralloc1_error_t error = mDevice->getNumFlexPlanes(handle, &numPlanes);
    } else {
    if (error != GRALLOC1_ERROR_NONE) {
        if (fenceFd >= 0) {
        ALOGV("Failed to retrieve number of flex planes: %d", error);
            close(fenceFd);
        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();

    error = mDevice->lockFlex(handle,
            static_cast<gralloc1_producer_usage_t>(usage),
            static_cast<gralloc1_consumer_usage_t>(usage),
            &accessRegion, &flexLayout, fence);
    if (error != GRALLOC1_ERROR_NONE) {
        ALOGW("lockFlex(%p, ...) failed: %d", handle, error);
        return error;
    }
    if (flexLayout.format != FLEX_FORMAT_YCbCr) {
        ALOGV("Unable to convert flex-format buffer to YCbCr");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    }
        return -EINVAL; // do not log failure

    // Find planes
    auto yPlane = planes.cend();
    auto cbPlane = planes.cend();
    auto crPlane = planes.cend();
    for (auto planeIter = planes.cbegin(); planeIter != planes.cend();
            ++planeIter) {
        if (planeIter->component == FLEX_COMPONENT_Y) {
            yPlane = planeIter;
        } else if (planeIter->component == FLEX_COMPONENT_Cb) {
            cbPlane = planeIter;
        } else if (planeIter->component == FLEX_COMPONENT_Cr) {
            crPlane = planeIter;
        }
    }
    if (yPlane == planes.cend()) {
        ALOGV("Unable to find Y plane");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    if (cbPlane == planes.cend()) {
        ALOGV("Unable to find Cb plane");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    if (crPlane == planes.cend()) {
        ALOGV("Unable to find Cr plane");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }

    // Validate planes
    if (!isValidYCbCrPlane(*yPlane)) {
        ALOGV("Y plane is invalid");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    if (!isValidYCbCrPlane(*cbPlane)) {
        ALOGV("Cb plane is invalid");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    if (!isValidYCbCrPlane(*crPlane)) {
        ALOGV("Cr plane is invalid");
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    }
    if (cbPlane->v_increment != crPlane->v_increment) {
        ALOGV("Cb and Cr planes have different step (%d vs. %d)",
                cbPlane->v_increment, crPlane->v_increment);
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }
    if (cbPlane->h_increment != crPlane->h_increment) {
        ALOGV("Cb and Cr planes have different stride (%d vs. %d)",
                cbPlane->h_increment, crPlane->h_increment);
        unlock(handle);
        return GRALLOC1_ERROR_UNSUPPORTED;
    }

    // Pack plane data into android_ycbcr struct
    ycbcr->y = yPlane->top_left;
    ycbcr->cb = cbPlane->top_left;
    ycbcr->cr = crPlane->top_left;
    ycbcr->ystride = static_cast<size_t>(yPlane->v_increment);
    ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment);
    ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment);


    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
    return error;
    return err;
}
}


status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    status_t err;


    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
    sp<Fence> fence = Fence::NO_FENCE;
        err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
    gralloc1_error_t error = mDevice->unlock(handle, &fence);
    } else {
    if (error != GRALLOC1_ERROR_NONE) {
        *fenceFd = -1;
        ALOGE("unlock(%p) failed: %d", handle, error);
        err = mAllocMod->unlock(mAllocMod, handle);
        return error;
    }
    }


    ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
    *fenceFd = fence->dup();
    return err;
    return error;
}
}


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------