1 /*
2  * Copyright (c) 2023-2023 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 #ifndef ROSEN_RENDER_SERVICE_BASE_ANIMATION_RS_FRAME_RATE_RANGE_H
17 #define ROSEN_RENDER_SERVICE_BASE_ANIMATION_RS_FRAME_RATE_RANGE_H
18 #include <string>
19 
20 #define RANGE_MAX_REFRESHRATE 144
21 
22 namespace OHOS {
23 namespace Rosen {
24 constexpr int32_t RS_ANIMATION_FRAME_RATE_TYPE = 1;
25 constexpr int32_t UI_ANIMATION_FRAME_RATE_TYPE = 2;
26 constexpr int32_t DISPLAY_SYNC_FRAME_RATE_TYPE = 3;
27 constexpr int32_t ACE_COMPONENT_FRAME_RATE_TYPE = 4;
28 constexpr int32_t DISPLAY_SOLOIST_FRAME_RATE_TYPE = 5;
29 
30 enum ComponentScene : int32_t {
31     UNKNOWN_SCENE = 0,
32     SWIPER_FLING = 1,
33 };
34 
35 class FrameRateRange {
36 public:
FrameRateRange()37     FrameRateRange() : min_(0), max_(0), preferred_(0), type_(0), isEnergyAssurance_(false),
38         componentScene_(ComponentScene::UNKNOWN_SCENE) {}
39 
FrameRateRange(int min,int max,int preferred)40     FrameRateRange(int min, int max, int preferred) : min_(min), max_(max), preferred_(preferred) {}
41 
FrameRateRange(int min,int max,int preferred,int type)42     FrameRateRange(int min, int max, int preferred, int type) : min_(min), max_(max),
43         preferred_(preferred), type_(type) {}
44 
FrameRateRange(int min,int max,int preferred,int type,ComponentScene componentScene)45     FrameRateRange(int min, int max, int preferred, int type, ComponentScene componentScene) : min_(min), max_(max),
46         preferred_(preferred), type_(type), componentScene_(componentScene) {}
47 
IsZero()48     bool IsZero() const
49     {
50         return this->preferred_ == 0;
51     }
52 
IsValid()53     bool IsValid() const
54     {
55         return !this->IsZero() && this->min_ <= this->preferred_ && this->preferred_ <= this->max_ &&
56             this->min_ >= 0 && this->max_ <= RANGE_MAX_REFRESHRATE;
57     }
58 
IsDynamic()59     bool IsDynamic() const
60     {
61         return IsValid() && this->min_ != this->max_;
62     }
63 
Reset()64     void Reset()
65     {
66         this->min_ = 0;
67         this->max_ = 0;
68         this->preferred_ = 0;
69         this->type_ = 0;
70         this->isEnergyAssurance_ = false;
71         this->componentScene_ = ComponentScene::UNKNOWN_SCENE;
72     }
73 
Set(int min,int max,int preferred)74     void Set(int min, int max, int preferred)
75     {
76         this->min_ = min;
77         this->max_ = max;
78         this->preferred_ = preferred;
79     }
80 
Set(int min,int max,int preferred,int type)81     void Set(int min, int max, int preferred, int type)
82     {
83         this->min_ = min;
84         this->max_ = max;
85         this->preferred_ = preferred;
86         this->type_ = type;
87     }
88 
Merge(const FrameRateRange & other)89     bool Merge(const FrameRateRange& other)
90     {
91         if (this->preferred_ < other.preferred_) {
92             this->Set(other.min_, other.max_, other.preferred_, other.type_);
93             this->isEnergyAssurance_ = other.isEnergyAssurance_;
94             this->componentScene_ = other.componentScene_;
95             return true;
96         }
97         return false;
98     }
99 
GetExtInfo()100     std::string GetExtInfo() const
101     {
102         auto componentName = GetComponentName();
103         if (isEnergyAssurance_ && componentName != "UNKNOWN_SCENE") {
104             return std::string("COMPONENT_") + componentName + "_ASSURANCE";
105         }
106         std::string extInfo = "";
107         switch (type_) {
108             case RS_ANIMATION_FRAME_RATE_TYPE:
109                 extInfo = "RS_ANIMATION";
110                 break;
111             case UI_ANIMATION_FRAME_RATE_TYPE:
112                 extInfo = "UI_ANIMATION";
113                 break;
114             case DISPLAY_SYNC_FRAME_RATE_TYPE:
115                 extInfo = "DISPLAY_SYNC";
116                 break;
117             case ACE_COMPONENT_FRAME_RATE_TYPE:
118                 extInfo = "ACE_COMPONENT";
119                 break;
120             case DISPLAY_SOLOIST_FRAME_RATE_TYPE:
121                 extInfo = "DISPLAY_SOLOIST";
122                 break;
123             default:
124                 return "";
125         }
126         return extInfo + (isEnergyAssurance_ ? "_ENERGY_ASSURANCE" : "");
127     }
128 
GetComponentName()129     std::string GetComponentName() const
130     {
131         switch (componentScene_) {
132             case ComponentScene::SWIPER_FLING:
133                 return "SWIPER_FLING";
134             default:
135                 return "UNKNOWN_SCENE";
136         }
137     }
138 
139     bool operator==(const FrameRateRange& other) const
140     {
141         return this->min_ == other.min_ && this->max_ == other.max_ &&
142             this->preferred_ == other.preferred_ && this->type_ == other.type_;
143     }
144 
145     bool operator!=(const FrameRateRange& other) const
146     {
147         return this->min_ != other.min_ || this->max_ != other.max_ ||
148             this->preferred_ != other.preferred_ || this->type_ != other.type_;
149     }
150 
151     int min_ = 0;
152     int max_ = 0;
153     int preferred_ = 0;
154     int type_ = 0;
155     bool isEnergyAssurance_ = false;
156     ComponentScene componentScene_ = ComponentScene::UNKNOWN_SCENE;
157 };
158 } // namespace Rosen
159 } // namespace OHOS
160 #endif // ROSEN_RENDER_SERVICE_BASE_ANIMATION_RS_FRAME_RATE_RANGE_H