• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 /*
17  * Font cache is used for managing font bitmap.
18  * Each bitmap is contained in a chunk block which can be searched quickly in a hash-table.
19  * All the struct and memory block are force-aligned to ALIGNMENT_BYTES.
20  * To make the most frequently used memroy hot, chunk blocks are managed with an easy lru algorithm.
21  * Memory maps of font cache is shown below:
22  *
23  *  aligned bitmapCache    ─────────►┌────────────────────┐
24  *  (FONT_BITMAP_CACHE_SIZE)         │    HashTable       │
25  *                                   │    (ListHead*32)   │
26  *  UIFontAllocator::free_ ──────────┼────────────────────┼───────────► ┌──────────────┐
27  *                                   │    chunk block     │             │ struct Chunk │
28  *                                   ├────────────────────┼──────┐      ├──────────────┤
29  *                                   │    ...             │      │      │ struct Bitmap│
30  *                                   ├────────────────────┤      │      ├──────────────┤
31  *                                   │    chunk block     │      │      │  FontCache   │
32  *                                   ├────────────────────┤      │      │  (20*20*n)   │
33  *                                   │    last_chunk      │      └────► └──────────────┘
34  *                                   │    (Head only)     │
35  *                                   └────────────────────┘
36  */
37 #ifndef UI_FONT_CACHE_H
38 #define UI_FONT_CACHE_H
39 
40 #include "ui_font_allocator.h"
41 #include "font/ui_font_header.h"
42 
43 namespace OHOS {
44 class UIFontCache : public HeapBase {
45 public:
46     static constexpr uint8_t FONT_CACHE_HASH_NR = 32;
47     static constexpr uint32_t FONT_CACHE_MIN_SIZE = 20 * 20;
48     struct UI_STRUCT_ALIGN ListHead {
49         ListHead* prev;
50         ListHead* next;
51     };
52     struct UI_STRUCT_ALIGN Bitmap {
53         ListHead hashHead;
54         ListHead lruHead;
55         uint32_t fontId; // bitmap font: fontId vector font: fontKey ttfId + fontsize
56         uint32_t unicode;
57         uint32_t reserve1;
58         uint32_t reserve2;
59 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
60         TextStyle textStyle;
61 #endif
62         uint8_t data[];
63     };
64 
65     UIFontCache(uint8_t* ram, uint32_t size);
66 
67     ~UIFontCache();
68 
69     uint8_t* GetSpace(uint16_t fontKey, uint32_t unicode, uint32_t size, TextStyle textStyle = TEXT_STYLE_NORMAL);
70     void PutSpace(uint8_t* addr);
71     uint8_t* GetBitmap(uint16_t fontKey, uint32_t unicode, TextStyle textStyle = TEXT_STYLE_NORMAL);
72 
73 private:
74     void UpdateLru(Bitmap* bitmap);
ListInit(ListHead * head)75     void ListInit(ListHead* head)
76     {
77         head->prev = head;
78         head->next = head;
79     }
ListAdd(ListHead * node,ListHead * head)80     void ListAdd(ListHead* node, ListHead* head)
81     {
82         head->next->prev = node;
83         node->next = head->next;
84         node->prev = head;
85         head->next = node;
86     }
ListDel(ListHead * node)87     void ListDel(ListHead* node)
88     {
89         node->next->prev = node->prev;
90         node->prev->next = node->next;
91     }
92 
93     UIFontAllocator allocator_;
94     ListHead* hashTable_ = nullptr;
95     ListHead lruList_ = {};
96 };
97 } // namespace OHOS
98 #endif /* UI_FONT_CACHE_H */
99