blob: dd65348ea076064ab166beb95706e2863e96765b [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>
21
22#include <private/android_filesystem_config.h>
23
24#include "Client.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070026#include "SurfaceFlinger.h"
27
28namespace android {
29
30// ---------------------------------------------------------------------------
31
32const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
33
34// ---------------------------------------------------------------------------
35
36Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianac9fa422013-02-11 16:40:36 -080037 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070038{
39}
40
41Client::~Client()
42{
43 const size_t count = mLayers.size();
44 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -080045 sp<Layer> layer(mLayers.valueAt(i).promote());
Mathias Agopiandb403e82012-06-18 16:47:56 -070046 if (layer != 0) {
47 mFlinger->removeLayer(layer);
48 }
49 }
50}
51
52status_t Client::initCheck() const {
53 return NO_ERROR;
54}
55
Mathias Agopian13127d82013-03-05 17:47:11 -080056void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070057{
58 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080059 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070060}
61
Mathias Agopian13127d82013-03-05 17:47:11 -080062void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070063{
64 Mutex::Autolock _l(mLock);
65 // we do a linear search here, because this doesn't happen often
66 const size_t count = mLayers.size();
67 for (size_t i=0 ; i<count ; i++) {
68 if (mLayers.valueAt(i) == layer) {
69 mLayers.removeItemsAt(i, 1);
70 break;
71 }
72 }
73}
Mathias Agopian13127d82013-03-05 17:47:11 -080074sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070075{
76 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080077 sp<Layer> lbc;
78 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070079 if (layer != 0) {
80 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080081 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070082 }
83 return lbc;
84}
85
86
87status_t Client::onTransact(
88 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
89{
90 // these must be checked
91 IPCThreadState* ipc = IPCThreadState::self();
92 const int pid = ipc->getCallingPid();
93 const int uid = ipc->getCallingUid();
94 const int self_pid = getpid();
95 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
96 // we're called from a different process, do the real check
97 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
98 {
99 ALOGE("Permission Denial: "
100 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
101 return PERMISSION_DENIED;
102 }
103 }
104 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
105}
106
107
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700109 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
111 sp<IBinder>* handle,
112 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700113{
114 /*
115 * createSurface must be called from the GL thread so that it can
116 * have access to the GL context.
117 */
118
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700119 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700120 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700121 Client* client;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 sp<IBinder>* handle;
123 sp<IGraphicBufferProducer>* gbp;
124 status_t result;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700125 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700126 uint32_t w, h;
127 PixelFormat format;
128 uint32_t flags;
129 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700130 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700131 const String8& name, Client* client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
133 sp<IBinder>* handle,
134 sp<IGraphicBufferProducer>* gbp)
135 : flinger(flinger), client(client),
136 handle(handle), gbp(gbp),
137 name(name), w(w), h(h), format(format), flags(flags) {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700138 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139 status_t getResult() const { return result; }
Mathias Agopiandb403e82012-06-18 16:47:56 -0700140 virtual bool handler() {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700141 result = flinger->createLayer(name, client, w, h, format, flags,
142 handle, gbp);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700143 return true;
144 }
145 };
146
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700147 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700148 name, this, w, h, format, flags, handle, gbp);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700150 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700151}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800152
153status_t Client::destroySurface(const sp<IBinder>& handle) {
154 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700155}
156
157// ---------------------------------------------------------------------------
158}; // namespace android