blob: e38f1a8ce69397cb6ff362dfe79d9fbd9cb2df1f [file] [log] [blame]
Alec Mouri0e7d8fd2023-05-03 23:58:43 +00001/*
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
25namespace android::gui {
26
27FenceMonitor::FenceMonitor(const char* name) : mName(name), mFencesQueued(0), mFencesSignaled(0) {
Alec Mouri3118f962024-09-13 22:34:11 +000028 mThread = std::thread(&FenceMonitor::loop, this);
29}
30
31FenceMonitor::~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 Mouri0e7d8fd2023-05-03 23:58:43 +000040}
41
42void 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 Mouri3118f962024-09-13 22:34:11 +000047 snprintf(message, sizeof(message), "%s fence %u has signaled", mName.c_str(),
48 mFencesQueued);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000049 ATRACE_NAME(message);
50 // Need an increment on both to make the trace number correct.
51 mFencesQueued++;
52 mFencesSignaled++;
53 return;
54 }
Alec Mouri3118f962024-09-13 22:34:11 +000055 snprintf(message, sizeof(message), "Trace %s fence %u", mName.c_str(), mFencesQueued);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000056 ATRACE_NAME(message);
57
58 mQueue.push_back(fence);
59 mCondition.notify_one();
60 mFencesQueued++;
Alec Mouri3118f962024-09-13 22:34:11 +000061 ATRACE_INT(mName.c_str(), int32_t(mQueue.size()));
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000062}
63
64void FenceMonitor::loop() {
Alec Mouri3118f962024-09-13 22:34:11 +000065 pthread_setname_np(pthread_self(), mName.c_str());
66 while (!mStopped) {
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000067 threadLoop();
68 }
69}
70
71void FenceMonitor::threadLoop() {
72 sp<Fence> fence;
73 uint32_t fenceNum;
74 {
75 std::unique_lock<std::mutex> lock(mMutex);
Alec Mouri3118f962024-09-13 22:34:11 +000076 while (mQueue.empty() && !mStopped) {
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000077 mCondition.wait(lock);
78 }
Alec Mouri3118f962024-09-13 22:34:11 +000079 if (mStopped) {
80 return;
81 }
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000082 fence = mQueue[0];
83 fenceNum = mFencesSignaled;
84 }
85 {
86 char message[64];
Alec Mouri3118f962024-09-13 22:34:11 +000087 snprintf(message, sizeof(message), "waiting for %s %u", mName.c_str(), fenceNum);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000088 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 Mouri3118f962024-09-13 22:34:11 +000099 ATRACE_INT(mName.c_str(), int32_t(mQueue.size()));
Alec Mouri0e7d8fd2023-05-03 23:58:43 +0000100 }
101}
102
Alec Mouri3118f962024-09-13 22:34:11 +0000103} // namespace android::gui