xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Client.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <private/android_filesystem_config.h>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <gui/AidlUtil.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <gui/SchedulingPolicy.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include "Client.h"
28*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/LayerCreationArgs.h"
29*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/LayerHandle.h"
30*38e8c45fSAndroid Build Coastguard Worker #include "Layer.h"
31*38e8c45fSAndroid Build Coastguard Worker #include "SurfaceFlinger.h"
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker namespace android {
34*38e8c45fSAndroid Build Coastguard Worker 
35*38e8c45fSAndroid Build Coastguard Worker using gui::aidl_utils::binderStatusFromStatusT;
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
42*38e8c45fSAndroid Build Coastguard Worker 
Client(const sp<SurfaceFlinger> & flinger)43*38e8c45fSAndroid Build Coastguard Worker Client::Client(const sp<SurfaceFlinger>& flinger)
44*38e8c45fSAndroid Build Coastguard Worker     : mFlinger(flinger)
45*38e8c45fSAndroid Build Coastguard Worker {
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker 
initCheck() const48*38e8c45fSAndroid Build Coastguard Worker status_t Client::initCheck() const {
49*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker 
createSurface(const std::string & name,int32_t flags,const sp<IBinder> & parent,const gui::LayerMetadata & metadata,gui::CreateSurfaceResult * outResult)52*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::createSurface(const std::string& name, int32_t flags,
53*38e8c45fSAndroid Build Coastguard Worker                                      const sp<IBinder>& parent, const gui::LayerMetadata& metadata,
54*38e8c45fSAndroid Build Coastguard Worker                                      gui::CreateSurfaceResult* outResult) {
55*38e8c45fSAndroid Build Coastguard Worker     // We rely on createLayer to check permissions.
56*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> handle;
57*38e8c45fSAndroid Build Coastguard Worker     LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
58*38e8c45fSAndroid Build Coastguard Worker                            static_cast<uint32_t>(flags), std::move(metadata));
59*38e8c45fSAndroid Build Coastguard Worker     args.parentHandle = parent;
60*38e8c45fSAndroid Build Coastguard Worker     const status_t status = mFlinger->createLayer(args, *outResult);
61*38e8c45fSAndroid Build Coastguard Worker     return binderStatusFromStatusT(status);
62*38e8c45fSAndroid Build Coastguard Worker }
63*38e8c45fSAndroid Build Coastguard Worker 
clearLayerFrameStats(const sp<IBinder> & handle)64*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::clearLayerFrameStats(const sp<IBinder>& handle) {
65*38e8c45fSAndroid Build Coastguard Worker     status_t status;
66*38e8c45fSAndroid Build Coastguard Worker     sp<Layer> layer = LayerHandle::getLayer(handle);
67*38e8c45fSAndroid Build Coastguard Worker     if (layer == nullptr) {
68*38e8c45fSAndroid Build Coastguard Worker         status = NAME_NOT_FOUND;
69*38e8c45fSAndroid Build Coastguard Worker     } else {
70*38e8c45fSAndroid Build Coastguard Worker         layer->clearFrameStats();
71*38e8c45fSAndroid Build Coastguard Worker         status = NO_ERROR;
72*38e8c45fSAndroid Build Coastguard Worker     }
73*38e8c45fSAndroid Build Coastguard Worker     return binderStatusFromStatusT(status);
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker 
getLayerFrameStats(const sp<IBinder> & handle,gui::FrameStats * outStats)76*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::getLayerFrameStats(const sp<IBinder>& handle, gui::FrameStats* outStats) {
77*38e8c45fSAndroid Build Coastguard Worker     status_t status;
78*38e8c45fSAndroid Build Coastguard Worker     sp<Layer> layer = LayerHandle::getLayer(handle);
79*38e8c45fSAndroid Build Coastguard Worker     if (layer == nullptr) {
80*38e8c45fSAndroid Build Coastguard Worker         status = NAME_NOT_FOUND;
81*38e8c45fSAndroid Build Coastguard Worker     } else {
82*38e8c45fSAndroid Build Coastguard Worker         FrameStats stats;
83*38e8c45fSAndroid Build Coastguard Worker         layer->getFrameStats(&stats);
84*38e8c45fSAndroid Build Coastguard Worker         outStats->refreshPeriodNano = stats.refreshPeriodNano;
85*38e8c45fSAndroid Build Coastguard Worker         outStats->desiredPresentTimesNano.reserve(stats.desiredPresentTimesNano.size());
86*38e8c45fSAndroid Build Coastguard Worker         for (const auto& t : stats.desiredPresentTimesNano) {
87*38e8c45fSAndroid Build Coastguard Worker             outStats->desiredPresentTimesNano.push_back(t);
88*38e8c45fSAndroid Build Coastguard Worker         }
89*38e8c45fSAndroid Build Coastguard Worker         outStats->actualPresentTimesNano.reserve(stats.actualPresentTimesNano.size());
90*38e8c45fSAndroid Build Coastguard Worker         for (const auto& t : stats.actualPresentTimesNano) {
91*38e8c45fSAndroid Build Coastguard Worker             outStats->actualPresentTimesNano.push_back(t);
92*38e8c45fSAndroid Build Coastguard Worker         }
93*38e8c45fSAndroid Build Coastguard Worker         outStats->frameReadyTimesNano.reserve(stats.frameReadyTimesNano.size());
94*38e8c45fSAndroid Build Coastguard Worker         for (const auto& t : stats.frameReadyTimesNano) {
95*38e8c45fSAndroid Build Coastguard Worker             outStats->frameReadyTimesNano.push_back(t);
96*38e8c45fSAndroid Build Coastguard Worker         }
97*38e8c45fSAndroid Build Coastguard Worker         status = NO_ERROR;
98*38e8c45fSAndroid Build Coastguard Worker     }
99*38e8c45fSAndroid Build Coastguard Worker     return binderStatusFromStatusT(status);
100*38e8c45fSAndroid Build Coastguard Worker }
101*38e8c45fSAndroid Build Coastguard Worker 
mirrorSurface(const sp<IBinder> & mirrorFromHandle,gui::CreateSurfaceResult * outResult)102*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::mirrorSurface(const sp<IBinder>& mirrorFromHandle,
103*38e8c45fSAndroid Build Coastguard Worker                                      gui::CreateSurfaceResult* outResult) {
104*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> handle;
105*38e8c45fSAndroid Build Coastguard Worker     LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), "MirrorRoot",
106*38e8c45fSAndroid Build Coastguard Worker                            0 /* flags */, gui::LayerMetadata());
107*38e8c45fSAndroid Build Coastguard Worker     status_t status = mFlinger->mirrorLayer(args, mirrorFromHandle, *outResult);
108*38e8c45fSAndroid Build Coastguard Worker     return binderStatusFromStatusT(status);
109*38e8c45fSAndroid Build Coastguard Worker }
110*38e8c45fSAndroid Build Coastguard Worker 
mirrorDisplay(int64_t displayId,gui::CreateSurfaceResult * outResult)111*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::mirrorDisplay(int64_t displayId, gui::CreateSurfaceResult* outResult) {
112*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> handle;
113*38e8c45fSAndroid Build Coastguard Worker     LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this),
114*38e8c45fSAndroid Build Coastguard Worker                            "MirrorRoot-" + std::to_string(displayId), 0 /* flags */,
115*38e8c45fSAndroid Build Coastguard Worker                            gui::LayerMetadata());
116*38e8c45fSAndroid Build Coastguard Worker     std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId));
117*38e8c45fSAndroid Build Coastguard Worker     status_t status = mFlinger->mirrorDisplay(*id, args, *outResult);
118*38e8c45fSAndroid Build Coastguard Worker     return binderStatusFromStatusT(status);
119*38e8c45fSAndroid Build Coastguard Worker }
120*38e8c45fSAndroid Build Coastguard Worker 
getSchedulingPolicy(gui::SchedulingPolicy * outPolicy)121*38e8c45fSAndroid Build Coastguard Worker binder::Status Client::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) {
122*38e8c45fSAndroid Build Coastguard Worker     return gui::getSchedulingPolicy(outPolicy);
123*38e8c45fSAndroid Build Coastguard Worker }
124*38e8c45fSAndroid Build Coastguard Worker 
125*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
126*38e8c45fSAndroid Build Coastguard Worker }; // namespace android
127