Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 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 | */ |
Steven Moreland | 5577d39 | 2017-05-17 18:15:15 -0700 | [diff] [blame] | 16 | |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 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> |
Manasi Navare | 4759f60 | 2025-01-31 00:12:06 +0000 | [diff] [blame] | 25 | #include <ftl/enum.h> |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace frameworks { |
| 29 | namespace displayservice { |
| 30 | namespace V1_0 { |
| 31 | namespace implementation { |
| 32 | |
| 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 | |
Steven Moreland | 5577d39 | 2017-05-17 18:15:15 -0700 | [diff] [blame] | 48 | DisplayEventReceiver::AttachedEvent::AttachedEvent(const sp<IEventCallback> &callback) |
| 49 | : mCallback(callback) |
| 50 | { |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 51 | mLooperAttached = getLooper()->addFd(mFwkReceiver.getFd(), |
Steven Moreland | 5577d39 | 2017-05-17 18:15:15 -0700 | [diff] [blame] | 52 | Looper::POLL_CALLBACK, |
| 53 | Looper::EVENT_INPUT, |
| 54 | this, |
| 55 | nullptr); |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | DisplayEventReceiver::AttachedEvent::~AttachedEvent() { |
| 59 | if (!detach()) { |
| 60 | LOG(ERROR) << "Could not remove fd from looper."; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | bool DisplayEventReceiver::AttachedEvent::detach() { |
| 65 | if (!valid()) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return getLooper()->removeFd(mFwkReceiver.getFd()); |
| 70 | } |
| 71 | |
| 72 | bool DisplayEventReceiver::AttachedEvent::valid() const { |
| 73 | return mFwkReceiver.initCheck() == OK && mLooperAttached; |
| 74 | } |
| 75 | |
| 76 | DisplayEventReceiver::FwkReceiver &DisplayEventReceiver::AttachedEvent::receiver() { |
| 77 | return mFwkReceiver; |
| 78 | } |
| 79 | |
| 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 | |
Steven Moreland | 5577d39 | 2017-05-17 18:15:15 -0700 | [diff] [blame] | 93 | constexpr size_t SIZE = 1; |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 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 | |
Manasi Navare | 4759f60 | 2025-01-31 00:12:06 +0000 | [diff] [blame] | 101 | android::DisplayEventType type = event.header.type; |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 102 | uint64_t timestamp = event.header.timestamp; |
| 103 | |
| 104 | switch(buf[i].header.type) { |
Manasi Navare | 4759f60 | 2025-01-31 00:12:06 +0000 | [diff] [blame] | 105 | case DisplayEventType::DISPLAY_EVENT_VSYNC: { |
Peiyong Lin | 9d2c1d8 | 2018-04-06 15:33:08 -0700 | [diff] [blame] | 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 | } |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 112 | } break; |
Manasi Navare | 4759f60 | 2025-01-31 00:12:06 +0000 | [diff] [blame] | 113 | case DisplayEventType::DISPLAY_EVENT_HOTPLUG: { |
Peiyong Lin | 9d2c1d8 | 2018-04-06 15:33:08 -0700 | [diff] [blame] | 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 | } |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 120 | } break; |
| 121 | default: { |
Manasi Navare | 4759f60 | 2025-01-31 00:12:06 +0000 | [diff] [blame] | 122 | LOG(ERROR) << "AttachedEvent handleEvent unknown type: " |
| 123 | << ftl::to_underlying(type); |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return 1; // keep on going |
| 130 | } |
| 131 | |
| 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 | |
| 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 | |
| 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 | |
| 166 | Return<Status> DisplayEventReceiver::close() { |
Steven Moreland | 5577d39 | 2017-05-17 18:15:15 -0700 | [diff] [blame] | 167 | std::unique_lock<std::mutex> lock(mMutex); |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 168 | if (mAttached == nullptr) { |
| 169 | return Status::BAD_VALUE; |
| 170 | } |
| 171 | |
Steven Moreland | 6ad20f7 | 2017-05-16 17:42:50 -0700 | [diff] [blame] | 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 |