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