Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #pragma once |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <memory> |
| 21 | |
| 22 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 23 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 24 | |
| 25 | #include "DispSync.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 26 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 27 | #include "EventThread.h" |
| 28 | #include "InjectVSyncSource.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 32 | class EventControlThread; |
| 33 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 | class Scheduler { |
| 35 | public: |
Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 36 | // Enum to indicate whether to start the transaction early, or at vsync time. |
| 37 | enum class TransactionStart { EARLY, NORMAL }; |
| 38 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 39 | /* The scheduler handle is a BBinder object passed to the client from which we can extract |
| 40 | * an ID for subsequent operations. |
| 41 | */ |
| 42 | class ConnectionHandle : public BBinder { |
| 43 | public: |
| 44 | ConnectionHandle(int64_t id) : id(id) {} |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 45 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 46 | ~ConnectionHandle() = default; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 47 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 48 | const int64_t id; |
| 49 | }; |
| 50 | |
| 51 | class Connection { |
| 52 | public: |
| 53 | Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection, |
| 54 | std::unique_ptr<EventThread> eventThread) |
| 55 | : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {} |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 56 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 57 | ~Connection() = default; |
| 58 | |
| 59 | sp<ConnectionHandle> handle; |
| 60 | sp<BnDisplayEventConnection> eventConnection; |
| 61 | const std::unique_ptr<EventThread> thread; |
| 62 | }; |
| 63 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 64 | explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function); |
| 65 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | virtual ~Scheduler(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 67 | |
| 68 | /** Creates an EventThread connection. */ |
| 69 | sp<ConnectionHandle> createConnection( |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 70 | const std::string& connectionName, int64_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 71 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 72 | impl::EventThread::InterceptVSyncsCallback interceptCallback); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 73 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 74 | sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle); |
| 75 | |
| 76 | // Getter methods. |
| 77 | EventThread* getEventThread(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 78 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 79 | sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle); |
| 80 | |
| 81 | // Should be called when receiving a hotplug event. |
| 82 | void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType, |
| 83 | bool connected); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 84 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 85 | // Should be called after the screen is turned on. |
| 86 | void onScreenAcquired(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 87 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 88 | // Should be called before the screen is turned off. |
| 89 | void onScreenReleased(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 90 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 91 | // Should be called when dumpsys command is received. |
| 92 | void dump(const sp<ConnectionHandle>& handle, String8& result) const; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 93 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 94 | // Offers ability to modify phase offset in the event thread. |
| 95 | void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset); |
| 96 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 97 | void getDisplayStatInfo(DisplayStatInfo* stats); |
| 98 | |
| 99 | void enableHardwareVsync(); |
| 100 | void disableHardwareVsync(bool makeUnavailable); |
| 101 | void setVsyncPeriod(const nsecs_t period); |
| 102 | void addResyncSample(const nsecs_t timestamp); |
| 103 | void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime); |
| 104 | void setIgnorePresentFences(bool ignore); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 105 | void makeHWSyncAvailable(bool makeAvailable); |
| 106 | void addNewFrameTimestamp(const nsecs_t newFrameTimestamp, bool isAutoTimestamp); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 107 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 108 | protected: |
| 109 | virtual std::unique_ptr<EventThread> makeEventThread( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 110 | const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 111 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 112 | impl::EventThread::InterceptVSyncsCallback interceptCallback); |
| 113 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 114 | private: |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 115 | nsecs_t calculateAverage() const; |
| 116 | void updateFrameSkipping(const int64_t skipCount); |
| 117 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 118 | // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it |
| 119 | // should make request to Scheduler to compute next refresh. |
| 120 | friend class BufferQueueLayer; |
| 121 | |
| 122 | // If fences from sync Framework are supported. |
| 123 | const bool mHasSyncFramework; |
| 124 | |
| 125 | // The offset in nanoseconds to use, when DispSync timestamps present fence |
| 126 | // signaling time. |
| 127 | const nsecs_t mDispSyncPresentTimeOffset; |
| 128 | |
| 129 | // Each connection has it's own ID. This variable keeps track of the count. |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 130 | static std::atomic<int64_t> sNextId; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 131 | |
| 132 | // Connections are stored in a map <connection ID, connection> for easy retrieval. |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 133 | std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 134 | |
| 135 | std::mutex mHWVsyncLock; |
| 136 | bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock); |
| 137 | bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock); |
| 138 | |
| 139 | std::unique_ptr<DispSync> mPrimaryDispSync; |
| 140 | std::unique_ptr<EventControlThread> mEventControlThread; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 141 | |
| 142 | // TODO(b/113612090): The following set of variables needs to be revised. For now, this is |
| 143 | // a proof of concept. We turn on frame skipping if the difference between the timestamps |
| 144 | // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz. |
| 145 | nsecs_t mPreviousFrameTimestamp = 0; |
| 146 | // Keeping track of whether we are skipping the refresh count. If we want to |
| 147 | // simulate 30Hz rendering, we skip every other frame, and this variable is set |
| 148 | // to 1. |
| 149 | int64_t mSkipCount = 0; |
| 150 | static constexpr size_t ARRAY_SIZE = 30; |
| 151 | std::array<int64_t, ARRAY_SIZE> mTimeDifferences; |
| 152 | size_t mCounter = 0; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 155 | } // namespace android |