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 
16 #include "font/ui_font_cache_manager.h"
17 #include "font/font_ram_allocator.h"
18 #include "gfx_utils/graphic_log.h"
19 #include "securec.h"
20 
21 namespace OHOS {
UIFontCacheManager()22 UIFontCacheManager::UIFontCacheManager() : bitmapCache_(nullptr) {}
23 
~UIFontCacheManager()24 UIFontCacheManager::~UIFontCacheManager()
25 {
26     if (bitmapCache_ != nullptr) {
27         delete bitmapCache_;
28         bitmapCache_ = nullptr;
29     }
30 }
31 
GetInstance()32 UIFontCacheManager* UIFontCacheManager::GetInstance()
33 {
34     static UIFontCacheManager instance_;
35     return &instance_;
36 }
37 
GlyphsCacheInit()38 int8_t UIFontCacheManager::GlyphsCacheInit()
39 {
40     return glyphsCache_.CacheInit();
41 }
42 
ClearCacheFlag()43 void UIFontCacheManager::ClearCacheFlag()
44 {
45     return glyphsCache_.ClearCacheFlag();
46 }
47 
GetNodeFromCache(uint32_t unicode,uint16_t fontKey,uint16_t cacheType)48 GlyphCacheNode* UIFontCacheManager::GetNodeFromCache(uint32_t unicode, uint16_t fontKey, uint16_t cacheType)
49 {
50     return glyphsCache_.GetNodeFromCache(unicode, fontKey, cacheType);
51 }
52 
GetNodeCacheSpace(uint32_t unicode,uint16_t fontKey)53 GlyphCacheNode* UIFontCacheManager::GetNodeCacheSpace(uint32_t unicode, uint16_t fontKey)
54 {
55     return glyphsCache_.GetNodeCacheSpace(unicode, fontKey);
56 }
57 
SetBitmapCacheSize(uint32_t bitmapCacheSize)58 void UIFontCacheManager::SetBitmapCacheSize(uint32_t bitmapCacheSize)
59 {
60     bitmapCacheSize_ = bitmapCacheSize;
61 }
62 
BitmapCacheInit()63 void UIFontCacheManager::BitmapCacheInit()
64 {
65     if (bitmapCache_ != nullptr) {
66         return;
67     }
68 
69     uint8_t* bitmapCacheAddr =
70         reinterpret_cast<uint8_t*>(FontRamAllocator::GetInstance().DynamicAllocate(bitmapCacheSize_));
71     if (bitmapCacheAddr == nullptr) {
72         GRAPHIC_LOGE("UIFontCacheManager::BitmapCacheInit allocate failed");
73         return;
74     }
75 
76     if (memset_s(bitmapCacheAddr, bitmapCacheSize_, 0, bitmapCacheSize_)!= EOK) {
77         GRAPHIC_LOGE("UIFontCacheManager::BitmapCacheInit memset failed");
78         return;
79     }
80     bitmapCache_ = new UIFontCache(bitmapCacheAddr, bitmapCacheSize_);
81 }
82 
BitmapCacheClear()83 void UIFontCacheManager::BitmapCacheClear()
84 {
85     delete bitmapCache_;
86     bitmapCache_ = nullptr;
87 }
88 
GetSpace(uint16_t fontKey,uint32_t unicode,uint32_t size,TextStyle textStyle)89 uint8_t* UIFontCacheManager::GetSpace(uint16_t fontKey, uint32_t unicode, uint32_t size, TextStyle textStyle)
90 {
91     if (bitmapCache_ != nullptr) {
92         return bitmapCache_->GetSpace(fontKey, unicode, size, textStyle);
93     }
94     GRAPHIC_LOGE("UIFontCacheManager::GetSpace invalid bitmapCache");
95     return nullptr;
96 }
97 
PutSpace(uint8_t * addr)98 void UIFontCacheManager::PutSpace(uint8_t* addr)
99 {
100     if (bitmapCache_ != nullptr) {
101         bitmapCache_->PutSpace(addr);
102     }
103     GRAPHIC_LOGE("UIFontCacheManager::PutSpace invalid bitmapCache");
104 }
105 
GetBitmap(uint16_t fontKey,uint32_t unicode,TextStyle textStyle)106 uint8_t* UIFontCacheManager::GetBitmap(uint16_t fontKey, uint32_t unicode, TextStyle textStyle)
107 {
108     if (bitmapCache_ != nullptr) {
109         return bitmapCache_->GetBitmap(fontKey, unicode, textStyle);
110     }
111     GRAPHIC_LOGE("UIFontCacheManager::GetBitmap invalid bitmapCache");
112     return nullptr;
113 }
114 } // namespace OHOS
115