blob: 8da2e24aa40f0011327b8b630fb054c30a52fd33 [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
24#include "Client.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070026#include "SurfaceFlinger.h"
27
28namespace android {
29
30// ---------------------------------------------------------------------------
31
32const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
33
34// ---------------------------------------------------------------------------
35
36Client::Client(const sp<SurfaceFlinger>& flinger)
Robert Carrb89ea9d2018-12-10 13:01:14 -080037 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070038{
39}
40
Mathias Agopiandb403e82012-06-18 16:47:56 -070041status_t Client::initCheck() const {
42 return NO_ERROR;
43}
44
Mathias Agopian13127d82013-03-05 17:47:11 -080045void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070046{
47 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080048 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070049}
50
Mathias Agopian13127d82013-03-05 17:47:11 -080051void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070052{
53 Mutex::Autolock _l(mLock);
54 // we do a linear search here, because this doesn't happen often
55 const size_t count = mLayers.size();
56 for (size_t i=0 ; i<count ; i++) {
57 if (mLayers.valueAt(i) == layer) {
58 mLayers.removeItemsAt(i, 1);
59 break;
60 }
61 }
62}
Mathias Agopian13127d82013-03-05 17:47:11 -080063sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070064{
65 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080066 sp<Layer> lbc;
67 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070068 if (layer != 0) {
69 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080070 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070071 }
72 return lbc;
73}
74
Evan Rosky1f6d6d52018-12-06 10:47:26 -080075status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
76 uint32_t flags, const sp<IBinder>& parentHandle,
77 LayerMetadata metadata, sp<IBinder>* handle,
Ady Abraham9f0a4002020-10-05 15:47:26 -070078 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +000079 uint32_t* outTransformHint) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080080 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080081 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Ady Abraham9f0a4002020-10-05 15:47:26 -070082 parentHandle, outLayerId, nullptr, outTransformHint);
Mathias Agopiandb403e82012-06-18 16:47:56 -070083}
Mathias Agopianac9fa422013-02-11 16:40:36 -080084
Marissa Wall35187b32019-01-08 10:08:52 -080085status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
86 PixelFormat format, uint32_t flags,
87 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080088 LayerMetadata metadata, sp<IBinder>* handle,
Ady Abraham9f0a4002020-10-05 15:47:26 -070089 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
Valerie Hau1acd6962019-10-28 16:35:48 -070090 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -080091 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
Marissa Walld1d644b2019-06-13 14:24:27 -070092 ALOGE("failed to authenticate surface texture");
Marissa Wall35187b32019-01-08 10:08:52 -080093 return BAD_VALUE;
94 }
95
96 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
97 if (layer == nullptr) {
Marissa Walld1d644b2019-06-13 14:24:27 -070098 ALOGE("failed to find parent layer");
Marissa Wall35187b32019-01-08 10:08:52 -080099 return BAD_VALUE;
100 }
101
Robert Carrc0df3122019-04-11 13:18:21 -0700102 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Ady Abraham9f0a4002020-10-05 15:47:26 -0700103 nullptr, outLayerId, layer, outTransformHint);
Marissa Wall35187b32019-01-08 10:08:52 -0800104}
105
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +0000106status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle,
Ady Abraham9f0a4002020-10-05 15:47:26 -0700107 int32_t* outLayerId) {
108 return mFlinger->mirrorLayer(this, mirrorFromHandle, outHandle, outLayerId);
chaviwfe94a222019-08-21 13:52:59 -0700109}
110
Svetoslavd85084b2014-03-20 10:28:31 -0700111status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
112 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800113 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700114 return NAME_NOT_FOUND;
115 }
116 layer->clearFrameStats();
117 return NO_ERROR;
118}
119
120status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
121 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800122 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700123 return NAME_NOT_FOUND;
124 }
125 layer->getFrameStats(outStats);
126 return NO_ERROR;
127}
128
Mathias Agopiandb403e82012-06-18 16:47:56 -0700129// ---------------------------------------------------------------------------
130}; // namespace android