blob: 22e4589ec3bdd85a2f6588831aff88187035b9d8 [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
17#define LOG_TAG "hwc2-device"
18
19#include "DrmHwcTwo.h"
20#include "backend/Backend.h"
21#include "utils/log.h"
22
23namespace android {
24
25struct Drmhwc2Device : hwc2_device {
26 DrmHwcTwo drmhwctwo;
27};
28
29static DrmHwcTwo *ToDrmHwcTwo(hwc2_device_t *dev) {
30 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast):
31 return &static_cast<Drmhwc2Device *>(dev)->drmhwctwo;
32}
33
34template <typename PFN, typename T>
35static hwc2_function_pointer_t ToHook(T function) {
36 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
37 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast):
38 return reinterpret_cast<hwc2_function_pointer_t>(function);
39}
40
41template <typename T, typename HookType, HookType func, typename... Args>
42static T DeviceHook(hwc2_device_t *dev, Args... args) {
43 DrmHwcTwo *hwc = ToDrmHwcTwo(dev);
44 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
45}
46
47template <typename HookType, HookType func, typename... Args>
48static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
49 Args... args) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +020050 HwcDisplay *display = DrmHwcTwo::GetDisplay(ToDrmHwcTwo(dev), display_handle);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020051 if (!display)
52 return static_cast<int32_t>(HWC2::Error::BadDisplay);
53
54 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
55}
56
57template <typename HookType, HookType func, typename... Args>
58static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
59 hwc2_layer_t layer_handle, Args... args) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +020060 HwcDisplay *display = DrmHwcTwo::GetDisplay(ToDrmHwcTwo(dev), display_handle);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020061 if (!display)
62 return static_cast<int32_t>(HWC2::Error::BadDisplay);
63
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020064 HwcLayer *layer = display->get_layer(layer_handle);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020065 if (!layer)
66 return static_cast<int32_t>(HWC2::Error::BadLayer);
67
68 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
69}
70
71static int HookDevClose(hw_device_t *dev) {
72 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe
73 auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev);
74 std::unique_ptr<DrmHwcTwo> ctx(ToDrmHwcTwo(hwc2_dev));
75 return 0;
76}
77
78static void HookDevGetCapabilities(hwc2_device_t * /*dev*/, uint32_t *out_count,
79 int32_t * /*out_capabilities*/) {
80 *out_count = 0;
81}
82
83static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device * /*dev*/,
84 int32_t descriptor) {
85 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
86 switch (func) {
87 // Device functions
88 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
89 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
90 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
91 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
92 int32_t *, hwc2_display_t *>);
93 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
94 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
95 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
96 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
97 case HWC2::FunctionDescriptor::Dump:
98 return ToHook<HWC2_PFN_DUMP>(
99 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
100 uint32_t *, char *>);
101 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
102 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
103 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
104 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
105 case HWC2::FunctionDescriptor::RegisterCallback:
106 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
107 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
108 &DrmHwcTwo::RegisterCallback, int32_t,
109 hwc2_callback_data_t, hwc2_function_pointer_t>);
110
111 // Display functions
112 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
113 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200114 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
115 &HwcDisplay::AcceptDisplayChanges>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200116 case HWC2::FunctionDescriptor::CreateLayer:
117 return ToHook<HWC2_PFN_CREATE_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200118 DisplayHook<decltype(&HwcDisplay::CreateLayer),
119 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200120 case HWC2::FunctionDescriptor::DestroyLayer:
121 return ToHook<HWC2_PFN_DESTROY_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200122 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
123 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200124 case HWC2::FunctionDescriptor::GetActiveConfig:
125 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200126 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
127 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200128 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
129 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200130 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
131 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
132 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200133 case HWC2::FunctionDescriptor::GetClientTargetSupport:
134 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200135 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
136 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
137 int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200138 case HWC2::FunctionDescriptor::GetColorModes:
139 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 DisplayHook<decltype(&HwcDisplay::GetColorModes),
141 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200142 case HWC2::FunctionDescriptor::GetDisplayAttribute:
143 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200144 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
145 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
146 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200147 case HWC2::FunctionDescriptor::GetDisplayConfigs:
148 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200149 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
150 &HwcDisplay::GetDisplayConfigs, uint32_t *,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200151 hwc2_config_t *>);
152 case HWC2::FunctionDescriptor::GetDisplayName:
153 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200154 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
155 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200156 case HWC2::FunctionDescriptor::GetDisplayRequests:
157 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200158 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
159 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
160 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200161 case HWC2::FunctionDescriptor::GetDisplayType:
162 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200163 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
164 &HwcDisplay::GetDisplayType, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200165 case HWC2::FunctionDescriptor::GetDozeSupport:
166 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200167 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
168 &HwcDisplay::GetDozeSupport, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200169 case HWC2::FunctionDescriptor::GetHdrCapabilities:
170 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
172 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
173 float *, float *, float *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200174 case HWC2::FunctionDescriptor::GetReleaseFences:
175 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200176 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
177 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
178 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200179 case HWC2::FunctionDescriptor::PresentDisplay:
180 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200181 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
182 &HwcDisplay::PresentDisplay, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200183 case HWC2::FunctionDescriptor::SetActiveConfig:
184 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
186 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200187 case HWC2::FunctionDescriptor::SetClientTarget:
188 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
190 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
191 int32_t, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200192 case HWC2::FunctionDescriptor::SetColorMode:
193 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194 DisplayHook<decltype(&HwcDisplay::SetColorMode),
195 &HwcDisplay::SetColorMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200196 case HWC2::FunctionDescriptor::SetColorTransform:
197 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
199 &HwcDisplay::SetColorTransform, const float *, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200200 case HWC2::FunctionDescriptor::SetOutputBuffer:
201 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
203 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200204 case HWC2::FunctionDescriptor::SetPowerMode:
205 return ToHook<HWC2_PFN_SET_POWER_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200206 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
207 &HwcDisplay::SetPowerMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200208 case HWC2::FunctionDescriptor::SetVsyncEnabled:
209 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200210 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
211 &HwcDisplay::SetVsyncEnabled, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200212 case HWC2::FunctionDescriptor::ValidateDisplay:
213 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200214 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
215 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200216#if PLATFORM_SDK_VERSION > 27
217 case HWC2::FunctionDescriptor::GetRenderIntents:
218 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200219 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
220 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
221 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200222 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
223 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200224 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
225 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200226#endif
227#if PLATFORM_SDK_VERSION > 28
228 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
229 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
231 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
232 uint32_t *, uint8_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200233 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
234 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200235 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
236 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
237 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200238 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
239 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200240 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
241 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200242 case HWC2::FunctionDescriptor::SetDisplayBrightness:
243 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200244 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
245 &HwcDisplay::SetDisplayBrightness, float>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200246#endif /* PLATFORM_SDK_VERSION > 28 */
247#if PLATFORM_SDK_VERSION > 29
248 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
249 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200250 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
251 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200252 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
253 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200254 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
255 &HwcDisplay::GetDisplayVsyncPeriod,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200256 hwc2_vsync_period_t *>);
257 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
258 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200259 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
260 &HwcDisplay::SetActiveConfigWithConstraints,
261 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
262 hwc_vsync_period_change_timeline_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200263 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
264 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200265 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
266 &HwcDisplay::SetAutoLowLatencyMode, bool>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200267 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
268 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200269 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
270 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
271 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200272 case HWC2::FunctionDescriptor::SetContentType:
273 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200274 DisplayHook<decltype(&HwcDisplay::SetContentType),
275 &HwcDisplay::SetContentType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200276#endif
277 // Layer functions
278 case HWC2::FunctionDescriptor::SetCursorPosition:
279 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200280 LayerHook<decltype(&HwcLayer::SetCursorPosition),
281 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200282 case HWC2::FunctionDescriptor::SetLayerBlendMode:
283 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200284 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
285 &HwcLayer::SetLayerBlendMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200286 case HWC2::FunctionDescriptor::SetLayerBuffer:
287 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200288 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
289 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200290 case HWC2::FunctionDescriptor::SetLayerColor:
291 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200292 LayerHook<decltype(&HwcLayer::SetLayerColor),
293 &HwcLayer::SetLayerColor, hwc_color_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200294 case HWC2::FunctionDescriptor::SetLayerCompositionType:
295 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200296 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
297 &HwcLayer::SetLayerCompositionType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200298 case HWC2::FunctionDescriptor::SetLayerDataspace:
299 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200300 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
301 &HwcLayer::SetLayerDataspace, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200302 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
303 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200304 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
305 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200306 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
307 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200308 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
309 &HwcLayer::SetLayerPlaneAlpha, float>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200310 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
311 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200312 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
313 &HwcLayer::SetLayerSidebandStream,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200314 const native_handle_t *>);
315 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
316 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200317 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
318 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200319 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
320 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200321 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
322 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200323 case HWC2::FunctionDescriptor::SetLayerTransform:
324 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200325 LayerHook<decltype(&HwcLayer::SetLayerTransform),
326 &HwcLayer::SetLayerTransform, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200327 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
328 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200329 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
330 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200331 case HWC2::FunctionDescriptor::SetLayerZOrder:
332 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200333 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
334 &HwcLayer::SetLayerZOrder, uint32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200335 case HWC2::FunctionDescriptor::Invalid:
336 default:
337 return nullptr;
338 }
339}
340
341static int HookDevOpen(const struct hw_module_t *module, const char *name,
342 struct hw_device_t **dev) {
343 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
344 ALOGE("Invalid module name- %s", name);
345 return -EINVAL;
346 }
347
348 auto ctx = std::make_unique<Drmhwc2Device>();
349 if (!ctx) {
350 ALOGE("Failed to allocate DrmHwcTwo");
351 return -ENOMEM;
352 }
353
354 ctx->common.tag = HARDWARE_DEVICE_TAG;
355 ctx->common.version = HWC_DEVICE_API_VERSION_2_0;
356 ctx->common.close = HookDevClose;
357 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
358 ctx->common.module = (hw_module_t *)module;
359 ctx->getCapabilities = HookDevGetCapabilities;
360 ctx->getFunction = HookDevGetFunction;
361
362 HWC2::Error err = ctx->drmhwctwo.Init();
363 if (err != HWC2::Error::None) {
364 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
365 return -EINVAL;
366 }
367
368 *dev = &ctx.release()->common;
369
370 return 0;
371}
372
373} // namespace android
374
375// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
376static struct hw_module_methods_t hwc2_module_methods = {
377 .open = android::HookDevOpen,
378};
379
380// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
381hw_module_t HAL_MODULE_INFO_SYM = {
382 .tag = HARDWARE_MODULE_TAG,
383 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
384 .id = HWC_HARDWARE_MODULE_ID,
385 .name = "DrmHwcTwo module",
386 .author = "The Android Open Source Project",
387 .methods = &hwc2_module_methods,
388 .dso = nullptr,
389 .reserved = {0},
390};