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 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
57 static_cast<uint32_t>(flags), std::move(metadata));
58 args.parentHandle = parent;
59 const status_t status = mFlinger->createLayer(args, *outResult);
60 return binderStatusFromStatusT(status);
61 }
62
clearLayerFrameStats(const sp<IBinder> & handle)63 binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
64 status_t status;
65 sp<Layer> layer = LayerHandle::getLayer(handle);
66 if (layer == nullptr) {
67 status = NAME_NOT_FOUND;
68 } else {
69 layer->clearFrameStats();
70 status = NO_ERROR;
71 }
72 return binderStatusFromStatusT(status);
73 }
74
getLayerFrameStats(const sp<IBinder> & handle,gui::FrameStats * outStats)75 binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
76 status_t status;
77 sp<Layer> layer = LayerHandle::getLayer(handle);
78 if (layer == nullptr) {
79 status = NAME_NOT_FOUND;
80 } else {
81 FrameStats stats;
82 layer->getFrameStats(&stats);
83 outStats->refreshPeriodNano = stats.refreshPeriodNano;
84 outStats->desiredPresentTimesNano.reserve(stats.desiredPresentTimesNano.size());
85 for (const auto& t : stats.desiredPresentTimesNano) {
86 outStats->desiredPresentTimesNano.push_back(t);
87 }
88 outStats->actualPresentTimesNano.reserve(stats.actualPresentTimesNano.size());
89 for (const auto& t : stats.actualPresentTimesNano) {
90 outStats->actualPresentTimesNano.push_back(t);
91 }
92 outStats->frameReadyTimesNano.reserve(stats.frameReadyTimesNano.size());
93 for (const auto& t : stats.frameReadyTimesNano) {
94 outStats->frameReadyTimesNano.push_back(t);
95 }
96 status = NO_ERROR;
97 }
98 return binderStatusFromStatusT(status);
99 }
100
mirrorSurface(const sp<IBinder> & mirrorFromHandle,gui::CreateSurfaceResult * outResult)101 binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
102 gui::CreateSurfaceResult* outResult) {
103 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot",
104 0 /* flags */, gui::LayerMetadata());
105 status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult);
106 return binderStatusFromStatusT(status);
107 }
108
mirrorDisplay(int64_t displayId,gui::CreateSurfaceResult * outResult)109 binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) {
110 LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this),
111 "MirrorRoot-" + std::to_string(displayId), 0 /* flags */,
112 gui::LayerMetadata());
113 const DisplayId id = DisplayId::fromValue(static_cast<uint64_t>(displayId));
114 status_t status = mFlinger->mirrorDisplay(id, args, *outResult);
115 return binderStatusFromStatusT(status);
116 }
117
getSchedulingPolicy(gui::SchedulingPolicy * outPolicy)118 binder::Status Client::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) {
119 return gui::getSchedulingPolicy(outPolicy);
120 }
121
122 // ---------------------------------------------------------------------------
123 }; // namespace android
124