blob: 54e19c78a15b9d0526c4a5d6deb5986121fa963c [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(
Ana Krulec1f027912018-09-10 21:36:25 +000042 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070043 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(
Ana Krulec1f027912018-09-10 21:36:25 +000059 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070060 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
61 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +000062 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -070063 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +000064 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
65 const std::string threadName = connectionName + "Thread";
Ana Krulec0c8cd522018-08-31 12:27:28 -070066 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
Ana Krulec1f027912018-09-10 21:36:25 +000067 interceptCallback, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -070068}
69
Ana Krulec98b5b242018-08-10 15:03:23 -070070sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
71 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070072 RETURN_VALUE_IF_INVALID(nullptr);
73 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -070074}
75
76EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070077 RETURN_VALUE_IF_INVALID(nullptr);
78 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -070079}
80
81sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070082 RETURN_VALUE_IF_INVALID(nullptr);
83 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070084}
85
86void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
87 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070088 RETURN_IF_INVALID();
89 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -070090}
91
92void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070093 RETURN_IF_INVALID();
94 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -070095}
96
97void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070098 RETURN_IF_INVALID();
99 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700100}
101
102void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700103 RETURN_IF_INVALID();
104 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700105}
106
107void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108 RETURN_IF_INVALID();
109 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700110}
111} // namespace android