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