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