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

Commit cb9be977 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove dependency between bufferpool and codec2"

parents b62c79b1 bee893b4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -43,8 +43,8 @@ Return<void> Accessor::connect(connect_cb _hidl_cb) {
    return Void();
}

Accessor::Accessor(const std::shared_ptr<C2Allocator> &allocator, bool linear)
    : mImpl(new Impl(allocator, linear)) {}
Accessor::Accessor(const std::shared_ptr<BufferPoolAllocator> &allocator)
    : mImpl(new Impl(allocator)) {}

Accessor::~Accessor() {
}
+1 −3
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
#include <android/hardware/media/bufferpool/1.0/IAccessor.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <C2Buffer.h>
#include <BufferPoolTypes.h>
#include "BufferStatus.h"

@@ -53,9 +52,8 @@ struct Accessor : public IAccessor {
     * Creates a buffer pool accessor which uses the specified allocator.
     *
     * @param allocator buffer allocator.
     * @param linear    whether the allocator is linear or not.
     */
    Accessor(const std::shared_ptr<C2Allocator> &allocator, bool linear);
    explicit Accessor(const std::shared_ptr<BufferPoolAllocator> &allocator);

    /** Destructs a buffer pool accessor. */
    ~Accessor();
+19 −106
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
#include <time.h>
#include <unistd.h>
#include <utils/Log.h>
#include <C2Buffer.h>
#include "AccessorImpl.h"
#include "Connection.h"

@@ -38,48 +37,18 @@ struct InternalBuffer {
    BufferId mId;
    size_t mOwnerCount;
    size_t mTransactionCount;
    const bool mLinear;
    const std::shared_ptr<C2LinearAllocation> mLinearAllocation;
    const std::shared_ptr<C2GraphicAllocation> mGraphicAllocation;
    const std::shared_ptr<BufferPoolAllocation> mAllocation;
    const std::vector<uint8_t> mConfig;

    InternalBuffer(
            BufferId id,
            const std::shared_ptr<C2LinearAllocation> &alloc,
            const std::shared_ptr<BufferPoolAllocation> &alloc,
            const std::vector<uint8_t> &allocConfig)
            : mId(id), mOwnerCount(0), mTransactionCount(0),
            mLinear(true), mLinearAllocation(alloc),
            mGraphicAllocation(nullptr),
            mConfig(allocConfig) {}

    InternalBuffer(
            BufferId id,
            const std::shared_ptr<C2GraphicAllocation> &alloc,
            const std::vector<uint8_t> &allocConfig)
            : mId(id), mOwnerCount(0), mTransactionCount(0),
            mLinear(false), mLinearAllocation(nullptr),
            mGraphicAllocation(alloc),
            mConfig(allocConfig) {}
            mAllocation(alloc), mConfig(allocConfig) {}

    const native_handle_t *handle() {
        if (mLinear) {
            return mLinearAllocation->handle();
        } else {
            return mGraphicAllocation->handle();
        }
    }

    // TODO : support non exact matching. e.g) capacity
    bool isRecyclable(const std::vector<uint8_t> &config) {
        if (mConfig.size() == config.size()) {
            for (size_t i = 0; i < config.size(); ++i) {
                if (mConfig[i] != config[i]) {
                    return false;
                }
            }
                return true;
        }
        return false;
        return mAllocation->handle();
    }
};

@@ -154,8 +123,8 @@ int32_t Accessor::Impl::sPid = getpid();
uint32_t Accessor::Impl::sSeqId = time(NULL);

Accessor::Impl::Impl(
        const std::shared_ptr<C2Allocator> &allocator, bool linear)
        : mAllocator(allocator), mLinear(linear) {}
        const std::shared_ptr<BufferPoolAllocator> &allocator)
        : mAllocator(allocator) {}

Accessor::Impl::~Impl() {
}
@@ -193,14 +162,8 @@ ResultStatus Accessor::Impl::allocate(
    std::lock_guard<std::mutex> lock(mBufferPool.mMutex);
    mBufferPool.processStatusMessages();
    ResultStatus status = ResultStatus::OK;
    if (!mBufferPool.getFreeBuffer(params, bufferId, handle)) {
        if (mLinear) {
            status = mBufferPool.getNewLinearBuffer(
                    mAllocator, params, bufferId, handle);
        } else {
            status = mBufferPool.getNewGraphicBuffer(
                    mAllocator, params, bufferId, handle);
        }
    if (!mBufferPool.getFreeBuffer(mAllocator, params, bufferId, handle)) {
        status = mBufferPool.getNewBuffer(mAllocator, params, bufferId, handle);
        ALOGV("create a buffer %d : %u %p",
              status == ResultStatus::OK, *bufferId, *handle);
    }
@@ -437,12 +400,13 @@ bool Accessor::Impl::BufferPool::handleClose(ConnectionId connectionId) {
}

bool Accessor::Impl::BufferPool::getFreeBuffer(
        const std::shared_ptr<BufferPoolAllocator> &allocator,
        const std::vector<uint8_t> &params, BufferId *pId,
        const native_handle_t** handle) {
    auto bufferIt = mFreeBuffers.begin();
    for (;bufferIt != mFreeBuffers.end(); ++bufferIt) {
        BufferId bufferId = *bufferIt;
        if (mBuffers[bufferId]->isRecyclable(params)) {
        if (allocator->compatible(params, mBuffers[bufferId]->mConfig)) {
            break;
        }
    }
@@ -457,81 +421,30 @@ bool Accessor::Impl::BufferPool::getFreeBuffer(
    return false;
}

ResultStatus Accessor::Impl::BufferPool::getNewLinearBuffer(
        const std::shared_ptr<C2Allocator> &allocator,
ResultStatus Accessor::Impl::BufferPool::getNewBuffer(
        const std::shared_ptr<BufferPoolAllocator> &allocator,
        const std::vector<uint8_t> &params, BufferId *pId,
        const native_handle_t** handle) {
    union LinearParam {
        struct {
            uint32_t capacity;
            C2MemoryUsage usage;
        } data;
        uint8_t array[0];
        LinearParam() : data{0, {0, 0}} {}
    } linearParam;
    memcpy(&linearParam, params.data(),
           std::min(sizeof(linearParam), params.size()));
    std::shared_ptr<C2LinearAllocation> linearAlloc;
    c2_status_t status = allocator->newLinearAllocation(
            linearParam.data.capacity, linearParam.data.usage, &linearAlloc);
    if (status == C2_OK) {
        BufferId bufferId = mSeq++;
        std::unique_ptr<InternalBuffer> buffer =
                std::make_unique<InternalBuffer>(
                        bufferId, linearAlloc, params);
        if (buffer) {
            auto res = mBuffers.insert(std::make_pair(
                    bufferId, std::move(buffer)));
            if (res.second) {
                *handle = linearAlloc->handle();
                *pId = bufferId;
                return ResultStatus::OK;
            }
        }
        return ResultStatus::NO_MEMORY;
    }
    // TODO: map C2 error code
    return ResultStatus::CRITICAL_ERROR;
}
    std::shared_ptr<BufferPoolAllocation> alloc;
    ResultStatus status = allocator->allocate(params, &alloc);

ResultStatus Accessor::Impl::BufferPool::getNewGraphicBuffer(
        const std::shared_ptr<C2Allocator> &allocator,
        const std::vector<uint8_t> &params, BufferId *pId,
        const native_handle_t** handle) {
    union GraphicParam {
        struct {
            uint32_t width;
            uint32_t height;
            uint32_t format;
            C2MemoryUsage usage;
        } data;
        uint8_t array[0];
        GraphicParam() : data{0, 0, 0, {0, 0}} {}
    } graphicParam;
    memcpy(&graphicParam, params.data(),
           std::min(sizeof(graphicParam), params.size()));
    std::shared_ptr<C2GraphicAllocation> graphicAlloc;
    c2_status_t status = allocator->newGraphicAllocation(
            graphicParam.data.width, graphicParam.data.height,
            graphicParam.data.format, graphicParam.data.usage, &graphicAlloc);
    if (status == C2_OK) {
        BufferId bufferId = mSeq;
    if (status == ResultStatus::OK) {
        BufferId bufferId = mSeq++;
        std::unique_ptr<InternalBuffer> buffer =
                std::make_unique<InternalBuffer>(
                        bufferId, graphicAlloc, params);
                        bufferId, alloc, params);
        if (buffer) {
            auto res = mBuffers.insert(std::make_pair(
                    bufferId, std::move(buffer)));
            if (res.second) {
                *handle = graphicAlloc->handle();
                *handle = alloc->handle();
                *pId = bufferId;
                return ResultStatus::OK;
            }
        }
        return ResultStatus::NO_MEMORY;
    }
    // TODO: map C2 error code
    return ResultStatus::CRITICAL_ERROR;
    return status;
}

}  // namespace implementation
+12 −27
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ struct TransactionStatus;
 * An implementation of a buffer pool accessor(or a buffer pool implementation.) */
class Accessor::Impl {
public:
    Impl(const std::shared_ptr<C2Allocator> &allocator, bool linear);
    Impl(const std::shared_ptr<BufferPoolAllocator> &allocator);

    ~Impl();

@@ -64,8 +64,7 @@ private:
    static uint32_t sSeqId;
    static int32_t sPid;

    const std::shared_ptr<C2Allocator> mAllocator;
    bool mLinear;
    const std::shared_ptr<BufferPoolAllocator> mAllocator;

    /**
     * Buffer pool implementation.
@@ -172,6 +171,7 @@ private:
        /**
         * Recycles a existing free buffer if it is possible.
         *
         * @param allocator the buffer allocator
         * @param params    the allocation parameters.
         * @param pId       the id of the recycled buffer.
         * @param handle    the native handle of the recycled buffer.
@@ -179,40 +179,25 @@ private:
         * @return {@code true} when a buffer is recycled, {@code false}
         *         otherwise.
         */
        bool getFreeBuffer(const std::vector<uint8_t> &params, BufferId *pId,
                           const native_handle_t **handle);

        /**
         * Creates a new linear buffer.
         *
         * @param allocator the linear buffer allocator
         * @param params    the allocator parameters
         * @param pId       the buffer id for the newly allocated buffer.
         * @param handle    the native handle for the newly allocated buffer.
         *
         * @return OK when a linear allocation is successfully allocated.
         *         NO_MEMORY when there is no memory.
         *         CRITICAL_ERROR otherwise.
         */
        ResultStatus getNewLinearBuffer(
                const std::shared_ptr<C2Allocator> &allocator,
                const std::vector<uint8_t> &params, BufferId *pId,
                const native_handle_t **handle);
        bool getFreeBuffer(
                const std::shared_ptr<BufferPoolAllocator> &allocator,
                const std::vector<uint8_t> &params,
                BufferId *pId, const native_handle_t **handle);

        /**
         * Creates a new graphic buffer.
         * Creates a new buffer.
         *
         * @param allocator the graphic buffer allocator
         * @param allocator the buffer allocator
         * @param params    the allocator parameters
         * @param pId       the buffer id for the newly allocated buffer.
         * @param handle    the native handle for the newly allocated buffer.
         *
         * @return OK when a graphic allocation is successfully allocated.
         * @return OK when an allocation is successfully allocated.
         *         NO_MEMORY when there is no memory.
         *         CRITICAL_ERROR otherwise.
         */
        ResultStatus getNewGraphicBuffer(
                const std::shared_ptr<C2Allocator> &allocator,
        ResultStatus getNewBuffer(
                const std::shared_ptr<BufferPoolAllocator> &allocator,
                const std::vector<uint8_t> &params, BufferId *pId,
                const native_handle_t **handle);

+0 −1
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ cc_library {
        "libhidlbase",
        "libhidltransport",
        "liblog",
        "libstagefright_codec2",
        "libutils",
        "android.hardware.media.bufferpool@1.0",
    ],
Loading