blob: 0ca3759f07c6d988a75f52763fd743d0be9e027c [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
20#include <binder/PermissionCache.h>
Mathias Agopian4f4f0942013-08-19 17:26:18 -070021#include <binder/IPCThreadState.h>
Mathias Agopiandb403e82012-06-18 16:47:56 -070022
23#include <private/android_filesystem_config.h>
24
25#include "Client.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026#include "Layer.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070027#include "SurfaceFlinger.h"
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
33const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
34
35// ---------------------------------------------------------------------------
36
37Client::Client(const sp<SurfaceFlinger>& flinger)
Robert Carrb89ea9d2018-12-10 13:01:14 -080038 : mFlinger(flinger)
Mathias Agopiandb403e82012-06-18 16:47:56 -070039{
40}
41
Mathias Agopiandb403e82012-06-18 16:47:56 -070042status_t Client::initCheck() const {
43 return NO_ERROR;
44}
45
Mathias Agopian13127d82013-03-05 17:47:11 -080046void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070047{
48 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080049 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070050}
51
Mathias Agopian13127d82013-03-05 17:47:11 -080052void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070053{
54 Mutex::Autolock _l(mLock);
55 // we do a linear search here, because this doesn't happen often
56 const size_t count = mLayers.size();
57 for (size_t i=0 ; i<count ; i++) {
58 if (mLayers.valueAt(i) == layer) {
59 mLayers.removeItemsAt(i, 1);
60 break;
61 }
62 }
63}
Mathias Agopian13127d82013-03-05 17:47:11 -080064sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070065{
66 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080067 sp<Layer> lbc;
68 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070069 if (layer != 0) {
70 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080071 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070072 }
73 return lbc;
74}
75
Evan Rosky1f6d6d52018-12-06 10:47:26 -080076status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
77 uint32_t flags, const sp<IBinder>& parentHandle,
78 LayerMetadata metadata, sp<IBinder>* handle,
79 sp<IGraphicBufferProducer>* gbp) {
Robert Carr1f0a16a2016-10-24 16:27:39 -070080 sp<Layer> parent = nullptr;
81 if (parentHandle != nullptr) {
chaviw161410b02017-07-27 10:46:08 -070082 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
83 parent = layerHandle->owner.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -070084 if (parent == nullptr) {
85 return NAME_NOT_FOUND;
86 }
87 }
88
Robert Carrb89ea9d2018-12-10 13:01:14 -080089 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080090 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
91 &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -070092}
Mathias Agopianac9fa422013-02-11 16:40:36 -080093
Marissa Wall35187b32019-01-08 10:08:52 -080094status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
95 PixelFormat format, uint32_t flags,
96 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080097 LayerMetadata metadata, sp<IBinder>* handle,
Marissa Wall35187b32019-01-08 10:08:52 -080098 sp<IGraphicBufferProducer>* gbp) {
99 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
100 return BAD_VALUE;
101 }
102
103 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
104 if (layer == nullptr) {
105 return BAD_VALUE;
106 }
107
108 sp<IBinder> parentHandle = layer->getHandle();
109
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800110 return createSurface(name, w, h, format, flags, parentHandle, std::move(metadata), handle, gbp);
Marissa Wall35187b32019-01-08 10:08:52 -0800111}
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