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
21 #include <gui/DisplayEventReceiver.h>
22 #include <gui/ISurfaceComposer.h>
23 #include <gui/VsyncEventData.h>
24
25 #include <private/gui/ComposerService.h>
26
27 #include <private/gui/BitTube.h>
28
29 // ---------------------------------------------------------------------------
30
31 namespace android {
32
33 // ---------------------------------------------------------------------------
34
DisplayEventReceiver(ISurfaceComposer::VsyncSource vsyncSource,ISurfaceComposer::EventRegistrationFlags eventRegistration)35 DisplayEventReceiver::DisplayEventReceiver(
36 ISurfaceComposer::VsyncSource vsyncSource,
37 ISurfaceComposer::EventRegistrationFlags eventRegistration) {
38 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
39 if (sf != nullptr) {
40 mEventConnection = sf->createDisplayEventConnection(vsyncSource, eventRegistration);
41 if (mEventConnection != nullptr) {
42 mDataChannel = std::make_unique<gui::BitTube>();
43 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 }
50 }
51 }
52 }
53
~DisplayEventReceiver()54 DisplayEventReceiver::~DisplayEventReceiver() {
55 }
56
initCheck() const57 status_t DisplayEventReceiver::initCheck() const {
58 if (mDataChannel != nullptr)
59 return NO_ERROR;
60 return mInitError.has_value() ? mInitError.value() : NO_INIT;
61 }
62
getFd() const63 int DisplayEventReceiver::getFd() const {
64 if (mDataChannel == nullptr) return mInitError.has_value() ? mInitError.value() : NO_INIT;
65
66 return mDataChannel->getFd();
67 }
68
setVsyncRate(uint32_t count)69 status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
70 if (int32_t(count) < 0)
71 return BAD_VALUE;
72
73 if (mEventConnection != nullptr) {
74 mEventConnection->setVsyncRate(count);
75 return NO_ERROR;
76 }
77 return mInitError.has_value() ? mInitError.value() : NO_INIT;
78 }
79
requestNextVsync()80 status_t DisplayEventReceiver::requestNextVsync() {
81 if (mEventConnection != nullptr) {
82 mEventConnection->requestNextVsync();
83 return NO_ERROR;
84 }
85 return mInitError.has_value() ? mInitError.value() : NO_INIT;
86 }
87
getLatestVsyncEventData(ParcelableVsyncEventData * outVsyncEventData) const88 status_t DisplayEventReceiver::getLatestVsyncEventData(
89 ParcelableVsyncEventData* outVsyncEventData) const {
90 if (mEventConnection != nullptr) {
91 auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData);
92 if (!status.isOk()) {
93 ALOGE("Failed to get latest vsync event data: %s", status.exceptionMessage().c_str());
94 return status.transactionError();
95 }
96 return NO_ERROR;
97 }
98 return NO_INIT;
99 }
100
getEvents(DisplayEventReceiver::Event * events,size_t count)101 ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
102 size_t count) {
103 return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count);
104 }
105
getEvents(gui::BitTube * dataChannel,Event * events,size_t count)106 ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel,
107 Event* events, size_t count)
108 {
109 return gui::BitTube::recvObjects(dataChannel, events, count);
110 }
111
sendEvents(Event const * events,size_t count)112 ssize_t DisplayEventReceiver::sendEvents(Event const* events, size_t count) {
113 return DisplayEventReceiver::sendEvents(mDataChannel.get(), events, count);
114 }
115
sendEvents(gui::BitTube * dataChannel,Event const * events,size_t count)116 ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
117 Event const* events, size_t count)
118 {
119 return gui::BitTube::sendObjects(dataChannel, events, count);
120 }
121
122 // ---------------------------------------------------------------------------
123
124 }; // namespace android
125