DisplayEventReceiver.cpp revision e6f43ddce78d6846af12550ff9193c5c6fe5844b
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
61status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
62    if (int32_t(count) < 0)
63        return BAD_VALUE;
64
65    if (mEventConnection != NULL) {
66        mEventConnection->setVsyncRate(count);
67        return NO_ERROR;
68    }
69    return NO_INIT;
70}
71
72status_t DisplayEventReceiver::requestNextVsync() {
73    if (mEventConnection != NULL) {
74        mEventConnection->requestNextVsync();
75        return NO_ERROR;
76    }
77    return NO_INIT;
78}
79
80
81ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
82        size_t count) {
83    ssize_t size = mDataChannel->read(events, sizeof(events[0])*count);
84    ALOGE_IF(size<0,
85            "DisplayEventReceiver::getEvents error (%s)",
86            strerror(-size));
87    if (size >= 0) {
88        // Note: if (size % sizeof(events[0])) != 0, we've got a
89        // partial read. This can happen if the queue filed up (ie: if we
90        // didn't pull from it fast enough).
91        // We discard the partial event and rely on the sender to
92        // re-send the event if appropriate (some events, like VSYNC
93        // can be lost forever).
94
95        // returns number of events read
96        size /= sizeof(events[0]);
97    }
98    return size;
99}
100
101// ---------------------------------------------------------------------------
102
103}; // namespace android
104