blob: b27055d57acd87862f6210df0d3e82ecfc39fdf0 [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;
84 int32_t layerId;
85 uint32_t transformHint;
86 LayerCreationArgs args(mFlinger.get(), this, name.c_str(), static_cast<uint32_t>(flags),
87 std::move(metadata));
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 Agopiandb403e82012-06-18 16:47:56 -070096}
Mathias Agopianac9fa422013-02-11 16:40:36 -080097
Huihong Luod3d8f8e2022-03-08 14:48:46 -080098binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
99 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -0700100 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800101 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800102 status = NAME_NOT_FOUND;
103 } else {
104 layer->clearFrameStats();
105 status = NO_ERROR;
Svetoslavd85084b2014-03-20 10:28:31 -0700106 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800107 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700108}
109
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800110binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
111 status_t status;
Svetoslavd85084b2014-03-20 10:28:31 -0700112 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800113 if (layer == nullptr) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800114 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;
Svetoslavd85084b2014-03-20 10:28:31 -0700132 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800133 return binderStatusFromStatusT(status);
134}
135
136binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
137 gui::MirrorSurfaceResult* outResult) {
138 sp<IBinder> handle;
139 int32_t layerId;
140 LayerCreationArgs args(mFlinger.get(), this, "MirrorRoot", 0 /* flags */, gui::LayerMetadata());
141 status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, &handle, &layerId);
142 if (status == NO_ERROR) {
143 outResult->handle = handle;
144 outResult->layerId = layerId;
145 }
146 return binderStatusFromStatusT(status);
Svetoslavd85084b2014-03-20 10:28:31 -0700147}
148
Mathias Agopiandb403e82012-06-18 16:47:56 -0700149// ---------------------------------------------------------------------------
150}; // namespace android