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