blob: 66babda8909dd20f5ab14776c398517f0c528e67 [file] [log] [blame]
Roman Stratiienko03fd35c2022-01-04 14:30:37 +02001/*
2 * Copyright (C) 2022 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#define LOG_TAG "hwc-layer"
18
19#include "HwcLayer.h"
20
21#include <fcntl.h>
22
23#include "utils/log.h"
24
25namespace android {
26
27// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
28HWC2::Error HwcLayer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
29 return HWC2::Error::None;
30}
31
32HWC2::Error HwcLayer::SetLayerBlendMode(int32_t mode) {
33 switch (static_cast<HWC2::BlendMode>(mode)) {
34 case HWC2::BlendMode::None:
35 blending_ = DrmHwcBlending::kNone;
36 break;
37 case HWC2::BlendMode::Premultiplied:
38 blending_ = DrmHwcBlending::kPreMult;
39 break;
40 case HWC2::BlendMode::Coverage:
41 blending_ = DrmHwcBlending::kCoverage;
42 break;
43 default:
44 ALOGE("Unknown blending mode b=%d", blending_);
45 blending_ = DrmHwcBlending::kNone;
46 break;
47 }
48 return HWC2::Error::None;
49}
50
51/* Find API details at:
52 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
53 */
54HWC2::Error HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
55 int32_t acquire_fence) {
56 buffer_ = buffer;
57 acquire_fence_ = UniqueFd(acquire_fence);
58 return HWC2::Error::None;
59}
60
61// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
62HWC2::Error HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
63 // TODO(nobody): Put to client composition here?
64 return HWC2::Error::None;
65}
66
67HWC2::Error HwcLayer::SetLayerCompositionType(int32_t type) {
68 sf_type_ = static_cast<HWC2::Composition>(type);
69 return HWC2::Error::None;
70}
71
72HWC2::Error HwcLayer::SetLayerDataspace(int32_t dataspace) {
73 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
74 case HAL_DATASPACE_STANDARD_BT709:
75 color_space_ = DrmHwcColorSpace::kItuRec709;
76 break;
77 case HAL_DATASPACE_STANDARD_BT601_625:
78 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
79 case HAL_DATASPACE_STANDARD_BT601_525:
80 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
81 color_space_ = DrmHwcColorSpace::kItuRec601;
82 break;
83 case HAL_DATASPACE_STANDARD_BT2020:
84 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
85 color_space_ = DrmHwcColorSpace::kItuRec2020;
86 break;
87 default:
88 color_space_ = DrmHwcColorSpace::kUndefined;
89 }
90
91 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
92 case HAL_DATASPACE_RANGE_FULL:
93 sample_range_ = DrmHwcSampleRange::kFullRange;
94 break;
95 case HAL_DATASPACE_RANGE_LIMITED:
96 sample_range_ = DrmHwcSampleRange::kLimitedRange;
97 break;
98 default:
99 sample_range_ = DrmHwcSampleRange::kUndefined;
100 }
101 return HWC2::Error::None;
102}
103
104HWC2::Error HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
105 display_frame_ = frame;
106 return HWC2::Error::None;
107}
108
109HWC2::Error HwcLayer::SetLayerPlaneAlpha(float alpha) {
110 alpha_ = alpha;
111 return HWC2::Error::None;
112}
113
114// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
115HWC2::Error HwcLayer::SetLayerSidebandStream(
116 const native_handle_t * /*stream*/) {
117 // TODO(nobody): We don't support sideband
118 return HWC2::Error::Unsupported;
119}
120
121HWC2::Error HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
122 source_crop_ = crop;
123 return HWC2::Error::None;
124}
125
126// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
127HWC2::Error HwcLayer::SetLayerSurfaceDamage(hwc_region_t /*damage*/) {
128 // TODO(nobody): We don't use surface damage, marking as unsupported
129 return HWC2::Error::None;
130}
131
132HWC2::Error HwcLayer::SetLayerTransform(int32_t transform) {
133 uint32_t l_transform = 0;
134
135 // 270* and 180* cannot be combined with flips. More specifically, they
136 // already contain both horizontal and vertical flips, so those fields are
137 // redundant in this case. 90* rotation can be combined with either horizontal
138 // flip or vertical flip, so treat it differently
139 if (transform == HWC_TRANSFORM_ROT_270) {
140 l_transform = DrmHwcTransform::kRotate270;
141 } else if (transform == HWC_TRANSFORM_ROT_180) {
142 l_transform = DrmHwcTransform::kRotate180;
143 } else {
144 if ((transform & HWC_TRANSFORM_FLIP_H) != 0)
145 l_transform |= DrmHwcTransform::kFlipH;
146 if ((transform & HWC_TRANSFORM_FLIP_V) != 0)
147 l_transform |= DrmHwcTransform::kFlipV;
148 if ((transform & HWC_TRANSFORM_ROT_90) != 0)
149 l_transform |= DrmHwcTransform::kRotate90;
150 }
151
152 transform_ = static_cast<DrmHwcTransform>(l_transform);
153 return HWC2::Error::None;
154}
155
156// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
157HWC2::Error HwcLayer::SetLayerVisibleRegion(hwc_region_t /*visible*/) {
158 // TODO(nobody): We don't use this information, marking as unsupported
159 return HWC2::Error::None;
160}
161
162HWC2::Error HwcLayer::SetLayerZOrder(uint32_t order) {
163 z_order_ = order;
164 return HWC2::Error::None;
165}
166
167void HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
168 layer->sf_handle = buffer_;
169 // TODO(rsglobal): Avoid extra fd duplication
170 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
171 layer->display_frame = display_frame_;
172 layer->alpha = std::lround(alpha_ * UINT16_MAX);
173 layer->blending = blending_;
174 layer->source_crop = source_crop_;
175 layer->transform = transform_;
176 layer->color_space = color_space_;
177 layer->sample_range = sample_range_;
178}
179
180} // namespace android