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

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

Merge "Return error when error occurs in buffer allocation."

parents 4d70c4ed a6e33a1a
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -440,6 +440,10 @@ ProducerQueue::ProducerQueue(const ProducerQueueConfig& config,
Status<std::vector<size_t>> ProducerQueue::AllocateBuffers(
    uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format,
    uint64_t usage, size_t buffer_count) {
  if (buffer_count == 0) {
    return {std::vector<size_t>()};
  }

  if (capacity() + buffer_count > kMaxQueueCapacity) {
    ALOGE(
        "ProducerQueue::AllocateBuffers: queue is at capacity: %zu, cannot "
@@ -481,10 +485,13 @@ Status<std::vector<size_t>> ProducerQueue::AllocateBuffers(
    }
  }

  if (buffer_slots.size() == 0) {
    // Error out if no buffer is allocated and improted.
    ALOGE_IF(TRACE, "ProducerQueue::AllocateBuffers: no buffer allocated.");
    ErrorStatus(ENOMEM);
  if (buffer_slots.size() != buffer_count) {
    // Error out if the count of imported buffer(s) is not correct.
    ALOGE(
        "ProducerQueue::AllocateBuffers: requested to import %zu "
        "buffers, but actually imported %zu buffers.",
        buffer_count, buffer_slots.size());
    return ErrorStatus(ENOMEM);
  }

  return {std::move(buffer_slots)};
@@ -503,11 +510,6 @@ Status<size_t> ProducerQueue::AllocateBuffer(uint32_t width, uint32_t height,
    return status.error_status();
  }

  if (status.get().size() == 0) {
    ALOGE_IF(TRACE, "ProducerQueue::AllocateBuffer: no buffer allocated.");
    ErrorStatus(ENOMEM);
  }

  return {status.get()[0]};
}

+24 −0
Original line number Diff line number Diff line
@@ -562,6 +562,30 @@ TEST_F(BufferHubQueueTest, TestAllocateBuffer) {
  ASSERT_EQ(cs2, ps2);
}

TEST_F(BufferHubQueueTest, TestAllocateTwoBuffers) {
  ASSERT_TRUE(CreateQueues(config_builder_.Build(), UsagePolicy{}));
  ASSERT_EQ(producer_queue_->capacity(), 0);
  auto status = producer_queue_->AllocateBuffers(
      kBufferWidth, kBufferHeight, kBufferLayerCount, kBufferFormat,
      kBufferUsage, /*buffer_count=*/2);
  ASSERT_TRUE(status.ok());
  std::vector<size_t> buffer_slots = status.take();
  ASSERT_EQ(buffer_slots.size(), 2);
  ASSERT_EQ(producer_queue_->capacity(), 2);
}

TEST_F(BufferHubQueueTest, TestAllocateZeroBuffers) {
  ASSERT_TRUE(CreateQueues(config_builder_.Build(), UsagePolicy{}));
  ASSERT_EQ(producer_queue_->capacity(), 0);
  auto status = producer_queue_->AllocateBuffers(
      kBufferWidth, kBufferHeight, kBufferLayerCount, kBufferFormat,
      kBufferUsage, /*buffer_count=*/0);
  ASSERT_TRUE(status.ok());
  std::vector<size_t> buffer_slots = status.take();
  ASSERT_EQ(buffer_slots.size(), 0);
  ASSERT_EQ(producer_queue_->capacity(), 0);
}

TEST_F(BufferHubQueueTest, TestUsageSetMask) {
  const uint32_t set_mask = GRALLOC_USAGE_SW_WRITE_OFTEN;
  ASSERT_TRUE(