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