DisplayEventReceiver.cpp 5.4 KB

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