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 namespace OHOS {
19 namespace Rosen {
20
21 template<typename T>
Compare(const T & lhs,const T & rhs)22 bool Compare(const T& lhs, const T& rhs)
23 {
24 return lhs == rhs;
25 }
26
27 template<>
Compare(const GraphicIRect & rect1,const GraphicIRect & rect2)28 bool Compare(const GraphicIRect& rect1, const GraphicIRect& rect2)
29 {
30 return rect1.x == rect2.x && rect1.y == rect2.y && rect1.w == rect2.w && rect1.h == rect2.h;
31 }
32
33 template<typename T>
IsNeedSetInfoToDevice(const std::vector<T> & lhs,const std::vector<T> & rhs)34 bool IsNeedSetInfoToDevice(const std::vector<T>& lhs, const std::vector<T>& rhs)
35 {
36 if (lhs.size() != rhs.size()) {
37 return true;
38 }
39
40 for (decltype(lhs.size()) i = 0; i < lhs.size(); i++) {
41 if (!Compare(lhs[i], rhs[i])) {
42 return true;
43 }
44 }
45
46 return false;
47 }
48
49 /* rs create layer and set layer info begin */
CreateHdiLayer(uint32_t screenId)50 std::shared_ptr<HdiLayer> HdiLayer::CreateHdiLayer(uint32_t screenId)
51 {
52 return std::make_shared<HdiLayer>(screenId);
53 }
54
HdiLayer(uint32_t screenId)55 HdiLayer::HdiLayer(uint32_t screenId) : screenId_(screenId)
56 {
57 prevSbuffer_ = new LayerBufferInfo();
58 currSbuffer_ = new LayerBufferInfo();
59 }
60
~HdiLayer()61 HdiLayer::~HdiLayer()
62 {
63 CloseLayer();
64 }
65
Init(const LayerInfoPtr & layerInfo)66 bool HdiLayer::Init(const LayerInfoPtr &layerInfo)
67 {
68 if (layerInfo == nullptr) {
69 return false;
70 }
71
72 if (CreateLayer(layerInfo) != GRAPHIC_DISPLAY_SUCCESS) {
73 return false;
74 }
75
76 return true;
77 }
78
InitDevice()79 int32_t HdiLayer::InitDevice()
80 {
81 if (device_ != nullptr) {
82 return GRAPHIC_DISPLAY_SUCCESS;
83 }
84
85 device_ = HdiDevice::GetInstance();
86 if (device_ == nullptr) {
87 HLOGE("device_ init failed.");
88 return GRAPHIC_DISPLAY_NULL_PTR;
89 }
90 return GRAPHIC_DISPLAY_SUCCESS;
91 }
92
SetHdiDeviceMock(HdiDevice * hdiDeviceMock)93 int32_t HdiLayer::SetHdiDeviceMock(HdiDevice* hdiDeviceMock)
94 {
95 if (hdiDeviceMock == nullptr) {
96 HLOGE("Input HdiDevice is nullptr");
97 return GRAPHIC_DISPLAY_NULL_PTR;
98 }
99
100 if (device_ != nullptr) {
101 HLOGD("device_ has been initialized");
102 return GRAPHIC_DISPLAY_SUCCESS;
103 }
104
105 device_ = hdiDeviceMock;
106 return GRAPHIC_DISPLAY_SUCCESS;
107 }
108
CreateLayer(const LayerInfoPtr & layerInfo)109 int32_t HdiLayer::CreateLayer(const LayerInfoPtr &layerInfo)
110 {
111 GraphicLayerInfo hdiLayerInfo = {
112 .width = layerInfo->GetLayerSize().w,
113 .height = layerInfo->GetLayerSize().h,
114 .type = GRAPHIC_LAYER_TYPE_GRAPHIC,
115 .pixFormat = GRAPHIC_PIXEL_FMT_RGBA_8888,
116 };
117
118 int32_t retCode = InitDevice();
119 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
120 return GRAPHIC_DISPLAY_NULL_PTR;
121 }
122
123 sptr<IConsumerSurface> surface = layerInfo->GetSurface();
124 if (surface == nullptr) {
125 HLOGE("Create layer failed because the consumer surface is nullptr.");
126 return GRAPHIC_DISPLAY_NULL_PTR;
127 }
128 bufferCacheCountMax_ = surface->GetQueueSize();
129 uint32_t layerId = INT_MAX;
130 int32_t ret = device_->CreateLayer(screenId_, hdiLayerInfo, bufferCacheCountMax_, layerId);
131 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
132 HLOGE("Create hwc layer failed, ret is %{public}d", ret);
133 return ret;
134 }
135
136 bufferCache_.resize(bufferCacheCountMax_);
137 layerId_ = layerId;
138
139 HLOGD("Create hwc layer succeed, layerId is %{public}u", layerId_);
140
141 CheckRet(device_->GetSupportedPresentTimestampType(screenId_, layerId_, supportedPresentTimestamptype_),
142 "GetSupportedPresentTimestamp");
143 return ret;
144 }
145
CloseLayer()146 void HdiLayer::CloseLayer()
147 {
148 if (layerId_ == INT_MAX) {
149 HLOGI("this layer has not been created");
150 return;
151 }
152
153 int32_t retCode = InitDevice();
154 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
155 return;
156 }
157
158 retCode = device_->CloseLayer(screenId_, layerId_);
159 if (retCode != GRAPHIC_DISPLAY_SUCCESS) {
160 HLOGE("Close hwc layer[%{public}u] failed, ret is %{public}d", layerId_, retCode);
161 }
162
163 HLOGD("Close hwc layer succeed, layerId is %{public}u", layerId_);
164 }
165
SetLayerAlpha()166 int32_t HdiLayer::SetLayerAlpha()
167 {
168 if (doLayerInfoCompare_) {
169 const GraphicLayerAlpha& layerAlpha1 = layerInfo_->GetAlpha();
170 const GraphicLayerAlpha& layerAlpha2 = prevLayerInfo_->GetAlpha();
171 bool isSame = layerAlpha1.enGlobalAlpha == layerAlpha2.enGlobalAlpha &&
172 layerAlpha1.enPixelAlpha == layerAlpha2.enPixelAlpha &&
173 layerAlpha1.alpha0 == layerAlpha2.alpha0 && layerAlpha1.alpha1 == layerAlpha2.alpha1 &&
174 layerAlpha1.gAlpha == layerAlpha2.gAlpha;
175 if (isSame) {
176 return GRAPHIC_DISPLAY_SUCCESS;
177 }
178 }
179
180 int32_t ret = device_->SetLayerAlpha(screenId_, layerId_, layerInfo_->GetAlpha());
181 return ret;
182 }
183
SetLayerSize()184 int32_t HdiLayer::SetLayerSize()
185 {
186 if (doLayerInfoCompare_ && Compare(layerInfo_->GetLayerSize(), prevLayerInfo_->GetLayerSize())) {
187 return GRAPHIC_DISPLAY_SUCCESS;
188 }
189
190 int32_t ret = device_->SetLayerSize(screenId_, layerId_, layerInfo_->GetLayerSize());
191 return ret;
192 }
193
SetTransformMode()194 int32_t HdiLayer::SetTransformMode()
195 {
196 if (layerInfo_->GetTransformType() == GraphicTransformType::GRAPHIC_ROTATE_BUTT || (doLayerInfoCompare_ &&
197 layerInfo_->GetTransformType() == prevLayerInfo_->GetTransformType())) {
198 return GRAPHIC_DISPLAY_SUCCESS;
199 }
200
201 GraphicTransformType transFormType = layerInfo_->GetTransformType();
202 int32_t ret = device_->SetTransformMode(screenId_, layerId_, transFormType);
203 return ret;
204 }
205
SetLayerVisibleRegion()206 int32_t HdiLayer::SetLayerVisibleRegion()
207 {
208 const std::vector<GraphicIRect>& curVisibles = layerInfo_->GetVisibleRegions();
209 bool isNeedSetInfoToDevice = true;
210 if (doLayerInfoCompare_) {
211 const std::vector<GraphicIRect>& prevVisibles = prevLayerInfo_->GetVisibleRegions();
212 if (!IsNeedSetInfoToDevice(curVisibles, prevVisibles)) {
213 isNeedSetInfoToDevice = false;
214 }
215 }
216
217 if (isNeedSetInfoToDevice) {
218 return device_->SetLayerVisibleRegion(screenId_, layerId_, curVisibles);
219 }
220
221 return GRAPHIC_DISPLAY_SUCCESS;
222 }
223
SetLayerDirtyRegion()224 int32_t HdiLayer::SetLayerDirtyRegion()
225 {
226 const std::vector<GraphicIRect>& curDirtyRegions = layerInfo_->GetDirtyRegions();
227 bool isNeedSetInfoToDevice = true;
228 if (doLayerInfoCompare_) {
229 const std::vector<GraphicIRect>& prevDirtyRegions = prevLayerInfo_->GetDirtyRegions();
230 if (!IsNeedSetInfoToDevice(curDirtyRegions, prevDirtyRegions)) {
231 isNeedSetInfoToDevice = false;
232 }
233 }
234
235 if (isNeedSetInfoToDevice) {
236 return device_->SetLayerDirtyRegion(screenId_, layerId_, curDirtyRegions);
237 }
238
239 return GRAPHIC_DISPLAY_SUCCESS;
240 }
241
CheckAndUpdateLayerBufferCahce(sptr<SurfaceBuffer> buffer,uint32_t & index,std::vector<uint32_t> & deletingList)242 bool HdiLayer::CheckAndUpdateLayerBufferCahce(sptr<SurfaceBuffer> buffer, uint32_t& index,
243 std::vector<uint32_t>& deletingList)
244 {
245 for (uint32_t i = 0; i < bufferCacheCountMax_; i++) {
246 if (bufferCache_[i] == buffer) {
247 index = i;
248 return true;
249 }
250 }
251
252 if (bufferCacheIndex_ >= bufferCacheCountMax_) {
253 for (uint32_t i = 0; i < bufferCacheCountMax_; i++) {
254 bufferCache_[i] = nullptr;
255 deletingList.push_back(i);
256 }
257 bufferCacheIndex_ = 0;
258 }
259 bufferCache_[bufferCacheIndex_] = buffer;
260 index = bufferCacheIndex_;
261 bufferCacheIndex_++;
262 return false;
263 }
264
SetLayerBuffer()265 int32_t HdiLayer::SetLayerBuffer()
266 {
267 sptr<SurfaceBuffer> currBuffer = layerInfo_->GetBuffer();
268 sptr<SyncFence> currAcquireFence = layerInfo_->GetAcquireFence();
269 if (currBuffer == nullptr) {
270 return GRAPHIC_DISPLAY_SUCCESS;
271 }
272 if (doLayerInfoCompare_) {
273 sptr<SurfaceBuffer> prevBuffer = prevLayerInfo_->GetBuffer();
274 sptr<SyncFence> prevAcquireFence = prevLayerInfo_->GetAcquireFence();
275 if (currBuffer == prevBuffer && currAcquireFence == prevAcquireFence) {
276 return GRAPHIC_DISPLAY_SUCCESS;
277 }
278 }
279
280 uint32_t index = INVALID_BUFFER_CACHE_INDEX;
281 std::vector<uint32_t> deletingList = {};
282 bool bufferCached = false;
283 if (bufferCacheCountMax_ == 0) {
284 bufferCache_.clear();
285 bufferCacheIndex_ = INVALID_BUFFER_CACHE_INDEX;
286 HLOGE("The count of this layer buffer cache is 0.");
287 } else {
288 bufferCached = CheckAndUpdateLayerBufferCahce(currBuffer, index, deletingList);
289 }
290
291 GraphicLayerBuffer layerBuffer;
292 layerBuffer.cacheIndex = index;
293 layerBuffer.acquireFence = currAcquireFence;
294 layerBuffer.deletingList = deletingList;
295 if (bufferCached && index < bufferCacheCountMax_) {
296 layerBuffer.handle = nullptr;
297 } else {
298 layerBuffer.handle = currBuffer->GetBufferHandle();
299 }
300 return device_->SetLayerBuffer(screenId_, layerId_, layerBuffer);
301 }
302
SetLayerCompositionType()303 int32_t HdiLayer::SetLayerCompositionType()
304 {
305 if (doLayerInfoCompare_ && layerInfo_->GetCompositionType() == prevLayerInfo_->GetCompositionType()) {
306 return GRAPHIC_DISPLAY_SUCCESS;
307 }
308
309 int32_t ret = device_->SetLayerCompositionType(screenId_, layerId_, layerInfo_->GetCompositionType());
310 return ret;
311 }
312
SetLayerBlendType()313 int32_t HdiLayer::SetLayerBlendType()
314 {
315 if (doLayerInfoCompare_ && layerInfo_->GetBlendType() == prevLayerInfo_->GetBlendType()) {
316 return GRAPHIC_DISPLAY_SUCCESS;
317 }
318
319 int32_t ret = device_->SetLayerBlendType(screenId_, layerId_, layerInfo_->GetBlendType());
320 return ret;
321 }
322
SetLayerCrop()323 int32_t HdiLayer::SetLayerCrop()
324 {
325 if (doLayerInfoCompare_ && Compare(layerInfo_->GetCropRect(), prevLayerInfo_->GetCropRect())) {
326 return GRAPHIC_DISPLAY_SUCCESS;
327 }
328
329 int32_t ret = device_->SetLayerCrop(screenId_, layerId_, layerInfo_->GetCropRect());
330 return ret;
331 }
332
SetLayerZorder()333 int32_t HdiLayer::SetLayerZorder()
334 {
335 if (doLayerInfoCompare_ && layerInfo_->GetZorder() == prevLayerInfo_->GetZorder()) {
336 return GRAPHIC_DISPLAY_SUCCESS;
337 }
338
339 int32_t ret = device_->SetLayerZorder(screenId_, layerId_, layerInfo_->GetZorder());
340 return ret;
341 }
342
SetLayerPreMulti()343 int32_t HdiLayer::SetLayerPreMulti()
344 {
345 if (doLayerInfoCompare_ && layerInfo_->IsPreMulti() == prevLayerInfo_->IsPreMulti()) {
346 return GRAPHIC_DISPLAY_SUCCESS;
347 }
348
349 int32_t ret = device_->SetLayerPreMulti(screenId_, layerId_, layerInfo_->IsPreMulti());
350 return ret;
351 }
352
SetLayerColorTransform()353 int32_t HdiLayer::SetLayerColorTransform()
354 {
355 const std::vector<float>& curMatrix = layerInfo_->GetColorTransform();
356 bool isNeedSetInfoToDevice = true;
357 if (doLayerInfoCompare_) {
358 const std::vector<float>& prevMatrix = prevLayerInfo_->GetColorTransform();
359 if (!IsNeedSetInfoToDevice(curMatrix, prevMatrix)) {
360 isNeedSetInfoToDevice = false;
361 }
362 }
363 if (isNeedSetInfoToDevice) {
364 // This method may not be supported, the return value is not check here
365 device_->SetLayerColorTransform(screenId_, layerId_, curMatrix);
366 }
367
368 return GRAPHIC_DISPLAY_SUCCESS;
369 }
370
SetLayerColorDataSpace()371 int32_t HdiLayer::SetLayerColorDataSpace()
372 {
373 if (doLayerInfoCompare_ && layerInfo_->GetColorDataSpace() == prevLayerInfo_->GetColorDataSpace()) {
374 return GRAPHIC_DISPLAY_SUCCESS;
375 }
376
377 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
378 device_->SetLayerColorDataSpace(screenId_, layerId_, layerInfo_->GetColorDataSpace());
379 return GRAPHIC_DISPLAY_SUCCESS;
380 }
381
IsSameLayerMetaData()382 bool HdiLayer::IsSameLayerMetaData()
383 {
384 bool isSame = false;
385 std::vector<GraphicHDRMetaData>& metaData = layerInfo_->GetMetaData();
386 std::vector<GraphicHDRMetaData>& prevMetaData = prevLayerInfo_->GetMetaData();
387 if (metaData.size() == prevMetaData.size()) {
388 isSame = true;
389 size_t metaDeataSize = metaData.size();
390 for (size_t i = 0; i < metaDeataSize; i++) {
391 if (metaData[i].key != prevMetaData[i].key || metaData[i].value != prevMetaData[i].value) {
392 isSame = false;
393 break;
394 }
395 }
396 }
397 return isSame;
398 }
399
SetLayerMetaData()400 int32_t HdiLayer::SetLayerMetaData()
401 {
402 if (doLayerInfoCompare_) {
403 bool isSame = IsSameLayerMetaData();
404 if (isSame) {
405 return GRAPHIC_DISPLAY_SUCCESS;
406 }
407 }
408
409 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
410 device_->SetLayerMetaData(screenId_, layerId_, layerInfo_->GetMetaData());
411 return GRAPHIC_DISPLAY_SUCCESS;
412 }
413
414
IsSameLayerMetaDataSet()415 bool HdiLayer::IsSameLayerMetaDataSet()
416 {
417 bool isSame = false;
418 GraphicHDRMetaDataSet &metaDataSet = layerInfo_->GetMetaDataSet();
419 GraphicHDRMetaDataSet &prevMetaDataSet = prevLayerInfo_->GetMetaDataSet();
420 if (metaDataSet.key == prevMetaDataSet.key &&
421 metaDataSet.metaData.size() == prevMetaDataSet.metaData.size()) {
422 isSame = true;
423 size_t metaDeataSetSize = metaDataSet.metaData.size();
424 for (size_t i = 0; i < metaDeataSetSize; i++) {
425 if (metaDataSet.metaData[i] != prevMetaDataSet.metaData[i]) {
426 isSame = false;
427 break;
428 }
429 }
430 }
431 return isSame;
432 }
433
SetLayerMetaDataSet()434 int32_t HdiLayer::SetLayerMetaDataSet()
435 {
436 if (doLayerInfoCompare_) {
437 bool isSame = IsSameLayerMetaDataSet();
438 if (isSame) {
439 return GRAPHIC_DISPLAY_SUCCESS;
440 }
441 }
442
443 // because hdi interface func is not implemented, delete CheckRet to avoid excessive print of log
444 device_->SetLayerMetaDataSet(screenId_, layerId_, layerInfo_->GetMetaDataSet().key,
445 layerInfo_->GetMetaDataSet().metaData);
446 return GRAPHIC_DISPLAY_SUCCESS;
447 }
448
SetLayerTunnelHandle()449 int32_t HdiLayer::SetLayerTunnelHandle()
450 {
451 if (!layerInfo_->GetTunnelHandleChange()) {
452 return GRAPHIC_DISPLAY_SUCCESS;
453 }
454 int32_t ret = GRAPHIC_DISPLAY_SUCCESS;
455 if (layerInfo_->GetTunnelHandle() == nullptr) {
456 ret = device_->SetLayerTunnelHandle(screenId_, layerId_, nullptr);
457 } else {
458 ret = device_->SetLayerTunnelHandle(screenId_, layerId_, layerInfo_->GetTunnelHandle()->GetHandle());
459 }
460 return ret;
461 }
462
SetLayerPresentTimestamp()463 int32_t HdiLayer::SetLayerPresentTimestamp()
464 {
465 if (supportedPresentTimestamptype_ == GraphicPresentTimestampType::GRAPHIC_DISPLAY_PTS_UNSUPPORTED) {
466 return GRAPHIC_DISPLAY_SUCCESS;
467 }
468 layerInfo_->SetIsSupportedPresentTimestamp(true);
469 GraphicPresentTimestamp timestamp = {GRAPHIC_DISPLAY_PTS_UNSUPPORTED, 0};
470 int32_t ret = device_->GetPresentTimestamp(screenId_, layerId_, timestamp);
471 GraphicPresentTimestamp graphicTimestamp = {
472 .type = static_cast<GraphicPresentTimestampType>(timestamp.type),
473 .time = timestamp.time,
474 };
475
476 CheckRet(ret, "GetPresentTimestamp");
477 if (ret == GRAPHIC_DISPLAY_SUCCESS) {
478 layerInfo_->SetPresentTimestamp(graphicTimestamp);
479 }
480 return ret;
481 }
482
SetLayerMaskInfo()483 int32_t HdiLayer::SetLayerMaskInfo()
484 {
485 return device_->SetLayerMaskInfo(screenId_, layerId_, static_cast<uint32_t>(layerInfo_->GetLayerMaskInfo()));
486 }
487
SetHdiLayerInfo()488 int32_t HdiLayer::SetHdiLayerInfo()
489 {
490 /*
491 Some hardware platforms may not support all layer settings.
492 If the current function is not supported, continue other layer settings.
493 */
494 int32_t ret = InitDevice();
495 if (ret != GRAPHIC_DISPLAY_SUCCESS || layerInfo_ == nullptr) {
496 return GRAPHIC_DISPLAY_FAILURE;
497 }
498
499 // All layer properities need to set to hwc when the layer is created firstly or the previous layer's composition
500 // type is COMPOSITION_DEVICE for COMPOSITION_DEVICE can not reuse COMPOSITION_CLIENT layers info.
501 doLayerInfoCompare_ = prevLayerInfo_ != nullptr &&
502 prevLayerInfo_->GetCompositionType() == GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE;
503
504 ret = SetLayerAlpha();
505 CheckRet(ret, "SetLayerAlpha");
506 ret = SetLayerSize();
507 CheckRet(ret, "SetLayerSize");
508 ret = SetTransformMode();
509 CheckRet(ret, "SetTransformMode");
510 ret = SetLayerVisibleRegion();
511 CheckRet(ret, "SetLayerVisibleRegion");
512 ret = SetLayerDirtyRegion();
513 CheckRet(ret, "SetLayerDirtyRegion");
514 ret = SetLayerBuffer();
515 CheckRet(ret, "SetLayerBuffer");
516 ret = SetLayerCompositionType();
517 CheckRet(ret, "SetLayerCompositionType");
518 ret = SetLayerBlendType();
519 CheckRet(ret, "SetLayerBlendType");
520 ret = SetLayerCrop();
521 CheckRet(ret, "SetLayerCrop");
522 ret = SetLayerZorder();
523 CheckRet(ret, "SetLayerZorder");
524 ret = SetLayerPreMulti();
525 CheckRet(ret, "SetLayerPreMulti");
526 // This method may not be supported, the return value is not check here
527 (void)SetLayerColorTransform();
528 ret = SetLayerColorDataSpace();
529 CheckRet(ret, "SetLayerColorDataSpace");
530 ret = SetLayerMetaData();
531 CheckRet(ret, "SetLayerMetaData");
532 ret = SetLayerMetaDataSet();
533 CheckRet(ret, "SetLayerMetaDataSet");
534 ret = SetLayerTunnelHandle();
535 CheckRet(ret, "SetLayerTunnelHandle");
536 ret = SetLayerPresentTimestamp();
537 CheckRet(ret, "SetLayerPresentTimestamp");
538 ret = SetLayerMaskInfo();
539 CheckRet(ret, "SetLayerMask");
540
541 return GRAPHIC_DISPLAY_SUCCESS;
542 }
543
GetLayerId() const544 uint32_t HdiLayer::GetLayerId() const
545 {
546 return layerId_;
547 }
548
GetLayerInfo()549 const LayerInfoPtr& HdiLayer::GetLayerInfo()
550 {
551 return layerInfo_;
552 }
553
SetLayerStatus(bool inUsing)554 void HdiLayer::SetLayerStatus(bool inUsing)
555 {
556 isInUsing_ = inUsing;
557 }
558
GetLayerStatus() const559 bool HdiLayer::GetLayerStatus() const
560 {
561 return isInUsing_;
562 }
563
UpdateLayerInfo(const LayerInfoPtr & layerInfo)564 void HdiLayer::UpdateLayerInfo(const LayerInfoPtr &layerInfo)
565 {
566 if (layerInfo == nullptr) {
567 return;
568 }
569
570 /* If the layer is updated, it indicates that the layer will be used
571 * in the frame. Mark it.
572 */
573
574 isInUsing_ = true;
575 layerInfo_ = layerInfo;
576
577 prevSbuffer_->sbuffer_ = currSbuffer_->sbuffer_;
578 prevSbuffer_->acquireFence_ = currSbuffer_->acquireFence_;
579
580 currSbuffer_->sbuffer_ = layerInfo_->GetBuffer();
581 currSbuffer_->acquireFence_ = layerInfo_->GetAcquireFence();
582 }
583
GetReleaseFence() const584 sptr<SyncFence> HdiLayer::GetReleaseFence() const
585 {
586 if (currSbuffer_ == nullptr) {
587 return SyncFence::INVALID_FENCE;
588 }
589 return currSbuffer_->releaseFence_;
590 }
591
RecordPresentTime(int64_t timestamp)592 void HdiLayer::RecordPresentTime(int64_t timestamp)
593 {
594 if (currSbuffer_->sbuffer_ != prevSbuffer_->sbuffer_) {
595 presentTimeRecords[count] = timestamp;
596 count = (count + 1) % FRAME_RECORDS_NUM;
597 }
598 }
599
MergeWithFramebufferFence(const sptr<SyncFence> & fbAcquireFence)600 void HdiLayer::MergeWithFramebufferFence(const sptr<SyncFence> &fbAcquireFence)
601 {
602 if (fbAcquireFence != nullptr) {
603 currSbuffer_->releaseFence_ = Merge(currSbuffer_->releaseFence_, fbAcquireFence);
604 }
605 }
606
MergeWithLayerFence(const sptr<SyncFence> & layerReleaseFence)607 void HdiLayer::MergeWithLayerFence(const sptr<SyncFence> &layerReleaseFence)
608 {
609 if (layerReleaseFence != nullptr) {
610 currSbuffer_->releaseFence_ = Merge(currSbuffer_->releaseFence_, layerReleaseFence);
611 }
612 }
613
UpdateCompositionType(GraphicCompositionType type)614 void HdiLayer::UpdateCompositionType(GraphicCompositionType type)
615 {
616 if (layerInfo_ == nullptr) {
617 return;
618 }
619
620 layerInfo_->SetCompositionType(type);
621 }
622 /* backend get layer info end */
623
Merge(const sptr<SyncFence> & fence1,const sptr<SyncFence> & fence2)624 sptr<SyncFence> HdiLayer::Merge(const sptr<SyncFence> &fence1, const sptr<SyncFence> &fence2)
625 {
626 return SyncFence::MergeFence("ReleaseFence", fence1, fence2);
627 }
628
CheckRet(int32_t ret,const char * func)629 void HdiLayer::CheckRet(int32_t ret, const char* func)
630 {
631 if (ret != GRAPHIC_DISPLAY_SUCCESS) {
632 HLOGD("call hdi %{public}s failed, ret is %{public}d", func, ret);
633 }
634 }
635
SavePrevLayerInfo()636 void HdiLayer::SavePrevLayerInfo()
637 {
638 if (prevLayerInfo_ == nullptr) {
639 prevLayerInfo_ = HdiLayerInfo::CreateHdiLayerInfo();
640 }
641 prevLayerInfo_->CopyLayerInfo(layerInfo_);
642 }
643
Dump(std::string & result)644 void HdiLayer::Dump(std::string &result)
645 {
646 const uint32_t offset = count;
647 for (uint32_t i = 0; i < FRAME_RECORDS_NUM; i++) {
648 uint32_t order = (offset + i) % FRAME_RECORDS_NUM;
649 result += std::to_string(presentTimeRecords[order]) + "\n";
650 }
651 }
652
ClearDump()653 void HdiLayer::ClearDump()
654 {
655 presentTimeRecords.fill(0);
656 }
657
658 } // namespace Rosen
659 } // namespace OHOS
660