RenderThread.h revision e45b1fd03b524d2b57cc6c222d89076a31a08bea
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#ifndef RENDERTHREAD_H_
18#define RENDERTHREAD_H_
19
20#include "RenderTask.h"
21
22#include <memory>
23#include <set>
24
25#include <cutils/compiler.h>
26#include <utils/Looper.h>
27#include <utils/Mutex.h>
28#include <utils/Singleton.h>
29#include <utils/Thread.h>
30
31namespace android {
32class DisplayEventReceiver;
33
34namespace uirenderer {
35namespace renderthread {
36
37class DispatchFrameCallbacks;
38
39class TaskQueue {
40public:
41    TaskQueue();
42
43    RenderTask* next();
44    void queue(RenderTask* task);
45    RenderTask* peek();
46    void remove(RenderTask* task);
47
48private:
49    RenderTask* mHead;
50    RenderTask* mTail;
51};
52
53// Mimics android.view.Choreographer.FrameCallback
54class IFrameCallback {
55public:
56    virtual void doFrame(nsecs_t frameTimeNanos) = 0;
57
58protected:
59    ~IFrameCallback() {}
60};
61
62class ANDROID_API RenderThread : public Thread, public Singleton<RenderThread> {
63public:
64    // RenderThread takes complete ownership of tasks that are queued
65    // and will delete them after they are run
66    ANDROID_API void queue(RenderTask* task);
67    void queueDelayed(RenderTask* task, int delayMs);
68    void remove(RenderTask* task);
69
70    // Mimics android.view.Choreographer
71    void postFrameCallback(IFrameCallback* callback);
72    void removeFrameCallback(IFrameCallback* callback);
73
74protected:
75    virtual bool threadLoop();
76
77private:
78    friend class Singleton<RenderThread>;
79    friend class DispatchFrameCallbacks;
80
81    RenderThread();
82    virtual ~RenderThread();
83
84    void initializeDisplayEventReceiver();
85    static int displayEventReceiverCallback(int fd, int events, void* data);
86    void drainDisplayEventQueue();
87    void dispatchFrameCallbacks();
88
89    // Returns the next task to be run. If this returns NULL nextWakeup is set
90    // to the time to requery for the nextTask to run. mNextWakeup is also
91    // set to this time
92    RenderTask* nextTask(nsecs_t* nextWakeup);
93
94    sp<Looper> mLooper;
95    Mutex mLock;
96
97    nsecs_t mNextWakeup;
98    TaskQueue mQueue;
99
100    DisplayEventReceiver* mDisplayEventReceiver;
101    bool mVsyncRequested;
102    std::set<IFrameCallback*> mFrameCallbacks;
103    bool mFrameCallbackTaskPending;
104    DispatchFrameCallbacks* mFrameCallbackTask;
105    nsecs_t mFrameTime;
106};
107
108} /* namespace renderthread */
109} /* namespace uirenderer */
110} /* namespace android */
111#endif /* RENDERTHREAD_H_ */
112