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