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 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 19 | #include "Scheduler.h" |
| 20 | |
| 21 | #include <cinttypes> |
| 22 | #include <cstdint> |
| 23 | #include <memory> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 24 | #include <numeric> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 25 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 26 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 27 | #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h> |
| 28 | #include <android/hardware/configstore/1.2/ISurfaceFlingerConfigs.h> |
| 29 | #include <configstore/Utils.h> |
| 30 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 31 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 32 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 33 | #include <utils/Trace.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 | |
| 35 | #include "DispSync.h" |
| 36 | #include "DispSyncSource.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 37 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 38 | #include "EventThread.h" |
| 39 | #include "InjectVSyncSource.h" |
| 40 | |
| 41 | namespace android { |
| 42 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 43 | using namespace android::hardware::configstore; |
| 44 | using namespace android::hardware::configstore::V1_0; |
| 45 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 46 | #define RETURN_VALUE_IF_INVALID(value) \ |
| 47 | if (handle == nullptr || mConnections.count(handle->id) == 0) return value |
| 48 | #define RETURN_IF_INVALID() \ |
| 49 | if (handle == nullptr || mConnections.count(handle->id) == 0) return |
| 50 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 51 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 52 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 53 | Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function) |
| 54 | : mHasSyncFramework( |
| 55 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(true)), |
| 56 | mDispSyncPresentTimeOffset( |
| 57 | getInt64<ISurfaceFlingerConfigs, |
| 58 | &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0)), |
| 59 | mPrimaryHWVsyncEnabled(false), |
| 60 | mHWVsyncAvailable(false) { |
| 61 | // Note: We create a local temporary with the real DispSync implementation |
| 62 | // type temporarily so we can initialize it with the configured values, |
| 63 | // before storing it for more generic use using the interface type. |
| 64 | auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync"); |
| 65 | primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset); |
| 66 | mPrimaryDispSync = std::move(primaryDispSync); |
| 67 | mEventControlThread = std::make_unique<impl::EventControlThread>(function); |
| 68 | } |
| 69 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 70 | Scheduler::~Scheduler() = default; |
| 71 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 72 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 73 | const std::string& connectionName, int64_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 74 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 75 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 76 | const int64_t id = sNextId++; |
| 77 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 78 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 79 | std::unique_ptr<EventThread> eventThread = |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 80 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 81 | interceptCallback); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 82 | auto connection = std::make_unique<Connection>(new ConnectionHandle(id), |
| 83 | eventThread->createEventConnection(), |
| 84 | std::move(eventThread)); |
| 85 | mConnections.insert(std::make_pair(id, std::move(connection))); |
| 86 | return mConnections[id]->handle; |
| 87 | } |
| 88 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 89 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 90 | const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 91 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 92 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 93 | const std::string sourceName = connectionName + "Source"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 94 | std::unique_ptr<VSyncSource> eventThreadSource = |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 95 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str()); |
| 96 | const std::string threadName = connectionName + "Thread"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 97 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback, |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 98 | interceptCallback, threadName.c_str()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 101 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
| 102 | const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 103 | RETURN_VALUE_IF_INVALID(nullptr); |
| 104 | return mConnections[handle->id]->thread->createEventConnection(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 108 | RETURN_VALUE_IF_INVALID(nullptr); |
| 109 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 113 | RETURN_VALUE_IF_INVALID(nullptr); |
| 114 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
| 118 | EventThread::DisplayType displayType, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 119 | RETURN_IF_INVALID(); |
| 120 | mConnections[handle->id]->thread->onHotplugReceived(displayType, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 124 | RETURN_IF_INVALID(); |
| 125 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 129 | RETURN_IF_INVALID(); |
| 130 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 | RETURN_IF_INVALID(); |
| 135 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 139 | RETURN_IF_INVALID(); |
| 140 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 141 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 142 | |
| 143 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 144 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 145 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 146 | } |
| 147 | |
| 148 | void Scheduler::enableHardwareVsync() { |
| 149 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 150 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
| 151 | mPrimaryDispSync->beginResync(); |
| 152 | mEventControlThread->setVsyncEnabled(true); |
| 153 | mPrimaryHWVsyncEnabled = true; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 158 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 159 | if (mPrimaryHWVsyncEnabled) { |
| 160 | mEventControlThread->setVsyncEnabled(false); |
| 161 | mPrimaryDispSync->endResync(); |
| 162 | mPrimaryHWVsyncEnabled = false; |
| 163 | } |
| 164 | if (makeUnavailable) { |
| 165 | mHWVsyncAvailable = false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
| 170 | mPrimaryDispSync->reset(); |
| 171 | mPrimaryDispSync->setPeriod(period); |
| 172 | enableHardwareVsync(); |
| 173 | } |
| 174 | |
| 175 | void Scheduler::addResyncSample(const nsecs_t timestamp) { |
| 176 | bool needsHwVsync = false; |
| 177 | { // Scope for the lock |
| 178 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 179 | if (mPrimaryHWVsyncEnabled) { |
| 180 | needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (needsHwVsync) { |
| 185 | enableHardwareVsync(); |
| 186 | } else { |
| 187 | disableHardwareVsync(false); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 192 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 193 | enableHardwareVsync(); |
| 194 | } else { |
| 195 | disableHardwareVsync(false); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 200 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 201 | } |
| 202 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame^] | 203 | void Scheduler::makeHWSyncAvailable(bool makeAvailable) { |
| 204 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 205 | mHWVsyncAvailable = makeAvailable; |
| 206 | } |
| 207 | |
| 208 | void Scheduler::addNewFrameTimestamp(const nsecs_t newFrameTimestamp, bool isAutoTimestamp) { |
| 209 | ATRACE_INT("AutoTimestamp", isAutoTimestamp); |
| 210 | // Video does not have timestamp automatically set, so we discard timestamps that are |
| 211 | // coming in from other sources for now. |
| 212 | if (isAutoTimestamp) { |
| 213 | return; |
| 214 | } |
| 215 | int64_t differenceMs = (newFrameTimestamp - mPreviousFrameTimestamp) / 1000000; |
| 216 | mPreviousFrameTimestamp = newFrameTimestamp; |
| 217 | |
| 218 | if (differenceMs < 10 || differenceMs > 100) { |
| 219 | // Dismiss noise. |
| 220 | return; |
| 221 | } |
| 222 | ATRACE_INT("TimestampDiff", differenceMs); |
| 223 | |
| 224 | mTimeDifferences[mCounter % ARRAY_SIZE] = differenceMs; |
| 225 | mCounter++; |
| 226 | nsecs_t average = calculateAverage(); |
| 227 | ATRACE_INT("TimestampAverage", average); |
| 228 | |
| 229 | // TODO(b/113612090): This are current numbers from trial and error while running videos |
| 230 | // from YouTube at 24, 30, and 60 fps. |
| 231 | if (average > 14 && average < 18) { |
| 232 | ATRACE_INT("FPS", 60); |
| 233 | } else if (average > 31 && average < 34) { |
| 234 | ATRACE_INT("FPS", 30); |
| 235 | updateFrameSkipping(1); |
| 236 | return; |
| 237 | } else if (average > 39 && average < 42) { |
| 238 | ATRACE_INT("FPS", 24); |
| 239 | } |
| 240 | updateFrameSkipping(0); |
| 241 | } |
| 242 | |
| 243 | nsecs_t Scheduler::calculateAverage() const { |
| 244 | nsecs_t sum = std::accumulate(mTimeDifferences.begin(), mTimeDifferences.end(), 0); |
| 245 | return (sum / ARRAY_SIZE); |
| 246 | } |
| 247 | |
| 248 | void Scheduler::updateFrameSkipping(const int64_t skipCount) { |
| 249 | ATRACE_INT("FrameSkipCount", skipCount); |
| 250 | if (mSkipCount != skipCount) { |
| 251 | // Only update DispSync if it hasn't been updated yet. |
| 252 | mPrimaryDispSync->setRefreshSkipCount(skipCount); |
| 253 | mSkipCount = skipCount; |
| 254 | } |
| 255 | } |
| 256 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 257 | } // namespace android |