Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 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> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame^] | 24 | #include <gui/ISurfaceComposer.h> |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 25 | |
| 26 | #include <private/gui/ComposerService.h> |
| 27 | |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 28 | // --------------------------------------------------------------------------- |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | |
| 34 | DisplayEventReceiver::DisplayEventReceiver() { |
| 35 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 36 | if (sf != NULL) { |
| 37 | mEventConnection = sf->createDisplayEventConnection(); |
| 38 | if (mEventConnection != NULL) { |
| 39 | mDataChannel = mEventConnection->getDataChannel(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | DisplayEventReceiver::~DisplayEventReceiver() { |
| 45 | } |
| 46 | |
| 47 | status_t DisplayEventReceiver::initCheck() const { |
| 48 | if (mDataChannel != NULL) |
| 49 | return NO_ERROR; |
| 50 | return NO_INIT; |
| 51 | } |
| 52 | |
| 53 | int DisplayEventReceiver::getFd() const { |
| 54 | if (mDataChannel == NULL) |
| 55 | return NO_INIT; |
| 56 | |
| 57 | return mDataChannel->getFd(); |
| 58 | } |
| 59 | |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 60 | status_t DisplayEventReceiver::setVsyncRate(uint32_t count) { |
| 61 | if (int32_t(count) < 0) |
| 62 | return BAD_VALUE; |
| 63 | |
| 64 | if (mEventConnection != NULL) { |
| 65 | mEventConnection->setVsyncRate(count); |
| 66 | return NO_ERROR; |
| 67 | } |
| 68 | return NO_INIT; |
| 69 | } |
| 70 | |
| 71 | status_t DisplayEventReceiver::requestNextVsync() { |
| 72 | if (mEventConnection != NULL) { |
| 73 | mEventConnection->requestNextVsync(); |
| 74 | return NO_ERROR; |
| 75 | } |
| 76 | return NO_INIT; |
| 77 | } |
| 78 | |
| 79 | |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 80 | ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events, |
| 81 | size_t count) { |
Mathias Agopian | 9a9dbd5 | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 82 | return DisplayEventReceiver::getEvents(mDataChannel, events, count); |
| 83 | } |
| 84 | |
| 85 | ssize_t DisplayEventReceiver::getEvents(const sp<BitTube>& dataChannel, |
| 86 | Event* events, size_t count) |
| 87 | { |
| 88 | ssize_t size = dataChannel->read(events, sizeof(events[0])*count); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 89 | ALOGE_IF(size<0, |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 90 | "DisplayEventReceiver::getEvents error (%s)", |
| 91 | strerror(-size)); |
| 92 | if (size >= 0) { |
| 93 | // Note: if (size % sizeof(events[0])) != 0, we've got a |
| 94 | // partial read. This can happen if the queue filed up (ie: if we |
| 95 | // didn't pull from it fast enough). |
| 96 | // We discard the partial event and rely on the sender to |
| 97 | // re-send the event if appropriate (some events, like VSYNC |
| 98 | // can be lost forever). |
| 99 | |
| 100 | // returns number of events read |
| 101 | size /= sizeof(events[0]); |
| 102 | } |
| 103 | return size; |
| 104 | } |
| 105 | |
| 106 | // --------------------------------------------------------------------------- |
| 107 | |
| 108 | }; // namespace android |