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