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

Commit d30823a1 authored by Jim Shargo's avatar Jim Shargo
Browse files

libgui: ConsumerBase-based classes now create their own BufferQueues

Using ConsumerBase-based classes is now the recommended way to create
BufferQueues.

This also includes a few new methods that are used by downstream classes
to avoid calling methods on raw IGBP/IGBCs.

I decided to keep and deprecate the old ctors temporarily. Before I roll
the flag out I'll remove them, but this way I can build the whole build
with or without the flag.

This is an important step for go/warren-buffers, because it consolidates
usages of BufferQueues to supported APIs and reduces the libgui API
surface that exposes IGBP/IGBC.

BYPASS_IGBP_IGBC_API_REASON: this CL is part of the migration.

Bug: 340933754
Flag: com.android.graphics.libgui.flags.wb_consumer_base_owns_bq
Test: atest, presubmit, compiles

Change-Id: I977165f3e50bc343df396a4c5ecc97fe31a67d5a
parent 912ff82e
Loading
Loading
Loading
Loading
+12 −4
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0
//#define LOG_NDEBUG 0


#include <com_android_graphics_libgui_flags.h>
#include <cutils/atomic.h>
#include <cutils/atomic.h>
#include <gui/BLASTBufferQueue.h>
#include <gui/BLASTBufferQueue.h>
#include <gui/BufferItemConsumer.h>
#include <gui/BufferItemConsumer.h>
@@ -174,14 +175,21 @@ BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinati
        mSyncTransaction(nullptr),
        mSyncTransaction(nullptr),
        mUpdateDestinationFrame(updateDestinationFrame) {
        mUpdateDestinationFrame(updateDestinationFrame) {
    createBufferQueue(&mProducer, &mConsumer);
    createBufferQueue(&mProducer, &mConsumer);
    // since the adapter is in the client process, set dequeue timeout
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    // explicitly so that dequeueBuffer will block
    mBufferItemConsumer = new BLASTBufferItemConsumer(mProducer, mConsumer,
    mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
                                                      GraphicBuffer::USAGE_HW_COMPOSER |

                                                              GraphicBuffer::USAGE_HW_TEXTURE,
                                                      1, false, this);
#else
    mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
    mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
                                                      GraphicBuffer::USAGE_HW_COMPOSER |
                                                      GraphicBuffer::USAGE_HW_COMPOSER |
                                                              GraphicBuffer::USAGE_HW_TEXTURE,
                                                              GraphicBuffer::USAGE_HW_TEXTURE,
                                                      1, false, this);
                                                      1, false, this);
#endif //  COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    // since the adapter is in the client process, set dequeue timeout
    // explicitly so that dequeueBuffer will block
    mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());

    static std::atomic<uint32_t> nextId = 0;
    static std::atomic<uint32_t> nextId = 0;
    mProducerId = nextId++;
    mProducerId = nextId++;
    mName = name + "#" + std::to_string(mProducerId);
    mName = name + "#" + std::to_string(mProducerId);
+23 −4
Original line number Original line Diff line number Diff line
@@ -35,18 +35,37 @@


namespace android {
namespace android {


#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
BufferItemConsumer::BufferItemConsumer(uint64_t consumerUsage, int bufferCount,
                                       bool controlledByApp, bool isConsumerSurfaceFlinger)
      : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger) {
    initialize(consumerUsage, bufferCount);
}

BufferItemConsumer::BufferItemConsumer(const sp<IGraphicBufferProducer>& producer,
                                       const sp<IGraphicBufferConsumer>& consumer,
                                       uint64_t consumerUsage, int bufferCount,
                                       bool controlledByApp)
      : ConsumerBase(producer, consumer, controlledByApp) {
    initialize(consumerUsage, bufferCount);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

BufferItemConsumer::BufferItemConsumer(
BufferItemConsumer::BufferItemConsumer(
        const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
        const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
        int bufferCount, bool controlledByApp) :
        int bufferCount, bool controlledByApp) :
    ConsumerBase(consumer, controlledByApp)
    ConsumerBase(consumer, controlledByApp)
{
{
    initialize(consumerUsage, bufferCount);
}

void BufferItemConsumer::initialize(uint64_t consumerUsage, int bufferCount) {
    status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
    status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
    LOG_ALWAYS_FATAL_IF(err != OK,
    LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
            "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
    if (bufferCount != DEFAULT_MAX_BUFFERS) {
    if (bufferCount != DEFAULT_MAX_BUFFERS) {
        err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
        err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
        LOG_ALWAYS_FATAL_IF(err != OK,
        LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set max acquired buffer count to %d",
                "Failed to set max acquired buffer count to %d", bufferCount);
                            bufferCount);
    }
    }
}
}


+64 −4
Original line number Original line Diff line number Diff line
@@ -14,8 +14,6 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <inttypes.h>

#define LOG_TAG "ConsumerBase"
#define LOG_TAG "ConsumerBase"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0
//#define LOG_NDEBUG 0
@@ -29,19 +27,22 @@


#include <cutils/atomic.h>
#include <cutils/atomic.h>


#include <com_android_graphics_libgui_flags.h>
#include <gui/BufferItem.h>
#include <gui/BufferItem.h>
#include <gui/BufferQueue.h>
#include <gui/ConsumerBase.h>
#include <gui/ConsumerBase.h>
#include <gui/ISurfaceComposer.h>
#include <gui/ISurfaceComposer.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/SurfaceComposerClient.h>
#include <ui/BufferQueueDefs.h>


#include <private/gui/ComposerService.h>
#include <private/gui/ComposerService.h>


#include <log/log.h>
#include <utils/Log.h>
#include <utils/Log.h>
#include <utils/String8.h>
#include <utils/String8.h>
#include <utils/Trace.h>
#include <utils/Trace.h>


#include <com_android_graphics_libgui_flags.h>
#include <inttypes.h>


// Macros for including the ConsumerBase name in log messages
// Macros for including the ConsumerBase name in log messages
#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
@@ -62,6 +63,30 @@ ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool c
        mAbandoned(false),
        mAbandoned(false),
        mConsumer(bufferQueue),
        mConsumer(bufferQueue),
        mPrevFinalReleaseFence(Fence::NO_FENCE) {
        mPrevFinalReleaseFence(Fence::NO_FENCE) {
    initialize(controlledByApp);
}

#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
ConsumerBase::ConsumerBase(bool controlledByApp, bool consumerIsSurfaceFlinger)
      : mAbandoned(false), mPrevFinalReleaseFence(Fence::NO_FENCE) {
    sp<IGraphicBufferProducer> producer;
    BufferQueue::createBufferQueue(&producer, &mConsumer, consumerIsSurfaceFlinger);
    mSurface = sp<Surface>::make(producer, controlledByApp);
    initialize(controlledByApp);
}

ConsumerBase::ConsumerBase(const sp<IGraphicBufferProducer>& producer,
                           const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp)
      : mAbandoned(false),
        mConsumer(consumer),
        mSurface(sp<Surface>::make(producer, controlledByApp)),
        mPrevFinalReleaseFence(Fence::NO_FENCE) {
    initialize(controlledByApp);
}

#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

void ConsumerBase::initialize(bool controlledByApp) {
    // Choose a name using the PID and a process-unique ID.
    // Choose a name using the PID and a process-unique ID.
    mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
    mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());


@@ -355,6 +380,17 @@ status_t ConsumerBase::setTransformHint(uint32_t hint) {
    return mConsumer->setTransformHint(hint);
    return mConsumer->setTransformHint(hint);
}
}


#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
status_t ConsumerBase::setMaxBufferCount(int bufferCount) {
    Mutex::Autolock lock(mMutex);
    if (mAbandoned) {
        CB_LOGE("setMaxBufferCount: ConsumerBase is abandoned!");
        return NO_INIT;
    }
    return mConsumer->setMaxBufferCount(bufferCount);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
    Mutex::Autolock lock(mMutex);
    Mutex::Autolock lock(mMutex);
    if (mAbandoned) {
    if (mAbandoned) {
@@ -364,6 +400,17 @@ status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
    return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
    return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
}
}


#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
status_t ConsumerBase::setConsumerIsProtected(bool isProtected) {
    Mutex::Autolock lock(mMutex);
    if (mAbandoned) {
        CB_LOGE("setConsumerIsProtected: ConsumerBase is abandoned!");
        return NO_INIT;
    }
    return mConsumer->setConsumerIsProtected(isProtected);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

sp<NativeHandle> ConsumerBase::getSidebandStream() const {
sp<NativeHandle> ConsumerBase::getSidebandStream() const {
    Mutex::Autolock _l(mMutex);
    Mutex::Autolock _l(mMutex);
    if (mAbandoned) {
    if (mAbandoned) {
@@ -430,6 +477,19 @@ void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
    }
    }
}
}


#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
sp<Surface> ConsumerBase::getSurface() const {
    LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
                        "It's illegal to get the surface of a Consumer that does not own it. This "
                        "should be impossible once the old CTOR is removed.");
    return mSurface;
}

sp<IGraphicBufferConsumer> ConsumerBase::getIGraphicBufferConsumer() const {
    return mConsumer;
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
        nsecs_t presentWhen, uint64_t maxFrameNumber) {
        nsecs_t presentWhen, uint64_t maxFrameNumber) {
    if (mAbandoned) {
    if (mAbandoned) {
+21 −8
Original line number Original line Diff line number Diff line
@@ -18,9 +18,9 @@
#define LOG_TAG "CpuConsumer"
#define LOG_TAG "CpuConsumer"
//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define ATRACE_TAG ATRACE_TAG_GRAPHICS


#include <gui/CpuConsumer.h>
#include <com_android_graphics_libgui_flags.h>

#include <gui/BufferItem.h>
#include <gui/BufferItem.h>
#include <gui/CpuConsumer.h>
#include <utils/Log.h>
#include <utils/Log.h>


#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
@@ -31,12 +31,25 @@


namespace android {
namespace android {


CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
        size_t maxLockedBuffers, bool controlledByApp) :
CpuConsumer::CpuConsumer(size_t maxLockedBuffers, bool controlledByApp,
    ConsumerBase(bq, controlledByApp),
                         bool isConsumerSurfaceFlinger)
      : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger),
        mMaxLockedBuffers(maxLockedBuffers),
        mCurrentLockedBuffers(0) {
    // Create tracking entries for locked buffers
    mAcquiredBuffers.insertAt(0, maxLockedBuffers);

    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
    mConsumer->setMaxAcquiredBufferCount(static_cast<int32_t>(maxLockedBuffers));
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
                         bool controlledByApp)
      : ConsumerBase(bq, controlledByApp),
        mMaxLockedBuffers(maxLockedBuffers),
        mMaxLockedBuffers(maxLockedBuffers),
    mCurrentLockedBuffers(0)
        mCurrentLockedBuffers(0) {
{
    // Create tracking entries for locked buffers
    // Create tracking entries for locked buffers
    mAcquiredBuffers.insertAt(0, maxLockedBuffers);
    mAcquiredBuffers.insertAt(0, maxLockedBuffers);


+76 −21
Original line number Original line Diff line number Diff line
@@ -101,6 +101,34 @@ static bool hasEglProtectedContent() {
    return hasIt;
    return hasIt;
}
}


#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
GLConsumer::GLConsumer(uint32_t tex, uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
      : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false),
        mCurrentCrop(Rect::EMPTY_RECT),
        mCurrentTransform(0),
        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
        mCurrentFence(Fence::NO_FENCE),
        mCurrentTimestamp(0),
        mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
        mCurrentFrameNumber(0),
        mDefaultWidth(1),
        mDefaultHeight(1),
        mFilteringEnabled(true),
        mTexName(tex),
        mUseFenceSync(useFenceSync),
        mTexTarget(texTarget),
        mEglDisplay(EGL_NO_DISPLAY),
        mEglContext(EGL_NO_CONTEXT),
        mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
        mAttached(true) {
    GLC_LOGV("GLConsumer");

    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));

    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
        uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
        uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
    ConsumerBase(bq, isControlledByApp),
    ConsumerBase(bq, isControlledByApp),
@@ -130,9 +158,9 @@ GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
}
}


GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
        bool useFenceSync, bool isControlledByApp) :
GLConsumer::GLConsumer(uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
    ConsumerBase(bq, isControlledByApp),
      : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false),
        mCurrentCrop(Rect::EMPTY_RECT),
        mCurrentCrop(Rect::EMPTY_RECT),
        mCurrentTransform(0),
        mCurrentTransform(0),
        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
@@ -149,8 +177,35 @@ GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
        mEglDisplay(EGL_NO_DISPLAY),
        mEglDisplay(EGL_NO_DISPLAY),
        mEglContext(EGL_NO_CONTEXT),
        mEglContext(EGL_NO_CONTEXT),
        mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
        mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
    mAttached(false)
        mAttached(false) {
{
    GLC_LOGV("GLConsumer");

    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));

    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
}
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget, bool useFenceSync,
                       bool isControlledByApp)
      : ConsumerBase(bq, isControlledByApp),
        mCurrentCrop(Rect::EMPTY_RECT),
        mCurrentTransform(0),
        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
        mCurrentFence(Fence::NO_FENCE),
        mCurrentTimestamp(0),
        mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
        mCurrentFrameNumber(0),
        mDefaultWidth(1),
        mDefaultHeight(1),
        mFilteringEnabled(true),
        mTexName(0),
        mUseFenceSync(useFenceSync),
        mTexTarget(texTarget),
        mEglDisplay(EGL_NO_DISPLAY),
        mEglContext(EGL_NO_CONTEXT),
        mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
        mAttached(false) {
    GLC_LOGV("GLConsumer");
    GLC_LOGV("GLConsumer");


    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Loading