blob: 4a1fd9ee3d479141686265c62772cc17bdc38a1f [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 Recke45b1fd2014-04-15 09:50:16 -070026
Derek Sollenberger98f75d52016-10-25 10:25:45 -040027#include <GrContext.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040028#include <SkBitmap.h>
John Reck1bcacfd2017-11-03 10:12:19 -070029#include <cutils/compiler.h>
John Reckb36016c2015-03-11 08:50:53 -070030#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080031#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080032#include <utils/Thread.h>
33
John Reck1bcacfd2017-11-03 10:12:19 -070034#include <thread/ThreadBase.h>
John Reckba6adf62015-02-19 14:36:50 -080035#include <memory>
John Reckf8441e62017-10-23 13:10:41 -070036#include <mutex>
John Reckba6adf62015-02-19 14:36:50 -080037#include <set>
John Reck18f16e62014-05-02 16:46:41 -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
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050045class Readback;
John Reck3b202512014-06-23 13:13:08 -070046class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070047class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070048
John Reckcec24ae2013-11-05 13:27:50 -080049namespace renderthread {
50
John Reck443a7142014-09-04 17:40:05 -070051class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070052class EglManager;
53class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070055
John Recke45b1fd2014-04-15 09:50:16 -070056// Mimics android.view.Choreographer.FrameCallback
57class IFrameCallback {
58public:
John Reck18f16e62014-05-02 16:46:41 -070059 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070060
61protected:
62 ~IFrameCallback() {}
63};
64
John Reck56428472018-03-16 17:27:17 -070065struct VsyncSource {
66 virtual void requestNextVsync() = 0;
67 virtual nsecs_t latestVsyncEvent() = 0;
68 virtual ~VsyncSource() {}
69};
70
71class DummyVsyncSource;
72
John Reckf8441e62017-10-23 13:10:41 -070073class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080074 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070075
John Reck1bcacfd2017-11-03 10:12:19 -070076public:
John Reck259b25a2017-12-01 16:18:53 -080077 // Sets a callback that fires before any RenderThread setup has occured.
78 ANDROID_API static void setOnStartHook(void (*onStartHook)());
79
John Reckf8441e62017-10-23 13:10:41 -070080 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080081
John Recke45b1fd2014-04-15 09:50:16 -070082 // Mimics android.view.Choreographer
83 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080084 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070085 // If the callback is currently registered, it will be pushed back until
86 // the next vsync. If it is not currently registered this does nothing.
87 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070088
John Reck18f16e62014-05-02 16:46:41 -070089 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040090 RenderState& renderState() const { return *mRenderState; }
91 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -070092 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050093 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -070094
John Reckb36016c2015-03-11 08:50:53 -070095 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
96
Derek Sollenberger98f75d52016-10-25 10:25:45 -040097 GrContext* getGrContext() const { return mGrContext.get(); }
Greg Daniel660d6ec2017-12-08 11:44:27 -050098 void setGrContext(sk_sp<GrContext> cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -040099
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400100 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500101 VulkanManager& vulkanManager() { return *mVkManager; }
102
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400103 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400104 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400105
John Reck1e510712018-04-23 08:15:03 -0700106 void requireGlContext();
107 void destroyGlContext();
108
Stan Iliev6b894d72017-08-23 12:41:41 -0400109 /**
110 * isCurrent provides a way to query, if the caller is running on
111 * the render thread.
112 *
113 * @return true only if isCurrent is invoked from the render thread.
114 */
115 static bool isCurrent();
116
John Reckcec24ae2013-11-05 13:27:50 -0800117protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800118 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800119
120private:
John Recke45b1fd2014-04-15 09:50:16 -0700121 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700122 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700123 friend class DummyVsyncSource;
Chris Craik0a24b142015-10-19 17:10:19 -0700124 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800125
126 RenderThread();
127 virtual ~RenderThread();
128
John Reck6b507802015-11-03 10:09:59 -0800129 static bool hasInstance();
130 static RenderThread& getInstance();
131
John Reck3b202512014-06-23 13:13:08 -0700132 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700133 void initializeDisplayEventReceiver();
134 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800135 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700136 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700137 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700138
John Reckb36016c2015-03-11 08:50:53 -0700139 DisplayInfo mDisplayInfo;
140
John Reck56428472018-03-16 17:27:17 -0700141 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700142 bool mVsyncRequested;
143 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700144 // We defer the actual registration of these callbacks until
145 // both mQueue *and* mDisplayEventReceiver have been drained off all
146 // immediate events. This makes sure that we catch the next vsync, not
147 // the previous one
148 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700149 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700150
151 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700152 RenderState* mRenderState;
153 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800154
John Reck34781b22017-07-05 16:39:36 -0700155 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500156 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400157
158 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400159 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500160 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800161};
162
163} /* namespace renderthread */
164} /* namespace uirenderer */
165} /* namespace android */
166#endif /* RENDERTHREAD_H_ */