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

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

Merge "CCodec: hold view reference in MediaCodecBuffer implementations"

parents 25823a2a 5887feb3
Loading
Loading
Loading
Loading
+30 −7
Original line number Diff line number Diff line
@@ -64,28 +64,51 @@ ICrypto::DestinationType SecureBuffer::getDestinationType() {
    return ICrypto::kDestinationTypeNativeHandle;
}

// Codec2Buffer
// LinearBlockBuffer

// static
sp<Codec2Buffer> Codec2Buffer::allocate(
sp<LinearBlockBuffer> LinearBlockBuffer::allocate(
        const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block) {
    C2WriteView writeView(block->map().get());
    if (writeView.error() != C2_OK) {
        return nullptr;
    }
    return new Codec2Buffer(format, new ABuffer(writeView.base(), writeView.capacity()), block);
    return new LinearBlockBuffer(format, std::move(writeView), block);
}

C2ConstLinearBlock Codec2Buffer::share() {
C2ConstLinearBlock LinearBlockBuffer::share() {
    return mBlock->share(offset(), size(), C2Fence());
}

Codec2Buffer::Codec2Buffer(
LinearBlockBuffer::LinearBlockBuffer(
        const sp<AMessage> &format,
        const sp<ABuffer> &buffer,
        C2WriteView&& writeView,
        const std::shared_ptr<C2LinearBlock> &block)
    : MediaCodecBuffer(format, buffer),
    : MediaCodecBuffer(format, new ABuffer(writeView.data(), writeView.size())),
      mWriteView(writeView),
      mBlock(block) {
}

// ConstLinearBlockBuffer

// static
sp<ConstLinearBlockBuffer> ConstLinearBlockBuffer::allocate(
        const sp<AMessage> &format, const C2ConstLinearBlock &block) {
    C2ReadView readView(block.map().get());
    if (readView.error() != C2_OK) {
        return nullptr;
    }
    return new ConstLinearBlockBuffer(format, std::move(readView));
}

ConstLinearBlockBuffer::ConstLinearBlockBuffer(
        const sp<AMessage> &format,
        C2ReadView&& readView)
    : MediaCodecBuffer(format, new ABuffer(
            // NOTE: ABuffer only takes non-const pointer but this data is
            //       supposed to be read-only.
            const_cast<uint8_t *>(readView.data()), readView.capacity())),
      mReadView(readView) {
}

}  // namespace android
+17 −27
Original line number Diff line number Diff line
@@ -249,7 +249,7 @@ ssize_t findBufferSlot(
    return std::distance(buffers->begin(), it);
}

sp<Codec2Buffer> allocateLinearBuffer(
sp<LinearBlockBuffer> allocateLinearBuffer(
        const std::shared_ptr<C2BlockPool> &pool,
        const sp<AMessage> &format,
        size_t size,
@@ -264,7 +264,7 @@ sp<Codec2Buffer> allocateLinearBuffer(
        return nullptr;
    }

    return Codec2Buffer::allocate(format, block);
    return LinearBlockBuffer::allocate(format, block);
}

class Buffer1D : public C2Buffer {
@@ -283,7 +283,7 @@ public:

    void add(
            size_t index,
            const sp<Codec2Buffer> &clientBuffer,
            const sp<LinearBlockBuffer> &clientBuffer,
            bool available) {
        if (mBufferArray.size() <= index) {
            // TODO: make this more efficient
@@ -340,7 +340,7 @@ public:

private:
    struct Entry {
        sp<Codec2Buffer> clientBuffer;
        sp<LinearBlockBuffer> clientBuffer;
        std::weak_ptr<C2Buffer> compBuffer;
        bool available;
    };
@@ -354,7 +354,7 @@ public:

    bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
        *buffer = nullptr;
        ssize_t ret = findBufferSlot<wp<Codec2Buffer>>(
        ssize_t ret = findBufferSlot<wp<LinearBlockBuffer>>(
                &mBuffers, kMinBufferArraySize,
                [] (const auto &elem) { return elem.promote() == nullptr; });
        if (ret < 0) {
@@ -363,7 +363,7 @@ public:
        // TODO: proper max input size and usage
        // TODO: read usage from intf
        C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
        sp<Codec2Buffer> newBuffer = allocateLinearBuffer(mPool, mFormat, kLinearBufferSize, usage);
        sp<LinearBlockBuffer> newBuffer = allocateLinearBuffer(mPool, mFormat, kLinearBufferSize, usage);
        if (newBuffer == nullptr) {
            return false;
        }
@@ -378,7 +378,7 @@ public:
        if (it == mBuffers.end()) {
            return nullptr;
        }
        sp<Codec2Buffer> codecBuffer = it->promote();
        sp<LinearBlockBuffer> codecBuffer = it->promote();
        // We got sp<> reference from the caller so this should never happen..
        CHECK(codecBuffer != nullptr);
        return std::make_shared<Buffer1D>(codecBuffer->share());
@@ -394,7 +394,7 @@ public:
        const size_t size = std::max(kMinBufferArraySize, mBuffers.size());
        mBuffers.resize(size);
        for (size_t i = 0; i < size; ++i) {
            sp<Codec2Buffer> clientBuffer = mBuffers[i].promote();
            sp<LinearBlockBuffer> clientBuffer = mBuffers[i].promote();
            bool available = false;
            if (clientBuffer == nullptr) {
                // TODO: proper max input size
@@ -414,7 +414,7 @@ public:
private:
    // Buffers we passed to the client. The index of a buffer matches what
    // was passed in BufferCallback::onInputBufferAvailable().
    std::vector<wp<Codec2Buffer>> mBuffers;
    std::vector<wp<LinearBlockBuffer>> mBuffers;
};

class GraphicInputBuffers : public CCodecBufferChannel::InputBuffers {
@@ -617,9 +617,7 @@ public:
        if (ret < 0) {
            return false;
        }
        sp<MediaCodecBuffer> newBuffer = new MediaCodecBuffer(
                mFormat,
                convert(buffer));
        sp<MediaCodecBuffer> newBuffer = convert(buffer);
        mBuffers[ret] = { newBuffer, buffer };
        *index = ret;
        *codecBuffer = newBuffer;
@@ -669,7 +667,7 @@ public:
        // track of the flushed work.
    }

    virtual sp<ABuffer> convert(const std::shared_ptr<C2Buffer> &buffer) = 0;
    virtual sp<MediaCodecBuffer> convert(const std::shared_ptr<C2Buffer> &buffer) = 0;

protected:
    struct BufferInfo {
@@ -687,9 +685,9 @@ class LinearOutputBuffers : public FlexOutputBuffers {
public:
    using FlexOutputBuffers::FlexOutputBuffers;

    virtual sp<ABuffer> convert(const std::shared_ptr<C2Buffer> &buffer) override {
    virtual sp<MediaCodecBuffer> convert(const std::shared_ptr<C2Buffer> &buffer) override {
        if (buffer == nullptr) {
            return new ABuffer(nullptr, 0);
            return new MediaCodecBuffer(mFormat, new ABuffer(nullptr, 0));
        }
        if (buffer->data().type() != C2BufferData::LINEAR) {
            // We expect linear output buffers from the component.
@@ -699,16 +697,7 @@ public:
            // We expect one and only one linear block from the component.
            return nullptr;
        }
        C2ReadView view = buffer->data().linearBlocks().front().map().get();
        if (view.error() != C2_OK) {
            // Mapping the linear block failed
            return nullptr;
        }
        return new ABuffer(
                // XXX: the data is supposed to be read-only. We don't have
                // const equivalent of ABuffer however...
                const_cast<uint8_t *>(view.data()),
                view.capacity());
        return ConstLinearBlockBuffer::allocate(mFormat, buffer->data().linearBlocks().front());
    }

    std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode() override {
@@ -737,8 +726,9 @@ class GraphicOutputBuffers : public FlexOutputBuffers {
public:
    using FlexOutputBuffers::FlexOutputBuffers;

    sp<ABuffer> convert(const std::shared_ptr<C2Buffer> &buffer) override {
        return buffer ? new ABuffer(nullptr, 1) : new ABuffer(nullptr, 0);
    sp<MediaCodecBuffer> convert(const std::shared_ptr<C2Buffer> &buffer) override {
        return new MediaCodecBuffer(
                mFormat, buffer ? new ABuffer(nullptr, 1) : new ABuffer(nullptr, 0));
    }

    std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode() override {
+26 −8
Original line number Diff line number Diff line
@@ -24,30 +24,48 @@

namespace android {

class C2Buffer;

/**
 * MediaCodecBuffer implementation wraps around C2LinearBlock.
 */
class Codec2Buffer : public MediaCodecBuffer {
class LinearBlockBuffer : public MediaCodecBuffer {
public:
    static sp<Codec2Buffer> allocate(
    static sp<LinearBlockBuffer> allocate(
            const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block);

    virtual ~Codec2Buffer() = default;
    virtual ~LinearBlockBuffer() = default;

    C2ConstLinearBlock share();

private:
    Codec2Buffer(
    LinearBlockBuffer(
            const sp<AMessage> &format,
            const sp<ABuffer> &buffer,
            C2WriteView &&writeView,
            const std::shared_ptr<C2LinearBlock> &block);
    Codec2Buffer() = delete;
    LinearBlockBuffer() = delete;

    C2WriteView mWriteView;
    std::shared_ptr<C2LinearBlock> mBlock;
};

/**
 * MediaCodecBuffer implementation wraps around C2ConstLinearBlock.
 */
class ConstLinearBlockBuffer : public MediaCodecBuffer {
public:
    static sp<ConstLinearBlockBuffer> allocate(
            const sp<AMessage> &format, const C2ConstLinearBlock &block);

    virtual ~ConstLinearBlockBuffer() = default;

private:
    ConstLinearBlockBuffer(
            const sp<AMessage> &format,
            C2ReadView &&readView);
    ConstLinearBlockBuffer() = delete;

    C2ReadView mReadView;
};

}  // namespace android

#endif  // CODEC2_BUFFER_H_