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/glyphs_cache.h"
17 #include "font/font_ram_allocator.h"
18 #include "font/ui_font_builder.h"
19 #include "gfx_utils/graphic_log.h"
20 #include "securec.h"
21
22 namespace OHOS {
GlyphsCache()23 GlyphsCache::GlyphsCache()
24 : nodeCache_(nullptr),
25 cacheStatus_(nullptr),
26 hasInit_(false)
27 {
28 }
~GlyphsCache()29 GlyphsCache::~GlyphsCache() {}
30
CacheInit()31 int8_t GlyphsCache::CacheInit()
32 {
33 if (hasInit_) {
34 return RET_VALUE_OK;
35 }
36
37 cacheStatus_ = reinterpret_cast<CacheState*>(FontRamAllocator::GetInstance().DynamicAllocate(sizeof(CacheState)));
38 if (cacheStatus_ == nullptr) {
39 GRAPHIC_LOGE("GlyphsCache::CacheInit allocate cache status failed");
40 return INVALID_RET_VALUE;
41 }
42
43 if (memset_s(cacheStatus_, sizeof(CacheState), 0, sizeof(CacheState)) != EOK) {
44 GRAPHIC_LOGE("GlyphsCache::CacheInit init cache status failed");
45 return INVALID_RET_VALUE;
46 }
47
48 nodeCache_ = reinterpret_cast<CacheType*>(FontRamAllocator::GetInstance().DynamicAllocate(sizeof(CacheType)));
49 if (nodeCache_ == nullptr) {
50 GRAPHIC_LOGE("GlyphsCache::CacheInit allocate node cache failed");
51 return INVALID_RET_VALUE;
52 }
53
54 if (memset_s(nodeCache_, sizeof(CacheType), 0, sizeof(CacheType)) != EOK) {
55 GRAPHIC_LOGE("GlyphsCache::CacheInit init node cache failed");
56 return INVALID_RET_VALUE;
57 }
58
59 hasInit_ = true;
60 return RET_VALUE_OK;
61 }
62
ClearCacheFlag()63 void GlyphsCache::ClearCacheFlag()
64 {
65 hasInit_ = false;
66 }
67
GetNodeFromCache(uint32_t unicode,uint16_t fontKey,uint16_t cacheType)68 GlyphCacheNode* GlyphsCache::GetNodeFromCache(uint32_t unicode, uint16_t fontKey, uint16_t cacheType)
69 {
70 if (!hasInit_) {
71 return nullptr;
72 }
73
74 GlyphCacheNode* node = nullptr;
75
76 uint8_t font = (fontKey ^ unicode) & FONT_HASH_MASK;
77 uint8_t uc = unicode & UNICODE_HASH_MASK;
78 for (uint8_t i = 0; i < NODE_HASH_NR; i++) {
79 GlyphCacheNode* p = &((*nodeCache_)[font][uc][i]);
80 if ((p->node.unicode == unicode) && (p->node.fontId == fontKey)) {
81 if (!cacheType || p->cacheType == cacheType) {
82 node = p;
83 break;
84 }
85 }
86 }
87 return node;
88 }
89
GetNodeCacheSpace(uint32_t unicode,uint16_t fontKey)90 GlyphCacheNode* GlyphsCache::GetNodeCacheSpace(uint32_t unicode, uint16_t fontKey)
91 {
92 if (!hasInit_) {
93 return nullptr;
94 }
95
96 uint8_t font = (fontKey ^ unicode) & FONT_HASH_MASK;
97 uint8_t uc = unicode & UNICODE_HASH_MASK;
98 uint8_t i = (*cacheStatus_)[font][uc];
99 GlyphCacheNode* node = &((*nodeCache_)[font][uc][i]);
100
101 i++;
102 if (i >= NODE_HASH_NR) {
103 i = 0;
104 }
105 (*cacheStatus_)[font][uc] = i;
106
107 return node;
108 }
109 } // namespace OHOS
110