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 dispatchConfigChanged;
44 } gDisplayEventReceiverClassInfo;
45
46
47 class NativeDisplayEventReceiver : public DisplayEventDispatcher {
48 public:
49 NativeDisplayEventReceiver(JNIEnv* env,
50 jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource,
51 jint configChanged);
52
53 void dispose();
54
55 protected:
56 virtual ~NativeDisplayEventReceiver();
57
58 private:
59 jobject mReceiverWeakGlobal;
60 sp<MessageQueue> mMessageQueue;
61
62 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override;
63 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
64 void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId,
65 int32_t configId, nsecs_t vsyncPeriod) override;
dispatchNullEvent(nsecs_t timestamp,PhysicalDisplayId displayId)66 void dispatchNullEvent(nsecs_t timestamp, PhysicalDisplayId displayId) override {}
67 };
68
69
NativeDisplayEventReceiver(JNIEnv * env,jobject receiverWeak,const sp<MessageQueue> & messageQueue,jint vsyncSource,jint configChanged)70 NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env,
71 jobject receiverWeak, const sp<MessageQueue>& messageQueue, jint vsyncSource,
72 jint configChanged) :
73 DisplayEventDispatcher(messageQueue->getLooper(),
74 static_cast<ISurfaceComposer::VsyncSource>(vsyncSource),
75 static_cast<ISurfaceComposer::ConfigChanged>(configChanged)),
76 mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
77 mMessageQueue(messageQueue) {
78 ALOGV("receiver %p ~ Initializing display event receiver.", this);
79 }
80
~NativeDisplayEventReceiver()81 NativeDisplayEventReceiver::~NativeDisplayEventReceiver() {
82 JNIEnv* env = AndroidRuntime::getJNIEnv();
83 env->DeleteGlobalRef(mReceiverWeakGlobal);
84 ALOGV("receiver %p ~ dtor display event receiver.", this);
85 }
86
dispose()87 void NativeDisplayEventReceiver::dispose() {
88 ALOGV("receiver %p ~ Disposing display event receiver.", this);
89 DisplayEventDispatcher::dispose();
90 }
91
dispatchVsync(nsecs_t timestamp,PhysicalDisplayId displayId,uint32_t count)92 void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId,
93 uint32_t count) {
94 JNIEnv* env = AndroidRuntime::getJNIEnv();
95
96 ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal));
97 if (receiverObj.get()) {
98 ALOGV("receiver %p ~ Invoking vsync handler.", this);
99 env->CallVoidMethod(receiverObj.get(),
100 gDisplayEventReceiverClassInfo.dispatchVsync, timestamp, displayId, count);
101 ALOGV("receiver %p ~ Returned from vsync handler.", this);
102 }
103
104 mMessageQueue->raiseAndClearException(env, "dispatchVsync");
105 }
106
dispatchHotplug(nsecs_t timestamp,PhysicalDisplayId displayId,bool connected)107 void NativeDisplayEventReceiver::dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId,
108 bool connected) {
109 JNIEnv* env = AndroidRuntime::getJNIEnv();
110
111 ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal));
112 if (receiverObj.get()) {
113 ALOGV("receiver %p ~ Invoking hotplug handler.", this);
114 env->CallVoidMethod(receiverObj.get(),
115 gDisplayEventReceiverClassInfo.dispatchHotplug, timestamp, displayId, connected);
116 ALOGV("receiver %p ~ Returned from hotplug handler.", this);
117 }
118
119 mMessageQueue->raiseAndClearException(env, "dispatchHotplug");
120 }
121
dispatchConfigChanged(nsecs_t timestamp,PhysicalDisplayId displayId,int32_t configId,nsecs_t)122 void NativeDisplayEventReceiver::dispatchConfigChanged(
123 nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId, nsecs_t) {
124 JNIEnv* env = AndroidRuntime::getJNIEnv();
125
126 ScopedLocalRef<jobject> receiverObj(env,
127 jniGetReferent(env, mReceiverWeakGlobal));
128 if (receiverObj.get()) {
129 ALOGV("receiver %p ~ Invoking config changed handler.", this);
130 env->CallVoidMethod(receiverObj.get(),
131 gDisplayEventReceiverClassInfo.dispatchConfigChanged,
132 timestamp, displayId, configId);
133 ALOGV("receiver %p ~ Returned from config changed handler.", this);
134 }
135
136 mMessageQueue->raiseAndClearException(env, "dispatchConfigChanged");
137 }
138
nativeInit(JNIEnv * env,jclass clazz,jobject receiverWeak,jobject messageQueueObj,jint vsyncSource,jint configChanged)139 static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak,
140 jobject messageQueueObj, jint vsyncSource, jint configChanged) {
141 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
142 if (messageQueue == NULL) {
143 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
144 return 0;
145 }
146
147 sp<NativeDisplayEventReceiver> receiver = new NativeDisplayEventReceiver(env,
148 receiverWeak, messageQueue, vsyncSource, configChanged);
149 status_t status = receiver->initialize();
150 if (status) {
151 String8 message;
152 message.appendFormat("Failed to initialize display event receiver. status=%d", status);
153 jniThrowRuntimeException(env, message.string());
154 return 0;
155 }
156
157 receiver->incStrong(gDisplayEventReceiverClassInfo.clazz); // retain a reference for the object
158 return reinterpret_cast<jlong>(receiver.get());
159 }
160
nativeDispose(JNIEnv * env,jclass clazz,jlong receiverPtr)161 static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) {
162 NativeDisplayEventReceiver* receiver =
163 reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);
164 receiver->dispose();
165 receiver->decStrong(gDisplayEventReceiverClassInfo.clazz); // drop reference held by the object
166 }
167
nativeScheduleVsync(JNIEnv * env,jclass clazz,jlong receiverPtr)168 static void nativeScheduleVsync(JNIEnv* env, jclass clazz, jlong receiverPtr) {
169 sp<NativeDisplayEventReceiver> receiver =
170 reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);
171 status_t status = receiver->scheduleVsync();
172 if (status) {
173 String8 message;
174 message.appendFormat("Failed to schedule next vertical sync pulse. status=%d", status);
175 jniThrowRuntimeException(env, message.string());
176 }
177 }
178
179
180 static const JNINativeMethod gMethods[] = {
181 /* name, signature, funcPtr */
182 { "nativeInit",
183 "(Ljava/lang/ref/WeakReference;Landroid/os/MessageQueue;II)J",
184 (void*)nativeInit },
185 { "nativeDispose",
186 "(J)V",
187 (void*)nativeDispose },
188 // @FastNative
189 { "nativeScheduleVsync", "(J)V",
190 (void*)nativeScheduleVsync }
191 };
192
register_android_view_DisplayEventReceiver(JNIEnv * env)193 int register_android_view_DisplayEventReceiver(JNIEnv* env) {
194 int res = RegisterMethodsOrDie(env, "android/view/DisplayEventReceiver", gMethods,
195 NELEM(gMethods));
196
197 jclass clazz = FindClassOrDie(env, "android/view/DisplayEventReceiver");
198 gDisplayEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
199
200 gDisplayEventReceiverClassInfo.dispatchVsync = GetMethodIDOrDie(env,
201 gDisplayEventReceiverClassInfo.clazz, "dispatchVsync", "(JJI)V");
202 gDisplayEventReceiverClassInfo.dispatchHotplug = GetMethodIDOrDie(env,
203 gDisplayEventReceiverClassInfo.clazz, "dispatchHotplug", "(JJZ)V");
204 gDisplayEventReceiverClassInfo.dispatchConfigChanged = GetMethodIDOrDie(env,
205 gDisplayEventReceiverClassInfo.clazz, "dispatchConfigChanged", "(JJI)V");
206
207 return res;
208 }
209
210 } // namespace android
211