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

Commit 123967f7 authored by Jiwen Cai's avatar Jiwen Cai Committed by Android (Google) Code Review
Browse files

Merge changes I35631762,I2215bd78,I7d8aa233,I78bd879f

* changes:
  Support BufferHub-backed IGBP in libgui_test
  Add BufferHub-based IGBC implementation
  Move clang warnings override out of libgui
  Add BufferHub backend for android::view::Surface
parents d079f373 22029508
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -65,6 +65,9 @@ cc_library_shared {
        // Allow implicit instantiation for templated class function
        "-Wno-undefined-func-template",

        // Allow explicitly marking struct as packed even when unnecessary
        "-Wno-packed",

        "-DDEBUG_ONLY_CODE=0",
    ],

@@ -82,6 +85,7 @@ cc_library_shared {

    srcs: [
        "BitTube.cpp",
        "BufferHubConsumer.cpp",
        "BufferHubProducer.cpp",
        "BufferItem.cpp",
        "BufferItemConsumer.cpp",
+161 −0
Original line number Diff line number Diff line
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gui/BufferHubConsumer.h>

namespace android {

using namespace dvr;

/* static */
sp<BufferHubConsumer> BufferHubConsumer::Create(const std::shared_ptr<ConsumerQueue>& queue) {
    sp<BufferHubConsumer> consumer = new BufferHubConsumer;
    consumer->mQueue = queue;
    return consumer;
}

/* static */ sp<BufferHubConsumer> BufferHubConsumer::Create(ConsumerQueueParcelable parcelable) {
    if (!parcelable.IsValid()) {
        ALOGE("BufferHubConsumer::Create: Invalid consumer parcelable.");
        return nullptr;
    }

    sp<BufferHubConsumer> consumer = new BufferHubConsumer;
    consumer->mQueue = ConsumerQueue::Import(parcelable.TakeChannelHandle());
    return consumer;
}

status_t BufferHubConsumer::acquireBuffer(BufferItem* /*buffer*/, nsecs_t /*presentWhen*/,
                                          uint64_t /*maxFrameNumber*/) {
    ALOGE("BufferHubConsumer::acquireBuffer: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::detachBuffer(int /*slot*/) {
    ALOGE("BufferHubConsumer::detachBuffer: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::attachBuffer(int* /*outSlot*/, const sp<GraphicBuffer>& /*buffer*/) {
    ALOGE("BufferHubConsumer::attachBuffer: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::releaseBuffer(int /*buf*/, uint64_t /*frameNumber*/,
                                          EGLDisplay /*display*/, EGLSyncKHR /*fence*/,
                                          const sp<Fence>& /*releaseFence*/) {
    ALOGE("BufferHubConsumer::releaseBuffer: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::consumerConnect(const sp<IConsumerListener>& /*consumer*/,
                                            bool /*controlledByApp*/) {
    ALOGE("BufferHubConsumer::consumerConnect: not implemented.");

    // TODO(b/73267953): Make BufferHub honor producer and consumer connection. Returns NO_ERROR to
    // make IGraphicBufferConsumer_test happy.
    return NO_ERROR;
}

status_t BufferHubConsumer::consumerDisconnect() {
    ALOGE("BufferHubConsumer::consumerDisconnect: not implemented.");

    // TODO(b/73267953): Make BufferHub honor producer and consumer connection. Returns NO_ERROR to
    // make IGraphicBufferConsumer_test happy.
    return NO_ERROR;
}

status_t BufferHubConsumer::getReleasedBuffers(uint64_t* /*slotMask*/) {
    ALOGE("BufferHubConsumer::getReleasedBuffers: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setDefaultBufferSize(uint32_t /*w*/, uint32_t /*h*/) {
    ALOGE("BufferHubConsumer::setDefaultBufferSize: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setMaxBufferCount(int /*bufferCount*/) {
    ALOGE("BufferHubConsumer::setMaxBufferCount: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setMaxAcquiredBufferCount(int /*maxAcquiredBuffers*/) {
    ALOGE("BufferHubConsumer::setMaxAcquiredBufferCount: not implemented.");

    // TODO(b/73267953): Make BufferHub honor producer and consumer connection. Returns NO_ERROR to
    // make IGraphicBufferConsumer_test happy.
    return NO_ERROR;
}

status_t BufferHubConsumer::setConsumerName(const String8& /*name*/) {
    ALOGE("BufferHubConsumer::setConsumerName: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setDefaultBufferFormat(PixelFormat /*defaultFormat*/) {
    ALOGE("BufferHubConsumer::setDefaultBufferFormat: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setDefaultBufferDataSpace(android_dataspace /*defaultDataSpace*/) {
    ALOGE("BufferHubConsumer::setDefaultBufferDataSpace: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setConsumerUsageBits(uint64_t /*usage*/) {
    ALOGE("BufferHubConsumer::setConsumerUsageBits: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setConsumerIsProtected(bool /*isProtected*/) {
    ALOGE("BufferHubConsumer::setConsumerIsProtected: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::setTransformHint(uint32_t /*hint*/) {
    ALOGE("BufferHubConsumer::setTransformHint: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::getSidebandStream(sp<NativeHandle>* /*outStream*/) const {
    ALOGE("BufferHubConsumer::getSidebandStream: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::getOccupancyHistory(
        bool /*forceFlush*/, std::vector<OccupancyTracker::Segment>* /*outHistory*/) {
    ALOGE("BufferHubConsumer::getOccupancyHistory: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::discardFreeBuffers() {
    ALOGE("BufferHubConsumer::discardFreeBuffers: not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubConsumer::dumpState(const String8& /*prefix*/, String8* /*outResult*/) const {
    ALOGE("BufferHubConsumer::dumpState: not implemented.");
    return INVALID_OPERATION;
}

IBinder* BufferHubConsumer::onAsBinder() {
    ALOGE("BufferHubConsumer::onAsBinder: BufferHubConsumer should never be used as an Binder "
          "object.");
    return nullptr;
}

} // namespace android
+38 −25
Original line number Diff line number Diff line
@@ -14,29 +14,18 @@
 * limitations under the License.
 */

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif

// The following headers are included without checking every warning.
// TODO(b/72172820): Remove the workaround once we have enforced -Weverything
// in these headers and their dependencies.
#include <dvr/dvr_api.h>
#include <gui/BufferHubProducer.h>

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

#include <inttypes.h>
#include <log/log.h>
#include <system/window.h>

namespace android {

using namespace dvr;

/* static */
sp<BufferHubProducer> BufferHubProducer::Create(const std::shared_ptr<dvr::ProducerQueue>& queue) {
sp<BufferHubProducer> BufferHubProducer::Create(const std::shared_ptr<ProducerQueue>& queue) {
    if (queue->metadata_size() != sizeof(DvrNativeBufferMetadata)) {
        ALOGE("BufferHubProducer::Create producer's metadata size is different "
              "than the size of DvrNativeBufferMetadata");
@@ -49,14 +38,14 @@ sp<BufferHubProducer> BufferHubProducer::Create(const std::shared_ptr<dvr::Produ
}

/* static */
sp<BufferHubProducer> BufferHubProducer::Create(dvr::ProducerQueueParcelable parcelable) {
sp<BufferHubProducer> BufferHubProducer::Create(ProducerQueueParcelable parcelable) {
    if (!parcelable.IsValid()) {
        ALOGE("BufferHubProducer::Create: Invalid producer parcelable.");
        return nullptr;
    }

    sp<BufferHubProducer> producer = new BufferHubProducer;
    producer->queue_ = dvr::ProducerQueue::Import(parcelable.TakeChannelHandle());
    producer->queue_ = ProducerQueue::Import(parcelable.TakeChannelHandle());
    return producer;
}

@@ -102,9 +91,9 @@ status_t BufferHubProducer::setMaxDequeuedBufferCount(int max_dequeued_buffers)

    if (max_dequeued_buffers <= 0 ||
        max_dequeued_buffers >
                int(dvr::BufferHubQueue::kMaxQueueCapacity - kDefaultUndequeuedBuffers)) {
                int(BufferHubQueue::kMaxQueueCapacity - kDefaultUndequeuedBuffers)) {
        ALOGE("setMaxDequeuedBufferCount: %d out of range (0, %zu]", max_dequeued_buffers,
              dvr::BufferHubQueue::kMaxQueueCapacity);
              BufferHubQueue::kMaxQueueCapacity);
        return BAD_VALUE;
    }

@@ -153,7 +142,7 @@ status_t BufferHubProducer::dequeueBuffer(int* out_slot, sp<Fence>* out_fence, u
                                          uint32_t height, PixelFormat format, uint64_t usage,
                                          uint64_t* /*outBufferAge*/,
                                          FrameEventHistoryDelta* /* out_timestamps */) {
    ALOGV("dequeueBuffer: w=%u, h=%u, format=%d, usage=%" PRIu64, width, height, format, usage);
    ALOGW("dequeueBuffer: w=%u, h=%u, format=%d, usage=%" PRIu64, width, height, format, usage);

    status_t ret;
    std::unique_lock<std::mutex> lock(mutex_);
@@ -174,9 +163,9 @@ status_t BufferHubProducer::dequeueBuffer(int* out_slot, sp<Fence>* out_fence, u
    }

    size_t slot = 0;
    std::shared_ptr<dvr::BufferProducer> buffer_producer;
    std::shared_ptr<BufferProducer> buffer_producer;

    for (size_t retry = 0; retry < dvr::BufferHubQueue::kMaxQueueCapacity; retry++) {
    for (size_t retry = 0; retry < BufferHubQueue::kMaxQueueCapacity; retry++) {
        LocalHandle fence;
        auto buffer_status = queue_->Dequeue(dequeue_timeout_ms_, &slot, &fence);
        if (!buffer_status) return NO_MEMORY;
@@ -225,7 +214,7 @@ status_t BufferHubProducer::dequeueBuffer(int* out_slot, sp<Fence>* out_fence, u

    buffers_[slot].mBufferState.freeQueued();
    buffers_[slot].mBufferState.dequeue();
    ALOGV("dequeueBuffer: slot=%zu", slot);
    ALOGW("dequeueBuffer: slot=%zu", slot);

    // TODO(jwcai) Handle fence properly. |BufferHub| has full fence support, we
    // just need to exopose that through |BufferHubQueue| once we need fence.
@@ -590,7 +579,7 @@ status_t BufferHubProducer::setDequeueTimeout(nsecs_t timeout) {
    ALOGV(__FUNCTION__);

    std::unique_lock<std::mutex> lock(mutex_);
    dequeue_timeout_ms_ = int(timeout / (1000 * 1000));
    dequeue_timeout_ms_ = static_cast<int>(timeout / (1000 * 1000));
    return NO_ERROR;
}

@@ -620,7 +609,7 @@ status_t BufferHubProducer::getConsumerUsage(uint64_t* out_usage) const {
    return NO_ERROR;
}

status_t BufferHubProducer::TakeAsParcelable(dvr::ProducerQueueParcelable* out_parcelable) {
status_t BufferHubProducer::TakeAsParcelable(ProducerQueueParcelable* out_parcelable) {
    if (!out_parcelable || out_parcelable->IsValid()) return BAD_VALUE;

    if (connected_api_ != kNoConnectedApi) {
@@ -684,7 +673,7 @@ status_t BufferHubProducer::RemoveBuffer(size_t slot) {
}

status_t BufferHubProducer::FreeAllBuffers() {
    for (size_t slot = 0; slot < dvr::BufferHubQueue::kMaxQueueCapacity; slot++) {
    for (size_t slot = 0; slot < BufferHubQueue::kMaxQueueCapacity; slot++) {
        // Reset in memory objects related the the buffer.
        buffers_[slot].mGraphicBuffer = nullptr;
        buffers_[slot].mBufferState.reset();
@@ -707,4 +696,28 @@ status_t BufferHubProducer::FreeAllBuffers() {
    return NO_ERROR;
}

status_t BufferHubProducer::exportToParcel(Parcel* parcel) {
    status_t res = TakeAsParcelable(&pending_producer_parcelable_);
    if (res != NO_ERROR) return res;

    if (!pending_producer_parcelable_.IsValid()) {
        ALOGE("BufferHubProducer::exportToParcel: Invalid parcelable object.");
        return BAD_VALUE;
    }

    res = parcel->writeUint32(USE_BUFFER_HUB);
    if (res != NO_ERROR) {
        ALOGE("BufferHubProducer::exportToParcel: Cannot write magic, res=%d.", res);
        return res;
    }

    return pending_producer_parcelable_.writeToParcel(parcel);
}

IBinder* BufferHubProducer::onAsBinder() {
    ALOGE("BufferHubProducer::onAsBinder: BufferHubProducer should never be used as an Binder "
          "object.");
    return nullptr;
}

} // namespace android
+29 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0

#include <gui/BufferHubConsumer.h>
#include <gui/BufferHubProducer.h>
#include <gui/BufferQueue.h>
#include <gui/BufferQueueConsumer.h>
#include <gui/BufferQueueCore.h>
@@ -101,4 +103,31 @@ void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
    *outConsumer = consumer;
}

void BufferQueue::createBufferHubQueue(sp<IGraphicBufferProducer>* outProducer,
                                       sp<IGraphicBufferConsumer>* outConsumer) {
    LOG_ALWAYS_FATAL_IF(outProducer == NULL, "BufferQueue: outProducer must not be NULL");
    LOG_ALWAYS_FATAL_IF(outConsumer == NULL, "BufferQueue: outConsumer must not be NULL");

    sp<IGraphicBufferProducer> producer;
    sp<IGraphicBufferConsumer> consumer;

    dvr::ProducerQueueConfigBuilder configBuilder;
    std::shared_ptr<dvr::ProducerQueue> producerQueue =
            dvr::ProducerQueue::Create(configBuilder.SetMetadata<DvrNativeBufferMetadata>().Build(),
                                       dvr::UsagePolicy{});
    LOG_ALWAYS_FATAL_IF(producerQueue == NULL, "BufferQueue: failed to create ProducerQueue.");

    std::shared_ptr<dvr::ConsumerQueue> consumerQueue = producerQueue->CreateConsumerQueue();
    LOG_ALWAYS_FATAL_IF(consumerQueue == NULL, "BufferQueue: failed to create ConsumerQueue.");

    producer = BufferHubProducer::Create(producerQueue);
    consumer = BufferHubConsumer::Create(consumerQueue);

    LOG_ALWAYS_FATAL_IF(producer == NULL, "BufferQueue: failed to create BufferQueueProducer");
    LOG_ALWAYS_FATAL_IF(consumer == NULL, "BufferQueue: failed to create BufferQueueConsumer");

    *outProducer = producer;
    *outConsumer = consumer;
}

}; // namespace android
+70 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <binder/Parcel.h>
#include <binder/IInterface.h>

#include <gui/BufferHubProducer.h>
#include <gui/BufferQueueDefs.h>
#include <gui/IGraphicBufferProducer.h>
#include <gui/IProducerListener.h>
@@ -653,6 +654,75 @@ IMPLEMENT_HYBRID_META_INTERFACE(GraphicBufferProducer, HGraphicBufferProducer,

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

status_t IGraphicBufferProducer::exportToParcel(Parcel* parcel) {
    status_t res = OK;
    res = parcel->writeUint32(USE_BUFFER_QUEUE);
    if (res != NO_ERROR) {
        ALOGE("exportToParcel: Cannot write magic, res=%d.", res);
        return res;
    }

    return parcel->writeStrongBinder(IInterface::asBinder(this));
}

/* static */
status_t IGraphicBufferProducer::exportToParcel(const sp<IGraphicBufferProducer>& producer,
                                                Parcel* parcel) {
    if (parcel == nullptr) {
        ALOGE("exportToParcel: Invalid parcel object.");
        return BAD_VALUE;
    }

    if (producer == nullptr) {
        status_t res = OK;
        res = parcel->writeUint32(IGraphicBufferProducer::USE_BUFFER_QUEUE);
        if (res != NO_ERROR) return res;
        return parcel->writeStrongBinder(nullptr);
    } else {
        return producer->exportToParcel(parcel);
    }
}

/* static */
sp<IGraphicBufferProducer> IGraphicBufferProducer::createFromParcel(const Parcel* parcel) {
    uint32_t outMagic = 0;
    status_t res = NO_ERROR;

    res = parcel->readUint32(&outMagic);
    if (res != NO_ERROR) {
        ALOGE("createFromParcel: Failed to read magic, error=%d.", res);
        return nullptr;
    }

    switch (outMagic) {
        case USE_BUFFER_QUEUE: {
            sp<IBinder> binder;
            res = parcel->readNullableStrongBinder(&binder);
            if (res != NO_ERROR) {
                ALOGE("createFromParcel: Can't read strong binder.");
                return nullptr;
            }
            return interface_cast<IGraphicBufferProducer>(binder);
        }
        case USE_BUFFER_HUB: {
            ALOGE("createFromParcel: BufferHub not implemented.");
            dvr::ProducerQueueParcelable producerParcelable;
            res = producerParcelable.readFromParcel(parcel);
            if (res != NO_ERROR) {
                ALOGE("createFromParcel: Failed to read from parcel, error=%d", res);
                return nullptr;
            }
            return BufferHubProducer::Create(std::move(producerParcelable));
        }
        default: {
            ALOGE("createFromParcel: Unexpected mgaic: 0x%x.", outMagic);
            return nullptr;
        }
    }
}

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

status_t BnGraphicBufferProducer::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
Loading