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 | |
Mathias Agopian | 4f4f094 | 2013-08-19 17:26:18 -0700 | [diff] [blame] | 20 | #include <binder/IPCThreadState.h> |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 21 | |
| 22 | #include <private/android_filesystem_config.h> |
| 23 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 24 | #include <gui/AidlStatusUtil.h> |
| 25 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 26 | #include "Client.h" |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame^] | 27 | #include "FrontEnd/LayerCreationArgs.h" |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 28 | #include "Layer.h" |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 29 | #include "SurfaceFlinger.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 33 | using gui::aidl_utils::binderStatusFromStatusT; |
| 34 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
| 38 | |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
| 41 | Client::Client(const sp<SurfaceFlinger>& flinger) |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 42 | : mFlinger(flinger) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 46 | status_t Client::initCheck() const { |
| 47 | return NO_ERROR; |
| 48 | } |
| 49 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 50 | void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 51 | { |
| 52 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 53 | mLayers.add(handle, layer); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 56 | void Client::detachLayer(const Layer* layer) |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 57 | { |
| 58 | Mutex::Autolock _l(mLock); |
| 59 | // we do a linear search here, because this doesn't happen often |
| 60 | const size_t count = mLayers.size(); |
| 61 | for (size_t i=0 ; i<count ; i++) { |
| 62 | if (mLayers.valueAt(i) == layer) { |
| 63 | mLayers.removeItemsAt(i, 1); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 68 | sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 69 | { |
| 70 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 71 | sp<Layer> lbc; |
| 72 | wp<Layer> layer(mLayers.valueFor(handle)); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 73 | if (layer != 0) { |
| 74 | lbc = layer.promote(); |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 75 | ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get()); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 76 | } |
| 77 | return lbc; |
| 78 | } |
| 79 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 80 | binder::Status Client::createSurface(const std::string& name, int32_t flags, |
| 81 | const sp<IBinder>& parent, const gui::LayerMetadata& metadata, |
| 82 | gui::CreateSurfaceResult* outResult) { |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 83 | // We rely on createLayer to check permissions. |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 84 | sp<IBinder> handle; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 85 | LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(), |
| 86 | static_cast<uint32_t>(flags), std::move(metadata)); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame^] | 87 | args.parentHandle = parent; |
| 88 | const status_t status = mFlinger->createLayer(args, *outResult); |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 89 | return binderStatusFromStatusT(status); |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 90 | } |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 91 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 92 | binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) { |
| 93 | status_t status; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 94 | sp<Layer> layer = getLayerUser(handle); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 95 | if (layer == nullptr) { |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 96 | status = NAME_NOT_FOUND; |
| 97 | } else { |
| 98 | layer->clearFrameStats(); |
| 99 | status = NO_ERROR; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 100 | } |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 101 | return binderStatusFromStatusT(status); |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 104 | binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) { |
| 105 | status_t status; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 106 | sp<Layer> layer = getLayerUser(handle); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 107 | if (layer == nullptr) { |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 108 | status = NAME_NOT_FOUND; |
| 109 | } else { |
| 110 | FrameStats stats; |
| 111 | layer->getFrameStats(&stats); |
| 112 | outStats->refreshPeriodNano = stats.refreshPeriodNano; |
| 113 | outStats->desiredPresentTimesNano.reserve(stats.desiredPresentTimesNano.size()); |
| 114 | for (const auto& t : stats.desiredPresentTimesNano) { |
| 115 | outStats->desiredPresentTimesNano.push_back(t); |
| 116 | } |
| 117 | outStats->actualPresentTimesNano.reserve(stats.actualPresentTimesNano.size()); |
| 118 | for (const auto& t : stats.actualPresentTimesNano) { |
| 119 | outStats->actualPresentTimesNano.push_back(t); |
| 120 | } |
| 121 | outStats->frameReadyTimesNano.reserve(stats.frameReadyTimesNano.size()); |
| 122 | for (const auto& t : stats.frameReadyTimesNano) { |
| 123 | outStats->frameReadyTimesNano.push_back(t); |
| 124 | } |
| 125 | status = NO_ERROR; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 126 | } |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 127 | return binderStatusFromStatusT(status); |
| 128 | } |
| 129 | |
| 130 | binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 131 | gui::CreateSurfaceResult* outResult) { |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 132 | sp<IBinder> handle; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 133 | LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot", |
| 134 | 0 /* flags */, gui::LayerMetadata()); |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 135 | status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult); |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 136 | return binderStatusFromStatusT(status); |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 139 | binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) { |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 140 | sp<IBinder> handle; |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 141 | LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), |
| 142 | "MirrorRoot-" + std::to_string(displayId), 0 /* flags */, |
| 143 | gui::LayerMetadata()); |
| 144 | std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId)); |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 145 | status_t status = mFlinger->mirrorDisplay(*id, args, *outResult); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 146 | return binderStatusFromStatusT(status); |
| 147 | } |
| 148 | |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 149 | // --------------------------------------------------------------------------- |
| 150 | }; // namespace android |