| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 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 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 17 | #pragma once | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 18 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 19 | #include <cstdint> | 
|  | 20 | #include <future> | 
|  | 21 | #include <type_traits> | 
|  | 22 | #include <utility> | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 23 |  | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 24 | #include <utils/Looper.h> | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 25 | #include <utils/Timers.h> | 
|  | 26 | #include <utils/threads.h> | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 27 |  | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 28 | #include <gui/IDisplayEventConnection.h> | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 29 | #include <private/gui/BitTube.h> | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 30 |  | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 31 | #include "EventThread.h" | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 |  | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 35 | class SurfaceFlinger; | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 36 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 37 | template <typename F> | 
|  | 38 | class Task : public MessageHandler { | 
|  | 39 | template <typename G> | 
|  | 40 | friend auto makeTask(G&&); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 41 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 42 | explicit Task(F&& f) : mTask(std::move(f)) {} | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 43 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 44 | void handleMessage(const Message&) override { mTask(); } | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 45 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 46 | using T = std::invoke_result_t<F>; | 
|  | 47 | std::packaged_task<T()> mTask; | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 48 | }; | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 49 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 50 | template <typename F> | 
|  | 51 | inline auto makeTask(F&& f) { | 
|  | 52 | sp<Task<F>> task = new Task<F>(std::move(f)); | 
|  | 53 | return std::make_pair(task, task->mTask.get_future()); | 
|  | 54 | } | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 55 |  | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 56 | class MessageQueue { | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 57 | public: | 
|  | 58 | enum { | 
|  | 59 | INVALIDATE = 0, | 
|  | 60 | REFRESH = 1, | 
|  | 61 | }; | 
|  | 62 |  | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 63 | virtual ~MessageQueue() = default; | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | virtual void init(const sp<SurfaceFlinger>& flinger) = 0; | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 66 | virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0; | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 67 | virtual void waitMessage() = 0; | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 68 | virtual void postMessage(sp<MessageHandler>&&) = 0; | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 69 | virtual void invalidate() = 0; | 
|  | 70 | virtual void refresh() = 0; | 
|  | 71 | }; | 
|  | 72 |  | 
|  | 73 | // --------------------------------------------------------------------------- | 
|  | 74 |  | 
|  | 75 | namespace impl { | 
|  | 76 |  | 
|  | 77 | class MessageQueue final : public android::MessageQueue { | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 78 | class Handler : public MessageHandler { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 79 | enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 }; | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 80 | MessageQueue& mQueue; | 
|  | 81 | int32_t mEventMask; | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 82 | std::atomic<nsecs_t> mExpectedVSyncTime; | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 83 |  | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 84 | public: | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 85 | explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {} | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 86 | virtual void handleMessage(const Message& message); | 
| Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 87 | void dispatchRefresh(); | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 88 | void dispatchInvalidate(nsecs_t expectedVSyncTimestamp); | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 89 | }; | 
|  | 90 |  | 
|  | 91 | friend class Handler; | 
|  | 92 |  | 
|  | 93 | sp<SurfaceFlinger> mFlinger; | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 94 | sp<Looper> mLooper; | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 95 | sp<EventThreadConnection> mEvents; | 
| Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 96 | gui::BitTube mEventTube; | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 97 | sp<Handler> mHandler; | 
|  | 98 |  | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 99 | static int cb_eventReceiver(int fd, int events, void* data); | 
|  | 100 | int eventReceiver(int fd, int events); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 101 |  | 
| Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 102 | public: | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 103 | ~MessageQueue() override = default; | 
|  | 104 | void init(const sp<SurfaceFlinger>& flinger) override; | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 105 | void setEventConnection(const sp<EventThreadConnection>& connection) override; | 
| Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 106 |  | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 107 | void waitMessage() override; | 
| Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 108 | void postMessage(sp<MessageHandler>&&) override; | 
| Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | // sends INVALIDATE message at next VSYNC | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 111 | void invalidate() override; | 
| Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 112 |  | 
| Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 113 | // sends REFRESH message at next VSYNC | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 114 | void refresh() override; | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 115 | }; | 
|  | 116 |  | 
| Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 117 | } // namespace impl | 
|  | 118 | } // namespace android |