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