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 #include <stdint.h>
18 #include <sys/types.h>
19 
20 #include <binder/IPCThreadState.h>
21 
22 #include <private/android_filesystem_config.h>
23 
24 #include "Client.h"
25 #include "Layer.h"
26 #include "SurfaceFlinger.h"
27 
28 namespace android {
29 
30 // ---------------------------------------------------------------------------
31 
32 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
33 
34 // ---------------------------------------------------------------------------
35 
Client(const sp<SurfaceFlinger> & flinger)36 Client::Client(const sp<SurfaceFlinger>& flinger)
37     : mFlinger(flinger)
38 {
39 }
40 
initCheck() const41 status_t Client::initCheck() const {
42     return NO_ERROR;
43 }
44 
attachLayer(const sp<IBinder> & handle,const sp<Layer> & layer)45 void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
46 {
47     Mutex::Autolock _l(mLock);
48     mLayers.add(handle, layer);
49 }
50 
detachLayer(const Layer * layer)51 void Client::detachLayer(const Layer* layer)
52 {
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 }
getLayerUser(const sp<IBinder> & handle) const63 sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
64 {
65     Mutex::Autolock _l(mLock);
66     sp<Layer> lbc;
67     wp<Layer> layer(mLayers.valueFor(handle));
68     if (layer != 0) {
69         lbc = layer.promote();
70         ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
71     }
72     return lbc;
73 }
74 
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)75 status_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) {
79     // We rely on createLayer to check permissions.
80     return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
81                                  parentHandle);
82 }
83 
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)84 status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
85                                          PixelFormat format, uint32_t flags,
86                                          const sp<IGraphicBufferProducer>& parent,
87                                          LayerMetadata metadata, sp<IBinder>* handle,
88                                          sp<IGraphicBufferProducer>* gbp) {
89     if (mFlinger->authenticateSurfaceTexture(parent) == false) {
90         ALOGE("failed to authenticate surface texture");
91         return BAD_VALUE;
92     }
93 
94     const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
95     if (layer == nullptr) {
96         ALOGE("failed to find parent layer");
97         return BAD_VALUE;
98     }
99 
100     return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
101                                  nullptr, layer);
102 }
103 
clearLayerFrameStats(const sp<IBinder> & handle) const104 status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
105     sp<Layer> layer = getLayerUser(handle);
106     if (layer == nullptr) {
107         return NAME_NOT_FOUND;
108     }
109     layer->clearFrameStats();
110     return NO_ERROR;
111 }
112 
getLayerFrameStats(const sp<IBinder> & handle,FrameStats * outStats) const113 status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
114     sp<Layer> layer = getLayerUser(handle);
115     if (layer == nullptr) {
116         return NAME_NOT_FOUND;
117     }
118     layer->getFrameStats(outStats);
119     return NO_ERROR;
120 }
121 
122 // ---------------------------------------------------------------------------
123 }; // namespace android
124