1 /*
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 "HwcDisplay.h"
22 #include "bufferinfo/BufferInfoGetter.h"
23 #include "utils/log.h"
24
25 namespace android {
26
27 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
SetCursorPosition(int32_t,int32_t)28 HWC2::Error HwcLayer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
29 return HWC2::Error::None;
30 }
31
SetLayerBlendMode(int32_t mode)32 HWC2::Error HwcLayer::SetLayerBlendMode(int32_t mode) {
33 switch (static_cast<HWC2::BlendMode>(mode)) {
34 case HWC2::BlendMode::None:
35 blend_mode_ = BufferBlendMode::kNone;
36 break;
37 case HWC2::BlendMode::Premultiplied:
38 blend_mode_ = BufferBlendMode::kPreMult;
39 break;
40 case HWC2::BlendMode::Coverage:
41 blend_mode_ = BufferBlendMode::kCoverage;
42 break;
43 default:
44 ALOGE("Unknown blending mode b=%d", blend_mode_);
45 blend_mode_ = BufferBlendMode::kUndefined;
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 */
SetLayerBuffer(buffer_handle_t buffer,int32_t acquire_fence)54 HWC2::Error HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
55 int32_t acquire_fence) {
56 acquire_fence_ = UniqueFd(acquire_fence);
57 buffer_handle_ = buffer;
58 buffer_handle_updated_ = true;
59
60 return HWC2::Error::None;
61 }
62
63 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
SetLayerColor(hwc_color_t)64 HWC2::Error HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
65 // TODO(nobody): Put to client composition here?
66 return HWC2::Error::None;
67 }
68
SetLayerCompositionType(int32_t type)69 HWC2::Error HwcLayer::SetLayerCompositionType(int32_t type) {
70 sf_type_ = static_cast<HWC2::Composition>(type);
71 return HWC2::Error::None;
72 }
73
SetLayerDataspace(int32_t dataspace)74 HWC2::Error HwcLayer::SetLayerDataspace(int32_t dataspace) {
75 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
76 case HAL_DATASPACE_STANDARD_BT709:
77 color_space_ = BufferColorSpace::kItuRec709;
78 break;
79 case HAL_DATASPACE_STANDARD_BT601_625:
80 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
81 case HAL_DATASPACE_STANDARD_BT601_525:
82 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
83 color_space_ = BufferColorSpace::kItuRec601;
84 break;
85 case HAL_DATASPACE_STANDARD_BT2020:
86 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
87 color_space_ = BufferColorSpace::kItuRec2020;
88 break;
89 default:
90 color_space_ = BufferColorSpace::kUndefined;
91 }
92
93 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
94 case HAL_DATASPACE_RANGE_FULL:
95 sample_range_ = BufferSampleRange::kFullRange;
96 break;
97 case HAL_DATASPACE_RANGE_LIMITED:
98 sample_range_ = BufferSampleRange::kLimitedRange;
99 break;
100 default:
101 sample_range_ = BufferSampleRange::kUndefined;
102 }
103 return HWC2::Error::None;
104 }
105
SetLayerDisplayFrame(hwc_rect_t frame)106 HWC2::Error HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
107 layer_data_.pi.display_frame = frame;
108 return HWC2::Error::None;
109 }
110
SetLayerPlaneAlpha(float alpha)111 HWC2::Error HwcLayer::SetLayerPlaneAlpha(float alpha) {
112 layer_data_.pi.alpha = std::lround(alpha * UINT16_MAX);
113 return HWC2::Error::None;
114 }
115
116 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
SetLayerSidebandStream(const native_handle_t *)117 HWC2::Error HwcLayer::SetLayerSidebandStream(
118 const native_handle_t* /*stream*/) {
119 // TODO(nobody): We don't support sideband
120 return HWC2::Error::Unsupported;
121 }
122
SetLayerSourceCrop(hwc_frect_t crop)123 HWC2::Error HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
124 layer_data_.pi.source_crop = crop;
125 return HWC2::Error::None;
126 }
127
128 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
SetLayerSurfaceDamage(hwc_region_t)129 HWC2::Error HwcLayer::SetLayerSurfaceDamage(hwc_region_t /*damage*/) {
130 // TODO(nobody): We don't use surface damage, marking as unsupported
131 return HWC2::Error::None;
132 }
133
SetLayerTransform(int32_t transform)134 HWC2::Error HwcLayer::SetLayerTransform(int32_t transform) {
135 uint32_t l_transform = 0;
136
137 // 270* and 180* cannot be combined with flips. More specifically, they
138 // already contain both horizontal and vertical flips, so those fields are
139 // redundant in this case. 90* rotation can be combined with either horizontal
140 // flip or vertical flip, so treat it differently
141 if (transform == HWC_TRANSFORM_ROT_270) {
142 l_transform = LayerTransform::kRotate270;
143 } else if (transform == HWC_TRANSFORM_ROT_180) {
144 l_transform = LayerTransform::kRotate180;
145 } else {
146 if ((transform & HWC_TRANSFORM_FLIP_H) != 0)
147 l_transform |= LayerTransform::kFlipH;
148 if ((transform & HWC_TRANSFORM_FLIP_V) != 0)
149 l_transform |= LayerTransform::kFlipV;
150 if ((transform & HWC_TRANSFORM_ROT_90) != 0)
151 l_transform |= LayerTransform::kRotate90;
152 }
153
154 layer_data_.pi.transform = static_cast<LayerTransform>(l_transform);
155 return HWC2::Error::None;
156 }
157
158 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
SetLayerVisibleRegion(hwc_region_t)159 HWC2::Error HwcLayer::SetLayerVisibleRegion(hwc_region_t /*visible*/) {
160 // TODO(nobody): We don't use this information, marking as unsupported
161 return HWC2::Error::None;
162 }
163
SetLayerZOrder(uint32_t order)164 HWC2::Error HwcLayer::SetLayerZOrder(uint32_t order) {
165 z_order_ = order;
166 return HWC2::Error::None;
167 }
168
ImportFb()169 void HwcLayer::ImportFb() {
170 if (!IsLayerUsableAsDevice() || !buffer_handle_updated_) {
171 return;
172 }
173 buffer_handle_updated_ = false;
174
175 layer_data_.fb = {};
176
177 auto unique_id = BufferInfoGetter::GetInstance()->GetUniqueId(buffer_handle_);
178 if (unique_id && SwChainGetBufferFromCache(*unique_id)) {
179 return;
180 }
181
182 layer_data_.bi = BufferInfoGetter::GetInstance()->GetBoInfo(buffer_handle_);
183 if (!layer_data_.bi) {
184 ALOGW("Unable to get buffer information (0x%p)", buffer_handle_);
185 bi_get_failed_ = true;
186 return;
187 }
188
189 layer_data_
190 .fb = parent_->GetPipe().device->GetDrmFbImporter().GetOrCreateFbId(
191 &layer_data_.bi.value());
192
193 if (!layer_data_.fb) {
194 ALOGV("Unable to create framebuffer object for buffer 0x%p",
195 buffer_handle_);
196 fb_import_failed_ = true;
197 return;
198 }
199
200 if (unique_id) {
201 SwChainAddCurrentBuffer(*unique_id);
202 }
203 }
204
PopulateLayerData(bool test)205 void HwcLayer::PopulateLayerData(bool test) {
206 ImportFb();
207
208 if (blend_mode_ != BufferBlendMode::kUndefined) {
209 layer_data_.bi->blend_mode = blend_mode_;
210 }
211 if (color_space_ != BufferColorSpace::kUndefined) {
212 layer_data_.bi->color_space = color_space_;
213 }
214 if (sample_range_ != BufferSampleRange::kUndefined) {
215 layer_data_.bi->sample_range = sample_range_;
216 }
217
218 if (!test) {
219 layer_data_.acquire_fence = std::move(acquire_fence_);
220 }
221 }
222
223 /* SwapChain Cache */
224
SwChainGetBufferFromCache(BufferUniqueId unique_id)225 bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
226 if (swchain_lookup_table_.count(unique_id) == 0) {
227 return false;
228 }
229
230 int seq = swchain_lookup_table_[unique_id];
231
232 if (swchain_cache_.count(seq) == 0) {
233 return false;
234 }
235
236 auto& el = swchain_cache_[seq];
237 if (!el.bi) {
238 return false;
239 }
240
241 layer_data_.bi = el.bi;
242 layer_data_.fb = el.fb;
243
244 return true;
245 }
246
SwChainReassemble(BufferUniqueId unique_id)247 void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
248 if (swchain_lookup_table_.count(unique_id) != 0) {
249 if (swchain_lookup_table_[unique_id] ==
250 int(swchain_lookup_table_.size()) - 1) {
251 /* Skip same buffer */
252 return;
253 }
254 if (swchain_lookup_table_[unique_id] == 0) {
255 swchain_reassembled_ = true;
256 return;
257 }
258 /* Tracking error */
259 SwChainClearCache();
260 return;
261 }
262
263 swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
264 }
265
SwChainAddCurrentBuffer(BufferUniqueId unique_id)266 void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
267 if (!swchain_reassembled_) {
268 SwChainReassemble(unique_id);
269 }
270
271 if (swchain_reassembled_) {
272 if (swchain_lookup_table_.count(unique_id) == 0) {
273 SwChainClearCache();
274 return;
275 }
276
277 int seq = swchain_lookup_table_[unique_id];
278
279 if (swchain_cache_.count(seq) == 0) {
280 swchain_cache_[seq] = {};
281 }
282
283 swchain_cache_[seq].bi = layer_data_.bi;
284 swchain_cache_[seq].fb = layer_data_.fb;
285 }
286 }
287
SwChainClearCache()288 void HwcLayer::SwChainClearCache() {
289 swchain_cache_.clear();
290 swchain_lookup_table_.clear();
291 swchain_reassembled_ = false;
292 }
293
294 } // namespace android