blob: e54b4605a4ee34e93a200063aace0e2d79a37eb5 [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) {
Marissa Wall2d814fb2019-04-09 18:52:57 +000079 sp<Layer> parent = nullptr;
80 if (parentHandle != nullptr) {
81 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
82 parent = layerHandle->owner.promote();
83 if (parent == nullptr) {
84 return NAME_NOT_FOUND;
85 }
86 }
87
Robert Carrb89ea9d2018-12-10 13:01:14 -080088 // We rely on createLayer to check permissions.
Evan Rosky1f6d6d52018-12-06 10:47:26 -080089 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
Marissa Wall2d814fb2019-04-09 18:52:57 +000090 &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -070091}
Mathias Agopianac9fa422013-02-11 16:40:36 -080092
Marissa Wall35187b32019-01-08 10:08:52 -080093status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
94 PixelFormat format, uint32_t flags,
95 const sp<IGraphicBufferProducer>& parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -080096 LayerMetadata metadata, sp<IBinder>* handle,
Marissa Wall35187b32019-01-08 10:08:52 -080097 sp<IGraphicBufferProducer>* gbp) {
98 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
99 return BAD_VALUE;
100 }
101
102 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
103 if (layer == nullptr) {
104 return BAD_VALUE;
105 }
106
107 sp<IBinder> parentHandle = layer->getHandle();
108
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800109 return createSurface(name, w, h, format, flags, parentHandle, std::move(metadata), handle, gbp);
Marissa Wall35187b32019-01-08 10:08:52 -0800110}
111
Svetoslavd85084b2014-03-20 10:28:31 -0700112status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
113 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800114 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700115 return NAME_NOT_FOUND;
116 }
117 layer->clearFrameStats();
118 return NO_ERROR;
119}
120
121status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
122 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800123 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700124 return NAME_NOT_FOUND;
125 }
126 layer->getFrameStats(outStats);
127 return NO_ERROR;
128}
129
Mathias Agopiandb403e82012-06-18 16:47:56 -0700130// ---------------------------------------------------------------------------
131}; // namespace android