• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_CACHE_DATA_H
17 #define OHOS_CACHE_DATA_H
18 
19 #include <cstddef>
20 #include <memory>
21 #include <vector>
22 #include <string>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <cstdint>
26 
27 namespace OHOS {
28 namespace Rosen {
29 class CacheData {
30 public:
31     enum class ErrorCode {
32         NO_ERR = 0,
33         KEY_NOT_FOUND,
34         VALUE_SIZE_TOO_SAMLL,
35         VALUE_SIZE_OVER_MAX_SIZE,
36         COPY_FAILED
37     };
38 
39     CacheData(const size_t maxKeySize, const size_t maxValueSize,
40         const size_t maxTotalSize, const std::string& fileName);
41 
42     ~CacheData();
43 
44     void Rewrite(const void *key, const size_t keySize, const void *value, const size_t valueSize);
45 
46     std::tuple<CacheData::ErrorCode, size_t> Get(const void *key, const size_t keySize,
47         void *value, const size_t valueSize);
48 
49     size_t SerializedSize() const;
50 
51     int Serialize(uint8_t *buffer, const size_t size) const;
52 
53     void CacheReadFromFile(const std::string filePath);
54 
55     void WriteToFile();
56 
57     void ReadFromFile();
58 
59     int DeSerialize(uint8_t const *buffer, const size_t size);
60 
Clear()61     void Clear()
62     {
63         shaderPointers_.clear();
64         totalSize_ = 0;
65     }
66 
GetTotalSize()67     size_t GetTotalSize() const
68     {
69         return totalSize_;
70     }
71 
GetShaderNum()72     size_t GetShaderNum() const
73     {
74         return shaderPointers_.size();
75     }
76 
77     bool IsValidFile(uint8_t *buffer, size_t bufferSize);
78 
79     uint32_t CrcGen(const uint8_t *buffer, size_t bufferSize);
80 
81     void DumpAbnormalCacheToFile(uint8_t *buffer, size_t bufferSize);
82 
83     bool CheckShaderCacheOverSoftLimit() const;
84 
85     void PurgeShaderCacheAfterAnimate(const std::function<bool(void)>& nextFrameHasArrived);
86 
87 private:
88     CacheData(const CacheData&);
89     void operator=(const CacheData&);
90 
91     unsigned short cleanInit_[3] = {0};
92     size_t cleanThreshold_ = 0;
93 
94     bool IfSizeValidate(const size_t newSize, const size_t addedSize) const;
95     bool IfSkipClean(const size_t addedSize) const;
96     bool IfCleanFinished();
97     void RandClean(const size_t cleanThreshold);
98     bool CleanInit();
99     bool StepClean();
100     size_t Clean(const size_t removeIndex);
101 
Align4(size_t size)102     static inline size_t Align4(size_t size)
103     {
104         return (size + ALIGN_FOUR) & ~ALIGN_FOUR;
105     }
106 
107     class DataPointer {
108     public:
109         DataPointer(const void *data, size_t size, bool ifOccupy);
110         ~DataPointer();
111 
112         bool operator<(const DataPointer& rValue) const
113         {
114             if (size_ == rValue.size_) {
115                 return memcmp(pointer_, rValue.pointer_, size_) < 0;
116             } else {
117                 return size_ < rValue.size_;
118             }
119         }
GetData()120         const void *GetData() const
121         {
122             return pointer_;
123         }
GetSize()124         size_t GetSize() const
125         {
126             return size_;
127         }
128 
129     private:
130         DataPointer(const DataPointer&);
131         void operator=(const DataPointer&);
132         const void *pointer_;
133         size_t size_;
134         bool toFree_;
135     };
136 
137     class ShaderPointer {
138     public:
139         ShaderPointer();
140         ShaderPointer(const std::shared_ptr<DataPointer>& key, const std::shared_ptr<DataPointer>& value);
141         ShaderPointer(const ShaderPointer& sp);
142         bool operator<(const ShaderPointer& rValue) const
143         {
144             return *keyPointer_ < *rValue.keyPointer_;
145         }
146         const ShaderPointer& operator=(const ShaderPointer& rValue)
147         {
148             keyPointer_ = rValue.keyPointer_;
149             valuePointer_ = rValue.valuePointer_;
150             return *this;
151         }
GetKeyPointer()152         std::shared_ptr<DataPointer> GetKeyPointer() const
153         {
154             return keyPointer_;
155         }
GetValuePointer()156         std::shared_ptr<DataPointer> GetValuePointer() const
157         {
158             return valuePointer_;
159         }
SetValue(const std::shared_ptr<DataPointer> & value)160         void SetValue(const std::shared_ptr<DataPointer>& value)
161         {
162             valuePointer_ = value;
163         }
164 
165     private:
166         std::shared_ptr<DataPointer> keyPointer_;
167         std::shared_ptr<DataPointer> valuePointer_;
168     };
169 
170     struct Header {
171         size_t numShaders_;
172     };
173 
174     struct ShaderData {
175         size_t keySize_;
176         size_t valueSize_;
177         uint8_t data_[];
178     };
179 
180     size_t totalSize_ = 0;
181     std::vector<ShaderPointer> shaderPointers_;
182     size_t numShaders_ = 0;
183 
184     const size_t maxMultipleSize_ = 2;
185     const size_t cleanLevel_ = 2;
186     static const size_t ALIGN_FOUR = 3;
187     static const int ERR_NUMBER = -1;
188     static const int TIME_MAX_LEN = 80;
189     static constexpr const float SHADER_CACHE_SOFT_LIMIT = 0.95;
190     const int randShift_ = 16;
191     const int randLength_ = 3;
192 
193     size_t maxKeySize_;
194     size_t maxValueSize_;
195     size_t maxTotalSize_;
196     std::string cacheDir_;
197     size_t softLimit_;
198 };
199 }   // namespace Rosen
200 }   // namespace OHOS
201 #endif // OHOS_CACHE_DATA_H
202