DisplayEventReceiver.cpp revision d0566bc26fcf6ca396118701fa11900b627f2c09
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#include <string.h>
18
19#include <utils/Errors.h>
20
21#include <gui/BitTube.h>
22#include <gui/DisplayEventReceiver.h>
23#include <gui/IDisplayEventConnection.h>
24
25#include <private/gui/ComposerService.h>
26
27#include <surfaceflinger/ISurfaceComposer.h>
28
29// ---------------------------------------------------------------------------
30
31namespace android {
32
33// ---------------------------------------------------------------------------
34
35DisplayEventReceiver::DisplayEventReceiver() {
36    sp<ISurfaceComposer> sf(ComposerService::getComposerService());
37    if (sf != NULL) {
38        mEventConnection = sf->createDisplayEventConnection();
39        if (mEventConnection != NULL) {
40            mDataChannel = mEventConnection->getDataChannel();
41        }
42    }
43}
44
45DisplayEventReceiver::~DisplayEventReceiver() {
46}
47
48status_t DisplayEventReceiver::initCheck() const {
49    if (mDataChannel != NULL)
50        return NO_ERROR;
51    return NO_INIT;
52}
53
54int DisplayEventReceiver::getFd() const {
55    if (mDataChannel == NULL)
56        return NO_INIT;
57
58    return mDataChannel->getFd();
59}
60
61ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
62        size_t count) {
63    ssize_t size = mDataChannel->read(events, sizeof(events[0])*count);
64    LOGE_IF(size<0,
65            "DisplayEventReceiver::getEvents error (%s)",
66            strerror(-size));
67    if (size >= 0) {
68        // Note: if (size % sizeof(events[0])) != 0, we've got a
69        // partial read. This can happen if the queue filed up (ie: if we
70        // didn't pull from it fast enough).
71        // We discard the partial event and rely on the sender to
72        // re-send the event if appropriate (some events, like VSYNC
73        // can be lost forever).
74
75        // returns number of events read
76        size /= sizeof(events[0]);
77    }
78    return size;
79}
80
81// ---------------------------------------------------------------------------
82
83}; // namespace android
84