blob: da79e97a6cebce11a2b3a694da75c43659ef12f0 [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
Stan Ilievaaa9e832019-09-17 14:07:23 -040020#include <GrContext.h>
21#include <SkBitmap.h>
22#include <cutils/compiler.h>
23#include <thread/ThreadBase.h>
24#include <utils/Looper.h>
25#include <utils/Thread.h>
26
27#include <memory>
28#include <mutex>
29#include <set>
John Recke45b1fd2014-04-15 09:50:16 -070030
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040031#include "CacheManager.h"
Fedor Kudasov85d6f102019-07-31 15:58:16 +010032#include "ProfileDataContainer.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040033#include "RenderTask.h"
John Reckba6adf62015-02-19 14:36:50 -080034#include "TimeLord.h"
John Reck283bb462018-12-13 16:40:14 -080035#include "WebViewFunctorManager.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070036#include "thread/ThreadBase.h"
John Reck3c0f5632019-03-15 16:36:01 -070037#include "utils/TimeUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070038
John Reckcec24ae2013-11-05 13:27:50 -080039namespace android {
John Reck3b202512014-06-23 13:13:08 -070040
Stan Iliev7bc3bc62017-05-24 13:28:36 -040041class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070042
John Reckcec24ae2013-11-05 13:27:50 -080043namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070044
Stan Ilievaaa9e832019-09-17 14:07:23 -040045class AutoBackendTextureRelease;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050046class Readback;
John Reck3b202512014-06-23 13:13:08 -070047class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070048class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070049
Bo Liu7b8c1eb2019-01-08 20:17:55 -080050namespace skiapipeline {
51class VkFunctorDrawHandler;
52}
53
Stan Iliev52e43922019-08-06 15:59:44 -040054namespace VectorDrawable {
55class Tree;
56}
57
John Reckcec24ae2013-11-05 13:27:50 -080058namespace renderthread {
59
John Reck443a7142014-09-04 17:40:05 -070060class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070061class EglManager;
62class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050063class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070064
John Recke45b1fd2014-04-15 09:50:16 -070065// Mimics android.view.Choreographer.FrameCallback
66class IFrameCallback {
67public:
John Reck18f16e62014-05-02 16:46:41 -070068 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070069
70protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070071 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070072};
73
John Reck56428472018-03-16 17:27:17 -070074struct VsyncSource {
75 virtual void requestNextVsync() = 0;
76 virtual nsecs_t latestVsyncEvent() = 0;
77 virtual ~VsyncSource() {}
78};
79
80class DummyVsyncSource;
81
Stan Iliev978d5322019-02-06 12:02:28 -050082typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -050083
John Reckf8441e62017-10-23 13:10:41 -070084class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080085 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070086
John Reck1bcacfd2017-11-03 10:12:19 -070087public:
John Reck8785ceb2018-10-29 16:45:58 -070088 // Sets a callback that fires before any RenderThread setup has occurred.
Stan Iliev80dbc352019-02-05 15:31:28 -050089 ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook);
90 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -080091
John Reckf8441e62017-10-23 13:10:41 -070092 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080093
John Recke45b1fd2014-04-15 09:50:16 -070094 // Mimics android.view.Choreographer
95 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080096 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070097 // If the callback is currently registered, it will be pushed back until
98 // the next vsync. If it is not currently registered this does nothing.
99 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -0700100
John Reck18f16e62014-05-02 16:46:41 -0700101 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400102 RenderState& renderState() const { return *mRenderState; }
103 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700104 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500105 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700106
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400107 GrContext* getGrContext() const { return mGrContext.get(); }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500108 void setGrContext(sk_sp<GrContext> cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400109
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400110 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111 VulkanManager& vulkanManager() { return *mVkManager; }
112
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400113 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400114 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400115
John Reck1e510712018-04-23 08:15:03 -0700116 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500117 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800118 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700119
Stan Iliev898123b2019-02-14 14:57:44 -0500120 void preload();
121
Stan Iliev6b894d72017-08-23 12:41:41 -0400122 /**
123 * isCurrent provides a way to query, if the caller is running on
124 * the render thread.
125 *
126 * @return true only if isCurrent is invoked from the render thread.
127 */
128 static bool isCurrent();
129
Stan Iliev981afe72019-02-13 14:24:33 -0500130 static void initGrContextOptions(GrContextOptions& options);
131
John Reckcec24ae2013-11-05 13:27:50 -0800132protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800133 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800134
135private:
John Recke45b1fd2014-04-15 09:50:16 -0700136 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700137 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700138 friend class DummyVsyncSource;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400139 friend class android::uirenderer::AutoBackendTextureRelease;
Chris Craik0a24b142015-10-19 17:10:19 -0700140 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800141 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800142 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
Stan Iliev52e43922019-08-06 15:59:44 -0400143 friend class android::uirenderer::VectorDrawable::Tree;
John Reckcec24ae2013-11-05 13:27:50 -0800144
145 RenderThread();
146 virtual ~RenderThread();
147
John Reck6b507802015-11-03 10:09:59 -0800148 static bool hasInstance();
149 static RenderThread& getInstance();
150
John Reck3b202512014-06-23 13:13:08 -0700151 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700152 void initializeDisplayEventReceiver();
John Reckcf185f52019-04-11 16:11:24 -0700153 void setupFrameInterval();
John Recke45b1fd2014-04-15 09:50:16 -0700154 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800155 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700156 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700157 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700158
John Reck56428472018-03-16 17:27:17 -0700159 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700160 bool mVsyncRequested;
161 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700162 // We defer the actual registration of these callbacks until
163 // both mQueue *and* mDisplayEventReceiver have been drained off all
164 // immediate events. This makes sure that we catch the next vsync, not
165 // the previous one
166 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700167 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700168
169 TimeLord mTimeLord;
John Reck3c0f5632019-03-15 16:36:01 -0700170 nsecs_t mDispatchFrameDelay = 4_ms;
John Reck3b202512014-06-23 13:13:08 -0700171 RenderState* mRenderState;
172 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800173 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800174
John Reck34781b22017-07-05 16:39:36 -0700175 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500176 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400177
178 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500180 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800181};
182
183} /* namespace renderthread */
184} /* namespace uirenderer */
185} /* namespace android */
186#endif /* RENDERTHREAD_H_ */