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