blob: 230c81a0b3a5170b3a0dd0c67a550b75f346835a [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 Mouri454c5522024-09-23 14:05:56 +000028 std::thread thread(&FenceMonitor::loop, this);
29 pthread_setname_np(thread.native_handle(), mName);
30 thread.detach();
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000031}
32
33void 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 Mouri454c5522024-09-23 14:05:56 +000038 snprintf(message, sizeof(message), "%s fence %u has signaled", mName, mFencesQueued);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000039 ATRACE_NAME(message);
40 // Need an increment on both to make the trace number correct.
41 mFencesQueued++;
42 mFencesSignaled++;
43 return;
44 }
Alec Mouri454c5522024-09-23 14:05:56 +000045 snprintf(message, sizeof(message), "Trace %s fence %u", mName, mFencesQueued);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000046 ATRACE_NAME(message);
47
48 mQueue.push_back(fence);
49 mCondition.notify_one();
50 mFencesQueued++;
Alec Mouri454c5522024-09-23 14:05:56 +000051 ATRACE_INT(mName, int32_t(mQueue.size()));
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000052}
53
54void FenceMonitor::loop() {
Alec Mouri454c5522024-09-23 14:05:56 +000055 while (true) {
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000056 threadLoop();
57 }
58}
59
60void FenceMonitor::threadLoop() {
61 sp<Fence> fence;
62 uint32_t fenceNum;
63 {
64 std::unique_lock<std::mutex> lock(mMutex);
Alec Mouri454c5522024-09-23 14:05:56 +000065 while (mQueue.empty()) {
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000066 mCondition.wait(lock);
67 }
68 fence = mQueue[0];
69 fenceNum = mFencesSignaled;
70 }
71 {
72 char message[64];
Alec Mouri454c5522024-09-23 14:05:56 +000073 snprintf(message, sizeof(message), "waiting for %s %u", mName, fenceNum);
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000074 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 Mouri454c5522024-09-23 14:05:56 +000085 ATRACE_INT(mName, int32_t(mQueue.size()));
Alec Mouri0e7d8fd2023-05-03 23:58:43 +000086 }
87}
88
Alec Mouri454c5522024-09-23 14:05:56 +000089} // namespace android::gui