标签:添加文件 有关 一个 sel val framework ice tran his
SurfaceFlinger有着自己的消息队列MessageQueue,用来处理显示相关的消息,比如Vsync消息。
相关文件:
类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();
};
SurfaceFlinger类中有一个MessageQueue类型的成员变量,定义如下:
// these are thread safe
mutable MessageQueue mEventQueue;
void SurfaceFlinger::onFirstRef()
{
mEventQueue.init(this);
}
void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
{
mFlinger = flinger;
mLooper = new Looper(true);
mHandler = new Handler(*this);
}
void SurfaceFlinger::init() {
......
mSFEventThread = new EventThread(sfVsyncSrc);
mEventQueue.setEventThread(mSFEventThread);
......
}
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);
}
消息的监听主要在SurfaceFlinger的run函数里
void SurfaceFlinger::run() {
do {
waitForEvent();
} while (true);
}
void SurfaceFlinger::waitForEvent() {
mEventQueue.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);
}
Android SurfaceFlinger服务(四) ----- 消息机制MessageQueue
标签:添加文件 有关 一个 sel val framework ice tran his
原文地址:https://www.cnblogs.com/jonnezhang/p/9204680.html