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

Commit 5d0cfe1e authored by Sungtak Lee's avatar Sungtak Lee
Browse files

GraphicsTracker/GraphicBufferAllocator : handle consumer side attach

Currently dequeueable count calculation of GraphisTracker could be wrong
when the consumer attaches a buffer. Handle consumer side attach.

Bug: 353202582
Flag: EXEMPT bugfix
Merged-In: If4d131c78896f711072511baec91f8117520e458
Change-Id: If4d131c78896f711072511baec91f8117520e458
parent 2107e8a5
Loading
Loading
Loading
Loading
+4 −25
Original line number Original line Diff line number Diff line
@@ -17,7 +17,6 @@
#define LOG_TAG "Codec2-GraphicBufferAllocator"
#define LOG_TAG "Codec2-GraphicBufferAllocator"




#include <gui/IProducerListener.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/ADebug.h>


#include <codec2/aidl/GraphicBufferAllocator.h>
#include <codec2/aidl/GraphicBufferAllocator.h>
@@ -25,25 +24,6 @@


namespace aidl::android::hardware::media::c2::implementation {
namespace aidl::android::hardware::media::c2::implementation {


class OnBufferReleasedListener : public ::android::BnProducerListener {
private:
    uint32_t mGeneration;
    std::weak_ptr<GraphicBufferAllocator> mAllocator;
public:
    OnBufferReleasedListener(
            uint32_t generation,
            const std::shared_ptr<GraphicBufferAllocator> &allocator)
            : mGeneration(generation), mAllocator(allocator) {}
    virtual ~OnBufferReleasedListener() = default;
    virtual void onBufferReleased() {
        auto p = mAllocator.lock();
        if (p) {
            p->onBufferReleased(mGeneration);
        }
    }
    virtual bool needsReleaseNotify() { return true; }
};

::ndk::ScopedAStatus GraphicBufferAllocator::allocate(
::ndk::ScopedAStatus GraphicBufferAllocator::allocate(
        const IGraphicBufferAllocator::Description& in_desc,
        const IGraphicBufferAllocator::Description& in_desc,
        IGraphicBufferAllocator::Allocation* _aidl_return) {
        IGraphicBufferAllocator::Allocation* _aidl_return) {
@@ -108,15 +88,14 @@ void GraphicBufferAllocator::reset() {
    mGraphicsTracker->stop();
    mGraphicsTracker->stop();
}
}


const ::android::sp<::android::IProducerListener> GraphicBufferAllocator::createReleaseListener(
      uint32_t generation) {
    return new OnBufferReleasedListener(generation, ref<GraphicBufferAllocator>());
}

void GraphicBufferAllocator::onBufferReleased(uint32_t generation) {
void GraphicBufferAllocator::onBufferReleased(uint32_t generation) {
    mGraphicsTracker->onReleased(generation);
    mGraphicsTracker->onReleased(generation);
}
}


void GraphicBufferAllocator::onBufferAttached(uint32_t generation) {
    mGraphicsTracker->onAttached(generation);
}

c2_status_t GraphicBufferAllocator::allocate(
c2_status_t GraphicBufferAllocator::allocate(
        uint32_t width, uint32_t height, ::android::PixelFormat format, uint64_t usage,
        uint32_t width, uint32_t height, ::android::PixelFormat format, uint64_t usage,
        AHardwareBuffer **buf, ::android::sp<::android::Fence> *fence) {
        AHardwareBuffer **buf, ::android::sp<::android::Fence> *fence) {
+13 −0
Original line number Original line Diff line number Diff line
@@ -1001,6 +1001,11 @@ void GraphicsTracker::onReleased(uint32_t generation) {
    {
    {
        std::unique_lock<std::mutex> l(mLock);
        std::unique_lock<std::mutex> l(mLock);
        if (mBufferCache->mGeneration == generation) {
        if (mBufferCache->mGeneration == generation) {
            if (mBufferCache->mNumAttached > 0) {
                ALOGV("one onReleased() ignored for each prior onAttached().");
                mBufferCache->mNumAttached--;
                return;
            }
            if (!adjustDequeueConfLocked(&updateDequeue)) {
            if (!adjustDequeueConfLocked(&updateDequeue)) {
                mDequeueable++;
                mDequeueable++;
                writeIncDequeueableLocked(1);
                writeIncDequeueableLocked(1);
@@ -1012,4 +1017,12 @@ void GraphicsTracker::onReleased(uint32_t generation) {
    }
    }
}
}


void GraphicsTracker::onAttached(uint32_t generation) {
    std::unique_lock<std::mutex> l(mLock);
    if (mBufferCache->mGeneration == generation) {
        ALOGV("buffer attached");
        mBufferCache->mNumAttached++;
    }
}

} // namespace aidl::android::hardware::media::c2::implementation
} // namespace aidl::android::hardware::media::c2::implementation
+4 −9
Original line number Original line Diff line number Diff line
@@ -71,23 +71,18 @@ public:
    void reset();
    void reset();


    /**
    /**
     * Create a listener for buffer being released.
     * Notifies a buffer being released.
     *
     * Surface will register this listener and notify whenever the consumer
     * releases a buffer.
     *
     *
     * @param   generation        generation # for the BufferQueue.
     * @param   generation        generation # for the BufferQueue.
     * @return  IProducerListener can be used when connect# to Surface.
     */
     */
    const ::android::sp<::android::IProducerListener> createReleaseListener(
    void onBufferReleased(uint32_t generation);
            uint32_t generation);


    /**
    /**
     * Notifies a buffer being released.
     * Notifies a buffer being attached to the consumer.
     *
     *
     * @param   generation        generation # for the BufferQueue.
     * @param   generation        generation # for the BufferQueue.
     */
     */
    void onBufferReleased(uint32_t generation);
    void onBufferAttached(uint32_t generation);


    /**
    /**
     * Allocates a buffer.
     * Allocates a buffer.
+16 −2
Original line number Original line Diff line number Diff line
@@ -142,6 +142,18 @@ public:
     */
     */
    void onReleased(uint32_t generation);
    void onReleased(uint32_t generation);


    /**
     * Notifies when a Buffer is attached to Graphics(consumer side).
     * If generation does not match to the current, notifications via the interface
     * will be ignored. (In the case, the notifications are from one of the old surfaces
     * which is no longer used.)
     * One onReleased() should be ignored for one onAttached() when both of
     * them have the same generation as params.
     *
     * @param[in] generation    generation id for specifying Graphics(BQ)
     */
    void onAttached(uint32_t generation);

    /**
    /**
     * Get waitable fd for events.(allocate is ready, end of life cycle)
     * Get waitable fd for events.(allocate is ready, end of life cycle)
     *
     *
@@ -217,9 +229,11 @@ private:


        BlockedSlot mBlockedSlots[kNumSlots];
        BlockedSlot mBlockedSlots[kNumSlots];


        BufferCache() : mBqId{0ULL}, mGeneration{0}, mIgbp{nullptr} {}
        std::atomic<int> mNumAttached;

        BufferCache() : mBqId{0ULL}, mGeneration{0}, mIgbp{nullptr}, mNumAttached{0} {}
        BufferCache(uint64_t bqId, uint32_t generation, const sp<IGraphicBufferProducer>& igbp) :
        BufferCache(uint64_t bqId, uint32_t generation, const sp<IGraphicBufferProducer>& igbp) :
            mBqId{bqId}, mGeneration{generation}, mIgbp{igbp} {}
            mBqId{bqId}, mGeneration{generation}, mIgbp{igbp}, mNumAttached{0} {}


        ~BufferCache();
        ~BufferCache();