• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <aidl/android/hardware/graphics/common/Hdr.h>
20 #include <aidl/android/hardware/graphics/composer3/IComposerClient.h>
21 #include <hardware/hwcomposer2.h>
22 #include <log/log.h>
23 
24 #include <cstdint>
25 
26 // NOLINTNEXTLINE
27 #define DEBUG_FUNC() ALOGV("%s", __func__)
28 
29 namespace aidl::android::hardware::graphics::composer3 {
30 
31 namespace hwc3 {
32 enum class Error : int32_t {
33   kNone = 0,
34   kBadConfig = IComposerClient::EX_BAD_CONFIG,
35   kBadDisplay = IComposerClient::EX_BAD_DISPLAY,
36   kBadLayer = IComposerClient::EX_BAD_LAYER,
37   kBadParameter = IComposerClient::EX_BAD_PARAMETER,
38   kNoResources = IComposerClient::EX_NO_RESOURCES,
39   kNotValidated = IComposerClient::EX_NOT_VALIDATED,
40   kUnsupported = IComposerClient::EX_UNSUPPORTED,
41   kSeamlessNotAllowed = IComposerClient::EX_SEAMLESS_NOT_ALLOWED,
42   kSeamlessNotPossible = IComposerClient::EX_SEAMLESS_NOT_POSSIBLE,
43   kConfigFailed = IComposerClient::EX_CONFIG_FAILED,
44 };
45 }  // namespace hwc3
46 
47 hwc3::Error Hwc2toHwc3Error(HWC2::Error error);
48 
ToBinderStatus(hwc3::Error error)49 inline ndk::ScopedAStatus ToBinderStatus(hwc3::Error error) {
50   if (error != hwc3::Error::kNone) {
51     return ndk::ScopedAStatus::fromServiceSpecificError(
52         static_cast<int32_t>(error));
53   }
54   return ndk::ScopedAStatus::ok();
55 }
56 
ToBinderStatus(HWC2::Error error)57 inline ndk::ScopedAStatus ToBinderStatus(HWC2::Error error) {
58   return ToBinderStatus(Hwc2toHwc3Error(error));
59 }
60 
Hwc2DisplayToHwc3(hwc2_display_t display)61 inline int64_t Hwc2DisplayToHwc3(hwc2_display_t display) {
62   return static_cast<int64_t>(display);
63 }
64 
Hwc2ConfigIdToHwc3(hwc2_config_t config_id)65 inline int32_t Hwc2ConfigIdToHwc3(hwc2_config_t config_id) {
66   return static_cast<int32_t>(config_id);
67 }
68 
Hwc3DisplayToHwc2(int64_t display)69 inline hwc2_display_t Hwc3DisplayToHwc2(int64_t display) {
70   return static_cast<hwc2_display_t>(display);
71 }
72 
Hwc3ConfigIdToHwc2(int32_t config_id)73 inline hwc2_config_t Hwc3ConfigIdToHwc2(int32_t config_id) {
74   return static_cast<hwc2_config_t>(config_id);
75 }
76 
77 // Values match up to HWC2_COMPOSITION_SIDEBAND, with HWC2 not supporting
78 // newer values. static_cast in between shared values.
79 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/Composition.aidl
80 // 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)81 inline Composition Hwc2CompositionTypeToHwc3(int32_t composition_type) {
82   if (composition_type < HWC2_COMPOSITION_INVALID ||
83       composition_type > HWC2_COMPOSITION_SIDEBAND) {
84     return Composition::INVALID;
85   }
86   return static_cast<Composition>(composition_type);
87 }
88 
89 // Values for color modes match across HWC versions, so static cast is safe:
90 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/ColorMode.aidl
91 // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=118
92 // 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)93 inline ColorMode Hwc2ColorModeToHwc3(int32_t color_mode) {
94   return static_cast<ColorMode>(color_mode);
95 }
96 
Hwc3ColorModeToHwc2(ColorMode color_mode)97 inline int32_t Hwc3ColorModeToHwc2(ColorMode color_mode) {
98   return static_cast<int32_t>(color_mode);
99 }
100 
101 // Capabilities match up to DisplayCapability::AUTO_LOW_LATENCY_MODE, with hwc2
102 // not defining capabilities beyond that.
103 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayCapability.aidl#28
104 // 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)105 inline DisplayCapability Hwc2DisplayCapabilityToHwc3(
106     uint32_t display_capability) {
107   if (display_capability > HWC2_DISPLAY_CAPABILITY_AUTO_LOW_LATENCY_MODE) {
108     return DisplayCapability::INVALID;
109   }
110   return static_cast<DisplayCapability>(display_capability);
111 }
112 
113 // Values match between hwc versions, so static cast is safe.
114 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayConnectionType.aidl
115 // 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)116 inline DisplayConnectionType Hwc2DisplayConnectionTypeToHwc3(uint32_t type) {
117   if (type > HWC2_DISPLAY_CONNECTION_TYPE_EXTERNAL) {
118     // Arbitrarily return EXTERNAL in this case, which shouldn't happen.
119     // TODO: This will be cleaned up once hwc2<->hwc3 conversion is removed.
120     ALOGE("Unknown HWC2 connection type. Could not translate: %d", type);
121     return DisplayConnectionType::EXTERNAL;
122   }
123   return static_cast<DisplayConnectionType>(type);
124 }
125 
126 // Values match, so static_cast is safe.
127 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/RenderIntent.aidl
128 // 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)129 inline RenderIntent Hwc2RenderIntentToHwc3(int32_t intent) {
130   if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
131       intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) {
132     ALOGE("Unknown HWC2 render intent. Could not translate: %d", intent);
133     return RenderIntent::COLORIMETRIC;
134   }
135   return static_cast<RenderIntent>(intent);
136 }
Hwc3RenderIntentToHwc2(RenderIntent render_intent)137 inline int32_t Hwc3RenderIntentToHwc2(RenderIntent render_intent) {
138   return static_cast<int32_t>(render_intent);
139 }
140 
141 // Values match up to DOZE_SUSPEND.
142 // https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/PowerMode.aidl
143 // https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=348;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24
Hwc3PowerModeToHwc2(PowerMode power_mode)144 inline int32_t Hwc3PowerModeToHwc2(PowerMode power_mode) {
145   if (power_mode > PowerMode::DOZE_SUSPEND) {
146     ALOGE("Unsupported HWC2 power mode. Could not translate: %d", power_mode);
147     return HWC2_POWER_MODE_ON;
148   }
149   return static_cast<int32_t>(power_mode);
150 }
151 
152 // Values appear to match.
153 // https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/Dataspace.aidl
154 // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;l=43
155 // 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)156 inline int32_t Hwc3DataspaceToHwc2(common::Dataspace dataspace) {
157   return static_cast<int32_t>(dataspace);
158 }
159 
160 // Values appear to match.
161 // https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/Hdr.aidl
162 // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;l=130;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2
163 // https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.2.h;l=12;drc=af7be7616859f8e9e57710b9c37c66cf880a6643
Hwc2HdrTypeToHwc3(int32_t hdr_type)164 inline common::Hdr Hwc2HdrTypeToHwc3(int32_t hdr_type) {
165   return static_cast<common::Hdr>(hdr_type);
166 }
167 
168 };  // namespace aidl::android::hardware::graphics::composer3
169