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