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