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 27namespace Updater { 28GraphicEngine &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 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 46void 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 69void 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 85void 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 91void 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 101__attribute__((weak)) void InitFlushBatteryStatusExt(void) 102{ 103} 104 105void 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 (void)memset_s(buffInfo_->virAddr, width_ * height_ * pixelBytes, 0, width_ * height_ * pixelBytes); 116 sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr)); 117} 118 119void GraphicEngine::StopEngine(void) 120{ 121 flushStop_ = true; 122 Utils::UsSleep(THREAD_USLEEP_TIME * 10); // 10: wait for stop 100ms 123} 124 125void GraphicEngine::SetSleepTime(uint32_t sleepTime) 126{ 127 sleepTime_ = sleepTime; 128} 129 130OHOS::BufferInfo *GraphicEngine::GetFBBufferInfo() 131{ 132 if (buffInfo_ != nullptr) { 133 return buffInfo_.get(); 134 } 135 136 uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_); 137 if (pixelBytes == 0) { 138 LOG(ERROR) << "GraphicEngine get pixelBytes fail"; 139 return nullptr; 140 } 141 142 if ((width_ == 0) || (height_ == 0)) { 143 LOG(ERROR) << "input error, width: " << width_ << ", height: " << height_; 144 return nullptr; 145 } 146 UiRotation::GetInstance().InitRotation(width_, height_, pixelBytes); 147 width_ = UiRotation::GetInstance().GetWidth(); 148 height_ = UiRotation::GetInstance().GetHeight(); 149 virAddr_ = std::make_unique<uint8_t[]>(width_ * height_ * pixelBytes); 150 buffInfo_ = std::make_unique<OHOS::BufferInfo>(); 151 buffInfo_->rect = { 0, 0, static_cast<int16_t>(width_ - 1), static_cast<int16_t>(height_ - 1) }; 152 buffInfo_->mode = static_cast<OHOS::ColorMode>(colorMode_); 153 buffInfo_->color = bkgColor_; 154 buffInfo_->virAddr = virAddr_.get(); 155 buffInfo_->phyAddr = buffInfo_->virAddr; 156 buffInfo_->stride = static_cast<uint32_t>(width_ * pixelBytes); 157 buffInfo_->width = width_; 158 buffInfo_->height = height_; 159 160 return buffInfo_.get(); 161} 162 163void GraphicEngine::Flush(const OHOS::Rect& flushRect) 164{ 165 if ((sfDev_ == nullptr) || (buffInfo_ == nullptr)) { 166 LOG(ERROR) << "null error"; 167 return; 168 } 169 std::lock_guard<std::mutex> lock {mtx_}; 170 UiRotation::GetInstance().SetFlushRange(flushRect); 171 sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr)); 172} 173 174uint16_t GraphicEngine::GetScreenWidth() 175{ 176 return width_; 177} 178 179uint16_t GraphicEngine::GetScreenHeight() 180{ 181 return height_; 182} 183} // namespace Updater 184