RenderThread.cpp revision 6b50780363d3bb8db600c770183fa07677509ae8
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#include "RenderThread.h"
18
19#include "../renderstate/RenderState.h"
20#include "CanvasContext.h"
21#include "EglManager.h"
22#include "RenderProxy.h"
23
24#include <gui/DisplayEventReceiver.h>
25#include <gui/ISurfaceComposer.h>
26#include <gui/SurfaceComposerClient.h>
27#include <sys/resource.h>
28#include <utils/Log.h>
29
30namespace android {
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 nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
41
42TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
43
44RenderTask* TaskQueue::next() {
45    RenderTask* ret = mHead;
46    if (ret) {
47        mHead = ret->mNext;
48        if (!mHead) {
49            mTail = nullptr;
50        }
51        ret->mNext = nullptr;
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 = nullptr;
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() override {
132        mRenderThread->dispatchFrameCallbacks();
133    }
134};
135
136static bool gHasRenderThreadInstance = false;
137
138bool RenderThread::hasInstance() {
139    return gHasRenderThreadInstance;
140}
141
142RenderThread& RenderThread::getInstance() {
143    // This is a pointer because otherwise __cxa_finalize
144    // will try to delete it like a Good Citizen but that causes us to crash
145    // because we don't want to delete the RenderThread normally.
146    static RenderThread* sInstance = new RenderThread();
147    gHasRenderThreadInstance = true;
148    return *sInstance;
149}
150
151RenderThread::RenderThread() : Thread(true)
152        , mNextWakeup(LLONG_MAX)
153        , mDisplayEventReceiver(nullptr)
154        , mVsyncRequested(false)
155        , mFrameCallbackTaskPending(false)
156        , mFrameCallbackTask(nullptr)
157        , mRenderState(nullptr)
158        , mEglManager(nullptr) {
159    Properties::load();
160    mFrameCallbackTask = new DispatchFrameCallbacks(this);
161    mLooper = new Looper(false);
162    run("RenderThread");
163}
164
165RenderThread::~RenderThread() {
166    LOG_ALWAYS_FATAL("Can't destroy the render thread");
167}
168
169void RenderThread::initializeDisplayEventReceiver() {
170    LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
171    mDisplayEventReceiver = new DisplayEventReceiver();
172    status_t status = mDisplayEventReceiver->initCheck();
173    LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
174            "failed with status: %d", status);
175
176    // Register the FD
177    mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
178            Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
179}
180
181void RenderThread::initThreadLocals() {
182    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
183            ISurfaceComposer::eDisplayIdMain));
184    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
185    LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
186    nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
187    mTimeLord.setFrameInterval(frameIntervalNanos);
188    initializeDisplayEventReceiver();
189    mEglManager = new EglManager(*this);
190    mRenderState = new RenderState(*this);
191    mJankTracker = new JankTracker(frameIntervalNanos);
192}
193
194int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
195    if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
196        ALOGE("Display event receiver pipe was closed or an error occurred.  "
197                "events=0x%x", events);
198        return 0; // remove the callback
199    }
200
201    if (!(events & Looper::EVENT_INPUT)) {
202        ALOGW("Received spurious callback for unhandled poll event.  "
203                "events=0x%x", events);
204        return 1; // keep the callback
205    }
206
207    reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
208
209    return 1; // keep the callback
210}
211
212static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
213    DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
214    nsecs_t latest = 0;
215    ssize_t n;
216    while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
217        for (ssize_t i = 0; i < n; i++) {
218            const DisplayEventReceiver::Event& ev = buf[i];
219            switch (ev.header.type) {
220            case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
221                latest = ev.header.timestamp;
222                break;
223            }
224        }
225    }
226    if (n < 0) {
227        ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
228    }
229    return latest;
230}
231
232void RenderThread::drainDisplayEventQueue() {
233    ATRACE_CALL();
234    nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
235    if (vsyncEvent > 0) {
236        mVsyncRequested = false;
237        if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
238            ATRACE_NAME("queue mFrameCallbackTask");
239            mFrameCallbackTaskPending = true;
240            nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
241            queueAt(mFrameCallbackTask, runAt);
242        }
243    }
244}
245
246void RenderThread::dispatchFrameCallbacks() {
247    ATRACE_CALL();
248    mFrameCallbackTaskPending = false;
249
250    std::set<IFrameCallback*> callbacks;
251    mFrameCallbacks.swap(callbacks);
252
253    if (callbacks.size()) {
254        // Assume one of them will probably animate again so preemptively
255        // request the next vsync in case it occurs mid-frame
256        requestVsync();
257        for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
258            (*it)->doFrame();
259        }
260    }
261}
262
263void RenderThread::requestVsync() {
264    if (!mVsyncRequested) {
265        mVsyncRequested = true;
266        status_t status = mDisplayEventReceiver->requestNextVsync();
267        LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
268                "requestNextVsync failed with status: %d", status);
269    }
270}
271
272bool RenderThread::threadLoop() {
273    setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
274    initThreadLocals();
275
276    int timeoutMillis = -1;
277    for (;;) {
278        int result = mLooper->pollOnce(timeoutMillis);
279        LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
280                "RenderThread Looper POLL_ERROR!");
281
282        nsecs_t nextWakeup;
283        // Process our queue, if we have anything
284        while (RenderTask* task = nextTask(&nextWakeup)) {
285            task->run();
286            // task may have deleted itself, do not reference it again
287        }
288        if (nextWakeup == LLONG_MAX) {
289            timeoutMillis = -1;
290        } else {
291            nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
292            timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
293            if (timeoutMillis < 0) {
294                timeoutMillis = 0;
295            }
296        }
297
298        if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
299            drainDisplayEventQueue();
300            mFrameCallbacks.insert(
301                    mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
302            mPendingRegistrationFrameCallbacks.clear();
303            requestVsync();
304        }
305
306        if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
307            // TODO: Clean this up. This is working around an issue where a combination
308            // of bad timing and slow drawing can result in dropping a stale vsync
309            // on the floor (correct!) but fails to schedule to listen for the
310            // next vsync (oops), so none of the callbacks are run.
311            requestVsync();
312        }
313    }
314
315    return false;
316}
317
318void RenderThread::queue(RenderTask* task) {
319    AutoMutex _lock(mLock);
320    mQueue.queue(task);
321    if (mNextWakeup && task->mRunAt < mNextWakeup) {
322        mNextWakeup = 0;
323        mLooper->wake();
324    }
325}
326
327void RenderThread::queueAndWait(RenderTask* task) {
328    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
329    AutoMutex _lock(mSyncMutex);
330    queue(&syncTask);
331    mSyncCondition.wait(mSyncMutex);
332}
333
334void RenderThread::queueAtFront(RenderTask* task) {
335    AutoMutex _lock(mLock);
336    mQueue.queueAtFront(task);
337    mLooper->wake();
338}
339
340void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
341    task->mRunAt = runAtNs;
342    queue(task);
343}
344
345void RenderThread::remove(RenderTask* task) {
346    AutoMutex _lock(mLock);
347    mQueue.remove(task);
348}
349
350void RenderThread::postFrameCallback(IFrameCallback* callback) {
351    mPendingRegistrationFrameCallbacks.insert(callback);
352}
353
354bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
355    size_t erased;
356    erased = mFrameCallbacks.erase(callback);
357    erased |= mPendingRegistrationFrameCallbacks.erase(callback);
358    return erased;
359}
360
361void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
362    if (mFrameCallbacks.erase(callback)) {
363        mPendingRegistrationFrameCallbacks.insert(callback);
364    }
365}
366
367RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
368    AutoMutex _lock(mLock);
369    RenderTask* next = mQueue.peek();
370    if (!next) {
371        mNextWakeup = LLONG_MAX;
372    } else {
373        mNextWakeup = next->mRunAt;
374        // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
375        if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
376            next = mQueue.next();
377        } else {
378            next = nullptr;
379        }
380    }
381    if (nextWakeup) {
382        *nextWakeup = mNextWakeup;
383    }
384    return next;
385}
386
387} /* namespace renderthread */
388} /* namespace uirenderer */
389} /* namespace android */
390