blob: 0b59147c5a2c6ac559a0a21d848ae41ce42856ac [file] [log] [blame]
Mathias Agopiandb403e82012-06-18 16:47:56 -07001/*
2 * Copyright (C) 2012 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 <stdint.h>
18#include <sys/types.h>
19
20#include <binder/PermissionCache.h>
Mathias Agopian4f4f0942013-08-19 17:26:18 -070021#include <binder/IPCThreadState.h>
Mathias Agopiandb403e82012-06-18 16:47:56 -070022
23#include <private/android_filesystem_config.h>
24
25#include "Client.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070027#include "SurfaceFlinger.h"
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
33const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
34
35// ---------------------------------------------------------------------------
36
37Client::Client(const sp<SurfaceFlinger>& flinger)
Robert Carr1db73f62016-12-21 12:58:51 -080038 : Client(flinger, nullptr)
39{
40}
41
42Client::Client(const sp<SurfaceFlinger>& flinger, const sp<Layer>& parentLayer)
43 : mFlinger(flinger),
44 mParentLayer(parentLayer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070045{
46}
47
48Client::~Client()
49{
Robert Carr5e8e1f02018-02-20 12:34:15 -080050 // We need to post a message to remove our remaining layers rather than
51 // do so directly by acquiring the SurfaceFlinger lock. If we were to
52 // attempt to directly call the lock it becomes effectively impossible
53 // to use sp<Client> while holding the SF lock as descoping it could
54 // then trigger a dead-lock.
55
Mathias Agopiandb403e82012-06-18 16:47:56 -070056 const size_t count = mLayers.size();
57 for (size_t i=0 ; i<count ; i++) {
Robert Carr9524cb32017-02-13 11:32:32 -080058 sp<Layer> l = mLayers.valueAt(i).promote();
Robert Carr5e8e1f02018-02-20 12:34:15 -080059 if (l == nullptr) {
60 continue;
Robert Carr9524cb32017-02-13 11:32:32 -080061 }
Robert Carr5e8e1f02018-02-20 12:34:15 -080062 mFlinger->postMessageAsync(new LambdaMessage([flinger = mFlinger, l]() {
63 flinger->removeLayer(l);
64 }));
Mathias Agopiandb403e82012-06-18 16:47:56 -070065 }
66}
67
Robert Carr94c7d3d2018-04-24 12:30:47 -070068void Client::updateParent(const sp<Layer>& parentLayer) {
Chia-I Wuf456f322017-06-15 14:01:18 -070069 Mutex::Autolock _l(mLock);
Robert Carr94c7d3d2018-04-24 12:30:47 -070070
71 // If we didn't ever have a parent, then we must instead be
72 // relying on permissions and we never need a parent.
73 if (mParentLayer != nullptr) {
74 mParentLayer = parentLayer;
75 }
Robert Carr1db73f62016-12-21 12:58:51 -080076}
77
Chia-I Wuf456f322017-06-15 14:01:18 -070078sp<Layer> Client::getParentLayer(bool* outParentDied) const {
79 Mutex::Autolock _l(mLock);
80 sp<Layer> parent = mParentLayer.promote();
81 if (outParentDied != nullptr) {
82 *outParentDied = (mParentLayer != nullptr && parent == nullptr);
83 }
84 return parent;
85}
86
Mathias Agopiandb403e82012-06-18 16:47:56 -070087status_t Client::initCheck() const {
88 return NO_ERROR;
89}
90
Mathias Agopian13127d82013-03-05 17:47:11 -080091void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070092{
93 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080094 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070095}
96
Mathias Agopian13127d82013-03-05 17:47:11 -080097void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070098{
99 Mutex::Autolock _l(mLock);
100 // we do a linear search here, because this doesn't happen often
101 const size_t count = mLayers.size();
102 for (size_t i=0 ; i<count ; i++) {
103 if (mLayers.valueAt(i) == layer) {
104 mLayers.removeItemsAt(i, 1);
105 break;
106 }
107 }
108}
Mathias Agopian13127d82013-03-05 17:47:11 -0800109sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -0700110{
111 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -0800112 sp<Layer> lbc;
113 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -0700114 if (layer != 0) {
115 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -0800116 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -0700117 }
118 return lbc;
119}
120
121
122status_t Client::onTransact(
123 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
124{
125 // these must be checked
126 IPCThreadState* ipc = IPCThreadState::self();
127 const int pid = ipc->getCallingPid();
128 const int uid = ipc->getCallingUid();
129 const int self_pid = getpid();
Robert Carr1db73f62016-12-21 12:58:51 -0800130 // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT
131 // uid we require the sAccessSurfaceFlinger permission.
132 // We grant an exception in the case that the Client has a "parent layer", as its
133 // effects will be scoped to that layer.
134 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)
Chia-I Wuf456f322017-06-15 14:01:18 -0700135 && (getParentLayer() == nullptr)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700136 // we're called from a different process, do the real check
137 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
138 {
139 ALOGE("Permission Denial: "
Robert Carr1db73f62016-12-21 12:58:51 -0800140 "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700141 return PERMISSION_DENIED;
142 }
143 }
144 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
145}
146
147
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700150 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
rongliuccd34842018-03-14 12:26:23 -0700151 const sp<IBinder>& parentHandle, int32_t windowType, int32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700152 sp<IBinder>* handle,
153 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700154{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700155 sp<Layer> parent = nullptr;
156 if (parentHandle != nullptr) {
chaviw161410b02017-07-27 10:46:08 -0700157 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
158 parent = layerHandle->owner.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700159 if (parent == nullptr) {
160 return NAME_NOT_FOUND;
161 }
162 }
Chia-I Wuf456f322017-06-15 14:01:18 -0700163 if (parent == nullptr) {
164 bool parentDied;
165 parent = getParentLayer(&parentDied);
Robert Carr1db73f62016-12-21 12:58:51 -0800166 // If we had a parent, but it died, we've lost all
167 // our capabilities.
Chia-I Wuf456f322017-06-15 14:01:18 -0700168 if (parentDied) {
Robert Carr1db73f62016-12-21 12:58:51 -0800169 return NAME_NOT_FOUND;
170 }
171 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700172
Dan Stoza436ccf32018-06-21 12:10:12 -0700173 return mFlinger->createLayer(name, this, w, h, format, flags, windowType,
174 ownerUid, handle, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700175}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800176
177status_t Client::destroySurface(const sp<IBinder>& handle) {
178 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700179}
180
Svetoslavd85084b2014-03-20 10:28:31 -0700181status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
182 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800183 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700184 return NAME_NOT_FOUND;
185 }
186 layer->clearFrameStats();
187 return NO_ERROR;
188}
189
190status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
191 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800192 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700193 return NAME_NOT_FOUND;
194 }
195 layer->getFrameStats(outStats);
196 return NO_ERROR;
197}
198
Mathias Agopiandb403e82012-06-18 16:47:56 -0700199// ---------------------------------------------------------------------------
200}; // namespace android