blob: 1d1e118f4f41b626dcc1b3e371e84709ed20702c [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) {
Drew Davenporte5fbbbb2024-10-14 15:49:27 -060028 if (layer_properties.buffer) {
29 layer_data_.acquire_fence = layer_properties.buffer->acquire_fence;
30 buffer_handle_ = layer_properties.buffer->buffer_handle;
31 buffer_handle_updated_ = true;
32 }
Drew Davenporta241a772024-09-24 11:26:30 -060033 if (layer_properties.blend_mode) {
34 blend_mode_ = layer_properties.blend_mode.value();
35 }
Drew Davenportac9681e2024-09-24 12:17:34 -060036 if (layer_properties.color_space) {
37 color_space_ = layer_properties.color_space.value();
38 }
39 if (layer_properties.sample_range) {
40 sample_range_ = layer_properties.sample_range.value();
41 }
Drew Davenport1b0d8b72024-09-24 15:31:38 -060042 if (layer_properties.composition_type) {
43 sf_type_ = layer_properties.composition_type.value();
44 }
Drew Davenport22d66b42024-09-24 15:34:57 -060045 if (layer_properties.display_frame) {
46 layer_data_.pi.display_frame = layer_properties.display_frame.value();
47 }
Drew Davenport07b96f02024-09-24 15:37:12 -060048 if (layer_properties.alpha) {
49 layer_data_.pi.alpha = std::lround(layer_properties.alpha.value() *
50 UINT16_MAX);
51 }
Drew Davenport7ab8c182024-09-24 17:04:26 -060052 if (layer_properties.source_crop) {
53 layer_data_.pi.source_crop = layer_properties.source_crop.value();
54 }
Drew Davenport51c61e42024-09-24 17:13:39 -060055 if (layer_properties.transform) {
56 layer_data_.pi.transform = layer_properties.transform.value();
57 }
Drew Davenport5d679b02024-09-25 10:15:58 -060058 if (layer_properties.z_order) {
59 z_order_ = layer_properties.z_order.value();
60 }
Drew Davenporta241a772024-09-24 11:26:30 -060061}
62
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020063void HwcLayer::ImportFb() {
64 if (!IsLayerUsableAsDevice() || !buffer_handle_updated_) {
65 return;
66 }
67 buffer_handle_updated_ = false;
68
69 layer_data_.fb = {};
70
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030071 auto unique_id = BufferInfoGetter::GetInstance()->GetUniqueId(buffer_handle_);
72 if (unique_id && SwChainGetBufferFromCache(*unique_id)) {
73 return;
74 }
75
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020076 layer_data_.bi = BufferInfoGetter::GetInstance()->GetBoInfo(buffer_handle_);
77 if (!layer_data_.bi) {
78 ALOGW("Unable to get buffer information (0x%p)", buffer_handle_);
79 bi_get_failed_ = true;
80 return;
81 }
82
83 layer_data_
84 .fb = parent_->GetPipe().device->GetDrmFbImporter().GetOrCreateFbId(
85 &layer_data_.bi.value());
86
87 if (!layer_data_.fb) {
88 ALOGV("Unable to create framebuffer object for buffer 0x%p",
89 buffer_handle_);
90 fb_import_failed_ = true;
91 return;
92 }
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030093
94 if (unique_id) {
95 SwChainAddCurrentBuffer(*unique_id);
96 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020097}
98
Roman Stratiienko359a9d32023-01-16 17:41:07 +020099void HwcLayer::PopulateLayerData() {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200100 ImportFb();
101
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300102 if (!layer_data_.bi) {
103 ALOGE("%s: Invalid state", __func__);
104 return;
105 }
106
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200107 if (blend_mode_ != BufferBlendMode::kUndefined) {
108 layer_data_.bi->blend_mode = blend_mode_;
109 }
110 if (color_space_ != BufferColorSpace::kUndefined) {
111 layer_data_.bi->color_space = color_space_;
112 }
113 if (sample_range_ != BufferSampleRange::kUndefined) {
114 layer_data_.bi->sample_range = sample_range_;
115 }
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200116}
117
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300118/* SwapChain Cache */
119
120bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
121 if (swchain_lookup_table_.count(unique_id) == 0) {
122 return false;
123 }
124
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300125 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300126
127 if (swchain_cache_.count(seq) == 0) {
128 return false;
129 }
130
131 auto& el = swchain_cache_[seq];
132 if (!el.bi) {
133 return false;
134 }
135
136 layer_data_.bi = el.bi;
137 layer_data_.fb = el.fb;
138
139 return true;
140}
141
142void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
143 if (swchain_lookup_table_.count(unique_id) != 0) {
144 if (swchain_lookup_table_[unique_id] ==
145 int(swchain_lookup_table_.size()) - 1) {
146 /* Skip same buffer */
147 return;
148 }
149 if (swchain_lookup_table_[unique_id] == 0) {
150 swchain_reassembled_ = true;
151 return;
152 }
153 /* Tracking error */
154 SwChainClearCache();
155 return;
156 }
157
158 swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
159}
160
161void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
162 if (!swchain_reassembled_) {
163 SwChainReassemble(unique_id);
164 }
165
166 if (swchain_reassembled_) {
167 if (swchain_lookup_table_.count(unique_id) == 0) {
168 SwChainClearCache();
169 return;
170 }
171
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300172 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300173
174 if (swchain_cache_.count(seq) == 0) {
175 swchain_cache_[seq] = {};
176 }
177
178 swchain_cache_[seq].bi = layer_data_.bi;
179 swchain_cache_[seq].fb = layer_data_.fb;
180 }
181}
182
183void HwcLayer::SwChainClearCache() {
184 swchain_cache_.clear();
185 swchain_lookup_table_.clear();
186 swchain_reassembled_ = false;
187}
188
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200189} // namespace android