blob: 8ba6cb9ba7ae954120a93e1dfd401814e996aada [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{
50 const size_t count = mLayers.size();
51 for (size_t i=0 ; i<count ; i++) {
Robert Carr9524cb32017-02-13 11:32:32 -080052 sp<Layer> l = mLayers.valueAt(i).promote();
53 if (l != nullptr) {
54 mFlinger->removeLayer(l);
55 }
Mathias Agopiandb403e82012-06-18 16:47:56 -070056 }
57}
58
Robert Carr1db73f62016-12-21 12:58:51 -080059void Client::setParentLayer(const sp<Layer>& parentLayer) {
Chia-I Wuf456f322017-06-15 14:01:18 -070060 Mutex::Autolock _l(mLock);
Robert Carr1db73f62016-12-21 12:58:51 -080061 mParentLayer = parentLayer;
62}
63
Chia-I Wuf456f322017-06-15 14:01:18 -070064sp<Layer> Client::getParentLayer(bool* outParentDied) const {
65 Mutex::Autolock _l(mLock);
66 sp<Layer> parent = mParentLayer.promote();
67 if (outParentDied != nullptr) {
68 *outParentDied = (mParentLayer != nullptr && parent == nullptr);
69 }
70 return parent;
71}
72
Mathias Agopiandb403e82012-06-18 16:47:56 -070073status_t Client::initCheck() const {
74 return NO_ERROR;
75}
76
Mathias Agopian13127d82013-03-05 17:47:11 -080077void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070078{
79 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080080 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070081}
82
Mathias Agopian13127d82013-03-05 17:47:11 -080083void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070084{
85 Mutex::Autolock _l(mLock);
86 // we do a linear search here, because this doesn't happen often
87 const size_t count = mLayers.size();
88 for (size_t i=0 ; i<count ; i++) {
89 if (mLayers.valueAt(i) == layer) {
90 mLayers.removeItemsAt(i, 1);
91 break;
92 }
93 }
94}
Mathias Agopian13127d82013-03-05 17:47:11 -080095sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070096{
97 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080098 sp<Layer> lbc;
99 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -0700100 if (layer != 0) {
101 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -0800102 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -0700103 }
104 return lbc;
105}
106
107
108status_t Client::onTransact(
109 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
110{
111 // these must be checked
112 IPCThreadState* ipc = IPCThreadState::self();
113 const int pid = ipc->getCallingPid();
114 const int uid = ipc->getCallingUid();
115 const int self_pid = getpid();
Robert Carr1db73f62016-12-21 12:58:51 -0800116 // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT
117 // uid we require the sAccessSurfaceFlinger permission.
118 // We grant an exception in the case that the Client has a "parent layer", as its
119 // effects will be scoped to that layer.
120 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)
Chia-I Wuf456f322017-06-15 14:01:18 -0700121 && (getParentLayer() == nullptr)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700122 // we're called from a different process, do the real check
123 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
124 {
125 ALOGE("Permission Denial: "
Robert Carr1db73f62016-12-21 12:58:51 -0800126 "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700127 return PERMISSION_DENIED;
128 }
129 }
130 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
131}
132
133
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700134status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700135 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500137 const sp<IBinder>& parentHandle, uint32_t windowType, uint32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138 sp<IBinder>* handle,
139 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700140{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700141 sp<Layer> parent = nullptr;
142 if (parentHandle != nullptr) {
143 parent = getLayerUser(parentHandle);
144 if (parent == nullptr) {
145 return NAME_NOT_FOUND;
146 }
147 }
Chia-I Wuf456f322017-06-15 14:01:18 -0700148 if (parent == nullptr) {
149 bool parentDied;
150 parent = getParentLayer(&parentDied);
Robert Carr1db73f62016-12-21 12:58:51 -0800151 // If we had a parent, but it died, we've lost all
152 // our capabilities.
Chia-I Wuf456f322017-06-15 14:01:18 -0700153 if (parentDied) {
Robert Carr1db73f62016-12-21 12:58:51 -0800154 return NAME_NOT_FOUND;
155 }
156 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700157
Mathias Agopiandb403e82012-06-18 16:47:56 -0700158 /*
159 * createSurface must be called from the GL thread so that it can
160 * have access to the GL context.
161 */
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700162 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700163 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700164 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700165 sp<IBinder>* handle;
166 sp<IGraphicBufferProducer>* gbp;
167 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700168 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700169 uint32_t w, h;
170 PixelFormat format;
171 uint32_t flags;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700172 sp<Layer>* parent;
Albert Chaulk479c60c2017-01-27 14:21:34 -0500173 uint32_t windowType;
174 uint32_t ownerUid;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700175 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700176 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700177 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700178 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500179 sp<IBinder>* handle, uint32_t windowType, uint32_t ownerUid,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700180 sp<IGraphicBufferProducer>* gbp,
181 sp<Layer>* parent)
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700182 : flinger(flinger), client(client),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700183 handle(handle), gbp(gbp), result(NO_ERROR),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700184 name(name), w(w), h(h), format(format), flags(flags),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500185 parent(parent), windowType(windowType), ownerUid(ownerUid) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700186 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700187 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700188 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700189 result = flinger->createLayer(name, client, w, h, format, flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500190 windowType, ownerUid, handle, gbp, parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700191 return true;
192 }
193 };
194
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700195 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500196 name, this, w, h, format, flags, handle,
197 windowType, ownerUid, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700198 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700199 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700200}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800201
202status_t Client::destroySurface(const sp<IBinder>& handle) {
203 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700204}
205
Svetoslavd85084b2014-03-20 10:28:31 -0700206status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
207 sp<Layer> layer = getLayerUser(handle);
208 if (layer == NULL) {
209 return NAME_NOT_FOUND;
210 }
211 layer->clearFrameStats();
212 return NO_ERROR;
213}
214
215status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
216 sp<Layer> layer = getLayerUser(handle);
217 if (layer == NULL) {
218 return NAME_NOT_FOUND;
219 }
220 layer->getFrameStats(outStats);
221 return NO_ERROR;
222}
223
Mathias Agopiandb403e82012-06-18 16:47:56 -0700224// ---------------------------------------------------------------------------
225}; // namespace android