blob: adfc07100351f4521a6095cd47aa738a70e11a6b [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"
28#include "InjectVSyncSource.h"
Ana Krulec3084c052018-11-21 20:27:17 +010029#include "LayerHistory.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070030
31namespace android {
32
Ana Krulece588e312018-09-18 12:32:24 -070033class EventControlThread;
34
Ana Krulec98b5b242018-08-10 15:03:23 -070035class Scheduler {
36public:
Ana Krulec7ecce8c2018-10-12 13:44:41 -070037 // Enum to indicate whether to start the transaction early, or at vsync time.
38 enum class TransactionStart { EARLY, NORMAL };
39
Ana Krulec98b5b242018-08-10 15:03:23 -070040 /* 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 Krulece588e312018-09-18 12:32:24 -070046
Ana Krulec98b5b242018-08-10 15:03:23 -070047 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070048
Ana Krulec98b5b242018-08-10 15:03:23 -070049 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 Krulece588e312018-09-18 12:32:24 -070057
Ana Krulec98b5b242018-08-10 15:03:23 -070058 ~Connection() = default;
59
60 sp<ConnectionHandle> handle;
61 sp<BnDisplayEventConnection> eventConnection;
62 const std::unique_ptr<EventThread> thread;
63 };
64
Ana Krulece588e312018-09-18 12:32:24 -070065 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
66
Ana Krulec0c8cd522018-08-31 12:27:28 -070067 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070068
69 /** Creates an EventThread connection. */
70 sp<ConnectionHandle> createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070071 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070072 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
73 impl::EventThread::InterceptVSyncsCallback interceptCallback);
Ana Krulece588e312018-09-18 12:32:24 -070074
Ana Krulec98b5b242018-08-10 15:03:23 -070075 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
76
77 // Getter methods.
78 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070079
Ana Krulec98b5b242018-08-10 15:03:23 -070080 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 Krulece588e312018-09-18 12:32:24 -070085
Ana Krulec98b5b242018-08-10 15:03:23 -070086 // Should be called after the screen is turned on.
87 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070088
Ana Krulec98b5b242018-08-10 15:03:23 -070089 // Should be called before the screen is turned off.
90 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070091
Ana Krulec98b5b242018-08-10 15:03:23 -070092 // Should be called when dumpsys command is received.
93 void dump(const sp<ConnectionHandle>& handle, String8& result) const;
Ana Krulece588e312018-09-18 12:32:24 -070094
Ana Krulec98b5b242018-08-10 15:03:23 -070095 // Offers ability to modify phase offset in the event thread.
96 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
97
Ana Krulece588e312018-09-18 12:32:24 -070098 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 Krulec7ab56032018-11-02 20:51:06 +0100106 void makeHWSyncAvailable(bool makeAvailable);
Ana Krulec3084c052018-11-21 20:27:17 +0100107 // 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 Krulece588e312018-09-18 12:32:24 -0700112
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113protected:
Ana Krulec3084c052018-11-21 20:27:17 +0100114 friend class SchedulerTest;
115
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116 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);
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 Krulec7ab56032018-11-02 20:51:06 +0100133
Ana Krulece588e312018-09-18 12:32:24 -0700134 // 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 Krulec98b5b242018-08-10 15:03:23 -0700146 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700147
148 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700149 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700150
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 Krulec7ab56032018-11-02 20:51:06 +0100157
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 Krulec3084c052018-11-21 20:27:17 +0100169
170 LayerHistory mLayerHistory;
Ana Krulec98b5b242018-08-10 15:03:23 -0700171};
172
Ana Krulec7ab56032018-11-02 20:51:06 +0100173} // namespace android