1a3e0fd82Sopenharmony_ci/* 2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd. 3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License. 5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at 6a3e0fd82Sopenharmony_ci * 7a3e0fd82Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8a3e0fd82Sopenharmony_ci * 9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and 13a3e0fd82Sopenharmony_ci * limitations under the License. 14a3e0fd82Sopenharmony_ci */ 15a3e0fd82Sopenharmony_ci 16a3e0fd82Sopenharmony_ci/* 17a3e0fd82Sopenharmony_ci * Font cache is used for managing font bitmap. 18a3e0fd82Sopenharmony_ci * Each bitmap is contained in a chunk block which can be searched quickly in a hash-table. 19a3e0fd82Sopenharmony_ci * All the struct and memory block are force-aligned to ALIGNMENT_BYTES. 20a3e0fd82Sopenharmony_ci * To make the most frequently used memroy hot, chunk blocks are managed with an easy lru algorithm. 21a3e0fd82Sopenharmony_ci * Memory maps of font cache is shown below: 22a3e0fd82Sopenharmony_ci * 23a3e0fd82Sopenharmony_ci * aligned bitmapCache ─────────►┌────────────────────┐ 24a3e0fd82Sopenharmony_ci * (FONT_BITMAP_CACHE_SIZE) │ HashTable │ 25a3e0fd82Sopenharmony_ci * │ (ListHead*32) │ 26a3e0fd82Sopenharmony_ci * UIFontAllocator::free_ ──────────┼────────────────────┼───────────► ┌──────────────┐ 27a3e0fd82Sopenharmony_ci * │ chunk block │ │ struct Chunk │ 28a3e0fd82Sopenharmony_ci * ├────────────────────┼──────┐ ├──────────────┤ 29a3e0fd82Sopenharmony_ci * │ ... │ │ │ struct Bitmap│ 30a3e0fd82Sopenharmony_ci * ├────────────────────┤ │ ├──────────────┤ 31a3e0fd82Sopenharmony_ci * │ chunk block │ │ │ FontCache │ 32a3e0fd82Sopenharmony_ci * ├────────────────────┤ │ │ (20*20*n) │ 33a3e0fd82Sopenharmony_ci * │ last_chunk │ └────► └──────────────┘ 34a3e0fd82Sopenharmony_ci * │ (Head only) │ 35a3e0fd82Sopenharmony_ci * └────────────────────┘ 36a3e0fd82Sopenharmony_ci */ 37a3e0fd82Sopenharmony_ci#ifndef UI_FONT_CACHE_H 38a3e0fd82Sopenharmony_ci#define UI_FONT_CACHE_H 39a3e0fd82Sopenharmony_ci 40a3e0fd82Sopenharmony_ci#include "ui_font_allocator.h" 41a3e0fd82Sopenharmony_ci#include "font/ui_font_header.h" 42a3e0fd82Sopenharmony_ci 43a3e0fd82Sopenharmony_cinamespace OHOS { 44a3e0fd82Sopenharmony_ciclass UIFontCache : public HeapBase { 45a3e0fd82Sopenharmony_cipublic: 46a3e0fd82Sopenharmony_ci static constexpr uint8_t FONT_CACHE_HASH_NR = 32; 47a3e0fd82Sopenharmony_ci static constexpr uint32_t FONT_CACHE_MIN_SIZE = 20 * 20; 48a3e0fd82Sopenharmony_ci struct UI_STRUCT_ALIGN ListHead { 49a3e0fd82Sopenharmony_ci ListHead* prev; 50a3e0fd82Sopenharmony_ci ListHead* next; 51a3e0fd82Sopenharmony_ci }; 52a3e0fd82Sopenharmony_ci struct UI_STRUCT_ALIGN Bitmap { 53a3e0fd82Sopenharmony_ci ListHead hashHead; 54a3e0fd82Sopenharmony_ci ListHead lruHead; 55a3e0fd82Sopenharmony_ci uint32_t fontId; // bitmap font: fontId vector font: fontKey ttfId + fontsize 56a3e0fd82Sopenharmony_ci uint32_t unicode; 57a3e0fd82Sopenharmony_ci uint32_t reserve1; 58a3e0fd82Sopenharmony_ci uint32_t reserve2; 59a3e0fd82Sopenharmony_ci#if defined(ENABLE_TEXT_STYLE) && ENABLE_TEXT_STYLE 60a3e0fd82Sopenharmony_ci TextStyle textStyle; 61a3e0fd82Sopenharmony_ci#endif 62a3e0fd82Sopenharmony_ci uint8_t data[]; 63a3e0fd82Sopenharmony_ci }; 64a3e0fd82Sopenharmony_ci 65a3e0fd82Sopenharmony_ci UIFontCache(uint8_t* ram, uint32_t size); 66a3e0fd82Sopenharmony_ci 67a3e0fd82Sopenharmony_ci ~UIFontCache(); 68a3e0fd82Sopenharmony_ci 69a3e0fd82Sopenharmony_ci /* default textStyle is TEXT_STYLE_NORMAL, TextStyle textStyle = TEXT_STYLE_NORMAL */ 70a3e0fd82Sopenharmony_ci uint8_t* GetSpace(uint16_t fontKey, uint32_t unicode, uint32_t size, TextStyle textStyle); 71a3e0fd82Sopenharmony_ci void PutSpace(uint8_t* addr); 72a3e0fd82Sopenharmony_ci uint8_t* GetBitmap(uint16_t fontKey, uint32_t unicode, TextStyle textStyle); 73a3e0fd82Sopenharmony_ci 74a3e0fd82Sopenharmony_ciprivate: 75a3e0fd82Sopenharmony_ci void UpdateLru(Bitmap* bitmap); 76a3e0fd82Sopenharmony_ci void ListInit(ListHead* head) 77a3e0fd82Sopenharmony_ci { 78a3e0fd82Sopenharmony_ci head->prev = head; 79a3e0fd82Sopenharmony_ci head->next = head; 80a3e0fd82Sopenharmony_ci } 81a3e0fd82Sopenharmony_ci void ListAdd(ListHead* node, ListHead* head) 82a3e0fd82Sopenharmony_ci { 83a3e0fd82Sopenharmony_ci head->next->prev = node; 84a3e0fd82Sopenharmony_ci node->next = head->next; 85a3e0fd82Sopenharmony_ci node->prev = head; 86a3e0fd82Sopenharmony_ci head->next = node; 87a3e0fd82Sopenharmony_ci } 88a3e0fd82Sopenharmony_ci void ListDel(ListHead* node) 89a3e0fd82Sopenharmony_ci { 90a3e0fd82Sopenharmony_ci node->next->prev = node->prev; 91a3e0fd82Sopenharmony_ci node->prev->next = node->next; 92a3e0fd82Sopenharmony_ci } 93a3e0fd82Sopenharmony_ci 94a3e0fd82Sopenharmony_ci UIFontAllocator allocator_; 95a3e0fd82Sopenharmony_ci ListHead* hashTable_ = nullptr; 96a3e0fd82Sopenharmony_ci ListHead lruList_ = {}; 97a3e0fd82Sopenharmony_ci}; 98a3e0fd82Sopenharmony_ci} // namespace OHOS 99a3e0fd82Sopenharmony_ci#endif /* UI_FONT_CACHE_H */ 100