• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_TAG "libdisplayservicehidl"
18 
19 #include <displayservice/DisplayEventReceiver.h>
20 
21 #include <android-base/logging.h>
22 #include <android/frameworks/displayservice/1.0/BpHwEventCallback.h>
23 
24 #include <thread>
25 #include <ftl/enum.h>
26 
27 namespace android {
28 namespace frameworks {
29 namespace displayservice {
30 namespace V1_0 {
31 namespace implementation {
32 
getLooper()33 sp<Looper> getLooper() {
34     static sp<Looper> looper = []() {
35         sp<Looper> looper = new Looper(false /* allowNonCallbacks */);
36 
37         std::thread{[&](){
38             int pollResult = looper->pollAll(-1 /* timeout */);
39             LOG(ERROR) << "Looper::pollAll returns unexpected " << pollResult;
40         }}.detach();
41 
42         return looper;
43     }();
44 
45     return looper;
46 }
47 
AttachedEvent(const sp<IEventCallback> & callback)48 DisplayEventReceiver::AttachedEvent::AttachedEvent(const sp<IEventCallback> &callback)
49     : mCallback(callback)
50 {
51     mLooperAttached = getLooper()->addFd(mFwkReceiver.getFd(),
52             Looper::POLL_CALLBACK,
53             Looper::EVENT_INPUT,
54             this,
55             nullptr);
56 }
57 
~AttachedEvent()58 DisplayEventReceiver::AttachedEvent::~AttachedEvent() {
59     if (!detach()) {
60         LOG(ERROR) << "Could not remove fd from looper.";
61     }
62 }
63 
detach()64 bool DisplayEventReceiver::AttachedEvent::detach() {
65     if (!valid()) {
66         return true;
67     }
68 
69     return getLooper()->removeFd(mFwkReceiver.getFd());
70 }
71 
valid() const72 bool DisplayEventReceiver::AttachedEvent::valid() const {
73     return mFwkReceiver.initCheck() == OK && mLooperAttached;
74 }
75 
receiver()76 DisplayEventReceiver::FwkReceiver &DisplayEventReceiver::AttachedEvent::receiver() {
77     return mFwkReceiver;
78 }
79 
handleEvent(int fd,int events,void *)80 int DisplayEventReceiver::AttachedEvent::handleEvent(int fd, int events, void* /* data */) {
81     CHECK(fd == mFwkReceiver.getFd());
82 
83     if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
84         LOG(ERROR) << "AttachedEvent handleEvent received error or hangup:" << events;
85         return 0; // remove the callback
86     }
87 
88     if (!(events & Looper::EVENT_INPUT)) {
89         LOG(ERROR) << "AttachedEvent handleEvent unhandled poll event:" << events;
90         return 1; // keep the callback
91     }
92 
93     constexpr size_t SIZE = 1;
94 
95     ssize_t n;
96     FwkReceiver::Event buf[SIZE];
97     while ((n = mFwkReceiver.getEvents(buf, SIZE)) > 0) {
98         for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
99             const FwkReceiver::Event &event = buf[i];
100 
101             android::DisplayEventType type = event.header.type;
102             uint64_t timestamp = event.header.timestamp;
103 
104             switch(buf[i].header.type) {
105                 case DisplayEventType::DISPLAY_EVENT_VSYNC: {
106                     auto ret = mCallback->onVsync(timestamp, event.vsync.count);
107                     if (!ret.isOk()) {
108                         LOG(ERROR) << "AttachedEvent handleEvent fails on onVsync callback"
109                                    << " because of " << ret.description();
110                         return 0;  // remove the callback
111                     }
112                 } break;
113                 case DisplayEventType::DISPLAY_EVENT_HOTPLUG: {
114                     auto ret = mCallback->onHotplug(timestamp, event.hotplug.connected);
115                     if (!ret.isOk()) {
116                         LOG(ERROR) << "AttachedEvent handleEvent fails on onHotplug callback"
117                                    << " because of " << ret.description();
118                         return 0;  // remove the callback
119                     }
120                 } break;
121                 default: {
122                     LOG(ERROR) << "AttachedEvent handleEvent unknown type: "
123                                 << ftl::to_underlying(type);
124                 }
125             }
126         }
127     }
128 
129     return 1; // keep on going
130 }
131 
init(const sp<IEventCallback> & callback)132 Return<Status> DisplayEventReceiver::init(const sp<IEventCallback>& callback) {
133     std::unique_lock<std::mutex> lock(mMutex);
134 
135     if (mAttached != nullptr || callback == nullptr) {
136         return Status::BAD_VALUE;
137     }
138 
139     mAttached = new AttachedEvent(callback);
140 
141     return mAttached->valid() ? Status::SUCCESS : Status::UNKNOWN;
142 }
143 
setVsyncRate(int32_t count)144 Return<Status> DisplayEventReceiver::setVsyncRate(int32_t count) {
145     std::unique_lock<std::mutex> lock(mMutex);
146 
147     if (mAttached == nullptr || count < 0) {
148         return Status::BAD_VALUE;
149     }
150 
151     bool success = OK == mAttached->receiver().setVsyncRate(count);
152     return success ? Status::SUCCESS : Status::UNKNOWN;
153 }
154 
requestNextVsync()155 Return<Status> DisplayEventReceiver::requestNextVsync() {
156     std::unique_lock<std::mutex> lock(mMutex);
157 
158     if (mAttached == nullptr) {
159         return Status::BAD_VALUE;
160     }
161 
162     bool success = OK == mAttached->receiver().requestNextVsync();
163     return success ? Status::SUCCESS : Status::UNKNOWN;
164 }
165 
close()166 Return<Status> DisplayEventReceiver::close() {
167     std::unique_lock<std::mutex> lock(mMutex);
168     if (mAttached == nullptr) {
169         return Status::BAD_VALUE;
170     }
171 
172     bool success = mAttached->detach();
173     mAttached = nullptr;
174 
175     return success ? Status::SUCCESS : Status::UNKNOWN;
176 }
177 
178 }  // namespace implementation
179 }  // namespace V1_0
180 }  // namespace displayservice
181 }  // namespace frameworks
182 }  // namespace android
183