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_TEXT_STYLE) && ENABLE_TEXT_STYLE
60         TextStyle textStyle;
61 #endif
62         uint8_t data[];
63     };
64 
65     UIFontCache(uint8_t* ram, uint32_t size);
66 
67     ~UIFontCache();
68 
69     /* default textStyle is TEXT_STYLE_NORMAL, TextStyle textStyle = TEXT_STYLE_NORMAL */
70     uint8_t* GetSpace(uint16_t fontKey, uint32_t unicode, uint32_t size, TextStyle textStyle);
71     void PutSpace(uint8_t* addr);
72     uint8_t* GetBitmap(uint16_t fontKey, uint32_t unicode, TextStyle textStyle);
73 
74 private:
75     void UpdateLru(Bitmap* bitmap);
ListInit(ListHead * head)76     void ListInit(ListHead* head)
77     {
78         head->prev = head;
79         head->next = head;
80     }
ListAdd(ListHead * node,ListHead * head)81     void ListAdd(ListHead* node, ListHead* head)
82     {
83         head->next->prev = node;
84         node->next = head->next;
85         node->prev = head;
86         head->next = node;
87     }
ListDel(ListHead * node)88     void ListDel(ListHead* node)
89     {
90         node->next->prev = node->prev;
91         node->prev->next = node->next;
92     }
93 
94     UIFontAllocator allocator_;
95     ListHead* hashTable_ = nullptr;
96     ListHead lruList_ = {};
97 };
98 } // namespace OHOS
99 #endif /* UI_FONT_CACHE_H */
100