1/*
2 * Copyright (c) 2020-2021 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 GRAPHIC_LITE_CACHE_MANAGER_H
17#define GRAPHIC_LITE_CACHE_MANAGER_H
18
19#include "file_img_decoder.h"
20
21namespace OHOS {
22class CacheEntry : public HeapBase {
23public:
24    CacheEntry() : dsc_{0}, life_(0) {}
25
26    ~CacheEntry() {}
27
28    ImageSrcType GetImgSrcType() const
29    {
30        return dsc_.srcType;
31    }
32
33    const uint8_t* GetImgData() const;
34
35    ImageHeader GetImgHeader() const
36    {
37        return dsc_.imgInfo.header;
38    }
39
40    ImageInfo GetImageInfo() const
41    {
42        return dsc_.imgInfo;
43    }
44
45    RetCode ReadLine(const Point& start, int16_t bufSize, uint8_t* buf);
46
47    bool InCache() const
48    {
49        return dsc_.inCache_;
50    }
51
52private:
53    static constexpr uint16_t MAX_SRC_LENGTH = 4096;
54
55    friend class CacheManager;
56
57    void Clear();
58    void ClearSrc();
59    RetCode SetSrc(const char* path);
60
61    FileImgDecoder::ImgResDsc dsc_;
62    int32_t life_;
63};
64
65class CacheManager : public HeapBase {
66public:
67    static CacheManager& GetInstance();
68
69    RetCode Init(uint16_t size);
70
71    uint16_t GetSize()
72    {
73        return size_;
74    }
75
76    RetCode Open(const char* path, const Style& style, CacheEntry& entry);
77
78    RetCode Close(const char* path);
79
80    RetCode Reset();
81
82    RetCode ReadToCache(CacheEntry& entry);
83
84    bool GetImageHeader(const char* path, ImageHeader& header);
85
86private:
87    CacheManager() : size_(0), entryArr_(nullptr){}
88
89    ~CacheManager() {}
90
91    void Clear(CacheEntry& entry);
92
93    void AgingAll(int32_t time = AGING_INTERVAL);
94
95    RetCode GetIndex(const char* src, uint16_t& hittedIndex);
96
97    RetCode SelectEntryToReplace(uint16_t& selectedIndex);
98
99    RetCode TryDecode(const char* path, const Style& style, CacheEntry& entry);
100
101    uint16_t size_;
102    CacheEntry* entryArr_;
103    static constexpr uint8_t AGING_INTERVAL = 1;
104    static constexpr uint8_t LIFE_GAIN_INTERVAL = 1;
105    static constexpr uint16_t LIFE_LIMIT = 1000;
106};
107} // namespace OHOS
108
109#endif
110