blob: d17a509f63574174165fa84486b40bb7e62f5574 [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 -070042class DisplayEventReceiver;
43
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
John Reckcec24ae2013-11-05 13:27:50 -080050namespace renderthread {
51
John Reck443a7142014-09-04 17:40:05 -070052class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070053class EglManager;
54class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050055class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070056
John Recke45b1fd2014-04-15 09:50:16 -070057// Mimics android.view.Choreographer.FrameCallback
58class IFrameCallback {
59public:
John Reck18f16e62014-05-02 16:46:41 -070060 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070061
62protected:
63 ~IFrameCallback() {}
64};
65
John Reckf8441e62017-10-23 13:10:41 -070066class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080067 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070068
John Reck1bcacfd2017-11-03 10:12:19 -070069public:
John Reckf8441e62017-10-23 13:10:41 -070070 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080071
John Recke45b1fd2014-04-15 09:50:16 -070072 // Mimics android.view.Choreographer
73 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080074 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070075 // If the callback is currently registered, it will be pushed back until
76 // the next vsync. If it is not currently registered this does nothing.
77 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070078
John Reck18f16e62014-05-02 16:46:41 -070079 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040080 RenderState& renderState() const { return *mRenderState; }
81 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -070082 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050083 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -070084
John Reckb36016c2015-03-11 08:50:53 -070085 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
86
Derek Sollenberger98f75d52016-10-25 10:25:45 -040087 GrContext* getGrContext() const { return mGrContext.get(); }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040088 void setGrContext(GrContext* cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -040089
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040090 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050091 VulkanManager& vulkanManager() { return *mVkManager; }
92
Stan Iliev7bc3bc62017-05-24 13:28:36 -040093 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040094 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -040095
Stan Iliev6b894d72017-08-23 12:41:41 -040096 /**
97 * isCurrent provides a way to query, if the caller is running on
98 * the render thread.
99 *
100 * @return true only if isCurrent is invoked from the render thread.
101 */
102 static bool isCurrent();
103
John Reckcec24ae2013-11-05 13:27:50 -0800104protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800105 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800106
107private:
John Recke45b1fd2014-04-15 09:50:16 -0700108 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700109 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700110 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800111
112 RenderThread();
113 virtual ~RenderThread();
114
John Reck6b507802015-11-03 10:09:59 -0800115 static bool hasInstance();
116 static RenderThread& getInstance();
117
John Reck3b202512014-06-23 13:13:08 -0700118 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700119 void initializeDisplayEventReceiver();
120 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800121 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700122 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700123 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700124
John Reckb36016c2015-03-11 08:50:53 -0700125 DisplayInfo mDisplayInfo;
126
John Recke45b1fd2014-04-15 09:50:16 -0700127 DisplayEventReceiver* mDisplayEventReceiver;
128 bool mVsyncRequested;
129 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700130 // We defer the actual registration of these callbacks until
131 // both mQueue *and* mDisplayEventReceiver have been drained off all
132 // immediate events. This makes sure that we catch the next vsync, not
133 // the previous one
134 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700135 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700136
137 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700138 RenderState* mRenderState;
139 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800140
John Reck34781b22017-07-05 16:39:36 -0700141 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500142 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400143
144 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400145 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500146 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800147};
148
149} /* namespace renderthread */
150} /* namespace uirenderer */
151} /* namespace android */
152#endif /* RENDERTHREAD_H_ */