1 /*
2  * Copyright (c) 2024 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 "pipeline/rs_vblank_idle_corrector.h"
17 
18 #include "hgm_core.h"
19 #include "platform/common/rs_log.h"
20 #include "rs_trace.h"
21 #include "screen_manager/rs_screen_manager.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26     constexpr uint32_t IDLE_FRAME_COUNT_THRESHOLD = 2;
27 };
28 
SetScreenVBlankIdle(ScreenId id)29 void RSVBlankIdleCorrector::SetScreenVBlankIdle(ScreenId id)
30 {
31     bool isCorrectorEnabled = HgmCore::Instance().IsVBlankIdleCorrectEnabled();
32     if (isCorrectorEnabled) {
33         currIdleScreenId_ = id;
34         idleFrameCount_ = IDLE_FRAME_COUNT_THRESHOLD;
35         isVBlankIdle_ = true;
36     }
37 }
38 
ProcessScreenConstraint(uint64_t timestamp,uint64_t constraintRelativeTime)39 void RSVBlankIdleCorrector::ProcessScreenConstraint(uint64_t timestamp, uint64_t constraintRelativeTime)
40 {
41     auto screenManager = CreateOrGetScreenManager();
42     if (screenManager == nullptr) {
43         RS_LOGE("RSHardwareThread CreateOrGetScreenManager fail.");
44         return;
45     }
46 
47     auto defaultScreenId = screenManager->GetDefaultScreenId();
48     bool isCorrectorEnabled = HgmCore::Instance().IsVBlankIdleCorrectEnabled();
49     if (!isCorrectorEnabled) {
50         idleFrameCount_ = 0;
51         isVBlankIdle_ = false;
52         screenManager->SetScreenConstraint(defaultScreenId, 0, ScreenConstraintType::CONSTRAINT_NONE);
53         return;
54     }
55 
56     if (isVBlankIdle_) {
57         uint64_t pipelineOffset = static_cast<uint64_t>(HgmCore::Instance().GetPipelineOffset());
58         if (idleFrameCount_ > 0) {
59             uint64_t absoluteTime = timestamp + pipelineOffset;
60             screenManager->SetScreenConstraint(currIdleScreenId_,
61                 absoluteTime, ScreenConstraintType::CONSTRAINT_ABSOLUTE);
62         } else {
63             screenManager->SetScreenConstraint(currIdleScreenId_, 0, ScreenConstraintType::CONSTRAINT_NONE);
64         }
65         idleFrameCount_--;
66         if (idleFrameCount_ < 0) {
67             currIdleScreenId_ = defaultScreenId;
68             idleFrameCount_ = 0;
69             isVBlankIdle_ = false;
70         }
71     } else if (constraintRelativeTime > 0) {
72         screenManager->SetScreenConstraint(defaultScreenId,
73             constraintRelativeTime, ScreenConstraintType::CONSTRAINT_RELATIVE);
74     } else {
75         screenManager->SetScreenConstraint(defaultScreenId, 0, ScreenConstraintType::CONSTRAINT_NONE);
76     }
77 }
78 }
79 }
80