• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
FenceMonitor(const char * name)27 FenceMonitor::FenceMonitor(const char* name) : mName(name), mFencesQueued(0), mFencesSignaled(0) {
28     std::thread thread(&FenceMonitor::loop, this);
29     pthread_setname_np(thread.native_handle(), mName);
30     thread.detach();
31 }
32 
queueFence(const sp<Fence> & fence)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) {
38         snprintf(message, sizeof(message), "%s fence %u has signaled", mName, mFencesQueued);
39         ATRACE_NAME(message);
40         // Need an increment on both to make the trace number correct.
41         mFencesQueued++;
42         mFencesSignaled++;
43         return;
44     }
45     snprintf(message, sizeof(message), "Trace %s fence %u", mName, mFencesQueued);
46     ATRACE_NAME(message);
47 
48     mQueue.push_back(fence);
49     mCondition.notify_one();
50     mFencesQueued++;
51     ATRACE_INT(mName, int32_t(mQueue.size()));
52 }
53 
loop()54 void FenceMonitor::loop() {
55     while (true) {
56         threadLoop();
57     }
58 }
59 
threadLoop()60 void FenceMonitor::threadLoop() {
61     sp<Fence> fence;
62     uint32_t fenceNum;
63     {
64         std::unique_lock<std::mutex> lock(mMutex);
65         while (mQueue.empty()) {
66             mCondition.wait(lock);
67         }
68         fence = mQueue[0];
69         fenceNum = mFencesSignaled;
70     }
71     {
72         char message[64];
73         snprintf(message, sizeof(message), "waiting for %s %u", mName, fenceNum);
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++;
85         ATRACE_INT(mName, int32_t(mQueue.size()));
86     }
87 }
88 
89 } // namespace android::gui