xref: /aosp_15_r20/external/drm_hwcomposer/hwc3/Utils.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1*0a9764feSAndroid Build Coastguard Worker /*
2*0a9764feSAndroid Build Coastguard Worker  * Copyright (C) 2024 The Android Open Source Project
3*0a9764feSAndroid Build Coastguard Worker  *
4*0a9764feSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*0a9764feSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*0a9764feSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*0a9764feSAndroid Build Coastguard Worker  *
8*0a9764feSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*0a9764feSAndroid Build Coastguard Worker  *
10*0a9764feSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*0a9764feSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*0a9764feSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*0a9764feSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*0a9764feSAndroid Build Coastguard Worker  * limitations under the License.
15*0a9764feSAndroid Build Coastguard Worker  */
16*0a9764feSAndroid Build Coastguard Worker 
17*0a9764feSAndroid Build Coastguard Worker #pragma once
18*0a9764feSAndroid Build Coastguard Worker 
19*0a9764feSAndroid Build Coastguard Worker #include <aidl/android/hardware/graphics/composer3/IComposerClient.h>
20*0a9764feSAndroid Build Coastguard Worker #include <hardware/hwcomposer2.h>
21*0a9764feSAndroid Build Coastguard Worker #include <log/log.h>
22*0a9764feSAndroid Build Coastguard Worker 
23*0a9764feSAndroid Build Coastguard Worker #include <cstdint>
24*0a9764feSAndroid Build Coastguard Worker 
25*0a9764feSAndroid Build Coastguard Worker // NOLINTNEXTLINE
26*0a9764feSAndroid Build Coastguard Worker #define DEBUG_FUNC() ALOGV("%s", __func__)
27*0a9764feSAndroid Build Coastguard Worker 
28*0a9764feSAndroid Build Coastguard Worker namespace aidl::android::hardware::graphics::composer3 {
29*0a9764feSAndroid Build Coastguard Worker 
30*0a9764feSAndroid Build Coastguard Worker namespace hwc3 {
31*0a9764feSAndroid Build Coastguard Worker enum class Error : int32_t {
32*0a9764feSAndroid Build Coastguard Worker   kNone = 0,
33*0a9764feSAndroid Build Coastguard Worker   kBadConfig = IComposerClient::EX_BAD_CONFIG,
34*0a9764feSAndroid Build Coastguard Worker   kBadDisplay = IComposerClient::EX_BAD_DISPLAY,
35*0a9764feSAndroid Build Coastguard Worker   kBadLayer = IComposerClient::EX_BAD_LAYER,
36*0a9764feSAndroid Build Coastguard Worker   kBadParameter = IComposerClient::EX_BAD_PARAMETER,
37*0a9764feSAndroid Build Coastguard Worker   kNoResources = IComposerClient::EX_NO_RESOURCES,
38*0a9764feSAndroid Build Coastguard Worker   kNotValidated = IComposerClient::EX_NOT_VALIDATED,
39*0a9764feSAndroid Build Coastguard Worker   kUnsupported = IComposerClient::EX_UNSUPPORTED,
40*0a9764feSAndroid Build Coastguard Worker   kSeamlessNotAllowed = IComposerClient::EX_SEAMLESS_NOT_ALLOWED,
41*0a9764feSAndroid Build Coastguard Worker   kSeamlessNotPossible = IComposerClient::EX_SEAMLESS_NOT_POSSIBLE,
42*0a9764feSAndroid Build Coastguard Worker };
43*0a9764feSAndroid Build Coastguard Worker }  // namespace hwc3
44*0a9764feSAndroid Build Coastguard Worker 
45*0a9764feSAndroid Build Coastguard Worker hwc3::Error Hwc2toHwc3Error(HWC2::Error error);
46*0a9764feSAndroid Build Coastguard Worker 
ToBinderStatus(hwc3::Error error)47*0a9764feSAndroid Build Coastguard Worker inline ndk::ScopedAStatus ToBinderStatus(hwc3::Error error) {
48*0a9764feSAndroid Build Coastguard Worker   if (error != hwc3::Error::kNone) {
49*0a9764feSAndroid Build Coastguard Worker     return ndk::ScopedAStatus::fromServiceSpecificError(
50*0a9764feSAndroid Build Coastguard Worker         static_cast<int32_t>(error));
51*0a9764feSAndroid Build Coastguard Worker   }
52*0a9764feSAndroid Build Coastguard Worker   return ndk::ScopedAStatus::ok();
53*0a9764feSAndroid Build Coastguard Worker }
54*0a9764feSAndroid Build Coastguard Worker 
ToBinderStatus(HWC2::Error error)55*0a9764feSAndroid Build Coastguard Worker inline ndk::ScopedAStatus ToBinderStatus(HWC2::Error error) {
56*0a9764feSAndroid Build Coastguard Worker   return ToBinderStatus(Hwc2toHwc3Error(error));
57*0a9764feSAndroid Build Coastguard Worker }
58*0a9764feSAndroid Build Coastguard Worker 
59*0a9764feSAndroid Build Coastguard Worker // ID conversion. HWC2 uses typedef'd unsigned integer types while HWC3 uses
60*0a9764feSAndroid Build Coastguard Worker // signed integer types. static_cast in between these.
Hwc2LayerToHwc3(hwc2_layer_t layer)61*0a9764feSAndroid Build Coastguard Worker inline int64_t Hwc2LayerToHwc3(hwc2_layer_t layer) {
62*0a9764feSAndroid Build Coastguard Worker   return static_cast<int64_t>(layer);
63*0a9764feSAndroid Build Coastguard Worker }
64*0a9764feSAndroid Build Coastguard Worker 
Hwc2DisplayToHwc3(hwc2_display_t display)65*0a9764feSAndroid Build Coastguard Worker inline int64_t Hwc2DisplayToHwc3(hwc2_display_t display) {
66*0a9764feSAndroid Build Coastguard Worker   return static_cast<int64_t>(display);
67*0a9764feSAndroid Build Coastguard Worker }
68*0a9764feSAndroid Build Coastguard Worker 
Hwc2ConfigIdToHwc3(hwc2_config_t config_id)69*0a9764feSAndroid Build Coastguard Worker inline int32_t Hwc2ConfigIdToHwc3(hwc2_config_t config_id) {
70*0a9764feSAndroid Build Coastguard Worker   return static_cast<int32_t>(config_id);
71*0a9764feSAndroid Build Coastguard Worker }
72*0a9764feSAndroid Build Coastguard Worker 
Hwc3LayerToHwc2(int64_t layer)73*0a9764feSAndroid Build Coastguard Worker inline hwc2_layer_t Hwc3LayerToHwc2(int64_t layer) {
74*0a9764feSAndroid Build Coastguard Worker   return static_cast<hwc2_layer_t>(layer);
75*0a9764feSAndroid Build Coastguard Worker }
76*0a9764feSAndroid Build Coastguard Worker 
Hwc3DisplayToHwc2(int64_t display)77*0a9764feSAndroid Build Coastguard Worker inline hwc2_display_t Hwc3DisplayToHwc2(int64_t display) {
78*0a9764feSAndroid Build Coastguard Worker   return static_cast<hwc2_display_t>(display);
79*0a9764feSAndroid Build Coastguard Worker }
80*0a9764feSAndroid Build Coastguard Worker 
Hwc3ConfigIdToHwc2(int32_t config_id)81*0a9764feSAndroid Build Coastguard Worker inline hwc2_config_t Hwc3ConfigIdToHwc2(int32_t config_id) {
82*0a9764feSAndroid Build Coastguard Worker   return static_cast<hwc2_config_t>(config_id);
83*0a9764feSAndroid Build Coastguard Worker }
84*0a9764feSAndroid Build Coastguard Worker 
85*0a9764feSAndroid Build Coastguard Worker // Values match up to HWC2_COMPOSITION_SIDEBAND, with HWC2 not supporting
86*0a9764feSAndroid Build Coastguard Worker // newer values. static_cast in between shared values.
87*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/Composition.aidl
88*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24;l=826
Hwc2CompositionTypeToHwc3(int32_t composition_type)89*0a9764feSAndroid Build Coastguard Worker inline Composition Hwc2CompositionTypeToHwc3(int32_t composition_type) {
90*0a9764feSAndroid Build Coastguard Worker   if (composition_type < HWC2_COMPOSITION_INVALID ||
91*0a9764feSAndroid Build Coastguard Worker       composition_type > HWC2_COMPOSITION_SIDEBAND) {
92*0a9764feSAndroid Build Coastguard Worker     return Composition::INVALID;
93*0a9764feSAndroid Build Coastguard Worker   }
94*0a9764feSAndroid Build Coastguard Worker   return static_cast<Composition>(composition_type);
95*0a9764feSAndroid Build Coastguard Worker }
96*0a9764feSAndroid Build Coastguard Worker 
97*0a9764feSAndroid Build Coastguard Worker // Values for color modes match across HWC versions, so static cast is safe:
98*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/ColorMode.aidl
99*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=118
100*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=35
Hwc2ColorModeToHwc3(int32_t color_mode)101*0a9764feSAndroid Build Coastguard Worker inline ColorMode Hwc2ColorModeToHwc3(int32_t color_mode) {
102*0a9764feSAndroid Build Coastguard Worker   return static_cast<ColorMode>(color_mode);
103*0a9764feSAndroid Build Coastguard Worker }
104*0a9764feSAndroid Build Coastguard Worker 
Hwc3ColorModeToHwc2(ColorMode color_mode)105*0a9764feSAndroid Build Coastguard Worker inline int32_t Hwc3ColorModeToHwc2(ColorMode color_mode) {
106*0a9764feSAndroid Build Coastguard Worker   return static_cast<int32_t>(color_mode);
107*0a9764feSAndroid Build Coastguard Worker }
108*0a9764feSAndroid Build Coastguard Worker 
109*0a9764feSAndroid Build Coastguard Worker // Capabilities match up to DisplayCapability::AUTO_LOW_LATENCY_MODE, with hwc2
110*0a9764feSAndroid Build Coastguard Worker // not defining capabilities beyond that.
111*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayCapability.aidl#28
112*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;drc=1a0e4a1698c7b080d6763cef9e16592bce75967e;l=418
Hwc2DisplayCapabilityToHwc3(uint32_t display_capability)113*0a9764feSAndroid Build Coastguard Worker inline DisplayCapability Hwc2DisplayCapabilityToHwc3(
114*0a9764feSAndroid Build Coastguard Worker     uint32_t display_capability) {
115*0a9764feSAndroid Build Coastguard Worker   if (display_capability > HWC2_DISPLAY_CAPABILITY_AUTO_LOW_LATENCY_MODE) {
116*0a9764feSAndroid Build Coastguard Worker     return DisplayCapability::INVALID;
117*0a9764feSAndroid Build Coastguard Worker   }
118*0a9764feSAndroid Build Coastguard Worker   return static_cast<DisplayCapability>(display_capability);
119*0a9764feSAndroid Build Coastguard Worker }
120*0a9764feSAndroid Build Coastguard Worker 
121*0a9764feSAndroid Build Coastguard Worker // Values match between hwc versions, so static cast is safe.
122*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayConnectionType.aidl
123*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=216;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24;bpv=0;bpt=1
Hwc2DisplayConnectionTypeToHwc3(uint32_t type)124*0a9764feSAndroid Build Coastguard Worker inline DisplayConnectionType Hwc2DisplayConnectionTypeToHwc3(uint32_t type) {
125*0a9764feSAndroid Build Coastguard Worker   if (type > HWC2_DISPLAY_CONNECTION_TYPE_EXTERNAL) {
126*0a9764feSAndroid Build Coastguard Worker     // Arbitrarily return EXTERNAL in this case, which shouldn't happen.
127*0a9764feSAndroid Build Coastguard Worker     // TODO: This will be cleaned up once hwc2<->hwc3 conversion is removed.
128*0a9764feSAndroid Build Coastguard Worker     ALOGE("Unknown HWC2 connection type. Could not translate: %d", type);
129*0a9764feSAndroid Build Coastguard Worker     return DisplayConnectionType::EXTERNAL;
130*0a9764feSAndroid Build Coastguard Worker   }
131*0a9764feSAndroid Build Coastguard Worker   return static_cast<DisplayConnectionType>(type);
132*0a9764feSAndroid Build Coastguard Worker }
133*0a9764feSAndroid Build Coastguard Worker 
134*0a9764feSAndroid Build Coastguard Worker // Values match, so static_cast is safe.
135*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/RenderIntent.aidl
136*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=37
Hwc2RenderIntentToHwc3(int32_t intent)137*0a9764feSAndroid Build Coastguard Worker inline RenderIntent Hwc2RenderIntentToHwc3(int32_t intent) {
138*0a9764feSAndroid Build Coastguard Worker   if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
139*0a9764feSAndroid Build Coastguard Worker       intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) {
140*0a9764feSAndroid Build Coastguard Worker     ALOGE("Unknown HWC2 render intent. Could not translate: %d", intent);
141*0a9764feSAndroid Build Coastguard Worker     return RenderIntent::COLORIMETRIC;
142*0a9764feSAndroid Build Coastguard Worker   }
143*0a9764feSAndroid Build Coastguard Worker   return static_cast<RenderIntent>(intent);
144*0a9764feSAndroid Build Coastguard Worker }
Hwc3RenderIntentToHwc2(RenderIntent render_intent)145*0a9764feSAndroid Build Coastguard Worker inline int32_t Hwc3RenderIntentToHwc2(RenderIntent render_intent) {
146*0a9764feSAndroid Build Coastguard Worker   return static_cast<int32_t>(render_intent);
147*0a9764feSAndroid Build Coastguard Worker }
148*0a9764feSAndroid Build Coastguard Worker 
149*0a9764feSAndroid Build Coastguard Worker // Values match up to DOZE_SUSPEND.
150*0a9764feSAndroid Build Coastguard Worker // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/PowerMode.aidl
151*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=348;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24
Hwc3PowerModeToHwc2(PowerMode power_mode)152*0a9764feSAndroid Build Coastguard Worker inline int32_t Hwc3PowerModeToHwc2(PowerMode power_mode) {
153*0a9764feSAndroid Build Coastguard Worker   if (power_mode > PowerMode::DOZE_SUSPEND) {
154*0a9764feSAndroid Build Coastguard Worker     ALOGE("Unsupported HWC2 power mode. Could not translate: %d", power_mode);
155*0a9764feSAndroid Build Coastguard Worker     return HWC2_POWER_MODE_ON;
156*0a9764feSAndroid Build Coastguard Worker   }
157*0a9764feSAndroid Build Coastguard Worker   return static_cast<int32_t>(power_mode);
158*0a9764feSAndroid Build Coastguard Worker }
159*0a9764feSAndroid Build Coastguard Worker 
160*0a9764feSAndroid Build Coastguard Worker // Values appear to match.
161*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/Dataspace.aidl
162*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;l=43
163*0a9764feSAndroid Build Coastguard Worker // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;l=22;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2
Hwc3DataspaceToHwc2(common::Dataspace dataspace)164*0a9764feSAndroid Build Coastguard Worker inline int32_t Hwc3DataspaceToHwc2(common::Dataspace dataspace) {
165*0a9764feSAndroid Build Coastguard Worker   return static_cast<int32_t>(dataspace);
166*0a9764feSAndroid Build Coastguard Worker }
167*0a9764feSAndroid Build Coastguard Worker 
168*0a9764feSAndroid Build Coastguard Worker };  // namespace aidl::android::hardware::graphics::composer3