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/IDisplayEventConnection.h>
23 #include <gui/ISurfaceComposer.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::ConfigChanged configChanged)35 DisplayEventReceiver::DisplayEventReceiver(ISurfaceComposer::VsyncSource vsyncSource,
36                                            ISurfaceComposer::ConfigChanged configChanged) {
37     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
38     if (sf != nullptr) {
39         mEventConnection = sf->createDisplayEventConnection(vsyncSource, configChanged);
40         if (mEventConnection != nullptr) {
41             mDataChannel = std::make_unique<gui::BitTube>();
42             mEventConnection->stealReceiveChannel(mDataChannel.get());
43         }
44     }
45 }
46 
~DisplayEventReceiver()47 DisplayEventReceiver::~DisplayEventReceiver() {
48 }
49 
initCheck() const50 status_t DisplayEventReceiver::initCheck() const {
51     if (mDataChannel != nullptr)
52         return NO_ERROR;
53     return NO_INIT;
54 }
55 
getFd() const56 int DisplayEventReceiver::getFd() const {
57     if (mDataChannel == nullptr)
58         return NO_INIT;
59 
60     return mDataChannel->getFd();
61 }
62 
setVsyncRate(uint32_t count)63 status_t DisplayEventReceiver::setVsyncRate(uint32_t count) {
64     if (int32_t(count) < 0)
65         return BAD_VALUE;
66 
67     if (mEventConnection != nullptr) {
68         mEventConnection->setVsyncRate(count);
69         return NO_ERROR;
70     }
71     return NO_INIT;
72 }
73 
requestNextVsync()74 status_t DisplayEventReceiver::requestNextVsync() {
75     if (mEventConnection != nullptr) {
76         mEventConnection->requestNextVsync();
77         return NO_ERROR;
78     }
79     return NO_INIT;
80 }
81 
82 
getEvents(DisplayEventReceiver::Event * events,size_t count)83 ssize_t DisplayEventReceiver::getEvents(DisplayEventReceiver::Event* events,
84         size_t count) {
85     return DisplayEventReceiver::getEvents(mDataChannel.get(), events, count);
86 }
87 
getEvents(gui::BitTube * dataChannel,Event * events,size_t count)88 ssize_t DisplayEventReceiver::getEvents(gui::BitTube* dataChannel,
89         Event* events, size_t count)
90 {
91     return gui::BitTube::recvObjects(dataChannel, events, count);
92 }
93 
sendEvents(gui::BitTube * dataChannel,Event const * events,size_t count)94 ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
95         Event const* events, size_t count)
96 {
97     return gui::BitTube::sendObjects(dataChannel, events, count);
98 }
99 
100 // ---------------------------------------------------------------------------
101 
102 }; // namespace android
103