Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "DisplayEventReceiver" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 21 | #include <nativehelper/JNIHelp.h> |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 22 | |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 25 | #include <android_runtime/AndroidRuntime.h> |
Alec Mouri | 7d77ff8 | 2019-10-28 16:39:38 -0700 | [diff] [blame] | 26 | #include <gui/DisplayEventDispatcher.h> |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | #include <utils/Looper.h> |
| 29 | #include <utils/threads.h> |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 30 | #include "android_os_MessageQueue.h" |
| 31 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 32 | #include <nativehelper/ScopedLocalRef.h> |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 33 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 34 | #include "core_jni_helpers.h" |
| 35 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 38 | static struct { |
| 39 | jclass clazz; |
| 40 | |
| 41 | jmethodID dispatchVsync; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 42 | jmethodID dispatchHotplug; |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 43 | jmethodID dispatchConfigChanged; |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 44 | } gDisplayEventReceiverClassInfo; |
| 45 | |
| 46 | |
Michael Wright | 3d3fe50 | 2015-12-04 17:59:42 +0000 | [diff] [blame] | 47 | class NativeDisplayEventReceiver : public DisplayEventDispatcher { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 48 | public: |
| 49 | NativeDisplayEventReceiver(JNIEnv* env, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 50 | jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource, |
| 51 | jint configChanged); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 52 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 53 | void dispose(); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 54 | |
| 55 | protected: |
| 56 | virtual ~NativeDisplayEventReceiver(); |
| 57 | |
| 58 | private: |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 59 | jobject mReceiverWeakGlobal; |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 60 | sp<MessageQueue> mMessageQueue; |
Jeff Brown | bec0a86 | 2012-03-29 12:42:31 -0700 | [diff] [blame] | 61 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 62 | void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override; |
| 63 | void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override; |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 64 | void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, |
Alec Mouri | be8bafb | 2019-11-06 21:06:32 -0800 | [diff] [blame] | 65 | int32_t configId, nsecs_t vsyncPeriod) override; |
Alec Mouri | 426d1e2 | 2020-08-07 11:37:31 -0700 | [diff] [blame] | 66 | void dispatchNullEvent(nsecs_t timestamp, PhysicalDisplayId displayId) override {} |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | |
| 70 | NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 71 | jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource, |
| 72 | jint configChanged) : |
Jorim Jaggi | 34a0cdb | 2017-06-08 15:40:38 -0700 | [diff] [blame] | 73 | DisplayEventDispatcher(messageQueue->getLooper(), |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 74 | static_cast<ISurfaceComposer::VsyncSource>(vsyncSource), |
| 75 | static_cast<ISurfaceComposer::ConfigChanged>(configChanged)), |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 76 | mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)), |
Ben Wagner | 85ea32b | 2016-03-16 17:10:42 -0400 | [diff] [blame] | 77 | mMessageQueue(messageQueue) { |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 78 | ALOGV("receiver %p ~ Initializing display event receiver.", this); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | NativeDisplayEventReceiver::~NativeDisplayEventReceiver() { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 82 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 83 | env->DeleteGlobalRef(mReceiverWeakGlobal); |
John Reck | ac04f4e | 2016-06-23 10:21:45 -0700 | [diff] [blame] | 84 | ALOGV("receiver %p ~ dtor display event receiver.", this); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 87 | void NativeDisplayEventReceiver::dispose() { |
| 88 | ALOGV("receiver %p ~ Disposing display event receiver.", this); |
John Reck | ac04f4e | 2016-06-23 10:21:45 -0700 | [diff] [blame] | 89 | DisplayEventDispatcher::dispose(); |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 92 | void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 93 | uint32_t count) { |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 94 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 95 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 96 | ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal)); |
| 97 | if (receiverObj.get()) { |
| 98 | ALOGV("receiver %p ~ Invoking vsync handler.", this); |
| 99 | env->CallVoidMethod(receiverObj.get(), |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 100 | gDisplayEventReceiverClassInfo.dispatchVsync, timestamp, displayId, count); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 101 | ALOGV("receiver %p ~ Returned from vsync handler.", this); |
| 102 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 103 | |
| 104 | mMessageQueue->raiseAndClearException(env, "dispatchVsync"); |
| 105 | } |
| 106 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 107 | void NativeDisplayEventReceiver::dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 108 | bool connected) { |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 109 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 110 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 111 | ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal)); |
| 112 | if (receiverObj.get()) { |
| 113 | ALOGV("receiver %p ~ Invoking hotplug handler.", this); |
| 114 | env->CallVoidMethod(receiverObj.get(), |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 115 | gDisplayEventReceiverClassInfo.dispatchHotplug, timestamp, displayId, connected); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 116 | ALOGV("receiver %p ~ Returned from hotplug handler.", this); |
| 117 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 118 | |
| 119 | mMessageQueue->raiseAndClearException(env, "dispatchHotplug"); |
| 120 | } |
| 121 | |
Alec Mouri | be8bafb | 2019-11-06 21:06:32 -0800 | [diff] [blame] | 122 | void NativeDisplayEventReceiver::dispatchConfigChanged( |
| 123 | nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId, nsecs_t) { |
| 124 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 125 | |
Alec Mouri | be8bafb | 2019-11-06 21:06:32 -0800 | [diff] [blame] | 126 | ScopedLocalRef<jobject> receiverObj(env, |
| 127 | jniGetReferent(env, mReceiverWeakGlobal)); |
| 128 | if (receiverObj.get()) { |
| 129 | ALOGV("receiver %p ~ Invoking config changed handler.", this); |
| 130 | env->CallVoidMethod(receiverObj.get(), |
| 131 | gDisplayEventReceiverClassInfo.dispatchConfigChanged, |
| 132 | timestamp, displayId, configId); |
| 133 | ALOGV("receiver %p ~ Returned from config changed handler.", this); |
| 134 | } |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 135 | |
Alec Mouri | be8bafb | 2019-11-06 21:06:32 -0800 | [diff] [blame] | 136 | mMessageQueue->raiseAndClearException(env, "dispatchConfigChanged"); |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 139 | static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 140 | jobject messageQueueObj, jint vsyncSource, jint configChanged) { |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 141 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj); |
| 142 | if (messageQueue == NULL) { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 143 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | sp<NativeDisplayEventReceiver> receiver = new NativeDisplayEventReceiver(env, |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 148 | receiverWeak, messageQueue, vsyncSource, configChanged); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 149 | status_t status = receiver->initialize(); |
| 150 | if (status) { |
| 151 | String8 message; |
| 152 | message.appendFormat("Failed to initialize display event receiver. status=%d", status); |
| 153 | jniThrowRuntimeException(env, message.string()); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | receiver->incStrong(gDisplayEventReceiverClassInfo.clazz); // retain a reference for the object |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 158 | return reinterpret_cast<jlong>(receiver.get()); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 161 | static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
John Reck | ac04f4e | 2016-06-23 10:21:45 -0700 | [diff] [blame] | 162 | NativeDisplayEventReceiver* receiver = |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 163 | reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr); |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 164 | receiver->dispose(); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 165 | receiver->decStrong(gDisplayEventReceiverClassInfo.clazz); // drop reference held by the object |
| 166 | } |
| 167 | |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 168 | static void nativeScheduleVsync(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 169 | sp<NativeDisplayEventReceiver> receiver = |
| 170 | reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr); |
| 171 | status_t status = receiver->scheduleVsync(); |
| 172 | if (status) { |
| 173 | String8 message; |
| 174 | message.appendFormat("Failed to schedule next vertical sync pulse. status=%d", status); |
| 175 | jniThrowRuntimeException(env, message.string()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 180 | static const JNINativeMethod gMethods[] = { |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 181 | /* name, signature, funcPtr */ |
| 182 | { "nativeInit", |
Ady Abraham | 9c501aa | 2019-06-04 16:07:44 -0700 | [diff] [blame] | 183 | "(Ljava/lang/ref/WeakReference;Landroid/os/MessageQueue;II)J", |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 184 | (void*)nativeInit }, |
| 185 | { "nativeDispose", |
Ashok Bhat | 2728582 | 2013-12-18 18:00:05 +0000 | [diff] [blame] | 186 | "(J)V", |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 187 | (void*)nativeDispose }, |
John Reck | e46af37 | 2016-10-03 16:21:30 -0700 | [diff] [blame] | 188 | // @FastNative |
| 189 | { "nativeScheduleVsync", "(J)V", |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 190 | (void*)nativeScheduleVsync } |
| 191 | }; |
| 192 | |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 193 | int register_android_view_DisplayEventReceiver(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 194 | int res = RegisterMethodsOrDie(env, "android/view/DisplayEventReceiver", gMethods, |
| 195 | NELEM(gMethods)); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 196 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 197 | jclass clazz = FindClassOrDie(env, "android/view/DisplayEventReceiver"); |
| 198 | gDisplayEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 199 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 200 | gDisplayEventReceiverClassInfo.dispatchVsync = GetMethodIDOrDie(env, |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 201 | gDisplayEventReceiverClassInfo.clazz, "dispatchVsync", "(JJI)V"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 202 | gDisplayEventReceiverClassInfo.dispatchHotplug = GetMethodIDOrDie(env, |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 203 | gDisplayEventReceiverClassInfo.clazz, "dispatchHotplug", "(JJZ)V"); |
Ady Abraham | a5a21f7 | 2019-02-13 16:41:59 -0800 | [diff] [blame] | 204 | gDisplayEventReceiverClassInfo.dispatchConfigChanged = GetMethodIDOrDie(env, |
| 205 | gDisplayEventReceiverClassInfo.clazz, "dispatchConfigChanged", "(JJI)V"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 206 | |
| 207 | return res; |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace android |