Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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 "VSyncReactor.h" |
| 18 | #include "VSyncDispatch.h" |
| 19 | #include "VSyncTracker.h" |
| 20 | |
| 21 | namespace android::scheduler { |
| 22 | |
| 23 | VSyncReactor::VSyncReactor(std::unique_ptr<VSyncDispatch> dispatch, |
| 24 | std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit) |
| 25 | : mDispatch(std::move(dispatch)), |
| 26 | mTracker(std::move(tracker)), |
| 27 | mPendingLimit(pendingFenceLimit) {} |
| 28 | |
| 29 | bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) { |
| 30 | if (!fence) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | nsecs_t const signalTime = fence->getCachedSignalTime(); |
| 35 | if (signalTime == Fence::SIGNAL_TIME_INVALID) { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | std::lock_guard<std::mutex> lk(mMutex); |
| 40 | if (mIgnorePresentFences) { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) { |
| 45 | auto const time = (*it)->getCachedSignalTime(); |
| 46 | if (time == Fence::SIGNAL_TIME_PENDING) { |
| 47 | it++; |
| 48 | } else if (time == Fence::SIGNAL_TIME_INVALID) { |
| 49 | it = mUnfiredFences.erase(it); |
| 50 | } else { |
| 51 | mTracker->addVsyncTimestamp(time); |
| 52 | it = mUnfiredFences.erase(it); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (signalTime == Fence::SIGNAL_TIME_PENDING) { |
| 57 | if (mPendingLimit == mUnfiredFences.size()) { |
| 58 | mUnfiredFences.erase(mUnfiredFences.begin()); |
| 59 | } |
| 60 | mUnfiredFences.push_back(fence); |
| 61 | } else { |
| 62 | mTracker->addVsyncTimestamp(signalTime); |
| 63 | } |
| 64 | |
| 65 | return false; // TODO(b/144707443): add policy for turning on HWVsync. |
| 66 | } |
| 67 | |
| 68 | void VSyncReactor::setIgnorePresentFences(bool ignoration) { |
| 69 | std::lock_guard<std::mutex> lk(mMutex); |
| 70 | mIgnorePresentFences = ignoration; |
| 71 | if (mIgnorePresentFences == true) { |
| 72 | mUnfiredFences.clear(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | } // namespace android::scheduler |