blob: ee4ec506f7e9256ab3f520e9fb37e17aa1dd6dad [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
42Client::~Client()
43{
Robert Carr5e8e1f02018-02-20 12:34:15 -080044 // We need to post a message to remove our remaining layers rather than
45 // do so directly by acquiring the SurfaceFlinger lock. If we were to
46 // attempt to directly call the lock it becomes effectively impossible
47 // to use sp<Client> while holding the SF lock as descoping it could
48 // then trigger a dead-lock.
49
Mathias Agopiandb403e82012-06-18 16:47:56 -070050 const size_t count = mLayers.size();
51 for (size_t i=0 ; i<count ; i++) {
Robert Carr9524cb32017-02-13 11:32:32 -080052 sp<Layer> l = mLayers.valueAt(i).promote();
Robert Carr5e8e1f02018-02-20 12:34:15 -080053 if (l == nullptr) {
54 continue;
Robert Carr9524cb32017-02-13 11:32:32 -080055 }
Robert Carr5e8e1f02018-02-20 12:34:15 -080056 mFlinger->postMessageAsync(new LambdaMessage([flinger = mFlinger, l]() {
57 flinger->removeLayer(l);
58 }));
Mathias Agopiandb403e82012-06-18 16:47:56 -070059 }
60}
61
62status_t Client::initCheck() const {
63 return NO_ERROR;
64}
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070067{
68 Mutex::Autolock _l(mLock);
Mathias Agopianac9fa422013-02-11 16:40:36 -080069 mLayers.add(handle, layer);
Mathias Agopiandb403e82012-06-18 16:47:56 -070070}
71
Mathias Agopian13127d82013-03-05 17:47:11 -080072void Client::detachLayer(const Layer* layer)
Mathias Agopiandb403e82012-06-18 16:47:56 -070073{
74 Mutex::Autolock _l(mLock);
75 // we do a linear search here, because this doesn't happen often
76 const size_t count = mLayers.size();
77 for (size_t i=0 ; i<count ; i++) {
78 if (mLayers.valueAt(i) == layer) {
79 mLayers.removeItemsAt(i, 1);
80 break;
81 }
82 }
83}
Mathias Agopian13127d82013-03-05 17:47:11 -080084sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
Mathias Agopiandb403e82012-06-18 16:47:56 -070085{
86 Mutex::Autolock _l(mLock);
Mathias Agopian13127d82013-03-05 17:47:11 -080087 sp<Layer> lbc;
88 wp<Layer> layer(mLayers.valueFor(handle));
Mathias Agopiandb403e82012-06-18 16:47:56 -070089 if (layer != 0) {
90 lbc = layer.promote();
Mathias Agopianac9fa422013-02-11 16:40:36 -080091 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
Mathias Agopiandb403e82012-06-18 16:47:56 -070092 }
93 return lbc;
94}
95
96
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097status_t Client::createSurface(
Mathias Agopiandb403e82012-06-18 16:47:56 -070098 const String8& name,
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
rongliuccd34842018-03-14 12:26:23 -0700100 const sp<IBinder>& parentHandle, int32_t windowType, int32_t ownerUid,
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700101 sp<IBinder>* handle,
102 sp<IGraphicBufferProducer>* gbp)
Mathias Agopiandb403e82012-06-18 16:47:56 -0700103{
Robert Carr1f0a16a2016-10-24 16:27:39 -0700104 sp<Layer> parent = nullptr;
105 if (parentHandle != nullptr) {
chaviw161410b02017-07-27 10:46:08 -0700106 auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get());
107 parent = layerHandle->owner.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700108 if (parent == nullptr) {
109 return NAME_NOT_FOUND;
110 }
111 }
112
Robert Carrb89ea9d2018-12-10 13:01:14 -0800113 // We rely on createLayer to check permissions.
Dan Stoza436ccf32018-06-21 12:10:12 -0700114 return mFlinger->createLayer(name, this, w, h, format, flags, windowType,
115 ownerUid, handle, gbp, &parent);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700116}
Mathias Agopianac9fa422013-02-11 16:40:36 -0800117
118status_t Client::destroySurface(const sp<IBinder>& handle) {
119 return mFlinger->onLayerRemoved(this, handle);
Mathias Agopiandb403e82012-06-18 16:47:56 -0700120}
121
Svetoslavd85084b2014-03-20 10:28:31 -0700122status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) 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->clearFrameStats();
128 return NO_ERROR;
129}
130
131status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
132 sp<Layer> layer = getLayerUser(handle);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800133 if (layer == nullptr) {
Svetoslavd85084b2014-03-20 10:28:31 -0700134 return NAME_NOT_FOUND;
135 }
136 layer->getFrameStats(outStats);
137 return NO_ERROR;
138}
139
Mathias Agopiandb403e82012-06-18 16:47:56 -0700140// ---------------------------------------------------------------------------
141}; // namespace android