blob: 2dd51ca986236b444d1ee2570a74bb8659cbef04 [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 Davenport07b96f02024-09-24 15:37:12 -060043 if (layer_properties.alpha) {
44 layer_data_.pi.alpha = std::lround(layer_properties.alpha.value() *
45 UINT16_MAX);
46 }
Drew Davenporta241a772024-09-24 11:26:30 -060047}
48
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020049// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
50HWC2::Error HwcLayer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
51 return HWC2::Error::None;
52}
53
54HWC2::Error HwcLayer::SetLayerBlendMode(int32_t mode) {
55 switch (static_cast<HWC2::BlendMode>(mode)) {
56 case HWC2::BlendMode::None:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020057 blend_mode_ = BufferBlendMode::kNone;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020058 break;
59 case HWC2::BlendMode::Premultiplied:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020060 blend_mode_ = BufferBlendMode::kPreMult;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020061 break;
62 case HWC2::BlendMode::Coverage:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020063 blend_mode_ = BufferBlendMode::kCoverage;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020064 break;
65 default:
John Stultz71d983d2024-02-13 20:40:36 -080066 ALOGE("Unknown blending mode b=%d", mode);
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020067 blend_mode_ = BufferBlendMode::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020068 break;
69 }
70 return HWC2::Error::None;
71}
72
73/* Find API details at:
74 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
75 */
76HWC2::Error HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
77 int32_t acquire_fence) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +020078 layer_data_.acquire_fence = MakeSharedFd(acquire_fence);
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020079 buffer_handle_ = buffer;
80 buffer_handle_updated_ = true;
81
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020082 return HWC2::Error::None;
83}
84
85// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
86HWC2::Error HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
87 // TODO(nobody): Put to client composition here?
88 return HWC2::Error::None;
89}
90
91HWC2::Error HwcLayer::SetLayerCompositionType(int32_t type) {
92 sf_type_ = static_cast<HWC2::Composition>(type);
93 return HWC2::Error::None;
94}
95
96HWC2::Error HwcLayer::SetLayerDataspace(int32_t dataspace) {
97 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
98 case HAL_DATASPACE_STANDARD_BT709:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020099 color_space_ = BufferColorSpace::kItuRec709;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200100 break;
101 case HAL_DATASPACE_STANDARD_BT601_625:
102 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
103 case HAL_DATASPACE_STANDARD_BT601_525:
104 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200105 color_space_ = BufferColorSpace::kItuRec601;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200106 break;
107 case HAL_DATASPACE_STANDARD_BT2020:
108 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200109 color_space_ = BufferColorSpace::kItuRec2020;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200110 break;
111 default:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200112 color_space_ = BufferColorSpace::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200113 }
114
115 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
116 case HAL_DATASPACE_RANGE_FULL:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200117 sample_range_ = BufferSampleRange::kFullRange;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200118 break;
119 case HAL_DATASPACE_RANGE_LIMITED:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200120 sample_range_ = BufferSampleRange::kLimitedRange;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200121 break;
122 default:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200123 sample_range_ = BufferSampleRange::kUndefined;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200124 }
125 return HWC2::Error::None;
126}
127
128HWC2::Error HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200129 layer_data_.pi.display_frame = frame;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200130 return HWC2::Error::None;
131}
132
133HWC2::Error HwcLayer::SetLayerPlaneAlpha(float alpha) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200134 layer_data_.pi.alpha = std::lround(alpha * UINT16_MAX);
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200135 return HWC2::Error::None;
136}
137
138// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
139HWC2::Error HwcLayer::SetLayerSidebandStream(
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200140 const native_handle_t* /*stream*/) {
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200141 // TODO(nobody): We don't support sideband
142 return HWC2::Error::Unsupported;
143}
144
145HWC2::Error HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200146 layer_data_.pi.source_crop = crop;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200147 return HWC2::Error::None;
148}
149
150// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
151HWC2::Error HwcLayer::SetLayerSurfaceDamage(hwc_region_t /*damage*/) {
152 // TODO(nobody): We don't use surface damage, marking as unsupported
153 return HWC2::Error::None;
154}
155
156HWC2::Error HwcLayer::SetLayerTransform(int32_t transform) {
157 uint32_t l_transform = 0;
158
159 // 270* and 180* cannot be combined with flips. More specifically, they
160 // already contain both horizontal and vertical flips, so those fields are
161 // redundant in this case. 90* rotation can be combined with either horizontal
162 // flip or vertical flip, so treat it differently
163 if (transform == HWC_TRANSFORM_ROT_270) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200164 l_transform = LayerTransform::kRotate270;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200165 } else if (transform == HWC_TRANSFORM_ROT_180) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200166 l_transform = LayerTransform::kRotate180;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200167 } else {
168 if ((transform & HWC_TRANSFORM_FLIP_H) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200169 l_transform |= LayerTransform::kFlipH;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200170 if ((transform & HWC_TRANSFORM_FLIP_V) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200171 l_transform |= LayerTransform::kFlipV;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200172 if ((transform & HWC_TRANSFORM_ROT_90) != 0)
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200173 l_transform |= LayerTransform::kRotate90;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200174 }
175
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200176 layer_data_.pi.transform = static_cast<LayerTransform>(l_transform);
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200177 return HWC2::Error::None;
178}
179
180// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
181HWC2::Error HwcLayer::SetLayerVisibleRegion(hwc_region_t /*visible*/) {
182 // TODO(nobody): We don't use this information, marking as unsupported
183 return HWC2::Error::None;
184}
185
186HWC2::Error HwcLayer::SetLayerZOrder(uint32_t order) {
187 z_order_ = order;
188 return HWC2::Error::None;
189}
190
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200191void HwcLayer::ImportFb() {
192 if (!IsLayerUsableAsDevice() || !buffer_handle_updated_) {
193 return;
194 }
195 buffer_handle_updated_ = false;
196
197 layer_data_.fb = {};
198
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300199 auto unique_id = BufferInfoGetter::GetInstance()->GetUniqueId(buffer_handle_);
200 if (unique_id && SwChainGetBufferFromCache(*unique_id)) {
201 return;
202 }
203
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200204 layer_data_.bi = BufferInfoGetter::GetInstance()->GetBoInfo(buffer_handle_);
205 if (!layer_data_.bi) {
206 ALOGW("Unable to get buffer information (0x%p)", buffer_handle_);
207 bi_get_failed_ = true;
208 return;
209 }
210
211 layer_data_
212 .fb = parent_->GetPipe().device->GetDrmFbImporter().GetOrCreateFbId(
213 &layer_data_.bi.value());
214
215 if (!layer_data_.fb) {
216 ALOGV("Unable to create framebuffer object for buffer 0x%p",
217 buffer_handle_);
218 fb_import_failed_ = true;
219 return;
220 }
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300221
222 if (unique_id) {
223 SwChainAddCurrentBuffer(*unique_id);
224 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200225}
226
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200227void HwcLayer::PopulateLayerData() {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200228 ImportFb();
229
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300230 if (!layer_data_.bi) {
231 ALOGE("%s: Invalid state", __func__);
232 return;
233 }
234
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200235 if (blend_mode_ != BufferBlendMode::kUndefined) {
236 layer_data_.bi->blend_mode = blend_mode_;
237 }
238 if (color_space_ != BufferColorSpace::kUndefined) {
239 layer_data_.bi->color_space = color_space_;
240 }
241 if (sample_range_ != BufferSampleRange::kUndefined) {
242 layer_data_.bi->sample_range = sample_range_;
243 }
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200244}
245
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300246/* SwapChain Cache */
247
248bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
249 if (swchain_lookup_table_.count(unique_id) == 0) {
250 return false;
251 }
252
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300253 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300254
255 if (swchain_cache_.count(seq) == 0) {
256 return false;
257 }
258
259 auto& el = swchain_cache_[seq];
260 if (!el.bi) {
261 return false;
262 }
263
264 layer_data_.bi = el.bi;
265 layer_data_.fb = el.fb;
266
267 return true;
268}
269
270void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
271 if (swchain_lookup_table_.count(unique_id) != 0) {
272 if (swchain_lookup_table_[unique_id] ==
273 int(swchain_lookup_table_.size()) - 1) {
274 /* Skip same buffer */
275 return;
276 }
277 if (swchain_lookup_table_[unique_id] == 0) {
278 swchain_reassembled_ = true;
279 return;
280 }
281 /* Tracking error */
282 SwChainClearCache();
283 return;
284 }
285
286 swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
287}
288
289void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
290 if (!swchain_reassembled_) {
291 SwChainReassemble(unique_id);
292 }
293
294 if (swchain_reassembled_) {
295 if (swchain_lookup_table_.count(unique_id) == 0) {
296 SwChainClearCache();
297 return;
298 }
299
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300300 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300301
302 if (swchain_cache_.count(seq) == 0) {
303 swchain_cache_[seq] = {};
304 }
305
306 swchain_cache_[seq].bi = layer_data_.bi;
307 swchain_cache_[seq].fb = layer_data_.fb;
308 }
309}
310
311void HwcLayer::SwChainClearCache() {
312 swchain_cache_.clear();
313 swchain_lookup_table_.clear();
314 swchain_reassembled_ = false;
315}
316
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200317} // namespace android