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