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" |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame^] | 28 | #include "IdleTimer.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 29 | #include "InjectVSyncSource.h" |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 30 | #include "LayerHistory.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 31 | #include "SchedulerUtils.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 35 | class EventControlThread; |
| 36 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | class Scheduler { |
| 38 | public: |
Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 39 | // Enum to indicate whether to start the transaction early, or at vsync time. |
| 40 | enum class TransactionStart { EARLY, NORMAL }; |
| 41 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 42 | /* The scheduler handle is a BBinder object passed to the client from which we can extract |
| 43 | * an ID for subsequent operations. |
| 44 | */ |
| 45 | class ConnectionHandle : public BBinder { |
| 46 | public: |
| 47 | ConnectionHandle(int64_t id) : id(id) {} |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 48 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 49 | ~ConnectionHandle() = default; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 50 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 51 | const int64_t id; |
| 52 | }; |
| 53 | |
| 54 | class Connection { |
| 55 | public: |
| 56 | Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection, |
| 57 | std::unique_ptr<EventThread> eventThread) |
| 58 | : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {} |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 59 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 60 | ~Connection() = default; |
| 61 | |
| 62 | sp<ConnectionHandle> handle; |
| 63 | sp<BnDisplayEventConnection> eventConnection; |
| 64 | const std::unique_ptr<EventThread> thread; |
| 65 | }; |
| 66 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 67 | explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function); |
| 68 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 69 | virtual ~Scheduler(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 70 | |
| 71 | /** Creates an EventThread connection. */ |
| 72 | sp<ConnectionHandle> createConnection( |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 73 | const std::string& connectionName, int64_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 74 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 75 | impl::EventThread::InterceptVSyncsCallback interceptCallback); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 76 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 77 | sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle); |
| 78 | |
| 79 | // Getter methods. |
| 80 | EventThread* getEventThread(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 81 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 82 | sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle); |
| 83 | |
| 84 | // Should be called when receiving a hotplug event. |
| 85 | void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType, |
| 86 | bool connected); |
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 after the screen is turned on. |
| 89 | void onScreenAcquired(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 before the screen is turned off. |
| 92 | void onScreenReleased(const sp<ConnectionHandle>& handle); |
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 | // Should be called when dumpsys command is received. |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 95 | void dump(const sp<ConnectionHandle>& handle, std::string& result) const; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 96 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 97 | // Offers ability to modify phase offset in the event thread. |
| 98 | void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset); |
| 99 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 100 | void getDisplayStatInfo(DisplayStatInfo* stats); |
| 101 | |
| 102 | void enableHardwareVsync(); |
| 103 | void disableHardwareVsync(bool makeUnavailable); |
| 104 | void setVsyncPeriod(const nsecs_t period); |
| 105 | void addResyncSample(const nsecs_t timestamp); |
| 106 | void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime); |
| 107 | void setIgnorePresentFences(bool ignore); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 108 | void makeHWSyncAvailable(bool makeAvailable); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 109 | // Adds the present time for given layer to the history of present times. |
| 110 | void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp, |
| 111 | const std::string layerName); |
| 112 | // Increments counter in the layer history to indicate that SF has started a new frame. |
| 113 | void incrementFrameCounter(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 114 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 115 | protected: |
| 116 | virtual std::unique_ptr<EventThread> makeEventThread( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 117 | const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 118 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 119 | impl::EventThread::InterceptVSyncsCallback interceptCallback); |
| 120 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 121 | private: |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 122 | nsecs_t calculateAverage() const; |
| 123 | void updateFrameSkipping(const int64_t skipCount); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 124 | // Collects the statistical mean (average) and median between timestamp |
| 125 | // intervals for each frame for each layer. |
| 126 | void determineLayerTimestampStats(const std::string layerName, const nsecs_t framePresentTime); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 127 | // Collects the average difference between timestamps for each frame regardless |
| 128 | // of which layer the timestamp came from. |
| 129 | void determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame^] | 130 | // Function that resets the idle timer. |
| 131 | void resetIdleTimer(); |
| 132 | // Function that is called when the timer expires. |
| 133 | void expiredTimerCallback(); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 134 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 135 | // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it |
| 136 | // should make request to Scheduler to compute next refresh. |
| 137 | friend class BufferQueueLayer; |
| 138 | |
| 139 | // If fences from sync Framework are supported. |
| 140 | const bool mHasSyncFramework; |
| 141 | |
| 142 | // The offset in nanoseconds to use, when DispSync timestamps present fence |
| 143 | // signaling time. |
| 144 | const nsecs_t mDispSyncPresentTimeOffset; |
| 145 | |
| 146 | // 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] | 147 | static std::atomic<int64_t> sNextId; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 148 | |
| 149 | // Connections are stored in a map <connection ID, connection> for easy retrieval. |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 150 | std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 151 | |
| 152 | std::mutex mHWVsyncLock; |
| 153 | bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock); |
| 154 | bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock); |
| 155 | |
| 156 | std::unique_ptr<DispSync> mPrimaryDispSync; |
| 157 | std::unique_ptr<EventControlThread> mEventControlThread; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 158 | |
| 159 | // TODO(b/113612090): The following set of variables needs to be revised. For now, this is |
| 160 | // a proof of concept. We turn on frame skipping if the difference between the timestamps |
| 161 | // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz. |
| 162 | nsecs_t mPreviousFrameTimestamp = 0; |
| 163 | // Keeping track of whether we are skipping the refresh count. If we want to |
| 164 | // simulate 30Hz rendering, we skip every other frame, and this variable is set |
| 165 | // to 1. |
| 166 | int64_t mSkipCount = 0; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 167 | std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{}; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 168 | size_t mCounter = 0; |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 169 | |
| 170 | LayerHistory mLayerHistory; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame^] | 171 | |
| 172 | // Timer that records time between requests for next vsync. If the time is higher than a given |
| 173 | // interval, a callback is fired. Set this variable to >0 to use this feature. |
| 174 | int64_t mSetIdleTimerMs = 0; |
| 175 | std::unique_ptr<scheduler::IdleTimer> mIdleTimer; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 178 | } // namespace android |