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