blob: 78bbcba4b4fbd5bf489b68debacff5a823a34235 [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,
Valerie Hau1acd6962019-10-28 16:35:48 -070082 sp<IGraphicBufferProducer>* gbp, uint32_t* outTransformHint) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080083 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080084 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Valerie Hau1acd6962019-10-28 16:35:48 -070085 parentHandle, nullptr, outTransformHint);
Mathias Agopiandb403e82012-06-18 16:47:56 -070086}
Mathias Agopianac9fa422013-02-11 16:40:36 -080087
Marissa Wall35187b32019-01-08 10:08:52 -080088status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
89 PixelFormat format, uint32_t flags,
90 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080091 LayerMetadata metadata, sp<IBinder>* handle,
Valerie Hau1acd6962019-10-28 16:35:48 -070092 sp<IGraphicBufferProducer>* gbp,
93 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -080094 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
Marissa Walld1d644b2019-06-13 14:24:27 -070095 ALOGE("failed to authenticate surface texture");
Marissa Wall35187b32019-01-08 10:08:52 -080096 return BAD_VALUE;
97 }
98
99 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
100 if (layer == nullptr) {
Marissa Walld1d644b2019-06-13 14:24:27 -0700101 ALOGE("failed to find parent layer");
Marissa Wall35187b32019-01-08 10:08:52 -0800102 return BAD_VALUE;
103 }
104
Robert Carrc0df3122019-04-11 13:18:21 -0700105 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Valerie Hau1acd6962019-10-28 16:35:48 -0700106 nullptr, layer, outTransformHint);
Marissa Wall35187b32019-01-08 10:08:52 -0800107}
108
chaviwfe94a222019-08-21 13:52:59 -0700109status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle) {
110 return mFlinger->mirrorLayer(this, mirrorFromHandle, outHandle);
111}
112
Svetoslavd85084b2014-03-20 10:28:31 -0700113status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
114 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800115 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700116 return NAME_NOT_FOUND;
117 }
118 layer->clearFrameStats();
119 return NO_ERROR;
120}
121
122status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
123 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800124 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700125 return NAME_NOT_FOUND;
126 }
127 layer->getFrameStats(outStats);
128 return NO_ERROR;
129}
130
Mathias Agopiandb403e82012-06-18 16:47:56 -0700131// ---------------------------------------------------------------------------
132}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800133
134// TODO(b/129481165): remove the #pragma below and fix conversion issues
135#pragma clang diagnostic pop // ignored "-Wconversion"