Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
| 19 | #include <gui/FenceMonitor.h> |
| 20 | #include <gui/TraceUtils.h> |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | #include <thread> |
| 24 | |
| 25 | namespace android::gui { |
| 26 | |
| 27 | FenceMonitor::FenceMonitor(const char* name) : mName(name), mFencesQueued(0), mFencesSignaled(0) { |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 28 | mThread = std::thread(&FenceMonitor::loop, this); |
| 29 | } |
| 30 | |
| 31 | FenceMonitor::~FenceMonitor() { |
| 32 | { |
| 33 | std::lock_guard<std::mutex> lock(mMutex); |
| 34 | mStopped = true; |
| 35 | mCondition.notify_one(); |
| 36 | } |
| 37 | if (mThread.joinable()) { |
| 38 | mThread.join(); |
| 39 | } |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void FenceMonitor::queueFence(const sp<Fence>& fence) { |
| 43 | char message[64]; |
| 44 | |
| 45 | std::lock_guard<std::mutex> lock(mMutex); |
| 46 | if (fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) { |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 47 | snprintf(message, sizeof(message), "%s fence %u has signaled", mName.c_str(), |
| 48 | mFencesQueued); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 49 | ATRACE_NAME(message); |
| 50 | // Need an increment on both to make the trace number correct. |
| 51 | mFencesQueued++; |
| 52 | mFencesSignaled++; |
| 53 | return; |
| 54 | } |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 55 | snprintf(message, sizeof(message), "Trace %s fence %u", mName.c_str(), mFencesQueued); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 56 | ATRACE_NAME(message); |
| 57 | |
| 58 | mQueue.push_back(fence); |
| 59 | mCondition.notify_one(); |
| 60 | mFencesQueued++; |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 61 | ATRACE_INT(mName.c_str(), int32_t(mQueue.size())); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void FenceMonitor::loop() { |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 65 | pthread_setname_np(pthread_self(), mName.c_str()); |
| 66 | while (!mStopped) { |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 67 | threadLoop(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void FenceMonitor::threadLoop() { |
| 72 | sp<Fence> fence; |
| 73 | uint32_t fenceNum; |
| 74 | { |
| 75 | std::unique_lock<std::mutex> lock(mMutex); |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 76 | while (mQueue.empty() && !mStopped) { |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 77 | mCondition.wait(lock); |
| 78 | } |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 79 | if (mStopped) { |
| 80 | return; |
| 81 | } |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 82 | fence = mQueue[0]; |
| 83 | fenceNum = mFencesSignaled; |
| 84 | } |
| 85 | { |
| 86 | char message[64]; |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 87 | snprintf(message, sizeof(message), "waiting for %s %u", mName.c_str(), fenceNum); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 88 | ATRACE_NAME(message); |
| 89 | |
| 90 | status_t result = fence->waitForever(message); |
| 91 | if (result != OK) { |
| 92 | ALOGE("Error waiting for fence: %d", result); |
| 93 | } |
| 94 | } |
| 95 | { |
| 96 | std::lock_guard<std::mutex> lock(mMutex); |
| 97 | mQueue.pop_front(); |
| 98 | mFencesSignaled++; |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 99 | ATRACE_INT(mName.c_str(), int32_t(mQueue.size())); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Alec Mouri | 3118f96 | 2024-09-13 22:34:11 +0000 | [diff] [blame^] | 103 | } // namespace android::gui |