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> |
Mathias Agopian | 4f4f094 | 2013-08-19 17:26:18 -0700 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 22 | |
| 23 | #include <private/android_filesystem_config.h> |
| 24 | |
| 25 | #include "Client.h" |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 26 | #include "Layer.h" |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 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) |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 38 | : Client(flinger, nullptr) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Client::Client(const sp<SurfaceFlinger>& flinger, const sp<Layer>& parentLayer) |
| 43 | : mFlinger(flinger), |
| 44 | mParentLayer(parentLayer) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | Client::~Client() |
| 49 | { |
| 50 | const size_t count = mLayers.size(); |
| 51 | for (size_t i=0 ; i<count ; i++) { |
Dan Stoza | 22851c3 | 2016-08-09 13:21:03 -0700 | [diff] [blame] | 52 | mFlinger->removeLayer(mLayers.valueAt(i)); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 56 | void Client::setParentLayer(const sp<Layer>& parentLayer) { |
| 57 | mParentLayer = parentLayer; |
| 58 | } |
| 59 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 60 | status_t Client::initCheck() const { |
| 61 | return NO_ERROR; |
| 62 | } |
| 63 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 64 | void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 65 | { |
| 66 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 67 | mLayers.add(handle, layer); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 70 | void Client::detachLayer(const Layer* layer) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 71 | { |
| 72 | Mutex::Autolock _l(mLock); |
| 73 | // we do a linear search here, because this doesn't happen often |
| 74 | const size_t count = mLayers.size(); |
| 75 | for (size_t i=0 ; i<count ; i++) { |
| 76 | if (mLayers.valueAt(i) == layer) { |
| 77 | mLayers.removeItemsAt(i, 1); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 82 | sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 83 | { |
| 84 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 85 | sp<Layer> lbc; |
| 86 | wp<Layer> layer(mLayers.valueFor(handle)); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 87 | if (layer != 0) { |
| 88 | lbc = layer.promote(); |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 89 | ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get()); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 90 | } |
| 91 | return lbc; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | status_t Client::onTransact( |
| 96 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 97 | { |
| 98 | // these must be checked |
| 99 | IPCThreadState* ipc = IPCThreadState::self(); |
| 100 | const int pid = ipc->getCallingPid(); |
| 101 | const int uid = ipc->getCallingUid(); |
| 102 | const int self_pid = getpid(); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 103 | // If we are called from another non root process without the GRAPHICS, SYSTEM, or ROOT |
| 104 | // uid we require the sAccessSurfaceFlinger permission. |
| 105 | // We grant an exception in the case that the Client has a "parent layer", as its |
| 106 | // effects will be scoped to that layer. |
| 107 | if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0) |
| 108 | && (mParentLayer.promote() == nullptr)) { |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 109 | // we're called from a different process, do the real check |
| 110 | if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger)) |
| 111 | { |
| 112 | ALOGE("Permission Denial: " |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 113 | "can't openGlobalTransaction pid=%d, uid<=%d", pid, uid); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 114 | return PERMISSION_DENIED; |
| 115 | } |
| 116 | } |
| 117 | return BnSurfaceComposerClient::onTransact(code, data, reply, flags); |
| 118 | } |
| 119 | |
| 120 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 121 | status_t Client::createSurface( |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 122 | const String8& name, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 123 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 124 | const sp<IBinder>& parentHandle, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 125 | sp<IBinder>* handle, |
| 126 | sp<IGraphicBufferProducer>* gbp) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 127 | { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 128 | sp<Layer> parent = nullptr; |
| 129 | if (parentHandle != nullptr) { |
| 130 | parent = getLayerUser(parentHandle); |
| 131 | if (parent == nullptr) { |
| 132 | return NAME_NOT_FOUND; |
| 133 | } |
| 134 | } |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 135 | if (parent == nullptr && mParentLayer != nullptr) { |
| 136 | parent = mParentLayer.promote(); |
| 137 | // If we had a parent, but it died, we've lost all |
| 138 | // our capabilities. |
| 139 | if (parent == nullptr) { |
| 140 | return NAME_NOT_FOUND; |
| 141 | } |
| 142 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 143 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 144 | /* |
| 145 | * createSurface must be called from the GL thread so that it can |
| 146 | * have access to the GL context. |
| 147 | */ |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 148 | class MessageCreateLayer : public MessageBase { |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 149 | SurfaceFlinger* flinger; |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 150 | Client* client; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 151 | sp<IBinder>* handle; |
| 152 | sp<IGraphicBufferProducer>* gbp; |
| 153 | status_t result; |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 154 | const String8& name; |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 155 | uint32_t w, h; |
| 156 | PixelFormat format; |
| 157 | uint32_t flags; |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 158 | sp<Layer>* parent; |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 159 | public: |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 160 | MessageCreateLayer(SurfaceFlinger* flinger, |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 161 | const String8& name, Client* client, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 162 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, |
| 163 | sp<IBinder>* handle, |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 164 | sp<IGraphicBufferProducer>* gbp, |
| 165 | sp<Layer>* parent) |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 166 | : flinger(flinger), client(client), |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 167 | handle(handle), gbp(gbp), result(NO_ERROR), |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 168 | name(name), w(w), h(h), format(format), flags(flags), |
| 169 | parent(parent) { |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 170 | } |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 171 | status_t getResult() const { return result; } |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 172 | virtual bool handler() { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 173 | result = flinger->createLayer(name, client, w, h, format, flags, |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 174 | handle, gbp, parent); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | }; |
| 178 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 179 | sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 180 | name, this, w, h, format, flags, handle, gbp, &parent); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 181 | mFlinger->postMessageSync(msg); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 182 | return static_cast<MessageCreateLayer*>( msg.get() )->getResult(); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 183 | } |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 184 | |
| 185 | status_t Client::destroySurface(const sp<IBinder>& handle) { |
| 186 | return mFlinger->onLayerRemoved(this, handle); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 189 | status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const { |
| 190 | sp<Layer> layer = getLayerUser(handle); |
| 191 | if (layer == NULL) { |
| 192 | return NAME_NOT_FOUND; |
| 193 | } |
| 194 | layer->clearFrameStats(); |
| 195 | return NO_ERROR; |
| 196 | } |
| 197 | |
| 198 | status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const { |
| 199 | sp<Layer> layer = getLayerUser(handle); |
| 200 | if (layer == NULL) { |
| 201 | return NAME_NOT_FOUND; |
| 202 | } |
| 203 | layer->getFrameStats(outStats); |
| 204 | return NO_ERROR; |
| 205 | } |
| 206 | |
Robert Carr | 367c568 | 2016-06-20 11:55:28 -0700 | [diff] [blame] | 207 | status_t Client::getTransformToDisplayInverse(const sp<IBinder>& handle, |
| 208 | bool* outTransformToDisplayInverse) const { |
| 209 | sp<Layer> layer = getLayerUser(handle); |
| 210 | if (layer == NULL) { |
| 211 | return NAME_NOT_FOUND; |
| 212 | } |
| 213 | *outTransformToDisplayInverse = layer->getTransformToDisplayInverse(); |
| 214 | return NO_ERROR; |
| 215 | } |
| 216 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 217 | // --------------------------------------------------------------------------- |
| 218 | }; // namespace android |