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