• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "graphic_engine.h"
16 #include "common/graphic_startup.h"
17 #include "common/image_decode_ability.h"
18 #include "common/task_manager.h"
19 #include "draw/draw_utils.h"
20 #include "font/ui_font_cache_manager.h"
21 #include "font/ui_font_header.h"
22 #include "log/log.h"
23 #include "updater_ui_const.h"
24 #include "ui_rotation.h"
25 #include "utils.h"
26 
27 namespace Updater {
GetInstance()28 GraphicEngine &GraphicEngine::GetInstance()
29 {
30     static GraphicEngine instance;
31     static bool isRegister = false;
32     if (!isRegister) {
33         OHOS::SoftEngine::InitGfxEngine(&instance);
34         isRegister = true;
35     }
36 
37     return instance;
38 }
39 
PostInitSurfDev(std::unique_ptr<SurfaceDev> & surfDev,GrSurface & surface)40 __attribute__((weak)) void PostInitSurfDev(std::unique_ptr<SurfaceDev> &surfDev, GrSurface &surface)
41 {
42     LOG(INFO) << "not inited the post InitSurfDev process";
43     return;
44 }
45 
Init(uint32_t bkgColor,uint8_t mode,const char * fontPath)46 void GraphicEngine::Init(uint32_t bkgColor, uint8_t mode, const char *fontPath)
47 {
48     bkgColor_ = bkgColor;
49     colorMode_ = mode;
50     [[maybe_unused]] static bool initOnce = [this, fontPath] () {
51         sfDev_ = std::make_unique<SurfaceDev>();
52         if (!sfDev_->Init()) {
53             LOG(INFO) << "GraphicEngine Init failed!";
54             return false;
55         }
56         GrSurface surface {};
57         sfDev_->GetScreenSize(width_, height_, surface);
58         PostInitSurfDev(sfDev_, surface);
59         buffInfo_ = nullptr;
60         virAddr_ = nullptr;
61         InitFontEngine(fontPath);
62         InitImageDecodeAbility();
63         InitFlushThread();
64         LOG(INFO) << "GraphicEngine Init width: " << width_ << ", height: " << height_ << ", bkgColor: " << bkgColor_;
65         return true;
66     } ();
67 }
68 
InitFontEngine(const char * fontPath) const69 void GraphicEngine::InitFontEngine(const char *fontPath) const
70 {
71     constexpr uint32_t uiFontMemAlignment = 4;
72     constexpr uint32_t fontPsramSize = OHOS::MIN_FONT_PSRAM_LENGTH * 2; // 2 : alloc more ram to optimize perfomance
73     constexpr uint32_t fontCacheSize = 0xC8000; // fontCacheSize should match with psram length
74     static uint32_t fontMemBaseAddr[fontPsramSize / uiFontMemAlignment];
75     static uint8_t icuMemBaseAddr[OHOS::SHAPING_WORD_DICT_LENGTH];
76     OHOS::UIFontCacheManager::GetInstance()->SetBitmapCacheSize(fontCacheSize);
77     OHOS::GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(fontMemBaseAddr), fontPsramSize,
78         fontPath, DEFAULT_FONT_FILENAME);
79     OHOS::GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(icuMemBaseAddr),
80         OHOS::SHAPING_WORD_DICT_LENGTH, fontPath, DEFAULT_LINE_BREAK_RULE_FILENAME);
81     LOG(INFO) << "fontPath = " << fontPath << ", InitFontEngine DEFAULT_FONT_FILENAME = " << DEFAULT_FONT_FILENAME <<
82         ", InitLineBreakEngine DEFAULT_LINE_BREAK_RULE_FILENAME = " << DEFAULT_LINE_BREAK_RULE_FILENAME;
83 }
84 
InitImageDecodeAbility() const85 void GraphicEngine::InitImageDecodeAbility() const
86 {
87     uint32_t imageType = OHOS::IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG;
88     OHOS::ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType);
89 }
90 
InitFlushThread()91 void GraphicEngine::InitFlushThread()
92 {
93     flushStop_ = false;
94     flushLoop_ = std::thread([this] {
95         this->FlushThreadLoop();
96     });
97     flushLoop_.detach();
98     LOG(INFO) << "init flush thread";
99 }
100 
InitFlushBatteryStatusExt(void)101 __attribute__((weak)) void InitFlushBatteryStatusExt(void)
102 {
103 }
104 
FlushThreadLoop() const105 void GraphicEngine::FlushThreadLoop() const
106 {
107     while (!flushStop_) {
108         OHOS::TaskManager::GetInstance()->TaskHandler();
109         InitFlushBatteryStatusExt();
110         Utils::UsSleep(sleepTime_);
111     }
112     // clear screen after stop
113     UiRotation::GetInstance().SetDegree(UI_ROTATION_DEGREE::UI_ROTATION_0);
114     uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_);
115     uint32_t picSize = 0;
116     if (__builtin_mul_overflow(width_ * height_, pixelBytes, &picSize)) {
117         return;
118     }
119     (void)memset_s(buffInfo_->virAddr, picSize, 0, picSize);
120     sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr));
121 }
122 
StopEngine(void)123 void GraphicEngine::StopEngine(void)
124 {
125     flushStop_ = true;
126     Utils::UsSleep(THREAD_USLEEP_TIME * 10); // 10: wait for stop 100ms
127 }
128 
SetSleepTime(uint32_t sleepTime)129 void GraphicEngine::SetSleepTime(uint32_t sleepTime)
130 {
131     sleepTime_ = sleepTime;
132 }
133 
HandleScreenPowerDown(bool blank)134 void GraphicEngine::HandleScreenPowerDown(bool blank)
135 {
136     if (sfDev_ == nullptr) {
137         LOG(ERROR) << "sfDev_ is nullptr";
138         return;
139     }
140     sfDev_->Blank(blank);
141 }
142 
GetFBBufferInfo()143 OHOS::BufferInfo *GraphicEngine::GetFBBufferInfo()
144 {
145     if (buffInfo_ != nullptr) {
146         return buffInfo_.get();
147     }
148 
149     uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_);
150     if (pixelBytes == 0) {
151         LOG(ERROR) << "GraphicEngine get pixelBytes fail";
152         return nullptr;
153     }
154 
155     if ((width_ == 0) || (height_ == 0)) {
156         LOG(ERROR) << "input error, width: " << width_ << ", height: " << height_;
157         return nullptr;
158     }
159     UiRotation::GetInstance().InitRotation(width_, height_, pixelBytes);
160     width_ = UiRotation::GetInstance().GetWidth();
161     height_ = UiRotation::GetInstance().GetHeight();
162     virAddr_ = std::make_unique<uint8_t[]>(width_ * height_ * pixelBytes);
163     buffInfo_ = std::make_unique<OHOS::BufferInfo>();
164     buffInfo_->rect = { 0, 0, static_cast<int16_t>(width_ - 1), static_cast<int16_t>(height_ - 1) };
165     buffInfo_->mode = static_cast<OHOS::ColorMode>(colorMode_);
166     buffInfo_->color = bkgColor_;
167     buffInfo_->virAddr = virAddr_.get();
168     buffInfo_->phyAddr = buffInfo_->virAddr;
169     buffInfo_->stride = static_cast<uint32_t>(width_ * pixelBytes);
170     buffInfo_->width = width_;
171     buffInfo_->height = height_;
172 
173     return buffInfo_.get();
174 }
175 
Flush(const OHOS::Rect & flushRect)176 void GraphicEngine::Flush(const OHOS::Rect& flushRect)
177 {
178     if ((sfDev_ == nullptr) || (buffInfo_ == nullptr)) {
179         LOG(ERROR) << "null error";
180         return;
181     }
182     std::lock_guard<std::mutex> lock {mtx_};
183     UiRotation::GetInstance().SetFlushRange(flushRect);
184     sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr));
185 }
186 
GetScreenWidth()187 uint16_t GraphicEngine::GetScreenWidth()
188 {
189     return width_;
190 }
191 
GetScreenHeight()192 uint16_t GraphicEngine::GetScreenHeight()
193 {
194     return height_;
195 }
196 } // namespace Updater
197