blob: a993acff8da2bd7afb269a6a42dfc07c116451d6 [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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020018
19#include "HwcLayer.h"
20
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020021#include "HwcDisplay.h"
22#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020023#include "utils/log.h"
24
25namespace android {
26
Drew Davenporta241a772024-09-24 11:26:30 -060027void HwcLayer::SetLayerProperties(const LayerProperties& layer_properties) {
28 if (layer_properties.blend_mode) {
29 blend_mode_ = layer_properties.blend_mode.value();
30 }
Drew Davenportac9681e2024-09-24 12:17:34 -060031 if (layer_properties.color_space) {
32 color_space_ = layer_properties.color_space.value();
33 }
34 if (layer_properties.sample_range) {
35 sample_range_ = layer_properties.sample_range.value();
36 }
Drew Davenport1b0d8b72024-09-24 15:31:38 -060037 if (layer_properties.composition_type) {
38 sf_type_ = layer_properties.composition_type.value();
39 }
Drew Davenport22d66b42024-09-24 15:34:57 -060040 if (layer_properties.display_frame) {
41 layer_data_.pi.display_frame = layer_properties.display_frame.value();
42 }
Drew Davenporta241a772024-09-24 11:26:30 -060043}
44
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020045// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
46HWC2::Error HwcLayer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
47 return HWC2::Error::None;
48}
49
50HWC2::Error HwcLayer::SetLayerBlendMode(int32_t mode) {
51 switch (static_cast<HWC2::BlendMode>(mode)) {
52 case HWC2::BlendMode::None:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020053 blend_mode_ = BufferBlendMode::kNone;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020054 break;
55 case HWC2::BlendMode::Premultiplied:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020056 blend_mode_ = BufferBlendMode::kPreMult;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020057 break;
58 case HWC2::BlendMode::Coverage:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020059 blend_mode_ = BufferBlendMode::kCoverage;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020060 break;
61 default:
John Stultz71d983d2024-02-13 20:40:36 -080062 ALOGE("Unknown blending mode b=%d", mode);
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020063 blend_mode_ = BufferBlendMode::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020064 break;
65 }
66 return HWC2::Error::None;
67}
68
69/* Find API details at:
70 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
71 */
72HWC2::Error HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
73 int32_t acquire_fence) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +020074 layer_data_.acquire_fence = MakeSharedFd(acquire_fence);
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020075 buffer_handle_ = buffer;
76 buffer_handle_updated_ = true;
77
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020078 return HWC2::Error::None;
79}
80
81// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
82HWC2::Error HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
83 // TODO(nobody): Put to client composition here?
84 return HWC2::Error::None;
85}
86
87HWC2::Error HwcLayer::SetLayerCompositionType(int32_t type) {
88 sf_type_ = static_cast<HWC2::Composition>(type);
89 return HWC2::Error::None;
90}
91
92HWC2::Error HwcLayer::SetLayerDataspace(int32_t dataspace) {
93 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
94 case HAL_DATASPACE_STANDARD_BT709:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020095 color_space_ = BufferColorSpace::kItuRec709;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020096 break;
97 case HAL_DATASPACE_STANDARD_BT601_625:
98 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
99 case HAL_DATASPACE_STANDARD_BT601_525:
100 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200101 color_space_ = BufferColorSpace::kItuRec601;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200102 break;
103 case HAL_DATASPACE_STANDARD_BT2020:
104 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200105 color_space_ = BufferColorSpace::kItuRec2020;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200106 break;
107 default:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200108 color_space_ = BufferColorSpace::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200109 }
110
111 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
112 case HAL_DATASPACE_RANGE_FULL:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200113 sample_range_ = BufferSampleRange::kFullRange;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200114 break;
115 case HAL_DATASPACE_RANGE_LIMITED:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200116 sample_range_ = BufferSampleRange::kLimitedRange;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200117 break;
118 default:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200119 sample_range_ = BufferSampleRange::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200120 }
121 return HWC2::Error::None;
122}
123
124HWC2::Error HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200125 layer_data_.pi.display_frame = frame;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200126 return HWC2::Error::None;
127}
128
129HWC2::Error HwcLayer::SetLayerPlaneAlpha(float alpha) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200130 layer_data_.pi.alpha = std::lround(alpha * UINT16_MAX);
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200131 return HWC2::Error::None;
132}
133
134// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
135HWC2::Error HwcLayer::SetLayerSidebandStream(
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200136 const native_handle_t* /*stream*/) {
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200137 // TODO(nobody): We don't support sideband
138 return HWC2::Error::Unsupported;
139}
140
141HWC2::Error HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200142 layer_data_.pi.source_crop = crop;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200143 return HWC2::Error::None;
144}
145
146// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
147HWC2::Error HwcLayer::SetLayerSurfaceDamage(hwc_region_t /*damage*/) {
148 // TODO(nobody): We don't use surface damage, marking as unsupported
149 return HWC2::Error::None;
150}
151
152HWC2::Error HwcLayer::SetLayerTransform(int32_t transform) {
153 uint32_t l_transform = 0;
154
155 // 270* and 180* cannot be combined with flips. More specifically, they
156 // already contain both horizontal and vertical flips, so those fields are
157 // redundant in this case. 90* rotation can be combined with either horizontal
158 // flip or vertical flip, so treat it differently
159 if (transform == HWC_TRANSFORM_ROT_270) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200160 l_transform = LayerTransform::kRotate270;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200161 } else if (transform == HWC_TRANSFORM_ROT_180) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200162 l_transform = LayerTransform::kRotate180;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200163 } else {
164 if ((transform & HWC_TRANSFORM_FLIP_H) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200165 l_transform |= LayerTransform::kFlipH;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200166 if ((transform & HWC_TRANSFORM_FLIP_V) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200167 l_transform |= LayerTransform::kFlipV;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200168 if ((transform & HWC_TRANSFORM_ROT_90) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200169 l_transform |= LayerTransform::kRotate90;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200170 }
171
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200172 layer_data_.pi.transform = static_cast<LayerTransform>(l_transform);
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200173 return HWC2::Error::None;
174}
175
176// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
177HWC2::Error HwcLayer::SetLayerVisibleRegion(hwc_region_t /*visible*/) {
178 // TODO(nobody): We don't use this information, marking as unsupported
179 return HWC2::Error::None;
180}
181
182HWC2::Error HwcLayer::SetLayerZOrder(uint32_t order) {
183 z_order_ = order;
184 return HWC2::Error::None;
185}
186
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200187void HwcLayer::ImportFb() {
188 if (!IsLayerUsableAsDevice() || !buffer_handle_updated_) {
189 return;
190 }
191 buffer_handle_updated_ = false;
192
193 layer_data_.fb = {};
194
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300195 auto unique_id = BufferInfoGetter::GetInstance()->GetUniqueId(buffer_handle_);
196 if (unique_id && SwChainGetBufferFromCache(*unique_id)) {
197 return;
198 }
199
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200200 layer_data_.bi = BufferInfoGetter::GetInstance()->GetBoInfo(buffer_handle_);
201 if (!layer_data_.bi) {
202 ALOGW("Unable to get buffer information (0x%p)", buffer_handle_);
203 bi_get_failed_ = true;
204 return;
205 }
206
207 layer_data_
208 .fb = parent_->GetPipe().device->GetDrmFbImporter().GetOrCreateFbId(
209 &layer_data_.bi.value());
210
211 if (!layer_data_.fb) {
212 ALOGV("Unable to create framebuffer object for buffer 0x%p",
213 buffer_handle_);
214 fb_import_failed_ = true;
215 return;
216 }
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300217
218 if (unique_id) {
219 SwChainAddCurrentBuffer(*unique_id);
220 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200221}
222
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200223void HwcLayer::PopulateLayerData() {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200224 ImportFb();
225
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300226 if (!layer_data_.bi) {
227 ALOGE("%s: Invalid state", __func__);
228 return;
229 }
230
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200231 if (blend_mode_ != BufferBlendMode::kUndefined) {
232 layer_data_.bi->blend_mode = blend_mode_;
233 }
234 if (color_space_ != BufferColorSpace::kUndefined) {
235 layer_data_.bi->color_space = color_space_;
236 }
237 if (sample_range_ != BufferSampleRange::kUndefined) {
238 layer_data_.bi->sample_range = sample_range_;
239 }
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200240}
241
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300242/* SwapChain Cache */
243
244bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
245 if (swchain_lookup_table_.count(unique_id) == 0) {
246 return false;
247 }
248
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300249 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300250
251 if (swchain_cache_.count(seq) == 0) {
252 return false;
253 }
254
255 auto& el = swchain_cache_[seq];
256 if (!el.bi) {
257 return false;
258 }
259
260 layer_data_.bi = el.bi;
261 layer_data_.fb = el.fb;
262
263 return true;
264}
265
266void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
267 if (swchain_lookup_table_.count(unique_id) != 0) {
268 if (swchain_lookup_table_[unique_id] ==
269 int(swchain_lookup_table_.size()) - 1) {
270 /* Skip same buffer */
271 return;
272 }
273 if (swchain_lookup_table_[unique_id] == 0) {
274 swchain_reassembled_ = true;
275 return;
276 }
277 /* Tracking error */
278 SwChainClearCache();
279 return;
280 }
281
282 swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
283}
284
285void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
286 if (!swchain_reassembled_) {
287 SwChainReassemble(unique_id);
288 }
289
290 if (swchain_reassembled_) {
291 if (swchain_lookup_table_.count(unique_id) == 0) {
292 SwChainClearCache();
293 return;
294 }
295
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300296 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300297
298 if (swchain_cache_.count(seq) == 0) {
299 swchain_cache_[seq] = {};
300 }
301
302 swchain_cache_[seq].bi = layer_data_.bi;
303 swchain_cache_[seq].fb = layer_data_.fb;
304 }
305}
306
307void HwcLayer::SwChainClearCache() {
308 swchain_cache_.clear();
309 swchain_lookup_table_.clear();
310 swchain_reassembled_ = false;
311}
312
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200313} // namespace android