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

Commit 63dd7c3b authored by Tianyu Jiang's avatar Tianyu Jiang
Browse files

Replace kProducerStateBit with kFirstClientBitMask.

kProducerStateBit covers the MSB of the buffer/fence state.
kFirstClientBitMask covers the LSB of the buffer/fence state.
This change replaces the kProducerStateBit with kFirstClientBitMask, and
update the kConsumerStateBit to be the inverse of kFirstClientBitMask.

Test: AHardwareBufferTest BufferHubBuffer_test BufferHubMetadata_test
buffer_hub-test buffer_hub_binder_service-test buffer_hub_queue-test
buffer_hub_queue_producer-test buffer_node-test dvr_api-test
dvr_buffer_queue-test dvr_display-test libgui_test libdvrcommon_test
pdx_tests GraphicBuffer_test
Bug: 118718713
Change-Id: I647f36ee3fb2eb5dc996b781ed0ff71f7f72c112
parent 88424206
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ const size_t kUserMetadataSize = 0;
} // namespace

using dvr::BufferHubDefs::IsBufferGained;
using dvr::BufferHubDefs::kFirstClientBitMask;
using dvr::BufferHubDefs::kMetadataHeaderSize;
using dvr::BufferHubDefs::kProducerStateBit;
using frameworks::bufferhub::V1_0::IBufferHub;
using hardware::hidl_handle;
using hidl::base::V1_0::IBase;
+8 −10
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ using android::dvr::BufferHubDefs::IsBufferGained;
using android::dvr::BufferHubDefs::IsBufferPosted;
using android::dvr::BufferHubDefs::IsBufferReleased;
using android::dvr::BufferHubDefs::kConsumerStateMask;
using android::dvr::BufferHubDefs::kFirstClientBitMask;
using android::dvr::BufferHubDefs::kMetadataHeaderSize;
using android::dvr::BufferHubDefs::kProducerStateBit;
using android::pdx::LocalChannelHandle;
using android::pdx::LocalHandle;
using android::pdx::Status;
@@ -62,14 +62,14 @@ TEST_F(LibBufferHubTest, TestBasicUsage) {
  ASSERT_TRUE(c2.get() != nullptr);

  // Producer state mask is unique, i.e. 1.
  EXPECT_EQ(p->client_state_mask(), kProducerStateBit);
  EXPECT_EQ(p->client_state_mask(), kFirstClientBitMask);
  // Consumer state mask cannot have producer bit on.
  EXPECT_EQ(c->client_state_mask() & kProducerStateBit, 0U);
  EXPECT_EQ(c->client_state_mask() & kFirstClientBitMask, 0U);
  // Consumer state mask must be a single, i.e. power of 2.
  EXPECT_NE(c->client_state_mask(), 0U);
  EXPECT_EQ(c->client_state_mask() & (c->client_state_mask() - 1), 0U);
  // Consumer state mask cannot have producer bit on.
  EXPECT_EQ(c2->client_state_mask() & kProducerStateBit, 0U);
  EXPECT_EQ(c2->client_state_mask() & kFirstClientBitMask, 0U);
  // Consumer state mask must be a single, i.e. power of 2.
  EXPECT_NE(c2->client_state_mask(), 0U);
  EXPECT_EQ(c2->client_state_mask() & (c2->client_state_mask() - 1), 0U);
@@ -190,7 +190,7 @@ TEST_F(LibBufferHubTest, TestStateMask) {
    EXPECT_EQ(client_state_masks & cs[i]->client_state_mask(), 0U);
    client_state_masks |= cs[i]->client_state_mask();
  }
  EXPECT_EQ(client_state_masks, kProducerStateBit | kConsumerStateMask);
  EXPECT_EQ(client_state_masks, kFirstClientBitMask | kConsumerStateMask);

  // The 64th creation will fail with out-of-memory error.
  auto state = p->CreateConsumer();
@@ -205,7 +205,7 @@ TEST_F(LibBufferHubTest, TestStateMask) {
    // The released state mask will be reused.
    EXPECT_EQ(client_state_masks & cs[i]->client_state_mask(), 0U);
    client_state_masks |= cs[i]->client_state_mask();
    EXPECT_EQ(client_state_masks, kProducerStateBit | kConsumerStateMask);
    EXPECT_EQ(client_state_masks, kFirstClientBitMask | kConsumerStateMask);
  }
}

@@ -891,6 +891,7 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) {
  int b1_id = b1->id();
  EXPECT_TRUE(b1->IsValid());
  EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize);
  EXPECT_NE(b1->client_state_mask(), 0ULL);

  auto status_or_handle = b1->Duplicate();
  EXPECT_TRUE(status_or_handle);
@@ -908,6 +909,7 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) {
  ASSERT_TRUE(b2 != nullptr);
  EXPECT_TRUE(b2->IsValid());
  EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize);
  EXPECT_NE(b2->client_state_mask(), 0ULL);

  int b2_id = b2->id();

@@ -916,10 +918,6 @@ TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) {
  EXPECT_EQ(b1_id, b2_id);
  // We use client_state_mask() to tell those two instances apart.
  EXPECT_NE(b1->client_state_mask(), b2->client_state_mask());
  EXPECT_NE(b1->client_state_mask(), 0ULL);
  EXPECT_NE(b2->client_state_mask(), 0ULL);
  EXPECT_NE(b1->client_state_mask(), kProducerStateBit);
  EXPECT_NE(b2->client_state_mask(), kProducerStateBit);

  // Both buffer instances should be in gained state.
  EXPECT_TRUE(IsBufferGained(b1->buffer_state()));
+2 −1
Original line number Diff line number Diff line
@@ -59,7 +59,8 @@ int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta,

  uint64_t fence_state = fence_state_->load(std::memory_order_acquire);
  // If there is an acquire fence from producer, we need to return it.
  if (fence_state & BufferHubDefs::kProducerStateBit) {
  // The producer state bit mask is kFirstClientBitMask for now.
  if (fence_state & BufferHubDefs::kFirstClientBitMask) {
    *out_fence = shared_acquire_fence_.Duplicate();
  }

+12 −12
Original line number Diff line number Diff line
@@ -26,14 +26,14 @@ static constexpr uint32_t kMetadataUsage =
// MSB           LSB
//  |             |
//  v             v
// [P|C62|...|C1|C0]
// Gain'ed state:     [0|..|0|0] -> Exclusively Writable.
// Post'ed state:     [1|..|0|0]
// Acquired'ed state: [1|..|X|X] -> At least one bit is set in lower 63 bits
// Released'ed state: [0|..|X|X] -> At least one bit is set in lower 63 bits
// [C62|...|C1|C0|P]
// Gain'ed state:     [..|0|0|0] -> Exclusively Writable.
// Post'ed state:     [..|0|0|1]
// Acquired'ed state: [..|X|X|1] -> At least one bit is set in higher 63 bits
// Released'ed state: [..|X|X|0] -> At least one bit is set in higher 63 bits
static constexpr int kMaxNumberOfClients = 64;
static constexpr uint64_t kProducerStateBit = 1ULL << 63;
static constexpr uint64_t kConsumerStateMask = (1ULL << 63) - 1;
static constexpr uint64_t kFirstClientBitMask = 1ULL;
static constexpr uint64_t kConsumerStateMask = ~kFirstClientBitMask;

static inline void ModifyBufferState(std::atomic<uint64_t>* buffer_state,
                                     uint64_t clear_mask, uint64_t set_mask) {
@@ -49,15 +49,15 @@ static inline bool IsBufferGained(uint64_t state) { return state == 0; }

static inline bool IsBufferPosted(uint64_t state,
                                  uint64_t consumer_bit = kConsumerStateMask) {
  return (state & kProducerStateBit) && !(state & consumer_bit);
  return (state & kFirstClientBitMask) && !(state & consumer_bit);
}

static inline bool IsBufferAcquired(uint64_t state) {
  return (state & kProducerStateBit) && (state & kConsumerStateMask);
  return (state & kFirstClientBitMask) && (state & kConsumerStateMask);
}

static inline bool IsBufferReleased(uint64_t state) {
  return !(state & kProducerStateBit) && (state & kConsumerStateMask);
  return !(state & kFirstClientBitMask) && (state & kConsumerStateMask);
}

static inline uint64_t FindNextAvailableClientStateMask(uint64_t bits) {
@@ -65,7 +65,7 @@ static inline uint64_t FindNextAvailableClientStateMask(uint64_t bits) {
}

static inline uint64_t FindFirstClearedBit() {
  return FindNextAvailableClientStateMask(kProducerStateBit);
  return FindNextAvailableClientStateMask(kFirstClientBitMask);
}

struct __attribute__((packed, aligned(8))) MetadataHeader {
@@ -123,7 +123,7 @@ class BufferTraits {

  // State mask of the buffer client. Each BufferHubBuffer client backed by the
  // same buffer channel has uniqued state bit among its siblings. For a
  // producer buffer the bit must be kProducerStateBit; for a consumer the bit
  // producer buffer the bit must be kFirstClientBitMask; for a consumer the bit
  // must be one of the kConsumerStateMask.
  uint64_t client_state_mask() const { return client_state_mask_; }
  uint64_t metadata_size() const { return metadata_size_; }
+2 −1
Original line number Diff line number Diff line
@@ -102,8 +102,9 @@ int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta,
    return error;

  // Set the producer bit atomically to transit into posted state.
  // The producer state bit mask is kFirstClientBitMask for now.
  BufferHubDefs::ModifyBufferState(buffer_state_, 0ULL,
                                   BufferHubDefs::kProducerStateBit);
                                   BufferHubDefs::kFirstClientBitMask);
  return 0;
}

Loading