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