Android SurfaceFlinger服务 ----- 消息机制MessageQueue

天下之事常成于困约,而败于奢靡。这篇文章主要讲述Android SurfaceFlinger服务 ----- 消息机制MessageQueue相关的知识,希望能为你提供帮助。
SurfaceFlinger有着自己的消息队列MessageQueue,用来处理显示相关的消息,比如Vsync消息。
相关文件:

  • frameworks/native/services/surfaceflinger/MessageQueue.h
  • frameworks/native/services/surfaceflinger/MessageQueue.cpp
类MessageQueue和Handler定义在MessageQueue.h文件中,代码如下:
class MessageQueue { // 类Handler定义也在这里 class Handler : public MessageHandler { enum { eventMaskInvalidate= 0x1, eventMaskRefresh= 0x2, eventMaskTransaction= 0x4 }; MessageQueue& mQueue; int32_t mEventMask; public: Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { } virtual void handleMessage(const Message& message); void dispatchRefresh(); void dispatchInvalidate(); void dispatchTransaction(); }; friend class Handler; sp< SurfaceFlinger> mFlinger; sp< Looper> mLooper; sp< EventThread> mEventThread; sp< IDisplayEventConnection> mEvents; sp< BitTube> mEventTube; sp< Handler> mHandler; static int cb_eventReceiver(int fd, int events, void* data); int eventReceiver(int fd, int events); public: enum { INVALIDATE= 0, REFRESH= 1, TRANSACTION = 2 }; MessageQueue(); ~MessageQueue(); void init(const sp< SurfaceFlinger> & flinger); void setEventThread(const sp< EventThread> & events); void waitMessage(); status_t postMessage(const sp< MessageBase> & message, nsecs_t reltime=0); // sends INVALIDATE message at next VSYNC void invalidate(); // sends REFRESH message at next VSYNC void refresh(); // sends TRANSACTION message immediately void invalidateTransactionNow(); };

  • mLooper线程循环体,主要用于取消息
  • mHander消息处理类
  • mEventThread、mEvents、mEventTube主要与Vsync消息的分发有关
SurfaceFlinger类中有一个MessageQueue类型的成员变量,定义如下:
// these are thread safe mutable MessageQueue mEventQueue;

  • 因些在SurfaceFlinger被构造的时候就构造一个mEventQueue。
void SurfaceFlinger::onFirstRef() { mEventQueue.init(this); }

  • 调用MessageQueue的init函数进行初始化
void MessageQueue::init(const sp< SurfaceFlinger> & flinger) { mFlinger = flinger; mLooper = new Looper(true); mHandler = new Handler(*this); }

  • 初始化mFlinger、mLooper、mHandler三个成员变量
void SurfaceFlinger::init() {......mSFEventThread = new EventThread(sfVsyncSrc); mEventQueue.setEventThread(mSFEventThread); ......}

  • 在SurfaceFlinger的init函数中调用MessageQueue的setEventThread函数
void MessageQueue::setEventThread(const sp< EventThread> & eventThread) { mEventThread = eventThread; mEvents = eventThread-> createEventConnection(); mEventTube = mEvents-> getDataChannel(); mLooper-> addFd(mEventTube-> getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver, this); }

  • 设置mEventThread、mEvents、mEventTube,并且添加文件句柄到mLooper
【Android SurfaceFlinger服务 ----- 消息机制MessageQueue】消息的监听主要在SurfaceFlinger的run函数里
void SurfaceFlinger::run() { do { waitForEvent(); } while (true); }

  • 死循环一直监听消息
  • 调用waitForEvent进行等待消息事件
void SurfaceFlinger::waitForEvent() { mEventQueue.waitMessage(); }

  • 调用MessageQueue的waitMessage等待消息
void MessageQueue::waitMessage() { do { IPCThreadState::self()-> flushCommands(); int32_t ret = mLooper-> pollOnce(-1); switch (ret) { case Looper::POLL_WAKE: case Looper::POLL_CALLBACK: continue; case Looper::POLL_ERROR: ALOGE("Looper::POLL_ERROR"); case Looper::POLL_TIMEOUT: // timeout (should not happen) continue; default: // should not happen ALOGE("Looper::pollOnce() returned unknown status %d", ret); continue; } } while (true); }

  • waitMessage主要通过调用mLooper的pollOnce方法来监听消息

    推荐阅读