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