1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hdi_layer.h"
17 #include "hdi_log.h"
18 #include <algorithm>
19 namespace OHOS {
20 namespace Rosen {
21 constexpr float SIXTY_SIX_INTERVAL_IN_MS = 66.f;
22 constexpr float THIRTY_THREE_INTERVAL_IN_MS = 33.f;
23 constexpr float SIXTEEN_INTERVAL_IN_MS = 16.67f;
24 constexpr float FPS_TO_MS = 1000000.f;
25 const std::string GENERIC_METADATA_KEY_SDR_RATIO = "SDRBrightnessRatio";
26 const std::string GENERIC_METADATA_KEY_BRIGHTNESS_NIT = "BrightnessNit";
27 const std::string GENERIC_METADATA_KEY_SOURCE_CROP_TUNING = "SourceCropTuning";
28
29 template<typename T>
Compare(const T& lhs, const T& rhs)30 bool Compare(const T& lhs, const T& rhs)
31 {
32 return lhs == rhs;
33 }
34
35 template<>
Compare(const GraphicIRect& rect1, const GraphicIRect& rect2)36 bool Compare(const GraphicIRect& rect1, const GraphicIRect& rect2)
37 {
38 return rect1.x == rect2.x && rect1.y == rect2.y && rect1.w == rect2.w && rect1.h == rect2.h;
39 }
40
41 template<typename T>
IsNeedSetInfoToDevice(const std::vector<T>& lhs, const std::vector<T>& rhs)42 bool IsNeedSetInfoToDevice(const std::vector<T>& lhs, const std::vector<T>& rhs)
43 {
44 if (lhs.size() != rhs.size()) {
45 return true;
46 }
47
48 for (decltype(lhs.size()) i = 0; i < lhs.size(); i++) {
49 if (!Compare(lhs[i], rhs[i])) {
50 return true;
51 }
52 }
53
54 return false;
55 }
56
57 /* rs create layer and set layer info begin */
CreateHdiLayer(uint32_t screenId)58 std::shared_ptr<HdiLayer> HdiLayer::CreateHdiLayer(uint32_t screenId)
59 {
60 return std::make_shared<HdiLayer>(screenId);
61 }
62
HdiLayer(uint32_t screenId)63 HdiLayer::HdiLayer(uint32_t screenId) : screenId_(screenId)
64 {
65 currBufferInfo_ = new LayerBufferInfo();
66 }
67
~HdiLayer()68 HdiLayer::~HdiLayer()
69 {
70 CloseLayer();
71 }
72
Init(const LayerInfoPtr &layerInfo)73 bool HdiLayer::Init(const LayerInfoPtr &layerInfo)
74 {
75 if (layerInfo == nullptr) {
76 return false;
77 }
78
79 if (CreateLayer(layerInfo) != GRAPHIC_DISPLAY_SUCCESS) {
80 return false;
81 }
82
83 return true;
84 }
85
InitDevice()86 int32_t HdiLayer::InitDevice()
87 {
88 if (device_ != nullptr) {
89 return GRAPHIC_DISPLAY_SUCCESS;
90 }
91
92 device_ = HdiDevice::GetInstance();
93 if (device_ == nullptr) {
94 HLOGE("device_ init failed.");
95 return GRAPHIC_DISPLAY_NULL_PTR;
96 }
97 return GRAPHIC_DISPLAY_SUCCESS;
98 }
99
SetHdiDeviceMock(HdiDevice* hdiDeviceMock)100 int32_t HdiLayer::SetHdiDeviceMock(HdiDevice* hdiDeviceMock)
101 {
102 if (hdiDeviceMock == nullptr) {
103 HLOGE("Input HdiDevice is nullptr");
104 return GRAPHIC_DISPLAY_NULL_PTR;
105 }
106
107 if (device_ != nullptr) {
108 HLOGD("device_ has been initialized");
109 return GRAPHIC_DISPLAY_SUCCESS;
110 }
111
112 device_ = hdiDeviceMock;
113 return GRAPHIC_DISPLAY_SUCCESS;
114 }
115
CreateLayer(const LayerInfoPtr &layerInfo)116 int32_t HdiLayer::CreateLayer(const LayerInfoPtr &layerInfo)
117 {
118 int32_t retCode = InitDevice();
119 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
120 return GRAPHIC_DISPLAY_NULL_PTR;
121 }
122
123 sptr<IConsumerSurface> surface = layerInfo->GetSurface();
124 if (surface == nullptr) {
125 HLOGE("Create layer failed because the consumer surface is nullptr.");
126 return GRAPHIC_DISPLAY_NULL_PTR;
127 }
128 bufferCacheCountMax_ = surface->GetQueueSize();
129 uint32_t layerId = INT_MAX;
130 GraphicLayerInfo hdiLayerInfo = {
131 .width = layerInfo->GetLayerSize().w,
132 .height = layerInfo->GetLayerSize().h,
133 .type = layerInfo->GetType(),
134 .pixFormat = GRAPHIC_PIXEL_FMT_RGBA_8888,
135 };
136 int32_t ret = device_->CreateLayer(screenId_, hdiLayerInfo, bufferCacheCountMax_, layerId);
137 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
138 HLOGE("Create hwc layer failed, ret is %{public}d", ret);
139 return ret;
140 }
141 ClearBufferCache();
142 bufferCache_.reserve(bufferCacheCountMax_);
143 layerId_ = layerId;
144
145 HLOGD("Create hwc layer succeed, layerId is %{public}u", layerId_);
146
147 CheckRet(device_->GetSupportedPresentTimestampType(screenId_, layerId_, supportedPresentTimestamptype_),
148 "GetSupportedPresentTimestamp");
149 return ret;
150 }
151
CloseLayer()152 void HdiLayer::CloseLayer()
153 {
154 if (layerId_ == INT_MAX) {
155 HLOGI("this layer has not been created");
156 return;
157 }
158
159 int32_t retCode = InitDevice();
160 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
161 return;
162 }
163
164 retCode = device_->CloseLayer(screenId_, layerId_);
165 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
166 HLOGE("Close hwc layer[%{public}u] failed, ret is %{public}d", layerId_, retCode);
167 }
168
169 HLOGD("Close hwc layer succeed, layerId is %{public}u", layerId_);
170 }
171
SetLayerAlpha()172 int32_t HdiLayer::SetLayerAlpha()
173 {
174 if (doLayerInfoCompare_) {
175 const GraphicLayerAlpha& layerAlpha1 = layerInfo_->GetAlpha();
176 const GraphicLayerAlpha& layerAlpha2 = prevLayerInfo_->GetAlpha();
177 bool isSame = layerAlpha1.enGlobalAlpha == layerAlpha2.enGlobalAlpha &&
178 layerAlpha1.enPixelAlpha == layerAlpha2.enPixelAlpha &&
179 layerAlpha1.alpha0 == layerAlpha2.alpha0 && layerAlpha1.alpha1 == layerAlpha2.alpha1 &&
180 layerAlpha1.gAlpha == layerAlpha2.gAlpha;
181 if (isSame) {
182 return GRAPHIC_DISPLAY_SUCCESS;
183 }
184 }
185
186 int32_t ret = device_->SetLayerAlpha(screenId_, layerId_, layerInfo_->GetAlpha());
187 return ret;
188 }
189
SetLayerSize()190 int32_t HdiLayer::SetLayerSize()
191 {
192 if (doLayerInfoCompare_ && Compare(layerInfo_->GetLayerSize(), prevLayerInfo_->GetLayerSize())) {
193 return GRAPHIC_DISPLAY_SUCCESS;
194 }
195
196 int32_t ret = device_->SetLayerSize(screenId_, layerId_, layerInfo_->GetLayerSize());
197 return ret;
198 }
199
SetTransformMode()200 int32_t HdiLayer::SetTransformMode()
201 {
202 if (layerInfo_->GetTransformType() == GraphicTransformType::GRAPHIC_ROTATE_BUTT || (doLayerInfoCompare_ &&
203 layerInfo_->GetTransformType() == prevLayerInfo_->GetTransformType())) {
204 return GRAPHIC_DISPLAY_SUCCESS;
205 }
206
207 GraphicTransformType transFormType = layerInfo_->GetTransformType();
208 int32_t ret = device_->SetTransformMode(screenId_, layerId_, transFormType);
209 return ret;
210 }
211
SetLayerVisibleRegion()212 int32_t HdiLayer::SetLayerVisibleRegion()
213 {
214 const std::vector<GraphicIRect>& curVisibles = layerInfo_->GetVisibleRegions();
215 bool isNeedSetInfoToDevice = true;
216 if (doLayerInfoCompare_) {
217 const std::vector<GraphicIRect>& prevVisibles = prevLayerInfo_->GetVisibleRegions();
218 if (!IsNeedSetInfoToDevice(curVisibles, prevVisibles)) {
219 isNeedSetInfoToDevice = false;
220 }
221 }
222
223 if (isNeedSetInfoToDevice) {
224 return device_->SetLayerVisibleRegion(screenId_, layerId_, curVisibles);
225 }
226
227 return GRAPHIC_DISPLAY_SUCCESS;
228 }
229
SetLayerDirtyRegion()230 int32_t HdiLayer::SetLayerDirtyRegion()
231 {
232 const std::vector<GraphicIRect>& curDirtyRegions = layerInfo_->GetDirtyRegions();
233 bool isNeedSetInfoToDevice = true;
234 if (doLayerInfoCompare_) {
235 const std::vector<GraphicIRect>& prevDirtyRegions = prevLayerInfo_->GetDirtyRegions();
236 if (!IsNeedSetInfoToDevice(curDirtyRegions, prevDirtyRegions)) {
237 isNeedSetInfoToDevice = false;
238 }
239 }
240
241 if (isNeedSetInfoToDevice) {
242 return device_->SetLayerDirtyRegion(screenId_, layerId_, curDirtyRegions);
243 }
244
245 return GRAPHIC_DISPLAY_SUCCESS;
246 }
247
CheckAndUpdateLayerBufferCahce(uint32_t sequence, uint32_t& index, std::vector<uint32_t>& deletingList)248 bool HdiLayer::CheckAndUpdateLayerBufferCahce(uint32_t sequence, uint32_t& index,
249 std::vector<uint32_t>& deletingList)
250 {
251 uint32_t bufferCacheSize = (uint32_t)bufferCache_.size();
252 for (uint32_t i = 0; i < bufferCacheSize; i++) {
253 if (bufferCache_[i] == sequence) {
254 index = i;
255 return true;
256 }
257 }
258
259 if (bufferCacheSize >= bufferCacheCountMax_) {
260 for (uint32_t i = 0; i < bufferCacheSize; i++) {
261 deletingList.push_back(i);
262 }
263 ClearBufferCache();
264 }
265 index = (uint32_t)bufferCache_.size();
266 bufferCache_.push_back(sequence);
267 return false;
268 }
269
SetLayerBuffer()270 int32_t HdiLayer::SetLayerBuffer()
271 {
272 sptr<SurfaceBuffer> currBuffer = layerInfo_->GetBuffer();
273 sptr<SyncFence> currAcquireFence = layerInfo_->GetAcquireFence();
274 if (currBuffer == nullptr) {
275 return GRAPHIC_DISPLAY_SUCCESS;
276 }
277 if (doLayerInfoCompare_) {
278 sptr<SurfaceBuffer> prevBuffer = prevLayerInfo_->GetBuffer();
279 sptr<SyncFence> prevAcquireFence = prevLayerInfo_->GetAcquireFence();
280 if (currBuffer == prevBuffer && currAcquireFence == prevAcquireFence) {
281 return GRAPHIC_DISPLAY_SUCCESS;
282 }
283 }
284
285 uint32_t index = INVALID_BUFFER_CACHE_INDEX;
286 std::vector<uint32_t> deletingList = {};
287 bool bufferCached = false;
288 if (bufferCacheCountMax_ == 0) {
289 ClearBufferCache();
290 HLOGE("The count of this layer buffer cache is 0.");
291 } else {
292 bufferCached = CheckAndUpdateLayerBufferCahce(currBuffer->GetSeqNum(), index, deletingList);
293 }
294
295 GraphicLayerBuffer layerBuffer;
296 layerBuffer.cacheIndex = index;
297 layerBuffer.acquireFence = currAcquireFence;
298 layerBuffer.deletingList = deletingList;
299 if (bufferCached && index < bufferCacheCountMax_) {
300 layerBuffer.handle = nullptr;
301 } else {
302 layerBuffer.handle = currBuffer->GetBufferHandle();
303 }
304 return device_->SetLayerBuffer(screenId_, layerId_, layerBuffer);
305 }
306
SetLayerCompositionType()307 int32_t HdiLayer::SetLayerCompositionType()
308 {
309 if (doLayerInfoCompare_ && layerInfo_->GetCompositionType() == prevLayerInfo_->GetCompositionType()) {
310 return GRAPHIC_DISPLAY_SUCCESS;
311 }
312
313 int32_t ret = device_->SetLayerCompositionType(screenId_, layerId_, layerInfo_->GetCompositionType());
314 return ret;
315 }
316
SetLayerBlendType()317 int32_t HdiLayer::SetLayerBlendType()
318 {
319 if (doLayerInfoCompare_ && layerInfo_->GetBlendType() == prevLayerInfo_->GetBlendType()) {
320 return GRAPHIC_DISPLAY_SUCCESS;
321 }
322
323 int32_t ret = device_->SetLayerBlendType(screenId_, layerId_, layerInfo_->GetBlendType());
324 return ret;
325 }
326
SetLayerCrop()327 int32_t HdiLayer::SetLayerCrop()
328 {
329 if (doLayerInfoCompare_ && Compare(layerInfo_->GetCropRect(), prevLayerInfo_->GetCropRect())) {
330 return GRAPHIC_DISPLAY_SUCCESS;
331 }
332
333 int32_t ret = device_->SetLayerCrop(screenId_, layerId_, layerInfo_->GetCropRect());
334 return ret;
335 }
336
SetLayerZorder()337 int32_t HdiLayer::SetLayerZorder()
338 {
339 if (doLayerInfoCompare_ && layerInfo_->GetZorder() == prevLayerInfo_->GetZorder()) {
340 return GRAPHIC_DISPLAY_SUCCESS;
341 }
342
343 int32_t ret = device_->SetLayerZorder(screenId_, layerId_, layerInfo_->GetZorder());
344 return ret;
345 }
346
SetLayerPreMulti()347 int32_t HdiLayer::SetLayerPreMulti()
348 {
349 if (doLayerInfoCompare_ && layerInfo_->IsPreMulti() == prevLayerInfo_->IsPreMulti()) {
350 return GRAPHIC_DISPLAY_SUCCESS;
351 }
352
353 int32_t ret = device_->SetLayerPreMulti(screenId_, layerId_, layerInfo_->IsPreMulti());
354 return ret;
355 }
356
SetLayerColor()357 int32_t HdiLayer::SetLayerColor()
358 {
359 if (doLayerInfoCompare_ && layerInfo_->GetLayerColor().r == prevLayerInfo_->GetLayerColor().r
360 && layerInfo_->GetLayerColor().g == prevLayerInfo_->GetLayerColor().g
361 && layerInfo_->GetLayerColor().b == prevLayerInfo_->GetLayerColor().b
362 && layerInfo_->GetLayerColor().a == prevLayerInfo_->GetLayerColor().a) {
363 return GRAPHIC_DISPLAY_SUCCESS;
364 }
365
366 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
367 device_->SetLayerColor(screenId_, layerId_, layerInfo_->GetLayerColor());
368 return GRAPHIC_DISPLAY_SUCCESS;
369 }
370
SetLayerColorTransform()371 int32_t HdiLayer::SetLayerColorTransform()
372 {
373 const std::vector<float>& curMatrix = layerInfo_->GetColorTransform();
374 bool isNeedSetInfoToDevice = true;
375 if (doLayerInfoCompare_) {
376 const std::vector<float>& prevMatrix = prevLayerInfo_->GetColorTransform();
377 if (!IsNeedSetInfoToDevice(curMatrix, prevMatrix)) {
378 isNeedSetInfoToDevice = false;
379 }
380 }
381 if (isNeedSetInfoToDevice) {
382 // This method may not be supported, the return value is not check here
383 device_->SetLayerColorTransform(screenId_, layerId_, curMatrix);
384 }
385
386 return GRAPHIC_DISPLAY_SUCCESS;
387 }
388
SetLayerColorDataSpace()389 int32_t HdiLayer::SetLayerColorDataSpace()
390 {
391 if (doLayerInfoCompare_ && layerInfo_->GetColorDataSpace() == prevLayerInfo_->GetColorDataSpace()) {
392 return GRAPHIC_DISPLAY_SUCCESS;
393 }
394
395 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
396 device_->SetLayerColorDataSpace(screenId_, layerId_, layerInfo_->GetColorDataSpace());
397 return GRAPHIC_DISPLAY_SUCCESS;
398 }
399
IsSameLayerMetaData()400 bool HdiLayer::IsSameLayerMetaData()
401 {
402 bool isSame = false;
403 std::vector<GraphicHDRMetaData>& metaData = layerInfo_->GetMetaData();
404 std::vector<GraphicHDRMetaData>& prevMetaData = prevLayerInfo_->GetMetaData();
405 if (metaData.size() == prevMetaData.size()) {
406 isSame = true;
407 size_t metaDeataSize = metaData.size();
408 for (size_t i = 0; i < metaDeataSize; i++) {
409 if (metaData[i].key != prevMetaData[i].key || metaData[i].value != prevMetaData[i].value) {
410 isSame = false;
411 break;
412 }
413 }
414 }
415 return isSame;
416 }
417
SetLayerMetaData()418 int32_t HdiLayer::SetLayerMetaData()
419 {
420 if (doLayerInfoCompare_) {
421 bool isSame = IsSameLayerMetaData();
422 if (isSame) {
423 return GRAPHIC_DISPLAY_SUCCESS;
424 }
425 }
426
427 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
428 device_->SetLayerMetaData(screenId_, layerId_, layerInfo_->GetMetaData());
429 return GRAPHIC_DISPLAY_SUCCESS;
430 }
431
432
IsSameLayerMetaDataSet()433 bool HdiLayer::IsSameLayerMetaDataSet()
434 {
435 bool isSame = false;
436 GraphicHDRMetaDataSet &metaDataSet = layerInfo_->GetMetaDataSet();
437 GraphicHDRMetaDataSet &prevMetaDataSet = prevLayerInfo_->GetMetaDataSet();
438 if (metaDataSet.key == prevMetaDataSet.key &&
439 metaDataSet.metaData.size() == prevMetaDataSet.metaData.size()) {
440 isSame = true;
441 size_t metaDeataSetSize = metaDataSet.metaData.size();
442 for (size_t i = 0; i < metaDeataSetSize; i++) {
443 if (metaDataSet.metaData[i] != prevMetaDataSet.metaData[i]) {
444 isSame = false;
445 break;
446 }
447 }
448 }
449 return isSame;
450 }
451
SetLayerMetaDataSet()452 int32_t HdiLayer::SetLayerMetaDataSet()
453 {
454 if (doLayerInfoCompare_) {
455 bool isSame = IsSameLayerMetaDataSet();
456 if (isSame) {
457 return GRAPHIC_DISPLAY_SUCCESS;
458 }
459 }
460
461 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
462 device_->SetLayerMetaDataSet(screenId_, layerId_, layerInfo_->GetMetaDataSet().key,
463 layerInfo_->GetMetaDataSet().metaData);
464 return GRAPHIC_DISPLAY_SUCCESS;
465 }
466
SetLayerTunnelHandle()467 int32_t HdiLayer::SetLayerTunnelHandle()
468 {
469 if (!layerInfo_->GetTunnelHandleChange()) {
470 return GRAPHIC_DISPLAY_SUCCESS;
471 }
472 int32_t ret = GRAPHIC_DISPLAY_SUCCESS;
473 if (layerInfo_->GetTunnelHandle() == nullptr) {
474 ret = device_->SetLayerTunnelHandle(screenId_, layerId_, nullptr);
475 } else {
476 ret = device_->SetLayerTunnelHandle(screenId_, layerId_, layerInfo_->GetTunnelHandle()->GetHandle());
477 }
478 return ret;
479 }
480
SetLayerPresentTimestamp()481 int32_t HdiLayer::SetLayerPresentTimestamp()
482 {
483 if (supportedPresentTimestamptype_ == GraphicPresentTimestampType::GRAPHIC_DISPLAY_PTS_UNSUPPORTED) {
484 return GRAPHIC_DISPLAY_SUCCESS;
485 }
486 layerInfo_->SetIsSupportedPresentTimestamp(true);
487 GraphicPresentTimestamp timestamp = {GRAPHIC_DISPLAY_PTS_UNSUPPORTED, 0};
488 int32_t ret = device_->GetPresentTimestamp(screenId_, layerId_, timestamp);
489 GraphicPresentTimestamp graphicTimestamp = {
490 .type = static_cast<GraphicPresentTimestampType>(timestamp.type),
491 .time = timestamp.time,
492 };
493
494 CheckRet(ret, "GetPresentTimestamp");
495 if (ret == GRAPHIC_DISPLAY_SUCCESS) {
496 layerInfo_->SetPresentTimestamp(graphicTimestamp);
497 }
498 return ret;
499 }
500
SetLayerMaskInfo()501 int32_t HdiLayer::SetLayerMaskInfo()
502 {
503 return device_->SetLayerMaskInfo(screenId_, layerId_, static_cast<uint32_t>(layerInfo_->GetLayerMaskInfo()));
504 }
505
SetHdiLayerInfo()506 int32_t HdiLayer::SetHdiLayerInfo()
507 {
508 /*
509 Some hardware platforms may not support all layer settings.
510 If the current function is not supported, continue other layer settings.
511 */
512 int32_t ret = InitDevice();
513 if (ret != GRAPHIC_DISPLAY_SUCCESS || layerInfo_ == nullptr) {
514 return GRAPHIC_DISPLAY_FAILURE;
515 }
516
517 // All layer properities need to set to hwc when the layer is created firstly or the previous layer's composition
518 // type is COMPOSITION_DEVICE for COMPOSITION_DEVICE can not reuse COMPOSITION_CLIENT layers info.
519 doLayerInfoCompare_ = prevLayerInfo_ != nullptr &&
520 prevLayerInfo_->GetCompositionType() == GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE;
521
522 ret = SetLayerAlpha();
523 CheckRet(ret, "SetLayerAlpha");
524 ret = SetLayerSize();
525 CheckRet(ret, "SetLayerSize");
526 ret = SetTransformMode();
527 CheckRet(ret, "SetTransformMode");
528 ret = SetLayerVisibleRegion();
529 CheckRet(ret, "SetLayerVisibleRegion");
530 ret = SetLayerDirtyRegion();
531 CheckRet(ret, "SetLayerDirtyRegion");
532 ret = SetLayerCrop();
533 CheckRet(ret, "SetLayerCrop");
534 ret = SetLayerBuffer();
535 CheckRet(ret, "SetLayerBuffer");
536 ret = SetLayerCompositionType();
537 CheckRet(ret, "SetLayerCompositionType");
538 ret = SetLayerBlendType();
539 CheckRet(ret, "SetLayerBlendType");
540 ret = SetLayerZorder();
541 CheckRet(ret, "SetLayerZorder");
542 ret = SetLayerPreMulti();
543 CheckRet(ret, "SetLayerPreMulti");
544 ret = SetLayerColor();
545 CheckRet(ret, "SetLayerColor");
546 // This method may not be supported, the return value is not check here
547 (void)SetLayerColorTransform();
548 ret = SetLayerColorDataSpace();
549 CheckRet(ret, "SetLayerColorDataSpace");
550 ret = SetLayerMetaData();
551 CheckRet(ret, "SetLayerMetaData");
552 ret = SetLayerMetaDataSet();
553 CheckRet(ret, "SetLayerMetaDataSet");
554 ret = SetLayerTunnelHandle();
555 CheckRet(ret, "SetLayerTunnelHandle");
556 ret = SetLayerPresentTimestamp();
557 CheckRet(ret, "SetLayerPresentTimestamp");
558 ret = SetLayerMaskInfo();
559 CheckRet(ret, "SetLayerMask");
560 ret = SetPerFrameParameters();
561 CheckRet(ret, "SetPerFrameParameters");
562
563 return GRAPHIC_DISPLAY_SUCCESS;
564 }
565
GetLayerId() const566 uint32_t HdiLayer::GetLayerId() const
567 {
568 return layerId_;
569 }
570
GetLayerInfo()571 const LayerInfoPtr HdiLayer::GetLayerInfo()
572 {
573 return layerInfo_;
574 }
575
SetLayerStatus(bool inUsing)576 void HdiLayer::SetLayerStatus(bool inUsing)
577 {
578 isInUsing_ = inUsing;
579 }
580
GetLayerStatus() const581 bool HdiLayer::GetLayerStatus() const
582 {
583 return isInUsing_;
584 }
585
UpdateLayerInfo(const LayerInfoPtr &layerInfo)586 void HdiLayer::UpdateLayerInfo(const LayerInfoPtr &layerInfo)
587 {
588 if (layerInfo == nullptr) {
589 return;
590 }
591
592 /* If the layer is updated, it indicates that the layer will be used
593 * in the frame. Mark it.
594 */
595
596 isInUsing_ = true;
597 layerInfo_ = layerInfo;
598
599 prevSbuffer_ = currBufferInfo_->sbuffer_;
600 currBufferInfo_->sbuffer_ = layerInfo_->GetBuffer();
601 }
602
SetReleaseFence(const sptr<SyncFence> &layerReleaseFence)603 void HdiLayer::SetReleaseFence(const sptr<SyncFence> &layerReleaseFence)
604 {
605 if (currBufferInfo_ == nullptr || layerReleaseFence == nullptr) {
606 return;
607 }
608 currBufferInfo_->releaseFence_ = layerReleaseFence;
609 }
610
GetReleaseFence() const611 sptr<SyncFence> HdiLayer::GetReleaseFence() const
612 {
613 if (currBufferInfo_ == nullptr) {
614 return SyncFence::InvalidFence();
615 }
616 return currBufferInfo_->releaseFence_;
617 }
618
RecordPresentTime(int64_t timestamp)619 bool HdiLayer::RecordPresentTime(int64_t timestamp)
620 {
621 std::unique_lock<std::mutex> lock(mutex_);
622 if (currBufferInfo_->sbuffer_ != prevSbuffer_) {
623 presentTimeRecords_[count_].presentTime = timestamp;
624 presentTimeRecords_[count_].windowsName = layerInfo_->GetWindowsName();
625 count_ = (count_ + 1) % FRAME_RECORDS_NUM;
626 return true;
627 }
628 return false;
629 }
630
SelectHitchsInfo(std::string windowName, std::string &result)631 void HdiLayer::SelectHitchsInfo(std::string windowName, std::string &result)
632 {
633 int sixtySixTimes = 0;
634 int thirtyThreeTimes = 0;
635 int sixteenTimes = 0;
636 {
637 std::unique_lock<std::mutex> lock(mutex_);
638 const uint32_t offset = count_;
639 for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
640 uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
641 auto windowsName = presentTimeRecords_[order].windowsName;
642 auto iter = std::find(windowsName.begin(), windowsName.end(), windowName);
643 int64_t lastFlushTimestamp = 0;
644 int64_t nowFlushTimestamp = 0;
645 if (iter != windowsName.end()) {
646 nowFlushTimestamp = presentTimeRecords_[order].presentTime;
647 if (lastFlushTimestamp != 0) {
648 float time = (nowFlushTimestamp - lastFlushTimestamp) / FPS_TO_MS;
649 if (time > SIXTY_SIX_INTERVAL_IN_MS) {
650 sixtySixTimes++;
651 } else if (time > THIRTY_THREE_INTERVAL_IN_MS) {
652 thirtyThreeTimes++;
653 } else if (time > SIXTEEN_INTERVAL_IN_MS) {
654 sixteenTimes++;
655 }
656 }
657 }
658 lastFlushTimestamp = nowFlushTimestamp;
659 }
660 }
661 result += "more than 66 ms " + std::to_string(sixtySixTimes) + "\n";
662 result += "more than 33 ms " + std::to_string(thirtyThreeTimes) + "\n";
663 result += "more than 16.67 ms " + std::to_string(sixteenTimes) + "\n";
664 }
665
RecordMergedPresentTime(int64_t timestamp)666 void HdiLayer::RecordMergedPresentTime(int64_t timestamp)
667 {
668 std::unique_lock<std::mutex> lock(mutex_);
669 mergedPresentTimeRecords_[mergedCount_] = timestamp;
670 mergedCount_ = (mergedCount_ + 1) % FRAME_RECORDS_NUM;
671 }
672
MergeWithFramebufferFence(const sptr<SyncFence> &fbAcquireFence)673 void HdiLayer::MergeWithFramebufferFence(const sptr<SyncFence> &fbAcquireFence)
674 {
675 if (currBufferInfo_ == nullptr || fbAcquireFence == nullptr) {
676 return;
677 }
678 currBufferInfo_->releaseFence_ = Merge(currBufferInfo_->releaseFence_, fbAcquireFence);
679 }
680
MergeWithLayerFence(const sptr<SyncFence> &layerReleaseFence)681 void HdiLayer::MergeWithLayerFence(const sptr<SyncFence> &layerReleaseFence)
682 {
683 if (currBufferInfo_ == nullptr || layerReleaseFence == nullptr) {
684 return;
685 }
686 currBufferInfo_->releaseFence_ = Merge(currBufferInfo_->releaseFence_, layerReleaseFence);
687 }
688
UpdateCompositionType(GraphicCompositionType type)689 void HdiLayer::UpdateCompositionType(GraphicCompositionType type)
690 {
691 if (layerInfo_ == nullptr) {
692 return;
693 }
694
695 layerInfo_->SetCompositionType(type);
696 }
697 /* backend get layer info end */
698
Merge(const sptr<SyncFence> &fence1, const sptr<SyncFence> &fence2)699 sptr<SyncFence> HdiLayer::Merge(const sptr<SyncFence> &fence1, const sptr<SyncFence> &fence2)
700 {
701 return SyncFence::MergeFence("ReleaseFence", fence1, fence2);
702 }
703
CheckRet(int32_t ret, const char* func)704 void HdiLayer::CheckRet(int32_t ret, const char* func)
705 {
706 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
707 HLOGD("call hdi %{public}s failed, ret is %{public}d", func, ret);
708 }
709 }
710
SavePrevLayerInfo()711 void HdiLayer::SavePrevLayerInfo()
712 {
713 if (prevLayerInfo_ == nullptr) {
714 prevLayerInfo_ = HdiLayerInfo::CreateHdiLayerInfo();
715 }
716 prevLayerInfo_->CopyLayerInfo(layerInfo_);
717 }
718
Dump(std::string &result)719 void HdiLayer::Dump(std::string &result)
720 {
721 std::unique_lock<std::mutex> lock(mutex_);
722 const uint32_t offset = count_;
723 for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
724 uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
725 result += std::to_string(presentTimeRecords_[order].presentTime) + "\n";
726 }
727 }
728
DumpByName(std::string windowName, std::string &result)729 void HdiLayer::DumpByName(std::string windowName, std::string &result)
730 {
731 std::unique_lock<std::mutex> lock(mutex_);
732 const uint32_t offset = count_;
733 for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
734 uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
735 auto windowsName = presentTimeRecords_[order].windowsName;
736 auto iter = std::find(windowsName.begin(), windowsName.end(), windowName);
737 if (iter != windowsName.end()) {
738 result += std::to_string(presentTimeRecords_[order].presentTime) + "\n";
739 }
740 }
741 }
742
DumpMergedResult(std::string &result)743 void HdiLayer::DumpMergedResult(std::string &result)
744 {
745 std::unique_lock<std::mutex> lock(mutex_);
746 const uint32_t offset = mergedCount_;
747 for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
748 uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
749 result += std::to_string(mergedPresentTimeRecords_[order]) + "\n";
750 }
751 }
752
ClearDump()753 void HdiLayer::ClearDump()
754 {
755 std::vector<std::string> windowName = {};
756 FPSInfo defaultFPSInfo = {0, windowName};
757
758 std::unique_lock<std::mutex> lock(mutex_);
759 presentTimeRecords_.fill(defaultFPSInfo);
760 mergedPresentTimeRecords_.fill(0);
761 }
762
SetPerFrameParameters()763 int32_t HdiLayer::SetPerFrameParameters()
764 {
765 const auto& supportedKeys = device_->GetSupportedLayerPerFrameParameterKey();
766 int32_t ret = GRAPHIC_DISPLAY_SUCCESS;
767 for (const auto& key : supportedKeys) {
768 if (key == GENERIC_METADATA_KEY_BRIGHTNESS_NIT) {
769 ret = SetPerFrameParameterDisplayNit();
770 CheckRet(ret, "SetPerFrameParameterDisplayNit");
771 } else if (key == GENERIC_METADATA_KEY_SDR_RATIO) {
772 ret = SetPerFrameParameterBrightnessRatio();
773 CheckRet(ret, "SetPerFrameParameterBrightnessRatio");
774 } else if (key == GENERIC_METADATA_KEY_SOURCE_CROP_TUNING) {
775 ret = SetPerFrameLayerSourceTuning();
776 CheckRet(ret, "SetLayerSourceTuning");
777 }
778 }
779 return ret;
780 }
781
SetPerFrameParameterDisplayNit()782 int32_t HdiLayer::SetPerFrameParameterDisplayNit()
783 {
784 if (doLayerInfoCompare_) {
785 if (layerInfo_->GetDisplayNit() == prevLayerInfo_->GetDisplayNit()) {
786 return GRAPHIC_DISPLAY_SUCCESS;
787 }
788 }
789
790 std::vector<int8_t> valueBlob(sizeof(int32_t));
791 *reinterpret_cast<int32_t*>(valueBlob.data()) = layerInfo_->GetDisplayNit();
792 return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_BRIGHTNESS_NIT, valueBlob);
793 }
794
SetPerFrameParameterBrightnessRatio()795 int32_t HdiLayer::SetPerFrameParameterBrightnessRatio()
796 {
797 if (doLayerInfoCompare_) {
798 if (layerInfo_->GetBrightnessRatio() == prevLayerInfo_->GetBrightnessRatio()) {
799 return GRAPHIC_DISPLAY_SUCCESS;
800 }
801 }
802
803 std::vector<int8_t> valueBlob(sizeof(float));
804 *reinterpret_cast<float*>(valueBlob.data()) = layerInfo_->GetBrightnessRatio();
805 return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_SDR_RATIO, valueBlob);
806 }
807
SetPerFrameLayerSourceTuning()808 int32_t HdiLayer::SetPerFrameLayerSourceTuning()
809 {
810 if (doLayerInfoCompare_) {
811 if (layerInfo_->GetLayerSourceTuning() == prevLayerInfo_->GetLayerSourceTuning()) {
812 return GRAPHIC_DISPLAY_SUCCESS;
813 }
814 }
815
816 std::vector<int8_t> valueBlob(sizeof(int32_t));
817 *reinterpret_cast<int32_t*>(valueBlob.data()) = layerInfo_->GetLayerSourceTuning();
818 return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_SOURCE_CROP_TUNING, valueBlob);
819 }
820
ClearBufferCache()821 void HdiLayer::ClearBufferCache()
822 {
823 if (bufferCache_.empty()) {
824 return;
825 }
826 int32_t ret = device_->ClearLayerBuffer(screenId_, layerId_);
827 CheckRet(ret, "ClearLayerBuffer");
828 bufferCache_.clear();
829 }
830 } // namespace Rosen
831 } // namespace OHOS
832