blob: c7ed9b0412d7177c3c20db8b965c2ccc8f34f336 [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,
78 sp<IGraphicBufferProducer>* gbp) {
Robert Carrb89ea9d2018-12-10 13:01:14 -080079 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080080 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Robert Carrc0df3122019-04-11 13:18:21 -070081 parentHandle);
Mathias Agopiandb403e82012-06-18 16:47:56 -070082}
Mathias Agopianac9fa422013-02-11 16:40:36 -080083
Marissa Wall35187b32019-01-08 10:08:52 -080084status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
85 PixelFormat format, uint32_t flags,
86 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080087 LayerMetadata metadata, sp<IBinder>* handle,
Marissa Wall35187b32019-01-08 10:08:52 -080088 sp<IGraphicBufferProducer>* gbp) {
89 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
Marissa Walld1d644b2019-06-13 14:24:27 -070090 ALOGE("failed to authenticate surface texture");
91 // The extra parent layer check below before returning is to help with debugging
92 // b/134888387. Once the bug is fixed the check can be deleted.
93 if ((static_cast<MonitoredProducer*>(parent.get()))->getLayer() == nullptr) {
94 ALOGE("failed to find parent layer");
95 }
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,
106 nullptr, layer);
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