1 /*
2  * Copyright (C) 2018 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "VideoFrameScheduler"
19 #include <utils/Log.h>
20 #define ATRACE_TAG ATRACE_TAG_VIDEO
21 #include <utils/Trace.h>
22 #include <utils/String16.h>
23 
24 #include <binder/IServiceManager.h>
25 #include <android/gui/ISurfaceComposer.h>
26 #include <android/gui/DisplayStatInfo.h>
27 
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <media/stagefright/foundation/AUtils.h>
30 #include <media/stagefright/VideoFrameScheduler.h>
31 
32 namespace android {
33 
VideoFrameScheduler()34 VideoFrameScheduler::VideoFrameScheduler() : VideoFrameSchedulerBase() {
35 }
36 
updateVsync()37 void VideoFrameScheduler::updateVsync() {
38     mVsyncRefreshAt = systemTime(SYSTEM_TIME_MONOTONIC) + kVsyncRefreshPeriod;
39     mVsyncTime = 0;
40     mVsyncPeriod = 0;
41 
42     // TODO(b/220021255): wrap this into SurfaceComposerClient
43     if (mComposer == NULL) {
44         String16 name("SurfaceFlingerAIDL");
45         sp<IServiceManager> sm = defaultServiceManager();
46         mComposer = interface_cast<gui::ISurfaceComposer>(sm->checkService(name));
47     }
48     if (mComposer != NULL) {
49         gui::DisplayStatInfo stats;
50         binder::Status status = mComposer->getDisplayStats(nullptr/* display */, &stats);
51         if (status.isOk()) {
52             ALOGV("vsync time:%lld period:%lld",
53                     (long long)stats.vsyncTime, (long long)stats.vsyncPeriod);
54             mVsyncTime = stats.vsyncTime;
55             mVsyncPeriod = stats.vsyncPeriod;
56         } else {
57             ALOGW("getDisplayStats returned %d", status.transactionError());
58         }
59     } else {
60         ALOGW("could not get surface mComposer service");
61     }
62 }
63 
release()64 void VideoFrameScheduler::release() {
65     mComposer.clear();
66 }
67 
~VideoFrameScheduler()68 VideoFrameScheduler::~VideoFrameScheduler() {
69     release();
70 }
71 
72 } // namespace android
73