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