Mathias Agopian | d0566bc | 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 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 21 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 22 | #include <gui/ISurfaceComposer.h> |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 23 | #include <gui/VsyncEventData.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 24 | |
| 25 | #include <private/gui/ComposerService.h> |
| 26 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 27 | #include <private/gui/BitTube.h> |
| 28 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 35 | DisplayEventReceiver::DisplayEventReceiver( |
| 36 | ISurfaceComposer::VsyncSource vsyncSource, |
| 37 | ISurfaceComposer::EventRegistrationFlags eventRegistration) { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 39 | if (sf != nullptr) { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 40 | mEventConnection = sf->createDisplayEventConnection(vsyncSource, eventRegistration); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 41 | if (mEventConnection != nullptr) { |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 42 | mDataChannel = std::make_unique<gui::BitTube>(); |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 43 | const auto status = mEventConnection->stealReceiveChannel(mDataChannel.get()); |
| 44 | if (!status.isOk()) { |
| 45 | ALOGE("stealReceiveChannel failed: %s", status.toString8().c_str()); |
| 46 | mInitError = std::make_optional<status_t>(status.transactionError()); |
| 47 | mDataChannel.reset(); |
| 48 | mEventConnection.clear(); |
| 49 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | DisplayEventReceiver::~DisplayEventReceiver() { |
| 55 | } |
| 56 | |
| 57 | status_t DisplayEventReceiver::initCheck() const { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 58 | if (mDataChannel != nullptr) |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 59 | return NO_ERROR; |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 60 | return mInitError.has_value() ? mInitError.value() : NO_INIT; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int DisplayEventReceiver::getFd() const { |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 64 | if (mDataChannel == nullptr) return mInitError.has_value() ? mInitError.value() : NO_INIT; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 65 | |
| 66 | return mDataChannel->getFd(); |
| 67 | } |
| 68 | |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 69 | status_t DisplayEventReceiver::setVsyncRate(uint32_t count) { |
| 70 | if (int32_t(count) < 0) |
| 71 | return BAD_VALUE; |
| 72 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 73 | if (mEventConnection != nullptr) { |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 74 | mEventConnection->setVsyncRate(count); |
| 75 | return NO_ERROR; |
| 76 | } |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 77 | return mInitError.has_value() ? mInitError.value() : NO_INIT; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | status_t DisplayEventReceiver::requestNextVsync() { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 81 | if (mEventConnection != nullptr) { |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 82 | mEventConnection->requestNextVsync(); |
| 83 | return NO_ERROR; |
| 84 | } |
Ady Abraham | 745f7b5 | 2022-02-08 19:36:37 -0800 | [diff] [blame] | 85 | return mInitError.has_value() ? mInitError.value() : NO_INIT; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 88 | status_t DisplayEventReceiver::getLatestVsyncEventData( |
| 89 | ParcelableVsyncEventData* outVsyncEventData) const { |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 90 | if (mEventConnection != nullptr) { |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 91 | auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 92 | if (!status.isOk()) { |
| 93 | ALOGE("Failed to get latest vsync event data: %s", status.exceptionMessage().c_str()); |
| 94 | return status.transactionError(); |
| 95 | } |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 96 | return NO_ERROR; |
| 97 | } |
| 98 | return NO_INIT; |
| 99 | } |
| 100 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 101 | ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events, |
| 102 | size_t count) { |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 103 | return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 106 | ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel, |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 107 | Event* events, size_t count) |
| 108 | { |
Dan Stoza | 27c8115 | 2017-03-31 16:30:42 -0700 | [diff] [blame] | 109 | return gui::BitTube::recvObjects(dataChannel, events, count); |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 110 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 111 | |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 112 | ssize_t DisplayEventReceiver::sendEvents(Event const* events, size_t count) { |
| 113 | return DisplayEventReceiver::sendEvents(mDataChannel.get(), events, count); |
| 114 | } |
| 115 | |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 116 | ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel, |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 117 | Event const* events, size_t count) |
| 118 | { |
Dan Stoza | 27c8115 | 2017-03-31 16:30:42 -0700 | [diff] [blame] | 119 | return gui::BitTube::sendObjects(dataChannel, events, count); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // --------------------------------------------------------------------------- |
| 123 | |
| 124 | }; // namespace android |