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 <androidfw/DisplayEventDispatcher.h>
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
33 #include <nativehelper/ScopedLocalRef.h>
34
35 #include "core_jni_helpers.h"
36
37 namespace android {
38
39 static struct {
40 jclass clazz;
41
42 jmethodID dispatchVsync;
43 jmethodID dispatchHotplug;
44 jmethodID dispatchConfigChanged;
45 } gDisplayEventReceiverClassInfo;
46
47
48 class NativeDisplayEventReceiver : public DisplayEventDispatcher {
49 public:
50 NativeDisplayEventReceiver(JNIEnv* env,
51 jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource);
52
53 void dispose();
54
55 protected:
56 virtual ~NativeDisplayEventReceiver();
57
58 private:
59 jobject mReceiverWeakGlobal;
60 sp<MessageQueue> mMessageQueue;
61 DisplayEventReceiver mReceiver;
62
63 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override;
64 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
65 void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId,
66 int32_t configId) override;
67 };
68
69
NativeDisplayEventReceiver(JNIEnv * env,jobject receiverWeak,const sp<MessageQueue> & messageQueue,jint vsyncSource)70 NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env,
71 jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource) :
72 DisplayEventDispatcher(messageQueue->getLooper(),
73 static_cast<ISurfaceComposer::VsyncSource>(vsyncSource)),
74 mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
75 mMessageQueue(messageQueue) {
76 ALOGV("receiver %p ~ Initializing display event receiver.", this);
77 }
78
~NativeDisplayEventReceiver()79 NativeDisplayEventReceiver::~NativeDisplayEventReceiver() {
80 JNIEnv* env = AndroidRuntime::getJNIEnv();
81 env->DeleteGlobalRef(mReceiverWeakGlobal);
82 ALOGV("receiver %p ~ dtor display event receiver.", this);
83 }
84
dispose()85 void NativeDisplayEventReceiver::dispose() {
86 ALOGV("receiver %p ~ Disposing display event receiver.", this);
87 DisplayEventDispatcher::dispose();
88 }
89
dispatchVsync(nsecs_t timestamp,PhysicalDisplayId displayId,uint32_t count)90 void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId,
91 uint32_t count) {
92 JNIEnv* env = AndroidRuntime::getJNIEnv();
93
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(),
98 gDisplayEventReceiverClassInfo.dispatchVsync, timestamp, displayId, count);
99 ALOGV("receiver %p ~ Returned from vsync handler.", this);
100 }
101
102 mMessageQueue->raiseAndClearException(env, "dispatchVsync");
103 }
104
dispatchHotplug(nsecs_t timestamp,PhysicalDisplayId displayId,bool connected)105 void NativeDisplayEventReceiver::dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId,
106 bool connected) {
107 JNIEnv* env = AndroidRuntime::getJNIEnv();
108
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(),
113 gDisplayEventReceiverClassInfo.dispatchHotplug, timestamp, displayId, connected);
114 ALOGV("receiver %p ~ Returned from hotplug handler.", this);
115 }
116
117 mMessageQueue->raiseAndClearException(env, "dispatchHotplug");
118 }
119
dispatchConfigChanged(nsecs_t timestamp,PhysicalDisplayId displayId,int32_t configId)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
137
nativeInit(JNIEnv * env,jclass clazz,jobject receiverWeak,jobject messageQueueObj,jint vsyncSource)138 static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak,
139 jobject messageQueueObj, jint vsyncSource) {
140 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
141 if (messageQueue == NULL) {
142 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
143 return 0;
144 }
145
146 sp<NativeDisplayEventReceiver> receiver = new NativeDisplayEventReceiver(env,
147 receiverWeak, messageQueue, vsyncSource);
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
157 return reinterpret_cast<jlong>(receiver.get());
158 }
159
nativeDispose(JNIEnv * env,jclass clazz,jlong receiverPtr)160 static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) {
161 NativeDisplayEventReceiver* receiver =
162 reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);
163 receiver->dispose();
164 receiver->decStrong(gDisplayEventReceiverClassInfo.clazz); // drop reference held by the object
165 }
166
nativeScheduleVsync(JNIEnv * env,jclass clazz,jlong receiverPtr)167 static void nativeScheduleVsync(JNIEnv* env, jclass clazz, jlong receiverPtr) {
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
179 static const JNINativeMethod gMethods[] = {
180 /* name, signature, funcPtr */
181 { "nativeInit",
182 "(Ljava/lang/ref/WeakReference;Landroid/os/MessageQueue;I)J",
183 (void*)nativeInit },
184 { "nativeDispose",
185 "(J)V",
186 (void*)nativeDispose },
187 // @FastNative
188 { "nativeScheduleVsync", "(J)V",
189 (void*)nativeScheduleVsync }
190 };
191
register_android_view_DisplayEventReceiver(JNIEnv * env)192 int register_android_view_DisplayEventReceiver(JNIEnv* env) {
193 int res = RegisterMethodsOrDie(env, "android/view/DisplayEventReceiver", gMethods,
194 NELEM(gMethods));
195
196 jclass clazz = FindClassOrDie(env, "android/view/DisplayEventReceiver");
197 gDisplayEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
198
199 gDisplayEventReceiverClassInfo.dispatchVsync = GetMethodIDOrDie(env,
200 gDisplayEventReceiverClassInfo.clazz, "dispatchVsync", "(JJI)V");
201 gDisplayEventReceiverClassInfo.dispatchHotplug = GetMethodIDOrDie(env,
202 gDisplayEventReceiverClassInfo.clazz, "dispatchHotplug", "(JJZ)V");
203 gDisplayEventReceiverClassInfo.dispatchConfigChanged = GetMethodIDOrDie(env,
204 gDisplayEventReceiverClassInfo.clazz, "dispatchConfigChanged", "(JJI)V");
205
206 return res;
207 }
208
209 } // namespace android
210