blob: f63784e18ba1e872eeb689f62bbe9bfa685dbf6d [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++) {
Dan Stoza22851c32016-08-09 13:21:03 -070052 mFlinger->removeLayer(mLayers.valueAt(i));
Mathias Agopiandb403e82012-06-18 16:47:56 -070053 }
54}
55
Robert Carr1db73f62016-12-21 12:58:51 -080056void Client::setParentLayer(const sp<Layer>& parentLayer) {
57 mParentLayer = parentLayer;
58}
59
Mathias Agopiandb403e82012-06-18 16:47:56 -070060status_t Client::initCheck() const {
61 return NO_ERROR;
62}
63
Mathias Agopian13127d82013-03-05 17:47:11 -080064void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070065{
66 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080067 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070068}
69
Mathias Agopian13127d82013-03-05 17:47:11 -080070void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070071{
72 Mutex::Autolock _l(mLock);
73 // we do a linear search here, because this doesn't happen often
74 const size_t count = mLayers.size();
75 for (size_t i=0 ; i<count ; i++) {
76 if (mLayers.valueAt(i) == layer) {
77 mLayers.removeItemsAt(i, 1);
78 break;
79 }
80 }
81}
Mathias Agopian13127d82013-03-05 17:47:11 -080082sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070083{
84 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080085 sp<Layer> lbc;
86 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070087 if (layer != 0) {
88 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080089 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070090 }
91 return lbc;
92}
93
94
95status_t Client::onTransact(
96 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
97{
98 // these must be checked
99 IPCThreadState* ipc = IPCThreadState::self();
100 const int pid = ipc->getCallingPid();
101 const int uid = ipc->getCallingUid();
102 const int self_pid = getpid();
Robert Carr1db73f62016-12-21 12:58:51 -0800103 // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT
104 // uid we require the sAccessSurfaceFlinger permission.
105 // We grant an exception in the case that the Client has a "parent layer", as its
106 // effects will be scoped to that layer.
107 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)
108 && (mParentLayer.promote() == nullptr)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700109 // we're called from a different process, do the real check
110 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
111 {
112 ALOGE("Permission Denial: "
Robert Carr1db73f62016-12-21 12:58:51 -0800113 "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700114 return PERMISSION_DENIED;
115 }
116 }
117 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
118}
119
120
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700122 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700124 const sp<IBinder>& parentHandle,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 sp<IBinder>* handle,
126 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700127{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700128 sp<Layer> parent = nullptr;
129 if (parentHandle != nullptr) {
130 parent = getLayerUser(parentHandle);
131 if (parent == nullptr) {
132 return NAME_NOT_FOUND;
133 }
134 }
Robert Carr1db73f62016-12-21 12:58:51 -0800135 if (parent == nullptr && mParentLayer != nullptr) {
136 parent = mParentLayer.promote();
137 // If we had a parent, but it died, we've lost all
138 // our capabilities.
139 if (parent == nullptr) {
140 return NAME_NOT_FOUND;
141 }
142 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700143
Mathias Agopiandb403e82012-06-18 16:47:56 -0700144 /*
145 * createSurface must be called from the GL thread so that it can
146 * have access to the GL context.
147 */
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700148 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700150 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700151 sp<IBinder>* handle;
152 sp<IGraphicBufferProducer>* gbp;
153 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700154 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700155 uint32_t w, h;
156 PixelFormat format;
157 uint32_t flags;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700158 sp<Layer>* parent;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700159 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700160 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700161 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700162 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
163 sp<IBinder>* handle,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700164 sp<IGraphicBufferProducer>* gbp,
165 sp<Layer>* parent)
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700166 : flinger(flinger), client(client),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700167 handle(handle), gbp(gbp), result(NO_ERROR),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700168 name(name), w(w), h(h), format(format), flags(flags),
169 parent(parent) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700170 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700171 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700172 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700173 result = flinger->createLayer(name, client, w, h, format, flags,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700174 handle, gbp, parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700175 return true;
176 }
177 };
178
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700179 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700180 name, this, w, h, format, flags, handle, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700181 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700182 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700183}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800184
185status_t Client::destroySurface(const sp<IBinder>& handle) {
186 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700187}
188
Svetoslavd85084b2014-03-20 10:28:31 -0700189status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
190 sp<Layer> layer = getLayerUser(handle);
191 if (layer == NULL) {
192 return NAME_NOT_FOUND;
193 }
194 layer->clearFrameStats();
195 return NO_ERROR;
196}
197
198status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
199 sp<Layer> layer = getLayerUser(handle);
200 if (layer == NULL) {
201 return NAME_NOT_FOUND;
202 }
203 layer->getFrameStats(outStats);
204 return NO_ERROR;
205}
206
Robert Carr367c5682016-06-20 11:55:28 -0700207status_t Client::getTransformToDisplayInverse(const sp<IBinder>& handle,
208 bool* outTransformToDisplayInverse) const {
209 sp<Layer> layer = getLayerUser(handle);
210 if (layer == NULL) {
211 return NAME_NOT_FOUND;
212 }
213 *outTransformToDisplayInverse = layer->getTransformToDisplayInverse();
214 return NO_ERROR;
215}
216
Mathias Agopiandb403e82012-06-18 16:47:56 -0700217// ---------------------------------------------------------------------------
218}; // namespace android