blob: 28b6963d22c369e33b424c47d7690ba7e1da75e4 [file] [log] [blame]
Roman Stratiienko26fd2b22022-01-04 12:59:29 +02001/*
2 * Copyright (C) 2022 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
Roman Stratiienko13017522022-01-17 10:35:34 +020017// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
18// #define LOG_NDEBUG 0 // Uncomment to see HWC2 API calls in logcat
19
Sean Paul468a7542024-07-16 19:50:58 +000020#define LOG_TAG "drmhwc"
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020021
Roman Stratiienko13017522022-01-17 10:35:34 +020022#include <cinttypes>
23
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020024#include "DrmHwcTwo.h"
25#include "backend/Backend.h"
26#include "utils/log.h"
27
28namespace android {
29
Roman Stratiienko13017522022-01-17 10:35:34 +020030/* Converts long __PRETTY_FUNCTION__ result, e.g.:
31 * "int32_t android::LayerHook(hwc2_device_t *, hwc2_display_t, hwc2_layer_t,"
32 * "Args...) [HookType = HWC2::Error (android::HwcLayer::*)(const native_handle"
33 * "*,int), func = &android::HwcLayer::SetLayerBuffer, Args = <const
34 * "native_handle, int>"
35 * to the short "android::HwcLayer::SetLayerBuffer" for better logs readability
36 */
37static std::string GetFuncName(const char *pretty_function) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030038 const std::string str(pretty_function);
Roman Stratiienko13017522022-01-17 10:35:34 +020039 const char *start = "func = &";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030040 auto p1 = str.find(start);
Roman Stratiienko13017522022-01-17 10:35:34 +020041 p1 += strlen(start);
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030042 auto p2 = str.find(',', p1);
Roman Stratiienko13017522022-01-17 10:35:34 +020043 return str.substr(p1, p2 - p1);
44}
45
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020046struct Drmhwc2Device : hwc2_device {
47 DrmHwcTwo drmhwctwo;
48};
49
50static DrmHwcTwo *ToDrmHwcTwo(hwc2_device_t *dev) {
51 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast):
52 return &static_cast<Drmhwc2Device *>(dev)->drmhwctwo;
53}
54
55template <typename PFN, typename T>
56static hwc2_function_pointer_t ToHook(T function) {
57 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
58 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast):
59 return reinterpret_cast<hwc2_function_pointer_t>(function);
60}
61
62template <typename T, typename HookType, HookType func, typename... Args>
63static T DeviceHook(hwc2_device_t *dev, Args... args) {
Roman Stratiienko13017522022-01-17 10:35:34 +020064 ALOGV("Device hook: %s", GetFuncName(__PRETTY_FUNCTION__).c_str());
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020065 DrmHwcTwo *hwc = ToDrmHwcTwo(dev);
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +020066 const std::unique_lock lock(hwc->GetResMan().GetMainLock());
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020067 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
68}
69
70template <typename HookType, HookType func, typename... Args>
71static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
72 Args... args) {
Roman Stratiienko13017522022-01-17 10:35:34 +020073 ALOGV("Display #%" PRIu64 " hook: %s", display_handle,
74 GetFuncName(__PRETTY_FUNCTION__).c_str());
Roman Stratiienko74923582022-01-17 11:24:21 +020075 DrmHwcTwo *hwc = ToDrmHwcTwo(dev);
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +020076 const std::unique_lock lock(hwc->GetResMan().GetMainLock());
Roman Stratiienko938a7422022-01-29 00:10:07 +020077 auto *display = hwc->GetDisplay(display_handle);
78 if (display == nullptr)
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020079 return static_cast<int32_t>(HWC2::Error::BadDisplay);
80
81 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
82}
83
84template <typename HookType, HookType func, typename... Args>
85static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
86 hwc2_layer_t layer_handle, Args... args) {
Roman Stratiienko13017522022-01-17 10:35:34 +020087 ALOGV("Display #%" PRIu64 " Layer: #%" PRIu64 " hook: %s", display_handle,
88 layer_handle, GetFuncName(__PRETTY_FUNCTION__).c_str());
Roman Stratiienko74923582022-01-17 11:24:21 +020089 DrmHwcTwo *hwc = ToDrmHwcTwo(dev);
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +020090 const std::unique_lock lock(hwc->GetResMan().GetMainLock());
Roman Stratiienko938a7422022-01-29 00:10:07 +020091 auto *display = hwc->GetDisplay(display_handle);
92 if (display == nullptr)
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020093 return static_cast<int32_t>(HWC2::Error::BadDisplay);
94
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020095 HwcLayer *layer = display->get_layer(layer_handle);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020096 if (!layer)
97 return static_cast<int32_t>(HWC2::Error::BadLayer);
98
99 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
100}
101
102static int HookDevClose(hw_device_t *dev) {
103 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe
104 auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev);
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300105 const std::unique_ptr<DrmHwcTwo> ctx(ToDrmHwcTwo(hwc2_dev));
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200106 return 0;
107}
108
109static void HookDevGetCapabilities(hwc2_device_t * /*dev*/, uint32_t *out_count,
110 int32_t * /*out_capabilities*/) {
111 *out_count = 0;
112}
113
114static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device * /*dev*/,
115 int32_t descriptor) {
116 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
117 switch (func) {
118 // Device functions
119 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
120 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
121 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
122 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
123 int32_t *, hwc2_display_t *>);
124 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
125 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
126 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
127 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
128 case HWC2::FunctionDescriptor::Dump:
129 return ToHook<HWC2_PFN_DUMP>(
130 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
131 uint32_t *, char *>);
132 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
133 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
134 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
135 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
136 case HWC2::FunctionDescriptor::RegisterCallback:
137 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
138 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
139 &DrmHwcTwo::RegisterCallback, int32_t,
140 hwc2_callback_data_t, hwc2_function_pointer_t>);
141
142 // Display functions
143 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
144 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200145 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
146 &HwcDisplay::AcceptDisplayChanges>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200147 case HWC2::FunctionDescriptor::CreateLayer:
148 return ToHook<HWC2_PFN_CREATE_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200149 DisplayHook<decltype(&HwcDisplay::CreateLayer),
150 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200151 case HWC2::FunctionDescriptor::DestroyLayer:
152 return ToHook<HWC2_PFN_DESTROY_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200153 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
154 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200155 case HWC2::FunctionDescriptor::GetActiveConfig:
156 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200157 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
158 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200159 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
160 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200161 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
162 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
163 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200164 case HWC2::FunctionDescriptor::GetClientTargetSupport:
165 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200166 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
167 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
168 int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200169 case HWC2::FunctionDescriptor::GetColorModes:
170 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171 DisplayHook<decltype(&HwcDisplay::GetColorModes),
172 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200173 case HWC2::FunctionDescriptor::GetDisplayAttribute:
174 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200175 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
176 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
177 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200178 case HWC2::FunctionDescriptor::GetDisplayConfigs:
179 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
Drew Davenportf7e88332024-09-06 12:54:38 -0600180 DisplayHook<decltype(&HwcDisplay::LegacyGetDisplayConfigs),
181 &HwcDisplay::LegacyGetDisplayConfigs, uint32_t *,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200182 hwc2_config_t *>);
183 case HWC2::FunctionDescriptor::GetDisplayName:
184 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
186 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200187 case HWC2::FunctionDescriptor::GetDisplayRequests:
188 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
190 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
191 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200192 case HWC2::FunctionDescriptor::GetDisplayType:
193 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
195 &HwcDisplay::GetDisplayType, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200196 case HWC2::FunctionDescriptor::GetDozeSupport:
197 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
199 &HwcDisplay::GetDozeSupport, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200200 case HWC2::FunctionDescriptor::GetHdrCapabilities:
201 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
203 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
204 float *, float *, float *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200205 case HWC2::FunctionDescriptor::GetReleaseFences:
206 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
208 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
209 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200210 case HWC2::FunctionDescriptor::PresentDisplay:
211 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200212 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
213 &HwcDisplay::PresentDisplay, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200214 case HWC2::FunctionDescriptor::SetActiveConfig:
215 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
217 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200218 case HWC2::FunctionDescriptor::SetClientTarget:
219 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
221 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
222 int32_t, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200223 case HWC2::FunctionDescriptor::SetColorMode:
224 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200225 DisplayHook<decltype(&HwcDisplay::SetColorMode),
226 &HwcDisplay::SetColorMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200227 case HWC2::FunctionDescriptor::SetColorTransform:
228 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200229 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
230 &HwcDisplay::SetColorTransform, const float *, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200231 case HWC2::FunctionDescriptor::SetOutputBuffer:
232 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
234 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200235 case HWC2::FunctionDescriptor::SetPowerMode:
236 return ToHook<HWC2_PFN_SET_POWER_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200237 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
238 &HwcDisplay::SetPowerMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200239 case HWC2::FunctionDescriptor::SetVsyncEnabled:
240 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200241 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
242 &HwcDisplay::SetVsyncEnabled, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200243 case HWC2::FunctionDescriptor::ValidateDisplay:
244 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
246 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200247#if __ANDROID_API__ > 27
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200248 case HWC2::FunctionDescriptor::GetRenderIntents:
249 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200250 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
251 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
252 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200253 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
254 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200255 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
256 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200257#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200258#if __ANDROID_API__ > 28
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200259 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
260 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200261 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
262 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
263 uint32_t *, uint8_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200264 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
265 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200266 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
267 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
268 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200269 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
270 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200271 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
272 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200273 case HWC2::FunctionDescriptor::SetDisplayBrightness:
274 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200275 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
276 &HwcDisplay::SetDisplayBrightness, float>);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200277#endif /* __ANDROID_API__ > 28 */
278#if __ANDROID_API__ > 29
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200279 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
280 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
282 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200283 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
284 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
286 &HwcDisplay::GetDisplayVsyncPeriod,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200287 hwc2_vsync_period_t *>);
288 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
289 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200290 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
291 &HwcDisplay::SetActiveConfigWithConstraints,
292 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
293 hwc_vsync_period_change_timeline_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200294 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
295 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200296 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
297 &HwcDisplay::SetAutoLowLatencyMode, bool>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200298 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
299 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200300 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
301 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
302 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200303 case HWC2::FunctionDescriptor::SetContentType:
304 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200305 DisplayHook<decltype(&HwcDisplay::SetContentType),
306 &HwcDisplay::SetContentType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200307#endif
308 // Layer functions
309 case HWC2::FunctionDescriptor::SetCursorPosition:
310 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200311 LayerHook<decltype(&HwcLayer::SetCursorPosition),
312 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200313 case HWC2::FunctionDescriptor::SetLayerBlendMode:
314 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200315 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
316 &HwcLayer::SetLayerBlendMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200317 case HWC2::FunctionDescriptor::SetLayerBuffer:
318 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200319 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
320 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200321 case HWC2::FunctionDescriptor::SetLayerColor:
322 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200323 LayerHook<decltype(&HwcLayer::SetLayerColor),
324 &HwcLayer::SetLayerColor, hwc_color_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200325 case HWC2::FunctionDescriptor::SetLayerCompositionType:
326 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200327 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
328 &HwcLayer::SetLayerCompositionType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200329 case HWC2::FunctionDescriptor::SetLayerDataspace:
330 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200331 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
332 &HwcLayer::SetLayerDataspace, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200333 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
334 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200335 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
336 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200337 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
338 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200339 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
340 &HwcLayer::SetLayerPlaneAlpha, float>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200341 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
342 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200343 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
344 &HwcLayer::SetLayerSidebandStream,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200345 const native_handle_t *>);
346 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
347 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200348 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
349 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200350 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
351 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200352 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
353 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200354 case HWC2::FunctionDescriptor::SetLayerTransform:
355 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200356 LayerHook<decltype(&HwcLayer::SetLayerTransform),
357 &HwcLayer::SetLayerTransform, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200358 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
359 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200360 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
361 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200362 case HWC2::FunctionDescriptor::SetLayerZOrder:
363 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200364 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
365 &HwcLayer::SetLayerZOrder, uint32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200366 case HWC2::FunctionDescriptor::Invalid:
367 default:
368 return nullptr;
369 }
370}
371
372static int HookDevOpen(const struct hw_module_t *module, const char *name,
373 struct hw_device_t **dev) {
374 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
375 ALOGE("Invalid module name- %s", name);
376 return -EINVAL;
377 }
378
379 auto ctx = std::make_unique<Drmhwc2Device>();
380 if (!ctx) {
381 ALOGE("Failed to allocate DrmHwcTwo");
382 return -ENOMEM;
383 }
384
385 ctx->common.tag = HARDWARE_DEVICE_TAG;
386 ctx->common.version = HWC_DEVICE_API_VERSION_2_0;
387 ctx->common.close = HookDevClose;
388 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
389 ctx->common.module = (hw_module_t *)module;
390 ctx->getCapabilities = HookDevGetCapabilities;
391 ctx->getFunction = HookDevGetFunction;
392
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200393 *dev = &ctx.release()->common;
394
395 return 0;
396}
397
398} // namespace android
399
400// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
401static struct hw_module_methods_t hwc2_module_methods = {
402 .open = android::HookDevOpen,
403};
404
405// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
406hw_module_t HAL_MODULE_INFO_SYM = {
407 .tag = HARDWARE_MODULE_TAG,
408 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
409 .id = HWC_HARDWARE_MODULE_ID,
410 .name = "DrmHwcTwo module",
411 .author = "The Android Open Source Project",
412 .methods = &hwc2_module_methods,
413 .dso = nullptr,
414 .reserved = {0},
415};