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

Commit 3f9ee01a authored by Gil Dekel's avatar Gil Dekel Committed by Android (Google) Code Review
Browse files

Merge "SF: Turn DisplayId::fromValue to an explicit wrapper" into main

parents f7d8ed5a bddaa241
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -772,8 +772,8 @@ int main(int argc, char** argv) {
            break;

            case 'i':
                displayId = DisplayId::fromValue<PhysicalDisplayId>(atoll(optarg));
                if (!displayId) {
                displayId = PhysicalDisplayId::fromValue(atoll(optarg));
                if (std::find(ids.begin(), ids.end(), displayId) == ids.end()) {
                    fprintf(stderr, "Invalid display ID: %s.\n", optarg);
                    exit(4);
                }
+2 −3
Original line number Diff line number Diff line
@@ -1415,9 +1415,8 @@ std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
            ComposerServiceAIDL::getComposerService()->getPhysicalDisplayIds(&displayIds);
    if (status.isOk()) {
        physicalDisplayIds.reserve(displayIds.size());
        for (auto item : displayIds) {
            auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(item));
            physicalDisplayIds.push_back(*id);
        for (auto id : displayIds) {
            physicalDisplayIds.push_back(PhysicalDisplayId::fromValue(static_cast<uint64_t>(id)));
        }
    }
    return physicalDisplayIds;
+1 −1
Original line number Diff line number Diff line
@@ -444,7 +444,7 @@ PhysicalDisplayId generateEdidDisplayId(const Edid& edid) {
            (edid.hashedBlockZeroSerialNumberOpt.value_or(0) >> 11) ^
            (edid.hashedDescriptorBlockSerialNumberOpt.value_or(0) << 23);

    return PhysicalDisplayId::fromEdidHash(id);
    return PhysicalDisplayId::fromValue(id);
}

} // namespace android
+28 −32
Original line number Diff line number Diff line
@@ -30,27 +30,16 @@ struct DisplayId {
    // Flag indicating that the display is virtual.
    static constexpr uint64_t FLAG_VIRTUAL = 1ULL << 63;

    // TODO(b/162612135) Remove default constructor
    // TODO: b/162612135 - Remove default constructor.
    DisplayId() = default;
    constexpr DisplayId(const DisplayId&) = default;
    DisplayId& operator=(const DisplayId&) = default;

    static constexpr DisplayId fromValue(uint64_t value) { return DisplayId(value); }
    constexpr bool isVirtual() const { return value & FLAG_VIRTUAL; }

    uint64_t value;

    // For deserialization.
    static constexpr std::optional<DisplayId> fromValue(uint64_t);

    // As above, but also upcast to Id.
    template <typename Id>
    static constexpr std::optional<Id> fromValue(uint64_t value) {
        if (const auto id = Id::tryCast(DisplayId(value))) {
            return id;
        }
        return {};
    }

protected:
    explicit constexpr DisplayId(uint64_t id) : value(id) {}
};
@@ -74,6 +63,9 @@ inline std::ostream& operator<<(std::ostream& stream, DisplayId displayId) {

// DisplayId of a physical display, such as the internal display or externally connected display.
struct PhysicalDisplayId : DisplayId {
    // TODO: b/162612135 - Remove default constructor.
    PhysicalDisplayId() = default;

    static constexpr ftl::Optional<PhysicalDisplayId> tryCast(DisplayId id) {
        if (id.isVirtual()) {
            return std::nullopt;
@@ -87,11 +79,6 @@ struct PhysicalDisplayId : DisplayId {
        return PhysicalDisplayId(FLAG_STABLE, port, manufacturerId, modelHash);
    }

    // Returns a stable and consistent ID based exclusively on EDID information.
    static constexpr PhysicalDisplayId fromEdidHash(uint64_t hashedEdid) {
        return PhysicalDisplayId(hashedEdid);
    }

    // Returns an unstable ID. If EDID is available using "fromEdid" is preferred.
    static constexpr PhysicalDisplayId fromPort(uint8_t port) {
        constexpr uint16_t kManufacturerId = 0;
@@ -99,8 +86,9 @@ struct PhysicalDisplayId : DisplayId {
        return PhysicalDisplayId(0, port, kManufacturerId, kModelHash);
    }

    // TODO(b/162612135) Remove default constructor
    PhysicalDisplayId() = default;
    static constexpr PhysicalDisplayId fromValue(uint64_t value) {
        return PhysicalDisplayId(value);
    }

    constexpr uint8_t getPort() const { return static_cast<uint8_t>(value); }

@@ -131,8 +119,15 @@ struct VirtualDisplayId : DisplayId {
        return std::nullopt;
    }

    static constexpr VirtualDisplayId fromValue(uint64_t value) {
        return VirtualDisplayId(SkipVirtualFlag{}, value);
    }

protected:
    struct SkipVirtualFlag {};
    constexpr VirtualDisplayId(SkipVirtualFlag, uint64_t value) : DisplayId(value) {}
    explicit constexpr VirtualDisplayId(uint64_t value) : DisplayId(FLAG_VIRTUAL | value) {}

    explicit constexpr VirtualDisplayId(DisplayId other) : DisplayId(other) {}
};

@@ -146,8 +141,12 @@ struct HalVirtualDisplayId : VirtualDisplayId {
        return std::nullopt;
    }

    static constexpr HalVirtualDisplayId fromValue(uint64_t value) {
        return HalVirtualDisplayId(SkipVirtualFlag{}, value);
    }

private:
    explicit constexpr HalVirtualDisplayId(DisplayId other) : VirtualDisplayId(other) {}
    using VirtualDisplayId::VirtualDisplayId;
};

struct GpuVirtualDisplayId : VirtualDisplayId {
@@ -160,8 +159,12 @@ struct GpuVirtualDisplayId : VirtualDisplayId {
        return std::nullopt;
    }

    static constexpr GpuVirtualDisplayId fromValue(uint64_t value) {
        return GpuVirtualDisplayId(SkipVirtualFlag{}, value);
    }

private:
    explicit constexpr GpuVirtualDisplayId(DisplayId other) : VirtualDisplayId(other) {}
    using VirtualDisplayId::VirtualDisplayId;
};

// HalDisplayId is the ID of a display which is managed by HWC.
@@ -177,20 +180,13 @@ struct HalDisplayId : DisplayId {
        return HalDisplayId(id);
    }

    static constexpr HalDisplayId fromValue(uint64_t value) { return HalDisplayId(value); }

private:
    using DisplayId::DisplayId;
    explicit constexpr HalDisplayId(DisplayId other) : DisplayId(other) {}
};

constexpr std::optional<DisplayId> DisplayId::fromValue(uint64_t value) {
    if (const auto id = fromValue<PhysicalDisplayId>(value)) {
        return id;
    }
    if (const auto id = fromValue<VirtualDisplayId>(value)) {
        return id;
    }
    return {};
}

static_assert(sizeof(DisplayId) == sizeof(uint64_t));
static_assert(sizeof(HalDisplayId) == sizeof(uint64_t));
static_assert(sizeof(VirtualDisplayId) == sizeof(uint64_t));
+4 −4
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ TEST(DisplayIdTest, createPhysicalIdFromEdid) {
    EXPECT_TRUE(HalDisplayId::tryCast(id));

    EXPECT_EQ(id, DisplayId::fromValue(id.value));
    EXPECT_EQ(id, DisplayId::fromValue<PhysicalDisplayId>(id.value));
    EXPECT_EQ(id, PhysicalDisplayId::fromValue(id.value));
}

TEST(DisplayIdTest, createPhysicalIdFromPort) {
@@ -47,7 +47,7 @@ TEST(DisplayIdTest, createPhysicalIdFromPort) {
    EXPECT_TRUE(HalDisplayId::tryCast(id));

    EXPECT_EQ(id, DisplayId::fromValue(id.value));
    EXPECT_EQ(id, DisplayId::fromValue<PhysicalDisplayId>(id.value));
    EXPECT_EQ(id, PhysicalDisplayId::fromValue(id.value));
}

TEST(DisplayIdTest, createGpuVirtualId) {
@@ -59,7 +59,7 @@ TEST(DisplayIdTest, createGpuVirtualId) {
    EXPECT_FALSE(HalDisplayId::tryCast(id));

    EXPECT_EQ(id, DisplayId::fromValue(id.value));
    EXPECT_EQ(id, DisplayId::fromValue<GpuVirtualDisplayId>(id.value));
    EXPECT_EQ(id, GpuVirtualDisplayId::fromValue(id.value));
}

TEST(DisplayIdTest, createVirtualIdFromGpuVirtualId) {
@@ -83,7 +83,7 @@ TEST(DisplayIdTest, createHalVirtualId) {
    EXPECT_TRUE(HalDisplayId::tryCast(id));

    EXPECT_EQ(id, DisplayId::fromValue(id.value));
    EXPECT_EQ(id, DisplayId::fromValue<HalVirtualDisplayId>(id.value));
    EXPECT_EQ(id, HalVirtualDisplayId::fromValue(id.value));
}

TEST(DisplayIdTest, createVirtualIdFromHalVirtualId) {
Loading