1 /*
2 * Copyright (C) 2021 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 <android-base/logging.h>
20 // hwc2 types
21 #include <hardware/hwcomposer2.h>
22 // new hwc3 types used by exynosdisplay
23 #include "ExynosHwc3Types.h"
24 // aidl types
25 #include "include/IComposerHal.h"
26
27 namespace aidl::android::hardware::graphics::composer3::impl {
28
29 // hwc2 to aidl conversion
30 namespace h2a {
31
32 template <typename T, typename U>
translate(const T & in,U & out)33 inline void translate(const T& in, U& out) {
34 out = static_cast<U>(in);
35 }
36
37 template <typename T, typename U>
translate(const std::optional<T> & in,std::optional<U> & out)38 inline void translate(const std::optional<T>& in, std::optional<U>& out) {
39 if (in.has_value()) {
40 out = static_cast<U>(in.value());
41 } else {
42 out.reset();
43 }
44 }
45
46 template <typename T, typename U>
translate(const std::vector<T> & in,std::vector<U> & out)47 inline void translate(const std::vector<T>& in, std::vector<U>& out) {
48 out.clear();
49 for (auto const &t : in) {
50 U u;
51 translate(t, u);
52 out.emplace_back(std::move(u));
53 }
54 }
55
56 template<>
translate(const hwc_vsync_period_change_timeline_t & in,VsyncPeriodChangeTimeline & out)57 inline void translate(const hwc_vsync_period_change_timeline_t& in,
58 VsyncPeriodChangeTimeline& out) {
59 out.newVsyncAppliedTimeNanos = in.newVsyncAppliedTimeNanos;
60 out.refreshRequired = in.refreshRequired;
61 out.refreshTimeNanos = in.refreshTimeNanos;
62 }
63
64 template<>
translate(const int32_t & fd,ndk::ScopedFileDescriptor & sfd)65 inline void translate(const int32_t& fd, ndk::ScopedFileDescriptor& sfd) {
66 // ownership of fd is transferred to sfd
67 sfd = ndk::ScopedFileDescriptor(fd);
68 }
69
70 template<>
translate(const hwc_client_target_property & in,ClientTargetProperty & out)71 inline void translate(const hwc_client_target_property& in, ClientTargetProperty& out) {
72 translate(in.pixelFormat, out.pixelFormat);
73 translate(in.dataspace, out.dataspace);
74 }
75
76 template<>
translate(const HwcMountOrientation & in,common::Transform & out)77 inline void translate(const HwcMountOrientation& in, common::Transform& out) {
78 switch (in) {
79 case HwcMountOrientation::ROT_0:
80 out = common::Transform::NONE;
81 break;
82
83 case HwcMountOrientation::ROT_90:
84 out = common::Transform::ROT_90;
85 break;
86
87 case HwcMountOrientation::ROT_180:
88 out = common::Transform::ROT_180;
89 break;
90
91 case HwcMountOrientation::ROT_270:
92 out = common::Transform::ROT_270;
93 break;
94
95 default:
96 LOG(WARNING) << "unrecoganized display orientation: " << static_cast<uint32_t>(in);
97 out = common::Transform::NONE;
98 break;
99 }
100 }
101
102 } // namespace h2a
103
104 // aidl to hwc2 conversion
105 namespace a2h {
106
107 template <typename T, typename U>
translate(const T & in,U & out)108 inline void translate(const T& in, U& out) {
109 out = static_cast<U>(in);
110 }
111
112 template <typename T, typename U>
translate(const std::vector<std::optional<T>> & in,std::vector<U> & out)113 inline void translate(const std::vector<std::optional<T>>& in, std::vector<U>& out) {
114 out.clear();
115 for (auto const &t : in) {
116 U u;
117 if (t) {
118 translate(*t, u);
119 out.emplace_back(std::move(u));
120 }
121 }
122 }
123
124 template <typename T, typename U>
translate(const std::vector<T> & in,std::vector<U> & out)125 inline void translate(const std::vector<T>& in, std::vector<U>& out) {
126 out.clear();
127 for (auto const &t : in) {
128 U u;
129 translate(t, u);
130 out.emplace_back(std::move(u));
131 }
132 }
133
134 template<>
translate(const common::Rect & in,hwc_rect_t & out)135 inline void translate(const common::Rect& in, hwc_rect_t& out) {
136 out.left = in.left;
137 out.top = in.top;
138 out.right =in.right;
139 out.bottom =in.bottom;
140 }
141
142 template<>
translate(const common::FRect & in,hwc_frect_t & out)143 inline void translate(const common::FRect& in, hwc_frect_t& out) {
144 out.left = in.left;
145 out.top = in.top;
146 out.right =in.right;
147 out.bottom =in.bottom;
148 }
149
150 template <>
translate(const VsyncPeriodChangeConstraints & in,hwc_vsync_period_change_constraints_t & out)151 inline void translate(const VsyncPeriodChangeConstraints& in,
152 hwc_vsync_period_change_constraints_t& out) {
153 out.desiredTimeNanos = in.desiredTimeNanos;
154 out.seamlessRequired = in.seamlessRequired;
155 }
156
157 template<>
translate(const ndk::ScopedFileDescriptor & in,int32_t & out)158 inline void translate(const ndk::ScopedFileDescriptor& in, int32_t& out) {
159 // it's not defined as const. drop the const to avoid dup
160 auto& sfd = const_cast<ndk::ScopedFileDescriptor&>(in);
161 // take the ownership
162 out = sfd.get();
163 *sfd.getR() = -1;
164 }
165
166 template<>
translate(const bool & in,hwc2_vsync_t & out)167 inline void translate(const bool& in, hwc2_vsync_t& out) {
168 // HWC_VSYNC_DISABLE is 2
169 out = in ? HWC2_VSYNC_ENABLE : HWC2_VSYNC_DISABLE;
170 }
171
172 template<>
translate(const Color & in,hwc_color_t & out)173 inline void translate(const Color& in, hwc_color_t& out) {
174 const auto floatColorToUint8Clamped = [](float val) -> uint8_t {
175 const auto intVal = static_cast<uint64_t>(std::round(255.0f * val));
176 const auto minVal = static_cast<uint64_t>(0);
177 const auto maxVal = static_cast<uint64_t>(255);
178 return std::clamp(intVal, minVal, maxVal);
179 };
180
181 out.r = floatColorToUint8Clamped(in.r);
182 out.g = floatColorToUint8Clamped(in.g);
183 out.b = floatColorToUint8Clamped(in.b);
184 out.a = floatColorToUint8Clamped(in.a);
185 }
186
187 } // namespace a2h
188
189 } // namespace aidl::android::hardware::graphics::composer3::impl
190