MessageQueue.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2009 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. #include <stdint.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <binder/IPCThreadState.h>
  20. #include <utils/threads.h>
  21. #include <utils/Timers.h>
  22. #include <utils/Log.h>
  23. #include <gui/IDisplayEventConnection.h>
  24. #include <gui/BitTube.h>
  25. #include "MessageQueue.h"
  26. #include "EventThread.h"
  27. #include "SurfaceFlinger.h"
  28. namespace android {
  29. // ---------------------------------------------------------------------------
  30. MessageBase::MessageBase()
  31. : MessageHandler() {
  32. }
  33. MessageBase::~MessageBase() {
  34. }
  35. void MessageBase::handleMessage(const Message&) {
  36. this->handler();
  37. barrier.open();
  38. };
  39. // ---------------------------------------------------------------------------
  40. void MessageQueue::Handler::dispatchRefresh() {
  41. if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
  42. mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
  43. }
  44. }
  45. void MessageQueue::Handler::dispatchInvalidate() {
  46. if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
  47. mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
  48. }
  49. }
  50. void MessageQueue::Handler::dispatchTransaction() {
  51. if ((android_atomic_or(eventMaskTransaction, &mEventMask) & eventMaskTransaction) == 0) {
  52. mQueue.mLooper->sendMessage(this, Message(MessageQueue::TRANSACTION));
  53. }
  54. }
  55. void MessageQueue::Handler::handleMessage(const Message& message) {
  56. switch (message.what) {
  57. case INVALIDATE:
  58. android_atomic_and(~eventMaskInvalidate, &mEventMask);
  59. mQueue.mFlinger->onMessageReceived(message.what);
  60. break;
  61. case REFRESH:
  62. android_atomic_and(~eventMaskRefresh, &mEventMask);
  63. mQueue.mFlinger->onMessageReceived(message.what);
  64. break;
  65. case TRANSACTION:
  66. android_atomic_and(~eventMaskTransaction, &mEventMask);
  67. mQueue.mFlinger->onMessageReceived(message.what);
  68. break;
  69. }
  70. }
  71. // ---------------------------------------------------------------------------
  72. MessageQueue::MessageQueue()
  73. {
  74. }
  75. MessageQueue::~MessageQueue() {
  76. }
  77. void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
  78. {
  79. mFlinger = flinger;
  80. mLooper = new Looper(true);
  81. mHandler = new Handler(*this);
  82. }
  83. void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
  84. {
  85. mEventThread = eventThread;
  86. mEvents = eventThread->createEventConnection();
  87. mEventTube = mEvents->getDataChannel();
  88. mLooper->addFd(mEventTube->getFd(), 0, Looper::EVENT_INPUT,
  89. MessageQueue::cb_eventReceiver, this);
  90. }
  91. void MessageQueue::waitMessage() {
  92. do {
  93. IPCThreadState::self()->flushCommands();
  94. int32_t ret = mLooper->pollOnce(-1);
  95. switch (ret) {
  96. case Looper::POLL_WAKE:
  97. case Looper::POLL_CALLBACK:
  98. continue;
  99. case Looper::POLL_ERROR:
  100. ALOGE("Looper::POLL_ERROR");
  101. case Looper::POLL_TIMEOUT:
  102. // timeout (should not happen)
  103. continue;
  104. default:
  105. // should not happen
  106. ALOGE("Looper::pollOnce() returned unknown status %d", ret);
  107. continue;
  108. }
  109. } while (true);
  110. }
  111. status_t MessageQueue::postMessage(
  112. const sp<MessageBase>& messageHandler, nsecs_t relTime)
  113. {
  114. const Message dummyMessage;
  115. if (relTime > 0) {
  116. mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
  117. } else {
  118. mLooper->sendMessage(messageHandler, dummyMessage);
  119. }
  120. return NO_ERROR;
  121. }
  122. /* when INVALIDATE_ON_VSYNC is set SF only processes
  123. * buffer updates on VSYNC and performs a refresh immediately
  124. * after.
  125. *
  126. * when INVALIDATE_ON_VSYNC is set to false, SF will instead
  127. * perform the buffer updates immediately, but the refresh only
  128. * at the next VSYNC.
  129. * THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
  130. */
  131. #define INVALIDATE_ON_VSYNC 1
  132. void MessageQueue::invalidateTransactionNow() {
  133. mHandler->dispatchTransaction();
  134. }
  135. void MessageQueue::invalidate() {
  136. #if INVALIDATE_ON_VSYNC
  137. mEvents->requestNextVsync();
  138. #else
  139. mHandler->dispatchInvalidate();
  140. #endif
  141. }
  142. void MessageQueue::refresh() {
  143. #if INVALIDATE_ON_VSYNC
  144. mHandler->dispatchRefresh();
  145. #else
  146. mEvents->requestNextVsync();
  147. #endif
  148. }
  149. int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
  150. MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
  151. return queue->eventReceiver(fd, events);
  152. }
  153. int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
  154. ssize_t n;
  155. DisplayEventReceiver::Event buffer[8];
  156. while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
  157. for (int i=0 ; i<n ; i++) {
  158. if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
  159. #if INVALIDATE_ON_VSYNC
  160. mHandler->dispatchInvalidate();
  161. #else
  162. mHandler->dispatchRefresh();
  163. #endif
  164. break;
  165. }
  166. }
  167. }
  168. return 1;
  169. }
  170. // ---------------------------------------------------------------------------
  171. }; // namespace android