| 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 | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 38 | : mFlinger(flinger) | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 39 | { | 
|  | 40 | } | 
|  | 41 |  | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 42 | status_t Client::initCheck() const { | 
|  | 43 | return NO_ERROR; | 
|  | 44 | } | 
|  | 45 |  | 
| Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 46 | void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer) | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 47 | { | 
|  | 48 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 49 | mLayers.add(handle, layer); | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
| Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 52 | void Client::detachLayer(const Layer* layer) | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 53 | { | 
|  | 54 | Mutex::Autolock _l(mLock); | 
|  | 55 | // we do a linear search here, because this doesn't happen often | 
|  | 56 | const size_t count = mLayers.size(); | 
|  | 57 | for (size_t i=0 ; i<count ; i++) { | 
|  | 58 | if (mLayers.valueAt(i) == layer) { | 
|  | 59 | mLayers.removeItemsAt(i, 1); | 
|  | 60 | break; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 | } | 
| Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 64 | sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 65 | { | 
|  | 66 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 67 | sp<Layer> lbc; | 
|  | 68 | wp<Layer> layer(mLayers.valueFor(handle)); | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 69 | if (layer != 0) { | 
|  | 70 | lbc = layer.promote(); | 
| Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 71 | ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get()); | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 72 | } | 
|  | 73 | return lbc; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 |  | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 77 | status_t Client::createSurface( | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 78 | const String8& name, | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 79 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, | 
| rongliu | ccd3484 | 2018-03-14 12:26:23 -0700 | [diff] [blame] | 80 | const sp<IBinder>& parentHandle, int32_t windowType, int32_t ownerUid, | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 81 | sp<IBinder>* handle, | 
|  | 82 | sp<IGraphicBufferProducer>* gbp) | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 83 | { | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 84 | sp<Layer> parent = nullptr; | 
|  | 85 | if (parentHandle != nullptr) { | 
| chaviw | 161410b0 | 2017-07-27 10:46:08 -0700 | [diff] [blame] | 86 | auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get()); | 
|  | 87 | parent = layerHandle->owner.promote(); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 88 | if (parent == nullptr) { | 
|  | 89 | return NAME_NOT_FOUND; | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
| Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 93 | // We rely on createLayer to check permissions. | 
| Dan Stoza | 436ccf3 | 2018-06-21 12:10:12 -0700 | [diff] [blame] | 94 | return mFlinger->createLayer(name, this, w, h, format, flags, windowType, | 
|  | 95 | ownerUid, handle, gbp, &parent); | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 96 | } | 
| Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 97 |  | 
| Marissa Wall | 35187b3 | 2019-01-08 10:08:52 -0800 | [diff] [blame] | 98 | status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h, | 
|  | 99 | PixelFormat format, uint32_t flags, | 
|  | 100 | const sp<IGraphicBufferProducer>& parent, | 
|  | 101 | int32_t windowType, int32_t ownerUid, sp<IBinder>* handle, | 
|  | 102 | sp<IGraphicBufferProducer>* gbp) { | 
|  | 103 | if (mFlinger->authenticateSurfaceTexture(parent) == false) { | 
|  | 104 | return BAD_VALUE; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer(); | 
|  | 108 | if (layer == nullptr) { | 
|  | 109 | return BAD_VALUE; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | sp<IBinder> parentHandle = layer->getHandle(); | 
|  | 113 |  | 
|  | 114 | return createSurface(name, w, h, format, flags, parentHandle, windowType, ownerUid, handle, | 
|  | 115 | gbp); | 
|  | 116 | } | 
|  | 117 |  | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 118 | status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const { | 
|  | 119 | sp<Layer> layer = getLayerUser(handle); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 120 | if (layer == nullptr) { | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 121 | return NAME_NOT_FOUND; | 
|  | 122 | } | 
|  | 123 | layer->clearFrameStats(); | 
|  | 124 | return NO_ERROR; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const { | 
|  | 128 | sp<Layer> layer = getLayerUser(handle); | 
| Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 129 | if (layer == nullptr) { | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 130 | return NAME_NOT_FOUND; | 
|  | 131 | } | 
|  | 132 | layer->getFrameStats(outStats); | 
|  | 133 | return NO_ERROR; | 
|  | 134 | } | 
|  | 135 |  | 
| Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 136 | // --------------------------------------------------------------------------- | 
|  | 137 | }; // namespace android |