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