blob: 8d4514b995173b83b9176809d5d8590730261cb1 [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
22#include <gui/ISurfaceComposer.h>
Ana Krulece588e312018-09-18 12:32:24 -070023#include <ui/DisplayStatInfo.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 Krulec7ecce8c2018-10-12 13:44:41 -070039 // Enum to indicate whether to start the transaction early, or at vsync time.
40 enum class TransactionStart { EARLY, NORMAL };
41
Ana Krulec98b5b242018-08-10 15:03:23 -070042 /* 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 Krulece588e312018-09-18 12:32:24 -070048
Ana Krulec98b5b242018-08-10 15:03:23 -070049 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070050
Ana Krulec98b5b242018-08-10 15:03:23 -070051 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 Krulece588e312018-09-18 12:32:24 -070059
Ana Krulec98b5b242018-08-10 15:03:23 -070060 ~Connection() = default;
61
62 sp<ConnectionHandle> handle;
63 sp<BnDisplayEventConnection> eventConnection;
64 const std::unique_ptr<EventThread> thread;
65 };
66
Ana Krulece588e312018-09-18 12:32:24 -070067 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
68
Ana Krulec0c8cd522018-08-31 12:27:28 -070069 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070070
71 /** Creates an EventThread connection. */
72 sp<ConnectionHandle> createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070073 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070074 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
75 impl::EventThread::InterceptVSyncsCallback interceptCallback);
Ana Krulece588e312018-09-18 12:32:24 -070076
Ana Krulec98b5b242018-08-10 15:03:23 -070077 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
78
79 // Getter methods.
80 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070081
Ana Krulec98b5b242018-08-10 15:03:23 -070082 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 Krulece588e312018-09-18 12:32:24 -070087
Ana Krulec98b5b242018-08-10 15:03:23 -070088 // Should be called after the screen is turned on.
89 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070090
Ana Krulec98b5b242018-08-10 15:03:23 -070091 // Should be called before the screen is turned off.
92 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070093
Ana Krulec98b5b242018-08-10 15:03:23 -070094 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -080095 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -070096
Ana Krulec98b5b242018-08-10 15:03:23 -070097 // Offers ability to modify phase offset in the event thread.
98 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
99
Ana Krulece588e312018-09-18 12:32:24 -0700100 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 Krulec7ab56032018-11-02 20:51:06 +0100108 void makeHWSyncAvailable(bool makeAvailable);
Ana Krulec3084c052018-11-21 20:27:17 +0100109 // 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 Krulece588e312018-09-18 12:32:24 -0700114
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115protected:
116 virtual std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000117 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700118 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
119 impl::EventThread::InterceptVSyncsCallback interceptCallback);
120
Ana Krulec98b5b242018-08-10 15:03:23 -0700121private:
Ana Krulec7ab56032018-11-02 20:51:06 +0100122 nsecs_t calculateAverage() const;
123 void updateFrameSkipping(const int64_t skipCount);
Ana Krulec3084c052018-11-21 20:27:17 +0100124 // 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 Krulec3084c052018-11-21 20:27:17 +0100127 // 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 Krulecfb772822018-11-30 10:44:07 +0100130 // Function that resets the idle timer.
131 void resetIdleTimer();
132 // Function that is called when the timer expires.
133 void expiredTimerCallback();
Ana Krulec7ab56032018-11-02 20:51:06 +0100134
Ana Krulece588e312018-09-18 12:32:24 -0700135 // 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 Krulec98b5b242018-08-10 15:03:23 -0700147 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700148
149 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700150 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700151
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 Krulec7ab56032018-11-02 20:51:06 +0100158
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 Krulec434c22d2018-11-28 13:48:36 +0100167 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100168 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100169
170 LayerHistory mLayerHistory;
Ana Krulecfb772822018-11-30 10:44:07 +0100171
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 Krulec98b5b242018-08-10 15:03:23 -0700176};
177
Ana Krulec7ab56032018-11-02 20:51:06 +0100178} // namespace android