blob: 7202bef60636c2005450c0aace13511bbb366e3e [file] [log] [blame]
Mathias Agopiandb403e82012-06-18 16:47:56 -07001/*
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 Agopian4f4f0942013-08-19 17:26:18 -070020#include <binder/IPCThreadState.h>
Mathias Agopiandb403e82012-06-18 16:47:56 -070021
22#include <private/android_filesystem_config.h>
23
Huihong Luod3d8f8e2022-03-08 14:48:46 -080024#include <gui/AidlStatusUtil.h>
25
Mathias Agopiandb403e82012-06-18 16:47:56 -070026#include "Client.h"
Vishnu Naircb8be502022-10-12 19:03:23 +000027#include "FrontEnd/LayerCreationArgs.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070029#include "SurfaceFlinger.h"
30
31namespace android {
32
Huihong Luod3d8f8e2022-03-08 14:48:46 -080033using gui::aidl_utils::binderStatusFromStatusT;
34
Mathias Agopiandb403e82012-06-18 16:47:56 -070035// ---------------------------------------------------------------------------
36
37const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
38
39// ---------------------------------------------------------------------------
40
41Client::Client(const sp<SurfaceFlinger>& flinger)
Robert Carrb89ea9d2018-12-10 13:01:14 -080042 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070043{
44}
45
Mathias Agopiandb403e82012-06-18 16:47:56 -070046status_t Client::initCheck() const {
47 return NO_ERROR;
48}
49
Mathias Agopian13127d82013-03-05 17:47:11 -080050void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070051{
52 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080053 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070054}
55
Mathias Agopian13127d82013-03-05 17:47:11 -080056void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070057{
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 Agopian13127d82013-03-05 17:47:11 -080068sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070069{
70 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080071 sp<Layer> lbc;
72 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070073 if (layer != 0) {
74 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080075 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070076 }
77 return lbc;
78}
79
Huihong Luod3d8f8e2022-03-08 14:48:46 -080080binder::Status Client::createSurface(const std::string& name, int32_t flags,
81 const sp<IBinder>& parent, const gui::LayerMetadata& metadata,
82 gui::CreateSurfaceResult* outResult) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080083 // We rely on createLayer to check permissions.
Huihong Luod3d8f8e2022-03-08 14:48:46 -080084 sp<IBinder> handle;
Ady Abrahamd11bade2022-08-01 16:18:03 -070085 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
86 static_cast<uint32_t>(flags), std::move(metadata));
Vishnu Naircb8be502022-10-12 19:03:23 +000087 args.parentHandle = parent;
88 const status_t status = mFlinger->createLayer(args, *outResult);
Huihong Luod3d8f8e2022-03-08 14:48:46 -080089 return binderStatusFromStatusT(status);
Mathias Agopiandb403e82012-06-18 16:47:56 -070090}
Mathias Agopianac9fa422013-02-11 16:40:36 -080091
Huihong Luod3d8f8e2022-03-08 14:48:46 -080092binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
93 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -070094 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -080095 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -080096 status = NAME_NOT_FOUND;
97 } else {
98 layer->clearFrameStats();
99 status = NO_ERROR;
Svetoslavd85084b2014-03-20 10:28:31 -0700100 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800101 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700102}
103
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800104binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
105 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -0700106 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800107 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800108 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;
Svetoslavd85084b2014-03-20 10:28:31 -0700126 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800127 return binderStatusFromStatusT(status);
128}
129
130binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
Patrick Williamsa361de62022-10-06 20:34:10 +0000131 gui::CreateSurfaceResult* outResult) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800132 sp<IBinder> handle;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700133 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot",
134 0 /* flags */, gui::LayerMetadata());
Patrick Williamsa361de62022-10-06 20:34:10 +0000135 status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800136 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700137}
138
Patrick Williamsa361de62022-10-06 20:34:10 +0000139binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) {
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000140 sp<IBinder> handle;
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000141 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 Williamsa361de62022-10-06 20:34:10 +0000145 status_t status = mFlinger->mirrorDisplay(*id, args, *outResult);
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000146 return binderStatusFromStatusT(status);
147}
148
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149// ---------------------------------------------------------------------------
150}; // namespace android