1 /*
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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #include <stdint.h>
22 #include <sys/types.h>
23
24 #include <binder/IPCThreadState.h>
25
26 #include <private/android_filesystem_config.h>
27
28 #include "Client.h"
29 #include "Layer.h"
30 #include "SurfaceFlinger.h"
31
32 namespace android {
33
34 // ---------------------------------------------------------------------------
35
36 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
37
38 // ---------------------------------------------------------------------------
39
Client(const sp<SurfaceFlinger> & flinger)40 Client::Client(const sp<SurfaceFlinger>& flinger)
41 : mFlinger(flinger)
42 {
43 }
44
initCheck() const45 status_t Client::initCheck() const {
46 return NO_ERROR;
47 }
48
attachLayer(const sp<IBinder> & handle,const sp<Layer> & layer)49 void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
50 {
51 Mutex::Autolock _l(mLock);
52 mLayers.add(handle, layer);
53 }
54
detachLayer(const Layer * layer)55 void Client::detachLayer(const Layer* layer)
56 {
57 Mutex::Autolock _l(mLock);
58 // we do a linear search here, because this doesn't happen often
59 const size_t count = mLayers.size();
60 for (size_t i=0 ; i<count ; i++) {
61 if (mLayers.valueAt(i) == layer) {
62 mLayers.removeItemsAt(i, 1);
63 break;
64 }
65 }
66 }
getLayerUser(const sp<IBinder> & handle) const67 sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
68 {
69 Mutex::Autolock _l(mLock);
70 sp<Layer> lbc;
71 wp<Layer> layer(mLayers.valueFor(handle));
72 if (layer != 0) {
73 lbc = layer.promote();
74 ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
75 }
76 return lbc;
77 }
78
createSurface(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,const sp<IBinder> & parentHandle,LayerMetadata metadata,sp<IBinder> * handle,sp<IGraphicBufferProducer> * gbp,int32_t * outLayerId,uint32_t * outTransformHint)79 status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
80 uint32_t flags, const sp<IBinder>& parentHandle,
81 LayerMetadata metadata, sp<IBinder>* handle,
82 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
83 uint32_t* outTransformHint) {
84 // We rely on createLayer to check permissions.
85 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
86 parentHandle, outLayerId, nullptr, outTransformHint);
87 }
88
createWithSurfaceParent(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,const sp<IGraphicBufferProducer> & parent,LayerMetadata metadata,sp<IBinder> * handle,sp<IGraphicBufferProducer> * gbp,int32_t * outLayerId,uint32_t * outTransformHint)89 status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
90 PixelFormat format, uint32_t flags,
91 const sp<IGraphicBufferProducer>& parent,
92 LayerMetadata metadata, sp<IBinder>* handle,
93 sp<IGraphicBufferProducer>* gbp, int32_t* outLayerId,
94 uint32_t* outTransformHint) {
95 if (mFlinger->authenticateSurfaceTexture(parent) == false) {
96 ALOGE("failed to authenticate surface texture");
97 return BAD_VALUE;
98 }
99
100 const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
101 if (layer == nullptr) {
102 ALOGE("failed to find parent layer");
103 return BAD_VALUE;
104 }
105
106 return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
107 nullptr, outLayerId, layer, outTransformHint);
108 }
109
mirrorSurface(const sp<IBinder> & mirrorFromHandle,sp<IBinder> * outHandle,int32_t * outLayerId)110 status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle,
111 int32_t* outLayerId) {
112 return mFlinger->mirrorLayer(this, mirrorFromHandle, outHandle, outLayerId);
113 }
114
clearLayerFrameStats(const sp<IBinder> & handle) const115 status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
116 sp<Layer> layer = getLayerUser(handle);
117 if (layer == nullptr) {
118 return NAME_NOT_FOUND;
119 }
120 layer->clearFrameStats();
121 return NO_ERROR;
122 }
123
getLayerFrameStats(const sp<IBinder> & handle,FrameStats * outStats) const124 status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
125 sp<Layer> layer = getLayerUser(handle);
126 if (layer == nullptr) {
127 return NAME_NOT_FOUND;
128 }
129 layer->getFrameStats(outStats);
130 return NO_ERROR;
131 }
132
133 // ---------------------------------------------------------------------------
134 }; // namespace android
135
136 // TODO(b/129481165): remove the #pragma below and fix conversion issues
137 #pragma clang diagnostic pop // ignored "-Wconversion"
138