1 /*
2  * Copyright 2020 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 <composer-vts/2.4/GraphicsComposerCallback.h>
18 
19 namespace android::hardware::graphics::composer::V2_4::vts {
20 
setVsyncAllowed(bool allowed)21 void GraphicsComposerCallback::setVsyncAllowed(bool allowed) {
22     std::lock_guard<std::mutex> lock(mMutex);
23     mVsyncAllowed = allowed;
24 }
25 
getDisplays() const26 std::vector<Display> GraphicsComposerCallback::getDisplays() const {
27     std::lock_guard<std::mutex> lock(mMutex);
28     return mDisplays;
29 }
30 
getInvalidHotplugCount() const31 int32_t GraphicsComposerCallback::getInvalidHotplugCount() const {
32     std::lock_guard<std::mutex> lock(mMutex);
33     return mInvalidHotplugCount;
34 }
35 
getInvalidRefreshCount() const36 int32_t GraphicsComposerCallback::getInvalidRefreshCount() const {
37     std::lock_guard<std::mutex> lock(mMutex);
38     return mInvalidRefreshCount;
39 }
40 
getInvalidVsyncCount() const41 int32_t GraphicsComposerCallback::getInvalidVsyncCount() const {
42     std::lock_guard<std::mutex> lock(mMutex);
43     return mInvalidVsyncCount;
44 }
45 
getInvalidVsync_2_4Count() const46 int32_t GraphicsComposerCallback::getInvalidVsync_2_4Count() const {
47     std::lock_guard<std::mutex> lock(mMutex);
48     return mInvalidVsync_2_4Count;
49 }
50 
getInvalidVsyncPeriodChangeCount() const51 int32_t GraphicsComposerCallback::getInvalidVsyncPeriodChangeCount() const {
52     std::lock_guard<std::mutex> lock(mMutex);
53     return mInvalidVsyncPeriodChangeCount;
54 }
55 
getInvalidSeamlessPossibleCount() const56 int32_t GraphicsComposerCallback::getInvalidSeamlessPossibleCount() const {
57     std::lock_guard<std::mutex> lock(mMutex);
58     return mInvalidSeamlessPossibleCount;
59 }
60 
61 std::optional<VsyncPeriodChangeTimeline>
takeLastVsyncPeriodChangeTimeline()62 GraphicsComposerCallback::takeLastVsyncPeriodChangeTimeline() {
63     std::lock_guard<std::mutex> lock(mMutex);
64 
65     std::optional<VsyncPeriodChangeTimeline> ret;
66     ret.swap(mTimeline);
67 
68     return ret;
69 }
70 
onHotplug(Display display,Connection connection)71 Return<void> GraphicsComposerCallback::onHotplug(Display display, Connection connection) {
72     std::lock_guard<std::mutex> lock(mMutex);
73 
74     auto it = std::find(mDisplays.begin(), mDisplays.end(), display);
75     if (connection == Connection::CONNECTED) {
76         if (it == mDisplays.end()) {
77             mDisplays.push_back(display);
78         } else {
79             mInvalidHotplugCount++;
80         }
81     } else if (connection == Connection::DISCONNECTED) {
82         if (it != mDisplays.end()) {
83             mDisplays.erase(it);
84         } else {
85             mInvalidHotplugCount++;
86         }
87     }
88 
89     return Void();
90 }
91 
onRefresh(Display display)92 Return<void> GraphicsComposerCallback::onRefresh(Display display) {
93     std::lock_guard<std::mutex> lock(mMutex);
94 
95     auto it = std::find(mDisplays.begin(), mDisplays.end(), display);
96     if (it == mDisplays.end()) {
97         mInvalidRefreshCount++;
98     }
99 
100     return Void();
101 }
102 
onVsync(Display,int64_t)103 Return<void> GraphicsComposerCallback::onVsync(Display, int64_t) {
104     std::lock_guard<std::mutex> lock(mMutex);
105 
106     // On composer 2.4, onVsync is not expected at all
107     mInvalidVsyncCount++;
108 
109     return Void();
110 }
111 
onVsync_2_4(Display display,int64_t,VsyncPeriodNanos)112 Return<void> GraphicsComposerCallback::onVsync_2_4(Display display, int64_t, VsyncPeriodNanos) {
113     std::lock_guard<std::mutex> lock(mMutex);
114 
115     auto it = std::find(mDisplays.begin(), mDisplays.end(), display);
116     if (!mVsyncAllowed || it == mDisplays.end()) {
117         mInvalidVsync_2_4Count++;
118     }
119 
120     return Void();
121 }
122 
onVsyncPeriodTimingChanged(Display display,const VsyncPeriodChangeTimeline & updatedTimeline)123 Return<void> GraphicsComposerCallback::onVsyncPeriodTimingChanged(
124         Display display, const VsyncPeriodChangeTimeline& updatedTimeline) {
125     std::lock_guard<std::mutex> lock(mMutex);
126 
127     auto it = std::find(mDisplays.begin(), mDisplays.end(), display);
128     if (it == mDisplays.end()) {
129         mInvalidVsyncPeriodChangeCount++;
130     }
131 
132     mTimeline = updatedTimeline;
133 
134     return Void();
135 }
136 
onSeamlessPossible(Display)137 Return<void> GraphicsComposerCallback::onSeamlessPossible(Display) {
138     std::lock_guard<std::mutex> lock(mMutex);
139 
140     mInvalidSeamlessPossibleCount++;
141 
142     return Void();
143 }
144 
145 }  // namespace android::hardware::graphics::composer::V2_4::vts
146