blob: 6af352c9f4eaf2c3394ba0c974ca38a9f27cb0bf [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};
26
Vishnu Naird47bcee2023-02-24 18:08:51 +000027uint32_t LayerCreationArgs::getInternalLayerId(uint32_t id) {
28 return id | INTERNAL_LAYER_PREFIX;
29}
30
Vishnu Naircb8be502022-10-12 19:03:23 +000031LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
32 uint32_t flags, gui::LayerMetadata metadataArg,
Vishnu Naird47bcee2023-02-24 18:08:51 +000033 std::optional<uint32_t> id, bool internalLayer)
Vishnu Naircb8be502022-10-12 19:03:23 +000034 : flinger(flinger),
35 client(std::move(client)),
36 name(std::move(name)),
37 flags(flags),
38 metadata(std::move(metadataArg)) {
39 IPCThreadState* ipc = IPCThreadState::self();
40 ownerPid = ipc->getCallingPid();
41 uid_t callingUid = ipc->getCallingUid();
42 metadata.setInt32(gui::METADATA_CALLING_UID, static_cast<int32_t>(callingUid));
43 ownerUid = callingUid;
44 if (ownerUid == AID_GRAPHICS || ownerUid == AID_SYSTEM) {
45 // System can override the calling UID/PID since it can create layers on behalf of apps.
46 ownerPid = metadata.getInt32(gui::METADATA_OWNER_PID, ownerPid);
47 ownerUid = static_cast<uid_t>(
48 metadata.getInt32(gui::METADATA_OWNER_UID, static_cast<int32_t>(ownerUid)));
49 }
50
51 if (id) {
52 sequence = *id;
Vishnu Naird47bcee2023-02-24 18:08:51 +000053 if (internalLayer) {
54 sequence = getInternalLayerId(*id);
55 } else {
56 sSequence = *id + 1;
57 }
Vishnu Naircb8be502022-10-12 19:03:23 +000058 } else {
59 sequence = sSequence++;
Vishnu Naird47bcee2023-02-24 18:08:51 +000060 if (sequence >= INTERNAL_LAYER_PREFIX) {
61 sSequence = 1;
Vishnu Naircb8be502022-10-12 19:03:23 +000062 ALOGW("Layer sequence id rolled over.");
63 sequence = sSequence++;
64 }
65 }
66}
67
Vishnu Nair1391de22023-03-05 19:56:14 -080068LayerCreationArgs::LayerCreationArgs(std::optional<uint32_t> id, bool internalLayer)
69 : LayerCreationArgs(nullptr, nullptr, /*name=*/"", /*flags=*/0, /*metadata=*/{}, id,
70 internalLayer) {}
71
Vishnu Nair81750622023-03-08 15:02:06 -080072LayerCreationArgs LayerCreationArgs::fromOtherArgs(const LayerCreationArgs& other) {
73 // returns a new instance of LayerCreationArgs with a unique id.
74 return LayerCreationArgs(other.flinger, other.client, other.name, other.flags, other.metadata);
75}
Vishnu Naircb8be502022-10-12 19:03:23 +000076
Vishnu Nair20e1f962023-03-29 15:58:34 -070077std::string LayerCreationArgs::getDebugString() const {
78 std::stringstream stream;
79 stream << "LayerCreationArgs{" << name << "[" << sequence << "] flags=" << flags
80 << " pid=" << ownerPid << " uid=" << ownerUid;
81 if (addToRoot) {
82 stream << " addToRoot=" << addToRoot;
83 }
84 if (parentId != UNASSIGNED_LAYER_ID) {
85 stream << " parentId=" << parentId;
86 }
87 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
88 stream << " layerIdToMirror=" << layerIdToMirror;
89 }
90 if (layerStackToMirror != ui::INVALID_LAYER_STACK) {
91 stream << " layerStackToMirror=" << layerStackToMirror.id;
92 }
93 return stream.str();
94}
95
Vishnu Naircb8be502022-10-12 19:03:23 +000096} // namespace android::surfaceflinger