blob: 0c9f0e21e12caf6aa7ce49967cb99e4bc5a5cfbf [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 Carr1db73f62016-12-21 12:58:51 -080068void Client::setParentLayer(const sp<Layer>& parentLayer) {
Chia-I Wuf456f322017-06-15 14:01:18 -070069 Mutex::Autolock _l(mLock);
Robert Carr1db73f62016-12-21 12:58:51 -080070 mParentLayer = parentLayer;
71}
72
Chia-I Wuf456f322017-06-15 14:01:18 -070073sp<Layer> Client::getParentLayer(bool* outParentDied) const {
74 Mutex::Autolock _l(mLock);
75 sp<Layer> parent = mParentLayer.promote();
76 if (outParentDied != nullptr) {
77 *outParentDied = (mParentLayer != nullptr && parent == nullptr);
78 }
79 return parent;
80}
81
Mathias Agopiandb403e82012-06-18 16:47:56 -070082status_t Client::initCheck() const {
83 return NO_ERROR;
84}
85
Mathias Agopian13127d82013-03-05 17:47:11 -080086void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070087{
88 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080089 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070090}
91
Mathias Agopian13127d82013-03-05 17:47:11 -080092void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070093{
94 Mutex::Autolock _l(mLock);
95 // we do a linear search here, because this doesn't happen often
96 const size_t count = mLayers.size();
97 for (size_t i=0 ; i<count ; i++) {
98 if (mLayers.valueAt(i) == layer) {
99 mLayers.removeItemsAt(i, 1);
100 break;
101 }
102 }
103}
Mathias Agopian13127d82013-03-05 17:47:11 -0800104sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -0700105{
106 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -0800107 sp<Layer> lbc;
108 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -0700109 if (layer != 0) {
110 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -0800111 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -0700112 }
113 return lbc;
114}
115
116
117status_t Client::onTransact(
118 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
119{
120 // these must be checked
121 IPCThreadState* ipc = IPCThreadState::self();
122 const int pid = ipc->getCallingPid();
123 const int uid = ipc->getCallingUid();
124 const int self_pid = getpid();
Robert Carr1db73f62016-12-21 12:58:51 -0800125 // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT
126 // uid we require the sAccessSurfaceFlinger permission.
127 // We grant an exception in the case that the Client has a "parent layer", as its
128 // effects will be scoped to that layer.
129 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)
Chia-I Wuf456f322017-06-15 14:01:18 -0700130 && (getParentLayer() == nullptr)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700131 // we're called from a different process, do the real check
132 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
133 {
134 ALOGE("Permission Denial: "
Robert Carr1db73f62016-12-21 12:58:51 -0800135 "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700136 return PERMISSION_DENIED;
137 }
138 }
139 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
140}
141
142
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700143status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700144 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500146 const sp<IBinder>& parentHandle, uint32_t windowType, uint32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700147 sp<IBinder>* handle,
148 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700150 sp<Layer> parent = nullptr;
151 if (parentHandle != nullptr) {
chaviw161410b02017-07-27 10:46:08 -0700152 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
153 parent = layerHandle->owner.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700154 if (parent == nullptr) {
155 return NAME_NOT_FOUND;
156 }
157 }
Chia-I Wuf456f322017-06-15 14:01:18 -0700158 if (parent == nullptr) {
159 bool parentDied;
160 parent = getParentLayer(&parentDied);
Robert Carr1db73f62016-12-21 12:58:51 -0800161 // If we had a parent, but it died, we've lost all
162 // our capabilities.
Chia-I Wuf456f322017-06-15 14:01:18 -0700163 if (parentDied) {
Robert Carr1db73f62016-12-21 12:58:51 -0800164 return NAME_NOT_FOUND;
165 }
166 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700167
Mathias Agopiandb403e82012-06-18 16:47:56 -0700168 /*
169 * createSurface must be called from the GL thread so that it can
170 * have access to the GL context.
171 */
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700172 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700173 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700174 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700175 sp<IBinder>* handle;
176 sp<IGraphicBufferProducer>* gbp;
177 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700178 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700179 uint32_t w, h;
180 PixelFormat format;
181 uint32_t flags;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700182 sp<Layer>* parent;
Albert Chaulk479c60c2017-01-27 14:21:34 -0500183 uint32_t windowType;
184 uint32_t ownerUid;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700185 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700186 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700187 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700188 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500189 sp<IBinder>* handle, uint32_t windowType, uint32_t ownerUid,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700190 sp<IGraphicBufferProducer>* gbp,
191 sp<Layer>* parent)
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700192 : flinger(flinger), client(client),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700193 handle(handle), gbp(gbp), result(NO_ERROR),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700194 name(name), w(w), h(h), format(format), flags(flags),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500195 parent(parent), windowType(windowType), ownerUid(ownerUid) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700196 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700197 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700198 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700199 result = flinger->createLayer(name, client, w, h, format, flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500200 windowType, ownerUid, handle, gbp, parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700201 return true;
202 }
203 };
204
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700205 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500206 name, this, w, h, format, flags, handle,
207 windowType, ownerUid, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700208 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700209 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700210}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800211
212status_t Client::destroySurface(const sp<IBinder>& handle) {
213 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700214}
215
Svetoslavd85084b2014-03-20 10:28:31 -0700216status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
217 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800218 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700219 return NAME_NOT_FOUND;
220 }
221 layer->clearFrameStats();
222 return NO_ERROR;
223}
224
225status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
226 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800227 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700228 return NAME_NOT_FOUND;
229 }
230 layer->getFrameStats(outStats);
231 return NO_ERROR;
232}
233
Mathias Agopiandb403e82012-06-18 16:47:56 -0700234// ---------------------------------------------------------------------------
235}; // namespace android