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