Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android::surfaceflinger { |
| 24 | |
| 25 | std::atomic<uint32_t> LayerCreationArgs::sSequence{1}; |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 26 | std::atomic<uint32_t> LayerCreationArgs::sInternalSequence{1}; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 27 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 28 | uint32_t LayerCreationArgs::getInternalLayerId(uint32_t id) { |
| 29 | return id | INTERNAL_LAYER_PREFIX; |
| 30 | } |
| 31 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 32 | LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name, |
| 33 | uint32_t flags, gui::LayerMetadata metadataArg, |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 34 | std::optional<uint32_t> id, bool internalLayer) |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 35 | : 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 Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 52 | if (internalLayer) { |
| 53 | sequence = getInternalLayerId(sInternalSequence++); |
| 54 | } else if (id) { |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 55 | sequence = *id; |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 56 | sSequence = *id + 1; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 57 | } else { |
| 58 | sequence = sSequence++; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 59 | if (sequence >= INTERNAL_LAYER_PREFIX) { |
| 60 | sSequence = 1; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 61 | ALOGW("Layer sequence id rolled over."); |
| 62 | sequence = sSequence++; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 67 | LayerCreationArgs::LayerCreationArgs(std::optional<uint32_t> id, bool internalLayer) |
| 68 | : LayerCreationArgs(nullptr, nullptr, /*name=*/"", /*flags=*/0, /*metadata=*/{}, id, |
| 69 | internalLayer) {} |
| 70 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 71 | LayerCreationArgs 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 Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 75 | |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 76 | std::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 Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 95 | } // namespace android::surfaceflinger |