blob: bb85f4c5a9f20bb1c0d34398e1bd52b6633fae07 [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#include "Scheduler.h"
18
19#include <cinttypes>
20#include <cstdint>
21#include <memory>
22
23#include <gui/ISurfaceComposer.h>
24
25#include "DispSync.h"
26#include "DispSyncSource.h"
27#include "EventThread.h"
28#include "InjectVSyncSource.h"
29
30namespace android {
31
Ana Krulec0c8cd522018-08-31 12:27:28 -070032#define RETURN_VALUE_IF_INVALID(value) \
33 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
34#define RETURN_IF_INVALID() \
35 if (handle == nullptr || mConnections.count(handle->id) == 0) return
36
Ana Krulec98b5b242018-08-10 15:03:23 -070037std::atomic<int64_t> Scheduler::sNextId = 0;
38
Ana Krulec0c8cd522018-08-31 12:27:28 -070039Scheduler::~Scheduler() = default;
40
Ana Krulec98b5b242018-08-10 15:03:23 -070041sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
42 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
43 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
44 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
45 const int64_t id = sNextId++;
46 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
47
Ana Krulec98b5b242018-08-10 15:03:23 -070048 std::unique_ptr<EventThread> eventThread =
Ana Krulec0c8cd522018-08-31 12:27:28 -070049 makeEventThread(connectionName, dispSync, phaseOffsetNs, resyncCallback,
50 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070051 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
52 eventThread->createEventConnection(),
53 std::move(eventThread));
54 mConnections.insert(std::make_pair(id, std::move(connection)));
55 return mConnections[id]->handle;
56}
57
Ana Krulec0c8cd522018-08-31 12:27:28 -070058std::unique_ptr<EventThread> Scheduler::makeEventThread(
59 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
60 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
61 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
62 std::unique_ptr<VSyncSource> eventThreadSource =
63 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
64 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
65 interceptCallback, connectionName);
66}
67
Ana Krulec98b5b242018-08-10 15:03:23 -070068sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
69 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070070 RETURN_VALUE_IF_INVALID(nullptr);
71 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -070072}
73
74EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070075 RETURN_VALUE_IF_INVALID(nullptr);
76 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -070077}
78
79sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070080 RETURN_VALUE_IF_INVALID(nullptr);
81 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070082}
83
84void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
85 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070086 RETURN_IF_INVALID();
87 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -070088}
89
90void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070091 RETURN_IF_INVALID();
92 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -070093}
94
95void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070096 RETURN_IF_INVALID();
97 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -070098}
99
100void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700101 RETURN_IF_INVALID();
102 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700103}
104
105void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106 RETURN_IF_INVALID();
107 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700108}
109} // namespace android