blob: c52fb6b7c362875bfe1f475f6936114561052017 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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 Agopiand0566bc2011-11-17 17:49:17 -080021#include <gui/DisplayEventReceiver.h>
Rachel Leeef2e21f2022-02-01 14:51:34 -080022#include <gui/VsyncEventData.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080023
Huihong Luo1b0c49f2022-03-15 19:18:21 -070024#include <private/gui/ComposerServiceAIDL.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025
Mathias Agopian801ea092017-03-06 15:05:04 -080026#include <private/gui/BitTube.h>
27
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028// ---------------------------------------------------------------------------
29
30namespace android {
31
32// ---------------------------------------------------------------------------
33
Huihong Luo1b0c49f2022-03-15 19:18:21 -070034DisplayEventReceiver::DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource,
35 EventRegistrationFlags eventRegistration) {
36 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -070037 if (sf != nullptr) {
Huihong Luo1b0c49f2022-03-15 19:18:21 -070038 mEventConnection = nullptr;
39 binder::Status status =
40 sf->createDisplayEventConnection(vsyncSource,
41 static_cast<
42 gui::ISurfaceComposer::EventRegistration>(
43 eventRegistration.get()),
44 &mEventConnection);
Yi Kong48a619f2018-06-05 16:34:59 -070045 if (mEventConnection != nullptr) {
Dan Stoza6b698e42017-04-03 13:09:08 -070046 mDataChannel = std::make_unique<gui::BitTube>();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070047 status = mEventConnection->stealReceiveChannel(mDataChannel.get());
Ady Abraham745f7b52022-02-08 19:36:37 -080048 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 Agopiand0566bc2011-11-17 17:49:17 -080054 }
55 }
56}
57
58DisplayEventReceiver::~DisplayEventReceiver() {
59}
60
61status_t DisplayEventReceiver::initCheck() const {
Yi Kong48a619f2018-06-05 16:34:59 -070062 if (mDataChannel != nullptr)
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063 return NO_ERROR;
Ady Abraham745f7b52022-02-08 19:36:37 -080064 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065}
66
67int DisplayEventReceiver::getFd() const {
Ady Abraham745f7b52022-02-08 19:36:37 -080068 if (mDataChannel == nullptr) return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069
70 return mDataChannel->getFd();
71}
72
Mathias Agopian478ae5e2011-12-06 17:22:19 -080073status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
74 if (int32_t(count) < 0)
75 return BAD_VALUE;
76
Yi Kong48a619f2018-06-05 16:34:59 -070077 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080078 mEventConnection->setVsyncRate(count);
79 return NO_ERROR;
80 }
Ady Abraham745f7b52022-02-08 19:36:37 -080081 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080082}
83
84status_t DisplayEventReceiver::requestNextVsync() {
Yi Kong48a619f2018-06-05 16:34:59 -070085 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080086 mEventConnection->requestNextVsync();
87 return NO_ERROR;
88 }
Ady Abraham745f7b52022-02-08 19:36:37 -080089 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080090}
91
Rachel Leeb9c5a772022-02-04 21:17:37 -080092status_t DisplayEventReceiver::getLatestVsyncEventData(
93 ParcelableVsyncEventData* outVsyncEventData) const {
Rachel Leeef2e21f2022-02-01 14:51:34 -080094 if (mEventConnection != nullptr) {
Rachel Leeb9c5a772022-02-04 21:17:37 -080095 auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -080096 if (!status.isOk()) {
97 ALOGE("Failed to get latest vsync event data: %s", status.exceptionMessage().c_str());
98 return status.transactionError();
99 }
Rachel Leeef2e21f2022-02-01 14:51:34 -0800100 return NO_ERROR;
101 }
102 return NO_INIT;
103}
104
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800105ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
106 size_t count) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700107 return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108}
109
Dan Stoza6b698e42017-04-03 13:09:08 -0700110ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800111 Event* events, size_t count)
112{
Dan Stoza27c81152017-03-31 16:30:42 -0700113 return gui::BitTube::recvObjects(dataChannel, events, count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700114}
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800115
Alec Mouri967d5d72020-08-05 12:50:03 -0700116ssize_t DisplayEventReceiver::sendEvents(Event const* events, size_t count) {
117 return DisplayEventReceiver::sendEvents(mDataChannel.get(), events, count);
118}
119
Dan Stoza6b698e42017-04-03 13:09:08 -0700120ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
Mathias Agopian7b5be952012-04-02 17:02:19 -0700121 Event const* events, size_t count)
122{
Dan Stoza27c81152017-03-31 16:30:42 -0700123 return gui::BitTube::sendObjects(dataChannel, events, count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800124}
125
126// ---------------------------------------------------------------------------
127
128}; // namespace android