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 <errno.h>
     21 #include <stdint.h>
     22 #include <sys/types.h>
     23 
     24 #include <utils/Looper.h>
     25 #include <utils/Timers.h>
     26 #include <utils/threads.h>
     27 
     28 #include <gui/IDisplayEventConnection.h>
     29 #include <private/gui/BitTube.h>
     30 
     31 #include "Barrier.h"
     32 
     33 #include <functional>
     34 
     35 namespace android {
     36 
     37 class EventThread;
     38 class SurfaceFlinger;
     39 
     40 // ---------------------------------------------------------------------------
     41 
     42 class MessageBase : public MessageHandler {
     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 class LambdaMessage : public MessageBase {
     62 public:
     63     explicit LambdaMessage(std::function<void()> handler)
     64           : MessageBase(), mHandler(std::move(handler)) {}
     65 
     66     bool handler() override {
     67         mHandler();
     68         // This return value is no longer checked, so it's always safe to return true
     69         return true;
     70     }
     71 
     72 private:
     73     const std::function<void()> mHandler;
     74 };
     75 
     76 // ---------------------------------------------------------------------------
     77 
     78 class MessageQueue {
     79 public:
     80     enum {
     81         INVALIDATE = 0,
     82         REFRESH = 1,
     83     };
     84 
     85     virtual ~MessageQueue();
     86 
     87     virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
     88     virtual void setEventThread(EventThread* events) = 0;
     89     virtual void waitMessage() = 0;
     90     virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
     91     virtual void invalidate() = 0;
     92     virtual void refresh() = 0;
     93 };
     94 
     95 // ---------------------------------------------------------------------------
     96 
     97 namespace impl {
     98 
     99 class MessageQueue final : public android::MessageQueue {
    100     class Handler : public MessageHandler {
    101         enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
    102         MessageQueue& mQueue;
    103         int32_t mEventMask;
    104 
    105     public:
    106         explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
    107         virtual void handleMessage(const Message& message);
    108         void dispatchRefresh();
    109         void dispatchInvalidate();
    110     };
    111 
    112     friend class Handler;
    113 
    114     sp<SurfaceFlinger> mFlinger;
    115     sp<Looper> mLooper;
    116     android::EventThread* mEventThread;
    117     sp<IDisplayEventConnection> mEvents;
    118     gui::BitTube mEventTube;
    119     sp<Handler> mHandler;
    120 
    121     static int cb_eventReceiver(int fd, int events, void* data);
    122     int eventReceiver(int fd, int events);
    123 
    124 public:
    125     ~MessageQueue() override = default;
    126     void init(const sp<SurfaceFlinger>& flinger) override;
    127     void setEventThread(android::EventThread* events) override;
    128 
    129     void waitMessage() override;
    130     status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
    131 
    132     // sends INVALIDATE message at next VSYNC
    133     void invalidate() override;
    134     // sends REFRESH message at next VSYNC
    135     void refresh() override;
    136 };
    137 
    138 // ---------------------------------------------------------------------------
    139 
    140 } // namespace impl
    141 } // namespace android
    142 
    143 #endif /* ANDROID_MESSAGE_QUEUE_H */
    144