1 /*
2  * Copyright (C) 2015 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 <gui/DisplayEventReceiver.h>
18 #include <utils/Log.h>
19 #include <utils/Looper.h>
20 
21 namespace android {
22 using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
23 
24 class DisplayEventDispatcher : public LooperCallback {
25 public:
26     explicit DisplayEventDispatcher(const sp<Looper>& looper,
27                                     gui::ISurfaceComposer::VsyncSource vsyncSource =
28                                             gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp,
29                                     EventRegistrationFlags eventRegistration = {},
30                                     const sp<IBinder>& layerHandle = nullptr);
31 
32     status_t initialize();
33     void dispose();
34     status_t scheduleVsync();
35     void injectEvent(const DisplayEventReceiver::Event& event);
36     int getFd() const;
37     virtual int handleEvent(int receiveFd, int events, void* data);
38     status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
39 
40 protected:
41     virtual ~DisplayEventDispatcher() = default;
42 
43 private:
44     sp<Looper> mLooper;
45     DisplayEventReceiver mReceiver;
46     bool mWaitingForVsync;
47     uint32_t mLastVsyncCount;
48     nsecs_t mLastScheduleVsyncTime;
49 
50     std::vector<FrameRateOverride> mFrameRateOverrides;
51 
52     virtual void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
53                                VsyncEventData vsyncEventData) = 0;
54     virtual void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId,
55                                  bool connected) = 0;
56 
57     virtual void dispatchHotplugConnectionError(nsecs_t timestamp, int32_t connectionError) = 0;
58 
59     virtual void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
60                                      nsecs_t vsyncPeriod) = 0;
61     // AChoreographer-specific hook for processing null-events so that looper
62     // can be properly poked.
63     virtual void dispatchNullEvent(nsecs_t timestamp, PhysicalDisplayId displayId) = 0;
64 
65     virtual void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
66                                             std::vector<FrameRateOverride> overrides) = 0;
67 
68     virtual void dispatchHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
69                                            int32_t maxLevel) = 0;
70 
71     bool processPendingEvents(nsecs_t* outTimestamp, PhysicalDisplayId* outDisplayId,
72                               uint32_t* outCount, VsyncEventData* outVsyncEventData);
73 
74     void populateFrameTimelines(const DisplayEventReceiver::Event& event,
75                                 VsyncEventData* outVsyncEventData) const;
76 };
77 } // namespace android
78