blob: ea9082456b15eb51c3d41a3ad4559b67eb488133 [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 Krulec434c22d2018-11-28 13:48:36 +010030#include "SchedulerUtils.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070031
32namespace android {
33
Ana Krulece588e312018-09-18 12:32:24 -070034class EventControlThread;
35
Ana Krulec98b5b242018-08-10 15:03:23 -070036class Scheduler {
37public:
Ana Krulec7ecce8c2018-10-12 13:44:41 -070038 // Enum to indicate whether to start the transaction early, or at vsync time.
39 enum class TransactionStart { EARLY, NORMAL };
40
Ana Krulec98b5b242018-08-10 15:03:23 -070041 /* The scheduler handle is a BBinder object passed to the client from which we can extract
42 * an ID for subsequent operations.
43 */
44 class ConnectionHandle : public BBinder {
45 public:
46 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070047
Ana Krulec98b5b242018-08-10 15:03:23 -070048 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070049
Ana Krulec98b5b242018-08-10 15:03:23 -070050 const int64_t id;
51 };
52
53 class Connection {
54 public:
55 Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection,
56 std::unique_ptr<EventThread> eventThread)
57 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070058
Ana Krulec98b5b242018-08-10 15:03:23 -070059 ~Connection() = default;
60
61 sp<ConnectionHandle> handle;
62 sp<BnDisplayEventConnection> eventConnection;
63 const std::unique_ptr<EventThread> thread;
64 };
65
Ana Krulece588e312018-09-18 12:32:24 -070066 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
67
Ana Krulec0c8cd522018-08-31 12:27:28 -070068 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070069
70 /** Creates an EventThread connection. */
71 sp<ConnectionHandle> createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070072 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070073 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
74 impl::EventThread::InterceptVSyncsCallback interceptCallback);
Ana Krulece588e312018-09-18 12:32:24 -070075
Ana Krulec98b5b242018-08-10 15:03:23 -070076 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
77
78 // Getter methods.
79 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070080
Ana Krulec98b5b242018-08-10 15:03:23 -070081 sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle);
82
83 // Should be called when receiving a hotplug event.
84 void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType,
85 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -070086
Ana Krulec98b5b242018-08-10 15:03:23 -070087 // Should be called after the screen is turned on.
88 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070089
Ana Krulec98b5b242018-08-10 15:03:23 -070090 // Should be called before the screen is turned off.
91 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070092
Ana Krulec98b5b242018-08-10 15:03:23 -070093 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -080094 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -070095
Ana Krulec98b5b242018-08-10 15:03:23 -070096 // Offers ability to modify phase offset in the event thread.
97 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
98
Ana Krulece588e312018-09-18 12:32:24 -070099 void getDisplayStatInfo(DisplayStatInfo* stats);
100
101 void enableHardwareVsync();
102 void disableHardwareVsync(bool makeUnavailable);
103 void setVsyncPeriod(const nsecs_t period);
104 void addResyncSample(const nsecs_t timestamp);
105 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
106 void setIgnorePresentFences(bool ignore);
Ana Krulec7ab56032018-11-02 20:51:06 +0100107 void makeHWSyncAvailable(bool makeAvailable);
Ana Krulec3084c052018-11-21 20:27:17 +0100108 // Adds the present time for given layer to the history of present times.
109 void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
110 const std::string layerName);
111 // Increments counter in the layer history to indicate that SF has started a new frame.
112 void incrementFrameCounter();
Ana Krulece588e312018-09-18 12:32:24 -0700113
Ana Krulec0c8cd522018-08-31 12:27:28 -0700114protected:
115 virtual std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000116 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700117 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
118 impl::EventThread::InterceptVSyncsCallback interceptCallback);
119
Ana Krulec98b5b242018-08-10 15:03:23 -0700120private:
Ana Krulec7ab56032018-11-02 20:51:06 +0100121 nsecs_t calculateAverage() const;
122 void updateFrameSkipping(const int64_t skipCount);
Ana Krulec3084c052018-11-21 20:27:17 +0100123 // Collects the statistical mean (average) and median between timestamp
124 // intervals for each frame for each layer.
125 void determineLayerTimestampStats(const std::string layerName, const nsecs_t framePresentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100126 // Collects the average difference between timestamps for each frame regardless
127 // of which layer the timestamp came from.
128 void determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime);
Ana Krulec7ab56032018-11-02 20:51:06 +0100129
Ana Krulece588e312018-09-18 12:32:24 -0700130 // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it
131 // should make request to Scheduler to compute next refresh.
132 friend class BufferQueueLayer;
133
134 // If fences from sync Framework are supported.
135 const bool mHasSyncFramework;
136
137 // The offset in nanoseconds to use, when DispSync timestamps present fence
138 // signaling time.
139 const nsecs_t mDispSyncPresentTimeOffset;
140
141 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700142 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700143
144 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700145 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700146
147 std::mutex mHWVsyncLock;
148 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
149 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
150
151 std::unique_ptr<DispSync> mPrimaryDispSync;
152 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100153
154 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
155 // a proof of concept. We turn on frame skipping if the difference between the timestamps
156 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
157 nsecs_t mPreviousFrameTimestamp = 0;
158 // Keeping track of whether we are skipping the refresh count. If we want to
159 // simulate 30Hz rendering, we skip every other frame, and this variable is set
160 // to 1.
161 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100162 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100163 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100164
165 LayerHistory mLayerHistory;
Ana Krulec98b5b242018-08-10 15:03:23 -0700166};
167
Ana Krulec7ab56032018-11-02 20:51:06 +0100168} // namespace android