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