blob: dd1f24b67a267f9d577d8c6a78a93faa53b6d9c2 [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"
29
30namespace android {
31
Ana Krulece588e312018-09-18 12:32:24 -070032class EventControlThread;
33
Ana Krulec98b5b242018-08-10 15:03:23 -070034class Scheduler {
35public:
Ana Krulec7ecce8c2018-10-12 13:44:41 -070036 // Enum to indicate whether to start the transaction early, or at vsync time.
37 enum class TransactionStart { EARLY, NORMAL };
38
Ana Krulec98b5b242018-08-10 15:03:23 -070039 /* The scheduler handle is a BBinder object passed to the client from which we can extract
40 * an ID for subsequent operations.
41 */
42 class ConnectionHandle : public BBinder {
43 public:
44 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070045
Ana Krulec98b5b242018-08-10 15:03:23 -070046 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070047
Ana Krulec98b5b242018-08-10 15:03:23 -070048 const int64_t id;
49 };
50
51 class Connection {
52 public:
53 Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection,
54 std::unique_ptr<EventThread> eventThread)
55 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070056
Ana Krulec98b5b242018-08-10 15:03:23 -070057 ~Connection() = default;
58
59 sp<ConnectionHandle> handle;
60 sp<BnDisplayEventConnection> eventConnection;
61 const std::unique_ptr<EventThread> thread;
62 };
63
Ana Krulece588e312018-09-18 12:32:24 -070064 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
65
Ana Krulec0c8cd522018-08-31 12:27:28 -070066 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070067
68 /** Creates an EventThread connection. */
69 sp<ConnectionHandle> createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070070 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070071 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
72 impl::EventThread::InterceptVSyncsCallback interceptCallback);
Ana Krulece588e312018-09-18 12:32:24 -070073
Ana Krulec98b5b242018-08-10 15:03:23 -070074 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
75
76 // Getter methods.
77 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070078
Ana Krulec98b5b242018-08-10 15:03:23 -070079 sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle);
80
81 // Should be called when receiving a hotplug event.
82 void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType,
83 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -070084
Ana Krulec98b5b242018-08-10 15:03:23 -070085 // Should be called after the screen is turned on.
86 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070087
Ana Krulec98b5b242018-08-10 15:03:23 -070088 // Should be called before the screen is turned off.
89 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070090
Ana Krulec98b5b242018-08-10 15:03:23 -070091 // Should be called when dumpsys command is received.
92 void dump(const sp<ConnectionHandle>& handle, String8& result) const;
Ana Krulece588e312018-09-18 12:32:24 -070093
Ana Krulec98b5b242018-08-10 15:03:23 -070094 // Offers ability to modify phase offset in the event thread.
95 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
96
Ana Krulece588e312018-09-18 12:32:24 -070097 void getDisplayStatInfo(DisplayStatInfo* stats);
98
99 void enableHardwareVsync();
100 void disableHardwareVsync(bool makeUnavailable);
101 void setVsyncPeriod(const nsecs_t period);
102 void addResyncSample(const nsecs_t timestamp);
103 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
104 void setIgnorePresentFences(bool ignore);
Ana Krulec7ab56032018-11-02 20:51:06 +0100105 void makeHWSyncAvailable(bool makeAvailable);
106 void addNewFrameTimestamp(const nsecs_t newFrameTimestamp, bool isAutoTimestamp);
Ana Krulece588e312018-09-18 12:32:24 -0700107
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108protected:
109 virtual std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000110 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700111 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
112 impl::EventThread::InterceptVSyncsCallback interceptCallback);
113
Ana Krulec98b5b242018-08-10 15:03:23 -0700114private:
Ana Krulec7ab56032018-11-02 20:51:06 +0100115 nsecs_t calculateAverage() const;
116 void updateFrameSkipping(const int64_t skipCount);
117
Ana Krulece588e312018-09-18 12:32:24 -0700118 // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it
119 // should make request to Scheduler to compute next refresh.
120 friend class BufferQueueLayer;
121
122 // If fences from sync Framework are supported.
123 const bool mHasSyncFramework;
124
125 // The offset in nanoseconds to use, when DispSync timestamps present fence
126 // signaling time.
127 const nsecs_t mDispSyncPresentTimeOffset;
128
129 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700130 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700131
132 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700133 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700134
135 std::mutex mHWVsyncLock;
136 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
137 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
138
139 std::unique_ptr<DispSync> mPrimaryDispSync;
140 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100141
142 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
143 // a proof of concept. We turn on frame skipping if the difference between the timestamps
144 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
145 nsecs_t mPreviousFrameTimestamp = 0;
146 // Keeping track of whether we are skipping the refresh count. If we want to
147 // simulate 30Hz rendering, we skip every other frame, and this variable is set
148 // to 1.
149 int64_t mSkipCount = 0;
150 static constexpr size_t ARRAY_SIZE = 30;
151 std::array<int64_t, ARRAY_SIZE> mTimeDifferences;
152 size_t mCounter = 0;
Ana Krulec98b5b242018-08-10 15:03:23 -0700153};
154
Ana Krulec7ab56032018-11-02 20:51:06 +0100155} // namespace android