blob: 6a32d55d258c3ab4610ebb59dd58ce927370decf [file] [log] [blame]
Robert Carr3720ed02018-08-08 16:08:27 -07001/*
2 * Copyright (C) 2011 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
chaviw3277faf2021-05-19 16:45:23 -050017#define LOG_TAG "WindowInfo"
Robert Carr3720ed02018-08-08 16:08:27 -070018#define LOG_NDEBUG 0
19
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020#include <type_traits>
21
Robert Carr3720ed02018-08-08 16:08:27 -070022#include <binder/Parcel.h>
chaviw3277faf2021-05-19 16:45:23 -050023#include <gui/WindowInfo.h>
Robert Carr3720ed02018-08-08 16:08:27 -070024
25#include <log/log.h>
26
chaviw3277faf2021-05-19 16:45:23 -050027namespace android::gui {
Robert Carr3720ed02018-08-08 16:08:27 -070028
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -080029namespace {
30
31std::ostream& operator<<(std::ostream& out, const sp<IBinder>& binder) {
32 if (binder == nullptr) {
33 out << "<null>";
34 } else {
35 out << binder.get();
36 }
37 return out;
38}
39
40std::ostream& operator<<(std::ostream& out, const Region& region) {
41 if (region.isEmpty()) {
42 out << "<empty>";
43 return out;
44 }
45
46 bool first = true;
47 Region::const_iterator cur = region.begin();
48 Region::const_iterator const tail = region.end();
49 while (cur != tail) {
50 if (first) {
51 first = false;
52 } else {
53 out << "|";
54 }
55 out << "[" << cur->left << "," << cur->top << "][" << cur->right << "," << cur->bottom
56 << "]";
57 cur++;
58 }
59 return out;
60}
61
Prabir Pradhan72c69902024-12-23 22:52:53 +000062status_t writeTransform(android::Parcel* parcel, const ui::Transform& transform) {
63 return parcel->writeFloat(transform.dsdx()) ?:
64 parcel->writeFloat(transform.dtdx()) ?:
65 parcel->writeFloat(transform.tx()) ?:
66 parcel->writeFloat(transform.dtdy()) ?:
67 parcel->writeFloat(transform.dsdy()) ?:
68 parcel->writeFloat(transform.ty());
69}
70
71status_t readTransform(const android::Parcel* parcel, ui::Transform& transform) {
72 float dsdx, dtdx, tx, dtdy, dsdy, ty;
73
74 const status_t status = parcel->readFloat(&dsdx) ?:
75 parcel->readFloat(&dtdx) ?:
76 parcel->readFloat(&tx) ?:
77 parcel->readFloat(&dtdy) ?:
78 parcel->readFloat(&dsdy) ?:
79 parcel->readFloat(&ty);
80 if (status != OK) {
81 return status;
82 }
83
84 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
85 return OK;
86}
87
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -080088} // namespace
89
Dominik Laskowski2f01d772022-03-23 16:01:29 -070090void WindowInfo::setInputConfig(ftl::Flags<InputConfig> config, bool value) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080091 if (value) {
92 inputConfig |= config;
93 return;
94 }
95 inputConfig &= ~config;
96}
97
chaviw3277faf2021-05-19 16:45:23 -050098void WindowInfo::addTouchableRegion(const Rect& region) {
Robert Carr3720ed02018-08-08 16:08:27 -070099 touchableRegion.orSelf(region);
100}
101
chaviw3277faf2021-05-19 16:45:23 -0500102bool WindowInfo::supportsSplitTouch() const {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -0800103 return !inputConfig.test(InputConfig::PREVENT_SPLITTING);
Robert Carr3720ed02018-08-08 16:08:27 -0700104}
105
Prabir Pradhan07e05b62021-11-19 03:57:24 -0800106bool WindowInfo::isSpy() const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -0800107 return inputConfig.test(InputConfig::SPY);
Prabir Pradhan07e05b62021-11-19 03:57:24 -0800108}
109
Prabir Pradhand65552b2021-10-07 11:23:50 -0700110bool WindowInfo::interceptsStylus() const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -0800111 return inputConfig.test(InputConfig::INTERCEPTS_STYLUS);
Prabir Pradhand65552b2021-10-07 11:23:50 -0700112}
113
chaviw3277faf2021-05-19 16:45:23 -0500114bool WindowInfo::overlaps(const WindowInfo* other) const {
Chavi Weingarten7f019192023-08-08 20:39:01 +0000115 return !frame.isEmpty() && frame.left < other->frame.right && frame.right > other->frame.left &&
116 frame.top < other->frame.bottom && frame.bottom > other->frame.top;
Robert Carr3720ed02018-08-08 16:08:27 -0700117}
118
chaviw3277faf2021-05-19 16:45:23 -0500119bool WindowInfo::operator==(const WindowInfo& info) const {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800120 return info.token == token && info.id == id && info.name == name &&
Chavi Weingarten7f019192023-08-08 20:39:01 +0000121 info.dispatchingTimeout == dispatchingTimeout && info.frame == frame &&
Vishnu Nair494a2e42023-11-10 17:21:19 -0800122 info.contentSize == contentSize && info.surfaceInset == surfaceInset &&
123 info.globalScaleFactor == globalScaleFactor && info.transform == transform &&
124 info.touchableRegion.hasSameRects(touchableRegion) &&
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800125 info.touchOcclusionMode == touchOcclusionMode && info.ownerPid == ownerPid &&
126 info.ownerUid == ownerUid && info.packageName == packageName &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -0800127 info.inputConfig == inputConfig && info.displayId == displayId &&
Chris Ye0783e992020-06-02 21:34:49 -0700128 info.replaceTouchableRegionWithCrop == replaceTouchableRegionWithCrop &&
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800129 info.applicationInfo == applicationInfo && info.layoutParamsType == layoutParamsType &&
Vishnu Nair59a6be32024-01-29 10:26:21 -0800130 info.layoutParamsFlags == layoutParamsFlags &&
131 info.canOccludePresentation == canOccludePresentation;
Chris Ye0783e992020-06-02 21:34:49 -0700132}
133
chaviw3277faf2021-05-19 16:45:23 -0500134status_t WindowInfo::writeToParcel(android::Parcel* parcel) const {
Chris Ye0783e992020-06-02 21:34:49 -0700135 if (parcel == nullptr) {
136 ALOGE("%s: Null parcel", __func__);
137 return BAD_VALUE;
138 }
Robert Carr2984b7a2020-04-13 17:06:45 -0700139 if (name.empty()) {
Chris Ye0783e992020-06-02 21:34:49 -0700140 parcel->writeInt32(0);
Robert Carr3720ed02018-08-08 16:08:27 -0700141 return OK;
142 }
Chris Ye0783e992020-06-02 21:34:49 -0700143 parcel->writeInt32(1);
Robert Carr3720ed02018-08-08 16:08:27 -0700144
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000145 // Ensure that the size of custom types are what we expect for writing into the parcel.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800146 static_assert(sizeof(inputConfig) == 4u);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000147 static_assert(sizeof(ownerPid.val()) == 4u);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000148 static_assert(sizeof(ownerUid.val()) == 4u);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800149
Vishnu Nair47074b82020-08-14 11:54:47 -0700150 // clang-format off
Chris Ye0783e992020-06-02 21:34:49 -0700151 status_t status = parcel->writeStrongBinder(token) ?:
152 parcel->writeInt64(dispatchingTimeout.count()) ?:
153 parcel->writeInt32(id) ?:
154 parcel->writeUtf8AsUtf16(name) ?:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800155 parcel->writeInt32(layoutParamsFlags.get()) ?:
156 parcel->writeInt32(
157 static_cast<std::underlying_type_t<WindowInfo::Type>>(layoutParamsType)) ?:
Chavi Weingarten7f019192023-08-08 20:39:01 +0000158 parcel->write(frame) ?:
Vishnu Nair494a2e42023-11-10 17:21:19 -0800159 parcel->writeInt32(contentSize.width) ?:
160 parcel->writeInt32(contentSize.height) ?:
Chris Ye0783e992020-06-02 21:34:49 -0700161 parcel->writeInt32(surfaceInset) ?:
162 parcel->writeFloat(globalScaleFactor) ?:
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100163 parcel->writeFloat(alpha) ?:
Prabir Pradhan72c69902024-12-23 22:52:53 +0000164 writeTransform(parcel, transform) ?:
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100165 parcel->writeInt32(static_cast<int32_t>(touchOcclusionMode)) ?:
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000166 parcel->writeInt32(ownerPid.val()) ?:
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000167 parcel->writeInt32(ownerUid.val()) ?:
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100168 parcel->writeUtf8AsUtf16(packageName) ?:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800169 parcel->writeInt32(inputConfig.get()) ?:
Linnan Li13bf76a2024-05-05 19:18:02 +0800170 parcel->writeInt32(displayId.val()) ?:
Chris Ye0783e992020-06-02 21:34:49 -0700171 applicationInfo.writeToParcel(parcel) ?:
172 parcel->write(touchableRegion) ?:
173 parcel->writeBool(replaceTouchableRegionWithCrop) ?:
chaviwe0ba4e92021-08-11 11:38:41 -0500174 parcel->writeStrongBinder(touchableRegionCropHandle.promote()) ?:
Vishnu Nair59a6be32024-01-29 10:26:21 -0800175 parcel->writeStrongBinder(windowToken) ?:
176 parcel->writeStrongBinder(focusTransferTarget) ?:
Prabir Pradhan72c69902024-12-23 22:52:53 +0000177 parcel->writeBool(canOccludePresentation) ?:
178 parcel->writeBool(cloneLayerStackTransform.has_value());
Vishnu Nair47074b82020-08-14 11:54:47 -0700179 // clang-format on
Prabir Pradhan72c69902024-12-23 22:52:53 +0000180 if (cloneLayerStackTransform) {
181 status = status ?: writeTransform(parcel, *cloneLayerStackTransform);
182 }
Chris Ye0783e992020-06-02 21:34:49 -0700183 return status;
Robert Carr3720ed02018-08-08 16:08:27 -0700184}
185
chaviw3277faf2021-05-19 16:45:23 -0500186status_t WindowInfo::readFromParcel(const android::Parcel* parcel) {
Chris Ye0783e992020-06-02 21:34:49 -0700187 if (parcel == nullptr) {
188 ALOGE("%s: Null parcel", __func__);
189 return BAD_VALUE;
190 }
191 if (parcel->readInt32() == 0) {
192 return OK;
Robert Carr3720ed02018-08-08 16:08:27 -0700193 }
Robert Carr5c8a0262018-10-03 16:30:44 -0700194
Chris Ye0783e992020-06-02 21:34:49 -0700195 token = parcel->readStrongBinder();
Michael Wright44753b12020-07-08 13:48:11 +0100196 dispatchingTimeout = static_cast<decltype(dispatchingTimeout)>(parcel->readInt64());
197 status_t status = parcel->readInt32(&id) ?: parcel->readUtf8FromUtf16(&name);
198 if (status != OK) {
199 return status;
200 }
201
Linnan Li13bf76a2024-05-05 19:18:02 +0800202 int32_t lpFlags, lpType, touchOcclusionModeInt, inputConfigInt, ownerPidInt, ownerUidInt,
203 displayIdInt;
Prabir Pradhan032141e2022-02-15 05:30:01 -0800204 sp<IBinder> touchableRegionCropHandleSp;
Prabir Pradhan72c69902024-12-23 22:52:53 +0000205 bool hasCloneLayerStackTransform = false;
Prabir Pradhan032141e2022-02-15 05:30:01 -0800206
Vishnu Nair47074b82020-08-14 11:54:47 -0700207 // clang-format off
Prabir Pradhan032141e2022-02-15 05:30:01 -0800208 status = parcel->readInt32(&lpFlags) ?:
209 parcel->readInt32(&lpType) ?:
Chavi Weingarten7f019192023-08-08 20:39:01 +0000210 parcel->read(frame) ?:
Vishnu Nair494a2e42023-11-10 17:21:19 -0800211 parcel->readInt32(&contentSize.width) ?:
212 parcel->readInt32(&contentSize.height) ?:
Chris Ye0783e992020-06-02 21:34:49 -0700213 parcel->readInt32(&surfaceInset) ?:
214 parcel->readFloat(&globalScaleFactor) ?:
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100215 parcel->readFloat(&alpha) ?:
Prabir Pradhan72c69902024-12-23 22:52:53 +0000216 readTransform(parcel, /*byRef*/ transform) ?:
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100217 parcel->readInt32(&touchOcclusionModeInt) ?:
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000218 parcel->readInt32(&ownerPidInt) ?:
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000219 parcel->readInt32(&ownerUidInt) ?:
Prabir Pradhan032141e2022-02-15 05:30:01 -0800220 parcel->readUtf8FromUtf16(&packageName) ?:
221 parcel->readInt32(&inputConfigInt) ?:
Linnan Li13bf76a2024-05-05 19:18:02 +0800222 parcel->readInt32(&displayIdInt) ?:
Chris Ye0783e992020-06-02 21:34:49 -0700223 applicationInfo.readFromParcel(parcel) ?:
224 parcel->read(touchableRegion) ?:
Prabir Pradhan032141e2022-02-15 05:30:01 -0800225 parcel->readBool(&replaceTouchableRegionWithCrop) ?:
226 parcel->readNullableStrongBinder(&touchableRegionCropHandleSp) ?:
Chavi Weingarten847e8512023-03-29 00:26:09 +0000227 parcel->readNullableStrongBinder(&windowToken) ?:
Vishnu Nair59a6be32024-01-29 10:26:21 -0800228 parcel->readNullableStrongBinder(&focusTransferTarget) ?:
Prabir Pradhan72c69902024-12-23 22:52:53 +0000229 parcel->readBool(&canOccludePresentation)?:
230 parcel->readBool(&hasCloneLayerStackTransform);
chaviw3277faf2021-05-19 16:45:23 -0500231 // clang-format on
Robert Carr3720ed02018-08-08 16:08:27 -0700232
Michael Wright44753b12020-07-08 13:48:11 +0100233 if (status != OK) {
234 return status;
235 }
236
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700237 layoutParamsFlags = ftl::Flags<Flag>(lpFlags);
Prabir Pradhan032141e2022-02-15 05:30:01 -0800238 layoutParamsType = static_cast<Type>(lpType);
Prabir Pradhan032141e2022-02-15 05:30:01 -0800239 touchOcclusionMode = static_cast<TouchOcclusionMode>(touchOcclusionModeInt);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700240 inputConfig = ftl::Flags<InputConfig>(inputConfigInt);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000241 ownerPid = Pid{ownerPidInt};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000242 ownerUid = Uid{static_cast<uid_t>(ownerUidInt)};
Prabir Pradhan032141e2022-02-15 05:30:01 -0800243 touchableRegionCropHandle = touchableRegionCropHandleSp;
Linnan Li13bf76a2024-05-05 19:18:02 +0800244 displayId = ui::LogicalDisplayId{displayIdInt};
Robert Carr3720ed02018-08-08 16:08:27 -0700245
Prabir Pradhan72c69902024-12-23 22:52:53 +0000246 cloneLayerStackTransform =
247 hasCloneLayerStackTransform ? std::make_optional<ui::Transform>() : std::nullopt;
248 if (cloneLayerStackTransform) {
249 status = readTransform(parcel, /*byRef*/ *cloneLayerStackTransform);
250 if (status != OK) {
251 return status;
252 }
253 }
254
Prabir Pradhan032141e2022-02-15 05:30:01 -0800255 return OK;
Robert Carr1cc78672018-07-31 14:25:57 -0700256}
257
chaviw3277faf2021-05-19 16:45:23 -0500258WindowInfoHandle::WindowInfoHandle() {}
Chris Ye0783e992020-06-02 21:34:49 -0700259
chaviw3277faf2021-05-19 16:45:23 -0500260WindowInfoHandle::~WindowInfoHandle() {}
Chris Ye0783e992020-06-02 21:34:49 -0700261
chaviw3277faf2021-05-19 16:45:23 -0500262WindowInfoHandle::WindowInfoHandle(const WindowInfoHandle& other) : mInfo(other.mInfo) {}
Chris Ye0783e992020-06-02 21:34:49 -0700263
chaviw3277faf2021-05-19 16:45:23 -0500264WindowInfoHandle::WindowInfoHandle(const WindowInfo& other) : mInfo(other) {}
Chris Ye0783e992020-06-02 21:34:49 -0700265
chaviw3277faf2021-05-19 16:45:23 -0500266status_t WindowInfoHandle::writeToParcel(android::Parcel* parcel) const {
Chris Ye0783e992020-06-02 21:34:49 -0700267 return mInfo.writeToParcel(parcel);
Robert Carr3720ed02018-08-08 16:08:27 -0700268}
269
chaviw3277faf2021-05-19 16:45:23 -0500270status_t WindowInfoHandle::readFromParcel(const android::Parcel* parcel) {
Chris Ye0783e992020-06-02 21:34:49 -0700271 return mInfo.readFromParcel(parcel);
Robert Carr3720ed02018-08-08 16:08:27 -0700272}
273
chaviw3277faf2021-05-19 16:45:23 -0500274void WindowInfoHandle::releaseChannel() {
Robert Carr5c8a0262018-10-03 16:30:44 -0700275 mInfo.token.clear();
Robert Carr3720ed02018-08-08 16:08:27 -0700276}
277
chaviw3277faf2021-05-19 16:45:23 -0500278sp<IBinder> WindowInfoHandle::getToken() const {
Robert Carr5c8a0262018-10-03 16:30:44 -0700279 return mInfo.token;
Robert Carr3720ed02018-08-08 16:08:27 -0700280}
281
chaviw3277faf2021-05-19 16:45:23 -0500282void WindowInfoHandle::updateFrom(sp<WindowInfoHandle> handle) {
Garfield Tanbd0fbcd2018-11-30 12:45:03 -0800283 mInfo = handle->mInfo;
284}
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -0800285
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800286std::ostream& operator<<(std::ostream& out, const WindowInfo& info) {
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -0800287 out << "name=" << info.name << ", id=" << info.id << ", displayId=" << info.displayId
288 << ", inputConfig=" << info.inputConfig.string() << ", alpha=" << info.alpha << ", frame=["
289 << info.frame.left << "," << info.frame.top << "][" << info.frame.right << ","
290 << info.frame.bottom << "], globalScale=" << info.globalScaleFactor
291 << ", applicationInfo.name=" << info.applicationInfo.name
292 << ", applicationInfo.token=" << info.applicationInfo.token
293 << ", touchableRegion=" << info.touchableRegion << ", ownerPid=" << info.ownerPid.toString()
294 << ", ownerUid=" << info.ownerUid.toString() << ", dispatchingTimeout="
295 << std::chrono::duration_cast<std::chrono::milliseconds>(info.dispatchingTimeout).count()
296 << "ms, token=" << info.token.get()
Vishnu Nairaa207702024-02-23 22:22:18 +0000297 << ", touchOcclusionMode=" << ftl::enum_string(info.touchOcclusionMode);
298 if (info.canOccludePresentation) out << ", canOccludePresentation";
299 std::string transform;
300 info.transform.dump(transform, "transform", " ");
301 out << "\n" << transform;
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -0800302 return out;
303}
304
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800305std::ostream& operator<<(std::ostream& out, const WindowInfoHandle& window) {
306 const WindowInfo& info = *window.getInfo();
307 out << info;
308 return out;
309}
310
chaviw3277faf2021-05-19 16:45:23 -0500311} // namespace android::gui