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_header.h"
21 #include "log/log.h"
22 #include "updater_ui_const.h"
23 #include "ui_rotation.h"
24 #include "utils.h"
25 
26 namespace Updater {
GetInstance()27 GraphicEngine &GraphicEngine::GetInstance()
28 {
29     static GraphicEngine instance;
30     static bool isRegister = false;
31     if (!isRegister) {
32         OHOS::SoftEngine::InitGfxEngine(&instance);
33         isRegister = true;
34     }
35 
36     return instance;
37 }
38 
PostInitSurfDev(std::unique_ptr<SurfaceDev> & surfDev,GrSurface & surface)39 __attribute__((weak)) void PostInitSurfDev(std::unique_ptr<SurfaceDev> &surfDev, GrSurface &surface)
40 {
41     LOG(INFO) << "not inited the post InitSurfDev process";
42     return;
43 }
44 
Init(uint32_t bkgColor,uint8_t mode,const char * fontPath)45 void GraphicEngine::Init(uint32_t bkgColor, uint8_t mode, const char *fontPath)
46 {
47     bkgColor_ = bkgColor;
48     colorMode_ = mode;
49     [[maybe_unused]] static bool initOnce = [this, fontPath] () {
50         sfDev_ = std::make_unique<SurfaceDev>();
51         if (!sfDev_->Init()) {
52             LOG(INFO) << "GraphicEngine Init failed!";
53             return false;
54         }
55         GrSurface surface {};
56         sfDev_->GetScreenSize(width_, height_, surface);
57         PostInitSurfDev(sfDev_, surface);
58         buffInfo_ = nullptr;
59         virAddr_ = nullptr;
60         InitFontEngine(fontPath);
61         InitImageDecodeAbility();
62         InitFlushThread();
63         LOG(INFO) << "GraphicEngine Init width: " << width_ << ", height: " << height_ << ", bkgColor: " << bkgColor_;
64         return true;
65     } ();
66 }
67 
InitFontEngine(const char * fontPath) const68 void GraphicEngine::InitFontEngine(const char *fontPath) const
69 {
70     constexpr uint32_t uiFontMemAlignment = 4;
71     static uint32_t fontMemBaseAddr[OHOS::MIN_FONT_PSRAM_LENGTH / uiFontMemAlignment];
72     static uint8_t icuMemBaseAddr[OHOS::SHAPING_WORD_DICT_LENGTH];
73     OHOS::GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(fontMemBaseAddr), OHOS::MIN_FONT_PSRAM_LENGTH,
74         fontPath, DEFAULT_FONT_FILENAME);
75     OHOS::GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(icuMemBaseAddr),
76         OHOS::SHAPING_WORD_DICT_LENGTH, fontPath, DEFAULT_LINE_BREAK_RULE_FILENAME);
77     LOG(INFO) << "fontPath = " << fontPath << ", InitFontEngine DEFAULT_FONT_FILENAME = " << DEFAULT_FONT_FILENAME <<
78         ", InitLineBreakEngine DEFAULT_LINE_BREAK_RULE_FILENAME = " << DEFAULT_LINE_BREAK_RULE_FILENAME;
79 }
80 
InitImageDecodeAbility() const81 void GraphicEngine::InitImageDecodeAbility() const
82 {
83     uint32_t imageType = OHOS::IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG;
84     OHOS::ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType);
85 }
86 
InitFlushThread()87 void GraphicEngine::InitFlushThread()
88 {
89     flushStop_ = false;
90     flushLoop_ = std::thread([this] {
91         this->FlushThreadLoop();
92         });
93     flushLoop_.detach();
94     LOG(INFO) << "init flush thread";
95 }
96 
InitFlushBatteryStatusExt(void)97 __attribute__((weak)) void InitFlushBatteryStatusExt(void)
98 {
99 }
100 
FlushThreadLoop() const101 void GraphicEngine::FlushThreadLoop() const
102 {
103     while (!flushStop_) {
104         OHOS::TaskManager::GetInstance()->TaskHandler();
105         InitFlushBatteryStatusExt();
106         Utils::UsSleep(THREAD_USLEEP_TIME);
107     }
108 }
109 
GetFBBufferInfo()110 OHOS::BufferInfo *GraphicEngine::GetFBBufferInfo()
111 {
112     if (buffInfo_ != nullptr) {
113         return buffInfo_.get();
114     }
115 
116     uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_);
117     if (pixelBytes == 0) {
118         LOG(ERROR) << "GraphicEngine get pixelBytes fail";
119         return nullptr;
120     }
121 
122     if ((width_ == 0) || (height_ == 0)) {
123         LOG(ERROR) << "input error, width: " << width_ << ", height: " << height_;
124         return nullptr;
125     }
126     UiRotation::GetInstance().InitRotation(width_, height_, pixelBytes);
127     width_ = UiRotation::GetInstance().GetWidth();
128     height_ = UiRotation::GetInstance().GetHeight();
129     virAddr_ = std::make_unique<uint8_t[]>(width_ * height_ * pixelBytes);
130     buffInfo_ = std::make_unique<OHOS::BufferInfo>();
131     buffInfo_->rect = { 0, 0, static_cast<int16_t>(width_ - 1), static_cast<int16_t>(height_ - 1) };
132     buffInfo_->mode = static_cast<OHOS::ColorMode>(colorMode_);
133     buffInfo_->color = bkgColor_;
134     buffInfo_->virAddr = virAddr_.get();
135     buffInfo_->phyAddr = buffInfo_->virAddr;
136     buffInfo_->stride = static_cast<uint32_t>(width_ * pixelBytes);
137     buffInfo_->width = width_;
138     buffInfo_->height = height_;
139 
140     return buffInfo_.get();
141 }
142 
Flush(const OHOS::Rect & flushRect)143 void GraphicEngine::Flush(const OHOS::Rect& flushRect)
144 {
145     if ((sfDev_ == nullptr) || (buffInfo_ == nullptr)) {
146         LOG(ERROR) << "null error";
147         return;
148     }
149     std::lock_guard<std::mutex> lock {mtx_};
150     UiRotation::GetInstance().SetFlushRange(flushRect);
151     sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr));
152 }
153 
GetScreenWidth()154 uint16_t GraphicEngine::GetScreenWidth()
155 {
156     return width_;
157 }
158 
GetScreenHeight()159 uint16_t GraphicEngine::GetScreenHeight()
160 {
161     return height_;
162 }
163 } // namespace Updater
164