blob: aac6c913cfb176e03af64b2279d61eac8a054acf [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopiandb403e82012-06-18 16:47:56 -070021#include <stdint.h>
22#include <sys/types.h>
23
Mathias Agopian4f4f0942013-08-19 17:26:18 -070024#include <binder/IPCThreadState.h>
Mathias Agopiandb403e82012-06-18 16:47:56 -070025
26#include <private/android_filesystem_config.h>
27
28#include "Client.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070029#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070030#include "SurfaceFlinger.h"
31
32namespace android {
33
34// ---------------------------------------------------------------------------
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
Evan Rosky1f6d6d52018-12-06 10:47:26 -080079status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
80 uint32_t flags, const sp<IBinder>& parentHandle,
81 LayerMetadata metadata, sp<IBinder>* handle,
Ady Abraham9f0a4002020-10-05 15:47:26 -070082 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +000083 uint32_t* outTransformHint) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080084 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080085 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Ady Abraham9f0a4002020-10-05 15:47:26 -070086 parentHandle, outLayerId, nullptr, outTransformHint);
Mathias Agopiandb403e82012-06-18 16:47:56 -070087}
Mathias Agopianac9fa422013-02-11 16:40:36 -080088
Marissa Wall35187b32019-01-08 10:08:52 -080089status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
90 PixelFormat format, uint32_t flags,
91 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080092 LayerMetadata metadata, sp<IBinder>* handle,
Ady Abraham9f0a4002020-10-05 15:47:26 -070093 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
Valerie Hau1acd6962019-10-28 16:35:48 -070094 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -080095 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
Marissa Walld1d644b2019-06-13 14:24:27 -070096 ALOGE("failed to authenticate surface texture");
Marissa Wall35187b32019-01-08 10:08:52 -080097 return BAD_VALUE;
98 }
99
100 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
101 if (layer == nullptr) {
Marissa Walld1d644b2019-06-13 14:24:27 -0700102 ALOGE("failed to find parent layer");
Marissa Wall35187b32019-01-08 10:08:52 -0800103 return BAD_VALUE;
104 }
105
Robert Carrc0df3122019-04-11 13:18:21 -0700106 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Ady Abraham9f0a4002020-10-05 15:47:26 -0700107 nullptr, outLayerId, layer, outTransformHint);
Marissa Wall35187b32019-01-08 10:08:52 -0800108}
109
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +0000110status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle,
Ady Abraham9f0a4002020-10-05 15:47:26 -0700111 int32_t* outLayerId) {
112 return mFlinger->mirrorLayer(this, mirrorFromHandle, outHandle, outLayerId);
chaviwfe94a222019-08-21 13:52:59 -0700113}
114
Svetoslavd85084b2014-03-20 10:28:31 -0700115status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
116 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800117 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700118 return NAME_NOT_FOUND;
119 }
120 layer->clearFrameStats();
121 return NO_ERROR;
122}
123
124status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
125 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800126 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700127 return NAME_NOT_FOUND;
128 }
129 layer->getFrameStats(outStats);
130 return NO_ERROR;
131}
132
Mathias Agopiandb403e82012-06-18 16:47:56 -0700133// ---------------------------------------------------------------------------
134}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800135
136// TODO(b/129481165): remove the #pragma below and fix conversion issues
137#pragma clang diagnostic pop // ignored "-Wconversion"