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