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 std::atomic<uint32_t> LayerCreationArgs::sInternalSequence{1};
27 
getInternalLayerId(uint32_t id)28 uint32_t LayerCreationArgs::getInternalLayerId(uint32_t id) {
29     return id | INTERNAL_LAYER_PREFIX;
30 }
31 
LayerCreationArgs(SurfaceFlinger * flinger,sp<Client> client,std::string name,uint32_t flags,gui::LayerMetadata metadataArg,std::optional<uint32_t> id,bool internalLayer)32 LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
33                                      uint32_t flags, gui::LayerMetadata metadataArg,
34                                      std::optional<uint32_t> id, bool internalLayer)
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 
52     if (internalLayer) {
53         sequence = id.value_or(getInternalLayerId(sInternalSequence++));
54     } else if (id) {
55         sequence = *id;
56         sSequence = *id + 1;
57     } else {
58         sequence = sSequence++;
59         if (sequence >= INTERNAL_LAYER_PREFIX) {
60             sSequence = 1;
61             ALOGW("Layer sequence id rolled over.");
62             sequence = sSequence++;
63         }
64     }
65 }
66 
LayerCreationArgs(std::optional<uint32_t> id,bool internalLayer)67 LayerCreationArgs::LayerCreationArgs(std::optional<uint32_t> id, bool internalLayer)
68       : LayerCreationArgs(nullptr, nullptr, /*name=*/"", /*flags=*/0, /*metadata=*/{}, id,
69                           internalLayer) {}
70 
fromOtherArgs(const LayerCreationArgs & other)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 }
75 
getDebugString() const76 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 
95 } // namespace android::surfaceflinger
96