blob: 6940c1288394765e8b15757bfe0e1bc1169cf5cb [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) {
Roman Stratiienkofb9fed52025-01-23 02:25:12 +020049 layer_data_.pi.alpha = layer_properties.alpha.value();
Drew Davenport07b96f02024-09-24 15:37:12 -060050 }
Drew Davenport7ab8c182024-09-24 17:04:26 -060051 if (layer_properties.source_crop) {
52 layer_data_.pi.source_crop = layer_properties.source_crop.value();
53 }
Drew Davenport51c61e42024-09-24 17:13:39 -060054 if (layer_properties.transform) {
55 layer_data_.pi.transform = layer_properties.transform.value();
56 }
Drew Davenport5d679b02024-09-25 10:15:58 -060057 if (layer_properties.z_order) {
58 z_order_ = layer_properties.z_order.value();
59 }
Drew Davenporta241a772024-09-24 11:26:30 -060060}
61
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020062void HwcLayer::ImportFb() {
63 if (!IsLayerUsableAsDevice() || !buffer_handle_updated_) {
64 return;
65 }
66 buffer_handle_updated_ = false;
67
68 layer_data_.fb = {};
69
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030070 auto unique_id = BufferInfoGetter::GetInstance()->GetUniqueId(buffer_handle_);
71 if (unique_id && SwChainGetBufferFromCache(*unique_id)) {
72 return;
73 }
74
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020075 layer_data_.bi = BufferInfoGetter::GetInstance()->GetBoInfo(buffer_handle_);
76 if (!layer_data_.bi) {
77 ALOGW("Unable to get buffer information (0x%p)", buffer_handle_);
78 bi_get_failed_ = true;
79 return;
80 }
81
82 layer_data_
83 .fb = parent_->GetPipe().device->GetDrmFbImporter().GetOrCreateFbId(
84 &layer_data_.bi.value());
85
86 if (!layer_data_.fb) {
87 ALOGV("Unable to create framebuffer object for buffer 0x%p",
88 buffer_handle_);
89 fb_import_failed_ = true;
90 return;
91 }
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030092
93 if (unique_id) {
94 SwChainAddCurrentBuffer(*unique_id);
95 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020096}
97
Roman Stratiienko359a9d32023-01-16 17:41:07 +020098void HwcLayer::PopulateLayerData() {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020099 ImportFb();
100
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300101 if (!layer_data_.bi) {
102 ALOGE("%s: Invalid state", __func__);
103 return;
104 }
105
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200106 if (blend_mode_ != BufferBlendMode::kUndefined) {
107 layer_data_.bi->blend_mode = blend_mode_;
108 }
109 if (color_space_ != BufferColorSpace::kUndefined) {
110 layer_data_.bi->color_space = color_space_;
111 }
112 if (sample_range_ != BufferSampleRange::kUndefined) {
113 layer_data_.bi->sample_range = sample_range_;
114 }
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200115}
116
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300117/* SwapChain Cache */
118
119bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
120 if (swchain_lookup_table_.count(unique_id) == 0) {
121 return false;
122 }
123
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300124 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300125
126 if (swchain_cache_.count(seq) == 0) {
127 return false;
128 }
129
130 auto& el = swchain_cache_[seq];
131 if (!el.bi) {
132 return false;
133 }
134
135 layer_data_.bi = el.bi;
136 layer_data_.fb = el.fb;
137
138 return true;
139}
140
141void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
142 if (swchain_lookup_table_.count(unique_id) != 0) {
143 if (swchain_lookup_table_[unique_id] ==
144 int(swchain_lookup_table_.size()) - 1) {
145 /* Skip same buffer */
146 return;
147 }
148 if (swchain_lookup_table_[unique_id] == 0) {
149 swchain_reassembled_ = true;
150 return;
151 }
152 /* Tracking error */
153 SwChainClearCache();
154 return;
155 }
156
157 swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
158}
159
160void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
161 if (!swchain_reassembled_) {
162 SwChainReassemble(unique_id);
163 }
164
165 if (swchain_reassembled_) {
166 if (swchain_lookup_table_.count(unique_id) == 0) {
167 SwChainClearCache();
168 return;
169 }
170
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300171 auto seq = swchain_lookup_table_[unique_id];
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300172
173 if (swchain_cache_.count(seq) == 0) {
174 swchain_cache_[seq] = {};
175 }
176
177 swchain_cache_[seq].bi = layer_data_.bi;
178 swchain_cache_[seq].fb = layer_data_.fb;
179 }
180}
181
182void HwcLayer::SwChainClearCache() {
183 swchain_cache_.clear();
184 swchain_lookup_table_.clear();
185 swchain_reassembled_ = false;
186}
187
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200188} // namespace android