blob: ebf7eeb20713f230c74f6f0f2c9e1522b21be3f4 [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
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020020#define LOG_TAG "hwc2-device"
21
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) {
38 std::string str(pretty_function);
39 const char *start = "func = &";
40 size_t p1 = str.find(start);
41 p1 += strlen(start);
42 size_t p2 = str.find(',', p1);
43 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);
66 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
67}
68
69template <typename HookType, HookType func, typename... Args>
70static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
71 Args... args) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +020072 HwcDisplay *display = DrmHwcTwo::GetDisplay(ToDrmHwcTwo(dev), display_handle);
Roman Stratiienko13017522022-01-17 10:35:34 +020073 ALOGV("Display #%" PRIu64 " hook: %s", display_handle,
74 GetFuncName(__PRETTY_FUNCTION__).c_str());
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020075 if (!display)
76 return static_cast<int32_t>(HWC2::Error::BadDisplay);
77
78 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
79}
80
81template <typename HookType, HookType func, typename... Args>
82static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
83 hwc2_layer_t layer_handle, Args... args) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +020084 HwcDisplay *display = DrmHwcTwo::GetDisplay(ToDrmHwcTwo(dev), display_handle);
Roman Stratiienko13017522022-01-17 10:35:34 +020085 ALOGV("Display #%" PRIu64 " Layer: #%" PRIu64 " hook: %s", display_handle,
86 layer_handle, GetFuncName(__PRETTY_FUNCTION__).c_str());
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020087 if (!display)
88 return static_cast<int32_t>(HWC2::Error::BadDisplay);
89
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020090 HwcLayer *layer = display->get_layer(layer_handle);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020091 if (!layer)
92 return static_cast<int32_t>(HWC2::Error::BadLayer);
93
94 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
95}
96
97static int HookDevClose(hw_device_t *dev) {
98 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe
99 auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev);
100 std::unique_ptr<DrmHwcTwo> ctx(ToDrmHwcTwo(hwc2_dev));
101 return 0;
102}
103
104static void HookDevGetCapabilities(hwc2_device_t * /*dev*/, uint32_t *out_count,
105 int32_t * /*out_capabilities*/) {
106 *out_count = 0;
107}
108
109static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device * /*dev*/,
110 int32_t descriptor) {
111 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
112 switch (func) {
113 // Device functions
114 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
115 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
116 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
117 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
118 int32_t *, hwc2_display_t *>);
119 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
120 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
121 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
122 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
123 case HWC2::FunctionDescriptor::Dump:
124 return ToHook<HWC2_PFN_DUMP>(
125 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
126 uint32_t *, char *>);
127 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
128 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
129 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
130 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
131 case HWC2::FunctionDescriptor::RegisterCallback:
132 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
133 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
134 &DrmHwcTwo::RegisterCallback, int32_t,
135 hwc2_callback_data_t, hwc2_function_pointer_t>);
136
137 // Display functions
138 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
139 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
141 &HwcDisplay::AcceptDisplayChanges>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200142 case HWC2::FunctionDescriptor::CreateLayer:
143 return ToHook<HWC2_PFN_CREATE_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200144 DisplayHook<decltype(&HwcDisplay::CreateLayer),
145 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200146 case HWC2::FunctionDescriptor::DestroyLayer:
147 return ToHook<HWC2_PFN_DESTROY_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200148 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
149 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200150 case HWC2::FunctionDescriptor::GetActiveConfig:
151 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200152 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
153 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200154 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
155 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200156 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
157 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
158 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200159 case HWC2::FunctionDescriptor::GetClientTargetSupport:
160 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200161 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
162 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
163 int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200164 case HWC2::FunctionDescriptor::GetColorModes:
165 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200166 DisplayHook<decltype(&HwcDisplay::GetColorModes),
167 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200168 case HWC2::FunctionDescriptor::GetDisplayAttribute:
169 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200170 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
171 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
172 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200173 case HWC2::FunctionDescriptor::GetDisplayConfigs:
174 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200175 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
176 &HwcDisplay::GetDisplayConfigs, uint32_t *,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200177 hwc2_config_t *>);
178 case HWC2::FunctionDescriptor::GetDisplayName:
179 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200180 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
181 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200182 case HWC2::FunctionDescriptor::GetDisplayRequests:
183 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200184 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
185 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
186 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200187 case HWC2::FunctionDescriptor::GetDisplayType:
188 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
190 &HwcDisplay::GetDisplayType, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200191 case HWC2::FunctionDescriptor::GetDozeSupport:
192 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200193 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
194 &HwcDisplay::GetDozeSupport, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200195 case HWC2::FunctionDescriptor::GetHdrCapabilities:
196 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
198 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
199 float *, float *, float *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200200 case HWC2::FunctionDescriptor::GetReleaseFences:
201 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
203 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
204 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200205 case HWC2::FunctionDescriptor::PresentDisplay:
206 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
208 &HwcDisplay::PresentDisplay, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200209 case HWC2::FunctionDescriptor::SetActiveConfig:
210 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
212 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200213 case HWC2::FunctionDescriptor::SetClientTarget:
214 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
216 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
217 int32_t, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200218 case HWC2::FunctionDescriptor::SetColorMode:
219 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220 DisplayHook<decltype(&HwcDisplay::SetColorMode),
221 &HwcDisplay::SetColorMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200222 case HWC2::FunctionDescriptor::SetColorTransform:
223 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200224 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
225 &HwcDisplay::SetColorTransform, const float *, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200226 case HWC2::FunctionDescriptor::SetOutputBuffer:
227 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200228 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
229 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200230 case HWC2::FunctionDescriptor::SetPowerMode:
231 return ToHook<HWC2_PFN_SET_POWER_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
233 &HwcDisplay::SetPowerMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200234 case HWC2::FunctionDescriptor::SetVsyncEnabled:
235 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200236 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
237 &HwcDisplay::SetVsyncEnabled, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200238 case HWC2::FunctionDescriptor::ValidateDisplay:
239 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200240 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
241 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200242#if PLATFORM_SDK_VERSION > 27
243 case HWC2::FunctionDescriptor::GetRenderIntents:
244 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
246 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
247 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200248 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
249 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200250 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
251 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200252#endif
253#if PLATFORM_SDK_VERSION > 28
254 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
255 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200256 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
257 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
258 uint32_t *, uint8_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200259 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
260 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200261 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
262 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
263 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200264 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
265 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200266 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
267 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200268 case HWC2::FunctionDescriptor::SetDisplayBrightness:
269 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200270 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
271 &HwcDisplay::SetDisplayBrightness, float>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200272#endif /* PLATFORM_SDK_VERSION > 28 */
273#if PLATFORM_SDK_VERSION > 29
274 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
275 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200276 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
277 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200278 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
279 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200280 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
281 &HwcDisplay::GetDisplayVsyncPeriod,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200282 hwc2_vsync_period_t *>);
283 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
284 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200285 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
286 &HwcDisplay::SetActiveConfigWithConstraints,
287 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
288 hwc_vsync_period_change_timeline_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200289 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
290 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200291 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
292 &HwcDisplay::SetAutoLowLatencyMode, bool>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200293 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
294 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200295 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
296 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
297 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200298 case HWC2::FunctionDescriptor::SetContentType:
299 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200300 DisplayHook<decltype(&HwcDisplay::SetContentType),
301 &HwcDisplay::SetContentType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200302#endif
303 // Layer functions
304 case HWC2::FunctionDescriptor::SetCursorPosition:
305 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200306 LayerHook<decltype(&HwcLayer::SetCursorPosition),
307 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200308 case HWC2::FunctionDescriptor::SetLayerBlendMode:
309 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200310 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
311 &HwcLayer::SetLayerBlendMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200312 case HWC2::FunctionDescriptor::SetLayerBuffer:
313 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200314 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
315 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200316 case HWC2::FunctionDescriptor::SetLayerColor:
317 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200318 LayerHook<decltype(&HwcLayer::SetLayerColor),
319 &HwcLayer::SetLayerColor, hwc_color_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200320 case HWC2::FunctionDescriptor::SetLayerCompositionType:
321 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200322 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
323 &HwcLayer::SetLayerCompositionType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200324 case HWC2::FunctionDescriptor::SetLayerDataspace:
325 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200326 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
327 &HwcLayer::SetLayerDataspace, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200328 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
329 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200330 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
331 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200332 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
333 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200334 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
335 &HwcLayer::SetLayerPlaneAlpha, float>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200336 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
337 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200338 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
339 &HwcLayer::SetLayerSidebandStream,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200340 const native_handle_t *>);
341 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
342 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200343 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
344 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200345 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
346 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200347 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
348 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200349 case HWC2::FunctionDescriptor::SetLayerTransform:
350 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200351 LayerHook<decltype(&HwcLayer::SetLayerTransform),
352 &HwcLayer::SetLayerTransform, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200353 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
354 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200355 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
356 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200357 case HWC2::FunctionDescriptor::SetLayerZOrder:
358 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200359 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
360 &HwcLayer::SetLayerZOrder, uint32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200361 case HWC2::FunctionDescriptor::Invalid:
362 default:
363 return nullptr;
364 }
365}
366
367static int HookDevOpen(const struct hw_module_t *module, const char *name,
368 struct hw_device_t **dev) {
369 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
370 ALOGE("Invalid module name- %s", name);
371 return -EINVAL;
372 }
373
374 auto ctx = std::make_unique<Drmhwc2Device>();
375 if (!ctx) {
376 ALOGE("Failed to allocate DrmHwcTwo");
377 return -ENOMEM;
378 }
379
380 ctx->common.tag = HARDWARE_DEVICE_TAG;
381 ctx->common.version = HWC_DEVICE_API_VERSION_2_0;
382 ctx->common.close = HookDevClose;
383 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
384 ctx->common.module = (hw_module_t *)module;
385 ctx->getCapabilities = HookDevGetCapabilities;
386 ctx->getFunction = HookDevGetFunction;
387
388 HWC2::Error err = ctx->drmhwctwo.Init();
389 if (err != HWC2::Error::None) {
390 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
391 return -EINVAL;
392 }
393
394 *dev = &ctx.release()->common;
395
396 return 0;
397}
398
399} // namespace android
400
401// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
402static struct hw_module_methods_t hwc2_module_methods = {
403 .open = android::HookDevOpen,
404};
405
406// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
407hw_module_t HAL_MODULE_INFO_SYM = {
408 .tag = HARDWARE_MODULE_TAG,
409 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
410 .id = HWC_HARDWARE_MODULE_ID,
411 .name = "DrmHwcTwo module",
412 .author = "The Android Open Source Project",
413 .methods = &hwc2_module_methods,
414 .dso = nullptr,
415 .reserved = {0},
416};