blob: caccd6f605b9790c4940ba945e9a038db49eac19 [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:
32 /* The scheduler handle is a BBinder object passed to the client from which we can extract
33 * an ID for subsequent operations.
34 */
35 class ConnectionHandle : public BBinder {
36 public:
37 ConnectionHandle(int64_t id) : id(id) {}
38 ~ConnectionHandle() = default;
39 const int64_t id;
40 };
41
42 class Connection {
43 public:
44 Connection(sp<ConnectionHandle> handle, sp<BnDisplayEventConnection> eventConnection,
45 std::unique_ptr<EventThread> eventThread)
46 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
47 ~Connection() = default;
48
49 sp<ConnectionHandle> handle;
50 sp<BnDisplayEventConnection> eventConnection;
51 const std::unique_ptr<EventThread> thread;
52 };
53
54 Scheduler() = default;
55 ~Scheduler() = default;
56
57 /** Creates an EventThread connection. */
58 sp<ConnectionHandle> createConnection(
59 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
60 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
61 impl::EventThread::InterceptVSyncsCallback interceptCallback);
62 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle);
63
64 // Getter methods.
65 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
66 sp<BnDisplayEventConnection> getEventConnection(const sp<ConnectionHandle>& handle);
67
68 // Should be called when receiving a hotplug event.
69 void hotplugReceived(const sp<ConnectionHandle>& handle, EventThread::DisplayType displayType,
70 bool connected);
71 // Should be called after the screen is turned on.
72 void onScreenAcquired(const sp<ConnectionHandle>& handle);
73 // Should be called before the screen is turned off.
74 void onScreenReleased(const sp<ConnectionHandle>& handle);
75 // Should be called when dumpsys command is received.
76 void dump(const sp<ConnectionHandle>& handle, String8& result) const;
77 // Offers ability to modify phase offset in the event thread.
78 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
79
80private:
81 static std::atomic<int64_t> sNextId;
82 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
83};
84
85} // namespace android