blob: 2e21b97320bcdd03f45bc11c9d607719960fef38 [file] [log] [blame]
Drew Davenport5951b112024-08-05 09:44:27 -06001/*
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/composer3/IComposerClient.h>
20#include <hardware/hwcomposer2.h>
Jason Macnak1f263842024-09-18 13:27:18 -070021#include <log/log.h>
Drew Davenport5951b112024-08-05 09:44:27 -060022
23#include <cstdint>
24
25// NOLINTNEXTLINE
26#define DEBUG_FUNC() ALOGV("%s", __func__)
27
28namespace aidl::android::hardware::graphics::composer3 {
29
30namespace hwc3 {
31enum class Error : int32_t {
32 kNone = 0,
33 kBadConfig = IComposerClient::EX_BAD_CONFIG,
34 kBadDisplay = IComposerClient::EX_BAD_DISPLAY,
35 kBadLayer = IComposerClient::EX_BAD_LAYER,
36 kBadParameter = IComposerClient::EX_BAD_PARAMETER,
37 kNoResources = IComposerClient::EX_NO_RESOURCES,
38 kNotValidated = IComposerClient::EX_NOT_VALIDATED,
39 kUnsupported = IComposerClient::EX_UNSUPPORTED,
40 kSeamlessNotAllowed = IComposerClient::EX_SEAMLESS_NOT_ALLOWED,
41 kSeamlessNotPossible = IComposerClient::EX_SEAMLESS_NOT_POSSIBLE,
42};
43} // namespace hwc3
44
45hwc3::Error Hwc2toHwc3Error(HWC2::Error error);
46
47inline ndk::ScopedAStatus ToBinderStatus(hwc3::Error error) {
48 if (error != hwc3::Error::kNone) {
49 return ndk::ScopedAStatus::fromServiceSpecificError(
50 static_cast<int32_t>(error));
51 }
52 return ndk::ScopedAStatus::ok();
53}
54
55inline ndk::ScopedAStatus ToBinderStatus(HWC2::Error error) {
56 return ToBinderStatus(Hwc2toHwc3Error(error));
57}
58
59// ID conversion. HWC2 uses typedef'd unsigned integer types while HWC3 uses
60// signed integer types. static_cast in between these.
61inline int64_t Hwc2LayerToHwc3(hwc2_layer_t layer) {
62 return static_cast<int64_t>(layer);
63}
64
65inline int64_t Hwc2DisplayToHwc3(hwc2_display_t display) {
66 return static_cast<int64_t>(display);
67}
68
69inline int32_t Hwc2ConfigIdToHwc3(hwc2_config_t config_id) {
70 return static_cast<int32_t>(config_id);
71}
72
73inline hwc2_layer_t Hwc3LayerToHwc2(int64_t layer) {
74 return static_cast<hwc2_layer_t>(layer);
75}
76
77inline hwc2_display_t Hwc3DisplayToHwc2(int64_t display) {
78 return static_cast<hwc2_display_t>(display);
79}
80
81inline hwc2_config_t Hwc3ConfigIdToHwc2(int32_t config_id) {
82 return static_cast<hwc2_config_t>(config_id);
83}
84
85// Values match up to HWC2_COMPOSITION_SIDEBAND, with HWC2 not supporting
86// newer values. static_cast in between shared values.
87// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/Composition.aidl
88// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24;l=826
89inline Composition Hwc2CompositionTypeToHwc3(int32_t composition_type) {
90 if (composition_type < HWC2_COMPOSITION_INVALID ||
91 composition_type > HWC2_COMPOSITION_SIDEBAND) {
92 return Composition::INVALID;
93 }
94 return static_cast<Composition>(composition_type);
95}
96
97inline int32_t Hwc3CompositionToHwc2(Composition composition_type) {
98 if (composition_type > Composition::SIDEBAND) {
99 return HWC2_COMPOSITION_INVALID;
100 }
101 return static_cast<int32_t>(composition_type);
102}
103
104// Values for color modes match across HWC versions, so static cast is safe:
105// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/ColorMode.aidl
106// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=118
107// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=35
108inline ColorMode Hwc2ColorModeToHwc3(int32_t color_mode) {
109 return static_cast<ColorMode>(color_mode);
110}
111
112inline int32_t Hwc3ColorModeToHwc2(ColorMode color_mode) {
113 return static_cast<int32_t>(color_mode);
114}
115
116// Capabilities match up to DisplayCapability::AUTO_LOW_LATENCY_MODE, with hwc2
117// not defining capabilities beyond that.
118// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayCapability.aidl#28
119// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;drc=1a0e4a1698c7b080d6763cef9e16592bce75967e;l=418
120inline DisplayCapability Hwc2DisplayCapabilityToHwc3(
121 uint32_t display_capability) {
122 if (display_capability > HWC2_DISPLAY_CAPABILITY_AUTO_LOW_LATENCY_MODE) {
123 return DisplayCapability::INVALID;
124 }
125 return static_cast<DisplayCapability>(display_capability);
126}
127
128// Values match between hwc versions, so static cast is safe.
129// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayConnectionType.aidl
130// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=216;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24;bpv=0;bpt=1
131inline DisplayConnectionType Hwc2DisplayConnectionTypeToHwc3(uint32_t type) {
132 if (type > HWC2_DISPLAY_CONNECTION_TYPE_EXTERNAL) {
133 // Arbitrarily return EXTERNAL in this case, which shouldn't happen.
134 // TODO: This will be cleaned up once hwc2<->hwc3 conversion is removed.
135 ALOGE("Unknown HWC2 connection type. Could not translate: %d", type);
136 return DisplayConnectionType::EXTERNAL;
137 }
138 return static_cast<DisplayConnectionType>(type);
139}
140
141// Values match, so static_cast is safe.
142// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/RenderIntent.aidl
143// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2;l=37
144inline RenderIntent Hwc2RenderIntentToHwc3(int32_t intent) {
145 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
146 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE) {
147 ALOGE("Unknown HWC2 render intent. Could not translate: %d", intent);
148 return RenderIntent::COLORIMETRIC;
149 }
150 return static_cast<RenderIntent>(intent);
151}
152inline int32_t Hwc3RenderIntentToHwc2(RenderIntent render_intent) {
153 return static_cast<int32_t>(render_intent);
154}
155
156// Content type matches, so static_cast is safe.
157// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/ContentType.aidl
158// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=350;drc=1a0e4a1698c7b080d6763cef9e16592bce75967e
159inline ContentType Hwc2ContentTypeToHwc3(uint32_t content_type) {
160 if (content_type > HWC2_CONTENT_TYPE_GAME) {
161 ALOGE("Unknown HWC2 content type. Could not translate: %d", content_type);
162 return ContentType::NONE;
163 }
164 return static_cast<ContentType>(content_type);
165}
166inline int32_t Hwc3ContentTypeToHwc2(ContentType content_type) {
167 return static_cast<int32_t>(content_type);
168}
169
170// Values match, so it's safe to do static_cast.
171// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/DisplayAttribute.aidl
172// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=58;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24
173inline int32_t Hwc3DisplayAttributeToHwc2(DisplayAttribute display_attribute) {
174 return static_cast<int32_t>(display_attribute);
175}
176
177// Values match up to DOZE_SUSPEND.
178// https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/main/graphics/composer/aidl/android/hardware/graphics/composer3/PowerMode.aidl
179// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=348;drc=d783cabd4d9bddb4b83f2dd38300b7598bb58b24
180inline int32_t Hwc3PowerModeToHwc2(PowerMode power_mode) {
181 if (power_mode > PowerMode::DOZE_SUSPEND) {
182 ALOGE("Unsupported HWC2 power mode. Could not translate: %d", power_mode);
183 return HWC2_POWER_MODE_ON;
184 }
185 return static_cast<int32_t>(power_mode);
186}
187
188// Values match, so static_cast is okay.
189// https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/BlendMode.aidl;drc=bab1ba54ede32520a5042d616a3af46ad4f55d5f;l=25
190// https://cs.android.com/android/platform/superproject/main/+/main:hardware/libhardware/include_all/hardware/hwcomposer2.h;l=72;drc=1a0e4a1698c7b080d6763cef9e16592bce75967e
191inline int32_t Hwc3BlendModeToHwc2(common::BlendMode blend_mode) {
192 return static_cast<int32_t>(blend_mode);
193}
194
195// Values appear to match.
196// https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/Dataspace.aidl
197// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;l=43
198// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.1.h;l=22;drc=7d940ae4afa450696afa25e07982f3a95e17e9b2
199inline int32_t Hwc3DataspaceToHwc2(common::Dataspace dataspace) {
200 return static_cast<int32_t>(dataspace);
201}
202
203// Values match, so static_cast is okay.
204// https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/graphics/common/aidl/android/hardware/graphics/common/Transform.aidl
205// https://cs.android.com/android/platform/superproject/main/+/main:system/core/libsystem/include/system/graphics-base-v1.0.h;l=41
206inline int32_t Hwc3TransformToHwc2(common::Transform transform) {
207 return static_cast<int32_t>(transform);
208}
209
210}; // namespace aidl::android::hardware::graphics::composer3