RenderThread.h revision 443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44
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
31#include "TimeLord.h"
32
33namespace android {
34
35class DisplayEventReceiver;
36
37namespace uirenderer {
38
39class RenderState;
40
41namespace renderthread {
42
43class CanvasContext;
44class DispatchFrameCallbacks;
45class EglManager;
46class RenderProxy;
47
48class TaskQueue {
49public:
50    TaskQueue();
51
52    RenderTask* next();
53    void queue(RenderTask* task);
54    void queueAtFront(RenderTask* task);
55    RenderTask* peek();
56    void remove(RenderTask* task);
57
58private:
59    RenderTask* mHead;
60    RenderTask* mTail;
61};
62
63// Mimics android.view.Choreographer.FrameCallback
64class IFrameCallback {
65public:
66    virtual void doFrame() = 0;
67
68protected:
69    ~IFrameCallback() {}
70};
71
72class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
73public:
74    // RenderThread takes complete ownership of tasks that are queued
75    // and will delete them after they are run
76    ANDROID_API void queue(RenderTask* task);
77    ANDROID_API void queueAtFront(RenderTask* task);
78    void queueDelayed(RenderTask* task, int delayMs);
79    void remove(RenderTask* task);
80
81    // Mimics android.view.Choreographer
82    void postFrameCallback(IFrameCallback* callback);
83    void removeFrameCallback(IFrameCallback* callback);
84    // If the callback is currently registered, it will be pushed back until
85    // the next vsync. If it is not currently registered this does nothing.
86    void pushBackFrameCallback(IFrameCallback* callback);
87
88    TimeLord& timeLord() { return mTimeLord; }
89    RenderState& renderState() { return *mRenderState; }
90    EglManager& eglManager() { return *mEglManager; }
91
92protected:
93    virtual bool threadLoop();
94
95private:
96    friend class Singleton<RenderThread>;
97    friend class DispatchFrameCallbacks;
98    friend class RenderProxy;
99
100    RenderThread();
101    virtual ~RenderThread();
102
103    void initThreadLocals();
104    void initializeDisplayEventReceiver();
105    static int displayEventReceiverCallback(int fd, int events, void* data);
106    void drainDisplayEventQueue(bool skipCallbacks = false);
107    void dispatchFrameCallbacks();
108    void requestVsync();
109
110    // Returns the next task to be run. If this returns NULL nextWakeup is set
111    // to the time to requery for the nextTask to run. mNextWakeup is also
112    // set to this time
113    RenderTask* nextTask(nsecs_t* nextWakeup);
114
115    sp<Looper> mLooper;
116    Mutex mLock;
117
118    nsecs_t mNextWakeup;
119    TaskQueue mQueue;
120
121    DisplayEventReceiver* mDisplayEventReceiver;
122    bool mVsyncRequested;
123    std::set<IFrameCallback*> mFrameCallbacks;
124    // We defer the actual registration of these callbacks until
125    // both mQueue *and* mDisplayEventReceiver have been drained off all
126    // immediate events. This makes sure that we catch the next vsync, not
127    // the previous one
128    std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
129    bool mFrameCallbackTaskPending;
130    DispatchFrameCallbacks* mFrameCallbackTask;
131
132    TimeLord mTimeLord;
133    RenderState* mRenderState;
134    EglManager* mEglManager;
135};
136
137} /* namespace renderthread */
138} /* namespace uirenderer */
139} /* namespace android */
140#endif /* RENDERTHREAD_H_ */
141