1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "graphic_engine.h" 16fb299fa2Sopenharmony_ci#include "common/graphic_startup.h" 17fb299fa2Sopenharmony_ci#include "common/image_decode_ability.h" 18fb299fa2Sopenharmony_ci#include "common/task_manager.h" 19fb299fa2Sopenharmony_ci#include "draw/draw_utils.h" 20fb299fa2Sopenharmony_ci#include "font/ui_font_cache_manager.h" 21fb299fa2Sopenharmony_ci#include "font/ui_font_header.h" 22fb299fa2Sopenharmony_ci#include "log/log.h" 23fb299fa2Sopenharmony_ci#include "updater_ui_const.h" 24fb299fa2Sopenharmony_ci#include "ui_rotation.h" 25fb299fa2Sopenharmony_ci#include "utils.h" 26fb299fa2Sopenharmony_ci 27fb299fa2Sopenharmony_cinamespace Updater { 28fb299fa2Sopenharmony_ciGraphicEngine &GraphicEngine::GetInstance() 29fb299fa2Sopenharmony_ci{ 30fb299fa2Sopenharmony_ci static GraphicEngine instance; 31fb299fa2Sopenharmony_ci static bool isRegister = false; 32fb299fa2Sopenharmony_ci if (!isRegister) { 33fb299fa2Sopenharmony_ci OHOS::SoftEngine::InitGfxEngine(&instance); 34fb299fa2Sopenharmony_ci isRegister = true; 35fb299fa2Sopenharmony_ci } 36fb299fa2Sopenharmony_ci 37fb299fa2Sopenharmony_ci return instance; 38fb299fa2Sopenharmony_ci} 39fb299fa2Sopenharmony_ci 40fb299fa2Sopenharmony_ci__attribute__((weak)) void PostInitSurfDev(std::unique_ptr<SurfaceDev> &surfDev, GrSurface &surface) 41fb299fa2Sopenharmony_ci{ 42fb299fa2Sopenharmony_ci LOG(INFO) << "not inited the post InitSurfDev process"; 43fb299fa2Sopenharmony_ci return; 44fb299fa2Sopenharmony_ci} 45fb299fa2Sopenharmony_ci 46fb299fa2Sopenharmony_civoid GraphicEngine::Init(uint32_t bkgColor, uint8_t mode, const char *fontPath) 47fb299fa2Sopenharmony_ci{ 48fb299fa2Sopenharmony_ci bkgColor_ = bkgColor; 49fb299fa2Sopenharmony_ci colorMode_ = mode; 50fb299fa2Sopenharmony_ci [[maybe_unused]] static bool initOnce = [this, fontPath] () { 51fb299fa2Sopenharmony_ci sfDev_ = std::make_unique<SurfaceDev>(); 52fb299fa2Sopenharmony_ci if (!sfDev_->Init()) { 53fb299fa2Sopenharmony_ci LOG(INFO) << "GraphicEngine Init failed!"; 54fb299fa2Sopenharmony_ci return false; 55fb299fa2Sopenharmony_ci } 56fb299fa2Sopenharmony_ci GrSurface surface {}; 57fb299fa2Sopenharmony_ci sfDev_->GetScreenSize(width_, height_, surface); 58fb299fa2Sopenharmony_ci PostInitSurfDev(sfDev_, surface); 59fb299fa2Sopenharmony_ci buffInfo_ = nullptr; 60fb299fa2Sopenharmony_ci virAddr_ = nullptr; 61fb299fa2Sopenharmony_ci InitFontEngine(fontPath); 62fb299fa2Sopenharmony_ci InitImageDecodeAbility(); 63fb299fa2Sopenharmony_ci InitFlushThread(); 64fb299fa2Sopenharmony_ci LOG(INFO) << "GraphicEngine Init width: " << width_ << ", height: " << height_ << ", bkgColor: " << bkgColor_; 65fb299fa2Sopenharmony_ci return true; 66fb299fa2Sopenharmony_ci } (); 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_civoid GraphicEngine::InitFontEngine(const char *fontPath) const 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci constexpr uint32_t uiFontMemAlignment = 4; 72fb299fa2Sopenharmony_ci constexpr uint32_t fontPsramSize = OHOS::MIN_FONT_PSRAM_LENGTH * 2; // 2 : alloc more ram to optimize perfomance 73fb299fa2Sopenharmony_ci constexpr uint32_t fontCacheSize = 0xC8000; // fontCacheSize should match with psram length 74fb299fa2Sopenharmony_ci static uint32_t fontMemBaseAddr[fontPsramSize / uiFontMemAlignment]; 75fb299fa2Sopenharmony_ci static uint8_t icuMemBaseAddr[OHOS::SHAPING_WORD_DICT_LENGTH]; 76fb299fa2Sopenharmony_ci OHOS::UIFontCacheManager::GetInstance()->SetBitmapCacheSize(fontCacheSize); 77fb299fa2Sopenharmony_ci OHOS::GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(fontMemBaseAddr), fontPsramSize, 78fb299fa2Sopenharmony_ci fontPath, DEFAULT_FONT_FILENAME); 79fb299fa2Sopenharmony_ci OHOS::GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(icuMemBaseAddr), 80fb299fa2Sopenharmony_ci OHOS::SHAPING_WORD_DICT_LENGTH, fontPath, DEFAULT_LINE_BREAK_RULE_FILENAME); 81fb299fa2Sopenharmony_ci LOG(INFO) << "fontPath = " << fontPath << ", InitFontEngine DEFAULT_FONT_FILENAME = " << DEFAULT_FONT_FILENAME << 82fb299fa2Sopenharmony_ci ", InitLineBreakEngine DEFAULT_LINE_BREAK_RULE_FILENAME = " << DEFAULT_LINE_BREAK_RULE_FILENAME; 83fb299fa2Sopenharmony_ci} 84fb299fa2Sopenharmony_ci 85fb299fa2Sopenharmony_civoid GraphicEngine::InitImageDecodeAbility() const 86fb299fa2Sopenharmony_ci{ 87fb299fa2Sopenharmony_ci uint32_t imageType = OHOS::IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG; 88fb299fa2Sopenharmony_ci OHOS::ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType); 89fb299fa2Sopenharmony_ci} 90fb299fa2Sopenharmony_ci 91fb299fa2Sopenharmony_civoid GraphicEngine::InitFlushThread() 92fb299fa2Sopenharmony_ci{ 93fb299fa2Sopenharmony_ci flushStop_ = false; 94fb299fa2Sopenharmony_ci flushLoop_ = std::thread([this] { 95fb299fa2Sopenharmony_ci this->FlushThreadLoop(); 96fb299fa2Sopenharmony_ci }); 97fb299fa2Sopenharmony_ci flushLoop_.detach(); 98fb299fa2Sopenharmony_ci LOG(INFO) << "init flush thread"; 99fb299fa2Sopenharmony_ci} 100fb299fa2Sopenharmony_ci 101fb299fa2Sopenharmony_ci__attribute__((weak)) void InitFlushBatteryStatusExt(void) 102fb299fa2Sopenharmony_ci{ 103fb299fa2Sopenharmony_ci} 104fb299fa2Sopenharmony_ci 105fb299fa2Sopenharmony_civoid GraphicEngine::FlushThreadLoop() const 106fb299fa2Sopenharmony_ci{ 107fb299fa2Sopenharmony_ci while (!flushStop_) { 108fb299fa2Sopenharmony_ci OHOS::TaskManager::GetInstance()->TaskHandler(); 109fb299fa2Sopenharmony_ci InitFlushBatteryStatusExt(); 110fb299fa2Sopenharmony_ci Utils::UsSleep(sleepTime_); 111fb299fa2Sopenharmony_ci } 112fb299fa2Sopenharmony_ci // clear screen after stop 113fb299fa2Sopenharmony_ci UiRotation::GetInstance().SetDegree(UI_ROTATION_DEGREE::UI_ROTATION_0); 114fb299fa2Sopenharmony_ci uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_); 115fb299fa2Sopenharmony_ci (void)memset_s(buffInfo_->virAddr, width_ * height_ * pixelBytes, 0, width_ * height_ * pixelBytes); 116fb299fa2Sopenharmony_ci sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr)); 117fb299fa2Sopenharmony_ci} 118fb299fa2Sopenharmony_ci 119fb299fa2Sopenharmony_civoid GraphicEngine::StopEngine(void) 120fb299fa2Sopenharmony_ci{ 121fb299fa2Sopenharmony_ci flushStop_ = true; 122fb299fa2Sopenharmony_ci Utils::UsSleep(THREAD_USLEEP_TIME * 10); // 10: wait for stop 100ms 123fb299fa2Sopenharmony_ci} 124fb299fa2Sopenharmony_ci 125fb299fa2Sopenharmony_civoid GraphicEngine::SetSleepTime(uint32_t sleepTime) 126fb299fa2Sopenharmony_ci{ 127fb299fa2Sopenharmony_ci sleepTime_ = sleepTime; 128fb299fa2Sopenharmony_ci} 129fb299fa2Sopenharmony_ci 130fb299fa2Sopenharmony_ciOHOS::BufferInfo *GraphicEngine::GetFBBufferInfo() 131fb299fa2Sopenharmony_ci{ 132fb299fa2Sopenharmony_ci if (buffInfo_ != nullptr) { 133fb299fa2Sopenharmony_ci return buffInfo_.get(); 134fb299fa2Sopenharmony_ci } 135fb299fa2Sopenharmony_ci 136fb299fa2Sopenharmony_ci uint8_t pixelBytes = OHOS::DrawUtils::GetByteSizeByColorMode(colorMode_); 137fb299fa2Sopenharmony_ci if (pixelBytes == 0) { 138fb299fa2Sopenharmony_ci LOG(ERROR) << "GraphicEngine get pixelBytes fail"; 139fb299fa2Sopenharmony_ci return nullptr; 140fb299fa2Sopenharmony_ci } 141fb299fa2Sopenharmony_ci 142fb299fa2Sopenharmony_ci if ((width_ == 0) || (height_ == 0)) { 143fb299fa2Sopenharmony_ci LOG(ERROR) << "input error, width: " << width_ << ", height: " << height_; 144fb299fa2Sopenharmony_ci return nullptr; 145fb299fa2Sopenharmony_ci } 146fb299fa2Sopenharmony_ci UiRotation::GetInstance().InitRotation(width_, height_, pixelBytes); 147fb299fa2Sopenharmony_ci width_ = UiRotation::GetInstance().GetWidth(); 148fb299fa2Sopenharmony_ci height_ = UiRotation::GetInstance().GetHeight(); 149fb299fa2Sopenharmony_ci virAddr_ = std::make_unique<uint8_t[]>(width_ * height_ * pixelBytes); 150fb299fa2Sopenharmony_ci buffInfo_ = std::make_unique<OHOS::BufferInfo>(); 151fb299fa2Sopenharmony_ci buffInfo_->rect = { 0, 0, static_cast<int16_t>(width_ - 1), static_cast<int16_t>(height_ - 1) }; 152fb299fa2Sopenharmony_ci buffInfo_->mode = static_cast<OHOS::ColorMode>(colorMode_); 153fb299fa2Sopenharmony_ci buffInfo_->color = bkgColor_; 154fb299fa2Sopenharmony_ci buffInfo_->virAddr = virAddr_.get(); 155fb299fa2Sopenharmony_ci buffInfo_->phyAddr = buffInfo_->virAddr; 156fb299fa2Sopenharmony_ci buffInfo_->stride = static_cast<uint32_t>(width_ * pixelBytes); 157fb299fa2Sopenharmony_ci buffInfo_->width = width_; 158fb299fa2Sopenharmony_ci buffInfo_->height = height_; 159fb299fa2Sopenharmony_ci 160fb299fa2Sopenharmony_ci return buffInfo_.get(); 161fb299fa2Sopenharmony_ci} 162fb299fa2Sopenharmony_ci 163fb299fa2Sopenharmony_civoid GraphicEngine::Flush(const OHOS::Rect& flushRect) 164fb299fa2Sopenharmony_ci{ 165fb299fa2Sopenharmony_ci if ((sfDev_ == nullptr) || (buffInfo_ == nullptr)) { 166fb299fa2Sopenharmony_ci LOG(ERROR) << "null error"; 167fb299fa2Sopenharmony_ci return; 168fb299fa2Sopenharmony_ci } 169fb299fa2Sopenharmony_ci std::lock_guard<std::mutex> lock {mtx_}; 170fb299fa2Sopenharmony_ci UiRotation::GetInstance().SetFlushRange(flushRect); 171fb299fa2Sopenharmony_ci sfDev_->Flip(reinterpret_cast<uint8_t *>(buffInfo_->virAddr)); 172fb299fa2Sopenharmony_ci} 173fb299fa2Sopenharmony_ci 174fb299fa2Sopenharmony_ciuint16_t GraphicEngine::GetScreenWidth() 175fb299fa2Sopenharmony_ci{ 176fb299fa2Sopenharmony_ci return width_; 177fb299fa2Sopenharmony_ci} 178fb299fa2Sopenharmony_ci 179fb299fa2Sopenharmony_ciuint16_t GraphicEngine::GetScreenHeight() 180fb299fa2Sopenharmony_ci{ 181fb299fa2Sopenharmony_ci return height_; 182fb299fa2Sopenharmony_ci} 183fb299fa2Sopenharmony_ci} // namespace Updater 184