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