blob: 0289d3fd2ef79f069798daf1b2ce70419b04d4d5 [file] [log] [blame]
John Reckf8441e62017-10-23 13:10:41 -07001/*
2 * Copyright (C) 2017 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#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
28namespace android::uirenderer {
29
John Reck6104cea2019-01-10 14:37:17 -080030class ThreadBase : public Thread {
John Reckf8441e62017-10-23 13:10:41 -070031 PREVENT_COPY_AND_ASSIGN(ThreadBase);
32
33public:
John Reck259b25a2017-12-01 16:18:53 -080034 ThreadBase()
35 : Thread(false)
36 , mLooper(new Looper(false))
37 , mQueue([this]() { mLooper->wake(); }, mLock) {}
John Reckf8441e62017-10-23 13:10:41 -070038
39 WorkQueue& queue() { return mQueue; }
40
41 void requestExit() {
42 Thread::requestExit();
43 mLooper->wake();
44 }
45
John Reck1bcacfd2017-11-03 10:12:19 -070046 void start(const char* name = "ThreadBase") { Thread::run(name); }
John Reckf8441e62017-10-23 13:10:41 -070047
John Reck1bcacfd2017-11-03 10:12:19 -070048 void join() { Thread::join(); }
John Reckf8441e62017-10-23 13:10:41 -070049
John Recke170fb62018-05-07 08:12:07 -070050 bool isRunning() const { return Thread::isRunning(); }
51
John Reckf8441e62017-10-23 13:10:41 -070052protected:
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);
John Reck1bcacfd2017-11-03 10:12:19 -070065 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, "RenderThread Looper POLL_ERROR!");
John Reckf8441e62017-10-23 13:10:41 -070066 }
67
John Reck1bcacfd2017-11-03 10:12:19 -070068 void processQueue() { mQueue.process(); }
John Reckf8441e62017-10-23 13:10:41 -070069
70 virtual bool threadLoop() override {
John Reck352e9f02019-02-14 16:26:08 -080071 Looper::setForThread(mLooper);
John Reckf8441e62017-10-23 13:10:41 -070072 while (!exitPending()) {
73 waitForWork();
74 processQueue();
75 }
John Reck352e9f02019-02-14 16:26:08 -080076 Looper::setForThread(nullptr);
John Reckf8441e62017-10-23 13:10:41 -070077 return false;
78 }
79
80 sp<Looper> mLooper;
81
82private:
83 WorkQueue mQueue;
84 std::mutex mLock;
85};
86
John Reck1bcacfd2017-11-03 10:12:19 -070087} // namespace android::uirenderer
John Reckf8441e62017-10-23 13:10:41 -070088
John Reck1bcacfd2017-11-03 10:12:19 -070089#endif // HWUI_THREADBASE_H