blob: 0e447d87f2c8c6643c492ac02c4ab6d1fdd6fccf [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
76
Mathias Agopian4d9b8222013-03-12 17:11:48 -070077status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -070078 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
rongliuccd34842018-03-14 12:26:23 -070080 const sp<IBinder>& parentHandle, int32_t windowType, int32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081 sp<IBinder>* handle,
82 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -070083{
Robert Carr1f0a16a2016-10-24 16:27:39 -070084 sp<Layer> parent = nullptr;
85 if (parentHandle != nullptr) {
chaviw161410b02017-07-27 10:46:08 -070086 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
87 parent = layerHandle->owner.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -070088 if (parent == nullptr) {
89 return NAME_NOT_FOUND;
90 }
91 }
92
Robert Carrb89ea9d2018-12-10 13:01:14 -080093 // We rely on createLayer to check permissions.
Dan Stoza436ccf32018-06-21 12:10:12 -070094 return mFlinger->createLayer(name, this, w, h, format, flags, windowType,
95 ownerUid, handle, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -070096}
Mathias Agopianac9fa422013-02-11 16:40:36 -080097
Marissa Wall35187b32019-01-08 10:08:52 -080098status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
99 PixelFormat format, uint32_t flags,
100 const sp<IGraphicBufferProducer>& parent,
101 int32_t windowType, int32_t ownerUid, sp<IBinder>* handle,
102 sp<IGraphicBufferProducer>* gbp) {
103 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
104 return BAD_VALUE;
105 }
106
107 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
108 if (layer == nullptr) {
109 return BAD_VALUE;
110 }
111
112 sp<IBinder> parentHandle = layer->getHandle();
113
114 return createSurface(name, w, h, format, flags, parentHandle, windowType, ownerUid, handle,
115 gbp);
116}
117
Svetoslavd85084b2014-03-20 10:28:31 -0700118status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
119 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800120 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700121 return NAME_NOT_FOUND;
122 }
123 layer->clearFrameStats();
124 return NO_ERROR;
125}
126
127status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
128 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800129 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700130 return NAME_NOT_FOUND;
131 }
132 layer->getFrameStats(outStats);
133 return NO_ERROR;
134}
135
Mathias Agopiandb403e82012-06-18 16:47:56 -0700136// ---------------------------------------------------------------------------
137}; // namespace android