Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 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" |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 25 | #include "Layer.h" |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 26 | #include "LayerBase.h" |
| 27 | #include "SurfaceFlinger.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
| 33 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | Client::Client(const sp<SurfaceFlinger>& flinger) |
| 38 | : mFlinger(flinger), mNameGenerator(1) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Client::~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 | |
| 53 | status_t Client::initCheck() const { |
| 54 | return NO_ERROR; |
| 55 | } |
| 56 | |
| 57 | size_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 | |
| 65 | void 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 | } |
| 77 | sp<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 | |
| 90 | status_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 | |
| 111 | sp<ISurface> Client::createSurface( |
| 112 | ISurfaceComposerClient::surface_data_t* params, |
| 113 | const String8& name, |
| 114 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 115 | 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 Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 122 | class MessageCreateLayer : public MessageBase { |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 123 | sp<ISurface> result; |
| 124 | SurfaceFlinger* flinger; |
| 125 | ISurfaceComposerClient::surface_data_t* params; |
| 126 | Client* client; |
| 127 | const String8& name; |
| 128 | DisplayID display; |
| 129 | uint32_t w, h; |
| 130 | PixelFormat format; |
| 131 | uint32_t flags; |
| 132 | public: |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 133 | MessageCreateLayer(SurfaceFlinger* flinger, |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 134 | ISurfaceComposerClient::surface_data_t* params, |
| 135 | const String8& name, Client* client, |
| 136 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, |
| 137 | uint32_t flags) |
| 138 | : flinger(flinger), params(params), client(client), name(name), |
| 139 | display(display), w(w), h(h), format(format), flags(flags) |
| 140 | { |
| 141 | } |
| 142 | sp<ISurface> getResult() const { return result; } |
| 143 | virtual bool handler() { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 144 | result = flinger->createLayer(params, name, client, |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 145 | display, w, h, format, flags); |
| 146 | return true; |
| 147 | } |
| 148 | }; |
| 149 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 150 | sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 151 | params, name, this, display, w, h, format, flags); |
| 152 | mFlinger->postMessageSync(msg); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 153 | return static_cast<MessageCreateLayer*>( msg.get() )->getResult(); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 154 | } |
| 155 | status_t Client::destroySurface(SurfaceID sid) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 156 | return mFlinger->onLayerRemoved(this, sid); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // --------------------------------------------------------------------------- |
| 160 | }; // namespace android |