码迷,mamicode.com
首页 > 移动开发 > 详细

Android SurfaceFlinger服务(四) ----- 消息机制MessageQueue

时间:2018-06-20 18:41:50      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:添加文件   有关   一个   sel   val   framework   ice   tran   his   

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

消息的监听主要在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方法来监听消息

Android SurfaceFlinger服务(四) ----- 消息机制MessageQueue

标签:添加文件   有关   一个   sel   val   framework   ice   tran   his   

原文地址:https://www.cnblogs.com/jonnezhang/p/9204680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!