Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 1 | /* |
| 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 Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 17 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) |
| 18 | // #define LOG_NDEBUG 0 // Uncomment to see HWC2 API calls in logcat |
| 19 | |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 20 | #define LOG_TAG "hwc2-device" |
| 21 | |
Roman Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 22 | #include <cinttypes> |
| 23 | |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 24 | #include "DrmHwcTwo.h" |
| 25 | #include "backend/Backend.h" |
| 26 | #include "utils/log.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
Roman Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 30 | /* 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 | */ |
| 37 | static 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 Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 46 | struct Drmhwc2Device : hwc2_device { |
| 47 | DrmHwcTwo drmhwctwo; |
| 48 | }; |
| 49 | |
| 50 | static DrmHwcTwo *ToDrmHwcTwo(hwc2_device_t *dev) { |
| 51 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast): |
| 52 | return &static_cast<Drmhwc2Device *>(dev)->drmhwctwo; |
| 53 | } |
| 54 | |
| 55 | template <typename PFN, typename T> |
| 56 | static 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 | |
| 62 | template <typename T, typename HookType, HookType func, typename... Args> |
| 63 | static T DeviceHook(hwc2_device_t *dev, Args... args) { |
Roman Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 64 | ALOGV("Device hook: %s", GetFuncName(__PRETTY_FUNCTION__).c_str()); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 65 | DrmHwcTwo *hwc = ToDrmHwcTwo(dev); |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame^] | 66 | const std::lock_guard<std::mutex> lock(hwc->GetResMan().GetMainLock()); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 67 | return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...)); |
| 68 | } |
| 69 | |
| 70 | template <typename HookType, HookType func, typename... Args> |
| 71 | static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle, |
| 72 | Args... args) { |
Roman Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 73 | ALOGV("Display #%" PRIu64 " hook: %s", display_handle, |
| 74 | GetFuncName(__PRETTY_FUNCTION__).c_str()); |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame^] | 75 | DrmHwcTwo *hwc = ToDrmHwcTwo(dev); |
| 76 | const std::lock_guard<std::mutex> lock(hwc->GetResMan().GetMainLock()); |
| 77 | HwcDisplay *display = DrmHwcTwo::GetDisplay(hwc, display_handle); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 78 | if (!display) |
| 79 | return static_cast<int32_t>(HWC2::Error::BadDisplay); |
| 80 | |
| 81 | return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...)); |
| 82 | } |
| 83 | |
| 84 | template <typename HookType, HookType func, typename... Args> |
| 85 | static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle, |
| 86 | hwc2_layer_t layer_handle, Args... args) { |
Roman Stratiienko | 1301752 | 2022-01-17 10:35:34 +0200 | [diff] [blame] | 87 | ALOGV("Display #%" PRIu64 " Layer: #%" PRIu64 " hook: %s", display_handle, |
| 88 | layer_handle, GetFuncName(__PRETTY_FUNCTION__).c_str()); |
Roman Stratiienko | 7492358 | 2022-01-17 11:24:21 +0200 | [diff] [blame^] | 89 | DrmHwcTwo *hwc = ToDrmHwcTwo(dev); |
| 90 | const std::lock_guard<std::mutex> lock(hwc->GetResMan().GetMainLock()); |
| 91 | HwcDisplay *display = DrmHwcTwo::GetDisplay(hwc, display_handle); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 92 | if (!display) |
| 93 | return static_cast<int32_t>(HWC2::Error::BadDisplay); |
| 94 | |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 95 | HwcLayer *layer = display->get_layer(layer_handle); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 96 | 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 | |
| 102 | static int HookDevClose(hw_device_t *dev) { |
| 103 | // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe |
| 104 | auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev); |
| 105 | std::unique_ptr<DrmHwcTwo> ctx(ToDrmHwcTwo(hwc2_dev)); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void HookDevGetCapabilities(hwc2_device_t * /*dev*/, uint32_t *out_count, |
| 110 | int32_t * /*out_capabilities*/) { |
| 111 | *out_count = 0; |
| 112 | } |
| 113 | |
| 114 | static 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 Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 145 | DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges), |
| 146 | &HwcDisplay::AcceptDisplayChanges>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 147 | case HWC2::FunctionDescriptor::CreateLayer: |
| 148 | return ToHook<HWC2_PFN_CREATE_LAYER>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 149 | DisplayHook<decltype(&HwcDisplay::CreateLayer), |
| 150 | &HwcDisplay::CreateLayer, hwc2_layer_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 151 | case HWC2::FunctionDescriptor::DestroyLayer: |
| 152 | return ToHook<HWC2_PFN_DESTROY_LAYER>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 153 | DisplayHook<decltype(&HwcDisplay::DestroyLayer), |
| 154 | &HwcDisplay::DestroyLayer, hwc2_layer_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 155 | case HWC2::FunctionDescriptor::GetActiveConfig: |
| 156 | return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 157 | DisplayHook<decltype(&HwcDisplay::GetActiveConfig), |
| 158 | &HwcDisplay::GetActiveConfig, hwc2_config_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 159 | case HWC2::FunctionDescriptor::GetChangedCompositionTypes: |
| 160 | return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 161 | DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes), |
| 162 | &HwcDisplay::GetChangedCompositionTypes, uint32_t *, |
| 163 | hwc2_layer_t *, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 164 | case HWC2::FunctionDescriptor::GetClientTargetSupport: |
| 165 | return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 166 | DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport), |
| 167 | &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t, |
| 168 | int32_t, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 169 | case HWC2::FunctionDescriptor::GetColorModes: |
| 170 | return ToHook<HWC2_PFN_GET_COLOR_MODES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 171 | DisplayHook<decltype(&HwcDisplay::GetColorModes), |
| 172 | &HwcDisplay::GetColorModes, uint32_t *, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 173 | case HWC2::FunctionDescriptor::GetDisplayAttribute: |
| 174 | return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 175 | DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute), |
| 176 | &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, |
| 177 | int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 178 | case HWC2::FunctionDescriptor::GetDisplayConfigs: |
| 179 | return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 180 | DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs), |
| 181 | &HwcDisplay::GetDisplayConfigs, uint32_t *, |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 182 | hwc2_config_t *>); |
| 183 | case HWC2::FunctionDescriptor::GetDisplayName: |
| 184 | return ToHook<HWC2_PFN_GET_DISPLAY_NAME>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 185 | DisplayHook<decltype(&HwcDisplay::GetDisplayName), |
| 186 | &HwcDisplay::GetDisplayName, uint32_t *, char *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 187 | case HWC2::FunctionDescriptor::GetDisplayRequests: |
| 188 | return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 189 | DisplayHook<decltype(&HwcDisplay::GetDisplayRequests), |
| 190 | &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *, |
| 191 | hwc2_layer_t *, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 192 | case HWC2::FunctionDescriptor::GetDisplayType: |
| 193 | return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 194 | DisplayHook<decltype(&HwcDisplay::GetDisplayType), |
| 195 | &HwcDisplay::GetDisplayType, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 196 | case HWC2::FunctionDescriptor::GetDozeSupport: |
| 197 | return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 198 | DisplayHook<decltype(&HwcDisplay::GetDozeSupport), |
| 199 | &HwcDisplay::GetDozeSupport, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 200 | case HWC2::FunctionDescriptor::GetHdrCapabilities: |
| 201 | return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 202 | DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities), |
| 203 | &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *, |
| 204 | float *, float *, float *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 205 | case HWC2::FunctionDescriptor::GetReleaseFences: |
| 206 | return ToHook<HWC2_PFN_GET_RELEASE_FENCES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 207 | DisplayHook<decltype(&HwcDisplay::GetReleaseFences), |
| 208 | &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *, |
| 209 | int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 210 | case HWC2::FunctionDescriptor::PresentDisplay: |
| 211 | return ToHook<HWC2_PFN_PRESENT_DISPLAY>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 212 | DisplayHook<decltype(&HwcDisplay::PresentDisplay), |
| 213 | &HwcDisplay::PresentDisplay, int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 214 | case HWC2::FunctionDescriptor::SetActiveConfig: |
| 215 | return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 216 | DisplayHook<decltype(&HwcDisplay::SetActiveConfig), |
| 217 | &HwcDisplay::SetActiveConfig, hwc2_config_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 218 | case HWC2::FunctionDescriptor::SetClientTarget: |
| 219 | return ToHook<HWC2_PFN_SET_CLIENT_TARGET>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 220 | DisplayHook<decltype(&HwcDisplay::SetClientTarget), |
| 221 | &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t, |
| 222 | int32_t, hwc_region_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 223 | case HWC2::FunctionDescriptor::SetColorMode: |
| 224 | return ToHook<HWC2_PFN_SET_COLOR_MODE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 225 | DisplayHook<decltype(&HwcDisplay::SetColorMode), |
| 226 | &HwcDisplay::SetColorMode, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 227 | case HWC2::FunctionDescriptor::SetColorTransform: |
| 228 | return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 229 | DisplayHook<decltype(&HwcDisplay::SetColorTransform), |
| 230 | &HwcDisplay::SetColorTransform, const float *, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 231 | case HWC2::FunctionDescriptor::SetOutputBuffer: |
| 232 | return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 233 | DisplayHook<decltype(&HwcDisplay::SetOutputBuffer), |
| 234 | &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 235 | case HWC2::FunctionDescriptor::SetPowerMode: |
| 236 | return ToHook<HWC2_PFN_SET_POWER_MODE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 237 | DisplayHook<decltype(&HwcDisplay::SetPowerMode), |
| 238 | &HwcDisplay::SetPowerMode, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 239 | case HWC2::FunctionDescriptor::SetVsyncEnabled: |
| 240 | return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 241 | DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled), |
| 242 | &HwcDisplay::SetVsyncEnabled, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 243 | case HWC2::FunctionDescriptor::ValidateDisplay: |
| 244 | return ToHook<HWC2_PFN_VALIDATE_DISPLAY>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 245 | DisplayHook<decltype(&HwcDisplay::ValidateDisplay), |
| 246 | &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 247 | #if PLATFORM_SDK_VERSION > 27 |
| 248 | case HWC2::FunctionDescriptor::GetRenderIntents: |
| 249 | return ToHook<HWC2_PFN_GET_RENDER_INTENTS>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 250 | DisplayHook<decltype(&HwcDisplay::GetRenderIntents), |
| 251 | &HwcDisplay::GetRenderIntents, int32_t, uint32_t *, |
| 252 | int32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 253 | case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent: |
| 254 | return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 255 | DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent), |
| 256 | &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 257 | #endif |
| 258 | #if PLATFORM_SDK_VERSION > 28 |
| 259 | case HWC2::FunctionDescriptor::GetDisplayIdentificationData: |
| 260 | return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 261 | DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData), |
| 262 | &HwcDisplay::GetDisplayIdentificationData, uint8_t *, |
| 263 | uint32_t *, uint8_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 264 | case HWC2::FunctionDescriptor::GetDisplayCapabilities: |
| 265 | return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 266 | DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities), |
| 267 | &HwcDisplay::GetDisplayCapabilities, uint32_t *, |
| 268 | uint32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 269 | case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport: |
| 270 | return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 271 | DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport), |
| 272 | &HwcDisplay::GetDisplayBrightnessSupport, bool *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 273 | case HWC2::FunctionDescriptor::SetDisplayBrightness: |
| 274 | return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 275 | DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness), |
| 276 | &HwcDisplay::SetDisplayBrightness, float>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 277 | #endif /* PLATFORM_SDK_VERSION > 28 */ |
| 278 | #if PLATFORM_SDK_VERSION > 29 |
| 279 | case HWC2::FunctionDescriptor::GetDisplayConnectionType: |
| 280 | return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 281 | DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType), |
| 282 | &HwcDisplay::GetDisplayConnectionType, uint32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 283 | case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod: |
| 284 | return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 285 | DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod), |
| 286 | &HwcDisplay::GetDisplayVsyncPeriod, |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 287 | hwc2_vsync_period_t *>); |
| 288 | case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints: |
| 289 | return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 290 | 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 Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 294 | case HWC2::FunctionDescriptor::SetAutoLowLatencyMode: |
| 295 | return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 296 | DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode), |
| 297 | &HwcDisplay::SetAutoLowLatencyMode, bool>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 298 | case HWC2::FunctionDescriptor::GetSupportedContentTypes: |
| 299 | return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 300 | DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes), |
| 301 | &HwcDisplay::GetSupportedContentTypes, uint32_t *, |
| 302 | uint32_t *>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 303 | case HWC2::FunctionDescriptor::SetContentType: |
| 304 | return ToHook<HWC2_PFN_SET_CONTENT_TYPE>( |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 305 | DisplayHook<decltype(&HwcDisplay::SetContentType), |
| 306 | &HwcDisplay::SetContentType, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 307 | #endif |
| 308 | // Layer functions |
| 309 | case HWC2::FunctionDescriptor::SetCursorPosition: |
| 310 | return ToHook<HWC2_PFN_SET_CURSOR_POSITION>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 311 | LayerHook<decltype(&HwcLayer::SetCursorPosition), |
| 312 | &HwcLayer::SetCursorPosition, int32_t, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 313 | case HWC2::FunctionDescriptor::SetLayerBlendMode: |
| 314 | return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 315 | LayerHook<decltype(&HwcLayer::SetLayerBlendMode), |
| 316 | &HwcLayer::SetLayerBlendMode, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 317 | case HWC2::FunctionDescriptor::SetLayerBuffer: |
| 318 | return ToHook<HWC2_PFN_SET_LAYER_BUFFER>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 319 | LayerHook<decltype(&HwcLayer::SetLayerBuffer), |
| 320 | &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 321 | case HWC2::FunctionDescriptor::SetLayerColor: |
| 322 | return ToHook<HWC2_PFN_SET_LAYER_COLOR>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 323 | LayerHook<decltype(&HwcLayer::SetLayerColor), |
| 324 | &HwcLayer::SetLayerColor, hwc_color_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 325 | case HWC2::FunctionDescriptor::SetLayerCompositionType: |
| 326 | return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 327 | LayerHook<decltype(&HwcLayer::SetLayerCompositionType), |
| 328 | &HwcLayer::SetLayerCompositionType, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 329 | case HWC2::FunctionDescriptor::SetLayerDataspace: |
| 330 | return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 331 | LayerHook<decltype(&HwcLayer::SetLayerDataspace), |
| 332 | &HwcLayer::SetLayerDataspace, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 333 | case HWC2::FunctionDescriptor::SetLayerDisplayFrame: |
| 334 | return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 335 | LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame), |
| 336 | &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 337 | case HWC2::FunctionDescriptor::SetLayerPlaneAlpha: |
| 338 | return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 339 | LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha), |
| 340 | &HwcLayer::SetLayerPlaneAlpha, float>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 341 | case HWC2::FunctionDescriptor::SetLayerSidebandStream: |
| 342 | return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 343 | LayerHook<decltype(&HwcLayer::SetLayerSidebandStream), |
| 344 | &HwcLayer::SetLayerSidebandStream, |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 345 | const native_handle_t *>); |
| 346 | case HWC2::FunctionDescriptor::SetLayerSourceCrop: |
| 347 | return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 348 | LayerHook<decltype(&HwcLayer::SetLayerSourceCrop), |
| 349 | &HwcLayer::SetLayerSourceCrop, hwc_frect_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 350 | case HWC2::FunctionDescriptor::SetLayerSurfaceDamage: |
| 351 | return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 352 | LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage), |
| 353 | &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 354 | case HWC2::FunctionDescriptor::SetLayerTransform: |
| 355 | return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 356 | LayerHook<decltype(&HwcLayer::SetLayerTransform), |
| 357 | &HwcLayer::SetLayerTransform, int32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 358 | case HWC2::FunctionDescriptor::SetLayerVisibleRegion: |
| 359 | return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 360 | LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion), |
| 361 | &HwcLayer::SetLayerVisibleRegion, hwc_region_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 362 | case HWC2::FunctionDescriptor::SetLayerZOrder: |
| 363 | return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>( |
Roman Stratiienko | 03fd35c | 2022-01-04 14:30:37 +0200 | [diff] [blame] | 364 | LayerHook<decltype(&HwcLayer::SetLayerZOrder), |
| 365 | &HwcLayer::SetLayerZOrder, uint32_t>); |
Roman Stratiienko | 26fd2b2 | 2022-01-04 12:59:29 +0200 | [diff] [blame] | 366 | case HWC2::FunctionDescriptor::Invalid: |
| 367 | default: |
| 368 | return nullptr; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static 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 | |
| 393 | HWC2::Error err = ctx->drmhwctwo.Init(); |
| 394 | if (err != HWC2::Error::None) { |
| 395 | ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err); |
| 396 | return -EINVAL; |
| 397 | } |
| 398 | |
| 399 | *dev = &ctx.release()->common; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | } // namespace android |
| 405 | |
| 406 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 407 | static struct hw_module_methods_t hwc2_module_methods = { |
| 408 | .open = android::HookDevOpen, |
| 409 | }; |
| 410 | |
| 411 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 412 | hw_module_t HAL_MODULE_INFO_SYM = { |
| 413 | .tag = HARDWARE_MODULE_TAG, |
| 414 | .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0), |
| 415 | .id = HWC_HARDWARE_MODULE_ID, |
| 416 | .name = "DrmHwcTwo module", |
| 417 | .author = "The Android Open Source Project", |
| 418 | .methods = &hwc2_module_methods, |
| 419 | .dso = nullptr, |
| 420 | .reserved = {0}, |
| 421 | }; |