RenderThread.cpp revision a5dda645da738da7b4ae15e28fa7d93d3b04b94f
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
21#include <gui/DisplayEventReceiver.h>
22#include <utils/Log.h>
23
24#include "CanvasContext.h"
25#include "RenderProxy.h"
26
27namespace android {
28using namespace uirenderer::renderthread;
29ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread);
30
31namespace uirenderer {
32namespace renderthread {
33
34// Number of events to read at a time from the DisplayEventReceiver pipe.
35// The value should be large enough that we can quickly drain the pipe
36// using just a few large reads.
37static const size_t EVENT_BUFFER_SIZE = 100;
38
39// Slight delay to give the UI time to push us a new frame before we replay
40static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4;
41
42TaskQueue::TaskQueue() : mHead(0), mTail(0) {}
43
44RenderTask* TaskQueue::next() {
45    RenderTask* ret = mHead;
46    if (ret) {
47        mHead = ret->mNext;
48        if (!mHead) {
49            mTail = 0;
50        }
51        ret->mNext = 0;
52    }
53    return ret;
54}
55
56RenderTask* TaskQueue::peek() {
57    return mHead;
58}
59
60void TaskQueue::queue(RenderTask* task) {
61    // Since the RenderTask itself forms the linked list it is not allowed
62    // to have the same task queued twice
63    LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
64    if (mTail) {
65        // Fast path if we can just append
66        if (mTail->mRunAt <= task->mRunAt) {
67            mTail->mNext = task;
68            mTail = task;
69        } else {
70            // Need to find the proper insertion point
71            RenderTask* previous = 0;
72            RenderTask* next = mHead;
73            while (next && next->mRunAt <= task->mRunAt) {
74                previous = next;
75                next = next->mNext;
76            }
77            if (!previous) {
78                task->mNext = mHead;
79                mHead = task;
80            } else {
81                previous->mNext = task;
82                if (next) {
83                    task->mNext = next;
84                } else {
85                    mTail = task;
86                }
87            }
88        }
89    } else {
90        mTail = mHead = task;
91    }
92}
93
94void TaskQueue::queueAtFront(RenderTask* task) {
95    if (mTail) {
96        task->mNext = mHead;
97        mHead = task;
98    } else {
99        mTail = mHead = task;
100    }
101}
102
103void TaskQueue::remove(RenderTask* task) {
104    // TaskQueue is strict here to enforce that users are keeping track of
105    // their RenderTasks due to how their memory is managed
106    LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
107            "Cannot remove a task that isn't in the queue!");
108
109    // If task is the head we can just call next() to pop it off
110    // Otherwise we need to scan through to find the task before it
111    if (peek() == task) {
112        next();
113    } else {
114        RenderTask* previous = mHead;
115        while (previous->mNext != task) {
116            previous = previous->mNext;
117        }
118        previous->mNext = task->mNext;
119        if (mTail == task) {
120            mTail = previous;
121        }
122    }
123}
124
125class DispatchFrameCallbacks : public RenderTask {
126private:
127    RenderThread* mRenderThread;
128public:
129    DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
130
131    virtual void run() {
132        mRenderThread->dispatchFrameCallbacks();
133    }
134};
135
136RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
137        , mNextWakeup(LLONG_MAX)
138        , mDisplayEventReceiver(0)
139        , mVsyncRequested(false)
140        , mFrameCallbackTaskPending(false)
141        , mFrameCallbackTask(0) {
142    mFrameCallbackTask = new DispatchFrameCallbacks(this);
143    mLooper = new Looper(false);
144    run("RenderThread");
145}
146
147RenderThread::~RenderThread() {
148}
149
150void RenderThread::initializeDisplayEventReceiver() {
151    LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
152    mDisplayEventReceiver = new DisplayEventReceiver();
153    status_t status = mDisplayEventReceiver->initCheck();
154    LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
155            "failed with status: %d", status);
156
157    // Register the FD
158    mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
159            Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
160}
161
162int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
163    if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
164        ALOGE("Display event receiver pipe was closed or an error occurred.  "
165                "events=0x%x", events);
166        return 0; // remove the callback
167    }
168
169    if (!(events & Looper::EVENT_INPUT)) {
170        ALOGW("Received spurious callback for unhandled poll event.  "
171                "events=0x%x", events);
172        return 1; // keep the callback
173    }
174
175    reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
176
177    return 1; // keep the callback
178}
179
180static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
181    DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
182    nsecs_t latest = 0;
183    ssize_t n;
184    while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
185        for (ssize_t i = 0; i < n; i++) {
186            const DisplayEventReceiver::Event& ev = buf[i];
187            switch (ev.header.type) {
188            case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
189                latest = ev.header.timestamp;
190                break;
191            }
192        }
193    }
194    if (n < 0) {
195        ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
196    }
197    return latest;
198}
199
200void RenderThread::drainDisplayEventQueue(bool skipCallbacks) {
201    ATRACE_CALL();
202    nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
203    if (vsyncEvent > 0) {
204        mVsyncRequested = false;
205        mTimeLord.vsyncReceived(vsyncEvent);
206        if (!skipCallbacks && !mFrameCallbackTaskPending) {
207            ATRACE_NAME("queue mFrameCallbackTask");
208            mFrameCallbackTaskPending = true;
209            queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY);
210        }
211    }
212}
213
214void RenderThread::dispatchFrameCallbacks() {
215    ATRACE_CALL();
216    mFrameCallbackTaskPending = false;
217
218    std::set<IFrameCallback*> callbacks;
219    mFrameCallbacks.swap(callbacks);
220
221    for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
222        (*it)->doFrame();
223    }
224}
225
226void RenderThread::requestVsync() {
227    if (!mVsyncRequested) {
228        mVsyncRequested = true;
229        status_t status = mDisplayEventReceiver->requestNextVsync();
230        LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
231                "requestNextVsync failed with status: %d", status);
232    }
233}
234
235bool RenderThread::threadLoop() {
236    initializeDisplayEventReceiver();
237
238    int timeoutMillis = -1;
239    for (;;) {
240        int result = mLooper->pollOnce(timeoutMillis);
241        LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
242                "RenderThread Looper POLL_ERROR!");
243
244        nsecs_t nextWakeup;
245        // Process our queue, if we have anything
246        while (RenderTask* task = nextTask(&nextWakeup)) {
247            task->run();
248            // task may have deleted itself, do not reference it again
249        }
250        if (nextWakeup == LLONG_MAX) {
251            timeoutMillis = -1;
252        } else {
253            nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
254            timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
255            if (timeoutMillis < 0) {
256                timeoutMillis = 0;
257            }
258        }
259
260        if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
261            drainDisplayEventQueue(true);
262            mFrameCallbacks.insert(
263                    mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
264            mPendingRegistrationFrameCallbacks.clear();
265            requestVsync();
266        }
267    }
268
269    return false;
270}
271
272void RenderThread::queue(RenderTask* task) {
273    AutoMutex _lock(mLock);
274    mQueue.queue(task);
275    if (mNextWakeup && task->mRunAt < mNextWakeup) {
276        mNextWakeup = 0;
277        mLooper->wake();
278    }
279}
280
281void RenderThread::queueAtFront(RenderTask* task) {
282    AutoMutex _lock(mLock);
283    mQueue.queueAtFront(task);
284    mLooper->wake();
285}
286
287void RenderThread::queueDelayed(RenderTask* task, int delayMs) {
288    nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
289    task->mRunAt = now + milliseconds_to_nanoseconds(delayMs);
290    queue(task);
291}
292
293void RenderThread::remove(RenderTask* task) {
294    AutoMutex _lock(mLock);
295    mQueue.remove(task);
296}
297
298void RenderThread::postFrameCallback(IFrameCallback* callback) {
299    mPendingRegistrationFrameCallbacks.insert(callback);
300}
301
302void RenderThread::removeFrameCallback(IFrameCallback* callback) {
303    mFrameCallbacks.erase(callback);
304    mPendingRegistrationFrameCallbacks.erase(callback);
305}
306
307void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
308    if (mFrameCallbacks.erase(callback)) {
309        mPendingRegistrationFrameCallbacks.insert(callback);
310    }
311}
312
313RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
314    AutoMutex _lock(mLock);
315    RenderTask* next = mQueue.peek();
316    if (!next) {
317        mNextWakeup = LLONG_MAX;
318    } else {
319        mNextWakeup = next->mRunAt;
320        // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
321        if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
322            next = mQueue.next();
323        } else {
324            next = 0;
325        }
326    }
327    if (nextWakeup) {
328        *nextWakeup = mNextWakeup;
329    }
330    return next;
331}
332
333} /* namespace renderthread */
334} /* namespace uirenderer */
335} /* namespace android */
336