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,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, uint32_t* outTransformHint) {
83     // We rely on createLayer to check permissions.
84     return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
85                                  parentHandle, nullptr, outTransformHint);
86 }
87 
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,uint32_t * outTransformHint)88 status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
89                                          PixelFormat format, uint32_t flags,
90                                          const sp<IGraphicBufferProducer>& parent,
91                                          LayerMetadata metadata, sp<IBinder>* handle,
92                                          sp<IGraphicBufferProducer>* gbp,
93                                          uint32_t* outTransformHint) {
94     if (mFlinger->authenticateSurfaceTexture(parent) == false) {
95         ALOGE("failed to authenticate surface texture");
96         return BAD_VALUE;
97     }
98 
99     const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
100     if (layer == nullptr) {
101         ALOGE("failed to find parent layer");
102         return BAD_VALUE;
103     }
104 
105     return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
106                                  nullptr, layer, outTransformHint);
107 }
108 
mirrorSurface(const sp<IBinder> & mirrorFromHandle,sp<IBinder> * outHandle)109 status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle) {
110     return mFlinger->mirrorLayer(this, mirrorFromHandle, outHandle);
111 }
112 
clearLayerFrameStats(const sp<IBinder> & handle) const113 status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
114     sp<Layer> layer = getLayerUser(handle);
115     if (layer == nullptr) {
116         return NAME_NOT_FOUND;
117     }
118     layer->clearFrameStats();
119     return NO_ERROR;
120 }
121 
getLayerFrameStats(const sp<IBinder> & handle,FrameStats * outStats) const122 status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
123     sp<Layer> layer = getLayerUser(handle);
124     if (layer == nullptr) {
125         return NAME_NOT_FOUND;
126     }
127     layer->getFrameStats(outStats);
128     return NO_ERROR;
129 }
130 
131 // ---------------------------------------------------------------------------
132 }; // namespace android
133 
134 // TODO(b/129481165): remove the #pragma below and fix conversion issues
135 #pragma clang diagnostic pop // ignored "-Wconversion"
136