John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 17 | #define LOG_TAG "ThreadedRenderer" |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 19 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 21 | #include <atomic> |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 23 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 24 | #include "jni.h" |
| 25 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 26 | #include "core_jni_helpers.h" |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 27 | #include <GraphicsJNI.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 28 | #include <nativehelper/ScopedPrimitiveArray.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 29 | |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 30 | #include <gui/BufferItemConsumer.h> |
| 31 | #include <gui/BufferQueue.h> |
| 32 | #include <gui/Surface.h> |
| 33 | |
Mathias Agopian | 04d0461 | 2017-03-08 23:21:54 -0800 | [diff] [blame] | 34 | #include <private/EGL/cache.h> |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 35 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 36 | #include <utils/Looper.h> |
| 37 | #include <utils/RefBase.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 38 | #include <utils/StrongPointer.h> |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 39 | #include <utils/Timers.h> |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 40 | #include <utils/TraceUtils.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 41 | #include <android_runtime/android_view_Surface.h> |
| 42 | #include <system/window.h> |
| 43 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 44 | #include "android_os_MessageQueue.h" |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 45 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 46 | #include <Animator.h> |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 47 | #include <AnimationContext.h> |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 48 | #include <FrameInfo.h> |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 49 | #include <FrameMetricsObserver.h> |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 50 | #include <IContextFactory.h> |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 51 | #include <Picture.h> |
John Reck | 938e884 | 2017-08-24 13:41:59 -0700 | [diff] [blame] | 52 | #include <Properties.h> |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 53 | #include <PropertyValuesAnimatorSet.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 54 | #include <RenderNode.h> |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 55 | #include <renderthread/CanvasContext.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 56 | #include <renderthread/RenderProxy.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 57 | #include <renderthread/RenderTask.h> |
| 58 | #include <renderthread/RenderThread.h> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 59 | #include <pipeline/skia/ShaderCache.h> |
Derek Sollenberger | e216948 | 2018-11-20 10:57:20 -0500 | [diff] [blame] | 60 | #include <utils/Color.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 61 | |
| 62 | namespace android { |
| 63 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 64 | using namespace android::uirenderer; |
| 65 | using namespace android::uirenderer::renderthread; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 66 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 67 | struct { |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 68 | jfieldID frameMetrics; |
| 69 | jfieldID timingDataBuffer; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 70 | jfieldID messageQueue; |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 71 | jmethodID callback; |
| 72 | } gFrameMetricsObserverClassInfo; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 73 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 74 | struct { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 75 | jclass clazz; |
| 76 | jmethodID invokePictureCapturedCallback; |
| 77 | } gHardwareRenderer; |
| 78 | |
| 79 | struct { |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 80 | jmethodID onFrameDraw; |
| 81 | } gFrameDrawingCallback; |
| 82 | |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 83 | struct { |
| 84 | jmethodID onFrameComplete; |
| 85 | } gFrameCompleteCallback; |
| 86 | |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 87 | static JNIEnv* getenv(JavaVM* vm) { |
| 88 | JNIEnv* env; |
| 89 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 90 | LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", vm); |
| 91 | } |
| 92 | return env; |
| 93 | } |
| 94 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 95 | class OnFinishedEvent { |
| 96 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 97 | OnFinishedEvent(BaseRenderNodeAnimator* animator, AnimationListener* listener) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 98 | : animator(animator), listener(listener) {} |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 99 | sp<BaseRenderNodeAnimator> animator; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 100 | sp<AnimationListener> listener; |
| 101 | }; |
| 102 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 103 | class InvokeAnimationListeners : public MessageHandler { |
| 104 | public: |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 105 | explicit InvokeAnimationListeners(std::vector<OnFinishedEvent>& events) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 106 | mOnFinishedEvents.swap(events); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 107 | } |
| 108 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 109 | static void callOnFinished(OnFinishedEvent& event) { |
| 110 | event.listener->onAnimationFinished(event.animator.get()); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | virtual void handleMessage(const Message& message) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 114 | std::for_each(mOnFinishedEvents.begin(), mOnFinishedEvents.end(), callOnFinished); |
| 115 | mOnFinishedEvents.clear(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 119 | std::vector<OnFinishedEvent> mOnFinishedEvents; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 122 | class FinishAndInvokeListener : public MessageHandler { |
| 123 | public: |
| 124 | explicit FinishAndInvokeListener(PropertyValuesAnimatorSet* anim) |
| 125 | : mAnimator(anim) { |
| 126 | mListener = anim->getOneShotListener(); |
| 127 | mRequestId = anim->getRequestId(); |
| 128 | } |
| 129 | |
| 130 | virtual void handleMessage(const Message& message) { |
| 131 | if (mAnimator->getRequestId() == mRequestId) { |
| 132 | // Request Id has not changed, meaning there's no animation lifecyle change since the |
| 133 | // message is posted, so go ahead and call finish to make sure the PlayState is properly |
| 134 | // updated. This is needed because before the next frame comes in from UI thread to |
| 135 | // trigger an animation update, there could be reverse/cancel etc. So we need to update |
| 136 | // the playstate in time to ensure all the subsequent events get chained properly. |
| 137 | mAnimator->end(); |
| 138 | } |
| 139 | mListener->onAnimationFinished(nullptr); |
| 140 | } |
| 141 | private: |
| 142 | sp<PropertyValuesAnimatorSet> mAnimator; |
| 143 | sp<AnimationListener> mListener; |
| 144 | uint32_t mRequestId; |
| 145 | }; |
| 146 | |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 147 | class RenderingException : public MessageHandler { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 148 | public: |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 149 | RenderingException(JavaVM* vm, const std::string& message) |
| 150 | : mVm(vm) |
| 151 | , mMessage(message) { |
| 152 | } |
| 153 | |
| 154 | virtual void handleMessage(const Message&) { |
| 155 | throwException(mVm, mMessage); |
| 156 | } |
| 157 | |
| 158 | static void throwException(JavaVM* vm, const std::string& message) { |
| 159 | JNIEnv* env = getenv(vm); |
| 160 | jniThrowException(env, "java/lang/IllegalStateException", message.c_str()); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | JavaVM* mVm; |
| 165 | std::string mMessage; |
| 166 | }; |
| 167 | |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 168 | class FrameCompleteWrapper : public MessageHandler { |
| 169 | public: |
| 170 | FrameCompleteWrapper(JNIEnv* env, jobject jobject) { |
| 171 | mLooper = Looper::getForThread(); |
| 172 | LOG_ALWAYS_FATAL_IF(!mLooper.get(), "Must create runnable on a Looper thread!"); |
| 173 | env->GetJavaVM(&mVm); |
| 174 | mObject = env->NewGlobalRef(jobject); |
| 175 | LOG_ALWAYS_FATAL_IF(!mObject, "Failed to make global ref"); |
| 176 | } |
| 177 | |
| 178 | virtual ~FrameCompleteWrapper() { |
| 179 | releaseObject(); |
| 180 | } |
| 181 | |
| 182 | void postFrameComplete(int64_t frameNr) { |
| 183 | if (mObject) { |
| 184 | mFrameNr = frameNr; |
| 185 | mLooper->sendMessage(this, 0); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | virtual void handleMessage(const Message&) { |
| 190 | if (mObject) { |
| 191 | ATRACE_FORMAT("frameComplete %" PRId64, mFrameNr); |
| 192 | getenv(mVm)->CallVoidMethod(mObject, gFrameCompleteCallback.onFrameComplete, mFrameNr); |
| 193 | releaseObject(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | JavaVM* mVm; |
| 199 | jobject mObject; |
| 200 | sp<Looper> mLooper; |
| 201 | int64_t mFrameNr = -1; |
| 202 | |
| 203 | void releaseObject() { |
| 204 | if (mObject) { |
| 205 | getenv(mVm)->DeleteGlobalRef(mObject); |
| 206 | mObject = nullptr; |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 211 | class RootRenderNode : public RenderNode, ErrorHandler { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 212 | public: |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 213 | explicit RootRenderNode(JNIEnv* env) : RenderNode() { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 214 | mLooper = Looper::getForThread(); |
| 215 | LOG_ALWAYS_FATAL_IF(!mLooper.get(), |
| 216 | "Must create RootRenderNode on a thread with a looper!"); |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 217 | env->GetJavaVM(&mVm); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | virtual ~RootRenderNode() {} |
| 221 | |
John Reck | 44b49f0 | 2016-03-25 14:29:48 -0700 | [diff] [blame] | 222 | virtual void onError(const std::string& message) override { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 223 | mLooper->sendMessage(new RenderingException(mVm, message), 0); |
| 224 | } |
| 225 | |
John Reck | 44b49f0 | 2016-03-25 14:29:48 -0700 | [diff] [blame] | 226 | virtual void prepareTree(TreeInfo& info) override { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 227 | info.errorHandler = this; |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 228 | |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 229 | for (auto& anim : mRunningVDAnimators) { |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 230 | // Assume that the property change in VD from the animators will not be consumed. Mark |
| 231 | // otherwise if the VDs are found in the display list tree. For VDs that are not in |
| 232 | // the display list tree, we stop providing animation pulses by 1) removing them from |
| 233 | // the animation list, 2) post a delayed message to end them at end time so their |
| 234 | // listeners can receive the corresponding callbacks. |
| 235 | anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false); |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 236 | // Mark the VD dirty so it will damage itself during prepareTree. |
| 237 | anim->getVectorDrawable()->markDirty(); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 238 | } |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 239 | if (info.mode == TreeInfo::MODE_FULL) { |
| 240 | for (auto &anim : mPausedVDAnimators) { |
| 241 | anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false); |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 242 | anim->getVectorDrawable()->markDirty(); |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 245 | // TODO: This is hacky |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 246 | info.updateWindowPositions = true; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 247 | RenderNode::prepareTree(info); |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 248 | info.updateWindowPositions = false; |
John Reck | 44b49f0 | 2016-03-25 14:29:48 -0700 | [diff] [blame] | 249 | info.errorHandler = nullptr; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 250 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 251 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 252 | void sendMessage(const sp<MessageHandler>& handler) { |
| 253 | mLooper->sendMessage(handler, 0); |
| 254 | } |
| 255 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 256 | void sendMessageDelayed(const sp<MessageHandler>& handler, nsecs_t delayInMs) { |
| 257 | mLooper->sendMessageDelayed(ms2ns(delayInMs), handler, 0); |
| 258 | } |
| 259 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 260 | void attachAnimatingNode(RenderNode* animatingNode) { |
| 261 | mPendingAnimatingRenderNodes.push_back(animatingNode); |
| 262 | } |
| 263 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 264 | void attachPendingVectorDrawableAnimators() { |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 265 | mRunningVDAnimators.insert(mPendingVectorDrawableAnimators.begin(), |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 266 | mPendingVectorDrawableAnimators.end()); |
| 267 | mPendingVectorDrawableAnimators.clear(); |
| 268 | } |
| 269 | |
| 270 | void detachAnimators() { |
| 271 | // Remove animators from the list and post a delayed message in future to end the animator |
Doris Liu | 0bfce2c | 2017-05-30 18:52:26 -0700 | [diff] [blame] | 272 | // For infinite animators, remove the listener so we no longer hold a global ref to the AVD |
| 273 | // java object, and therefore the AVD objects in both native and Java can be properly |
| 274 | // released. |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 275 | for (auto& anim : mRunningVDAnimators) { |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 276 | detachVectorDrawableAnimator(anim.get()); |
Doris Liu | 0bfce2c | 2017-05-30 18:52:26 -0700 | [diff] [blame] | 277 | anim->clearOneShotListener(); |
| 278 | } |
| 279 | for (auto& anim : mPausedVDAnimators) { |
| 280 | anim->clearOneShotListener(); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 281 | } |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 282 | mRunningVDAnimators.clear(); |
| 283 | mPausedVDAnimators.clear(); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Doris Liu | c82e879 | 2016-07-29 16:45:24 -0700 | [diff] [blame] | 286 | // Move all the animators to the paused list, and send a delayed message to notify the finished |
| 287 | // listener. |
| 288 | void pauseAnimators() { |
| 289 | mPausedVDAnimators.insert(mRunningVDAnimators.begin(), mRunningVDAnimators.end()); |
| 290 | for (auto& anim : mRunningVDAnimators) { |
| 291 | detachVectorDrawableAnimator(anim.get()); |
| 292 | } |
| 293 | mRunningVDAnimators.clear(); |
| 294 | } |
| 295 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 296 | void doAttachAnimatingNodes(AnimationContext* context) { |
| 297 | for (size_t i = 0; i < mPendingAnimatingRenderNodes.size(); i++) { |
| 298 | RenderNode* node = mPendingAnimatingRenderNodes[i].get(); |
| 299 | context->addAnimatingRenderNode(*node); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 300 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 301 | mPendingAnimatingRenderNodes.clear(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 304 | // Run VectorDrawable animators after prepareTree. |
| 305 | void runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info) { |
| 306 | // Push staging. |
| 307 | if (info.mode == TreeInfo::MODE_FULL) { |
| 308 | pushStagingVectorDrawableAnimators(context); |
| 309 | } |
| 310 | |
| 311 | // Run the animators in the running list. |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 312 | for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) { |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 313 | if ((*it)->animate(*context)) { |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 314 | it = mRunningVDAnimators.erase(it); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 315 | } else { |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 316 | it++; |
| 317 | } |
| 318 | } |
| 319 | |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 320 | // Run the animators in paused list during full sync. |
| 321 | if (info.mode == TreeInfo::MODE_FULL) { |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 322 | // During full sync we also need to pulse paused animators, in case their targets |
| 323 | // have been added back to the display list. All the animators that passed the |
| 324 | // scheduled finish time will be removed from the paused list. |
| 325 | for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) { |
| 326 | if ((*it)->animate(*context)) { |
| 327 | // Animator has finished, remove from the list. |
| 328 | it = mPausedVDAnimators.erase(it); |
| 329 | } else { |
| 330 | it++; |
| 331 | } |
| 332 | } |
| 333 | } |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 334 | |
| 335 | // Move the animators with a target not in DisplayList to paused list. |
| 336 | for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) { |
| 337 | if (!(*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) { |
| 338 | // Vector Drawable is not in the display list, we should remove this animator from |
| 339 | // the list, put it in the paused list, and post a delayed message to end the |
| 340 | // animator. |
| 341 | detachVectorDrawableAnimator(it->get()); |
| 342 | mPausedVDAnimators.insert(*it); |
| 343 | it = mRunningVDAnimators.erase(it); |
| 344 | } else { |
| 345 | it++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Move the animators with a target in DisplayList from paused list to running list, and |
| 350 | // trim paused list. |
| 351 | if (info.mode == TreeInfo::MODE_FULL) { |
| 352 | // Check whether any paused animator's target is back in Display List. If so, put the |
| 353 | // animator back in the running list. |
| 354 | for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) { |
| 355 | if ((*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) { |
| 356 | mRunningVDAnimators.insert(*it); |
| 357 | it = mPausedVDAnimators.erase(it); |
| 358 | } else { |
| 359 | it++; |
| 360 | } |
| 361 | } |
| 362 | // Trim paused VD animators at full sync, so that when Java loses reference to an |
| 363 | // animator, we know we won't be requested to animate it any more, then we remove such |
| 364 | // animators from the paused list so they can be properly freed. We also remove the |
| 365 | // animators from paused list when the time elapsed since start has exceeded duration. |
| 366 | trimPausedVDAnimators(context); |
| 367 | } |
| 368 | |
| 369 | info.out.hasAnimations |= !mRunningVDAnimators.empty(); |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void trimPausedVDAnimators(AnimationContext* context) { |
| 373 | // Trim paused vector drawable animator list. |
| 374 | for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) { |
| 375 | // Remove paused VD animator if no one else is referencing it. Note that animators that |
| 376 | // have passed scheduled finish time are removed from list when they are being pulsed |
| 377 | // before prepare tree. |
| 378 | // TODO: this is a bit hacky, need to figure out a better way to track when the paused |
| 379 | // animators should be freed. |
| 380 | if ((*it)->getStrongCount() == 1) { |
| 381 | it = mPausedVDAnimators.erase(it); |
| 382 | } else { |
| 383 | it++; |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Doris Liu | 7f2bf46 | 2016-06-21 16:06:06 -0700 | [diff] [blame] | 388 | void pushStagingVectorDrawableAnimators(AnimationContext* context) { |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 389 | for (auto& anim : mRunningVDAnimators) { |
Doris Liu | 7f2bf46 | 2016-06-21 16:06:06 -0700 | [diff] [blame] | 390 | anim->pushStaging(*context); |
| 391 | } |
| 392 | } |
| 393 | |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 394 | void destroy() { |
| 395 | for (auto& renderNode : mPendingAnimatingRenderNodes) { |
| 396 | renderNode->animators().endAllStagingAnimators(); |
| 397 | } |
| 398 | mPendingAnimatingRenderNodes.clear(); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 399 | mPendingVectorDrawableAnimators.clear(); |
| 400 | } |
| 401 | |
| 402 | void addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) { |
| 403 | mPendingVectorDrawableAnimators.insert(anim); |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 404 | } |
| 405 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 406 | private: |
| 407 | sp<Looper> mLooper; |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 408 | JavaVM* mVm; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 409 | std::vector< sp<RenderNode> > mPendingAnimatingRenderNodes; |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 410 | std::set< sp<PropertyValuesAnimatorSet> > mPendingVectorDrawableAnimators; |
Doris Liu | 3bb5ddd | 2016-07-19 19:04:31 -0700 | [diff] [blame] | 411 | std::set< sp<PropertyValuesAnimatorSet> > mRunningVDAnimators; |
| 412 | // mPausedVDAnimators stores a list of animators that have not yet passed the finish time, but |
| 413 | // their VectorDrawable targets are no longer in the DisplayList. We skip these animators when |
| 414 | // render thread runs animators independent of UI thread (i.e. RT_ONLY mode). These animators |
| 415 | // need to be re-activated once their VD target is added back into DisplayList. Since that could |
| 416 | // only happen when we do a full sync, we need to make sure to pulse these paused animators at |
| 417 | // full sync. If any animator's VD target is found in DisplayList during a full sync, we move |
| 418 | // the animator back to the running list. |
| 419 | std::set< sp<PropertyValuesAnimatorSet> > mPausedVDAnimators; |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 420 | void detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) { |
| 421 | if (anim->isInfinite() || !anim->isRunning()) { |
| 422 | // Do not need to post anything if the animation is infinite (i.e. no meaningful |
| 423 | // end listener action), or if the animation has already ended. |
| 424 | return; |
| 425 | } |
| 426 | nsecs_t remainingTimeInMs = anim->getRemainingPlayTime(); |
| 427 | // Post a delayed onFinished event that is scheduled to be handled when the animator ends. |
| 428 | if (anim->getOneShotListener()) { |
| 429 | // VectorDrawable's oneshot listener is updated when there are user triggered animation |
| 430 | // lifecycle changes, such as start(), end(), etc. By using checking and clearing |
| 431 | // one shot listener, we ensure the same end listener event gets posted only once. |
| 432 | // Therefore no duplicates. Another benefit of using one shot listener is that no |
| 433 | // removal is necessary: the end time of animation will not change unless triggered by |
| 434 | // user events, in which case the already posted listener's id will become stale, and |
| 435 | // the onFinished callback will then be ignored. |
| 436 | sp<FinishAndInvokeListener> message |
| 437 | = new FinishAndInvokeListener(anim); |
| 438 | sendMessageDelayed(message, remainingTimeInMs); |
| 439 | anim->clearOneShotListener(); |
| 440 | } |
| 441 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | class AnimationContextBridge : public AnimationContext { |
| 445 | public: |
| 446 | AnimationContextBridge(renderthread::TimeLord& clock, RootRenderNode* rootNode) |
| 447 | : AnimationContext(clock), mRootNode(rootNode) { |
| 448 | } |
| 449 | |
| 450 | virtual ~AnimationContextBridge() {} |
| 451 | |
| 452 | // Marks the start of a frame, which will update the frame time and move all |
| 453 | // next frame animations into the current frame |
John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 454 | virtual void startFrame(TreeInfo::TraversalMode mode) { |
| 455 | if (mode == TreeInfo::MODE_FULL) { |
| 456 | mRootNode->doAttachAnimatingNodes(this); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 457 | mRootNode->attachPendingVectorDrawableAnimators(); |
John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 458 | } |
| 459 | AnimationContext::startFrame(mode); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Runs any animations still left in mCurrentFrameAnimations |
| 463 | virtual void runRemainingAnimations(TreeInfo& info) { |
| 464 | AnimationContext::runRemainingAnimations(info); |
Doris Liu | 7c7052d | 2016-07-25 17:19:24 -0700 | [diff] [blame] | 465 | mRootNode->runVectorDrawableAnimators(this, info); |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 466 | postOnFinishedEvents(); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Doris Liu | c82e879 | 2016-07-29 16:45:24 -0700 | [diff] [blame] | 469 | virtual void pauseAnimators() override { |
| 470 | mRootNode->pauseAnimators(); |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 471 | } |
| 472 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 473 | virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) { |
| 474 | OnFinishedEvent event(animator, listener); |
| 475 | mOnFinishedEvents.push_back(event); |
| 476 | } |
| 477 | |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 478 | virtual void destroy() { |
| 479 | AnimationContext::destroy(); |
Doris Liu | c82e879 | 2016-07-29 16:45:24 -0700 | [diff] [blame] | 480 | mRootNode->detachAnimators(); |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 481 | postOnFinishedEvents(); |
| 482 | } |
| 483 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 484 | private: |
| 485 | sp<RootRenderNode> mRootNode; |
| 486 | std::vector<OnFinishedEvent> mOnFinishedEvents; |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 487 | |
| 488 | void postOnFinishedEvents() { |
| 489 | if (mOnFinishedEvents.size()) { |
| 490 | sp<InvokeAnimationListeners> message |
| 491 | = new InvokeAnimationListeners(mOnFinishedEvents); |
| 492 | mRootNode->sendMessage(message); |
| 493 | } |
| 494 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | class ContextFactoryImpl : public IContextFactory { |
| 498 | public: |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 499 | explicit ContextFactoryImpl(RootRenderNode* rootNode) : mRootNode(rootNode) {} |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 500 | |
| 501 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) { |
| 502 | return new AnimationContextBridge(clock, mRootNode); |
| 503 | } |
| 504 | |
| 505 | private: |
| 506 | RootRenderNode* mRootNode; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 509 | class ObserverProxy; |
| 510 | |
| 511 | class NotifyHandler : public MessageHandler { |
| 512 | public: |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 513 | NotifyHandler(JavaVM* vm, ObserverProxy* observer) : mVm(vm), mObserver(observer) {} |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 514 | |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 515 | virtual void handleMessage(const Message& message); |
| 516 | |
| 517 | private: |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 518 | JavaVM* const mVm; |
| 519 | ObserverProxy* const mObserver; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 520 | }; |
| 521 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 522 | static jlongArray get_metrics_buffer(JNIEnv* env, jobject observer) { |
| 523 | jobject frameMetrics = env->GetObjectField( |
| 524 | observer, gFrameMetricsObserverClassInfo.frameMetrics); |
| 525 | LOG_ALWAYS_FATAL_IF(frameMetrics == nullptr, "unable to retrieve data sink object"); |
| 526 | jobject buffer = env->GetObjectField( |
| 527 | frameMetrics, gFrameMetricsObserverClassInfo.timingDataBuffer); |
| 528 | LOG_ALWAYS_FATAL_IF(buffer == nullptr, "unable to retrieve data sink buffer"); |
| 529 | return reinterpret_cast<jlongArray>(buffer); |
| 530 | } |
| 531 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 532 | /* |
| 533 | * Implements JNI layer for hwui frame metrics reporting. |
| 534 | */ |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 535 | class ObserverProxy : public FrameMetricsObserver { |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 536 | public: |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 537 | ObserverProxy(JavaVM *vm, jobject observer) : mVm(vm) { |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 538 | JNIEnv* env = getenv(mVm); |
| 539 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 540 | mObserverWeak = env->NewWeakGlobalRef(observer); |
| 541 | LOG_ALWAYS_FATAL_IF(mObserverWeak == nullptr, |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 542 | "unable to create frame stats observer reference"); |
| 543 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 544 | jlongArray buffer = get_metrics_buffer(env, observer); |
| 545 | jsize bufferSize = env->GetArrayLength(reinterpret_cast<jarray>(buffer)); |
| 546 | LOG_ALWAYS_FATAL_IF(bufferSize != kBufferSize, |
| 547 | "Mismatched Java/Native FrameMetrics data format."); |
| 548 | |
| 549 | jobject messageQueueLocal = env->GetObjectField( |
| 550 | observer, gFrameMetricsObserverClassInfo.messageQueue); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 551 | mMessageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueLocal); |
| 552 | LOG_ALWAYS_FATAL_IF(mMessageQueue == nullptr, "message queue not available"); |
| 553 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 554 | mMessageHandler = new NotifyHandler(mVm, this); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 555 | LOG_ALWAYS_FATAL_IF(mMessageHandler == nullptr, |
| 556 | "OOM: unable to allocate NotifyHandler"); |
| 557 | } |
| 558 | |
| 559 | ~ObserverProxy() { |
| 560 | JNIEnv* env = getenv(mVm); |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 561 | env->DeleteWeakGlobalRef(mObserverWeak); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 564 | jweak getObserverReference() { |
| 565 | return mObserverWeak; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 568 | bool getNextBuffer(JNIEnv* env, jlongArray sink, int* dropCount) { |
| 569 | FrameMetricsNotification& elem = mRingBuffer[mNextInQueue]; |
| 570 | |
| 571 | if (elem.hasData.load()) { |
| 572 | env->SetLongArrayRegion(sink, 0, kBufferSize, elem.buffer); |
| 573 | *dropCount = elem.dropCount; |
| 574 | mNextInQueue = (mNextInQueue + 1) % kRingSize; |
| 575 | elem.hasData = false; |
| 576 | return true; |
| 577 | } |
| 578 | |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | virtual void notify(const int64_t* stats) { |
| 583 | FrameMetricsNotification& elem = mRingBuffer[mNextFree]; |
| 584 | |
| 585 | if (!elem.hasData.load()) { |
| 586 | memcpy(elem.buffer, stats, kBufferSize * sizeof(stats[0])); |
| 587 | |
| 588 | elem.dropCount = mDroppedReports; |
| 589 | mDroppedReports = 0; |
| 590 | |
| 591 | incStrong(nullptr); |
| 592 | mNextFree = (mNextFree + 1) % kRingSize; |
| 593 | elem.hasData = true; |
| 594 | |
| 595 | mMessageQueue->getLooper()->sendMessage(mMessageHandler, mMessage); |
| 596 | } else { |
| 597 | mDroppedReports++; |
| 598 | } |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | private: |
| 602 | static const int kBufferSize = static_cast<int>(FrameInfoIndex::NumIndexes); |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 603 | static constexpr int kRingSize = 3; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 604 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 605 | class FrameMetricsNotification { |
| 606 | public: |
| 607 | FrameMetricsNotification() : hasData(false) {} |
| 608 | |
| 609 | std::atomic_bool hasData; |
| 610 | int64_t buffer[kBufferSize]; |
| 611 | int dropCount = 0; |
| 612 | }; |
| 613 | |
| 614 | JavaVM* const mVm; |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 615 | jweak mObserverWeak; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 616 | |
| 617 | sp<MessageQueue> mMessageQueue; |
| 618 | sp<NotifyHandler> mMessageHandler; |
| 619 | Message mMessage; |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 620 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 621 | int mNextFree = 0; |
| 622 | int mNextInQueue = 0; |
| 623 | FrameMetricsNotification mRingBuffer[kRingSize]; |
| 624 | |
| 625 | int mDroppedReports = 0; |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | void NotifyHandler::handleMessage(const Message& message) { |
| 629 | JNIEnv* env = getenv(mVm); |
| 630 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 631 | jobject target = env->NewLocalRef(mObserver->getObserverReference()); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 632 | |
| 633 | if (target != nullptr) { |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 634 | jlongArray javaBuffer = get_metrics_buffer(env, target); |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 635 | int dropCount = 0; |
| 636 | while (mObserver->getNextBuffer(env, javaBuffer, &dropCount)) { |
| 637 | env->CallVoidMethod(target, gFrameMetricsObserverClassInfo.callback, dropCount); |
| 638 | } |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 639 | env->DeleteLocalRef(target); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Andres Morales | 11f02d7 | 2016-02-12 18:19:52 -0800 | [diff] [blame] | 642 | mObserver->decStrong(nullptr); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 643 | } |
| 644 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 645 | static void android_view_ThreadedRenderer_rotateProcessStatsBuffer(JNIEnv* env, jobject clazz) { |
| 646 | RenderProxy::rotateProcessStatsBuffer(); |
| 647 | } |
| 648 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 649 | static void android_view_ThreadedRenderer_setProcessStatsBuffer(JNIEnv* env, jobject clazz, |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 650 | jint fd) { |
| 651 | RenderProxy::setProcessStatsBuffer(fd); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 654 | static jint android_view_ThreadedRenderer_getRenderThreadTid(JNIEnv* env, jobject clazz, |
| 655 | jlong proxyPtr) { |
| 656 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 657 | return proxy->getRenderThreadTid(); |
| 658 | } |
| 659 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 660 | static jlong android_view_ThreadedRenderer_createRootRenderNode(JNIEnv* env, jobject clazz) { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 661 | RootRenderNode* node = new RootRenderNode(env); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 662 | node->incStrong(0); |
| 663 | node->setName("RootRenderNode"); |
| 664 | return reinterpret_cast<jlong>(node); |
| 665 | } |
| 666 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 667 | static jlong android_view_ThreadedRenderer_createProxy(JNIEnv* env, jobject clazz, |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 668 | jboolean translucent, jlong rootRenderNodePtr) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 669 | RootRenderNode* rootRenderNode = reinterpret_cast<RootRenderNode*>(rootRenderNodePtr); |
| 670 | ContextFactoryImpl factory(rootRenderNode); |
| 671 | return (jlong) new RenderProxy(translucent, rootRenderNode, &factory); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static void android_view_ThreadedRenderer_deleteProxy(JNIEnv* env, jobject clazz, |
| 675 | jlong proxyPtr) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 676 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 677 | delete proxy; |
| 678 | } |
| 679 | |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 680 | static jboolean android_view_ThreadedRenderer_loadSystemProperties(JNIEnv* env, jobject clazz, |
| 681 | jlong proxyPtr) { |
| 682 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 683 | return proxy->loadSystemProperties(); |
| 684 | } |
| 685 | |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 686 | static void android_view_ThreadedRenderer_setName(JNIEnv* env, jobject clazz, |
| 687 | jlong proxyPtr, jstring jname) { |
| 688 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 689 | const char* name = env->GetStringUTFChars(jname, NULL); |
| 690 | proxy->setName(name); |
| 691 | env->ReleaseStringUTFChars(jname, name); |
| 692 | } |
| 693 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 694 | static void android_view_ThreadedRenderer_setSurface(JNIEnv* env, jobject clazz, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 695 | jlong proxyPtr, jobject jsurface) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 696 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 697 | sp<Surface> surface; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 698 | if (jsurface) { |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 699 | surface = android_view_Surface_getSurface(env, jsurface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 700 | } |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 701 | proxy->setSurface(surface); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 702 | } |
| 703 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 704 | static jboolean android_view_ThreadedRenderer_pause(JNIEnv* env, jobject clazz, |
| 705 | jlong proxyPtr) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 706 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 707 | return proxy->pause(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 708 | } |
| 709 | |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 710 | static void android_view_ThreadedRenderer_setStopped(JNIEnv* env, jobject clazz, |
| 711 | jlong proxyPtr, jboolean stopped) { |
| 712 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 713 | proxy->setStopped(stopped); |
| 714 | } |
| 715 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 716 | static void android_view_ThreadedRenderer_setLightAlpha(JNIEnv* env, jobject clazz, jlong proxyPtr, |
| 717 | jfloat ambientShadowAlpha, jfloat spotShadowAlpha) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 718 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 719 | proxy->setLightAlpha((uint8_t) (255 * ambientShadowAlpha), (uint8_t) (255 * spotShadowAlpha)); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 722 | static void android_view_ThreadedRenderer_setLightGeometry(JNIEnv* env, jobject clazz, |
| 723 | jlong proxyPtr, jfloat lightX, jfloat lightY, jfloat lightZ, jfloat lightRadius) { |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 724 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 725 | proxy->setLightGeometry((Vector3){lightX, lightY, lightZ}, lightRadius); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 726 | } |
| 727 | |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 728 | static void android_view_ThreadedRenderer_setOpaque(JNIEnv* env, jobject clazz, |
| 729 | jlong proxyPtr, jboolean opaque) { |
| 730 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 731 | proxy->setOpaque(opaque); |
| 732 | } |
| 733 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 734 | static void android_view_ThreadedRenderer_setWideGamut(JNIEnv* env, jobject clazz, |
| 735 | jlong proxyPtr, jboolean wideGamut) { |
| 736 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 737 | proxy->setWideGamut(wideGamut); |
| 738 | } |
| 739 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 740 | static int android_view_ThreadedRenderer_syncAndDrawFrame(JNIEnv* env, jobject clazz, |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 741 | jlong proxyPtr, jlongArray frameInfo, jint frameInfoSize) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 742 | LOG_ALWAYS_FATAL_IF(frameInfoSize != UI_THREAD_FRAME_INFO_SIZE, |
| 743 | "Mismatched size expectations, given %d expected %d", |
| 744 | frameInfoSize, UI_THREAD_FRAME_INFO_SIZE); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 745 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 746 | env->GetLongArrayRegion(frameInfo, 0, frameInfoSize, proxy->frameInfo()); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 747 | return proxy->syncAndDrawFrame(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 748 | } |
| 749 | |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 750 | static void android_view_ThreadedRenderer_destroy(JNIEnv* env, jobject clazz, |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 751 | jlong proxyPtr, jlong rootNodePtr) { |
| 752 | RootRenderNode* rootRenderNode = reinterpret_cast<RootRenderNode*>(rootNodePtr); |
| 753 | rootRenderNode->destroy(); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 754 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 755 | proxy->destroy(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 756 | } |
| 757 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 758 | static void android_view_ThreadedRenderer_registerAnimatingRenderNode(JNIEnv* env, jobject clazz, |
| 759 | jlong rootNodePtr, jlong animatingNodePtr) { |
| 760 | RootRenderNode* rootRenderNode = reinterpret_cast<RootRenderNode*>(rootNodePtr); |
| 761 | RenderNode* animatingNode = reinterpret_cast<RenderNode*>(animatingNodePtr); |
| 762 | rootRenderNode->attachAnimatingNode(animatingNode); |
| 763 | } |
| 764 | |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 765 | static void android_view_ThreadedRenderer_registerVectorDrawableAnimator(JNIEnv* env, jobject clazz, |
| 766 | jlong rootNodePtr, jlong animatorPtr) { |
| 767 | RootRenderNode* rootRenderNode = reinterpret_cast<RootRenderNode*>(rootNodePtr); |
| 768 | PropertyValuesAnimatorSet* animator = reinterpret_cast<PropertyValuesAnimatorSet*>(animatorPtr); |
| 769 | rootRenderNode->addVectorDrawableAnimator(animator); |
| 770 | } |
| 771 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 772 | static void android_view_ThreadedRenderer_invokeFunctor(JNIEnv* env, jobject clazz, |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 773 | jlong functorPtr, jboolean waitForCompletion) { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 774 | Functor* functor = reinterpret_cast<Functor*>(functorPtr); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 775 | RenderProxy::invokeFunctor(functor, waitForCompletion); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 776 | } |
| 777 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 778 | static jlong android_view_ThreadedRenderer_createTextureLayer(JNIEnv* env, jobject clazz, |
| 779 | jlong proxyPtr) { |
| 780 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 781 | DeferredLayerUpdater* layer = proxy->createTextureLayer(); |
| 782 | return reinterpret_cast<jlong>(layer); |
| 783 | } |
| 784 | |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 785 | static void android_view_ThreadedRenderer_buildLayer(JNIEnv* env, jobject clazz, |
| 786 | jlong proxyPtr, jlong nodePtr) { |
| 787 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 788 | RenderNode* node = reinterpret_cast<RenderNode*>(nodePtr); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 789 | proxy->buildLayer(node); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 790 | } |
| 791 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 792 | static jboolean android_view_ThreadedRenderer_copyLayerInto(JNIEnv* env, jobject clazz, |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 793 | jlong proxyPtr, jlong layerPtr, jobject jbitmap) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 794 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 795 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 796 | SkBitmap bitmap; |
| 797 | GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 798 | return proxy->copyLayerInto(layer, bitmap); |
| 799 | } |
| 800 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 801 | static void android_view_ThreadedRenderer_pushLayerUpdate(JNIEnv* env, jobject clazz, |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 802 | jlong proxyPtr, jlong layerPtr) { |
| 803 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 804 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 805 | proxy->pushLayerUpdate(layer); |
| 806 | } |
| 807 | |
| 808 | static void android_view_ThreadedRenderer_cancelLayerUpdate(JNIEnv* env, jobject clazz, |
| 809 | jlong proxyPtr, jlong layerPtr) { |
| 810 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 811 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
| 812 | proxy->cancelLayerUpdate(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 813 | } |
| 814 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 815 | static void android_view_ThreadedRenderer_detachSurfaceTexture(JNIEnv* env, jobject clazz, |
| 816 | jlong proxyPtr, jlong layerPtr) { |
| 817 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 818 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
| 819 | proxy->detachSurfaceTexture(layer); |
| 820 | } |
| 821 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 822 | static void android_view_ThreadedRenderer_destroyHardwareResources(JNIEnv* env, jobject clazz, |
| 823 | jlong proxyPtr) { |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 824 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 825 | proxy->destroyHardwareResources(); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static void android_view_ThreadedRenderer_trimMemory(JNIEnv* env, jobject clazz, |
| 829 | jint level) { |
| 830 | RenderProxy::trimMemory(level); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 833 | static void android_view_ThreadedRenderer_overrideProperty(JNIEnv* env, jobject clazz, |
| 834 | jstring name, jstring value) { |
| 835 | const char* nameCharArray = env->GetStringUTFChars(name, NULL); |
| 836 | const char* valueCharArray = env->GetStringUTFChars(value, NULL); |
| 837 | RenderProxy::overrideProperty(nameCharArray, valueCharArray); |
| 838 | env->ReleaseStringUTFChars(name, nameCharArray); |
| 839 | env->ReleaseStringUTFChars(name, valueCharArray); |
| 840 | } |
| 841 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 842 | static void android_view_ThreadedRenderer_fence(JNIEnv* env, jobject clazz, |
| 843 | jlong proxyPtr) { |
| 844 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 845 | proxy->fence(); |
| 846 | } |
| 847 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 848 | static void android_view_ThreadedRenderer_stopDrawing(JNIEnv* env, jobject clazz, |
| 849 | jlong proxyPtr) { |
| 850 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 851 | proxy->stopDrawing(); |
| 852 | } |
| 853 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 854 | static void android_view_ThreadedRenderer_notifyFramePending(JNIEnv* env, jobject clazz, |
| 855 | jlong proxyPtr) { |
| 856 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 857 | proxy->notifyFramePending(); |
| 858 | } |
| 859 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 860 | static void android_view_ThreadedRenderer_dumpProfileInfo(JNIEnv* env, jobject clazz, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 861 | jlong proxyPtr, jobject javaFileDescriptor, jint dumpFlags) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 862 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 863 | int fd = jniGetFDFromFileDescriptor(env, javaFileDescriptor); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 864 | proxy->dumpProfileInfo(fd, dumpFlags); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 867 | static void android_view_ThreadedRenderer_addRenderNode(JNIEnv* env, jobject clazz, |
| 868 | jlong proxyPtr, jlong renderNodePtr, jboolean placeFront) { |
| 869 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 870 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 871 | proxy->addRenderNode(renderNode, placeFront); |
| 872 | } |
| 873 | |
| 874 | static void android_view_ThreadedRenderer_removeRenderNode(JNIEnv* env, jobject clazz, |
| 875 | jlong proxyPtr, jlong renderNodePtr) { |
| 876 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 877 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 878 | proxy->removeRenderNode(renderNode); |
| 879 | } |
| 880 | |
| 881 | static void android_view_ThreadedRendererd_drawRenderNode(JNIEnv* env, jobject clazz, |
| 882 | jlong proxyPtr, jlong renderNodePtr) { |
| 883 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 884 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 885 | proxy->drawRenderNode(renderNode); |
| 886 | } |
| 887 | |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 888 | static void android_view_ThreadedRenderer_setContentDrawBounds(JNIEnv* env, |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 889 | jobject clazz, jlong proxyPtr, jint left, jint top, jint right, jint bottom) { |
| 890 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 891 | proxy->setContentDrawBounds(left, top, right, bottom); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 892 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 893 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 894 | class JGlobalRefHolder { |
| 895 | public: |
| 896 | JGlobalRefHolder(JavaVM* vm, jobject object) : mVm(vm), mObject(object) {} |
| 897 | |
| 898 | virtual ~JGlobalRefHolder() { |
| 899 | getenv(mVm)->DeleteGlobalRef(mObject); |
| 900 | mObject = nullptr; |
| 901 | } |
| 902 | |
| 903 | jobject object() { return mObject; } |
| 904 | JavaVM* vm() { return mVm; } |
| 905 | |
| 906 | private: |
| 907 | JGlobalRefHolder(const JGlobalRefHolder&) = delete; |
| 908 | void operator=(const JGlobalRefHolder&) = delete; |
| 909 | |
| 910 | JavaVM* mVm; |
| 911 | jobject mObject; |
| 912 | }; |
| 913 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 914 | static void android_view_ThreadedRenderer_setPictureCapturedCallbackJNI(JNIEnv* env, |
| 915 | jobject clazz, jlong proxyPtr, jobject pictureCallback) { |
| 916 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 917 | if (!pictureCallback) { |
| 918 | proxy->setPictureCapturedCallback(nullptr); |
| 919 | } else { |
| 920 | JavaVM* vm = nullptr; |
| 921 | LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Unable to get Java VM"); |
| 922 | auto globalCallbackRef = std::make_shared<JGlobalRefHolder>(vm, |
| 923 | env->NewGlobalRef(pictureCallback)); |
| 924 | proxy->setPictureCapturedCallback([globalCallbackRef](sk_sp<SkPicture>&& picture) { |
| 925 | JNIEnv* env = getenv(globalCallbackRef->vm()); |
| 926 | Picture* wrapper = new Picture{std::move(picture)}; |
| 927 | env->CallStaticVoidMethod(gHardwareRenderer.clazz, |
| 928 | gHardwareRenderer.invokePictureCapturedCallback, |
| 929 | static_cast<jlong>(reinterpret_cast<intptr_t>(wrapper)), |
| 930 | globalCallbackRef->object()); |
| 931 | }); |
| 932 | } |
| 933 | } |
| 934 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 935 | static void android_view_ThreadedRenderer_setFrameCallback(JNIEnv* env, |
| 936 | jobject clazz, jlong proxyPtr, jobject frameCallback) { |
| 937 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 938 | if (!frameCallback) { |
| 939 | proxy->setFrameCallback(nullptr); |
| 940 | } else { |
| 941 | JavaVM* vm = nullptr; |
| 942 | LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Unable to get Java VM"); |
| 943 | auto globalCallbackRef = std::make_shared<JGlobalRefHolder>(vm, |
| 944 | env->NewGlobalRef(frameCallback)); |
| 945 | proxy->setFrameCallback([globalCallbackRef](int64_t frameNr) { |
| 946 | JNIEnv* env = getenv(globalCallbackRef->vm()); |
| 947 | env->CallVoidMethod(globalCallbackRef->object(), gFrameDrawingCallback.onFrameDraw, |
| 948 | static_cast<jlong>(frameNr)); |
| 949 | }); |
| 950 | } |
| 951 | } |
| 952 | |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 953 | static void android_view_ThreadedRenderer_setFrameCompleteCallback(JNIEnv* env, |
| 954 | jobject clazz, jlong proxyPtr, jobject callback) { |
| 955 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 956 | if (!callback) { |
| 957 | proxy->setFrameCompleteCallback(nullptr); |
| 958 | } else { |
| 959 | sp<FrameCompleteWrapper> wrapper = new FrameCompleteWrapper{env, callback}; |
| 960 | proxy->setFrameCompleteCallback([wrapper](int64_t frameNr) { |
| 961 | wrapper->postFrameComplete(frameNr); |
| 962 | }); |
| 963 | } |
| 964 | } |
| 965 | |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 966 | static jint android_view_ThreadedRenderer_copySurfaceInto(JNIEnv* env, |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 967 | jobject clazz, jobject jsurface, jint left, jint top, |
| 968 | jint right, jint bottom, jobject jbitmap) { |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 969 | SkBitmap bitmap; |
| 970 | GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap); |
| 971 | sp<Surface> surface = android_view_Surface_getSurface(env, jsurface); |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 972 | return RenderProxy::copySurfaceInto(surface, left, top, right, bottom, &bitmap); |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 973 | } |
| 974 | |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 975 | class ContextFactory : public IContextFactory { |
| 976 | public: |
| 977 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) { |
| 978 | return new AnimationContext(clock); |
| 979 | } |
| 980 | }; |
| 981 | |
| 982 | static jobject android_view_ThreadedRenderer_createHardwareBitmapFromRenderNode(JNIEnv* env, |
| 983 | jobject clazz, jlong renderNodePtr, jint jwidth, jint jheight) { |
| 984 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 985 | if (jwidth <= 0 || jheight <= 0) { |
| 986 | ALOGW("Invalid width %d or height %d", jwidth, jheight); |
| 987 | return nullptr; |
| 988 | } |
| 989 | |
| 990 | uint32_t width = jwidth; |
| 991 | uint32_t height = jheight; |
| 992 | |
| 993 | // Create a Surface wired up to a BufferItemConsumer |
| 994 | sp<IGraphicBufferProducer> producer; |
| 995 | sp<IGraphicBufferConsumer> rawConsumer; |
| 996 | BufferQueue::createBufferQueue(&producer, &rawConsumer); |
John Reck | a67b62e | 2017-06-01 12:44:58 -0700 | [diff] [blame] | 997 | // We only need 1 buffer but some drivers have bugs so workaround it by setting max count to 2 |
| 998 | rawConsumer->setMaxBufferCount(2); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 999 | sp<BufferItemConsumer> consumer = new BufferItemConsumer(rawConsumer, |
| 1000 | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER); |
| 1001 | consumer->setDefaultBufferSize(width, height); |
| 1002 | sp<Surface> surface = new Surface(producer); |
| 1003 | |
| 1004 | // Render into the surface |
| 1005 | { |
| 1006 | ContextFactory factory; |
John Reck | 226fcca | 2017-09-05 11:24:59 -0700 | [diff] [blame] | 1007 | RenderProxy proxy{true, renderNode, &factory}; |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1008 | proxy.setSwapBehavior(SwapBehavior::kSwap_discardBuffer); |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1009 | proxy.setSurface(surface); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1010 | // Shadows can't be used via this interface, so just set the light source |
| 1011 | // to all 0s. |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1012 | proxy.setLightAlpha(0, 0); |
| 1013 | proxy.setLightGeometry((Vector3){0, 0, 0}, 0); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1014 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
| 1015 | UiFrameInfoBuilder(proxy.frameInfo()) |
| 1016 | .setVsync(vsync, vsync) |
| 1017 | .addFlag(FrameInfoFlags::SurfaceCanvas); |
| 1018 | proxy.syncAndDrawFrame(); |
| 1019 | } |
| 1020 | |
| 1021 | // Yank out the GraphicBuffer |
| 1022 | BufferItem bufferItem; |
| 1023 | status_t err; |
| 1024 | if ((err = consumer->acquireBuffer(&bufferItem, 0, true)) != OK) { |
| 1025 | ALOGW("Failed to acquireBuffer, error %d (%s)", err, strerror(-err)); |
| 1026 | return nullptr; |
| 1027 | } |
| 1028 | sp<GraphicBuffer> buffer = bufferItem.mGraphicBuffer; |
| 1029 | // We don't really care if this fails or not since we're just going to destroy this anyway |
| 1030 | consumer->releaseBuffer(bufferItem); |
| 1031 | if (!buffer.get()) { |
| 1032 | ALOGW("GraphicBuffer is null?"); |
| 1033 | return nullptr; |
| 1034 | } |
| 1035 | if (buffer->getWidth() != width || buffer->getHeight() != height) { |
| 1036 | ALOGW("GraphicBuffer size mismatch, got %dx%d expected %dx%d", |
| 1037 | buffer->getWidth(), buffer->getHeight(), width, height); |
| 1038 | // Continue I guess? |
| 1039 | } |
Derek Sollenberger | e216948 | 2018-11-20 10:57:20 -0500 | [diff] [blame] | 1040 | |
Derek Sollenberger | 6e35e63 | 2019-01-22 13:56:25 -0500 | [diff] [blame] | 1041 | SkColorType ct = uirenderer::PixelFormatToColorType(buffer->getPixelFormat()); |
Derek Sollenberger | e216948 | 2018-11-20 10:57:20 -0500 | [diff] [blame] | 1042 | sk_sp<SkColorSpace> cs = uirenderer::DataSpaceToColorSpace(bufferItem.mDataSpace); |
Derek Sollenberger | 6e35e63 | 2019-01-22 13:56:25 -0500 | [diff] [blame] | 1043 | sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer, ct, cs); |
Stan Iliev | c231182 | 2017-08-23 16:25:33 -0400 | [diff] [blame] | 1044 | return bitmap::createBitmap(env, bitmap.release(), |
| 1045 | android::bitmap::kBitmapCreateFlag_Premultiplied); |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 1048 | static void android_view_ThreadedRenderer_disableVsync(JNIEnv*, jclass) { |
| 1049 | RenderProxy::disableVsync(); |
| 1050 | } |
| 1051 | |
John Reck | 938e884 | 2017-08-24 13:41:59 -0700 | [diff] [blame] | 1052 | static void android_view_ThreadedRenderer_setHighContrastText(JNIEnv*, jclass, jboolean enable) { |
| 1053 | Properties::enableHighContrastText = enable; |
| 1054 | } |
| 1055 | |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 1056 | static void android_view_ThreadedRenderer_hackySetRTAnimationsEnabled(JNIEnv*, jclass, |
| 1057 | jboolean enable) { |
| 1058 | Properties::enableRTAnimations = enable; |
| 1059 | } |
| 1060 | |
John Reck | 6afa009 | 2018-03-01 17:28:35 -0800 | [diff] [blame] | 1061 | static void android_view_ThreadedRenderer_setDebuggingEnabled(JNIEnv*, jclass, jboolean enable) { |
| 1062 | Properties::debuggingEnabled = enable; |
| 1063 | } |
| 1064 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 1065 | static void android_view_ThreadedRenderer_setIsolatedProcess(JNIEnv*, jclass, jboolean isolated) { |
| 1066 | Properties::isolatedProcess = isolated; |
| 1067 | } |
| 1068 | |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 1069 | static void android_view_ThreadedRenderer_setContextPriority(JNIEnv*, jclass, |
| 1070 | jint contextPriority) { |
| 1071 | Properties::contextPriority = contextPriority; |
| 1072 | } |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 1073 | |
Jorim Jaggi | 7823ee7 | 2018-07-17 15:24:16 +0200 | [diff] [blame] | 1074 | static void android_view_ThreadedRenderer_allocateBuffers(JNIEnv* env, jobject clazz, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1075 | jlong proxyPtr) { |
Jorim Jaggi | 7823ee7 | 2018-07-17 15:24:16 +0200 | [diff] [blame] | 1076 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1077 | proxy->allocateBuffers(); |
Jorim Jaggi | 7823ee7 | 2018-07-17 15:24:16 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
John Reck | bb3a358 | 2018-09-26 11:21:08 -0700 | [diff] [blame] | 1080 | static void android_view_ThreadedRenderer_setForceDark(JNIEnv* env, jobject clazz, |
| 1081 | jlong proxyPtr, jboolean enable) { |
| 1082 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr); |
| 1083 | proxy->setForceDark(enable); |
| 1084 | } |
| 1085 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1086 | // ---------------------------------------------------------------------------- |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1087 | // FrameMetricsObserver |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1088 | // ---------------------------------------------------------------------------- |
| 1089 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1090 | static jlong android_view_ThreadedRenderer_addFrameMetricsObserver(JNIEnv* env, |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1091 | jclass clazz, jlong proxyPtr, jobject fso) { |
| 1092 | JavaVM* vm = nullptr; |
| 1093 | if (env->GetJavaVM(&vm) != JNI_OK) { |
| 1094 | LOG_ALWAYS_FATAL("Unable to get Java VM"); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | renderthread::RenderProxy* renderProxy = |
| 1099 | reinterpret_cast<renderthread::RenderProxy*>(proxyPtr); |
| 1100 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1101 | FrameMetricsObserver* observer = new ObserverProxy(vm, fso); |
| 1102 | renderProxy->addFrameMetricsObserver(observer); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1103 | return reinterpret_cast<jlong>(observer); |
| 1104 | } |
| 1105 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1106 | static void android_view_ThreadedRenderer_removeFrameMetricsObserver(JNIEnv* env, jclass clazz, |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1107 | jlong proxyPtr, jlong observerPtr) { |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1108 | FrameMetricsObserver* observer = reinterpret_cast<FrameMetricsObserver*>(observerPtr); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1109 | renderthread::RenderProxy* renderProxy = |
| 1110 | reinterpret_cast<renderthread::RenderProxy*>(proxyPtr); |
| 1111 | |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1112 | renderProxy->removeFrameMetricsObserver(observer); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // ---------------------------------------------------------------------------- |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1116 | // Shaders |
| 1117 | // ---------------------------------------------------------------------------- |
| 1118 | |
| 1119 | static void android_view_ThreadedRenderer_setupShadersDiskCache(JNIEnv* env, jobject clazz, |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1120 | jstring diskCachePath, jstring skiaDiskCachePath) { |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1121 | const char* cacheArray = env->GetStringUTFChars(diskCachePath, NULL); |
Mathias Agopian | 04d0461 | 2017-03-08 23:21:54 -0800 | [diff] [blame] | 1122 | android::egl_set_cache_filename(cacheArray); |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1123 | env->ReleaseStringUTFChars(diskCachePath, cacheArray); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1124 | |
| 1125 | const char* skiaCacheArray = env->GetStringUTFChars(skiaDiskCachePath, NULL); |
| 1126 | uirenderer::skiapipeline::ShaderCache::get().setFilename(skiaCacheArray); |
| 1127 | env->ReleaseStringUTFChars(skiaDiskCachePath, skiaCacheArray); |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | // ---------------------------------------------------------------------------- |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1131 | // JNI Glue |
| 1132 | // ---------------------------------------------------------------------------- |
| 1133 | |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1134 | const char* const kClassPathName = "android/graphics/HardwareRenderer"; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1135 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1136 | static const JNINativeMethod gMethods[] = { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 1137 | { "nRotateProcessStatsBuffer", "()V", (void*) android_view_ThreadedRenderer_rotateProcessStatsBuffer }, |
| 1138 | { "nSetProcessStatsBuffer", "(I)V", (void*) android_view_ThreadedRenderer_setProcessStatsBuffer }, |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 1139 | { "nGetRenderThreadTid", "(J)I", (void*) android_view_ThreadedRenderer_getRenderThreadTid }, |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1140 | { "nCreateRootRenderNode", "()J", (void*) android_view_ThreadedRenderer_createRootRenderNode }, |
| 1141 | { "nCreateProxy", "(ZJ)J", (void*) android_view_ThreadedRenderer_createProxy }, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1142 | { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy }, |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 1143 | { "nLoadSystemProperties", "(J)Z", (void*) android_view_ThreadedRenderer_loadSystemProperties }, |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 1144 | { "nSetName", "(JLjava/lang/String;)V", (void*) android_view_ThreadedRenderer_setName }, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1145 | { "nSetSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_setSurface }, |
| 1146 | { "nPause", "(J)Z", (void*) android_view_ThreadedRenderer_pause }, |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 1147 | { "nSetStopped", "(JZ)V", (void*) android_view_ThreadedRenderer_setStopped }, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1148 | { "nSetLightAlpha", "(JFF)V", (void*) android_view_ThreadedRenderer_setLightAlpha }, |
| 1149 | { "nSetLightGeometry", "(JFFFF)V", (void*) android_view_ThreadedRenderer_setLightGeometry }, |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 1150 | { "nSetOpaque", "(JZ)V", (void*) android_view_ThreadedRenderer_setOpaque }, |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 1151 | { "nSetWideGamut", "(JZ)V", (void*) android_view_ThreadedRenderer_setWideGamut }, |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 1152 | { "nSyncAndDrawFrame", "(J[JI)I", (void*) android_view_ThreadedRenderer_syncAndDrawFrame }, |
Doris Liu | 350e652 | 2016-02-19 14:20:37 -0800 | [diff] [blame] | 1153 | { "nDestroy", "(JJ)V", (void*) android_view_ThreadedRenderer_destroy }, |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 1154 | { "nRegisterAnimatingRenderNode", "(JJ)V", (void*) android_view_ThreadedRenderer_registerAnimatingRenderNode }, |
Doris Liu | 67ce99b | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 1155 | { "nRegisterVectorDrawableAnimator", "(JJ)V", (void*) android_view_ThreadedRenderer_registerVectorDrawableAnimator }, |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 1156 | { "nInvokeFunctor", "(JZ)V", (void*) android_view_ThreadedRenderer_invokeFunctor }, |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 1157 | { "nCreateTextureLayer", "(J)J", (void*) android_view_ThreadedRenderer_createTextureLayer }, |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 1158 | { "nBuildLayer", "(JJ)V", (void*) android_view_ThreadedRenderer_buildLayer }, |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 1159 | { "nCopyLayerInto", "(JJLandroid/graphics/Bitmap;)Z", (void*) android_view_ThreadedRenderer_copyLayerInto }, |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 1160 | { "nPushLayerUpdate", "(JJ)V", (void*) android_view_ThreadedRenderer_pushLayerUpdate }, |
| 1161 | { "nCancelLayerUpdate", "(JJ)V", (void*) android_view_ThreadedRenderer_cancelLayerUpdate }, |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 1162 | { "nDetachSurfaceTexture", "(JJ)V", (void*) android_view_ThreadedRenderer_detachSurfaceTexture }, |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 1163 | { "nDestroyHardwareResources", "(J)V", (void*) android_view_ThreadedRenderer_destroyHardwareResources }, |
| 1164 | { "nTrimMemory", "(I)V", (void*) android_view_ThreadedRenderer_trimMemory }, |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 1165 | { "nOverrideProperty", "(Ljava/lang/String;Ljava/lang/String;)V", (void*) android_view_ThreadedRenderer_overrideProperty }, |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 1166 | { "nFence", "(J)V", (void*) android_view_ThreadedRenderer_fence }, |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 1167 | { "nStopDrawing", "(J)V", (void*) android_view_ThreadedRenderer_stopDrawing }, |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 1168 | { "nNotifyFramePending", "(J)V", (void*) android_view_ThreadedRenderer_notifyFramePending }, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 1169 | { "nDumpProfileInfo", "(JLjava/io/FileDescriptor;I)V", (void*) android_view_ThreadedRenderer_dumpProfileInfo }, |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1170 | { "setupShadersDiskCache", "(Ljava/lang/String;Ljava/lang/String;)V", |
John Reck | 84a4c88 | 2014-05-30 14:34:03 -0700 | [diff] [blame] | 1171 | (void*) android_view_ThreadedRenderer_setupShadersDiskCache }, |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 1172 | { "nAddRenderNode", "(JJZ)V", (void*) android_view_ThreadedRenderer_addRenderNode}, |
| 1173 | { "nRemoveRenderNode", "(JJ)V", (void*) android_view_ThreadedRenderer_removeRenderNode}, |
| 1174 | { "nDrawRenderNode", "(JJ)V", (void*) android_view_ThreadedRendererd_drawRenderNode}, |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 1175 | { "nSetContentDrawBounds", "(JIIII)V", (void*)android_view_ThreadedRenderer_setContentDrawBounds}, |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 1176 | { "nSetPictureCaptureCallback", "(JLandroid/graphics/HardwareRenderer$PictureCapturedCallback;)V", |
| 1177 | (void*) android_view_ThreadedRenderer_setPictureCapturedCallbackJNI }, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1178 | { "nSetFrameCallback", "(JLandroid/graphics/HardwareRenderer$FrameDrawingCallback;)V", |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 1179 | (void*)android_view_ThreadedRenderer_setFrameCallback}, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1180 | { "nSetFrameCompleteCallback", "(JLandroid/graphics/HardwareRenderer$FrameCompleteCallback;)V", |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 1181 | (void*)android_view_ThreadedRenderer_setFrameCompleteCallback }, |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1182 | { "nAddFrameMetricsObserver", |
| 1183 | "(JLandroid/view/FrameMetricsObserver;)J", |
| 1184 | (void*)android_view_ThreadedRenderer_addFrameMetricsObserver }, |
| 1185 | { "nRemoveFrameMetricsObserver", |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1186 | "(JJ)V", |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1187 | (void*)android_view_ThreadedRenderer_removeFrameMetricsObserver }, |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 1188 | { "nCopySurfaceInto", "(Landroid/view/Surface;IIIILandroid/graphics/Bitmap;)I", |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 1189 | (void*)android_view_ThreadedRenderer_copySurfaceInto }, |
John Reck | 1fedd91 | 2017-05-23 14:45:22 -0700 | [diff] [blame] | 1190 | { "nCreateHardwareBitmap", "(JII)Landroid/graphics/Bitmap;", |
| 1191 | (void*)android_view_ThreadedRenderer_createHardwareBitmapFromRenderNode }, |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 1192 | { "disableVsync", "()V", (void*)android_view_ThreadedRenderer_disableVsync }, |
John Reck | 938e884 | 2017-08-24 13:41:59 -0700 | [diff] [blame] | 1193 | { "nSetHighContrastText", "(Z)V", (void*)android_view_ThreadedRenderer_setHighContrastText }, |
John Reck | 9f51644 | 2017-09-25 10:27:21 -0700 | [diff] [blame] | 1194 | { "nHackySetRTAnimationsEnabled", "(Z)V", |
| 1195 | (void*)android_view_ThreadedRenderer_hackySetRTAnimationsEnabled }, |
John Reck | 6afa009 | 2018-03-01 17:28:35 -0800 | [diff] [blame] | 1196 | { "nSetDebuggingEnabled", "(Z)V", (void*)android_view_ThreadedRenderer_setDebuggingEnabled }, |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 1197 | { "nSetIsolatedProcess", "(Z)V", (void*)android_view_ThreadedRenderer_setIsolatedProcess }, |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 1198 | { "nSetContextPriority", "(I)V", (void*)android_view_ThreadedRenderer_setContextPriority }, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1199 | { "nAllocateBuffers", "(J)V", (void*)android_view_ThreadedRenderer_allocateBuffers }, |
John Reck | bb3a358 | 2018-09-26 11:21:08 -0700 | [diff] [blame] | 1200 | { "nSetForceDark", "(JZ)V", (void*)android_view_ThreadedRenderer_setForceDark }, |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1201 | }; |
| 1202 | |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 1203 | static JavaVM* mJvm = nullptr; |
| 1204 | |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame^] | 1205 | static void attachRenderThreadToJvm(const char* name) { |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 1206 | LOG_ALWAYS_FATAL_IF(!mJvm, "No jvm but we set the hook??"); |
| 1207 | |
| 1208 | JavaVMAttachArgs args; |
| 1209 | args.version = JNI_VERSION_1_4; |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame^] | 1210 | args.name = name; |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 1211 | args.group = NULL; |
| 1212 | JNIEnv* env; |
| 1213 | mJvm->AttachCurrentThreadAsDaemon(&env, (void*) &args); |
| 1214 | } |
| 1215 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1216 | int register_android_view_ThreadedRenderer(JNIEnv* env) { |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 1217 | env->GetJavaVM(&mJvm); |
| 1218 | RenderThread::setOnStartHook(&attachRenderThreadToJvm); |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 1219 | jclass observerClass = FindClassOrDie(env, "android/view/FrameMetricsObserver"); |
| 1220 | gFrameMetricsObserverClassInfo.frameMetrics = GetFieldIDOrDie( |
| 1221 | env, observerClass, "mFrameMetrics", "Landroid/view/FrameMetrics;"); |
| 1222 | gFrameMetricsObserverClassInfo.messageQueue = GetFieldIDOrDie( |
| 1223 | env, observerClass, "mMessageQueue", "Landroid/os/MessageQueue;"); |
| 1224 | gFrameMetricsObserverClassInfo.callback = GetMethodIDOrDie( |
| 1225 | env, observerClass, "notifyDataAvailable", "(I)V"); |
| 1226 | |
| 1227 | jclass metricsClass = FindClassOrDie(env, "android/view/FrameMetrics"); |
| 1228 | gFrameMetricsObserverClassInfo.timingDataBuffer = GetFieldIDOrDie( |
| 1229 | env, metricsClass, "mTimingData", "[J"); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 1230 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 1231 | jclass hardwareRenderer = FindClassOrDie(env, |
| 1232 | "android/graphics/HardwareRenderer"); |
| 1233 | gHardwareRenderer.clazz = reinterpret_cast<jclass>(env->NewGlobalRef(hardwareRenderer)); |
| 1234 | gHardwareRenderer.invokePictureCapturedCallback = GetStaticMethodIDOrDie(env, hardwareRenderer, |
| 1235 | "invokePictureCapturedCallback", |
| 1236 | "(JLandroid/graphics/HardwareRenderer$PictureCapturedCallback;)V"); |
| 1237 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 1238 | jclass frameCallbackClass = FindClassOrDie(env, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1239 | "android/graphics/HardwareRenderer$FrameDrawingCallback"); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 1240 | gFrameDrawingCallback.onFrameDraw = GetMethodIDOrDie(env, frameCallbackClass, |
| 1241 | "onFrameDraw", "(J)V"); |
| 1242 | |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 1243 | jclass frameCompleteClass = FindClassOrDie(env, |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 1244 | "android/graphics/HardwareRenderer$FrameCompleteCallback"); |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 1245 | gFrameCompleteCallback.onFrameComplete = GetMethodIDOrDie(env, frameCompleteClass, |
| 1246 | "onFrameComplete", "(J)V"); |
| 1247 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1248 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | }; // namespace android |