blob: 0575e352d1a71408a0818be1a1d1fca4e916a70b [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
108sp<ISurface> Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -0700109 const String8& name,
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700110 uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700111 uint32_t flags)
112{
113 /*
114 * createSurface must be called from the GL thread so that it can
115 * have access to the GL context.
116 */
117
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700118 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700119 sp<ISurface> result;
120 SurfaceFlinger* flinger;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700121 Client* client;
122 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700123 uint32_t w, h;
124 PixelFormat format;
125 uint32_t flags;
126 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700127 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700128 const String8& name, Client* client,
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700129 uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700130 uint32_t flags)
Mathias Agopianac9fa422013-02-11 16:40:36 -0800131 : flinger(flinger), client(client), name(name),
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700132 w(w), h(h), format(format), flags(flags)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700133 {
134 }
135 sp<ISurface> getResult() const { return result; }
136 virtual bool handler() {
Mathias Agopianac9fa422013-02-11 16:40:36 -0800137 result = flinger->createLayer(name, client, w, h, format, flags);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700138 return true;
139 }
140 };
141
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700142 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Mathias Agopianac9fa422013-02-11 16:40:36 -0800143 name, this, w, h, format, flags);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700144 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700145 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700146}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800147
148status_t Client::destroySurface(const sp<IBinder>& handle) {
149 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700150}
151
152// ---------------------------------------------------------------------------
153}; // namespace android