blob: 9298be65c5e8f747012a4540160b8baa7e8ca1cf [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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"
John Recke45b1fd2014-04-15 09:50:16 -070021
John Reckba6adf62015-02-19 14:36:50 -080022#include "../JankTracker.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040023#include "CacheManager.h"
John Reckba6adf62015-02-19 14:36:50 -080024#include "TimeLord.h"
John Reckf8441e62017-10-23 13:10:41 -070025#include "thread/ThreadBase.h"
John Reck283bb462018-12-13 16:40:14 -080026#include "WebViewFunctorManager.h"
John Reck3c0f5632019-03-15 16:36:01 -070027#include "utils/TimeUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070028
Derek Sollenberger98f75d52016-10-25 10:25:45 -040029#include <GrContext.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040030#include <SkBitmap.h>
John Reck1bcacfd2017-11-03 10:12:19 -070031#include <cutils/compiler.h>
John Reckb36016c2015-03-11 08:50:53 -070032#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080033#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080034#include <utils/Thread.h>
35
John Reck1bcacfd2017-11-03 10:12:19 -070036#include <thread/ThreadBase.h>
John Reckba6adf62015-02-19 14:36:50 -080037#include <memory>
John Reckf8441e62017-10-23 13:10:41 -070038#include <mutex>
John Reckba6adf62015-02-19 14:36:50 -080039#include <set>
John Reck18f16e62014-05-02 16:46:41 -070040
John Reckcec24ae2013-11-05 13:27:50 -080041namespace android {
John Reck3b202512014-06-23 13:13:08 -070042
Stan Iliev7bc3bc62017-05-24 13:28:36 -040043class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070044
John Reckcec24ae2013-11-05 13:27:50 -080045namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070046
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050047class Readback;
John Reck3b202512014-06-23 13:13:08 -070048class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070049class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070050
Bo Liu7b8c1eb2019-01-08 20:17:55 -080051namespace skiapipeline {
52class VkFunctorDrawHandler;
53}
54
John Reckcec24ae2013-11-05 13:27:50 -080055namespace renderthread {
56
John Reck443a7142014-09-04 17:40:05 -070057class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070058class EglManager;
59class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070061
John Recke45b1fd2014-04-15 09:50:16 -070062// Mimics android.view.Choreographer.FrameCallback
63class IFrameCallback {
64public:
John Reck18f16e62014-05-02 16:46:41 -070065 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070066
67protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070068 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070069};
70
John Reck56428472018-03-16 17:27:17 -070071struct VsyncSource {
72 virtual void requestNextVsync() = 0;
73 virtual nsecs_t latestVsyncEvent() = 0;
74 virtual ~VsyncSource() {}
75};
76
77class DummyVsyncSource;
78
Stan Iliev978d5322019-02-06 12:02:28 -050079typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -050080
John Reckf8441e62017-10-23 13:10:41 -070081class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080082 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070083
John Reck1bcacfd2017-11-03 10:12:19 -070084public:
John Reck8785ceb2018-10-29 16:45:58 -070085 // Sets a callback that fires before any RenderThread setup has occurred.
Stan Iliev80dbc352019-02-05 15:31:28 -050086 ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook);
87 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -080088
John Reckf8441e62017-10-23 13:10:41 -070089 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080090
John Recke45b1fd2014-04-15 09:50:16 -070091 // Mimics android.view.Choreographer
92 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080093 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070094 // If the callback is currently registered, it will be pushed back until
95 // the next vsync. If it is not currently registered this does nothing.
96 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070097
John Reck18f16e62014-05-02 16:46:41 -070098 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040099 RenderState& renderState() const { return *mRenderState; }
100 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700101 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500102 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700103
John Reckb36016c2015-03-11 08:50:53 -0700104 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
105
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400106 GrContext* getGrContext() const { return mGrContext.get(); }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500107 void setGrContext(sk_sp<GrContext> cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400108
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400109 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500110 VulkanManager& vulkanManager() { return *mVkManager; }
111
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400112 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400113 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400114
John Reck1e510712018-04-23 08:15:03 -0700115 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500116 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800117 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700118
Stan Iliev898123b2019-02-14 14:57:44 -0500119 void preload();
120
Stan Iliev6b894d72017-08-23 12:41:41 -0400121 /**
122 * isCurrent provides a way to query, if the caller is running on
123 * the render thread.
124 *
125 * @return true only if isCurrent is invoked from the render thread.
126 */
127 static bool isCurrent();
128
Stan Iliev981afe72019-02-13 14:24:33 -0500129 static void initGrContextOptions(GrContextOptions& options);
130
John Reckcec24ae2013-11-05 13:27:50 -0800131protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800132 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800133
134private:
John Recke45b1fd2014-04-15 09:50:16 -0700135 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700136 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700137 friend class DummyVsyncSource;
Chris Craik0a24b142015-10-19 17:10:19 -0700138 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800139 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800140 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
John Reckcec24ae2013-11-05 13:27:50 -0800141
142 RenderThread();
143 virtual ~RenderThread();
144
John Reck6b507802015-11-03 10:09:59 -0800145 static bool hasInstance();
146 static RenderThread& getInstance();
147
John Reck3b202512014-06-23 13:13:08 -0700148 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700149 void initializeDisplayEventReceiver();
150 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800151 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700152 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700153 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700154
John Reckb36016c2015-03-11 08:50:53 -0700155 DisplayInfo mDisplayInfo;
156
John Reck56428472018-03-16 17:27:17 -0700157 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700158 bool mVsyncRequested;
159 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700160 // We defer the actual registration of these callbacks until
161 // both mQueue *and* mDisplayEventReceiver have been drained off all
162 // immediate events. This makes sure that we catch the next vsync, not
163 // the previous one
164 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700165 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700166
167 TimeLord mTimeLord;
John Reck3c0f5632019-03-15 16:36:01 -0700168 nsecs_t mDispatchFrameDelay = 4_ms;
John Reck3b202512014-06-23 13:13:08 -0700169 RenderState* mRenderState;
170 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800171 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800172
John Reck34781b22017-07-05 16:39:36 -0700173 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500174 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400175
176 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400177 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500178 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800179};
180
181} /* namespace renderthread */
182} /* namespace uirenderer */
183} /* namespace android */
184#endif /* RENDERTHREAD_H_ */