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