John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_TAG "RenderThread" |
| 18 | |
| 19 | #include "RenderThread.h" |
| 20 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 21 | #include "CanvasContext.h" |
| 22 | #include "RenderProxy.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
| 25 | namespace android { |
| 26 | using namespace uirenderer::renderthread; |
| 27 | ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread); |
| 28 | |
| 29 | namespace uirenderer { |
| 30 | namespace renderthread { |
| 31 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 32 | TaskQueue::TaskQueue() : mHead(0), mTail(0) {} |
| 33 | |
| 34 | RenderTask* TaskQueue::next() { |
| 35 | RenderTask* ret = mHead; |
| 36 | if (ret) { |
| 37 | mHead = ret->mNext; |
| 38 | if (!mHead) { |
| 39 | mTail = 0; |
| 40 | } |
| 41 | ret->mNext = 0; |
| 42 | } |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | RenderTask* TaskQueue::peek() { |
| 47 | return mHead; |
| 48 | } |
| 49 | |
| 50 | void TaskQueue::queue(RenderTask* task) { |
| 51 | // Since the RenderTask itself forms the linked list it is not allowed |
| 52 | // to have the same task queued twice |
| 53 | LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!"); |
| 54 | if (mTail) { |
| 55 | // Fast path if we can just append |
| 56 | if (mTail->mRunAt <= task->mRunAt) { |
| 57 | mTail->mNext = task; |
| 58 | mTail = task; |
| 59 | } else { |
| 60 | // Need to find the proper insertion point |
| 61 | RenderTask* previous = 0; |
| 62 | RenderTask* next = mHead; |
| 63 | while (next && next->mRunAt <= task->mRunAt) { |
| 64 | previous = next; |
| 65 | next = next->mNext; |
| 66 | } |
| 67 | if (!previous) { |
| 68 | task->mNext = mHead; |
| 69 | mHead = task; |
| 70 | } else { |
| 71 | previous->mNext = task; |
| 72 | if (next) { |
| 73 | task->mNext = next; |
| 74 | } else { |
| 75 | mTail = task; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | mTail = mHead = task; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void TaskQueue::remove(RenderTask* task) { |
| 85 | // TaskQueue is strict here to enforce that users are keeping track of |
| 86 | // their RenderTasks due to how their memory is managed |
| 87 | LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task, |
| 88 | "Cannot remove a task that isn't in the queue!"); |
| 89 | |
| 90 | // If task is the head we can just call next() to pop it off |
| 91 | // Otherwise we need to scan through to find the task before it |
| 92 | if (peek() == task) { |
| 93 | next(); |
| 94 | } else { |
| 95 | RenderTask* previous = mHead; |
| 96 | while (previous->mNext != task) { |
| 97 | previous = previous->mNext; |
| 98 | } |
| 99 | previous->mNext = task->mNext; |
| 100 | if (mTail == task) { |
| 101 | mTail = previous; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 106 | RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>() |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 107 | , mNextWakeup(LLONG_MAX) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 108 | mLooper = new Looper(false); |
| 109 | run("RenderThread"); |
| 110 | } |
| 111 | |
| 112 | RenderThread::~RenderThread() { |
| 113 | } |
| 114 | |
| 115 | bool RenderThread::threadLoop() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 116 | int timeoutMillis = -1; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 117 | for (;;) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 118 | int result = mLooper->pollAll(timeoutMillis); |
| 119 | LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, |
| 120 | "RenderThread Looper POLL_ERROR!"); |
| 121 | |
| 122 | nsecs_t nextWakeup; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 123 | // Process our queue, if we have anything |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 124 | while (RenderTask* task = nextTask(&nextWakeup)) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 125 | task->run(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 126 | // task may have deleted itself, do not reference it again |
| 127 | } |
| 128 | if (nextWakeup == LLONG_MAX) { |
| 129 | timeoutMillis = -1; |
| 130 | } else { |
John Reck | a6260b8 | 2014-01-29 18:31:51 -0800 | [diff] [blame^] | 131 | nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC); |
| 132 | timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 133 | if (timeoutMillis < 0) { |
| 134 | timeoutMillis = 0; |
| 135 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void RenderThread::queue(RenderTask* task) { |
| 143 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 144 | mQueue.queue(task); |
| 145 | if (mNextWakeup && task->mRunAt < mNextWakeup) { |
| 146 | mNextWakeup = 0; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 147 | mLooper->wake(); |
| 148 | } |
| 149 | } |
| 150 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 151 | void RenderThread::queueDelayed(RenderTask* task, int delayMs) { |
| 152 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | a6260b8 | 2014-01-29 18:31:51 -0800 | [diff] [blame^] | 153 | task->mRunAt = now + milliseconds_to_nanoseconds(delayMs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 154 | queue(task); |
| 155 | } |
| 156 | |
| 157 | void RenderThread::remove(RenderTask* task) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 158 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 159 | mQueue.remove(task); |
| 160 | } |
| 161 | |
| 162 | RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) { |
| 163 | AutoMutex _lock(mLock); |
| 164 | RenderTask* next = mQueue.peek(); |
| 165 | if (!next) { |
| 166 | mNextWakeup = LLONG_MAX; |
| 167 | } else { |
| 168 | // Most tasks won't be delayed, so avoid unnecessary systemTime() calls |
| 169 | if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) { |
| 170 | next = mQueue.next(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 171 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 172 | mNextWakeup = next->mRunAt; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 173 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 174 | if (nextWakeup) { |
| 175 | *nextWakeup = mNextWakeup; |
| 176 | } |
| 177 | return next; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } /* namespace renderthread */ |
| 181 | } /* namespace uirenderer */ |
| 182 | } /* namespace android */ |