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