blob: 9927fb66d3080d9b19228ab7d76d86cacbfddd05 [file] [log] [blame]
Steven Moreland6ad20f72017-05-16 17:42:50 -07001/*
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 Moreland5577d392017-05-17 18:15:15 -070016
Steven Moreland6ad20f72017-05-16 17:42:50 -070017#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 Navare4759f602025-01-31 00:12:06 +000025#include <ftl/enum.h>
Steven Moreland6ad20f72017-05-16 17:42:50 -070026
27namespace android {
28namespace frameworks {
29namespace displayservice {
30namespace V1_0 {
31namespace implementation {
32
33sp<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 Moreland5577d392017-05-17 18:15:15 -070048DisplayEventReceiver::AttachedEvent::AttachedEvent(const sp<IEventCallback> &callback)
49 : mCallback(callback)
50{
Steven Moreland6ad20f72017-05-16 17:42:50 -070051 mLooperAttached = getLooper()->addFd(mFwkReceiver.getFd(),
Steven Moreland5577d392017-05-17 18:15:15 -070052 Looper::POLL_CALLBACK,
53 Looper::EVENT_INPUT,
54 this,
55 nullptr);
Steven Moreland6ad20f72017-05-16 17:42:50 -070056}
57
58DisplayEventReceiver::AttachedEvent::~AttachedEvent() {
59 if (!detach()) {
60 LOG(ERROR) << "Could not remove fd from looper.";
61 }
62}
63
64bool DisplayEventReceiver::AttachedEvent::detach() {
65 if (!valid()) {
66 return true;
67 }
68
69 return getLooper()->removeFd(mFwkReceiver.getFd());
70}
71
72bool DisplayEventReceiver::AttachedEvent::valid() const {
73 return mFwkReceiver.initCheck() == OK && mLooperAttached;
74}
75
76DisplayEventReceiver::FwkReceiver &DisplayEventReceiver::AttachedEvent::receiver() {
77 return mFwkReceiver;
78}
79
80int 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 Moreland5577d392017-05-17 18:15:15 -070093 constexpr size_t SIZE = 1;
Steven Moreland6ad20f72017-05-16 17:42:50 -070094
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 Navare4759f602025-01-31 00:12:06 +0000101 android::DisplayEventType type = event.header.type;
Steven Moreland6ad20f72017-05-16 17:42:50 -0700102 uint64_t timestamp = event.header.timestamp;
103
104 switch(buf[i].header.type) {
Manasi Navare4759f602025-01-31 00:12:06 +0000105 case DisplayEventType::DISPLAY_EVENT_VSYNC: {
Peiyong Lin9d2c1d82018-04-06 15:33:08 -0700106 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 Moreland6ad20f72017-05-16 17:42:50 -0700112 } break;
Manasi Navare4759f602025-01-31 00:12:06 +0000113 case DisplayEventType::DISPLAY_EVENT_HOTPLUG: {
Peiyong Lin9d2c1d82018-04-06 15:33:08 -0700114 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 Moreland6ad20f72017-05-16 17:42:50 -0700120 } break;
121 default: {
Manasi Navare4759f602025-01-31 00:12:06 +0000122 LOG(ERROR) << "AttachedEvent handleEvent unknown type: "
123 << ftl::to_underlying(type);
Steven Moreland6ad20f72017-05-16 17:42:50 -0700124 }
125 }
126 }
127 }
128
129 return 1; // keep on going
130}
131
132Return<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
144Return<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
155Return<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
166Return<Status> DisplayEventReceiver::close() {
Steven Moreland5577d392017-05-17 18:15:15 -0700167 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland6ad20f72017-05-16 17:42:50 -0700168 if (mAttached == nullptr) {
169 return Status::BAD_VALUE;
170 }
171
Steven Moreland6ad20f72017-05-16 17:42:50 -0700172 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