1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <functional>
20 #include <utility>
21 
22 #include <binder/IBinder.h>
23 #include <ui/DisplayId.h>
24 #include <ui/DisplayMap.h>
25 #include <utils/StrongPointer.h>
26 
27 #include "DisplaySnapshot.h"
28 #include "DisplaySnapshotRef.h"
29 
30 namespace android::display {
31 
32 // TODO(b/229877597): Replace with AIDL type.
33 using DisplayToken = IBinder;
34 
35 class PhysicalDisplay {
36 public:
37     template <typename... Args>
PhysicalDisplay(sp<DisplayToken> token,Args &&...args)38     PhysicalDisplay(sp<DisplayToken> token, Args&&... args)
39           : mToken(std::move(token)), mSnapshot(std::forward<Args>(args)...) {}
40 
41     PhysicalDisplay(const PhysicalDisplay&) = delete;
42     PhysicalDisplay(PhysicalDisplay&&) = default;
43 
token()44     const sp<DisplayToken>& token() const { return mToken; }
snapshot()45     const DisplaySnapshot& snapshot() const { return mSnapshot; }
46 
47     // Transformers for PhysicalDisplays::get.
48 
snapshotRef()49     DisplaySnapshotRef snapshotRef() const { return std::cref(mSnapshot); }
50 
isInternal()51     bool isInternal() const {
52         return mSnapshot.connectionType() == ui::DisplayConnectionType::Internal;
53     }
54 
55     // Predicate for ftl::find_if on PhysicalDisplays.
hasToken(const sp<DisplayToken> & token)56     static constexpr auto hasToken(const sp<DisplayToken>& token) {
57         return [&token](const std::pair<const PhysicalDisplayId, PhysicalDisplay>& pair) {
58             return pair.second.token() == token;
59         };
60     }
61 
62 private:
63     const sp<DisplayToken> mToken;
64 
65     // Effectively const except in move constructor.
66     DisplaySnapshot mSnapshot;
67 };
68 
69 using PhysicalDisplays = ui::PhysicalDisplayMap<PhysicalDisplayId, PhysicalDisplay>;
70 
71 // Combinator for ftl::Optional<PhysicalDisplayId>::and_then.
getPhysicalDisplay(const PhysicalDisplays & displays)72 constexpr auto getPhysicalDisplay(const PhysicalDisplays& displays) {
73     return [&](PhysicalDisplayId id) { return displays.get(id); };
74 }
75 
76 } // namespace android::display
77