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

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

Merge changes I8163a752,Ifae94a14

* changes:
  Move BufferHubDefs namespace to ui/BufferHubDefs.h
  Move MetadataHeader to libui
parents c8fa5ce6 d34a80a6
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@

#include <android-base/unique_fd.h>
#include <ui/BufferHubBuffer.h>
#include <ui/BufferHubDefs.h>

using android::base::unique_fd;
using android::dvr::BufferTraits;
@@ -69,7 +70,6 @@ using dvr::BufferHubDefs::IsClientGained;
using dvr::BufferHubDefs::IsClientPosted;
using dvr::BufferHubDefs::IsClientReleased;
using dvr::BufferHubDefs::kHighBitsMask;
using dvr::BufferHubDefs::kMetadataHeaderSize;

} // namespace

@@ -161,7 +161,7 @@ int BufferHubBuffer::ImportGraphicBuffer() {
    }

    size_t metadataSize = static_cast<size_t>(bufferTraits.metadata_size());
    if (metadataSize < kMetadataHeaderSize) {
    if (metadataSize < BufferHubDefs::kMetadataHeaderSize) {
        ALOGE("BufferHubBuffer::ImportGraphicBuffer: metadata too small: %zu", metadataSize);
        return -EINVAL;
    }
+3 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include <errno.h>
#include <sys/mman.h>
#include <limits>

#include <cutils/ashmem.h>
#include <log/log.h>
@@ -29,8 +30,8 @@ static const int kAshmemProt = PROT_READ | PROT_WRITE;

} // namespace

using dvr::BufferHubDefs::kMetadataHeaderSize;
using dvr::BufferHubDefs::MetadataHeader;
using BufferHubDefs::kMetadataHeaderSize;
using BufferHubDefs::MetadataHeader;

/* static */
BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) {
+162 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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_BUFFER_HUB_DEFS_H_
#define ANDROID_BUFFER_HUB_DEFS_H_

#include <atomic>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpacked"
// TODO(b/118893702): remove dependency once DvrNativeBufferMetadata moved out of libdvr
#include <dvr/dvr_api.h>
#pragma clang diagnostic pop

namespace android {

namespace BufferHubDefs {

// Single buffer clients (up to 32) ownership signal.
// 64-bit atomic unsigned int.
// Each client takes 2 bits. The first bit locates in the first 32 bits of
// buffer_state; the second bit locates in the last 32 bits of buffer_state.
// Client states:
// Gained state 11. Exclusive write state.
// Posted state 10.
// Acquired state 01. Shared read state.
// Released state 00.
//
//  MSB                        LSB
//   |                          |
//   v                          v
// [C31|...|C1|C0|C31| ... |C1|C0]

// Maximum number of clients a buffer can have.
static constexpr int kMaxNumberOfClients = 32;

// Definition of bit masks.
//  MSB                            LSB
//   | kHighBitsMask | kLowbitsMask |
//   v               v              v
// [b63|   ...   |b32|b31|   ...  |b0]

// The location of lower 32 bits in the 64-bit buffer state.
static constexpr uint64_t kLowbitsMask = (1ULL << kMaxNumberOfClients) - 1ULL;

// The location of higher 32 bits in the 64-bit buffer state.
static constexpr uint64_t kHighBitsMask = ~kLowbitsMask;

// The client bit mask of the first client.
static constexpr uint64_t kFirstClientBitMask = (1ULL << kMaxNumberOfClients) + 1ULL;

// Returns true if any of the client is in gained state.
static inline bool AnyClientGained(uint64_t state) {
    uint64_t high_bits = state >> kMaxNumberOfClients;
    uint64_t low_bits = state & kLowbitsMask;
    return high_bits == low_bits && low_bits != 0ULL;
}

// Returns true if the input client is in gained state.
static inline bool IsClientGained(uint64_t state, uint64_t client_bit_mask) {
    return state == client_bit_mask;
}

// Returns true if any of the client is in posted state.
static inline bool AnyClientPosted(uint64_t state) {
    uint64_t high_bits = state >> kMaxNumberOfClients;
    uint64_t low_bits = state & kLowbitsMask;
    uint64_t posted_or_acquired = high_bits ^ low_bits;
    return posted_or_acquired & high_bits;
}

// Returns true if the input client is in posted state.
static inline bool IsClientPosted(uint64_t state, uint64_t client_bit_mask) {
    uint64_t client_bits = state & client_bit_mask;
    if (client_bits == 0ULL) return false;
    uint64_t low_bits = client_bits & kLowbitsMask;
    return low_bits == 0ULL;
}

// Return true if any of the client is in acquired state.
static inline bool AnyClientAcquired(uint64_t state) {
    uint64_t high_bits = state >> kMaxNumberOfClients;
    uint64_t low_bits = state & kLowbitsMask;
    uint64_t posted_or_acquired = high_bits ^ low_bits;
    return posted_or_acquired & low_bits;
}

// Return true if the input client is in acquired state.
static inline bool IsClientAcquired(uint64_t state, uint64_t client_bit_mask) {
    uint64_t client_bits = state & client_bit_mask;
    if (client_bits == 0ULL) return false;
    uint64_t high_bits = client_bits & kHighBitsMask;
    return high_bits == 0ULL;
}

// Returns true if all clients are in released state.
static inline bool IsBufferReleased(uint64_t state) {
    return state == 0ULL;
}

// Returns true if the input client is in released state.
static inline bool IsClientReleased(uint64_t state, uint64_t client_bit_mask) {
    return (state & client_bit_mask) == 0ULL;
}

// Returns the next available buffer client's client_state_masks.
// @params union_bits. Union of all existing clients' client_state_masks.
static inline uint64_t FindNextAvailableClientStateMask(uint64_t union_bits) {
    uint64_t low_union = union_bits & kLowbitsMask;
    if (low_union == kLowbitsMask) return 0ULL;
    uint64_t incremented = low_union + 1ULL;
    uint64_t difference = incremented ^ low_union;
    uint64_t new_low_bit = (difference + 1ULL) >> 1;
    return new_low_bit + (new_low_bit << kMaxNumberOfClients);
}

struct __attribute__((aligned(8))) MetadataHeader {
    // Internal data format, which can be updated as long as the size, padding and field alignment
    // of the struct is consistent within the same ABI. As this part is subject for future updates,
    // it's not stable cross Android version, so don't have it visible from outside of the Android
    // platform (include Apps and vendor HAL).

    // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in
    // buffer_state.
    std::atomic<uint64_t> buffer_state;

    // Every client takes up one bit in fence_state. Only the lower 32 bits are valid. The upper 32
    // bits are there for easier manipulation, but the value should be ignored.
    std::atomic<uint64_t> fence_state;

    // Every client takes up one bit from the higher 32 bits and one bit from the lower 32 bits in
    // active_clients_bit_mask.
    std::atomic<uint64_t> active_clients_bit_mask;

    // The index of the buffer queue where the buffer belongs to.
    uint64_t queue_index;

    // Public data format, which should be updated with caution. See more details in dvr_api.h
    DvrNativeBufferMetadata metadata;
};

static_assert(sizeof(MetadataHeader) == 136, "Unexpected MetadataHeader size");
static constexpr size_t kMetadataHeaderSize = sizeof(MetadataHeader);

} // namespace BufferHubDefs

} // namespace android

#endif // ANDROID_BUFFER_HUB_DEFS_H_
+5 −25
Original line number Diff line number Diff line
@@ -17,26 +17,8 @@
#ifndef ANDROID_BUFFER_HUB_METADATA_H_
#define ANDROID_BUFFER_HUB_METADATA_H_

// We would eliminate the clang warnings introduced by libdpx.
// TODO(b/112338294): Remove those once BufferHub moved to use Binder
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wdouble-promotion"
#pragma clang diagnostic ignored "-Wgnu-case-range"
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
#pragma clang diagnostic ignored "-Wnested-anon-types"
#pragma clang diagnostic ignored "-Wpacked"
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wswitch-enum"
#pragma clang diagnostic ignored "-Wundefined-func-template"
#pragma clang diagnostic ignored "-Wunused-template"
#pragma clang diagnostic ignored "-Wweak-vtables"
#include <private/dvr/buffer_hub_defs.h>
#pragma clang diagnostic pop

#include <android-base/unique_fd.h>
#include <ui/BufferHubDefs.h>

namespace android {

@@ -84,23 +66,21 @@ public:
    bool IsValid() const { return mAshmemFd.get() != -1 && mMetadataHeader != nullptr; }

    size_t user_metadata_size() const { return mUserMetadataSize; }
    size_t metadata_size() const {
        return mUserMetadataSize + dvr::BufferHubDefs::kMetadataHeaderSize;
    }
    size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; }

    const unique_fd& ashmem_fd() const { return mAshmemFd; }
    dvr::BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; }
    BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; }

private:
    BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd,
                      dvr::BufferHubDefs::MetadataHeader* metadataHeader);
                      BufferHubDefs::MetadataHeader* metadataHeader);

    BufferHubMetadata(const BufferHubMetadata&) = delete;
    void operator=(const BufferHubMetadata&) = delete;

    size_t mUserMetadataSize = 0;
    unique_fd mAshmemFd;
    dvr::BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr;
    BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr;
};

} // namespace android
+1 −2
Original line number Diff line number Diff line
@@ -73,10 +73,9 @@ cc_test {

cc_test {
    name: "BufferHubMetadata_test",
    header_libs: ["libbufferhub_headers", "libdvr_headers"],
    header_libs: ["libdvr_headers"],
    shared_libs: [
        "libbase",
        "libpdx_default_transport",
        "libui",
        "libutils",
    ],
Loading