blob: 97af445513a78a93a9364358605062c796d23b2d [file] [log] [blame]
Vishnu Naircb8be502022-10-12 19:03:23 +00001/*
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#include "LayerCreationArgs.h"
18#include <binder/IPCThreadState.h>
19#include <private/android_filesystem_config.h>
20#include "Client.h"
21#include "gui/LayerMetadata.h"
22
23namespace android::surfaceflinger {
24
25std::atomic<uint32_t> LayerCreationArgs::sSequence{1};
Vishnu Nair150065b2023-04-17 19:14:11 -070026std::atomic<uint32_t> LayerCreationArgs::sInternalSequence{1};
Vishnu Naircb8be502022-10-12 19:03:23 +000027
Vishnu Naird47bcee2023-02-24 18:08:51 +000028uint32_t LayerCreationArgs::getInternalLayerId(uint32_t id) {
29 return id | INTERNAL_LAYER_PREFIX;
30}
31
Vishnu Naircb8be502022-10-12 19:03:23 +000032LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
33 uint32_t flags, gui::LayerMetadata metadataArg,
Vishnu Naird47bcee2023-02-24 18:08:51 +000034 std::optional<uint32_t> id, bool internalLayer)
Vishnu Naircb8be502022-10-12 19:03:23 +000035 : flinger(flinger),
36 client(std::move(client)),
37 name(std::move(name)),
38 flags(flags),
39 metadata(std::move(metadataArg)) {
40 IPCThreadState* ipc = IPCThreadState::self();
41 ownerPid = ipc->getCallingPid();
42 uid_t callingUid = ipc->getCallingUid();
43 metadata.setInt32(gui::METADATA_CALLING_UID, static_cast<int32_t>(callingUid));
44 ownerUid = callingUid;
45 if (ownerUid == AID_GRAPHICS || ownerUid == AID_SYSTEM) {
46 // System can override the calling UID/PID since it can create layers on behalf of apps.
47 ownerPid = metadata.getInt32(gui::METADATA_OWNER_PID, ownerPid);
48 ownerUid = static_cast<uid_t>(
49 metadata.getInt32(gui::METADATA_OWNER_UID, static_cast<int32_t>(ownerUid)));
50 }
51
Vishnu Nair150065b2023-04-17 19:14:11 -070052 if (internalLayer) {
Vishnu Naire4af0952023-05-01 09:11:33 -070053 sequence = id.value_or(getInternalLayerId(sInternalSequence++));
Vishnu Nair150065b2023-04-17 19:14:11 -070054 } else if (id) {
Vishnu Naircb8be502022-10-12 19:03:23 +000055 sequence = *id;
Vishnu Nair150065b2023-04-17 19:14:11 -070056 sSequence = *id + 1;
Vishnu Naircb8be502022-10-12 19:03:23 +000057 } else {
58 sequence = sSequence++;
Vishnu Naird47bcee2023-02-24 18:08:51 +000059 if (sequence >= INTERNAL_LAYER_PREFIX) {
60 sSequence = 1;
Vishnu Naircb8be502022-10-12 19:03:23 +000061 ALOGW("Layer sequence id rolled over.");
62 sequence = sSequence++;
63 }
64 }
65}
66
Vishnu Nair1391de22023-03-05 19:56:14 -080067LayerCreationArgs::LayerCreationArgs(std::optional<uint32_t> id, bool internalLayer)
68 : LayerCreationArgs(nullptr, nullptr, /*name=*/"", /*flags=*/0, /*metadata=*/{}, id,
69 internalLayer) {}
70
Vishnu Nair81750622023-03-08 15:02:06 -080071LayerCreationArgs LayerCreationArgs::fromOtherArgs(const LayerCreationArgs& other) {
72 // returns a new instance of LayerCreationArgs with a unique id.
73 return LayerCreationArgs(other.flinger, other.client, other.name, other.flags, other.metadata);
74}
Vishnu Naircb8be502022-10-12 19:03:23 +000075
Vishnu Nair20e1f962023-03-29 15:58:34 -070076std::string LayerCreationArgs::getDebugString() const {
77 std::stringstream stream;
78 stream << "LayerCreationArgs{" << name << "[" << sequence << "] flags=" << flags
79 << " pid=" << ownerPid << " uid=" << ownerUid;
80 if (addToRoot) {
81 stream << " addToRoot=" << addToRoot;
82 }
83 if (parentId != UNASSIGNED_LAYER_ID) {
84 stream << " parentId=" << parentId;
85 }
86 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
87 stream << " layerIdToMirror=" << layerIdToMirror;
88 }
89 if (layerStackToMirror != ui::INVALID_LAYER_STACK) {
90 stream << " layerStackToMirror=" << layerStackToMirror.id;
91 }
92 return stream.str();
93}
94
Vishnu Naircb8be502022-10-12 19:03:23 +000095} // namespace android::surfaceflinger