| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef HWUI_THREADBASE_H |
| 18 |
#define HWUI_THREADBASE_H |
| 19 |
|
| 20 |
#include "WorkQueue.h" |
| 21 |
#include "utils/Macros.h" |
| 22 |
|
| 23 |
#include <utils/Looper.h> |
| 24 |
#include <utils/Thread.h> |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
namespace android::uirenderer { |
| 29 |
|
| 30 |
class ThreadBase : public Thread { |
| 31 |
PREVENT_COPY_AND_ASSIGN(ThreadBase); |
| 32 |
|
| 33 |
public: |
| 34 |
ThreadBase() |
| 35 |
: Thread(false) |
| 36 |
, mLooper(new Looper(false)) |
| 37 |
, mQueue([this]() { mLooper->wake(); }, mLock) {} |
| 38 |
|
| 39 |
WorkQueue& queue() { return mQueue; } |
| 40 |
|
| 41 |
void requestExit() { |
| 42 |
Thread::requestExit(); |
| 43 |
mLooper->wake(); |
| 44 |
} |
| 45 |
|
| 46 |
void start(const char* name = "ThreadBase") { Thread::run(name); } |
| 47 |
|
| 48 |
void join() { Thread::join(); } |
| 49 |
|
| 50 |
bool isRunning() const { return Thread::isRunning(); } |
| 51 |
|
| 52 |
protected: |
| 53 |
void waitForWork() { |
| 54 |
nsecs_t nextWakeup; |
| 55 |
{ |
| 56 |
std::unique_lock lock{mLock}; |
| 57 |
nextWakeup = mQueue.nextWakeup(lock); |
| 58 |
} |
| 59 |
int timeout = -1; |
| 60 |
if (nextWakeup < std::numeric_limits<nsecs_t>::max()) { |
| 61 |
timeout = ns2ms(nextWakeup - WorkQueue::clock::now()); |
| 62 |
if (timeout < 0) timeout = 0; |
| 63 |
} |
| 64 |
int result = mLooper->pollOnce(timeout); |
| 65 |
LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, "RenderThread Looper POLL_ERROR!"); |
| 66 |
} |
| 67 |
|
| 68 |
void processQueue() { mQueue.process(); } |
| 69 |
|
| 70 |
virtual bool threadLoop() override { |
| 71 |
Looper::setForThread(mLooper); |
| 72 |
while (!exitPending()) { |
| 73 |
waitForWork(); |
| 74 |
processQueue(); |
| 75 |
} |
| 76 |
Looper::setForThread(nullptr); |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
sp<Looper> mLooper; |
| 81 |
|
| 82 |
private: |
| 83 |
WorkQueue mQueue; |
| 84 |
std::mutex mLock; |
| 85 |
}; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
#endif |