• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = GRAPHIC_LAYER_TYPE_GRAPHIC,
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     // The crop needs to be set in the first order
531     ret = SetLayerCrop();
532     CheckRet(ret, "SetLayerCrop");
533     // The data space contained in the layerbuffer needs to be set in the second order
534     ret = SetLayerBuffer();
535     CheckRet(ret, "SetLayerBuffer");
536     // The dirty region needs to be set in the third order
537     ret = SetLayerDirtyRegion();
538     CheckRet(ret, "SetLayerDirtyRegion");
539     ret = SetLayerCompositionType();
540     CheckRet(ret, "SetLayerCompositionType");
541     ret = SetLayerBlendType();
542     CheckRet(ret, "SetLayerBlendType");
543     ret = SetLayerZorder();
544     CheckRet(ret, "SetLayerZorder");
545     ret = SetLayerPreMulti();
546     CheckRet(ret, "SetLayerPreMulti");
547     ret = SetLayerColor();
548     CheckRet(ret, "SetLayerColor");
549     // This method may not be supported, the return value is not check here
550     (void)SetLayerColorTransform();
551     ret = SetLayerColorDataSpace();
552     CheckRet(ret, "SetLayerColorDataSpace");
553     ret = SetLayerMetaData();
554     CheckRet(ret, "SetLayerMetaData");
555     ret = SetLayerMetaDataSet();
556     CheckRet(ret, "SetLayerMetaDataSet");
557     ret = SetLayerTunnelHandle();
558     CheckRet(ret, "SetLayerTunnelHandle");
559     ret = SetLayerPresentTimestamp();
560     CheckRet(ret, "SetLayerPresentTimestamp");
561     ret = SetLayerMaskInfo();
562     CheckRet(ret, "SetLayerMask");
563     ret = SetPerFrameParameters();
564     CheckRet(ret, "SetPerFrameParameters");
565 
566     return GRAPHIC_DISPLAY_SUCCESS;
567 }
568 
GetLayerId() const569 uint32_t HdiLayer::GetLayerId() const
570 {
571     return layerId_;
572 }
573 
GetLayerInfo()574 const LayerInfoPtr HdiLayer::GetLayerInfo()
575 {
576     return layerInfo_;
577 }
578 
SetLayerStatus(bool inUsing)579 void HdiLayer::SetLayerStatus(bool inUsing)
580 {
581     isInUsing_ = inUsing;
582 }
583 
GetLayerStatus() const584 bool HdiLayer::GetLayerStatus() const
585 {
586     return isInUsing_;
587 }
588 
UpdateLayerInfo(const LayerInfoPtr & layerInfo)589 void HdiLayer::UpdateLayerInfo(const LayerInfoPtr &layerInfo)
590 {
591     if (layerInfo == nullptr) {
592         return;
593     }
594 
595     /* If the layer is updated, it indicates that the layer will be used
596      * in the frame. Mark it.
597      */
598 
599     isInUsing_ = true;
600     layerInfo_ = layerInfo;
601 
602     prevSbuffer_ = currBufferInfo_->sbuffer_;
603     currBufferInfo_->sbuffer_ = layerInfo_->GetBuffer();
604 }
605 
SetReleaseFence(const sptr<SyncFence> & layerReleaseFence)606 void HdiLayer::SetReleaseFence(const sptr<SyncFence> &layerReleaseFence)
607 {
608     if (currBufferInfo_ == nullptr || layerReleaseFence == nullptr) {
609         return;
610     }
611     currBufferInfo_->releaseFence_ = layerReleaseFence;
612 }
613 
GetReleaseFence() const614 sptr<SyncFence> HdiLayer::GetReleaseFence() const
615 {
616     if (currBufferInfo_ == nullptr) {
617         return SyncFence::InvalidFence();
618     }
619     return currBufferInfo_->releaseFence_;
620 }
621 
RecordPresentTime(int64_t timestamp)622 bool HdiLayer::RecordPresentTime(int64_t timestamp)
623 {
624     std::unique_lock<std::mutex> lock(mutex_);
625     if (currBufferInfo_->sbuffer_ != prevSbuffer_) {
626         presentTimeRecords_[count_].presentTime = timestamp;
627         presentTimeRecords_[count_].windowsName = layerInfo_->GetWindowsName();
628         count_ = (count_ + 1) % FRAME_RECORDS_NUM;
629         return true;
630     }
631     return false;
632 }
633 
SelectHitchsInfo(std::string windowName,std::string & result)634 void HdiLayer::SelectHitchsInfo(std::string windowName, std::string &result)
635 {
636     int sixtySixTimes = 0;
637     int thirtyThreeTimes = 0;
638     int sixteenTimes = 0;
639     int64_t lastFlushTimestamp = 0;
640     int64_t nowFlushTimestamp = 0;
641     {
642         std::unique_lock<std::mutex> lock(mutex_);
643         const uint32_t offset = count_;
644         for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
645             uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
646             auto windowsName = presentTimeRecords_[order].windowsName;
647             auto iter = std::find(windowsName.begin(), windowsName.end(), windowName);
648             if (iter != windowsName.end()) {
649                 nowFlushTimestamp = presentTimeRecords_[order].presentTime;
650                 if (lastFlushTimestamp != 0) {
651                     float time = (nowFlushTimestamp - lastFlushTimestamp) / FPS_TO_MS;
652                     if (time > SIXTY_SIX_INTERVAL_IN_MS) {
653                         sixtySixTimes++;
654                     } else if (time > THIRTY_THREE_INTERVAL_IN_MS) {
655                         thirtyThreeTimes++;
656                     } else if (time > SIXTEEN_INTERVAL_IN_MS) {
657                         sixteenTimes++;
658                     }
659                 }
660             }
661             lastFlushTimestamp = nowFlushTimestamp;
662         }
663     }
664     result += "more than 66 ms       " + std::to_string(sixtySixTimes) + "\n";
665     result += "more than 33 ms       " + std::to_string(thirtyThreeTimes) + "\n";
666     result += "more than 16.67 ms    " + std::to_string(sixteenTimes) + "\n";
667 }
668 
RecordMergedPresentTime(int64_t timestamp)669 void HdiLayer::RecordMergedPresentTime(int64_t timestamp)
670 {
671     std::unique_lock<std::mutex> lock(mutex_);
672     mergedPresentTimeRecords_[mergedCount_] = timestamp;
673     mergedCount_ = (mergedCount_ + 1) % FRAME_RECORDS_NUM;
674 }
675 
MergeWithFramebufferFence(const sptr<SyncFence> & fbAcquireFence)676 void HdiLayer::MergeWithFramebufferFence(const sptr<SyncFence> &fbAcquireFence)
677 {
678     if (currBufferInfo_ == nullptr || fbAcquireFence == nullptr) {
679         return;
680     }
681     currBufferInfo_->releaseFence_ = Merge(currBufferInfo_->releaseFence_, fbAcquireFence);
682 }
683 
MergeWithLayerFence(const sptr<SyncFence> & layerReleaseFence)684 void HdiLayer::MergeWithLayerFence(const sptr<SyncFence> &layerReleaseFence)
685 {
686     if (currBufferInfo_ == nullptr || layerReleaseFence == nullptr) {
687         return;
688     }
689     currBufferInfo_->releaseFence_ = Merge(currBufferInfo_->releaseFence_, layerReleaseFence);
690 }
691 
UpdateCompositionType(GraphicCompositionType type)692 void HdiLayer::UpdateCompositionType(GraphicCompositionType type)
693 {
694     if (layerInfo_ == nullptr) {
695         return;
696     }
697 
698     layerInfo_->SetCompositionType(type);
699 }
700 /* backend get layer info end */
701 
Merge(const sptr<SyncFence> & fence1,const sptr<SyncFence> & fence2)702 sptr<SyncFence> HdiLayer::Merge(const sptr<SyncFence> &fence1, const sptr<SyncFence> &fence2)
703 {
704     return SyncFence::MergeFence("ReleaseFence", fence1, fence2);
705 }
706 
CheckRet(int32_t ret,const char * func)707 void HdiLayer::CheckRet(int32_t ret, const char* func)
708 {
709     if (ret != GRAPHIC_DISPLAY_SUCCESS) {
710         HLOGD("call hdi %{public}s failed, ret is %{public}d", func, ret);
711     }
712 }
713 
SavePrevLayerInfo()714 void HdiLayer::SavePrevLayerInfo()
715 {
716     if (prevLayerInfo_ == nullptr) {
717         prevLayerInfo_ = HdiLayerInfo::CreateHdiLayerInfo();
718     }
719     prevLayerInfo_->CopyLayerInfo(layerInfo_);
720 }
721 
Dump(std::string & result)722 void HdiLayer::Dump(std::string &result)
723 {
724     std::unique_lock<std::mutex> lock(mutex_);
725     const uint32_t offset = count_;
726     for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
727         uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
728         result += std::to_string(presentTimeRecords_[order].presentTime) + "\n";
729     }
730 }
731 
DumpByName(std::string windowName,std::string & result)732 void HdiLayer::DumpByName(std::string windowName, std::string &result)
733 {
734     std::unique_lock<std::mutex> lock(mutex_);
735     const uint32_t offset = count_;
736     for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
737         uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
738         auto windowsName = presentTimeRecords_[order].windowsName;
739         auto iter = std::find(windowsName.begin(), windowsName.end(), windowName);
740         if (iter != windowsName.end()) {
741             result += std::to_string(presentTimeRecords_[order].presentTime) + "\n";
742         }
743     }
744 }
745 
DumpMergedResult(std::string & result)746 void HdiLayer::DumpMergedResult(std::string &result)
747 {
748     std::unique_lock<std::mutex> lock(mutex_);
749     const uint32_t offset = mergedCount_;
750     for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
751         uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
752         result += std::to_string(mergedPresentTimeRecords_[order]) + "\n";
753     }
754 }
755 
ClearDump()756 void HdiLayer::ClearDump()
757 {
758     std::vector<std::string> windowName = {};
759     FPSInfo defaultFPSInfo = {0, windowName};
760 
761     std::unique_lock<std::mutex> lock(mutex_);
762     presentTimeRecords_.fill(defaultFPSInfo);
763     mergedPresentTimeRecords_.fill(0);
764 }
765 
SetPerFrameParameters()766 int32_t HdiLayer::SetPerFrameParameters()
767 {
768     const auto& supportedKeys = device_->GetSupportedLayerPerFrameParameterKey();
769     int32_t ret = GRAPHIC_DISPLAY_SUCCESS;
770     for (const auto& key : supportedKeys) {
771         if (key == GENERIC_METADATA_KEY_BRIGHTNESS_NIT) {
772             ret = SetPerFrameParameterDisplayNit();
773             CheckRet(ret, "SetPerFrameParameterDisplayNit");
774         } else if (key == GENERIC_METADATA_KEY_SDR_RATIO) {
775             ret = SetPerFrameParameterBrightnessRatio();
776             CheckRet(ret, "SetPerFrameParameterBrightnessRatio");
777         } else if (key == GENERIC_METADATA_KEY_SOURCE_CROP_TUNING) {
778             ret = SetPerFrameLayerSourceTuning();
779             CheckRet(ret, "SetLayerSourceTuning");
780         }
781     }
782     return ret;
783 }
784 
SetPerFrameParameterDisplayNit()785 int32_t HdiLayer::SetPerFrameParameterDisplayNit()
786 {
787     if (doLayerInfoCompare_) {
788         if (layerInfo_->GetDisplayNit() == prevLayerInfo_->GetDisplayNit()) {
789             return GRAPHIC_DISPLAY_SUCCESS;
790         }
791     }
792 
793     std::vector<int8_t> valueBlob(sizeof(int32_t));
794     *reinterpret_cast<int32_t*>(valueBlob.data()) = layerInfo_->GetDisplayNit();
795     return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_BRIGHTNESS_NIT, valueBlob);
796 }
797 
SetPerFrameParameterBrightnessRatio()798 int32_t HdiLayer::SetPerFrameParameterBrightnessRatio()
799 {
800     if (doLayerInfoCompare_) {
801         if (layerInfo_->GetBrightnessRatio() == prevLayerInfo_->GetBrightnessRatio()) {
802             return GRAPHIC_DISPLAY_SUCCESS;
803         }
804     }
805 
806     std::vector<int8_t> valueBlob(sizeof(float));
807     *reinterpret_cast<float*>(valueBlob.data()) = layerInfo_->GetBrightnessRatio();
808     return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_SDR_RATIO, valueBlob);
809 }
810 
SetPerFrameLayerSourceTuning()811 int32_t HdiLayer::SetPerFrameLayerSourceTuning()
812 {
813     if (doLayerInfoCompare_) {
814         if (layerInfo_->GetLayerSourceTuning() == prevLayerInfo_->GetLayerSourceTuning()) {
815             return GRAPHIC_DISPLAY_SUCCESS;
816         }
817     }
818 
819     std::vector<int8_t> valueBlob(sizeof(int32_t));
820     *reinterpret_cast<int32_t*>(valueBlob.data()) = layerInfo_->GetLayerSourceTuning();
821     return device_->SetLayerPerFrameParameter(screenId_, layerId_, GENERIC_METADATA_KEY_SOURCE_CROP_TUNING, valueBlob);
822 }
823 
ClearBufferCache()824 void HdiLayer::ClearBufferCache()
825 {
826     if (bufferCache_.empty()) {
827         return;
828     }
829     int32_t ret = device_->ClearLayerBuffer(screenId_, layerId_);
830     CheckRet(ret, "ClearLayerBuffer");
831     bufferCache_.clear();
832 }
833 } // namespace Rosen
834 } // namespace OHOS
835