1 /*
2  * Copyright (C) 2017 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 <android-base/stringprintf.h>
20 #include <ftl/enum.h>
21 #include <ftl/string.h>
22 #include <input/Input.h>
23 #include <ui/Rotation.h>
24 
25 #include <cinttypes>
26 #include <optional>
27 
28 using android::base::StringPrintf;
29 
30 namespace android {
31 
32 /**
33  * Describes the different type of viewports supported by input flinger.
34  * Keep in sync with values in InputManagerService.java.
35  */
36 enum class ViewportType : int32_t {
37     INTERNAL = 1,
38     EXTERNAL = 2,
39     VIRTUAL = 3,
40 
41     ftl_last = VIRTUAL
42 };
43 
44 /*
45  * Describes how coordinates are mapped on a physical display.
46  * See com.android.server.display.DisplayViewport.
47  */
48 struct DisplayViewport {
49     ui::LogicalDisplayId displayId;
50     ui::Rotation orientation;
51     int32_t logicalLeft;
52     int32_t logicalTop;
53     int32_t logicalRight;
54     int32_t logicalBottom;
55     int32_t physicalLeft;
56     int32_t physicalTop;
57     int32_t physicalRight;
58     int32_t physicalBottom;
59     int32_t deviceWidth;
60     int32_t deviceHeight;
61     bool isActive;
62     std::string uniqueId;
63     // The actual (hardware) port that the associated display is connected to.
64     // Not all viewports will have this specified.
65     std::optional<uint8_t> physicalPort;
66     ViewportType type;
67 
DisplayViewportDisplayViewport68     DisplayViewport()
69           : displayId(ui::LogicalDisplayId::INVALID),
70             orientation(ui::ROTATION_0),
71             logicalLeft(0),
72             logicalTop(0),
73             logicalRight(0),
74             logicalBottom(0),
75             physicalLeft(0),
76             physicalTop(0),
77             physicalRight(0),
78             physicalBottom(0),
79             deviceWidth(0),
80             deviceHeight(0),
81             isActive(false),
82             uniqueId(),
83             physicalPort(std::nullopt),
84             type(ViewportType::INTERNAL) {}
85 
86     bool operator==(const DisplayViewport& other) const {
87         return displayId == other.displayId && orientation == other.orientation &&
88                 logicalLeft == other.logicalLeft && logicalTop == other.logicalTop &&
89                 logicalRight == other.logicalRight && logicalBottom == other.logicalBottom &&
90                 physicalLeft == other.physicalLeft && physicalTop == other.physicalTop &&
91                 physicalRight == other.physicalRight && physicalBottom == other.physicalBottom &&
92                 deviceWidth == other.deviceWidth && deviceHeight == other.deviceHeight &&
93                 isActive == other.isActive && uniqueId == other.uniqueId &&
94                 physicalPort == other.physicalPort && type == other.type;
95     }
96 
97     bool operator!=(const DisplayViewport& other) const {
98         return !(*this == other);
99     }
100 
isValidDisplayViewport101     inline bool isValid() const { return displayId.isValid(); }
102 
setNonDisplayViewportDisplayViewport103     void setNonDisplayViewport(int32_t width, int32_t height) {
104         displayId = ui::LogicalDisplayId::INVALID;
105         orientation = ui::ROTATION_0;
106         logicalLeft = 0;
107         logicalTop = 0;
108         logicalRight = width;
109         logicalBottom = height;
110         physicalLeft = 0;
111         physicalTop = 0;
112         physicalRight = width;
113         physicalBottom = height;
114         deviceWidth = width;
115         deviceHeight = height;
116         isActive = true;
117         uniqueId.clear();
118         physicalPort = std::nullopt;
119         type = ViewportType::INTERNAL;
120     }
121 
toStringDisplayViewport122     std::string toString() const {
123         return StringPrintf("Viewport %s: displayId=%s, uniqueId=%s, port=%s, orientation=%d, "
124                             "logicalFrame=[%d, %d, %d, %d], "
125                             "physicalFrame=[%d, %d, %d, %d], "
126                             "deviceSize=[%d, %d], "
127                             "isActive=[%d]",
128                             ftl::enum_string(type).c_str(), displayId.toString().c_str(),
129                             uniqueId.c_str(),
130                             physicalPort ? ftl::to_string(*physicalPort).c_str() : "<none>",
131                             static_cast<int>(orientation), logicalLeft, logicalTop, logicalRight,
132                             logicalBottom, physicalLeft, physicalTop, physicalRight, physicalBottom,
133                             deviceWidth, deviceHeight, isActive);
134     }
135 };
136 
137 } // namespace android
138