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