• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mock/MockDispSync.h"
18 #include <thread>
19 
20 using namespace std::chrono_literals;
21 namespace android {
22 namespace mock {
23 
24 // Explicit default instantiation is recommended.
25 DispSync::DispSync() = default;
26 DispSync::~DispSync() = default;
27 
addEventListener(const char *,nsecs_t phase,Callback * callback,nsecs_t)28 status_t DispSync::addEventListener(const char* /*name*/, nsecs_t phase, Callback* callback,
29                                     nsecs_t /*lastCallbackTime*/) {
30     if (mCallback.callback != nullptr) {
31         return BAD_VALUE;
32     }
33 
34     mCallback = {callback, phase};
35     return NO_ERROR;
36 }
removeEventListener(Callback * callback,nsecs_t *)37 status_t DispSync::removeEventListener(Callback* callback, nsecs_t* /*outLastCallback*/) {
38     if (mCallback.callback != callback) {
39         return BAD_VALUE;
40     }
41 
42     mCallback = {nullptr, 0};
43     return NO_ERROR;
44 }
45 
changePhaseOffset(Callback * callback,nsecs_t phase)46 status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
47     if (mCallback.callback != callback) {
48         return BAD_VALUE;
49     }
50 
51     mCallback.phase = phase;
52     return NO_ERROR;
53 }
54 
triggerCallback()55 void DispSync::triggerCallback() {
56     if (mCallback.callback == nullptr) return;
57 
58     const std::chrono::nanoseconds now = std::chrono::steady_clock::now().time_since_epoch();
59     const auto expectedVSyncTime = now + 16ms;
60     mCallback.callback->onDispSyncEvent(now.count(), expectedVSyncTime.count());
61 }
62 
63 } // namespace mock
64 } // namespace android
65