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