blob: 67cbc7b11183032c2035c82826a43440efc02279 [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
Rachel Lee2248f522023-01-27 16:45:23 -080017#define LOG_TAG "DisplayEventReceiver"
18
Mathias Agopiand0566bc2011-11-17 17:49:17 -080019#include <string.h>
20
21#include <utils/Errors.h>
22
Mathias Agopiand0566bc2011-11-17 17:49:17 -080023#include <gui/DisplayEventReceiver.h>
Rachel Leeef2e21f2022-02-01 14:51:34 -080024#include <gui/VsyncEventData.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025
Huihong Luo1b0c49f2022-03-15 19:18:21 -070026#include <private/gui/ComposerServiceAIDL.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080027
Mathias Agopian801ea092017-03-06 15:05:04 -080028#include <private/gui/BitTube.h>
29
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030// ---------------------------------------------------------------------------
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
Huihong Luo1b0c49f2022-03-15 19:18:21 -070036DisplayEventReceiver::DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource,
Rachel Lee2248f522023-01-27 16:45:23 -080037 EventRegistrationFlags eventRegistration,
38 const sp<IBinder>& layerHandle) {
Huihong Luo1b0c49f2022-03-15 19:18:21 -070039 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -070040 if (sf != nullptr) {
Huihong Luo1b0c49f2022-03-15 19:18:21 -070041 mEventConnection = nullptr;
42 binder::Status status =
43 sf->createDisplayEventConnection(vsyncSource,
44 static_cast<
45 gui::ISurfaceComposer::EventRegistration>(
46 eventRegistration.get()),
Rachel Lee2248f522023-01-27 16:45:23 -080047 layerHandle, &mEventConnection);
48 if (status.isOk() && mEventConnection != nullptr) {
Dan Stoza6b698e42017-04-03 13:09:08 -070049 mDataChannel = std::make_unique<gui::BitTube>();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070050 status = mEventConnection->stealReceiveChannel(mDataChannel.get());
Ady Abraham745f7b52022-02-08 19:36:37 -080051 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 Lee2248f522023-01-27 16:45:23 -080057 } else {
58 ALOGE("DisplayEventConnection creation failed: status=%s", status.toString8().c_str());
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059 }
60 }
61}
62
63DisplayEventReceiver::~DisplayEventReceiver() {
64}
65
66status_t DisplayEventReceiver::initCheck() const {
Yi Kong48a619f2018-06-05 16:34:59 -070067 if (mDataChannel != nullptr)
Mathias Agopiand0566bc2011-11-17 17:49:17 -080068 return NO_ERROR;
Ady Abraham745f7b52022-02-08 19:36:37 -080069 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080070}
71
72int DisplayEventReceiver::getFd() const {
Ady Abraham745f7b52022-02-08 19:36:37 -080073 if (mDataChannel == nullptr) return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080074
75 return mDataChannel->getFd();
76}
77
Mathias Agopian478ae5e2011-12-06 17:22:19 -080078status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
79 if (int32_t(count) < 0)
80 return BAD_VALUE;
81
Yi Kong48a619f2018-06-05 16:34:59 -070082 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083 mEventConnection->setVsyncRate(count);
84 return NO_ERROR;
85 }
Ady Abraham745f7b52022-02-08 19:36:37 -080086 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080087}
88
89status_t DisplayEventReceiver::requestNextVsync() {
Yi Kong48a619f2018-06-05 16:34:59 -070090 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080091 mEventConnection->requestNextVsync();
92 return NO_ERROR;
93 }
Ady Abraham745f7b52022-02-08 19:36:37 -080094 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080095}
96
Rachel Leeb9c5a772022-02-04 21:17:37 -080097status_t DisplayEventReceiver::getLatestVsyncEventData(
98 ParcelableVsyncEventData* outVsyncEventData) const {
Rachel Leeef2e21f2022-02-01 14:51:34 -080099 if (mEventConnection != nullptr) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800100 auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800101 if (!status.isOk()) {
Rachel Leef3f39912023-05-19 11:20:05 -0700102 ALOGE("Failed to get latest vsync event data: %s", status.toString8().c_str());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800103 return status.transactionError();
104 }
Rachel Leeef2e21f2022-02-01 14:51:34 -0800105 return NO_ERROR;
106 }
107 return NO_INIT;
108}
109
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800110ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
111 size_t count) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700112 return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800113}
114
Dan Stoza6b698e42017-04-03 13:09:08 -0700115ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800116 Event* events, size_t count)
117{
Dan Stoza27c81152017-03-31 16:30:42 -0700118 return gui::BitTube::recvObjects(dataChannel, events, count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700119}
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800120
Alec Mouri967d5d72020-08-05 12:50:03 -0700121ssize_t DisplayEventReceiver::sendEvents(Event const* events, size_t count) {
122 return DisplayEventReceiver::sendEvents(mDataChannel.get(), events, count);
123}
124
Dan Stoza6b698e42017-04-03 13:09:08 -0700125ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
Mathias Agopian7b5be952012-04-02 17:02:19 -0700126 Event const* events, size_t count)
127{
Dan Stoza27c81152017-03-31 16:30:42 -0700128 return gui::BitTube::sendObjects(dataChannel, events, count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800129}
130
131// ---------------------------------------------------------------------------
132
133}; // namespace android