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