1 /*
2  * Copyright 2019 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 LOG_NDEBUG 0
18 #undef LOG_TAG
19 #define LOG_TAG "RenderEngine"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 
22 #include <pthread.h>
23 
24 #include <processgroup/sched_policy.h>
25 #include <utils/Trace.h>
26 #include "GLESRenderEngine.h"
27 #include "ImageManager.h"
28 
29 namespace android {
30 namespace renderengine {
31 namespace gl {
32 
ImageManager(GLESRenderEngine * engine)33 ImageManager::ImageManager(GLESRenderEngine* engine) : mEngine(engine) {}
34 
initThread()35 void ImageManager::initThread() {
36     mThread = std::thread([this]() { threadMain(); });
37     pthread_setname_np(mThread.native_handle(), "ImageManager");
38     // Use SCHED_FIFO to minimize jitter
39     struct sched_param param = {0};
40     param.sched_priority = 2;
41     if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
42         ALOGE("Couldn't set SCHED_FIFO for ImageManager");
43     }
44 }
45 
~ImageManager()46 ImageManager::~ImageManager() {
47     {
48         std::lock_guard<std::mutex> lock(mMutex);
49         mRunning = false;
50     }
51     mCondition.notify_all();
52     if (mThread.joinable()) {
53         mThread.join();
54     }
55 }
56 
cacheAsync(const sp<GraphicBuffer> & buffer,const std::shared_ptr<Barrier> & barrier)57 void ImageManager::cacheAsync(const sp<GraphicBuffer>& buffer,
58                               const std::shared_ptr<Barrier>& barrier) {
59     if (buffer == nullptr) {
60         {
61             std::lock_guard<std::mutex> lock(barrier->mutex);
62             barrier->isOpen = true;
63             barrier->result = BAD_VALUE;
64         }
65         barrier->condition.notify_one();
66         return;
67     }
68     ATRACE_CALL();
69     QueueEntry entry = {QueueEntry::Operation::Insert, buffer, buffer->getId(), barrier};
70     queueOperation(std::move(entry));
71 }
72 
cache(const sp<GraphicBuffer> & buffer)73 status_t ImageManager::cache(const sp<GraphicBuffer>& buffer) {
74     ATRACE_CALL();
75     auto barrier = std::make_shared<Barrier>();
76     cacheAsync(buffer, barrier);
77     std::lock_guard<std::mutex> lock(barrier->mutex);
78     barrier->condition.wait(barrier->mutex,
79                             [&]() REQUIRES(barrier->mutex) { return barrier->isOpen; });
80     return barrier->result;
81 }
82 
releaseAsync(uint64_t bufferId,const std::shared_ptr<Barrier> & barrier)83 void ImageManager::releaseAsync(uint64_t bufferId, const std::shared_ptr<Barrier>& barrier) {
84     ATRACE_CALL();
85     QueueEntry entry = {QueueEntry::Operation::Delete, nullptr, bufferId, barrier};
86     queueOperation(std::move(entry));
87 }
88 
queueOperation(const QueueEntry && entry)89 void ImageManager::queueOperation(const QueueEntry&& entry) {
90     {
91         std::lock_guard<std::mutex> lock(mMutex);
92         mQueue.emplace(entry);
93         ATRACE_INT("ImageManagerQueueDepth", mQueue.size());
94     }
95     mCondition.notify_one();
96 }
97 
threadMain()98 void ImageManager::threadMain() {
99     set_sched_policy(0, SP_FOREGROUND);
100     bool run;
101     {
102         std::lock_guard<std::mutex> lock(mMutex);
103         run = mRunning;
104     }
105     while (run) {
106         QueueEntry entry;
107         {
108             std::lock_guard<std::mutex> lock(mMutex);
109             mCondition.wait(mMutex,
110                             [&]() REQUIRES(mMutex) { return !mQueue.empty() || !mRunning; });
111             run = mRunning;
112 
113             if (!mRunning) {
114                 // if mRunning is false, then ImageManager is being destroyed, so
115                 // bail out now.
116                 break;
117             }
118 
119             entry = mQueue.front();
120             mQueue.pop();
121             ATRACE_INT("ImageManagerQueueDepth", mQueue.size());
122         }
123 
124         status_t result = NO_ERROR;
125         switch (entry.op) {
126             case QueueEntry::Operation::Delete:
127                 mEngine->unbindExternalTextureBufferInternal(entry.bufferId);
128                 break;
129             case QueueEntry::Operation::Insert:
130                 result = mEngine->cacheExternalTextureBufferInternal(entry.buffer);
131                 break;
132         }
133         if (entry.barrier != nullptr) {
134             {
135                 std::lock_guard<std::mutex> entryLock(entry.barrier->mutex);
136                 entry.barrier->result = result;
137                 entry.barrier->isOpen = true;
138             }
139             entry.barrier->condition.notify_one();
140         }
141     }
142 
143     ALOGD("Reached end of threadMain, terminating ImageManager thread!");
144 }
145 
146 } // namespace gl
147 } // namespace renderengine
148 } // namespace android
149