• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  #include "EventThread.h"
33  
34  #include <functional>
35  
36  namespace android {
37  
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
wait()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:
LambdaMessage(std::function<void ()> handler)63      explicit LambdaMessage(std::function<void()> handler)
64            : MessageBase(), mHandler(std::move(handler)) {}
65  
handler()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      // TODO(b/128863962): Remove this function once everything is migrated to Scheduler.
89      virtual void setEventThread(EventThread* events, ResyncCallback resyncCallback) = 0;
90      virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
91      virtual void waitMessage() = 0;
92      virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
93      virtual void invalidate() = 0;
94      virtual void refresh() = 0;
95  };
96  
97  // ---------------------------------------------------------------------------
98  
99  namespace impl {
100  
101  class MessageQueue final : public android::MessageQueue {
102      class Handler : public MessageHandler {
103          enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
104          MessageQueue& mQueue;
105          int32_t mEventMask;
106  
107      public:
Handler(MessageQueue & queue)108          explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
109          virtual void handleMessage(const Message& message);
110          void dispatchRefresh();
111          void dispatchInvalidate();
112      };
113  
114      friend class Handler;
115  
116      sp<SurfaceFlinger> mFlinger;
117      sp<Looper> mLooper;
118      android::EventThread* mEventThread;
119      sp<EventThreadConnection> mEvents;
120      gui::BitTube mEventTube;
121      sp<Handler> mHandler;
122  
123      static int cb_eventReceiver(int fd, int events, void* data);
124      int eventReceiver(int fd, int events);
125  
126  public:
127      ~MessageQueue() override = default;
128      void init(const sp<SurfaceFlinger>& flinger) override;
129      void setEventThread(android::EventThread* events, ResyncCallback resyncCallback) override;
130      void setEventConnection(const sp<EventThreadConnection>& connection) override;
131  
132      void waitMessage() override;
133      status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
134  
135      // sends INVALIDATE message at next VSYNC
136      void invalidate() override;
137  
138      // sends REFRESH message at next VSYNC
139      void refresh() override;
140  };
141  
142  // ---------------------------------------------------------------------------
143  
144  } // namespace impl
145  } // namespace android
146  
147  #endif /* ANDROID_MESSAGE_QUEUE_H */
148