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