blob: 30b875967c0c35dcfc6890670895bc6c7e0cecd7 [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"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070027#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070028#include "SurfaceFlinger.h"
29
30namespace android {
31
Huihong Luod3d8f8e2022-03-08 14:48:46 -080032using gui::aidl_utils::binderStatusFromStatusT;
33
Mathias Agopiandb403e82012-06-18 16:47:56 -070034// ---------------------------------------------------------------------------
35
36const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
37
38// ---------------------------------------------------------------------------
39
40Client::Client(const sp<SurfaceFlinger>& flinger)
Robert Carrb89ea9d2018-12-10 13:01:14 -080041 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070042{
43}
44
Mathias Agopiandb403e82012-06-18 16:47:56 -070045status_t Client::initCheck() const {
46 return NO_ERROR;
47}
48
Mathias Agopian13127d82013-03-05 17:47:11 -080049void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070050{
51 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080052 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070053}
54
Mathias Agopian13127d82013-03-05 17:47:11 -080055void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070056{
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 Agopian13127d82013-03-05 17:47:11 -080067sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070068{
69 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sp<Layer> lbc;
71 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070072 if (layer != 0) {
73 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080074 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070075 }
76 return lbc;
77}
78
Huihong Luod3d8f8e2022-03-08 14:48:46 -080079binder::Status Client::createSurface(const std::string& name, int32_t flags,
80 const sp<IBinder>& parent, const gui::LayerMetadata& metadata,
81 gui::CreateSurfaceResult* outResult) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080082 // We rely on createLayer to check permissions.
Huihong Luod3d8f8e2022-03-08 14:48:46 -080083 sp<IBinder> handle;
Ady Abrahamd11bade2022-08-01 16:18:03 -070084 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
85 static_cast<uint32_t>(flags), std::move(metadata));
Patrick Williamsa361de62022-10-06 20:34:10 +000086 const status_t status = mFlinger->createLayer(args, parent, *outResult);
Huihong Luod3d8f8e2022-03-08 14:48:46 -080087 return binderStatusFromStatusT(status);
Mathias Agopiandb403e82012-06-18 16:47:56 -070088}
Mathias Agopianac9fa422013-02-11 16:40:36 -080089
Huihong Luod3d8f8e2022-03-08 14:48:46 -080090binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
91 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -070092 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -080093 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -080094 status = NAME_NOT_FOUND;
95 } else {
96 layer->clearFrameStats();
97 status = NO_ERROR;
Svetoslavd85084b2014-03-20 10:28:31 -070098 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -080099 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700100}
101
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800102binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
103 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -0700104 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800105 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800106 status = NAME_NOT_FOUND;
107 } else {
108 FrameStats stats;
109 layer->getFrameStats(&stats);
110 outStats->refreshPeriodNano = stats.refreshPeriodNano;
111 outStats->desiredPresentTimesNano.reserve(stats.desiredPresentTimesNano.size());
112 for (const auto& t : stats.desiredPresentTimesNano) {
113 outStats->desiredPresentTimesNano.push_back(t);
114 }
115 outStats->actualPresentTimesNano.reserve(stats.actualPresentTimesNano.size());
116 for (const auto& t : stats.actualPresentTimesNano) {
117 outStats->actualPresentTimesNano.push_back(t);
118 }
119 outStats->frameReadyTimesNano.reserve(stats.frameReadyTimesNano.size());
120 for (const auto& t : stats.frameReadyTimesNano) {
121 outStats->frameReadyTimesNano.push_back(t);
122 }
123 status = NO_ERROR;
Svetoslavd85084b2014-03-20 10:28:31 -0700124 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800125 return binderStatusFromStatusT(status);
126}
127
128binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
Patrick Williamsa361de62022-10-06 20:34:10 +0000129 gui::CreateSurfaceResult* outResult) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800130 sp<IBinder> handle;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700131 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot",
132 0 /* flags */, gui::LayerMetadata());
Patrick Williamsa361de62022-10-06 20:34:10 +0000133 status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800134 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700135}
136
Patrick Williamsa361de62022-10-06 20:34:10 +0000137binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) {
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000138 sp<IBinder> handle;
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000139 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this),
140 "MirrorRoot-" + std::to_string(displayId), 0 /* flags */,
141 gui::LayerMetadata());
142 std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId));
Patrick Williamsa361de62022-10-06 20:34:10 +0000143 status_t status = mFlinger->mirrorDisplay(*id, args, *outResult);
Chavi Weingarten7043a7d2022-07-19 23:40:35 +0000144 return binderStatusFromStatusT(status);
145}
146
Mathias Agopiandb403e82012-06-18 16:47:56 -0700147// ---------------------------------------------------------------------------
148}; // namespace android