Home | History | Annotate | Download | only in jni
      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 
     22 #include "JNIHelp.h"
     23 
     24 #include <android_runtime/AndroidRuntime.h>
     25 #include <utils/Log.h>
     26 #include <utils/Looper.h>
     27 #include <utils/threads.h>
     28 #include <gui/DisplayEventReceiver.h>
     29 #include "android_os_MessageQueue.h"
     30 
     31 namespace android {
     32 
     33 // Number of events to read at a time from the DisplayEventReceiver pipe.
     34 // The value should be large enough that we can quickly drain the pipe
     35 // using just a few large reads.
     36 static const size_t EVENT_BUFFER_SIZE = 100;
     37 
     38 static struct {
     39     jclass clazz;
     40 
     41     jmethodID dispatchVsync;
     42     jmethodID dispatchHotplug;
     43 } gDisplayEventReceiverClassInfo;
     44 
     45 
     46 class NativeDisplayEventReceiver : public LooperCallback {
     47 public:
     48     NativeDisplayEventReceiver(JNIEnv* env,
     49             jobject receiverObj, const sp<MessageQueue>& messageQueue);
     50 
     51     status_t initialize();
     52     void dispose();
     53     status_t scheduleVsync();
     54 
     55 protected:
     56     virtual ~NativeDisplayEventReceiver();
     57 
     58 private:
     59     jobject mReceiverObjGlobal;
     60     sp<MessageQueue> mMessageQueue;
     61     DisplayEventReceiver mReceiver;
     62     bool mWaitingForVsync;
     63 
     64     virtual int handleEvent(int receiveFd, int events, void* data);
     65     bool readLastVsyncMessage(nsecs_t* outTimestamp, int32_t* id, uint32_t* outCount);
     66     void dispatchVsync(nsecs_t timestamp, int32_t id, uint32_t count);
     67     void dispatchHotplug(nsecs_t timestamp, int32_t id, bool connected);
     68 };
     69 
     70 
     71 NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env,
     72         jobject receiverObj, const sp<MessageQueue>& messageQueue) :
     73         mReceiverObjGlobal(env->NewGlobalRef(receiverObj)),
     74         mMessageQueue(messageQueue), mWaitingForVsync(false) {
     75     ALOGV("receiver %p ~ Initializing input event receiver.", this);
     76 }
     77 
     78 NativeDisplayEventReceiver::~NativeDisplayEventReceiver() {
     79     JNIEnv* env = AndroidRuntime::getJNIEnv();
     80     env->DeleteGlobalRef(mReceiverObjGlobal);
     81 }
     82 
     83 status_t NativeDisplayEventReceiver::initialize() {
     84     status_t result = mReceiver.initCheck();
     85     if (result) {
     86         ALOGW("Failed to initialize display event receiver, status=%d", result);
     87         return result;
     88     }
     89 
     90     int rc = mMessageQueue->getLooper()->addFd(mReceiver.getFd(), 0, ALOOPER_EVENT_INPUT,
     91             this, NULL);
     92     if (rc < 0) {
     93         return UNKNOWN_ERROR;
     94     }
     95     return OK;
     96 }
     97 
     98 void NativeDisplayEventReceiver::dispose() {
     99     ALOGV("receiver %p ~ Disposing display event receiver.", this);
    100 
    101     if (!mReceiver.initCheck()) {
    102         mMessageQueue->getLooper()->removeFd(mReceiver.getFd());
    103     }
    104 }
    105 
    106 status_t NativeDisplayEventReceiver::scheduleVsync() {
    107     if (!mWaitingForVsync) {
    108         ALOGV("receiver %p ~ Scheduling vsync.", this);
    109 
    110         // Drain all pending events.
    111         nsecs_t vsyncTimestamp;
    112         int32_t vsyncDisplayId;
    113         uint32_t vsyncCount;
    114         readLastVsyncMessage(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount);
    115 
    116         status_t status = mReceiver.requestNextVsync();
    117         if (status) {
    118             ALOGW("Failed to request next vsync, status=%d", status);
    119             return status;
    120         }
    121 
    122         mWaitingForVsync = true;
    123     }
    124     return OK;
    125 }
    126 
    127 int NativeDisplayEventReceiver::handleEvent(int receiveFd, int events, void* data) {
    128     if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
    129         ALOGE("Display event receiver pipe was closed or an error occurred.  "
    130                 "events=0x%x", events);
    131         return 0; // remove the callback
    132     }
    133 
    134     if (!(events & ALOOPER_EVENT_INPUT)) {
    135         ALOGW("Received spurious callback for unhandled poll event.  "
    136                 "events=0x%x", events);
    137         return 1; // keep the callback
    138     }
    139 
    140     // Drain all pending events, keep the last vsync.
    141     nsecs_t vsyncTimestamp;
    142     int32_t vsyncDisplayId;
    143     uint32_t vsyncCount;
    144     if (!readLastVsyncMessage(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount)) {
    145         ALOGV("receiver %p ~ Woke up but there was no vsync pulse!", this);
    146         return 1; // keep the callback, did not obtain a vsync pulse
    147     }
    148 
    149     ALOGV("receiver %p ~ Vsync pulse: timestamp=%lld, id=%d, count=%d",
    150             this, vsyncTimestamp, vsyncDisplayId, vsyncCount);
    151     mWaitingForVsync = false;
    152 
    153     dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount);
    154     return 1; // keep the callback
    155 }
    156 
    157 bool NativeDisplayEventReceiver::readLastVsyncMessage(
    158         nsecs_t* outTimestamp, int32_t* outId, uint32_t* outCount) {
    159     DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
    160     ssize_t n;
    161     while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
    162         ALOGV("receiver %p ~ Read %d events.", this, int(n));
    163         while (n-- > 0) {
    164             const DisplayEventReceiver::Event& ev = buf[n];
    165             if (ev.header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
    166                 *outTimestamp = ev.header.timestamp;
    167                 *outId = ev.header.id;
    168                 *outCount = ev.vsync.count;
    169                 return true; // stop at last vsync in the buffer
    170             }
    171 
    172             if (ev.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
    173                 dispatchHotplug(ev.header.timestamp, ev.header.id, ev.hotplug.connected);
    174             }
    175         }
    176     }
    177     if (n < 0) {
    178         ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
    179     }
    180     return false;
    181 }
    182 
    183 void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, int32_t id, uint32_t count) {
    184     JNIEnv* env = AndroidRuntime::getJNIEnv();
    185 
    186     ALOGV("receiver %p ~ Invoking vsync handler.", this);
    187     env->CallVoidMethod(mReceiverObjGlobal,
    188             gDisplayEventReceiverClassInfo.dispatchVsync, timestamp, id, count);
    189     ALOGV("receiver %p ~ Returned from vsync handler.", this);
    190 
    191     mMessageQueue->raiseAndClearException(env, "dispatchVsync");
    192 }
    193 
    194 void NativeDisplayEventReceiver::dispatchHotplug(nsecs_t timestamp, int32_t id, bool connected) {
    195     JNIEnv* env = AndroidRuntime::getJNIEnv();
    196 
    197     ALOGV("receiver %p ~ Invoking hotplug handler.", this);
    198     env->CallVoidMethod(mReceiverObjGlobal,
    199             gDisplayEventReceiverClassInfo.dispatchHotplug, timestamp, id, connected);
    200     ALOGV("receiver %p ~ Returned from hotplug handler.", this);
    201 
    202     mMessageQueue->raiseAndClearException(env, "dispatchHotplug");
    203 }
    204 
    205 
    206 static jint nativeInit(JNIEnv* env, jclass clazz, jobject receiverObj,
    207         jobject messageQueueObj) {
    208     sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
    209     if (messageQueue == NULL) {
    210         jniThrowRuntimeException(env, "MessageQueue is not initialized.");
    211         return 0;
    212     }
    213 
    214     sp<NativeDisplayEventReceiver> receiver = new NativeDisplayEventReceiver(env,
    215             receiverObj, messageQueue);
    216     status_t status = receiver->initialize();
    217     if (status) {
    218         String8 message;
    219         message.appendFormat("Failed to initialize display event receiver.  status=%d", status);
    220         jniThrowRuntimeException(env, message.string());
    221         return 0;
    222     }
    223 
    224     receiver->incStrong(gDisplayEventReceiverClassInfo.clazz); // retain a reference for the object
    225     return reinterpret_cast<jint>(receiver.get());
    226 }
    227 
    228 static void nativeDispose(JNIEnv* env, jclass clazz, jint receiverPtr) {
    229     sp<NativeDisplayEventReceiver> receiver =
    230             reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);
    231     receiver->dispose();
    232     receiver->decStrong(gDisplayEventReceiverClassInfo.clazz); // drop reference held by the object
    233 }
    234 
    235 static void nativeScheduleVsync(JNIEnv* env, jclass clazz, jint receiverPtr) {
    236     sp<NativeDisplayEventReceiver> receiver =
    237             reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);
    238     status_t status = receiver->scheduleVsync();
    239     if (status) {
    240         String8 message;
    241         message.appendFormat("Failed to schedule next vertical sync pulse.  status=%d", status);
    242         jniThrowRuntimeException(env, message.string());
    243     }
    244 }
    245 
    246 
    247 static JNINativeMethod gMethods[] = {
    248     /* name, signature, funcPtr */
    249     { "nativeInit",
    250             "(Landroid/view/DisplayEventReceiver;Landroid/os/MessageQueue;)I",
    251             (void*)nativeInit },
    252     { "nativeDispose",
    253             "(I)V",
    254             (void*)nativeDispose },
    255     { "nativeScheduleVsync", "(I)V",
    256             (void*)nativeScheduleVsync }
    257 };
    258 
    259 #define FIND_CLASS(var, className) \
    260         var = env->FindClass(className); \
    261         LOG_FATAL_IF(! var, "Unable to find class " className); \
    262         var = jclass(env->NewGlobalRef(var));
    263 
    264 #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
    265         var = env->GetMethodID(clazz, methodName, methodDescriptor); \
    266         LOG_FATAL_IF(! var, "Unable to find method " methodName);
    267 
    268 int register_android_view_DisplayEventReceiver(JNIEnv* env) {
    269     int res = jniRegisterNativeMethods(env, "android/view/DisplayEventReceiver",
    270             gMethods, NELEM(gMethods));
    271     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
    272 
    273     FIND_CLASS(gDisplayEventReceiverClassInfo.clazz, "android/view/DisplayEventReceiver");
    274 
    275     GET_METHOD_ID(gDisplayEventReceiverClassInfo.dispatchVsync,
    276             gDisplayEventReceiverClassInfo.clazz,
    277             "dispatchVsync", "(JII)V");
    278     GET_METHOD_ID(gDisplayEventReceiverClassInfo.dispatchHotplug,
    279             gDisplayEventReceiverClassInfo.clazz,
    280             "dispatchHotplug", "(JIZ)V");
    281     return 0;
    282 }
    283 
    284 } // namespace android
    285