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,uint32_t,PixelFormat,uint32_t flags,const sp<IBinder> & parentHandle,LayerMetadata metadata,sp<IBinder> * outHandle,sp<IGraphicBufferProducer> *,int32_t * outLayerId,uint32_t * outTransformHint)75 status_t Client::createSurface(const String8& name, uint32_t /* w */, uint32_t /* h */,
76 PixelFormat /* format */, uint32_t flags,
77 const sp<IBinder>& parentHandle, LayerMetadata metadata,
78 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* /* gbp */,
79 int32_t* outLayerId, uint32_t* outTransformHint) {
80 // We rely on createLayer to check permissions.
81 LayerCreationArgs args(mFlinger.get(), this, name.c_str(), flags, std::move(metadata));
82 return mFlinger->createLayer(args, outHandle, parentHandle, outLayerId, nullptr,
83 outTransformHint);
84 }
85
createWithSurfaceParent(const String8 &,uint32_t,uint32_t,PixelFormat,uint32_t,const sp<IGraphicBufferProducer> &,LayerMetadata,sp<IBinder> *,sp<IGraphicBufferProducer> *,int32_t *,uint32_t *)86 status_t Client::createWithSurfaceParent(const String8& /* name */, uint32_t /* w */,
87 uint32_t /* h */, PixelFormat /* format */,
88 uint32_t /* flags */,
89 const sp<IGraphicBufferProducer>& /* parent */,
90 LayerMetadata /* metadata */, sp<IBinder>* /* handle */,
91 sp<IGraphicBufferProducer>* /* gbp */,
92 int32_t* /* outLayerId */,
93 uint32_t* /* outTransformHint */) {
94 // This api does not make sense with blast since SF no longer tracks IGBP. This api should be
95 // removed.
96 return BAD_VALUE;
97 }
98
mirrorSurface(const sp<IBinder> & mirrorFromHandle,sp<IBinder> * outHandle,int32_t * outLayerId)99 status_t Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle, sp<IBinder>* outHandle,
100 int32_t* outLayerId) {
101 LayerCreationArgs args(mFlinger.get(), this, "MirrorRoot", 0 /* flags */, LayerMetadata());
102 return mFlinger->mirrorLayer(args, mirrorFromHandle, outHandle, outLayerId);
103 }
104
clearLayerFrameStats(const sp<IBinder> & handle) const105 status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
106 sp<Layer> layer = getLayerUser(handle);
107 if (layer == nullptr) {
108 return NAME_NOT_FOUND;
109 }
110 layer->clearFrameStats();
111 return NO_ERROR;
112 }
113
getLayerFrameStats(const sp<IBinder> & handle,FrameStats * outStats) const114 status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
115 sp<Layer> layer = getLayerUser(handle);
116 if (layer == nullptr) {
117 return NAME_NOT_FOUND;
118 }
119 layer->getFrameStats(outStats);
120 return NO_ERROR;
121 }
122
123 // ---------------------------------------------------------------------------
124 }; // namespace android
125