blob: 7e04fda6def298fb49f736228b8e813da92461bb [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)
Mathias Agopianac9fa422013-02-11 16:40:36 -080038 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070039{
40}
41
42Client::~Client()
43{
44 const size_t count = mLayers.size();
45 for (size_t i=0 ; i<count ; i++) {
Dan Stoza22851c32016-08-09 13:21:03 -070046 mFlinger->removeLayer(mLayers.valueAt(i));
Mathias Agopiandb403e82012-06-18 16:47:56 -070047 }
48}
49
50status_t Client::initCheck() const {
51 return NO_ERROR;
52}
53
Mathias Agopian13127d82013-03-05 17:47:11 -080054void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070055{
56 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080057 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070058}
59
Mathias Agopian13127d82013-03-05 17:47:11 -080060void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070061{
62 Mutex::Autolock _l(mLock);
63 // we do a linear search here, because this doesn't happen often
64 const size_t count = mLayers.size();
65 for (size_t i=0 ; i<count ; i++) {
66 if (mLayers.valueAt(i) == layer) {
67 mLayers.removeItemsAt(i, 1);
68 break;
69 }
70 }
71}
Mathias Agopian13127d82013-03-05 17:47:11 -080072sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070073{
74 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080075 sp<Layer> lbc;
76 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070077 if (layer != 0) {
78 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080079 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070080 }
81 return lbc;
82}
83
84
85status_t Client::onTransact(
86 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
87{
88 // these must be checked
89 IPCThreadState* ipc = IPCThreadState::self();
90 const int pid = ipc->getCallingPid();
91 const int uid = ipc->getCallingUid();
92 const int self_pid = getpid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -070093 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -070094 // we're called from a different process, do the real check
95 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
96 {
97 ALOGE("Permission Denial: "
98 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
99 return PERMISSION_DENIED;
100 }
101 }
102 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
103}
104
105
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700107 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700109 const sp<IBinder>& parentHandle,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 sp<IBinder>* handle,
111 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700112{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700113 sp<Layer> parent = nullptr;
114 if (parentHandle != nullptr) {
115 parent = getLayerUser(parentHandle);
116 if (parent == nullptr) {
117 return NAME_NOT_FOUND;
118 }
119 }
120
Mathias Agopiandb403e82012-06-18 16:47:56 -0700121 /*
122 * createSurface must be called from the GL thread so that it can
123 * have access to the GL context.
124 */
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700125 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700126 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700127 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128 sp<IBinder>* handle;
129 sp<IGraphicBufferProducer>* gbp;
130 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700131 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700132 uint32_t w, h;
133 PixelFormat format;
134 uint32_t flags;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700135 sp<Layer>* parent;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700136 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700137 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700138 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
140 sp<IBinder>* handle,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700141 sp<IGraphicBufferProducer>* gbp,
142 sp<Layer>* parent)
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700143 : flinger(flinger), client(client),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700144 handle(handle), gbp(gbp), result(NO_ERROR),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700145 name(name), w(w), h(h), format(format), flags(flags),
146 parent(parent) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700147 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700150 result = flinger->createLayer(name, client, w, h, format, flags,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700151 handle, gbp, parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700152 return true;
153 }
154 };
155
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700156 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Robert Carr1f0a16a2016-10-24 16:27:39 -0700157 name, this, w, h, format, flags, handle, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700158 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700159 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700160}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800161
162status_t Client::destroySurface(const sp<IBinder>& handle) {
163 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700164}
165
Svetoslavd85084b2014-03-20 10:28:31 -0700166status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
167 sp<Layer> layer = getLayerUser(handle);
168 if (layer == NULL) {
169 return NAME_NOT_FOUND;
170 }
171 layer->clearFrameStats();
172 return NO_ERROR;
173}
174
175status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
176 sp<Layer> layer = getLayerUser(handle);
177 if (layer == NULL) {
178 return NAME_NOT_FOUND;
179 }
180 layer->getFrameStats(outStats);
181 return NO_ERROR;
182}
183
Robert Carr367c5682016-06-20 11:55:28 -0700184status_t Client::getTransformToDisplayInverse(const sp<IBinder>& handle,
185 bool* outTransformToDisplayInverse) const {
186 sp<Layer> layer = getLayerUser(handle);
187 if (layer == NULL) {
188 return NAME_NOT_FOUND;
189 }
190 *outTransformToDisplayInverse = layer->getTransformToDisplayInverse();
191 return NO_ERROR;
192}
193
Mathias Agopiandb403e82012-06-18 16:47:56 -0700194// ---------------------------------------------------------------------------
195}; // namespace android