Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 23 | #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 Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 28 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 29 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 30 | |
| 31 | #include "DispSync.h" |
| 32 | #include "DispSyncSource.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 33 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 | #include "EventThread.h" |
| 35 | #include "InjectVSyncSource.h" |
| 36 | |
| 37 | namespace android { |
| 38 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 39 | using namespace android::hardware::configstore; |
| 40 | using namespace android::hardware::configstore::V1_0; |
| 41 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 42 | #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 Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 47 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 48 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 49 | Scheduler::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 Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | Scheduler::~Scheduler() = default; |
| 67 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 68 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 69 | const std::string& connectionName, int64_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 70 | 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 Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 75 | std::unique_ptr<EventThread> eventThread = |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 76 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 77 | interceptCallback); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 78 | 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 Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 85 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 86 | const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 87 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 88 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 89 | const std::string sourceName = connectionName + "Source"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 90 | std::unique_ptr<VSyncSource> eventThreadSource = |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 91 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str()); |
| 92 | const std::string threadName = connectionName + "Thread"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 93 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback, |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 94 | interceptCallback, threadName.c_str()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 97 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
| 98 | const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 99 | RETURN_VALUE_IF_INVALID(nullptr); |
| 100 | return mConnections[handle->id]->thread->createEventConnection(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 104 | RETURN_VALUE_IF_INVALID(nullptr); |
| 105 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 109 | RETURN_VALUE_IF_INVALID(nullptr); |
| 110 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
| 114 | EventThread::DisplayType displayType, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 115 | RETURN_IF_INVALID(); |
| 116 | mConnections[handle->id]->thread->onHotplugReceived(displayType, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | RETURN_IF_INVALID(); |
| 121 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 | RETURN_IF_INVALID(); |
| 126 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 130 | RETURN_IF_INVALID(); |
| 131 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 135 | RETURN_IF_INVALID(); |
| 136 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 137 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame^] | 138 | |
| 139 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 140 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 141 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 142 | } |
| 143 | |
| 144 | void 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 | |
| 153 | void 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 | |
| 165 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
| 166 | mPrimaryDispSync->reset(); |
| 167 | mPrimaryDispSync->setPeriod(period); |
| 168 | enableHardwareVsync(); |
| 169 | } |
| 170 | |
| 171 | void 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 | |
| 187 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 188 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 189 | enableHardwareVsync(); |
| 190 | } else { |
| 191 | disableHardwareVsync(false); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 196 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 197 | } |
| 198 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 199 | } // namespace android |