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 | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 28 | std::thread thread(&FenceMonitor::loop, this); |
| 29 | pthread_setname_np(thread.native_handle(), mName); |
| 30 | thread.detach(); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void FenceMonitor::queueFence(const sp<Fence>& fence) { |
| 34 | char message[64]; |
| 35 | |
| 36 | std::lock_guard<std::mutex> lock(mMutex); |
| 37 | if (fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) { |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 38 | snprintf(message, sizeof(message), "%s fence %u has signaled", mName, mFencesQueued); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 39 | ATRACE_NAME(message); |
| 40 | // Need an increment on both to make the trace number correct. |
| 41 | mFencesQueued++; |
| 42 | mFencesSignaled++; |
| 43 | return; |
| 44 | } |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 45 | snprintf(message, sizeof(message), "Trace %s fence %u", mName, mFencesQueued); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 46 | ATRACE_NAME(message); |
| 47 | |
| 48 | mQueue.push_back(fence); |
| 49 | mCondition.notify_one(); |
| 50 | mFencesQueued++; |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 51 | ATRACE_INT(mName, int32_t(mQueue.size())); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void FenceMonitor::loop() { |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 55 | while (true) { |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 56 | threadLoop(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void FenceMonitor::threadLoop() { |
| 61 | sp<Fence> fence; |
| 62 | uint32_t fenceNum; |
| 63 | { |
| 64 | std::unique_lock<std::mutex> lock(mMutex); |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 65 | while (mQueue.empty()) { |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 66 | mCondition.wait(lock); |
| 67 | } |
| 68 | fence = mQueue[0]; |
| 69 | fenceNum = mFencesSignaled; |
| 70 | } |
| 71 | { |
| 72 | char message[64]; |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 73 | snprintf(message, sizeof(message), "waiting for %s %u", mName, fenceNum); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 74 | ATRACE_NAME(message); |
| 75 | |
| 76 | status_t result = fence->waitForever(message); |
| 77 | if (result != OK) { |
| 78 | ALOGE("Error waiting for fence: %d", result); |
| 79 | } |
| 80 | } |
| 81 | { |
| 82 | std::lock_guard<std::mutex> lock(mMutex); |
| 83 | mQueue.pop_front(); |
| 84 | mFencesSignaled++; |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 85 | ATRACE_INT(mName, int32_t(mQueue.size())); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Alec Mouri | 454c552 | 2024-09-23 14:05:56 +0000 | [diff] [blame] | 89 | } // namespace android::gui |