• 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 <SkBitmap.h>
21 #include <cutils/compiler.h>
22 #include <include/gpu/ganesh/GrDirectContext.h>
23 #include <surface_control_private.h>
24 #include <utils/Thread.h>
25 
26 #include <memory>
27 #include <mutex>
28 #include <set>
29 
30 #include "CacheManager.h"
31 #include "MemoryPolicy.h"
32 #include "ProfileDataContainer.h"
33 #include "RenderTask.h"
34 #include "TimeLord.h"
35 #include "WebViewFunctorManager.h"
36 #include "thread/ThreadBase.h"
37 #include "utils/TimeUtils.h"
38 
39 namespace android {
40 
41 class Bitmap;
42 
43 namespace uirenderer {
44 
45 class AutoBackendTextureRelease;
46 class Readback;
47 class RenderState;
48 class TestUtils;
49 
50 namespace skiapipeline {
51 class VkFunctorDrawHandler;
52 }
53 
54 namespace VectorDrawable {
55 class Tree;
56 }
57 
58 namespace renderthread {
59 
60 class CanvasContext;
61 class EglManager;
62 class RenderProxy;
63 class VulkanManager;
64 
65 // Mimics android.view.Choreographer.FrameCallback
66 class IFrameCallback {
67 public:
68     virtual void doFrame() = 0;
69 
70 protected:
~IFrameCallback()71     virtual ~IFrameCallback() {}
72 };
73 
74 struct VsyncSource {
75     virtual void requestNextVsync() = 0;
76     virtual void drainPendingEvents() = 0;
~VsyncSourceVsyncSource77     virtual ~VsyncSource() {}
78 };
79 
80 class ChoreographerSource;
81 class DummyVsyncSource;
82 
83 typedef void (*JVMAttachHook)(const char* name);
84 
85 class RenderThread : private ThreadBase {
86     PREVENT_COPY_AND_ASSIGN(RenderThread);
87 
88 public:
89     // Sets a callback that fires before any RenderThread setup has occurred.
90     static void setOnStartHook(JVMAttachHook onStartHook);
91     static JVMAttachHook getOnStartHook();
92 
queue()93     WorkQueue& queue() { return ThreadBase::queue(); }
94 
95     // Mimics android.view.Choreographer
96     void postFrameCallback(IFrameCallback* callback);
97     bool removeFrameCallback(IFrameCallback* callback);
98     // If the callback is currently registered, it will be pushed back until
99     // the next vsync. If it is not currently registered this does nothing.
100     void pushBackFrameCallback(IFrameCallback* callback);
101 
timeLord()102     TimeLord& timeLord() { return mTimeLord; }
renderState()103     RenderState& renderState() const { return *mRenderState; }
eglManager()104     EglManager& eglManager() const { return *mEglManager; }
globalProfileData()105     ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
getJankDataMutex()106     std::mutex& getJankDataMutex() { return mJankDataMutex; }
107     Readback& readback();
108 
getGrContext()109     GrDirectContext* getGrContext() const { return mGrContext.get(); }
110     void setGrContext(sk_sp<GrDirectContext> cxt);
111     sk_sp<GrDirectContext> requireGrContext();
112 
cacheManager()113     CacheManager& cacheManager() { return *mCacheManager; }
114     VulkanManager& vulkanManager();
115 
116     sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
117     void dumpGraphicsMemory(int fd, bool includeProfileData);
118     void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
119 
120     void requireGlContext();
121     void requireVkContext();
122     void destroyRenderingContext();
123 
124     void preload();
125 
126     void trimMemory(TrimLevel level);
127     void trimCaches(CacheTrimLevel level);
128 
129     /**
130      * isCurrent provides a way to query, if the caller is running on
131      * the render thread.
132      *
133      * @return true only if isCurrent is invoked from the render thread.
134      */
135     static bool isCurrent();
136 
137     static void initGrContextOptions(GrContextOptions& options);
138 
139 protected:
140     virtual bool threadLoop() override;
141 
142 private:
143     friend class DispatchFrameCallbacks;
144     friend class RenderProxy;
145     friend class DummyVsyncSource;
146     friend class ChoreographerSource;
147     friend class android::uirenderer::AutoBackendTextureRelease;
148     friend class android::uirenderer::TestUtils;
149     friend class android::uirenderer::WebViewFunctor;
150     friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
151     friend class android::uirenderer::VectorDrawable::Tree;
152     friend class sp<RenderThread>;
153 
154     RenderThread();
155     virtual ~RenderThread();
156 
157     static bool hasInstance();
158     static RenderThread& getInstance();
159 
160     void initThreadLocals();
161     void initializeChoreographer();
162     void setupFrameInterval();
163     // Callbacks for choreographer events:
164     // choreographerCallback will call AChoreograper_handleEvent to call the
165     // corresponding callbacks for each display event type
166     static int choreographerCallback(int fd, int events, void* data);
167     // Callback that will be run on vsync ticks.
168     static void extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, void* data);
169     void frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
170                        int64_t frameInterval);
171     // Callback that will be run whenver there is a refresh rate change.
172     static void refreshRateCallback(int64_t vsyncPeriod, void* data);
173     void drainDisplayEventQueue();
174     void dispatchFrameCallbacks();
175     void requestVsync();
176 
177     AChoreographer* mChoreographer;
178     VsyncSource* mVsyncSource;
179     bool mVsyncRequested;
180     std::set<IFrameCallback*> mFrameCallbacks;
181     // We defer the actual registration of these callbacks until
182     // both mQueue *and* mDisplayEventReceiver have been drained off all
183     // immediate events. This makes sure that we catch the next vsync, not
184     // the previous one
185     std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
186     bool mFrameCallbackTaskPending;
187 
188     TimeLord mTimeLord;
189     RenderState* mRenderState;
190     EglManager* mEglManager;
191     WebViewFunctorManager& mFunctorManager;
192 
193     ProfileDataContainer mGlobalProfileData;
194     Readback* mReadback = nullptr;
195 
196     sk_sp<GrDirectContext> mGrContext;
197     CacheManager* mCacheManager;
198     sp<VulkanManager> mVkManager;
199 
200     std::mutex mJankDataMutex;
201 };
202 
203 } /* namespace renderthread */
204 } /* namespace uirenderer */
205 } /* namespace android */
206 #endif /* RENDERTHREAD_H_ */
207