blob: 49389e062ff92238716dcdf1b4f30504d66466ce [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++) {
Mathias Agopian13127d82013-03-05 17:47:11 -080046 sp<Layer> layer(mLayers.valueAt(i).promote());
Mathias Agopiandb403e82012-06-18 16:47:56 -070047 if (layer != 0) {
48 mFlinger->removeLayer(layer);
49 }
50 }
51}
52
53status_t Client::initCheck() const {
54 return NO_ERROR;
55}
56
Mathias Agopian13127d82013-03-05 17:47:11 -080057void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070058{
59 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080060 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070061}
62
Mathias Agopian13127d82013-03-05 17:47:11 -080063void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070064{
65 Mutex::Autolock _l(mLock);
66 // we do a linear search here, because this doesn't happen often
67 const size_t count = mLayers.size();
68 for (size_t i=0 ; i<count ; i++) {
69 if (mLayers.valueAt(i) == layer) {
70 mLayers.removeItemsAt(i, 1);
71 break;
72 }
73 }
74}
Mathias Agopian13127d82013-03-05 17:47:11 -080075sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070076{
77 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080078 sp<Layer> lbc;
79 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070080 if (layer != 0) {
81 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080082 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070083 }
84 return lbc;
85}
86
87
88status_t Client::onTransact(
89 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
90{
91 // these must be checked
92 IPCThreadState* ipc = IPCThreadState::self();
93 const int pid = ipc->getCallingPid();
94 const int uid = ipc->getCallingUid();
95 const int self_pid = getpid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -070096 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)) {
Mathias Agopiandb403e82012-06-18 16:47:56 -070097 // we're called from a different process, do the real check
98 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
99 {
100 ALOGE("Permission Denial: "
101 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
102 return PERMISSION_DENIED;
103 }
104 }
105 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
106}
107
108
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700110 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
112 sp<IBinder>* handle,
113 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700114{
115 /*
116 * createSurface must be called from the GL thread so that it can
117 * have access to the GL context.
118 */
119
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700120 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700121 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700122 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 sp<IBinder>* handle;
124 sp<IGraphicBufferProducer>* gbp;
125 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700126 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700127 uint32_t w, h;
128 PixelFormat format;
129 uint32_t flags;
130 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700131 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700132 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
134 sp<IBinder>* handle,
135 sp<IGraphicBufferProducer>* gbp)
136 : flinger(flinger), client(client),
137 handle(handle), gbp(gbp),
138 name(name), w(w), h(h), format(format), flags(flags) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700139 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700140 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700141 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700142 result = flinger->createLayer(name, client, w, h, format, flags,
143 handle, gbp);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700144 return true;
145 }
146 };
147
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700148 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700149 name, this, w, h, format, flags, handle, gbp);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700150 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700151 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700152}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800153
154status_t Client::destroySurface(const sp<IBinder>& handle) {
155 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700156}
157
Svetoslavd85084b2014-03-20 10:28:31 -0700158status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
159 sp<Layer> layer = getLayerUser(handle);
160 if (layer == NULL) {
161 return NAME_NOT_FOUND;
162 }
163 layer->clearFrameStats();
164 return NO_ERROR;
165}
166
167status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
168 sp<Layer> layer = getLayerUser(handle);
169 if (layer == NULL) {
170 return NAME_NOT_FOUND;
171 }
172 layer->getFrameStats(outStats);
173 return NO_ERROR;
174}
175
Mathias Agopiandb403e82012-06-18 16:47:56 -0700176// ---------------------------------------------------------------------------
177}; // namespace android