| 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 | |
| 21 | #include <nativehelper/JNIHelp.h> |
| 22 | |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <android_runtime/AndroidRuntime.h> |
| 26 | #include <gui/DisplayEventDispatcher.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <utils/Looper.h> |
| 29 | #include <utils/threads.h> |
| 30 | #include "android_os_MessageQueue.h" |
| 31 | |
| 32 | #include <nativehelper/ScopedLocalRef.h> |
| 33 | |
| 34 | #include "core_jni_helpers.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | static struct { |
| 39 | jclass clazz; |
| 40 | |
| 41 | jmethodID dispatchVsync; |
| 42 | jmethodID dispatchHotplug; |
| 43 | jmethodID dispatchModeChanged; |
| 44 | jmethodID dispatchFrameRateOverrides; |
| 45 | |
| 46 | struct { |
| 47 | jclass clazz; |
| 48 | jmethodID init; |
| 49 | } frameRateOverrideClassInfo; |
| 50 | |
| 51 | struct { |
| 52 | jclass clazz; |
| 53 | jmethodID init; |
| 54 | } frameTimelineClassInfo; |
| 55 | |
| 56 | struct { |
| 57 | jclass clazz; |
| 58 | jmethodID init; |
| 59 | } vsyncEventDataClassInfo; |
| 60 | |
| 61 | } gDisplayEventReceiverClassInfo; |
| 62 | |
| 63 | |
| 64 | class NativeDisplayEventReceiver : public DisplayEventDispatcher { |
| 65 | public: |
| 66 | NativeDisplayEventReceiver(JNIEnv* env, jobject receiverWeak, |
| 67 | const sp<MessageQueue>& messageQueue, jint vsyncSource, |
| 68 | jint eventRegistration); |
| 69 | |
| 70 | void dispose(); |
| 71 | |
| 72 | protected: |
| 73 | virtual ~NativeDisplayEventReceiver(); |
| 74 | |
| 75 | private: |
| 76 | jobject mReceiverWeakGlobal; |
| 77 | sp<MessageQueue> mMessageQueue; |
| 78 | |
| 79 | void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count, |
| 80 | VsyncEventData vsyncEventData) override; |
| 81 | void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override; |
| 82 | void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId, |
| 83 | nsecs_t vsyncPeriod) override; |
| 84 | void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 85 | std::vector<FrameRateOverride> overrides) override; |
| 86 | void dispatchNullEvent(nsecs_t timestamp, PhysicalDisplayId displayId) override {} |
| 87 | }; |
| 88 | |
| 89 | NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env, jobject receiverWeak, |
| 90 | const sp<MessageQueue>& messageQueue, |
| 91 | jint vsyncSource, jint eventRegistration) |
| 92 | : DisplayEventDispatcher(messageQueue->getLooper(), |
| 93 | static_cast<ISurfaceComposer::VsyncSource>(vsyncSource), |
| 94 | static_cast<ISurfaceComposer::EventRegistration>(eventRegistration)), |
| 95 | mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)), |
| 96 | mMessageQueue(messageQueue) { |
| 97 | ALOGV("receiver %p ~ Initializing display event receiver.", this); |
| 98 | } |
| 99 | |
| 100 | NativeDisplayEventReceiver::~NativeDisplayEventReceiver() { |
| 101 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 102 | env->DeleteGlobalRef(mReceiverWeakGlobal); |
| 103 | ALOGV("receiver %p ~ dtor display event receiver.", this); |
| 104 | } |
| 105 | |
| 106 | void NativeDisplayEventReceiver::dispose() { |
| 107 | ALOGV("receiver %p ~ Disposing display event receiver.", this); |
| 108 | DisplayEventDispatcher::dispose(); |
| 109 | } |
| 110 | |
| 111 | static jobject createJavaVsyncEventData(JNIEnv* env, VsyncEventData vsyncEventData) { |
| 112 | ScopedLocalRef<jobjectArray> |
| 113 | frameTimelineObjs(env, |
| 114 | env->NewObjectArray(VsyncEventData::kFrameTimelinesLength, |
| 115 | gDisplayEventReceiverClassInfo |
| 116 | .frameTimelineClassInfo.clazz, |
| 117 | /*initial element*/ NULL)); |
| 118 | for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) { |
| 119 | VsyncEventData::FrameTimeline frameTimeline = vsyncEventData.frameTimelines[i]; |
| 120 | ScopedLocalRef<jobject> |
| 121 | frameTimelineObj(env, |
| 122 | env->NewObject(gDisplayEventReceiverClassInfo |
| 123 | .frameTimelineClassInfo.clazz, |
| 124 | gDisplayEventReceiverClassInfo |
| 125 | .frameTimelineClassInfo.init, |
| 126 | frameTimeline.vsyncId, |
| 127 | frameTimeline.expectedPresentationTime, |
| 128 | frameTimeline.deadlineTimestamp)); |
| 129 | env->SetObjectArrayElement(frameTimelineObjs.get(), i, frameTimelineObj.get()); |
| 130 | } |
| 131 | return env->NewObject(gDisplayEventReceiverClassInfo.vsyncEventDataClassInfo.clazz, |
| 132 | gDisplayEventReceiverClassInfo.vsyncEventDataClassInfo.init, |
| 133 | frameTimelineObjs.get(), vsyncEventData.preferredFrameTimelineIndex, |
| 134 | vsyncEventData.frameInterval); |
| 135 | } |
| 136 | |
| 137 | void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 138 | uint32_t count, VsyncEventData vsyncEventData) { |
| 139 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 140 | |
| 141 | ScopedLocalRef<jobject> receiverObj(env, GetReferent(env, mReceiverWeakGlobal)); |
| 142 | if (receiverObj.get()) { |
| 143 | ALOGV("receiver %p ~ Invoking vsync handler.", this); |
| 144 | |
| 145 | jobject javaVsyncEventData = createJavaVsyncEventData(env, vsyncEventData); |
| 146 | env->CallVoidMethod(receiverObj.get(), gDisplayEventReceiverClassInfo.dispatchVsync, |
| 147 | timestamp, displayId.value, count, javaVsyncEventData); |
| 148 | ALOGV("receiver %p ~ Returned from vsync handler.", this); |
| 149 | } |
| 150 | |
| 151 | mMessageQueue->raiseAndClearException(env, "dispatchVsync"); |
| 152 | } |
| 153 | |
| 154 | void NativeDisplayEventReceiver::dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 155 | bool connected) { |
| 156 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 157 | |
| 158 | ScopedLocalRef<jobject> receiverObj(env, GetReferent(env, mReceiverWeakGlobal)); |
| 159 | if (receiverObj.get()) { |
| 160 | ALOGV("receiver %p ~ Invoking hotplug handler.", this); |
| 161 | env->CallVoidMethod(receiverObj.get(), gDisplayEventReceiverClassInfo.dispatchHotplug, |
| 162 | timestamp, displayId.value, connected); |
| 163 | ALOGV("receiver %p ~ Returned from hotplug handler.", this); |
| 164 | } |
| 165 | |
| 166 | mMessageQueue->raiseAndClearException(env, "dispatchHotplug"); |
| 167 | } |
| 168 | |
| 169 | void NativeDisplayEventReceiver::dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 170 | int32_t modeId, nsecs_t) { |
| 171 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 172 | |
| 173 | ScopedLocalRef<jobject> receiverObj(env, GetReferent(env, mReceiverWeakGlobal)); |
| 174 | if (receiverObj.get()) { |
| 175 | ALOGV("receiver %p ~ Invoking mode changed handler.", this); |
| 176 | env->CallVoidMethod(receiverObj.get(), gDisplayEventReceiverClassInfo.dispatchModeChanged, |
| 177 | timestamp, displayId.value, modeId); |
| 178 | ALOGV("receiver %p ~ Returned from mode changed handler.", this); |
| 179 | } |
| 180 | |
| 181 | mMessageQueue->raiseAndClearException(env, "dispatchModeChanged"); |
| 182 | } |
| 183 | |
| 184 | void NativeDisplayEventReceiver::dispatchFrameRateOverrides( |
| 185 | nsecs_t timestamp, PhysicalDisplayId displayId, std::vector<FrameRateOverride> overrides) { |
| 186 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 187 | |
| 188 | ScopedLocalRef<jobject> receiverObj(env, GetReferent(env, mReceiverWeakGlobal)); |
| 189 | if (receiverObj.get()) { |
| 190 | ALOGV("receiver %p ~ Invoking FrameRateOverride handler.", this); |
| 191 | const auto frameRateOverrideClass = |
| 192 | gDisplayEventReceiverClassInfo.frameRateOverrideClassInfo.clazz; |
| 193 | const auto frameRateOverrideInit = |
| 194 | gDisplayEventReceiverClassInfo.frameRateOverrideClassInfo.init; |
| 195 | auto frameRateOverrideInitObject = |
| 196 | env->NewObject(frameRateOverrideClass, frameRateOverrideInit, 0, 0); |
| 197 | auto frameRateOverrideArray = env->NewObjectArray(overrides.size(), frameRateOverrideClass, |
| 198 | frameRateOverrideInitObject); |
| 199 | for (size_t i = 0; i < overrides.size(); i++) { |
| 200 | auto FrameRateOverrideObject = |
| 201 | env->NewObject(frameRateOverrideClass, frameRateOverrideInit, overrides[i].uid, |
| 202 | overrides[i].frameRateHz); |
| 203 | env->SetObjectArrayElement(frameRateOverrideArray, i, FrameRateOverrideObject); |
| 204 | } |
| 205 | |
| 206 | env->CallVoidMethod(receiverObj.get(), |
| 207 | gDisplayEventReceiverClassInfo.dispatchFrameRateOverrides, timestamp, |
| 208 | displayId.value, frameRateOverrideArray); |
| 209 | ALOGV("receiver %p ~ Returned from FrameRateOverride handler.", this); |
| 210 | } |
| 211 | |
| 212 | mMessageQueue->raiseAndClearException(env, "dispatchModeChanged"); |
| 213 | } |
| 214 | |
| 215 | static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak, jobject messageQueueObj, |
| 216 | jint vsyncSource, jint eventRegistration) { |
| 217 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj); |
| 218 | if (messageQueue == NULL) { |
| 219 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | sp<NativeDisplayEventReceiver> receiver = |
| 224 | new NativeDisplayEventReceiver(env, receiverWeak, messageQueue, vsyncSource, |
| 225 | eventRegistration); |
| 226 | status_t status = receiver->initialize(); |
| 227 | if (status) { |
| 228 | String8 message; |
| 229 | message.appendFormat("Failed to initialize display event receiver. status=%d", status); |
| 230 | jniThrowRuntimeException(env, message.string()); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | receiver->incStrong(gDisplayEventReceiverClassInfo.clazz); // retain a reference for the object |
| 235 | return reinterpret_cast<jlong>(receiver.get()); |
| 236 | } |
| 237 | |
| 238 | static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
| 239 | NativeDisplayEventReceiver* receiver = |
| 240 | reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr); |
| 241 | receiver->dispose(); |
| 242 | receiver->decStrong(gDisplayEventReceiverClassInfo.clazz); // drop reference held by the object |
| 243 | } |
| 244 | |
| 245 | static void nativeScheduleVsync(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
| 246 | sp<NativeDisplayEventReceiver> receiver = |
| 247 | reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr); |
| 248 | status_t status = receiver->scheduleVsync(); |
| 249 | if (status) { |
| 250 | String8 message; |
| 251 | message.appendFormat("Failed to schedule next vertical sync pulse. status=%d", status); |
| 252 | jniThrowRuntimeException(env, message.string()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static jobject nativeGetLatestVsyncEventData(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
| 257 | sp<NativeDisplayEventReceiver> receiver = |
| 258 | reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr); |
| 259 | gui::ParcelableVsyncEventData parcelableVsyncEventData; |
| 260 | status_t status = receiver->getLatestVsyncEventData(&parcelableVsyncEventData); |
| 261 | if (status) { |
| 262 | ALOGW("Failed to get latest vsync event data from surface flinger"); |
| 263 | return NULL; |
| 264 | } |
| 265 | return createJavaVsyncEventData(env, parcelableVsyncEventData.vsync); |
| 266 | } |
| 267 | |
| 268 | static const JNINativeMethod gMethods[] = { |
| 269 | /* name, signature, funcPtr */ |
| 270 | {"nativeInit", "(Ljava/lang/ref/WeakReference;Landroid/os/MessageQueue;II)J", |
| 271 | (void*)nativeInit}, |
| 272 | {"nativeDispose", "(J)V", (void*)nativeDispose}, |
| 273 | // @FastNative |
| 274 | {"nativeScheduleVsync", "(J)V", (void*)nativeScheduleVsync}, |
| 275 | {"nativeGetLatestVsyncEventData", "(J)Landroid/view/DisplayEventReceiver$VsyncEventData;", |
| 276 | (void*)nativeGetLatestVsyncEventData}}; |
| 277 | |
| 278 | int register_android_view_DisplayEventReceiver(JNIEnv* env) { |
| 279 | int res = RegisterMethodsOrDie(env, "android/view/DisplayEventReceiver", gMethods, |
| 280 | NELEM(gMethods)); |
| 281 | |
| 282 | jclass clazz = FindClassOrDie(env, "android/view/DisplayEventReceiver"); |
| 283 | gDisplayEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
| 284 | |
| 285 | gDisplayEventReceiverClassInfo.dispatchVsync = |
| 286 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.clazz, "dispatchVsync", |
| 287 | "(JJILandroid/view/DisplayEventReceiver$VsyncEventData;)V"); |
| 288 | gDisplayEventReceiverClassInfo.dispatchHotplug = GetMethodIDOrDie(env, |
| 289 | gDisplayEventReceiverClassInfo.clazz, "dispatchHotplug", "(JJZ)V"); |
| 290 | gDisplayEventReceiverClassInfo.dispatchModeChanged = |
| 291 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.clazz, "dispatchModeChanged", |
| 292 | "(JJI)V"); |
| 293 | gDisplayEventReceiverClassInfo.dispatchFrameRateOverrides = |
| 294 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.clazz, |
| 295 | "dispatchFrameRateOverrides", |
| 296 | "(JJ[Landroid/view/DisplayEventReceiver$FrameRateOverride;)V"); |
| 297 | |
| 298 | jclass frameRateOverrideClazz = |
| 299 | FindClassOrDie(env, "android/view/DisplayEventReceiver$FrameRateOverride"); |
| 300 | gDisplayEventReceiverClassInfo.frameRateOverrideClassInfo.clazz = |
| 301 | MakeGlobalRefOrDie(env, frameRateOverrideClazz); |
| 302 | gDisplayEventReceiverClassInfo.frameRateOverrideClassInfo.init = |
| 303 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.frameRateOverrideClassInfo.clazz, |
| 304 | "<init>", "(IF)V"); |
| 305 | |
| 306 | jclass frameTimelineClazz = |
| 307 | FindClassOrDie(env, "android/view/DisplayEventReceiver$VsyncEventData$FrameTimeline"); |
| 308 | gDisplayEventReceiverClassInfo.frameTimelineClassInfo.clazz = |
| 309 | MakeGlobalRefOrDie(env, frameTimelineClazz); |
| 310 | gDisplayEventReceiverClassInfo.frameTimelineClassInfo.init = |
| 311 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.frameTimelineClassInfo.clazz, |
| 312 | "<init>", "(JJJ)V"); |
| 313 | |
| 314 | jclass vsyncEventDataClazz = |
| 315 | FindClassOrDie(env, "android/view/DisplayEventReceiver$VsyncEventData"); |
| 316 | gDisplayEventReceiverClassInfo.vsyncEventDataClassInfo.clazz = |
| 317 | MakeGlobalRefOrDie(env, vsyncEventDataClazz); |
| 318 | gDisplayEventReceiverClassInfo.vsyncEventDataClassInfo.init = |
| 319 | GetMethodIDOrDie(env, gDisplayEventReceiverClassInfo.vsyncEventDataClassInfo.clazz, |
| 320 | "<init>", |
| 321 | "([Landroid/view/" |
| 322 | "DisplayEventReceiver$VsyncEventData$FrameTimeline;IJ)V"); |
| 323 | |
| 324 | return res; |
| 325 | } |
| 326 | |
| 327 | } // namespace android |