blob: d7c96516507b41fdd85e0d58e5f568ce4d7bc62c [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>
23
24#include "DispSync.h"
25#include "EventThread.h"
26#include "InjectVSyncSource.h"
27
28namespace android {
29
30class Scheduler {
31public:
Ana Krulec7ecce8c2018-10-12 13:44:41 -070032 // Enum to indicate whether to start the transaction early, or at vsync time.
33 enum class TransactionStart { EARLY, NORMAL };
34
Ana Krulec98b5b242018-08-10 15:03:23 -070035 /* The scheduler handle is a BBinder object passed to the client from which we can extract
36 * an ID for subsequent operations.
37 */
38 class ConnectionHandle : public BBinder {
39 public:
40 ConnectionHandle(int64_t id) : id(id) {}
41 ~ConnectionHandle() = default;
42 const int64_t id;
43 };
44
45 class Connection {
46 public:
47 Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection,
48 std::unique_ptr<EventThread> eventThread)
49 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
50 ~Connection() = default;
51
52 sp<ConnectionHandle> handle;
53 sp<BnDisplayEventConnection> eventConnection;
54 const std::unique_ptr<EventThread> thread;
55 };
56
57 Scheduler() = default;
Ana Krulec0c8cd522018-08-31 12:27:28 -070058 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070059
60 /** Creates an EventThread connection. */
61 sp<ConnectionHandle> createConnection(
Ana Krulec1f027912018-09-10 21:36:25 +000062 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070063 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
64 impl::EventThread::InterceptVSyncsCallback interceptCallback);
65 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
66
67 // Getter methods.
68 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
69 sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle);
70
71 // Should be called when receiving a hotplug event.
72 void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType,
73 bool connected);
74 // Should be called after the screen is turned on.
75 void onScreenAcquired(const sp<ConnectionHandle>& handle);
76 // Should be called before the screen is turned off.
77 void onScreenReleased(const sp<ConnectionHandle>& handle);
78 // Should be called when dumpsys command is received.
79 void dump(const sp<ConnectionHandle>& handle, String8& result) const;
80 // Offers ability to modify phase offset in the event thread.
81 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
82
Ana Krulec0c8cd522018-08-31 12:27:28 -070083protected:
84 virtual std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +000085 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070086 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
87 impl::EventThread::InterceptVSyncsCallback interceptCallback);
88
Ana Krulec98b5b242018-08-10 15:03:23 -070089private:
90 static std::atomic<int64_t> sNextId;
91 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
92};
93
94} // namespace android