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