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