blob: 419e7c7a6b51155d517a6a562c84d40a73d015aa [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 Recke45b1fd2014-04-15 09:50:16 -070027
Derek Sollenberger98f75d52016-10-25 10:25:45 -040028#include <GrContext.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040029#include <SkBitmap.h>
John Reck1bcacfd2017-11-03 10:12:19 -070030#include <cutils/compiler.h>
John Reckb36016c2015-03-11 08:50:53 -070031#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080032#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080033#include <utils/Thread.h>
34
John Reck1bcacfd2017-11-03 10:12:19 -070035#include <thread/ThreadBase.h>
John Reckba6adf62015-02-19 14:36:50 -080036#include <memory>
John Reckf8441e62017-10-23 13:10:41 -070037#include <mutex>
John Reckba6adf62015-02-19 14:36:50 -080038#include <set>
John Reck18f16e62014-05-02 16:46:41 -070039
John Reckcec24ae2013-11-05 13:27:50 -080040namespace android {
John Reck3b202512014-06-23 13:13:08 -070041
Stan Iliev7bc3bc62017-05-24 13:28:36 -040042class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070043
John Reckcec24ae2013-11-05 13:27:50 -080044namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070045
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
John Reckcec24ae2013-11-05 13:27:50 -080054namespace renderthread {
55
John Reck443a7142014-09-04 17:40:05 -070056class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070057class EglManager;
58class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050059class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070060
John Recke45b1fd2014-04-15 09:50:16 -070061// Mimics android.view.Choreographer.FrameCallback
62class IFrameCallback {
63public:
John Reck18f16e62014-05-02 16:46:41 -070064 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070065
66protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070067 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070068};
69
John Reck56428472018-03-16 17:27:17 -070070struct VsyncSource {
71 virtual void requestNextVsync() = 0;
72 virtual nsecs_t latestVsyncEvent() = 0;
73 virtual ~VsyncSource() {}
74};
75
76class DummyVsyncSource;
77
Stan Iliev978d5322019-02-06 12:02:28 -050078typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -050079
John Reckf8441e62017-10-23 13:10:41 -070080class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080081 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070082
John Reck1bcacfd2017-11-03 10:12:19 -070083public:
John Reck8785ceb2018-10-29 16:45:58 -070084 // Sets a callback that fires before any RenderThread setup has occurred.
Stan Iliev80dbc352019-02-05 15:31:28 -050085 ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook);
86 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -080087
John Reckf8441e62017-10-23 13:10:41 -070088 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080089
John Recke45b1fd2014-04-15 09:50:16 -070090 // Mimics android.view.Choreographer
91 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080092 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070093 // If the callback is currently registered, it will be pushed back until
94 // the next vsync. If it is not currently registered this does nothing.
95 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070096
John Reck18f16e62014-05-02 16:46:41 -070097 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040098 RenderState& renderState() const { return *mRenderState; }
99 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700100 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500101 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700102
John Reckb36016c2015-03-11 08:50:53 -0700103 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
104
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400105 GrContext* getGrContext() const { return mGrContext.get(); }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500106 void setGrContext(sk_sp<GrContext> cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400107
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400108 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 VulkanManager& vulkanManager() { return *mVkManager; }
110
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400111 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400112 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400113
John Reck1e510712018-04-23 08:15:03 -0700114 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500115 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800116 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700117
Stan Iliev6b894d72017-08-23 12:41:41 -0400118 /**
119 * isCurrent provides a way to query, if the caller is running on
120 * the render thread.
121 *
122 * @return true only if isCurrent is invoked from the render thread.
123 */
124 static bool isCurrent();
125
Stan Iliev981afe72019-02-13 14:24:33 -0500126 static void initGrContextOptions(GrContextOptions& options);
127
John Reckcec24ae2013-11-05 13:27:50 -0800128protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800129 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800130
131private:
John Recke45b1fd2014-04-15 09:50:16 -0700132 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700133 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700134 friend class DummyVsyncSource;
Chris Craik0a24b142015-10-19 17:10:19 -0700135 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800136 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800137 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
John Reckcec24ae2013-11-05 13:27:50 -0800138
139 RenderThread();
140 virtual ~RenderThread();
141
John Reck6b507802015-11-03 10:09:59 -0800142 static bool hasInstance();
143 static RenderThread& getInstance();
144
John Reck3b202512014-06-23 13:13:08 -0700145 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700146 void initializeDisplayEventReceiver();
147 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800148 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700149 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700150 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700151
John Reckb36016c2015-03-11 08:50:53 -0700152 DisplayInfo mDisplayInfo;
153
John Reck56428472018-03-16 17:27:17 -0700154 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700155 bool mVsyncRequested;
156 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700157 // We defer the actual registration of these callbacks until
158 // both mQueue *and* mDisplayEventReceiver have been drained off all
159 // immediate events. This makes sure that we catch the next vsync, not
160 // the previous one
161 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700162 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700163
164 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700165 RenderState* mRenderState;
166 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800167 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800168
John Reck34781b22017-07-05 16:39:36 -0700169 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500170 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400171
172 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500174 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800175};
176
177} /* namespace renderthread */
178} /* namespace uirenderer */
179} /* namespace android */
180#endif /* RENDERTHREAD_H_ */