• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", 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   layer_data_.acquire_fence = MakeSharedFd(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()205 void HwcLayer::PopulateLayerData() {
206   ImportFb();
207 
208   if (!layer_data_.bi) {
209     ALOGE("%s: Invalid state", __func__);
210     return;
211   }
212 
213   if (blend_mode_ != BufferBlendMode::kUndefined) {
214     layer_data_.bi->blend_mode = blend_mode_;
215   }
216   if (color_space_ != BufferColorSpace::kUndefined) {
217     layer_data_.bi->color_space = color_space_;
218   }
219   if (sample_range_ != BufferSampleRange::kUndefined) {
220     layer_data_.bi->sample_range = sample_range_;
221   }
222 }
223 
224 /* SwapChain Cache */
225 
SwChainGetBufferFromCache(BufferUniqueId unique_id)226 bool HwcLayer::SwChainGetBufferFromCache(BufferUniqueId unique_id) {
227   if (swchain_lookup_table_.count(unique_id) == 0) {
228     return false;
229   }
230 
231   auto seq = swchain_lookup_table_[unique_id];
232 
233   if (swchain_cache_.count(seq) == 0) {
234     return false;
235   }
236 
237   auto& el = swchain_cache_[seq];
238   if (!el.bi) {
239     return false;
240   }
241 
242   layer_data_.bi = el.bi;
243   layer_data_.fb = el.fb;
244 
245   return true;
246 }
247 
SwChainReassemble(BufferUniqueId unique_id)248 void HwcLayer::SwChainReassemble(BufferUniqueId unique_id) {
249   if (swchain_lookup_table_.count(unique_id) != 0) {
250     if (swchain_lookup_table_[unique_id] ==
251         int(swchain_lookup_table_.size()) - 1) {
252       /* Skip same buffer */
253       return;
254     }
255     if (swchain_lookup_table_[unique_id] == 0) {
256       swchain_reassembled_ = true;
257       return;
258     }
259     /* Tracking error */
260     SwChainClearCache();
261     return;
262   }
263 
264   swchain_lookup_table_[unique_id] = int(swchain_lookup_table_.size());
265 }
266 
SwChainAddCurrentBuffer(BufferUniqueId unique_id)267 void HwcLayer::SwChainAddCurrentBuffer(BufferUniqueId unique_id) {
268   if (!swchain_reassembled_) {
269     SwChainReassemble(unique_id);
270   }
271 
272   if (swchain_reassembled_) {
273     if (swchain_lookup_table_.count(unique_id) == 0) {
274       SwChainClearCache();
275       return;
276     }
277 
278     auto seq = swchain_lookup_table_[unique_id];
279 
280     if (swchain_cache_.count(seq) == 0) {
281       swchain_cache_[seq] = {};
282     }
283 
284     swchain_cache_[seq].bi = layer_data_.bi;
285     swchain_cache_[seq].fb = layer_data_.fb;
286   }
287 }
288 
SwChainClearCache()289 void HwcLayer::SwChainClearCache() {
290   swchain_cache_.clear();
291   swchain_lookup_table_.clear();
292   swchain_reassembled_ = false;
293 }
294 
295 }  // namespace android