1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <functional>
20 #include <sstream>
21 #include <string>
22 
23 #include "../Utils.h"
24 #include "DisplayContextProvider.h"
25 
26 namespace android::hardware::graphics::composer {
27 
28 enum class VrrControllerEventType {
29     kGeneralEventMask = 0x100,
30     // kSystemRenderingTimeout is responsible for managing present timeout according to the
31     // configuration specified in the system HAL API.
32     kSystemRenderingTimeout,
33     // kVendorRenderingTimeout is responsible for managing present timeout based on the vendor's
34     // proprietary definition.
35     kVendorRenderingTimeout,
36     // kHandleVendorRenderingTimeout is responsible for addressing present timeout by invoking
37     // the handling function provided by the vendor.
38     kHandleVendorRenderingTimeout,
39     kHibernateTimeout,
40     kNotifyExpectedPresentConfig,
41     kGeneralEventMax = 0x1FF,
42     // General callback events.
43     kCallbackEventMask = 0x200,
44     kRefreshRateCalculatorUpdateMask = 0x200,
45     kInstantRefreshRateCalculatorUpdate,
46     kPeriodRefreshRateCalculatorUpdate,
47     kVideoFrameRateCalculatorUpdate,
48     kCombinedRefreshRateCalculatorUpdate,
49     kAodRefreshRateCalculatorUpdate,
50     kExitIdleRefreshRateCalculatorUpdate,
51     kStaticticUpdate,
52     kMinLockTimeForPeakRefreshRate,
53     kCallbackEventMax = 0x2FF,
54     // Sensors, outer events...
55 };
56 
57 struct TimedEvent {
TimedEventTimedEvent58     explicit TimedEvent(const std::string& name) : mEventName(std::move(name)) {}
59 
TimedEventTimedEvent60     TimedEvent(const std::string& name, int64_t whenNs) : mEventName(name), mWhenNs(whenNs) {}
61 
TimedEventTimedEvent62     TimedEvent(std::string& name, int64_t whenNs) : mEventName(name), mWhenNs(whenNs) {}
63 
64     bool operator<(const TimedEvent& b) const { return mWhenNs > b.mWhenNs; }
65 
66     std::string mEventName;
67     std::function<int()> mFunctor;
68     bool mIsRelativeTime = true;
69     int64_t mWhenNs = 0;
70 };
71 
72 struct VrrControllerEvent {
73     bool operator<(const VrrControllerEvent& b) const { return mWhenNs > b.mWhenNs; }
getNameVrrControllerEvent74     std::string getName() const {
75         switch (mEventType) {
76             case VrrControllerEventType::kSystemRenderingTimeout:
77                 return "kSystemRenderingTimeout";
78             case VrrControllerEventType::kVendorRenderingTimeout:
79                 return "kVendorRenderingTimeout";
80             case VrrControllerEventType::kHandleVendorRenderingTimeout:
81                 return "kHandleVendorRenderingTimeout";
82             case VrrControllerEventType::kHibernateTimeout:
83                 return "kHibernateTimeout";
84             case VrrControllerEventType::kNotifyExpectedPresentConfig:
85                 return "kNotifyExpectedPresentConfig";
86             case VrrControllerEventType::kInstantRefreshRateCalculatorUpdate:
87                 return "kInstantRefreshRateCalculatorUpdate";
88             case VrrControllerEventType::kPeriodRefreshRateCalculatorUpdate:
89                 return "kPeriodRefreshRateCalculatorUpdate";
90             case VrrControllerEventType::kVideoFrameRateCalculatorUpdate:
91                 return "kVideoFrameRateCalculatorUpdate";
92             case VrrControllerEventType::kCombinedRefreshRateCalculatorUpdate:
93                 return "kCombinedRefreshRateCalculatorUpdate";
94             case VrrControllerEventType::kAodRefreshRateCalculatorUpdate:
95                 return "kAodRefreshRateCalculatorUpdate";
96             case VrrControllerEventType::kStaticticUpdate:
97                 return "kStaticticUpdate";
98             case VrrControllerEventType::kMinLockTimeForPeakRefreshRate:
99                 return "kMinLockTimeForPeakRefreshRate";
100             default:
101                 return "Unknown";
102         }
103     }
104 
toStringVrrControllerEvent105     std::string toString() const {
106         std::ostringstream os;
107         os << "Vrr event: [";
108         os << "type = " << getName() << ", ";
109         os << "when = " << mWhenNs << "ns]";
110         return os.str();
111     }
112     int64_t mDisplay;
113     VrrControllerEventType mEventType;
114     int64_t mWhenNs;
115     std::function<int()> mFunctor;
116     int64_t mPeriodNs = -1;
117 };
118 
119 class ExternalEventHandler {
120 public:
121     virtual ~ExternalEventHandler() = default;
122 
123     virtual std::vector<TimedEvent> getHandleEvents() = 0;
124 
125     virtual std::function<int()> getHandleFunction() = 0;
126 
127     virtual int64_t getPresentTimeoutNs() = 0;
128 };
129 
130 } // namespace android::hardware::graphics::composer
131 
132 typedef android::hardware::graphics::composer::ExternalEventHandler* (
133         *createExternalEventHandler_t)(void* interface, void* host, const char* panelName);
134 
135 typedef void (*destroyExternalEventHandler_t)(
136         android::hardware::graphics::composer::ExternalEventHandler* handler);
137