blob: c28254f6f02fa5ba1834b285aa524872195b02f7 [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 "LayerBase.h"
27#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)
38 : mFlinger(flinger), mNameGenerator(1)
39{
40}
41
42Client::~Client()
43{
44 const size_t count = mLayers.size();
45 for (size_t i=0 ; i<count ; i++) {
46 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
47 if (layer != 0) {
48 mFlinger->removeLayer(layer);
49 }
50 }
51}
52
53status_t Client::initCheck() const {
54 return NO_ERROR;
55}
56
57size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
58{
59 Mutex::Autolock _l(mLock);
60 size_t name = mNameGenerator++;
61 mLayers.add(name, layer);
62 return name;
63}
64
65void Client::detachLayer(const LayerBaseClient* layer)
66{
67 Mutex::Autolock _l(mLock);
68 // we do a linear search here, because this doesn't happen often
69 const size_t count = mLayers.size();
70 for (size_t i=0 ; i<count ; i++) {
71 if (mLayers.valueAt(i) == layer) {
72 mLayers.removeItemsAt(i, 1);
73 break;
74 }
75 }
76}
77sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
78{
79 Mutex::Autolock _l(mLock);
80 sp<LayerBaseClient> lbc;
81 wp<LayerBaseClient> layer(mLayers.valueFor(i));
82 if (layer != 0) {
83 lbc = layer.promote();
84 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
85 }
86 return lbc;
87}
88
89
90status_t Client::onTransact(
91 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
92{
93 // these must be checked
94 IPCThreadState* ipc = IPCThreadState::self();
95 const int pid = ipc->getCallingPid();
96 const int uid = ipc->getCallingUid();
97 const int self_pid = getpid();
98 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
99 // we're called from a different process, do the real check
100 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
101 {
102 ALOGE("Permission Denial: "
103 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
104 return PERMISSION_DENIED;
105 }
106 }
107 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
108}
109
110
111sp<ISurface> Client::createSurface(
112 ISurfaceComposerClient::surface_data_t* params,
113 const String8& name,
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700114 uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700115 uint32_t flags)
116{
117 /*
118 * createSurface must be called from the GL thread so that it can
119 * have access to the GL context.
120 */
121
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700122 class MessageCreateLayer : public MessageBase {
Mathias Agopiandb403e82012-06-18 16:47:56 -0700123 sp<ISurface> result;
124 SurfaceFlinger* flinger;
125 ISurfaceComposerClient::surface_data_t* params;
126 Client* client;
127 const String8& name;
Mathias Agopiandb403e82012-06-18 16:47:56 -0700128 uint32_t w, h;
129 PixelFormat format;
130 uint32_t flags;
131 public:
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700132 MessageCreateLayer(SurfaceFlinger* flinger,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700133 ISurfaceComposerClient::surface_data_t* params,
134 const String8& name, Client* client,
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700135 uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopiandb403e82012-06-18 16:47:56 -0700136 uint32_t flags)
137 : flinger(flinger), params(params), client(client), name(name),
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700138 w(w), h(h), format(format), flags(flags)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700139 {
140 }
141 sp<ISurface> getResult() const { return result; }
142 virtual bool handler() {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700143 result = flinger->createLayer(params, name, client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700144 w, h, format, flags);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700145 return true;
146 }
147 };
148
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700149 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700150 params, name, this, w, h, format, flags);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700151 mFlinger->postMessageSync(msg);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700152 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
Mathias Agopiandb403e82012-06-18 16:47:56 -0700153}
154status_t Client::destroySurface(SurfaceID sid) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700155 return mFlinger->onLayerRemoved(this, sid);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700156}
157
158// ---------------------------------------------------------------------------
159}; // namespace android