blob: 9ddae2b4dc89f766474f0b60f10434c9368576f1 [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) {
60 mParentLayer = parentLayer;
61}
62
Mathias Agopiandb403e82012-06-18 16:47:56 -070063status_t Client::initCheck() const {
64 return NO_ERROR;
65}
66
Mathias Agopian13127d82013-03-05 17:47:11 -080067void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070068{
69 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080070 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070071}
72
Mathias Agopian13127d82013-03-05 17:47:11 -080073void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070074{
75 Mutex::Autolock _l(mLock);
76 // we do a linear search here, because this doesn't happen often
77 const size_t count = mLayers.size();
78 for (size_t i=0 ; i<count ; i++) {
79 if (mLayers.valueAt(i) == layer) {
80 mLayers.removeItemsAt(i, 1);
81 break;
82 }
83 }
84}
Mathias Agopian13127d82013-03-05 17:47:11 -080085sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070086{
87 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080088 sp<Layer> lbc;
89 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070090 if (layer != 0) {
91 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080092 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070093 }
94 return lbc;
95}
96
97
98status_t Client::onTransact(
99 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
100{
101 // these must be checked
102 IPCThreadState* ipc = IPCThreadState::self();
103 const int pid = ipc->getCallingPid();
104 const int uid = ipc->getCallingUid();
105 const int self_pid = getpid();
Robert Carr1db73f62016-12-21 12:58:51 -0800106 // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT
107 // uid we require the sAccessSurfaceFlinger permission.
108 // We grant an exception in the case that the Client has a "parent layer", as its
109 // effects will be scoped to that layer.
110 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)
111 && (mParentLayer.promote() == nullptr)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700112 // we're called from a different process, do the real check
113 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
114 {
115 ALOGE("Permission Denial: "
Robert Carr1db73f62016-12-21 12:58:51 -0800116 "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700117 return PERMISSION_DENIED;
118 }
119 }
120 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
121}
122
123
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700125 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500127 const sp<IBinder>& parentHandle, uint32_t windowType, uint32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128 sp<IBinder>* handle,
129 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700130{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700131 sp<Layer> parent = nullptr;
132 if (parentHandle != nullptr) {
133 parent = getLayerUser(parentHandle);
134 if (parent == nullptr) {
135 return NAME_NOT_FOUND;
136 }
137 }
Robert Carr1db73f62016-12-21 12:58:51 -0800138 if (parent == nullptr && mParentLayer != nullptr) {
139 parent = mParentLayer.promote();
140 // If we had a parent, but it died, we've lost all
141 // our capabilities.
142 if (parent == nullptr) {
143 return NAME_NOT_FOUND;
144 }
145 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700146
Mathias Agopiandb403e82012-06-18 16:47:56 -0700147 /*
148 * createSurface must be called from the GL thread so that it can
149 * have access to the GL context.
150 */
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700151 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700152 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700153 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700154 sp<IBinder>* handle;
155 sp<IGraphicBufferProducer>* gbp;
156 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700157 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700158 uint32_t w, h;
159 PixelFormat format;
160 uint32_t flags;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700161 sp<Layer>* parent;
Albert Chaulk479c60c2017-01-27 14:21:34 -0500162 uint32_t windowType;
163 uint32_t ownerUid;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700164 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700165 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700166 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700167 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500168 sp<IBinder>* handle, uint32_t windowType, uint32_t ownerUid,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700169 sp<IGraphicBufferProducer>* gbp,
170 sp<Layer>* parent)
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171 : flinger(flinger), client(client),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700172 handle(handle), gbp(gbp), result(NO_ERROR),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700173 name(name), w(w), h(h), format(format), flags(flags),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500174 parent(parent), windowType(windowType), ownerUid(ownerUid) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700175 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700176 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700177 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700178 result = flinger->createLayer(name, client, w, h, format, flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500179 windowType, ownerUid, handle, gbp, parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700180 return true;
181 }
182 };
183
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700184 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Albert Chaulk479c60c2017-01-27 14:21:34 -0500185 name, this, w, h, format, flags, handle,
186 windowType, ownerUid, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700187 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700188 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700189}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800190
191status_t Client::destroySurface(const sp<IBinder>& handle) {
192 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700193}
194
Svetoslavd85084b2014-03-20 10:28:31 -0700195status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
196 sp<Layer> layer = getLayerUser(handle);
197 if (layer == NULL) {
198 return NAME_NOT_FOUND;
199 }
200 layer->clearFrameStats();
201 return NO_ERROR;
202}
203
204status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
205 sp<Layer> layer = getLayerUser(handle);
206 if (layer == NULL) {
207 return NAME_NOT_FOUND;
208 }
209 layer->getFrameStats(outStats);
210 return NO_ERROR;
211}
212
Robert Carr367c5682016-06-20 11:55:28 -0700213status_t Client::getTransformToDisplayInverse(const sp<IBinder>& handle,
214 bool* outTransformToDisplayInverse) const {
215 sp<Layer> layer = getLayerUser(handle);
216 if (layer == NULL) {
217 return NAME_NOT_FOUND;
218 }
219 *outTransformToDisplayInverse = layer->getTransformToDisplayInverse();
220 return NO_ERROR;
221}
222
Mathias Agopiandb403e82012-06-18 16:47:56 -0700223// ---------------------------------------------------------------------------
224}; // namespace android