2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
20 #include <binder/PermissionCache.h>
21 #include <binder/IPCThreadState.h>
23 #include <private/android_filesystem_config.h>
27 #include "SurfaceFlinger.h"
31 // ---------------------------------------------------------------------------
33 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
35 // ---------------------------------------------------------------------------
37 Client::Client(const sp<SurfaceFlinger>& flinger)
44 const size_t count = mLayers.size();
45 for (size_t i=0 ; i<count ; i++) {
46 sp<Layer> layer(mLayers.valueAt(i).promote());
48 mFlinger->removeLayer(layer);
53 status_t Client::initCheck() const {
57 void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
59 Mutex::Autolock _l(mLock);
60 mLayers.add(handle, layer);
63 void Client::detachLayer(const Layer* layer)
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);
75 sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
77 Mutex::Autolock _l(mLock);
79 wp<Layer> layer(mLayers.valueFor(handle));
81 lbc = layer.promote();
82 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
88 status_t Client::onTransact(
89 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
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();
96 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
97 // we're called from a different process, do the real check
98 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
100 ALOGE("Permission Denial: "
101 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
102 return PERMISSION_DENIED;
105 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
109 status_t Client::createSurface(
111 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
113 sp<IGraphicBufferProducer>* gbp)
116 * createSurface must be called from the GL thread so that it can
117 * have access to the GL context.
120 class MessageCreateLayer : public MessageBase {
121 SurfaceFlinger* flinger;
124 sp<IGraphicBufferProducer>* gbp;
131 MessageCreateLayer(SurfaceFlinger* flinger,
132 const String8& name, Client* client,
133 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
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) {
140 status_t getResult() const { return result; }
141 virtual bool handler() {
142 result = flinger->createLayer(name, client, w, h, format, flags,
148 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
149 name, this, w, h, format, flags, handle, gbp);
150 mFlinger->postMessageSync(msg);
151 return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
154 status_t Client::destroySurface(const sp<IBinder>& handle) {
155 return mFlinger->onLayerRemoved(this, handle);
158 // ---------------------------------------------------------------------------
159 }; // namespace android