• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <surface_control_private.h>
21 #include <GrDirectContext.h>
22 #include <SkBitmap.h>
23 #include <cutils/compiler.h>
24 #include <private/android/choreographer.h>
25 #include <thread/ThreadBase.h>
26 #include <utils/Looper.h>
27 #include <utils/Thread.h>
28 
29 #include <memory>
30 #include <mutex>
31 #include <set>
32 
33 #include "CacheManager.h"
34 #include "ProfileDataContainer.h"
35 #include "RenderTask.h"
36 #include "TimeLord.h"
37 #include "WebViewFunctorManager.h"
38 #include "thread/ThreadBase.h"
39 #include "utils/TimeUtils.h"
40 
41 namespace android {
42 
43 class Bitmap;
44 
45 namespace uirenderer {
46 
47 class AutoBackendTextureRelease;
48 class Readback;
49 class RenderState;
50 class TestUtils;
51 
52 namespace skiapipeline {
53 class VkFunctorDrawHandler;
54 }
55 
56 namespace VectorDrawable {
57 class Tree;
58 }
59 
60 namespace renderthread {
61 
62 class CanvasContext;
63 class EglManager;
64 class RenderProxy;
65 class VulkanManager;
66 
67 // Mimics android.view.Choreographer.FrameCallback
68 class IFrameCallback {
69 public:
70     virtual void doFrame() = 0;
71 
72 protected:
~IFrameCallback()73     virtual ~IFrameCallback() {}
74 };
75 
76 struct VsyncSource {
77     virtual void requestNextVsync() = 0;
78     virtual void drainPendingEvents() = 0;
~VsyncSourceVsyncSource79     virtual ~VsyncSource() {}
80 };
81 
82 typedef ASurfaceControl* (*ASC_create)(ASurfaceControl* parent, const char* debug_name);
83 typedef void (*ASC_acquire)(ASurfaceControl* control);
84 typedef void (*ASC_release)(ASurfaceControl* control);
85 
86 typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, int32_t id,
87                                                  void* context,
88                                                  ASurfaceControl_SurfaceStatsListener func);
89 typedef void (*ASC_unregisterSurfaceStatsListener)(void* context,
90                                                    ASurfaceControl_SurfaceStatsListener func);
91 
92 typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats);
93 typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats);
94 
95 typedef ASurfaceTransaction* (*AST_create)();
96 typedef void (*AST_delete)(ASurfaceTransaction* transaction);
97 typedef void (*AST_apply)(ASurfaceTransaction* transaction);
98 typedef void (*AST_reparent)(ASurfaceTransaction* aSurfaceTransaction,
99                              ASurfaceControl* aSurfaceControl,
100                              ASurfaceControl* newParentASurfaceControl);
101 typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction,
102                                   ASurfaceControl* surface_control, int8_t visibility);
103 typedef void (*AST_setZOrder)(ASurfaceTransaction* transaction, ASurfaceControl* surface_control,
104                               int32_t z_order);
105 
106 struct ASurfaceControlFunctions {
107     ASurfaceControlFunctions();
108 
109     ASC_create createFunc;
110     ASC_acquire acquireFunc;
111     ASC_release releaseFunc;
112     ASC_registerSurfaceStatsListener registerListenerFunc;
113     ASC_unregisterSurfaceStatsListener unregisterListenerFunc;
114     ASCStats_getAcquireTime getAcquireTimeFunc;
115     ASCStats_getFrameNumber getFrameNumberFunc;
116 
117     AST_create transactionCreateFunc;
118     AST_delete transactionDeleteFunc;
119     AST_apply transactionApplyFunc;
120     AST_reparent transactionReparentFunc;
121     AST_setVisibility transactionSetVisibilityFunc;
122     AST_setZOrder transactionSetZOrderFunc;
123 };
124 
125 class ChoreographerSource;
126 class DummyVsyncSource;
127 
128 typedef void (*JVMAttachHook)(const char* name);
129 
130 class RenderThread : private ThreadBase {
131     PREVENT_COPY_AND_ASSIGN(RenderThread);
132 
133 public:
134     // Sets a callback that fires before any RenderThread setup has occurred.
135     static void setOnStartHook(JVMAttachHook onStartHook);
136     static JVMAttachHook getOnStartHook();
137 
queue()138     WorkQueue& queue() { return ThreadBase::queue(); }
139 
140     // Mimics android.view.Choreographer
141     void postFrameCallback(IFrameCallback* callback);
142     bool removeFrameCallback(IFrameCallback* callback);
143     // If the callback is currently registered, it will be pushed back until
144     // the next vsync. If it is not currently registered this does nothing.
145     void pushBackFrameCallback(IFrameCallback* callback);
146 
timeLord()147     TimeLord& timeLord() { return mTimeLord; }
renderState()148     RenderState& renderState() const { return *mRenderState; }
eglManager()149     EglManager& eglManager() const { return *mEglManager; }
globalProfileData()150     ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
getJankDataMutex()151     std::mutex& getJankDataMutex() { return mJankDataMutex; }
152     Readback& readback();
153 
getGrContext()154     GrDirectContext* getGrContext() const { return mGrContext.get(); }
155     void setGrContext(sk_sp<GrDirectContext> cxt);
156     sk_sp<GrDirectContext> requireGrContext();
157 
cacheManager()158     CacheManager& cacheManager() { return *mCacheManager; }
159     VulkanManager& vulkanManager();
160 
161     sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
162     void dumpGraphicsMemory(int fd, bool includeProfileData);
163     void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
164 
165     void requireGlContext();
166     void requireVkContext();
167     void destroyRenderingContext();
168 
169     void preload();
170 
getASurfaceControlFunctions()171     const ASurfaceControlFunctions& getASurfaceControlFunctions() {
172         return mASurfaceControlFunctions;
173     }
174 
175     /**
176      * isCurrent provides a way to query, if the caller is running on
177      * the render thread.
178      *
179      * @return true only if isCurrent is invoked from the render thread.
180      */
181     static bool isCurrent();
182 
183     static void initGrContextOptions(GrContextOptions& options);
184 
185 protected:
186     virtual bool threadLoop() override;
187 
188 private:
189     friend class DispatchFrameCallbacks;
190     friend class RenderProxy;
191     friend class DummyVsyncSource;
192     friend class ChoreographerSource;
193     friend class android::uirenderer::AutoBackendTextureRelease;
194     friend class android::uirenderer::TestUtils;
195     friend class android::uirenderer::WebViewFunctor;
196     friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
197     friend class android::uirenderer::VectorDrawable::Tree;
198     friend class sp<RenderThread>;
199 
200     RenderThread();
201     virtual ~RenderThread();
202 
203     static bool hasInstance();
204     static RenderThread& getInstance();
205 
206     void initThreadLocals();
207     void initializeChoreographer();
208     void setupFrameInterval();
209     // Callbacks for choreographer events:
210     // choreographerCallback will call AChoreograper_handleEvent to call the
211     // corresponding callbacks for each display event type
212     static int choreographerCallback(int fd, int events, void* data);
213     // Callback that will be run on vsync ticks.
214     static void extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, void* data);
215     void frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
216                        int64_t frameInterval);
217     // Callback that will be run whenver there is a refresh rate change.
218     static void refreshRateCallback(int64_t vsyncPeriod, void* data);
219     void drainDisplayEventQueue();
220     void dispatchFrameCallbacks();
221     void requestVsync();
222 
223     AChoreographer* mChoreographer;
224     VsyncSource* mVsyncSource;
225     bool mVsyncRequested;
226     std::set<IFrameCallback*> mFrameCallbacks;
227     // We defer the actual registration of these callbacks until
228     // both mQueue *and* mDisplayEventReceiver have been drained off all
229     // immediate events. This makes sure that we catch the next vsync, not
230     // the previous one
231     std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
232     bool mFrameCallbackTaskPending;
233 
234     TimeLord mTimeLord;
235     nsecs_t mDispatchFrameDelay = 4_ms;
236     RenderState* mRenderState;
237     EglManager* mEglManager;
238     WebViewFunctorManager& mFunctorManager;
239 
240     ProfileDataContainer mGlobalProfileData;
241     Readback* mReadback = nullptr;
242 
243     sk_sp<GrDirectContext> mGrContext;
244     CacheManager* mCacheManager;
245     sp<VulkanManager> mVkManager;
246 
247     ASurfaceControlFunctions mASurfaceControlFunctions;
248     std::mutex mJankDataMutex;
249 };
250 
251 } /* namespace renderthread */
252 } /* namespace uirenderer */
253 } /* namespace android */
254 #endif /* RENDERTHREAD_H_ */
255