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 <gui/AidlUtil.h>
25 #include <gui/SchedulingPolicy.h>
26
27 #include "Client.h"
28 #include "FrontEnd/LayerCreationArgs.h"
29 #include "FrontEnd/LayerHandle.h"
30 #include "Layer.h"
31 #include "SurfaceFlinger.h"
32
33 namespace android {
34
35 using gui::aidl_utils::binderStatusFromStatusT;
36
37 // ---------------------------------------------------------------------------
38
39 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
40
41 // ---------------------------------------------------------------------------
42
Client(const sp<SurfaceFlinger> & flinger)43 Client::Client(const sp<SurfaceFlinger>& flinger)
44 : mFlinger(flinger)
45 {
46 }
47
initCheck() const48 status_t Client::initCheck() const {
49 return NO_ERROR;
50 }
51
createSurface(const std::string & name,int32_t flags,const sp<IBinder> & parent,const gui::LayerMetadata & metadata,gui::CreateSurfaceResult * outResult)52 binder::Status Client::createSurface(const std::string& name, int32_t flags,
53 const sp<IBinder>& parent, const gui::LayerMetadata& metadata,
54 gui::CreateSurfaceResult* outResult) {
55 // We rely on createLayer to check permissions.
56 sp<IBinder> handle;
57 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
58 static_cast<uint32_t>(flags), std::move(metadata));
59 args.parentHandle = parent;
60 const status_t status = mFlinger->createLayer(args, *outResult);
61 return binderStatusFromStatusT(status);
62 }
63
clearLayerFrameStats(const sp<IBinder> & handle)64 binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
65 status_t status;
66 sp<Layer> layer = LayerHandle::getLayer(handle);
67 if (layer == nullptr) {
68 status = NAME_NOT_FOUND;
69 } else {
70 layer->clearFrameStats();
71 status = NO_ERROR;
72 }
73 return binderStatusFromStatusT(status);
74 }
75
getLayerFrameStats(const sp<IBinder> & handle,gui::FrameStats * outStats)76 binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
77 status_t status;
78 sp<Layer> layer = LayerHandle::getLayer(handle);
79 if (layer == nullptr) {
80 status = NAME_NOT_FOUND;
81 } else {
82 FrameStats stats;
83 layer->getFrameStats(&stats);
84 outStats->refreshPeriodNano = stats.refreshPeriodNano;
85 outStats->desiredPresentTimesNano.reserve(stats.desiredPresentTimesNano.size());
86 for (const auto& t : stats.desiredPresentTimesNano) {
87 outStats->desiredPresentTimesNano.push_back(t);
88 }
89 outStats->actualPresentTimesNano.reserve(stats.actualPresentTimesNano.size());
90 for (const auto& t : stats.actualPresentTimesNano) {
91 outStats->actualPresentTimesNano.push_back(t);
92 }
93 outStats->frameReadyTimesNano.reserve(stats.frameReadyTimesNano.size());
94 for (const auto& t : stats.frameReadyTimesNano) {
95 outStats->frameReadyTimesNano.push_back(t);
96 }
97 status = NO_ERROR;
98 }
99 return binderStatusFromStatusT(status);
100 }
101
mirrorSurface(const sp<IBinder> & mirrorFromHandle,gui::CreateSurfaceResult * outResult)102 binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
103 gui::CreateSurfaceResult* outResult) {
104 sp<IBinder> handle;
105 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot",
106 0 /* flags */, gui::LayerMetadata());
107 status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult);
108 return binderStatusFromStatusT(status);
109 }
110
mirrorDisplay(int64_t displayId,gui::CreateSurfaceResult * outResult)111 binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) {
112 sp<IBinder> handle;
113 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this),
114 "MirrorRoot-" + std::to_string(displayId), 0 /* flags */,
115 gui::LayerMetadata());
116 std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId));
117 status_t status = mFlinger->mirrorDisplay(*id, args, *outResult);
118 return binderStatusFromStatusT(status);
119 }
120
getSchedulingPolicy(gui::SchedulingPolicy * outPolicy)121 binder::Status Client::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) {
122 return gui::getSchedulingPolicy(outPolicy);
123 }
124
125 // ---------------------------------------------------------------------------
126 }; // namespace android
127