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

Commit 1543d888 authored by Tianyu Jiang's avatar Tianyu Jiang
Browse files

Fix non camelCase member variables

Android Framework C++ Code Style Guidelines says that member variables
should be camelCase: http://go/droidcppstyle.

Test: m, mma in frameworks/native
Bug: 68273829
Change-Id: I859b916244f1e2ca2ba35c0a6e816b6250cd71ce
parent 13fb8861
Loading
Loading
Loading
Loading
+40 −40
Original line number Diff line number Diff line
@@ -210,15 +210,15 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) {

    // Populate shortcuts to the atomics in metadata.
    auto metadata_header = mMetadata.metadata_header();
    buffer_state_ = &metadata_header->buffer_state;
    fence_state_ = &metadata_header->fence_state;
    active_clients_bit_mask_ = &metadata_header->active_clients_bit_mask;
    mBufferState = &metadata_header->buffer_state;
    mFenceState = &metadata_header->fence_state;
    mActiveClientsBitMask = &metadata_header->active_clients_bit_mask;
    // The C++ standard recommends (but does not require) that lock-free atomic operations are
    // also address-free, that is, suitable for communication between processes using shared
    // memory.
    LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) ||
                                !std::atomic_is_lock_free(fence_state_) ||
                                !std::atomic_is_lock_free(active_clients_bit_mask_),
    LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) ||
                                !std::atomic_is_lock_free(mFenceState) ||
                                !std::atomic_is_lock_free(mActiveClientsBitMask),
                        "Atomic variables in ashmen are not lock free.");

    // Import the buffer: We only need to hold on the native_handle_t here so that
@@ -231,27 +231,27 @@ int BufferHubBuffer::initWithBufferTraits(const BufferTraits& bufferTraits) {

    // TODO(b/112012161) Set up shared fences.
    ALOGD("%s: id=%d, buffer_state=%" PRIx32 ".", __FUNCTION__, mId,
          buffer_state_->load(std::memory_order_acquire));
          mBufferState->load(std::memory_order_acquire));
    return 0;
}

int BufferHubBuffer::Gain() {
    uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
    if (IsClientGained(current_buffer_state, mClientStateMask)) {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (IsClientGained(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already gained by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    do {
        if (AnyClientGained(current_buffer_state & (~mClientStateMask)) ||
            AnyClientAcquired(current_buffer_state)) {
        if (AnyClientGained(currentBufferState & (~mClientStateMask)) ||
            AnyClientAcquired(currentBufferState)) {
            ALOGE("%s: Buffer is in use, id=%d mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, current_buffer_state);
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Change the buffer state to gained state, whose value happens to be the same as
        // mClientStateMask.
    } while (!buffer_state_->compare_exchange_weak(current_buffer_state, mClientStateMask,
    } while (!mBufferState->compare_exchange_weak(currentBufferState, mClientStateMask,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence.
@@ -259,18 +259,18 @@ int BufferHubBuffer::Gain() {
}

int BufferHubBuffer::Post() {
    uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
    uint32_t updated_buffer_state = (~mClientStateMask) & BufferHubDefs::kHighBitsMask;
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    uint32_t updatedBufferState = (~mClientStateMask) & BufferHubDefs::kHighBitsMask;
    do {
        if (!IsClientGained(current_buffer_state, mClientStateMask)) {
        if (!IsClientGained(currentBufferState, mClientStateMask)) {
            ALOGE("%s: Cannot post a buffer that is not gained by this client. buffer_id=%d "
                  "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, current_buffer_state);
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Set the producer client buffer state to released, other clients' buffer state to posted.
        // Post to all existing and non-existing clients.
    } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence if needed.
@@ -278,23 +278,23 @@ int BufferHubBuffer::Post() {
}

int BufferHubBuffer::Acquire() {
    uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
    if (IsClientAcquired(current_buffer_state, mClientStateMask)) {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (IsClientAcquired(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already acquired by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    uint32_t updated_buffer_state = 0U;
    uint32_t updatedBufferState = 0U;
    do {
        if (!IsClientPosted(current_buffer_state, mClientStateMask)) {
        if (!IsClientPosted(currentBufferState, mClientStateMask)) {
            ALOGE("%s: Cannot acquire a buffer that is not in posted state. buffer_id=%d "
                  "mClientStateMask=%" PRIx32 " state=%" PRIx32 ".",
                  __FUNCTION__, mId, mClientStateMask, current_buffer_state);
                  __FUNCTION__, mId, mClientStateMask, currentBufferState);
            return -EBUSY;
        }
        // Change the buffer state for this consumer from posted to acquired.
        updated_buffer_state = current_buffer_state ^ mClientStateMask;
    } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
        updatedBufferState = currentBufferState ^ mClientStateMask;
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence.
@@ -302,16 +302,16 @@ int BufferHubBuffer::Acquire() {
}

int BufferHubBuffer::Release() {
    uint32_t current_buffer_state = buffer_state_->load(std::memory_order_acquire);
    if (IsClientReleased(current_buffer_state, mClientStateMask)) {
    uint32_t currentBufferState = mBufferState->load(std::memory_order_acquire);
    if (IsClientReleased(currentBufferState, mClientStateMask)) {
        ALOGV("%s: Buffer is already released by this client %" PRIx32 ".", __FUNCTION__,
              mClientStateMask);
        return 0;
    }
    uint32_t updated_buffer_state = 0U;
    uint32_t updatedBufferState = 0U;
    do {
        updated_buffer_state = current_buffer_state & (~mClientStateMask);
    } while (!buffer_state_->compare_exchange_weak(current_buffer_state, updated_buffer_state,
        updatedBufferState = currentBufferState & (~mClientStateMask);
    } while (!mBufferState->compare_exchange_weak(currentBufferState, updatedBufferState,
                                                  std::memory_order_acq_rel,
                                                  std::memory_order_acquire));
    // TODO(b/119837586): Update fence state and return GPU fence if needed.
@@ -319,8 +319,8 @@ int BufferHubBuffer::Release() {
}

bool BufferHubBuffer::IsReleased() const {
    return (buffer_state_->load(std::memory_order_acquire) &
            active_clients_bit_mask_->load(std::memory_order_acquire)) == 0;
    return (mBufferState->load(std::memory_order_acquire) &
            mActiveClientsBitMask->load(std::memory_order_acquire)) == 0;
}

bool BufferHubBuffer::IsValid() const {
+4 −4
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public:
    const BufferHubEventFd& eventFd() const { return mEventFd; }

    // Returns the current value of MetadataHeader::buffer_state.
    uint32_t buffer_state() const { return buffer_state_->load(std::memory_order_acquire); }
    uint32_t buffer_state() const { return mBufferState->load(std::memory_order_acquire); }

    // A state mask which is unique to a buffer hub client among all its siblings sharing the same
    // concrete graphic buffer.
@@ -134,9 +134,9 @@ private:
    // bufferhubd daemon and all buffer clients.
    BufferHubMetadata mMetadata;
    // Shortcuts to the atomics inside the header of mMetadata.
    std::atomic<uint32_t>* buffer_state_ = nullptr;
    std::atomic<uint32_t>* fence_state_ = nullptr;
    std::atomic<uint32_t>* active_clients_bit_mask_ = nullptr;
    std::atomic<uint32_t>* mBufferState = nullptr;
    std::atomic<uint32_t>* mFenceState = nullptr;
    std::atomic<uint32_t>* mActiveClientsBitMask = nullptr;

    // HwBinder backend
    sp<frameworks::bufferhub::V1_0::IBufferClient> mBufferClient;
+7 −7
Original line number Diff line number Diff line
@@ -239,8 +239,8 @@ TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromReleasedState) {

TEST_F(BufferHubBufferStateTransitionTest, GainBuffer_fromGainedState) {
    ASSERT_EQ(b1->Gain(), 0);
    auto current_buffer_state = b1->buffer_state();
    ASSERT_TRUE(IsClientGained(current_buffer_state, b1ClientMask));
    auto currentBufferState = b1->buffer_state();
    ASSERT_TRUE(IsClientGained(currentBufferState, b1ClientMask));

    // Gaining from gained state by the same client should not return error.
    EXPECT_EQ(b1->Gain(), 0);
@@ -291,9 +291,9 @@ TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromSelfInGainedState) {
    ASSERT_TRUE(IsClientGained(b1->buffer_state(), b1ClientMask));

    EXPECT_EQ(b1->Post(), 0);
    auto current_buffer_state = b1->buffer_state();
    EXPECT_TRUE(IsClientReleased(current_buffer_state, b1ClientMask));
    EXPECT_TRUE(IsClientPosted(current_buffer_state, b2ClientMask));
    auto currentBufferState = b1->buffer_state();
    EXPECT_TRUE(IsClientReleased(currentBufferState, b1ClientMask));
    EXPECT_TRUE(IsClientPosted(currentBufferState, b2ClientMask));
}

TEST_F(BufferHubBufferStateTransitionTest, PostBuffer_fromPostedState) {
@@ -348,8 +348,8 @@ TEST_F(BufferHubBufferStateTransitionTest, AcquireBuffer_fromSelfInAcquiredState
    ASSERT_EQ(b1->Gain(), 0);
    ASSERT_EQ(b1->Post(), 0);
    ASSERT_EQ(b2->Acquire(), 0);
    auto current_buffer_state = b1->buffer_state();
    ASSERT_TRUE(IsClientAcquired(current_buffer_state, b2ClientMask));
    auto currentBufferState = b1->buffer_state();
    ASSERT_TRUE(IsClientAcquired(currentBufferState, b2ClientMask));

    // Acquiring from acquired state by the same client should not error out.
    EXPECT_EQ(b2->Acquire(), 0);
+29 −30
Original line number Diff line number Diff line
@@ -14,17 +14,16 @@ namespace implementation {
void BufferNode::InitializeMetadata() {
    // Using placement new here to reuse shared memory instead of new allocation
    // Initialize the atomic variables to zero.
    BufferHubDefs::MetadataHeader* metadata_header = metadata_.metadata_header();
    buffer_state_ = new (&metadata_header->buffer_state) std::atomic<uint32_t>(0);
    fence_state_ = new (&metadata_header->fence_state) std::atomic<uint32_t>(0);
    active_clients_bit_mask_ =
            new (&metadata_header->active_clients_bit_mask) std::atomic<uint32_t>(0);
    BufferHubDefs::MetadataHeader* metadataHeader = mMetadata.metadata_header();
    mBufferState = new (&metadataHeader->buffer_state) std::atomic<uint32_t>(0);
    mFenceState = new (&metadataHeader->fence_state) std::atomic<uint32_t>(0);
    mActiveClientsBitMask = new (&metadataHeader->active_clients_bit_mask) std::atomic<uint32_t>(0);
    // The C++ standard recommends (but does not require) that lock-free atomic operations are
    // also address-free, that is, suitable for communication between processes using shared
    // memory.
    LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(buffer_state_) ||
                                !std::atomic_is_lock_free(fence_state_) ||
                                !std::atomic_is_lock_free(active_clients_bit_mask_),
    LOG_ALWAYS_FATAL_IF(!std::atomic_is_lock_free(mBufferState) ||
                                !std::atomic_is_lock_free(mFenceState) ||
                                !std::atomic_is_lock_free(mActiveClientsBitMask),
                        "Atomic variables in ashmen are not lock free.");
}

@@ -38,25 +37,25 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui
    // hardcoded service name "bufferhub".
    int ret = GraphicBufferAllocator::get().allocate(width, height, format, layer_count, usage,
                                                     const_cast<const native_handle_t**>(
                                                             &buffer_handle_),
                                                             &mBufferHandle),
                                                     &out_stride,
                                                     /*graphicBufferId=*/0,
                                                     /*requestor=*/"bufferhub");

    if (ret != OK || buffer_handle_ == nullptr) {
    if (ret != OK || mBufferHandle == nullptr) {
        ALOGE("%s: Failed to allocate buffer: %s", __FUNCTION__, strerror(-ret));
        return;
    }

    buffer_desc_.width = width;
    buffer_desc_.height = height;
    buffer_desc_.layers = layer_count;
    buffer_desc_.format = format;
    buffer_desc_.usage = usage;
    buffer_desc_.stride = out_stride;
    mBufferDesc.width = width;
    mBufferDesc.height = height;
    mBufferDesc.layers = layer_count;
    mBufferDesc.format = format;
    mBufferDesc.usage = usage;
    mBufferDesc.stride = out_stride;

    metadata_ = BufferHubMetadata::Create(user_metadata_size);
    if (!metadata_.IsValid()) {
    mMetadata = BufferHubMetadata::Create(user_metadata_size);
    if (!mMetadata.IsValid()) {
        ALOGE("%s: Failed to allocate metadata.", __FUNCTION__);
        return;
    }
@@ -65,8 +64,8 @@ BufferNode::BufferNode(uint32_t width, uint32_t height, uint32_t layer_count, ui

BufferNode::~BufferNode() {
    // Free the handle
    if (buffer_handle_ != nullptr) {
        status_t ret = GraphicBufferAllocator::get().free(buffer_handle_);
    if (mBufferHandle != nullptr) {
        status_t ret = GraphicBufferAllocator::get().free(mBufferHandle);
        if (ret != OK) {
            ALOGE("%s: Failed to free handle; Got error: %d", __FUNCTION__, ret);
        }
@@ -79,32 +78,32 @@ BufferNode::~BufferNode() {
}

uint32_t BufferNode::GetActiveClientsBitMask() const {
    return active_clients_bit_mask_->load(std::memory_order_acquire);
    return mActiveClientsBitMask->load(std::memory_order_acquire);
}

uint32_t BufferNode::AddNewActiveClientsBitToMask() {
    uint32_t current_active_clients_bit_mask = GetActiveClientsBitMask();
    uint32_t currentActiveClientsBitMask = GetActiveClientsBitMask();
    uint32_t client_state_mask = 0U;
    uint32_t updated_active_clients_bit_mask = 0U;
    uint32_t updatedActiveClientsBitMask = 0U;
    do {
        client_state_mask =
                BufferHubDefs::FindNextAvailableClientStateMask(current_active_clients_bit_mask);
                BufferHubDefs::FindNextAvailableClientStateMask(currentActiveClientsBitMask);
        if (client_state_mask == 0U) {
            ALOGE("%s: reached the maximum number of channels per buffer node: %d.", __FUNCTION__,
                  BufferHubDefs::kMaxNumberOfClients);
            errno = E2BIG;
            return 0U;
        }
        updated_active_clients_bit_mask = current_active_clients_bit_mask | client_state_mask;
    } while (!(active_clients_bit_mask_->compare_exchange_weak(current_active_clients_bit_mask,
                                                               updated_active_clients_bit_mask,
        updatedActiveClientsBitMask = currentActiveClientsBitMask | client_state_mask;
    } while (!(mActiveClientsBitMask->compare_exchange_weak(currentActiveClientsBitMask,
                                                            updatedActiveClientsBitMask,
                                                            std::memory_order_acq_rel,
                                                            std::memory_order_acquire)));
    return client_state_mask;
}

void BufferNode::RemoveClientsBitFromMask(const uint32_t& value) {
    active_clients_bit_mask_->fetch_and(~value);
    mActiveClientsBitMask->fetch_and(~value);
}

} // namespace implementation
+23 −23
Original line number Diff line number Diff line
@@ -22,36 +22,36 @@ public:
    ~BufferNode();

    // Returns whether the object holds a valid metadata.
    bool IsValid() const { return metadata_.IsValid(); }
    bool IsValid() const { return mMetadata.IsValid(); }

    int id() const { return mId; }

    size_t user_metadata_size() const { return metadata_.user_metadata_size(); }
    size_t user_metadata_size() const { return mMetadata.user_metadata_size(); }

    // Accessors of the buffer description and handle
    const native_handle_t* buffer_handle() const { return buffer_handle_; }
    const AHardwareBuffer_Desc& buffer_desc() const { return buffer_desc_; }
    const native_handle_t* buffer_handle() const { return mBufferHandle; }
    const AHardwareBuffer_Desc& buffer_desc() const { return mBufferDesc; }

    // Accessor of event fd.
    const BufferHubEventFd& eventFd() const { return mEventFd; }

    // Accessors of metadata.
    const BufferHubMetadata& metadata() const { return metadata_; }
    // Accessors of mMetadata.
    const BufferHubMetadata& metadata() const { return mMetadata; }

    // Gets the current value of active_clients_bit_mask in metadata_ with
    // Gets the current value of mActiveClientsBitMask in mMetadata with
    // std::memory_order_acquire, so that all previous releases of
    // active_clients_bit_mask from all threads will be returned here.
    // mActiveClientsBitMask from all threads will be returned here.
    uint32_t GetActiveClientsBitMask() const;

    // Find and add a new client_state_mask to active_clients_bit_mask in
    // metadata_.
    // Return the new client_state_mask that is added to active_clients_bit_mask.
    // Find and add a new client state mask to mActiveClientsBitMask in
    // mMetadata.
    // Return the new client state mask that is added to mActiveClientsBitMask.
    // Return 0U if there are already 16 clients of the buffer.
    uint32_t AddNewActiveClientsBitToMask();

    // Removes the value from active_clients_bit_mask in metadata_ with
    // Removes the value from active_clients_bit_mask in mMetadata with
    // std::memory_order_release, so that the change will be visible to any
    // acquire of active_clients_bit_mask_ in any threads after the succeed of
    // acquire of mActiveClientsBitMask in any threads after the succeed of
    // this operation.
    void RemoveClientsBitFromMask(const uint32_t& value);

@@ -61,34 +61,34 @@ private:
    void InitializeMetadata();

    // Gralloc buffer handles.
    native_handle_t* buffer_handle_;
    AHardwareBuffer_Desc buffer_desc_;
    native_handle_t* mBufferHandle;
    AHardwareBuffer_Desc mBufferDesc;

    // Eventfd used for signalling buffer events among the clients of the buffer.
    BufferHubEventFd mEventFd;

    // Metadata in shared memory.
    BufferHubMetadata metadata_;
    BufferHubMetadata mMetadata;

    // A system-unique id generated by bufferhub from 0 to std::numeric_limits<int>::max().
    // BufferNodes not created by bufferhub will have id < 0, meaning "not specified".
    // TODO(b/118891412): remove default id = -1 and update comments after pdx is no longer in use
    const int mId = -1;

    // The following variables are atomic variables in metadata_ that are visible
    // The following variables are atomic variables in mMetadata that are visible
    // to Bn object and Bp objects. Please find more info in
    // BufferHubDefs::MetadataHeader.

    // buffer_state_ tracks the state of the buffer. Buffer can be in one of these
    // mBufferState tracks the state of the buffer. Buffer can be in one of these
    // four states: gained, posted, acquired, released.
    std::atomic<uint32_t>* buffer_state_ = nullptr;
    std::atomic<uint32_t>* mBufferState = nullptr;

    // TODO(b/112012161): add comments to fence_state_.
    std::atomic<uint32_t>* fence_state_ = nullptr;
    // TODO(b/112012161): add comments to mFenceState.
    std::atomic<uint32_t>* mFenceState = nullptr;

    // active_clients_bit_mask_ tracks all the bp clients of the buffer. It is the
    // mActiveClientsBitMask tracks all the bp clients of the buffer. It is the
    // union of all client_state_mask of all bp clients.
    std::atomic<uint32_t>* active_clients_bit_mask_ = nullptr;
    std::atomic<uint32_t>* mActiveClientsBitMask = nullptr;
};

} // namespace implementation