1 /* 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" 25 #include "Layer.h" 26 #include "SurfaceFlinger.h" 27 28 namespace android { 29 30 // --------------------------------------------------------------------------- 31 32 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); 33 34 // --------------------------------------------------------------------------- 35 36 Client::Client(const sp<SurfaceFlinger>& flinger) 37 : mFlinger(flinger) 38 { 39 } 40 41 Client::~Client() 42 { 43 const size_t count = mLayers.size(); 44 for (size_t i=0 ; i<count ; i++) { 45 sp<Layer> layer(mLayers.valueAt(i).promote()); 46 if (layer != 0) { 47 mFlinger->removeLayer(layer); 48 } 49 } 50 } 51 52 status_t Client::initCheck() const { 53 return NO_ERROR; 54 } 55 56 void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer) 57 { 58 Mutex::Autolock _l(mLock); 59 mLayers.add(handle, layer); 60 } 61 62 void Client::detachLayer(const Layer* layer) 63 { 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 } 74 sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const 75 { 76 Mutex::Autolock _l(mLock); 77 sp<Layer> lbc; 78 wp<Layer> layer(mLayers.valueFor(handle)); 79 if (layer != 0) { 80 lbc = layer.promote(); 81 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get()); 82 } 83 return lbc; 84 } 85 86 87 status_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 108 status_t Client::createSurface( 109 const String8& name, 110 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 111 sp<IBinder>* handle, 112 sp<IGraphicBufferProducer>* gbp) 113 { 114 /* 115 * createSurface must be called from the GL thread so that it can 116 * have access to the GL context. 117 */ 118 119 class MessageCreateLayer : public MessageBase { 120 SurfaceFlinger* flinger; 121 Client* client; 122 sp<IBinder>* handle; 123 sp<IGraphicBufferProducer>* gbp; 124 status_t result; 125 const String8& name; 126 uint32_t w, h; 127 PixelFormat format; 128 uint32_t flags; 129 public: 130 MessageCreateLayer(SurfaceFlinger* flinger, 131 const String8& name, Client* client, 132 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) { 138 } 139 status_t getResult() const { return result; } 140 virtual bool handler() { 141 result = flinger->createLayer(name, client, w, h, format, flags, 142 handle, gbp); 143 return true; 144 } 145 }; 146 147 sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), 148 name, this, w, h, format, flags, handle, gbp); 149 mFlinger->postMessageSync(msg); 150 return static_cast<MessageCreateLayer*>( msg.get() )->getResult(); 151 } 152 153 status_t Client::destroySurface(const sp<IBinder>& handle) { 154 return mFlinger->onLayerRemoved(this, handle); 155 } 156 157 // --------------------------------------------------------------------------- 158 }; // namespace android 159