blob: 4286cc98f0be098fbcfc704276966bdc515e8623 [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
Ana Krulec7ab56032018-11-02 20:51:06 +010017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Ana Krulec98b5b242018-08-10 15:03:23 -070019#include "Scheduler.h"
20
21#include <cinttypes>
22#include <cstdint>
23#include <memory>
Ana Krulec7ab56032018-11-02 20:51:06 +010024#include <numeric>
Ana Krulec98b5b242018-08-10 15:03:23 -070025
Ana Krulece588e312018-09-18 12:32:24 -070026#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 Krulec98b5b242018-08-10 15:03:23 -070031#include <gui/ISurfaceComposer.h>
Ana Krulece588e312018-09-18 12:32:24 -070032#include <ui/DisplayStatInfo.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010033#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070034
35#include "DispSync.h"
36#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070037#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070038#include "EventThread.h"
39#include "InjectVSyncSource.h"
40
41namespace android {
42
Ana Krulece588e312018-09-18 12:32:24 -070043using namespace android::hardware::configstore;
44using namespace android::hardware::configstore::V1_0;
45
Ana Krulec0c8cd522018-08-31 12:27:28 -070046#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 Krulec98b5b242018-08-10 15:03:23 -070051std::atomic<int64_t> Scheduler::sNextId = 0;
52
Ana Krulece588e312018-09-18 12:32:24 -070053Scheduler::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 Krulec0c8cd522018-08-31 12:27:28 -070070Scheduler::~Scheduler() = default;
71
Ana Krulec98b5b242018-08-10 15:03:23 -070072sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070073 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070074 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 Krulec98b5b242018-08-10 15:03:23 -070079 std::unique_ptr<EventThread> eventThread =
Ana Krulece588e312018-09-18 12:32:24 -070080 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback,
Ana Krulec0c8cd522018-08-31 12:27:28 -070081 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070082 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 Krulec0c8cd522018-08-31 12:27:28 -070089std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +000090 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070091 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
92 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +000093 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -070094 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +000095 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
96 const std::string threadName = connectionName + "Thread";
Ana Krulec0c8cd522018-08-31 12:27:28 -070097 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
Ana Krulec1f027912018-09-10 21:36:25 +000098 interceptCallback, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -070099}
100
Ana Krulec98b5b242018-08-10 15:03:23 -0700101sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
102 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700103 RETURN_VALUE_IF_INVALID(nullptr);
104 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -0700105}
106
107EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108 RETURN_VALUE_IF_INVALID(nullptr);
109 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700110}
111
112sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113 RETURN_VALUE_IF_INVALID(nullptr);
114 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700115}
116
117void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
118 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119 RETURN_IF_INVALID();
120 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700121}
122
123void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700124 RETURN_IF_INVALID();
125 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700126}
127
128void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129 RETURN_IF_INVALID();
130 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700131}
132
133void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700134 RETURN_IF_INVALID();
135 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700136}
137
138void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139 RETURN_IF_INVALID();
140 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700141}
Ana Krulece588e312018-09-18 12:32:24 -0700142
143void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
144 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
145 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
146}
147
148void 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
157void 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
169void Scheduler::setVsyncPeriod(const nsecs_t period) {
170 mPrimaryDispSync->reset();
171 mPrimaryDispSync->setPeriod(period);
172 enableHardwareVsync();
173}
174
175void 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
191void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
192 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
193 enableHardwareVsync();
194 } else {
195 disableHardwareVsync(false);
196 }
197}
198
199void Scheduler::setIgnorePresentFences(bool ignore) {
200 mPrimaryDispSync->setIgnorePresentFences(ignore);
201}
202
Ana Krulec7ab56032018-11-02 20:51:06 +0100203void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
204 std::lock_guard<std::mutex> lock(mHWVsyncLock);
205 mHWVsyncAvailable = makeAvailable;
206}
207
208void 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
243nsecs_t Scheduler::calculateAverage() const {
244 nsecs_t sum = std::accumulate(mTimeDifferences.begin(), mTimeDifferences.end(), 0);
245 return (sum / ARRAY_SIZE);
246}
247
248void 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 Krulec98b5b242018-08-10 15:03:23 -0700257} // namespace android