blob: bfb77699c08e7084a23fee23c2435e85693ee313 [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>
Mathias Agopian90ac7992012-02-25 18:48:35 -080022#include <gui/ISurfaceComposer.h>
Rachel Leeef2e21f2022-02-01 14:51:34 -080023#include <gui/VsyncEventData.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024
25#include <private/gui/ComposerService.h>
26
Mathias Agopian801ea092017-03-06 15:05:04 -080027#include <private/gui/BitTube.h>
28
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029// ---------------------------------------------------------------------------
30
31namespace android {
32
33// ---------------------------------------------------------------------------
34
Ady Abraham62f216c2020-10-13 19:07:23 -070035DisplayEventReceiver::DisplayEventReceiver(
36 ISurfaceComposer::VsyncSource vsyncSource,
37 ISurfaceComposer::EventRegistrationFlags eventRegistration) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -070039 if (sf != nullptr) {
Ady Abraham62f216c2020-10-13 19:07:23 -070040 mEventConnection = sf->createDisplayEventConnection(vsyncSource, eventRegistration);
Yi Kong48a619f2018-06-05 16:34:59 -070041 if (mEventConnection != nullptr) {
Dan Stoza6b698e42017-04-03 13:09:08 -070042 mDataChannel = std::make_unique<gui::BitTube>();
Ady Abraham745f7b52022-02-08 19:36:37 -080043 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 Agopiand0566bc2011-11-17 17:49:17 -080050 }
51 }
52}
53
54DisplayEventReceiver::~DisplayEventReceiver() {
55}
56
57status_t DisplayEventReceiver::initCheck() const {
Yi Kong48a619f2018-06-05 16:34:59 -070058 if (mDataChannel != nullptr)
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059 return NO_ERROR;
Ady Abraham745f7b52022-02-08 19:36:37 -080060 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080061}
62
63int DisplayEventReceiver::getFd() const {
Ady Abraham745f7b52022-02-08 19:36:37 -080064 if (mDataChannel == nullptr) return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065
66 return mDataChannel->getFd();
67}
68
Mathias Agopian478ae5e2011-12-06 17:22:19 -080069status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
70 if (int32_t(count) < 0)
71 return BAD_VALUE;
72
Yi Kong48a619f2018-06-05 16:34:59 -070073 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080074 mEventConnection->setVsyncRate(count);
75 return NO_ERROR;
76 }
Ady Abraham745f7b52022-02-08 19:36:37 -080077 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080078}
79
80status_t DisplayEventReceiver::requestNextVsync() {
Yi Kong48a619f2018-06-05 16:34:59 -070081 if (mEventConnection != nullptr) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080082 mEventConnection->requestNextVsync();
83 return NO_ERROR;
84 }
Ady Abraham745f7b52022-02-08 19:36:37 -080085 return mInitError.has_value() ? mInitError.value() : NO_INIT;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080086}
87
Rachel Leeb9c5a772022-02-04 21:17:37 -080088status_t DisplayEventReceiver::getLatestVsyncEventData(
89 ParcelableVsyncEventData* outVsyncEventData) const {
Rachel Leeef2e21f2022-02-01 14:51:34 -080090 if (mEventConnection != nullptr) {
Rachel Leeb9c5a772022-02-04 21:17:37 -080091 auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -080092 if (!status.isOk()) {
93 ALOGE("Failed to get latest vsync event data: %s", status.exceptionMessage().c_str());
94 return status.transactionError();
95 }
Rachel Leeef2e21f2022-02-01 14:51:34 -080096 return NO_ERROR;
97 }
98 return NO_INIT;
99}
100
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
102 size_t count) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700103 return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800104}
105
Dan Stoza6b698e42017-04-03 13:09:08 -0700106ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107 Event* events, size_t count)
108{
Dan Stoza27c81152017-03-31 16:30:42 -0700109 return gui::BitTube::recvObjects(dataChannel, events, count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700110}
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111
Alec Mouri967d5d72020-08-05 12:50:03 -0700112ssize_t DisplayEventReceiver::sendEvents(Event const* events, size_t count) {
113 return DisplayEventReceiver::sendEvents(mDataChannel.get(), events, count);
114}
115
Dan Stoza6b698e42017-04-03 13:09:08 -0700116ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
Mathias Agopian7b5be952012-04-02 17:02:19 -0700117 Event const* events, size_t count)
118{
Dan Stoza27c81152017-03-31 16:30:42 -0700119 return gui::BitTube::sendObjects(dataChannel, events, count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800120}
121
122// ---------------------------------------------------------------------------
123
124}; // namespace android