1 /*
2  * Copyright (c) 2020-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 #ifndef GRAPHIC_LITE_RENDER_MANAGER_H
17 #define GRAPHIC_LITE_RENDER_MANAGER_H
18 
19 #include "common/task_manager.h"
20 #include "components/root_view.h"
21 #include "components/ui_view.h"
22 #include "gfx_utils/geometry2d.h"
23 #include "gfx_utils/list.h"
24 #include "gfx_utils/sys_info.h"
25 
26 #if ENABLE_WINDOW
27 #include "window/window.h"
28 #endif
29 
30 namespace OHOS {
31 static constexpr float MAX_FPS = 90.f;
32 
33 static constexpr int16_t SAMPLE_NUMBER = 100;
34 
35 static constexpr uint16_t MILLISECONDS_PER_SECOND = 1000;
36 
37 class RenderManager : public Task {
38 public:
39     static RenderManager& GetInstance();
40 
41     void Init() override;
42 
43 #if ENABLE_WINDOW
GetWindowById(int32_t id)44     Window* GetWindowById(int32_t id)
45     {
46         auto node = winList_.Begin();
47         while (node != winList_.End()) {
48             if (node->data_->GetWindowId() == id) {
49                 return node->data_;
50             }
51             node = node->next_;
52         }
53         return nullptr;
54     }
55 #endif
56 
57     void Callback() override;
58 
GetFPS()59     float GetFPS() const
60     {
61         return fps_;
62     }
63 
RegisterFPSChangedListener(SysInfo::OnFPSChangedListener * onFPSChangedListener)64     void RegisterFPSChangedListener(SysInfo::OnFPSChangedListener* onFPSChangedListener)
65     {
66         onFPSChangedListener_ = onFPSChangedListener;
67         needResetFPS_ = true;
68     }
69 
70 #if ENABLE_WINDOW
71     void AddToDisplay(Window* window);
72 
73     void RemoveFromDisplay(Window* window);
74 #endif
75     static void RenderRect(const Rect& rect, RootView* rootView);
76     void RefreshScreen();
77 
78 private:
79     RenderManager();
80 
81     ~RenderManager();
82 
83 #if ENABLE_FPS_SUPPORT
84     void UpdateFPS();
85 
86     void UpdateFPSByFixedTimeMethod();
87 
88     void UpdateFPSByAverageSamplingMethod();
89 
90     void UpdateFPSByPreciseSamplingMethod();
91 
OnFPSChanged(float newFPS)92     void OnFPSChanged(float newFPS)
93     {
94         if (onFPSChangedListener_) {
95             onFPSChangedListener_->OnFPSChanged(newFPS);
96         }
97     }
98 #endif
99 
100     float fps_;
101 
102     bool needResetFPS_;
103 
104     SysInfo::OnFPSChangedListener* onFPSChangedListener_;
105 
106 #if ENABLE_WINDOW
107     List<Window*> winList_;
108 #endif
109 };
110 } // namespace OHOS
111 #endif // GRAPHIC_LITE_RENDER_MANAGER_H
112