blob: 4abf027c14179d1b000c895428f4e536d00e9ec6 [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
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
Ana Krulece588e312018-09-18 12:32:24 -070022#include <ui/DisplayStatInfo.h>
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080023#include <ui/GraphicTypes.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070024
25#include "DispSync.h"
Ana Krulece588e312018-09-18 12:32:24 -070026#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070027#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010028#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070029#include "InjectVSyncSource.h"
Ana Krulec3084c052018-11-21 20:27:17 +010030#include "LayerHistory.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010031#include "SchedulerUtils.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070032
33namespace android {
34
Ana Krulece588e312018-09-18 12:32:24 -070035class EventControlThread;
36
Ana Krulec98b5b242018-08-10 15:03:23 -070037class Scheduler {
38public:
Ana Krulec7d1d6832018-12-27 11:10:09 -080039 using ExpiredIdleTimerCallback = std::function<void()>;
Ana Krulec7d1d6832018-12-27 11:10:09 -080040
Ana Krulec7ecce8c2018-10-12 13:44:41 -070041 // Enum to indicate whether to start the transaction early, or at vsync time.
42 enum class TransactionStart { EARLY, NORMAL };
43
Ana Krulec98b5b242018-08-10 15:03:23 -070044 /* The scheduler handle is a BBinder object passed to the client from which we can extract
45 * an ID for subsequent operations.
46 */
47 class ConnectionHandle : public BBinder {
48 public:
49 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070050
Ana Krulec98b5b242018-08-10 15:03:23 -070051 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070052
Ana Krulec98b5b242018-08-10 15:03:23 -070053 const int64_t id;
54 };
55
56 class Connection {
57 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080058 Connection(sp<ConnectionHandle> handle, sp<EventThreadConnection> eventConnection,
Ana Krulec98b5b242018-08-10 15:03:23 -070059 std::unique_ptr<EventThread> eventThread)
60 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070061
Ana Krulec98b5b242018-08-10 15:03:23 -070062 ~Connection() = default;
63
64 sp<ConnectionHandle> handle;
Ana Krulec85c39af2018-12-26 17:29:57 -080065 sp<EventThreadConnection> eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070066 const std::unique_ptr<EventThread> thread;
67 };
68
Ana Krulece588e312018-09-18 12:32:24 -070069 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
70
Ana Krulec0c8cd522018-08-31 12:27:28 -070071 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070072
73 /** Creates an EventThread connection. */
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080074 sp<ConnectionHandle> createConnection(const char* connectionName, int64_t phaseOffsetNs,
75 ResyncCallback, ResetIdleTimerCallback,
76 impl::EventThread::InterceptVSyncsCallback);
Ana Krulece588e312018-09-18 12:32:24 -070077
Dominik Laskowskif654d572018-12-20 11:03:06 -080078 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080079 ResyncCallback,
80 ResetIdleTimerCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070081
82 // Getter methods.
83 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070084
Ana Krulec85c39af2018-12-26 17:29:57 -080085 sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle);
Ana Krulec98b5b242018-08-10 15:03:23 -070086
87 // Should be called when receiving a hotplug event.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080088 void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
Ana Krulec98b5b242018-08-10 15:03:23 -070089 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -070090
Ana Krulec98b5b242018-08-10 15:03:23 -070091 // Should be called after the screen is turned on.
92 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070093
Ana Krulec98b5b242018-08-10 15:03:23 -070094 // Should be called before the screen is turned off.
95 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070096
Ana Krulec98b5b242018-08-10 15:03:23 -070097 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -080098 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -070099
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 // Offers ability to modify phase offset in the event thread.
101 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
102
Ana Krulece588e312018-09-18 12:32:24 -0700103 void getDisplayStatInfo(DisplayStatInfo* stats);
104
105 void enableHardwareVsync();
106 void disableHardwareVsync(bool makeUnavailable);
107 void setVsyncPeriod(const nsecs_t period);
108 void addResyncSample(const nsecs_t timestamp);
109 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
110 void setIgnorePresentFences(bool ignore);
Ana Krulec7ab56032018-11-02 20:51:06 +0100111 void makeHWSyncAvailable(bool makeAvailable);
Ana Krulec3084c052018-11-21 20:27:17 +0100112 // Adds the present time for given layer to the history of present times.
113 void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
114 const std::string layerName);
115 // Increments counter in the layer history to indicate that SF has started a new frame.
116 void incrementFrameCounter();
Ana Krulec7d1d6832018-12-27 11:10:09 -0800117 // Callback that gets invoked once the idle timer expires.
118 void setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback);
Ana Krulecb43429d2019-01-09 14:28:51 -0800119 // Returns relevant information about Scheduler for dumpsys purposes.
120 std::string doDump();
Ana Krulece588e312018-09-18 12:32:24 -0700121
Ana Krulec0c8cd522018-08-31 12:27:28 -0700122protected:
123 virtual std::unique_ptr<EventThread> makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800124 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125 impl::EventThread::InterceptVSyncsCallback interceptCallback);
126
Ana Krulec98b5b242018-08-10 15:03:23 -0700127private:
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800128 // Creates a connection on the given EventThread and forwards the given callbacks.
129 sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&,
130 ResetIdleTimerCallback&&);
131
Ana Krulec7ab56032018-11-02 20:51:06 +0100132 nsecs_t calculateAverage() const;
133 void updateFrameSkipping(const int64_t skipCount);
Ana Krulec3084c052018-11-21 20:27:17 +0100134 // Collects the statistical mean (average) and median between timestamp
135 // intervals for each frame for each layer.
136 void determineLayerTimestampStats(const std::string layerName, const nsecs_t framePresentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100137 // Collects the average difference between timestamps for each frame regardless
138 // of which layer the timestamp came from.
139 void determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime);
Ana Krulecfb772822018-11-30 10:44:07 +0100140 // Function that resets the idle timer.
141 void resetIdleTimer();
142 // Function that is called when the timer expires.
143 void expiredTimerCallback();
Ana Krulec7ab56032018-11-02 20:51:06 +0100144
Ana Krulece588e312018-09-18 12:32:24 -0700145 // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it
146 // should make request to Scheduler to compute next refresh.
147 friend class BufferQueueLayer;
148
149 // If fences from sync Framework are supported.
150 const bool mHasSyncFramework;
151
152 // The offset in nanoseconds to use, when DispSync timestamps present fence
153 // signaling time.
Sundong Ahnd5e08f62018-12-12 20:27:28 +0900154 nsecs_t mDispSyncPresentTimeOffset;
Ana Krulece588e312018-09-18 12:32:24 -0700155
156 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700157 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700158
159 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700160 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700161
162 std::mutex mHWVsyncLock;
163 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
164 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
165
166 std::unique_ptr<DispSync> mPrimaryDispSync;
167 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100168
169 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
170 // a proof of concept. We turn on frame skipping if the difference between the timestamps
171 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
172 nsecs_t mPreviousFrameTimestamp = 0;
173 // Keeping track of whether we are skipping the refresh count. If we want to
174 // simulate 30Hz rendering, we skip every other frame, and this variable is set
175 // to 1.
176 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100177 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100178 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100179
180 LayerHistory mLayerHistory;
Ana Krulecfb772822018-11-30 10:44:07 +0100181
182 // Timer that records time between requests for next vsync. If the time is higher than a given
183 // interval, a callback is fired. Set this variable to >0 to use this feature.
184 int64_t mSetIdleTimerMs = 0;
185 std::unique_ptr<scheduler::IdleTimer> mIdleTimer;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800186
187 std::mutex mCallbackLock;
188 ExpiredIdleTimerCallback mExpiredTimerCallback GUARDED_BY(mCallbackLock);
Ana Krulec98b5b242018-08-10 15:03:23 -0700189};
190
Ana Krulec7ab56032018-11-02 20:51:06 +0100191} // namespace android