blob: c7bce5f475ddf12f187807f53a19dc3bbf54e6b8 [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
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020084static int HookDevClose(hw_device_t *dev) {
85 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe
86 auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev);
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030087 const std::unique_ptr<DrmHwcTwo> ctx(ToDrmHwcTwo(hwc2_dev));
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020088 return 0;
89}
90
91static void HookDevGetCapabilities(hwc2_device_t * /*dev*/, uint32_t *out_count,
92 int32_t * /*out_capabilities*/) {
93 *out_count = 0;
94}
95
Roman Stratiienko41cd9502025-01-22 16:48:58 +020096// NOLINTBEGIN(cppcoreguidelines-macro-usage)
97
98#define LOCK_COMPOSER(dev) \
99 auto *ihwc = ToDrmHwcTwo(dev); \
100 const std::unique_lock lock(ihwc->GetResMan().GetMainLock());
101
102#define GET_DISPLAY(display_id) \
103 auto *idisplay = ihwc->GetDisplay(display_id); \
104 if (!idisplay) \
105 return static_cast<int32_t>(HWC2::Error::BadDisplay);
106
107#define GET_LAYER(layer_id) \
108 auto *ilayer = idisplay->get_layer(layer_id); \
109 if (!ilayer) \
110 return static_cast<int32_t>(HWC2::Error::BadLayer);
111
112// NOLINTEND(cppcoreguidelines-macro-usage)
113
114static BufferColorSpace Hwc2ToColorSpace(int32_t dataspace) {
115 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
116 case HAL_DATASPACE_STANDARD_BT709:
117 return BufferColorSpace::kItuRec709;
118 case HAL_DATASPACE_STANDARD_BT601_625:
119 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
120 case HAL_DATASPACE_STANDARD_BT601_525:
121 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
122 return BufferColorSpace::kItuRec601;
123 case HAL_DATASPACE_STANDARD_BT2020:
124 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
125 return BufferColorSpace::kItuRec2020;
126 default:
127 return BufferColorSpace::kUndefined;
128 }
129}
130
131static BufferSampleRange Hwc2ToSampleRange(int32_t dataspace) {
132 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
133 case HAL_DATASPACE_RANGE_FULL:
134 return BufferSampleRange::kFullRange;
135 case HAL_DATASPACE_RANGE_LIMITED:
136 return BufferSampleRange::kLimitedRange;
137 default:
138 return BufferSampleRange::kUndefined;
139 }
140}
141
142static int32_t SetLayerBlendMode(hwc2_device_t *device, hwc2_display_t display,
143 hwc2_layer_t layer,
144 int32_t /*hwc2_blend_mode_t*/ mode) {
145 ALOGV("SetLayerBlendMode");
146 LOCK_COMPOSER(device);
147 GET_DISPLAY(display);
148 GET_LAYER(layer);
149
150 BufferBlendMode blend_mode{};
151 switch (static_cast<HWC2::BlendMode>(mode)) {
152 case HWC2::BlendMode::None:
153 blend_mode = BufferBlendMode::kNone;
154 break;
155 case HWC2::BlendMode::Premultiplied:
156 blend_mode = BufferBlendMode::kPreMult;
157 break;
158 case HWC2::BlendMode::Coverage:
159 blend_mode = BufferBlendMode::kCoverage;
160 break;
161 default:
162 ALOGE("Unknown blending mode b=%d", mode);
163 blend_mode = BufferBlendMode::kUndefined;
164 break;
165 }
166
167 HwcLayer::LayerProperties layer_properties;
168 layer_properties.blend_mode = blend_mode;
169
170 ilayer->SetLayerProperties(layer_properties);
171
172 return 0;
173}
174
175static int32_t SetLayerBuffer(hwc2_device_t *device, hwc2_display_t display,
176 hwc2_layer_t layer, buffer_handle_t buffer,
177 int32_t acquire_fence) {
178 ALOGV("SetLayerBuffer");
179 LOCK_COMPOSER(device);
180 GET_DISPLAY(display);
181 GET_LAYER(layer);
182
183 HwcLayer::LayerProperties layer_properties;
184 layer_properties.buffer = {.buffer_handle = buffer,
185 .acquire_fence = MakeSharedFd(acquire_fence)};
186 ilayer->SetLayerProperties(layer_properties);
187
188 return 0;
189}
190
191static int32_t SetLayerDataspace(hwc2_device_t *device, hwc2_display_t display,
192 hwc2_layer_t layer,
193 int32_t /*android_dataspace_t*/ dataspace) {
194 ALOGV("SetLayerDataspace");
195 LOCK_COMPOSER(device);
196 GET_DISPLAY(display);
197 GET_LAYER(layer);
198
199 HwcLayer::LayerProperties layer_properties;
200 layer_properties.color_space = Hwc2ToColorSpace(dataspace);
201 layer_properties.sample_range = Hwc2ToSampleRange(dataspace);
202 ilayer->SetLayerProperties(layer_properties);
203 return 0;
204}
205
206static int32_t SetCursorPosition(hwc2_device_t * /*device*/,
207 hwc2_display_t /*display*/,
208 hwc2_layer_t /*layer*/, int32_t /*x*/,
209 int32_t /*y*/) {
210 ALOGV("SetCursorPosition");
211 return 0;
212}
213
214static int32_t SetLayerColor(hwc2_device_t * /*device*/,
215 hwc2_display_t /*display*/, hwc2_layer_t /*layer*/,
216 hwc_color_t /*color*/) {
217 ALOGV("SetLayerColor");
218 return 0;
219}
220
221static int32_t SetLayerCompositionType(hwc2_device_t *device,
222 hwc2_display_t display,
223 hwc2_layer_t layer,
224 int32_t /*hwc2_composition_t*/ type) {
225 ALOGV("SetLayerCompositionType");
226 LOCK_COMPOSER(device);
227 GET_DISPLAY(display);
228 GET_LAYER(layer);
229
230 HwcLayer::LayerProperties layer_properties;
231 layer_properties.composition_type = static_cast<HWC2::Composition>(type);
232 ilayer->SetLayerProperties(layer_properties);
233
234 return 0;
235}
236
237static int32_t SetLayerDisplayFrame(hwc2_device_t *device,
238 hwc2_display_t display, hwc2_layer_t layer,
239 hwc_rect_t frame) {
240 ALOGV("SetLayerDisplayFrame");
241 LOCK_COMPOSER(device);
242 GET_DISPLAY(display);
243 GET_LAYER(layer);
244
245 HwcLayer::LayerProperties layer_properties;
246 layer_properties.display_frame = frame;
247 ilayer->SetLayerProperties(layer_properties);
248
249 return 0;
250}
251
252static int32_t SetLayerPlaneAlpha(hwc2_device_t *device, hwc2_display_t display,
253 hwc2_layer_t layer, float alpha) {
254 ALOGV("SetLayerPlaneAlpha");
255 LOCK_COMPOSER(device);
256 GET_DISPLAY(display);
257 GET_LAYER(layer);
258
259 HwcLayer::LayerProperties layer_properties;
260 layer_properties.alpha = alpha;
261 ilayer->SetLayerProperties(layer_properties);
262
263 return 0;
264}
265
266static int32_t SetLayerSidebandStream(hwc2_device_t * /*device*/,
267 hwc2_display_t /*display*/,
268 hwc2_layer_t /*layer*/,
269 const native_handle_t * /*stream*/) {
270 ALOGV("SetLayerSidebandStream");
271 return static_cast<int32_t>(HWC2::Error::Unsupported);
272}
273
274static int32_t SetLayerSourceCrop(hwc2_device_t *device, hwc2_display_t display,
275 hwc2_layer_t layer, hwc_frect_t crop) {
276 ALOGV("SetLayerSourceCrop");
277 LOCK_COMPOSER(device);
278 GET_DISPLAY(display);
279 GET_LAYER(layer);
280
281 HwcLayer::LayerProperties layer_properties;
282 layer_properties.source_crop = crop;
283 ilayer->SetLayerProperties(layer_properties);
284
285 return 0;
286}
287
288static int32_t SetLayerSurfaceDamage(hwc2_device_t * /*device*/,
289 hwc2_display_t /*display*/,
290 hwc2_layer_t /*layer*/,
291 hwc_region_t /*damage*/) {
292 ALOGV("SetLayerSurfaceDamage");
293 return 0;
294}
295
296static int32_t SetLayerTransform(hwc2_device_t *device, hwc2_display_t display,
297 hwc2_layer_t layer, int32_t transform) {
298 ALOGV("SetLayerTransform");
299 LOCK_COMPOSER(device);
300 GET_DISPLAY(display);
301 GET_LAYER(layer);
302
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200303 HwcLayer::LayerProperties layer_properties;
Roman Stratiienkoda2fcf62025-01-23 17:47:03 +0200304 layer_properties.transform = {
305 .hflip = (transform & HAL_TRANSFORM_FLIP_H) != 0,
306 .vflip = (transform & HAL_TRANSFORM_FLIP_V) != 0,
307 .rotate90 = (transform & HAL_TRANSFORM_ROT_90) != 0,
308 };
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200309 ilayer->SetLayerProperties(layer_properties);
310
311 return 0;
312}
313
314static int32_t SetLayerVisibleRegion(hwc2_device_t * /*device*/,
315 hwc2_display_t /*display*/,
316 hwc2_layer_t /*layer*/,
317 hwc_region_t /*visible*/) {
318 ALOGV("SetLayerVisibleRegion");
319 return 0;
320}
321
322static int32_t SetLayerZOrder(hwc2_device_t *device, hwc2_display_t display,
323 hwc2_layer_t layer, uint32_t z) {
324 ALOGV("SetLayerZOrder");
325 LOCK_COMPOSER(device);
326 GET_DISPLAY(display);
327 GET_LAYER(layer);
328
329 HwcLayer::LayerProperties layer_properties;
330 layer_properties.z_order = z;
331 ilayer->SetLayerProperties(layer_properties);
332
333 return 0;
334}
335
336/* Entry point for the HWC2 API */
337// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast)
338
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200339static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device * /*dev*/,
340 int32_t descriptor) {
341 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
342 switch (func) {
343 // Device functions
344 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
345 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
346 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
347 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
348 int32_t *, hwc2_display_t *>);
349 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
350 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
351 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
352 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
353 case HWC2::FunctionDescriptor::Dump:
354 return ToHook<HWC2_PFN_DUMP>(
355 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
356 uint32_t *, char *>);
357 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
358 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
359 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
360 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
361 case HWC2::FunctionDescriptor::RegisterCallback:
362 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
363 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
364 &DrmHwcTwo::RegisterCallback, int32_t,
365 hwc2_callback_data_t, hwc2_function_pointer_t>);
366
367 // Display functions
368 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
369 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200370 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
371 &HwcDisplay::AcceptDisplayChanges>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200372 case HWC2::FunctionDescriptor::CreateLayer:
373 return ToHook<HWC2_PFN_CREATE_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200374 DisplayHook<decltype(&HwcDisplay::CreateLayer),
375 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200376 case HWC2::FunctionDescriptor::DestroyLayer:
377 return ToHook<HWC2_PFN_DESTROY_LAYER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200378 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
379 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200380 case HWC2::FunctionDescriptor::GetActiveConfig:
381 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200382 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
383 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200384 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
385 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200386 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
387 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
388 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200389 case HWC2::FunctionDescriptor::GetClientTargetSupport:
390 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200391 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
392 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
393 int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200394 case HWC2::FunctionDescriptor::GetColorModes:
395 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200396 DisplayHook<decltype(&HwcDisplay::GetColorModes),
397 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200398 case HWC2::FunctionDescriptor::GetDisplayAttribute:
399 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200400 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
401 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
402 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200403 case HWC2::FunctionDescriptor::GetDisplayConfigs:
404 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
Drew Davenportf7e88332024-09-06 12:54:38 -0600405 DisplayHook<decltype(&HwcDisplay::LegacyGetDisplayConfigs),
406 &HwcDisplay::LegacyGetDisplayConfigs, uint32_t *,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200407 hwc2_config_t *>);
408 case HWC2::FunctionDescriptor::GetDisplayName:
409 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200410 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
411 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200412 case HWC2::FunctionDescriptor::GetDisplayRequests:
413 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200414 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
415 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
416 hwc2_layer_t *, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200417 case HWC2::FunctionDescriptor::GetDisplayType:
418 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200419 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
420 &HwcDisplay::GetDisplayType, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200421 case HWC2::FunctionDescriptor::GetDozeSupport:
422 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200423 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
424 &HwcDisplay::GetDozeSupport, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200425 case HWC2::FunctionDescriptor::GetHdrCapabilities:
426 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200427 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
428 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
429 float *, float *, float *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200430 case HWC2::FunctionDescriptor::GetReleaseFences:
431 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200432 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
433 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
434 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200435 case HWC2::FunctionDescriptor::PresentDisplay:
436 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
438 &HwcDisplay::PresentDisplay, int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200439 case HWC2::FunctionDescriptor::SetActiveConfig:
440 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200441 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
442 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200443 case HWC2::FunctionDescriptor::SetClientTarget:
444 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200445 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
446 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
447 int32_t, hwc_region_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200448 case HWC2::FunctionDescriptor::SetColorMode:
449 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200450 DisplayHook<decltype(&HwcDisplay::SetColorMode),
451 &HwcDisplay::SetColorMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200452 case HWC2::FunctionDescriptor::SetColorTransform:
453 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200454 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
455 &HwcDisplay::SetColorTransform, const float *, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200456 case HWC2::FunctionDescriptor::SetOutputBuffer:
457 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200458 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
459 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200460 case HWC2::FunctionDescriptor::SetPowerMode:
461 return ToHook<HWC2_PFN_SET_POWER_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200462 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
463 &HwcDisplay::SetPowerMode, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200464 case HWC2::FunctionDescriptor::SetVsyncEnabled:
465 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200466 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
467 &HwcDisplay::SetVsyncEnabled, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200468 case HWC2::FunctionDescriptor::ValidateDisplay:
469 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200470 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
471 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200472#if __ANDROID_API__ > 27
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200473 case HWC2::FunctionDescriptor::GetRenderIntents:
474 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200475 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
476 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
477 int32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200478 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
479 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200480 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
481 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200482#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200483#if __ANDROID_API__ > 28
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200484 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
485 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200486 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
487 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
488 uint32_t *, uint8_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200489 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
490 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200491 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
492 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
493 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200494 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
495 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
497 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200498 case HWC2::FunctionDescriptor::SetDisplayBrightness:
499 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
501 &HwcDisplay::SetDisplayBrightness, float>);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200502#endif /* __ANDROID_API__ > 28 */
503#if __ANDROID_API__ > 29
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200504 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
505 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200506 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
507 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200508 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
509 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
511 &HwcDisplay::GetDisplayVsyncPeriod,
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200512 hwc2_vsync_period_t *>);
513 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
514 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
516 &HwcDisplay::SetActiveConfigWithConstraints,
517 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
518 hwc_vsync_period_change_timeline_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200519 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
520 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
522 &HwcDisplay::SetAutoLowLatencyMode, bool>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200523 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
524 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200525 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
526 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
527 uint32_t *>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200528 case HWC2::FunctionDescriptor::SetContentType:
529 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200530 DisplayHook<decltype(&HwcDisplay::SetContentType),
531 &HwcDisplay::SetContentType, int32_t>);
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200532#endif
533 // Layer functions
534 case HWC2::FunctionDescriptor::SetCursorPosition:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200535 return (hwc2_function_pointer_t)SetCursorPosition;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200536 case HWC2::FunctionDescriptor::SetLayerBlendMode:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200537 return (hwc2_function_pointer_t)SetLayerBlendMode;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200538 case HWC2::FunctionDescriptor::SetLayerBuffer:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200539 return (hwc2_function_pointer_t)SetLayerBuffer;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200540 case HWC2::FunctionDescriptor::SetLayerColor:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200541 return (hwc2_function_pointer_t)SetLayerColor;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200542 case HWC2::FunctionDescriptor::SetLayerCompositionType:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200543 return (hwc2_function_pointer_t)SetLayerCompositionType;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200544 case HWC2::FunctionDescriptor::SetLayerDataspace:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200545 return (hwc2_function_pointer_t)SetLayerDataspace;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200546 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200547 return (hwc2_function_pointer_t)SetLayerDisplayFrame;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200548 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200549 return (hwc2_function_pointer_t)SetLayerPlaneAlpha;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200550 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200551 return (hwc2_function_pointer_t)SetLayerSidebandStream;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200552 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200553 return (hwc2_function_pointer_t)SetLayerSourceCrop;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200554 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200555 return (hwc2_function_pointer_t)SetLayerSurfaceDamage;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200556 case HWC2::FunctionDescriptor::SetLayerTransform:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200557 return (hwc2_function_pointer_t)SetLayerTransform;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200558 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200559 return (hwc2_function_pointer_t)SetLayerVisibleRegion;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200560 case HWC2::FunctionDescriptor::SetLayerZOrder:
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200561 return (hwc2_function_pointer_t)SetLayerZOrder;
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200562 case HWC2::FunctionDescriptor::Invalid:
563 default:
564 return nullptr;
565 }
566}
567
Roman Stratiienko41cd9502025-01-22 16:48:58 +0200568// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast)
569
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200570static int HookDevOpen(const struct hw_module_t *module, const char *name,
571 struct hw_device_t **dev) {
572 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
573 ALOGE("Invalid module name- %s", name);
574 return -EINVAL;
575 }
576
577 auto ctx = std::make_unique<Drmhwc2Device>();
578 if (!ctx) {
579 ALOGE("Failed to allocate DrmHwcTwo");
580 return -ENOMEM;
581 }
582
583 ctx->common.tag = HARDWARE_DEVICE_TAG;
584 ctx->common.version = HWC_DEVICE_API_VERSION_2_0;
585 ctx->common.close = HookDevClose;
586 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
587 ctx->common.module = (hw_module_t *)module;
588 ctx->getCapabilities = HookDevGetCapabilities;
589 ctx->getFunction = HookDevGetFunction;
590
Roman Stratiienko26fd2b22022-01-04 12:59:29 +0200591 *dev = &ctx.release()->common;
592
593 return 0;
594}
595
596} // namespace android
597
598// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
599static struct hw_module_methods_t hwc2_module_methods = {
600 .open = android::HookDevOpen,
601};
602
603// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
604hw_module_t HAL_MODULE_INFO_SYM = {
605 .tag = HARDWARE_MODULE_TAG,
606 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
607 .id = HWC_HARDWARE_MODULE_ID,
608 .name = "DrmHwcTwo module",
609 .author = "The Android Open Source Project",
610 .methods = &hwc2_module_methods,
611 .dso = nullptr,
612 .reserved = {0},
613};