MessageQueue.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef ANDROID_MESSAGE_QUEUE_H
  17. #define ANDROID_MESSAGE_QUEUE_H
  18. #include <stdint.h>
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <utils/threads.h>
  22. #include <utils/Timers.h>
  23. #include <utils/Looper.h>
  24. #include <gui/DisplayEventReceiver.h>
  25. #include "Barrier.h"
  26. namespace android {
  27. class IDisplayEventConnection;
  28. class EventThread;
  29. class SurfaceFlinger;
  30. // ---------------------------------------------------------------------------
  31. class MessageBase : public MessageHandler
  32. {
  33. public:
  34. MessageBase();
  35. // return true if message has a handler
  36. virtual bool handler() = 0;
  37. // waits for the handler to be processed
  38. void wait() const { barrier.wait(); }
  39. protected:
  40. virtual ~MessageBase();
  41. private:
  42. virtual void handleMessage(const Message& message);
  43. mutable Barrier barrier;
  44. };
  45. // ---------------------------------------------------------------------------
  46. class MessageQueue {
  47. class Handler : public MessageHandler {
  48. enum {
  49. eventMaskInvalidate = 0x1,
  50. eventMaskRefresh = 0x2,
  51. eventMaskTransaction = 0x4
  52. };
  53. MessageQueue& mQueue;
  54. int32_t mEventMask;
  55. public:
  56. Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { }
  57. virtual void handleMessage(const Message& message);
  58. void dispatchRefresh();
  59. void dispatchInvalidate();
  60. void dispatchTransaction();
  61. };
  62. friend class Handler;
  63. sp<SurfaceFlinger> mFlinger;
  64. sp<Looper> mLooper;
  65. sp<EventThread> mEventThread;
  66. sp<IDisplayEventConnection> mEvents;
  67. sp<BitTube> mEventTube;
  68. sp<Handler> mHandler;
  69. static int cb_eventReceiver(int fd, int events, void* data);
  70. int eventReceiver(int fd, int events);
  71. public:
  72. enum {
  73. INVALIDATE = 0,
  74. REFRESH = 1,
  75. TRANSACTION = 2
  76. };
  77. MessageQueue();
  78. ~MessageQueue();
  79. void init(const sp<SurfaceFlinger>& flinger);
  80. void setEventThread(const sp<EventThread>& events);
  81. void waitMessage();
  82. status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
  83. // sends INVALIDATE message at next VSYNC
  84. void invalidate();
  85. // sends REFRESH message at next VSYNC
  86. void refresh();
  87. // sends TRANSACTION message immediately
  88. void invalidateTransactionNow();
  89. };
  90. // ---------------------------------------------------------------------------
  91. }; // namespace android
  92. #endif /* ANDROID_MESSAGE_QUEUE_H */