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

Commit 9a6ddf78 authored by Jiwen 'Steve' Cai's avatar Jiwen 'Steve' Cai
Browse files

Update BufferHubProducer style to match libgui

Bug: 72051005
Bug: 70046255
Test: libgui-test, buffer_hub_queue_producer-test, dvr_api-test
Change-Id: I0e0d853a2ba633926e490ff22922c1dd90860257
parent 0f950843
Loading
Loading
Loading
Loading
+595 −615
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.
 */

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
@@ -20,11 +36,9 @@
namespace android {

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

/* static */
sp<BufferHubProducer> BufferHubProducer::Create(
    dvr::ProducerQueueParcelable parcelable) {
sp<BufferHubProducer> BufferHubProducer::Create(dvr::ProducerQueueParcelable parcelable) {
    if (!parcelable.IsValid()) {
        ALOGE("BufferHubProducer::Create: Invalid producer parcelable.");
        return nullptr;
@@ -58,12 +71,11 @@ status_t BufferHubProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
    }

    if (slot < 0 || slot >= max_buffer_count_) {
    ALOGE("requestBuffer: slot index %d out of range [0, %d)", slot,
          max_buffer_count_);
        ALOGE("requestBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_);
        return BAD_VALUE;
    } else if (!buffers_[slot].mBufferState.isDequeued()) {
    ALOGE("requestBuffer: slot %d is not owned by the producer (state = %s)",
          slot, buffers_[slot].mBufferState.string());
        ALOGE("requestBuffer: slot %d is not owned by the producer (state = %s)", slot,
              buffers_[slot].mBufferState.string());
        return BAD_VALUE;
    } else if (buffers_[slot].mGraphicBuffer != nullptr) {
        ALOGE("requestBuffer: slot %d is not empty.", slot);
@@ -83,18 +95,16 @@ status_t BufferHubProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
    return NO_ERROR;
}

status_t BufferHubProducer::setMaxDequeuedBufferCount(
    int max_dequeued_buffers) {
  ALOGV("setMaxDequeuedBufferCount: max_dequeued_buffers=%d",
        max_dequeued_buffers);
status_t BufferHubProducer::setMaxDequeuedBufferCount(int max_dequeued_buffers) {
    ALOGV("setMaxDequeuedBufferCount: max_dequeued_buffers=%d", max_dequeued_buffers);

    std::unique_lock<std::mutex> lock(mutex_);

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

@@ -107,8 +117,7 @@ status_t BufferHubProducer::setMaxDequeuedBufferCount(
        }
    }
    if (dequeued_count > max_dequeued_buffers) {
    ALOGE(
        "setMaxDequeuedBufferCount: the requested dequeued_buffers"
        ALOGE("setMaxDequeuedBufferCount: the requested dequeued_buffers"
              "count (%d) exceeds the current dequeued buffer count (%d)",
              max_dequeued_buffers, dequeued_count);
        return BAD_VALUE;
@@ -133,20 +142,18 @@ status_t BufferHubProducer::setAsyncMode(bool async) {
        //
        // See: IGraphicBufferProducer::setAsyncMode and
        // BufferQueueProducer::setAsyncMode for more about original implementation.
    ALOGW(
        "BufferHubProducer::setAsyncMode: BufferHubQueue should always be "
        ALOGW("BufferHubProducer::setAsyncMode: BufferHubQueue should always be "
              "asynchronous. This call makes no effact.");
        return NO_ERROR;
    }
    return NO_ERROR;
}

status_t BufferHubProducer::dequeueBuffer(
    int* out_slot, sp<Fence>* out_fence, uint32_t width, uint32_t height,
    PixelFormat format, uint64_t usage, uint64_t* /*outBufferAge*/,
status_t BufferHubProducer::dequeueBuffer(int* out_slot, sp<Fence>* out_fence, uint32_t width,
                                          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);
    ALOGV("dequeueBuffer: w=%u, h=%u, format=%d, usage=%" PRIu64, width, height, format, usage);

    status_t ret;
    std::unique_lock<std::mutex> lock(mutex_);
@@ -163,26 +170,21 @@ status_t BufferHubProducer::dequeueBuffer(
        // TODO(jwcai) To save memory, the really reasonable thing to do is to go
        // over existing slots and find first existing one to dequeue.
        ret = AllocateBuffer(width, height, kLayerCount, format, usage);
    if (ret < 0)
      return ret;
        if (ret < 0) return ret;
    }

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

  for (size_t retry = 0; retry < dvr::BufferHubQueue::kMaxQueueCapacity;
       retry++) {
    for (size_t retry = 0; retry < dvr::BufferHubQueue::kMaxQueueCapacity; retry++) {
        LocalHandle fence;
        auto buffer_status = queue_->Dequeue(dequeue_timeout_ms_, &slot, &fence);
    if (!buffer_status)
      return NO_MEMORY;
        if (!buffer_status) return NO_MEMORY;

        buffer_producer = buffer_status.take();
    if (!buffer_producer)
      return NO_MEMORY;
        if (!buffer_producer) return NO_MEMORY;

    if (width == buffer_producer->width() &&
        height == buffer_producer->height() &&
        if (width == buffer_producer->width() && height == buffer_producer->height() &&
            uint32_t(format) == buffer_producer->format()) {
            // The producer queue returns a buffer producer matches the request.
            break;
@@ -190,12 +192,11 @@ status_t BufferHubProducer::dequeueBuffer(

        // Needs reallocation.
        // TODO(jwcai) Consider use VLOG instead if we find this log is not useful.
    ALOGI(
        "dequeueBuffer: requested buffer (w=%u, h=%u, format=%u) is different "
        ALOGI("dequeueBuffer: requested buffer (w=%u, h=%u, format=%u) is different "
              "from the buffer returned at slot: %zu (w=%u, h=%u, format=%u). Need "
              "re-allocattion.",
        width, height, format, slot, buffer_producer->width(),
        buffer_producer->height(), buffer_producer->format());
              width, height, format, slot, buffer_producer->width(), buffer_producer->height(),
              buffer_producer->format());
        // Mark the slot as reallocating, so that later we can set
        // BUFFER_NEEDS_REALLOCATION when the buffer actually get dequeued.
        buffers_[slot].mIsReallocating = true;
@@ -209,8 +210,7 @@ status_t BufferHubProducer::dequeueBuffer(
        // from |queue_->Dequeue| may not be the new buffer we just reallocated.
        // Retry up to BufferHubQueue::kMaxQueueCapacity times.
        ret = AllocateBuffer(width, height, kLayerCount, format, usage);
    if (ret < 0)
      return ret;
        if (ret < 0) return ret;
    }

    // With the BufferHub backed solution. Buffer slot returned from
@@ -218,8 +218,7 @@ status_t BufferHubProducer::dequeueBuffer(
    // It's either in free state (if the buffer has never been used before) or
    // in queued state (if the buffer has been dequeued and queued back to
    // BufferHubQueue).
  LOG_ALWAYS_FATAL_IF(
      (!buffers_[slot].mBufferState.isFree() &&
    LOG_ALWAYS_FATAL_IF((!buffers_[slot].mBufferState.isFree() &&
                         !buffers_[slot].mBufferState.isQueued()),
                        "dequeueBuffer: slot %zu is not free or queued, actual state: %s.", slot,
                        buffers_[slot].mBufferState.string());
@@ -247,14 +246,14 @@ status_t BufferHubProducer::detachBuffer(int /* slot */) {
    return INVALID_OPERATION;
}

status_t BufferHubProducer::detachNextBuffer(
    sp<GraphicBuffer>* /* out_buffer */, sp<Fence>* /* out_fence */) {
status_t BufferHubProducer::detachNextBuffer(sp<GraphicBuffer>* /* out_buffer */,
                                             sp<Fence>* /* out_fence */) {
    ALOGE("BufferHubProducer::detachNextBuffer not implemented.");
    return INVALID_OPERATION;
}

status_t BufferHubProducer::attachBuffer(
    int* /* out_slot */, const sp<GraphicBuffer>& /* buffer */) {
status_t BufferHubProducer::attachBuffer(int* /* out_slot */,
                                         const sp<GraphicBuffer>& /* buffer */) {
    // With this BufferHub backed implementation, we assume (for now) all buffers
    // are allocated and owned by the BufferHub. Thus the attempt of transfering
    // ownership of a buffer to the buffer queue is intentionally unsupported.
@@ -278,8 +277,8 @@ status_t BufferHubProducer::queueBuffer(int slot, const QueueBufferInput& input,
    uint32_t transform;
    sp<Fence> fence;

  input.deflate(&timestamp, &is_auto_timestamp, &dataspace, &crop,
                &scaling_mode, &transform, &fence);
    input.deflate(&timestamp, &is_auto_timestamp, &dataspace, &crop, &scaling_mode, &transform,
                  &fence);

    // Check input scaling mode is valid.
    switch (scaling_mode) {
@@ -307,20 +306,16 @@ status_t BufferHubProducer::queueBuffer(int slot, const QueueBufferInput& input,
    }

    if (slot < 0 || slot >= max_buffer_count_) {
    ALOGE("queueBuffer: slot index %d out of range [0, %d)", slot,
          max_buffer_count_);
        ALOGE("queueBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_);
        return BAD_VALUE;
    } else if (!buffers_[slot].mBufferState.isDequeued()) {
    ALOGE("queueBuffer: slot %d is not owned by the producer (state = %s)",
          slot, buffers_[slot].mBufferState.string());
        ALOGE("queueBuffer: slot %d is not owned by the producer (state = %s)", slot,
              buffers_[slot].mBufferState.string());
        return BAD_VALUE;
  } else if ((!buffers_[slot].mRequestBufferCalled ||
              buffers_[slot].mGraphicBuffer == nullptr)) {
    ALOGE(
        "queueBuffer: slot %d is not requested (mRequestBufferCalled=%d, "
    } else if ((!buffers_[slot].mRequestBufferCalled || buffers_[slot].mGraphicBuffer == nullptr)) {
        ALOGE("queueBuffer: slot %d is not requested (mRequestBufferCalled=%d, "
              "mGraphicBuffer=%p)",
        slot, buffers_[slot].mRequestBufferCalled,
        buffers_[slot].mGraphicBuffer.get());
              slot, buffers_[slot].mRequestBufferCalled, buffers_[slot].mGraphicBuffer.get());
        return BAD_VALUE;
    }

@@ -380,12 +375,11 @@ status_t BufferHubProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
    }

    if (slot < 0 || slot >= max_buffer_count_) {
    ALOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
          max_buffer_count_);
        ALOGE("cancelBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_);
        return BAD_VALUE;
    } else if (!buffers_[slot].mBufferState.isDequeued()) {
    ALOGE("cancelBuffer: slot %d is not owned by the producer (state = %s)",
          slot, buffers_[slot].mBufferState.string());
        ALOGE("cancelBuffer: slot %d is not owned by the producer (state = %s)", slot,
              buffers_[slot].mBufferState.string());
        return BAD_VALUE;
    } else if (fence == nullptr) {
        ALOGE("cancelBuffer: fence is NULL");
@@ -466,8 +460,7 @@ status_t BufferHubProducer::query(int what, int* out_value) {
    return NO_ERROR;
}

status_t BufferHubProducer::connect(const sp<IProducerListener>& /* listener */,
                                    int api,
status_t BufferHubProducer::connect(const sp<IProducerListener>& /* listener */, int api,
                                    bool /* producer_controlled_by_app */,
                                    QueueBufferOutput* output) {
    // Consumer interaction are actually handled by buffer hub, and we need
@@ -486,8 +479,7 @@ status_t BufferHubProducer::connect(const sp<IProducerListener>& /* listener */,
    }

    if (!queue_->is_connected()) {
    ALOGE(
        "BufferHubProducer::connect: This BufferHubProducer is not "
        ALOGE("BufferHubProducer::connect: This BufferHubProducer is not "
              "connected to bufferhud. Has it been taken out as a parcelable?");
        return BAD_VALUE;
    }
@@ -546,10 +538,8 @@ status_t BufferHubProducer::setSidebandStream(const sp<NativeHandle>& stream) {
    return NO_ERROR;
}

void BufferHubProducer::allocateBuffers(uint32_t /* width */,
                                        uint32_t /* height */,
                                        PixelFormat /* format */,
                                        uint64_t /* usage */) {
void BufferHubProducer::allocateBuffers(uint32_t /* width */, uint32_t /* height */,
                                        PixelFormat /* format */, uint64_t /* usage */) {
    // TODO(jwcai) |allocateBuffers| aims to preallocate up to the maximum number
    // of buffers permitted by the current BufferQueue configuration (aka
    // |max_buffer_count_|).
@@ -604,15 +594,14 @@ status_t BufferHubProducer::setDequeueTimeout(nsecs_t timeout) {
    return NO_ERROR;
}

status_t BufferHubProducer::getLastQueuedBuffer(
    sp<GraphicBuffer>* /* out_buffer */, sp<Fence>* /* out_fence */,
status_t BufferHubProducer::getLastQueuedBuffer(sp<GraphicBuffer>* /* out_buffer */,
                                                sp<Fence>* /* out_fence */,
                                                float /*out_transform_matrix*/[16]) {
    ALOGE("BufferHubProducer::getLastQueuedBuffer not implemented.");
    return INVALID_OPERATION;
}

void BufferHubProducer::getFrameTimestamps(
    FrameEventHistoryDelta* /*outDelta*/) {
void BufferHubProducer::getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) {
    ALOGE("BufferHubProducer::getFrameTimestamps not implemented.");
}

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

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

    if (connected_api_ != kNoConnectedApi) {
    ALOGE(
        "BufferHubProducer::TakeAsParcelable: BufferHubProducer has "
        ALOGE("BufferHubProducer::TakeAsParcelable: BufferHubProducer has "
              "connected client. Must disconnect first.");
        return BAD_VALUE;
    }

    if (!queue_->is_connected()) {
    ALOGE(
        "BufferHubProducer::TakeAsParcelable: This BufferHubProducer "
        ALOGE("BufferHubProducer::TakeAsParcelable: This BufferHubProducer "
              "is not connected to bufferhud. Has it been taken out as a "
              "parcelable?");
        return BAD_VALUE;
@@ -653,8 +638,7 @@ status_t BufferHubProducer::TakeAsParcelable(

    auto status = queue_->TakeAsParcelable();
    if (!status) {
    ALOGE(
        "BufferHubProducer::TakeAsParcelable: Failed to take out "
        ALOGE("BufferHubProducer::TakeAsParcelable: Failed to take out "
              "ProducuerQueueParcelable from the producer queue, error: %s.",
              status.GetErrorMessage().c_str());
        return BAD_VALUE;
@@ -664,11 +648,9 @@ status_t BufferHubProducer::TakeAsParcelable(
    return NO_ERROR;
}

status_t BufferHubProducer::AllocateBuffer(uint32_t width, uint32_t height,
                                           uint32_t layer_count,
status_t BufferHubProducer::AllocateBuffer(uint32_t width, uint32_t height, uint32_t layer_count,
                                           PixelFormat format, uint64_t usage) {
  auto status = queue_->AllocateBuffer(width, height, layer_count,
                                       uint32_t(format), usage);
    auto status = queue_->AllocateBuffer(width, height, layer_count, uint32_t(format), usage);
    if (!status) {
        ALOGE("BufferHubProducer::AllocateBuffer: Failed to allocate buffer: %s",
              status.GetErrorMessage().c_str());
@@ -678,8 +660,8 @@ status_t BufferHubProducer::AllocateBuffer(uint32_t width, uint32_t height,
    size_t slot = status.get();
    auto buffer_producer = queue_->GetBuffer(slot);

  LOG_ALWAYS_FATAL_IF(buffer_producer == nullptr,
                      "Failed to get buffer producer at slot: %zu", slot);
    LOG_ALWAYS_FATAL_IF(buffer_producer == nullptr, "Failed to get buffer producer at slot: %zu",
                        slot);

    buffers_[slot].mBufferProducer = buffer_producer;

@@ -713,15 +695,13 @@ status_t BufferHubProducer::FreeAllBuffers() {

    auto status = queue_->FreeAllBuffers();
    if (!status) {
    ALOGE(
        "BufferHubProducer::FreeAllBuffers: Failed to free all buffers on "
        ALOGE("BufferHubProducer::FreeAllBuffers: Failed to free all buffers on "
              "the queue: %s",
              status.GetErrorMessage().c_str());
    }

    if (queue_->capacity() != 0 || queue_->count() != 0) {
    LOG_ALWAYS_FATAL(
        "BufferHubProducer::FreeAllBuffers: Not all buffers are freed.");
        LOG_ALWAYS_FATAL("BufferHubProducer::FreeAllBuffers: Not all buffers are freed.");
    }

    return NO_ERROR;
+180 −171
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.
 */

#ifndef ANDROID_GUI_BUFFERHUBPRODUCER_H_
#define ANDROID_GUI_BUFFERHUBPRODUCER_H_

@@ -20,8 +36,7 @@ class BufferHubProducer : public BnGraphicBufferProducer {

    // Creates a BufferHubProducer instance by importing an existing prodcuer
    // queue.
  static sp<BufferHubProducer> Create(
      const std::shared_ptr<dvr::ProducerQueue>& producer);
    static sp<BufferHubProducer> Create(const std::shared_ptr<dvr::ProducerQueue>& producer);

    // Creates a BufferHubProducer instance by importing an existing prodcuer
    // parcelable. Note that this call takes the ownership of the parcelable
@@ -44,21 +59,18 @@ class BufferHubProducer : public BnGraphicBufferProducer {
    status_t setAsyncMode(bool async) override;

    // See |IGraphicBufferProducer::dequeueBuffer|
  status_t dequeueBuffer(int* out_slot, sp<Fence>* out_fence, uint32_t width,
                         uint32_t height, PixelFormat format, uint64_t usage,
                         uint64_t* outBufferAge,
    status_t dequeueBuffer(int* out_slot, sp<Fence>* out_fence, uint32_t width, uint32_t height,
                           PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
                           FrameEventHistoryDelta* outTimestamps) override;

    // See |IGraphicBufferProducer::detachBuffer|
    status_t detachBuffer(int slot) override;

    // See |IGraphicBufferProducer::detachNextBuffer|
  status_t detachNextBuffer(sp<GraphicBuffer>* out_buffer,
                            sp<Fence>* out_fence) override;
    status_t detachNextBuffer(sp<GraphicBuffer>* out_buffer, sp<Fence>* out_fence) override;

    // See |IGraphicBufferProducer::attachBuffer|
  status_t attachBuffer(int* out_slot,
                        const sp<GraphicBuffer>& buffer) override;
    status_t attachBuffer(int* out_slot, const sp<GraphicBuffer>& buffer) override;

    // See |IGraphicBufferProducer::queueBuffer|
    status_t queueBuffer(int slot, const QueueBufferInput& input,
@@ -72,12 +84,10 @@ class BufferHubProducer : public BnGraphicBufferProducer {

    // See |IGraphicBufferProducer::connect|
    status_t connect(const sp<IProducerListener>& listener, int api,
                   bool producer_controlled_by_app,
                   QueueBufferOutput* output) override;
                     bool producer_controlled_by_app, QueueBufferOutput* output) override;

    // See |IGraphicBufferProducer::disconnect|
  status_t disconnect(int api,
                      DisconnectMode mode = DisconnectMode::Api) override;
    status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api) override;

    // See |IGraphicBufferProducer::setSidebandStream|
    status_t setSidebandStream(const sp<NativeHandle>& stream) override;
@@ -105,8 +115,7 @@ class BufferHubProducer : public BnGraphicBufferProducer {
    status_t setDequeueTimeout(nsecs_t timeout) override;

    // See |IGraphicBufferProducer::getLastQueuedBuffer|
  status_t getLastQueuedBuffer(sp<GraphicBuffer>* out_buffer,
                               sp<Fence>* out_fence,
    status_t getLastQueuedBuffer(sp<GraphicBuffer>* out_buffer, sp<Fence>* out_fence,
                                 float out_transform_matrix[16]) override;

    // See |IGraphicBufferProducer::getFrameTimestamps|