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#ifndef GLYPHS_CACHE_H
17#define GLYPHS_CACHE_H
18
19#include "font/ui_font_header.h"
20#include "gfx_utils/heap_base.h"
21
22namespace OHOS {
23class GlyphsCache : public HeapBase {
24public:
25    GlyphsCache();
26
27    GlyphsCache(const GlyphsCache&) = delete;
28
29    GlyphsCache& operator=(const GlyphsCache&) = delete;
30
31    GlyphsCache(const GlyphsCache&&) = delete;
32
33    GlyphsCache& operator=(const GlyphsCache&&) = delete;
34
35    ~GlyphsCache();
36
37    int8_t CacheInit();
38    void ClearCacheFlag();
39    GlyphCacheNode* GetNodeFromCache(uint32_t unicode, uint16_t fontKey, uint16_t cacheType);
40    GlyphCacheNode* GetNodeCacheSpace(uint32_t unicode, uint16_t fontId);
41
42private:
43#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
44    static constexpr uint8_t FONT_HASH_SHIFT = 3;
45#else
46    static constexpr uint8_t FONT_HASH_SHIFT = 4;
47#endif
48    static constexpr uint8_t FONT_HASH_NR = 1 << FONT_HASH_SHIFT;
49    static constexpr uint32_t FONT_HASH_MASK = FONT_HASH_NR - 1;
50    static constexpr uint8_t UNICODE_HASH_SHIFT = 6;
51    static constexpr uint8_t UNICODE_HASH_NR = 1 << UNICODE_HASH_SHIFT;
52    static constexpr uint32_t UNICODE_HASH_MASK = UNICODE_HASH_NR - 1;
53    static constexpr uint8_t NODE_HASH_SHIFT = 4;
54    static constexpr uint8_t NODE_HASH_NR = 1 << NODE_HASH_SHIFT;
55
56    using CacheType = GlyphCacheNode[FONT_HASH_NR][UNICODE_HASH_NR][NODE_HASH_NR];
57    using CacheState = uint8_t[FONT_HASH_NR][UNICODE_HASH_NR];
58
59    CacheType* nodeCache_;
60    CacheState* cacheStatus_;
61    bool hasInit_;
62};
63} // namespace OHOS
64#endif /* GLYPHS_CACHE_H */