blob: 18a8bb1578c800927e7e7f15fe3c020ac90a7030 [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
Ana Krulece588e312018-09-18 12:32:24 -070023#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
24#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
25#include <android/hardware/configstore/1.2/ISurfaceFlingerConfigs.h>
26#include <configstore/Utils.h>
27
Ana Krulec98b5b242018-08-10 15:03:23 -070028#include <gui/ISurfaceComposer.h>
Ana Krulece588e312018-09-18 12:32:24 -070029#include <ui/DisplayStatInfo.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070030
31#include "DispSync.h"
32#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070033#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070034#include "EventThread.h"
35#include "InjectVSyncSource.h"
36
37namespace android {
38
Ana Krulece588e312018-09-18 12:32:24 -070039using namespace android::hardware::configstore;
40using namespace android::hardware::configstore::V1_0;
41
Ana Krulec0c8cd522018-08-31 12:27:28 -070042#define RETURN_VALUE_IF_INVALID(value) \
43 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
44#define RETURN_IF_INVALID() \
45 if (handle == nullptr || mConnections.count(handle->id) == 0) return
46
Ana Krulec98b5b242018-08-10 15:03:23 -070047std::atomic<int64_t> Scheduler::sNextId = 0;
48
Ana Krulece588e312018-09-18 12:32:24 -070049Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
50 : mHasSyncFramework(
51 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(true)),
52 mDispSyncPresentTimeOffset(
53 getInt64<ISurfaceFlingerConfigs,
54 &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0)),
55 mPrimaryHWVsyncEnabled(false),
56 mHWVsyncAvailable(false) {
57 // Note: We create a local temporary with the real DispSync implementation
58 // type temporarily so we can initialize it with the configured values,
59 // before storing it for more generic use using the interface type.
60 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
61 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
62 mPrimaryDispSync = std::move(primaryDispSync);
63 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
64}
65
Ana Krulec0c8cd522018-08-31 12:27:28 -070066Scheduler::~Scheduler() = default;
67
Ana Krulec98b5b242018-08-10 15:03:23 -070068sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070069 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070070 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
71 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
72 const int64_t id = sNextId++;
73 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
74
Ana Krulec98b5b242018-08-10 15:03:23 -070075 std::unique_ptr<EventThread> eventThread =
Ana Krulece588e312018-09-18 12:32:24 -070076 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback,
Ana Krulec0c8cd522018-08-31 12:27:28 -070077 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070078 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
79 eventThread->createEventConnection(),
80 std::move(eventThread));
81 mConnections.insert(std::make_pair(id, std::move(connection)));
82 return mConnections[id]->handle;
83}
84
Ana Krulec0c8cd522018-08-31 12:27:28 -070085std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +000086 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070087 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
88 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +000089 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -070090 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +000091 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
92 const std::string threadName = connectionName + "Thread";
Ana Krulec0c8cd522018-08-31 12:27:28 -070093 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
Ana Krulec1f027912018-09-10 21:36:25 +000094 interceptCallback, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -070095}
96
Ana Krulec98b5b242018-08-10 15:03:23 -070097sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
98 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -070099 RETURN_VALUE_IF_INVALID(nullptr);
100 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -0700101}
102
103EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104 RETURN_VALUE_IF_INVALID(nullptr);
105 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700106}
107
108sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700109 RETURN_VALUE_IF_INVALID(nullptr);
110 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700111}
112
113void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
114 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115 RETURN_IF_INVALID();
116 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700117}
118
119void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120 RETURN_IF_INVALID();
121 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700122}
123
124void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125 RETURN_IF_INVALID();
126 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700127}
128
129void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130 RETURN_IF_INVALID();
131 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700132}
133
134void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700135 RETURN_IF_INVALID();
136 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700137}
Ana Krulece588e312018-09-18 12:32:24 -0700138
139void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
140 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
141 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
142}
143
144void Scheduler::enableHardwareVsync() {
145 std::lock_guard<std::mutex> lock(mHWVsyncLock);
146 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
147 mPrimaryDispSync->beginResync();
148 mEventControlThread->setVsyncEnabled(true);
149 mPrimaryHWVsyncEnabled = true;
150 }
151}
152
153void Scheduler::disableHardwareVsync(bool makeUnavailable) {
154 std::lock_guard<std::mutex> lock(mHWVsyncLock);
155 if (mPrimaryHWVsyncEnabled) {
156 mEventControlThread->setVsyncEnabled(false);
157 mPrimaryDispSync->endResync();
158 mPrimaryHWVsyncEnabled = false;
159 }
160 if (makeUnavailable) {
161 mHWVsyncAvailable = false;
162 }
163}
164
165void Scheduler::setVsyncPeriod(const nsecs_t period) {
166 mPrimaryDispSync->reset();
167 mPrimaryDispSync->setPeriod(period);
168 enableHardwareVsync();
169}
170
171void Scheduler::addResyncSample(const nsecs_t timestamp) {
172 bool needsHwVsync = false;
173 { // Scope for the lock
174 std::lock_guard<std::mutex> lock(mHWVsyncLock);
175 if (mPrimaryHWVsyncEnabled) {
176 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
177 }
178 }
179
180 if (needsHwVsync) {
181 enableHardwareVsync();
182 } else {
183 disableHardwareVsync(false);
184 }
185}
186
187void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
188 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
189 enableHardwareVsync();
190 } else {
191 disableHardwareVsync(false);
192 }
193}
194
195void Scheduler::setIgnorePresentFences(bool ignore) {
196 mPrimaryDispSync->setIgnorePresentFences(ignore);
197}
198
Ana Krulec98b5b242018-08-10 15:03:23 -0700199} // namespace android