blob: fdafe587844a8ba64335de9cf4a10fcbdc99b2d6 [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);
105
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106protected:
107 virtual std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000108 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700109 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
110 impl::EventThread::InterceptVSyncsCallback interceptCallback);
111
Ana Krulec98b5b242018-08-10 15:03:23 -0700112private:
Ana Krulece588e312018-09-18 12:32:24 -0700113 // TODO(b/113612090): Instead of letting BufferQueueLayer to access mDispSync directly, it
114 // should make request to Scheduler to compute next refresh.
115 friend class BufferQueueLayer;
116
117 // If fences from sync Framework are supported.
118 const bool mHasSyncFramework;
119
120 // The offset in nanoseconds to use, when DispSync timestamps present fence
121 // signaling time.
122 const nsecs_t mDispSyncPresentTimeOffset;
123
124 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700125 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700126
127 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700128 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700129
130 std::mutex mHWVsyncLock;
131 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
132 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
133
134 std::unique_ptr<DispSync> mPrimaryDispSync;
135 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec98b5b242018-08-10 15:03:23 -0700136};
137
138} // namespace android