• 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     CacheData(const size_t maxKeySize, const size_t maxValueSize,
32         const size_t maxTotalSize, const std::string& fileName);
33 
34     ~CacheData();
35 
36     void Rewrite(const void *key, const size_t keySize, const void *value, const size_t valueSize);
37 
38     size_t Get(const void *key, const size_t keySize, void *value, const size_t valueSize);
39 
40     size_t SerializedSize() const;
41 
42     int Serialize(uint8_t *buffer, const size_t size) const;
43 
44     void WriteToFile();
45 
46     void ReadFromFile();
47 
48     int DeSerialize(uint8_t const *buffer, const size_t size);
49 
Clear()50     void Clear()
51     {
52         shaderPointers_.clear();
53         totalSize_ = 0;
54     }
55 
GetTotalSize()56     size_t GetTotalSize() const
57     {
58         return totalSize_;
59     }
60 
GetShaderNum()61     size_t GetShaderNum() const
62     {
63         return shaderPointers_.size();
64     }
65 
66 private:
67     CacheData(const CacheData&);
68     void operator=(const CacheData&);
69 
70     unsigned short cleanInit_[3] = {0};
71     size_t cleanThreshold_ = 0;
72 
73     bool IfSizeValidate(const size_t newSize, const size_t addedSize) const;
74     bool IfSkipClean(const size_t addedSize) const;
75     bool IfCleanFinished();
76     void RandClean(const size_t cleanThreshold);
77     size_t Clean(const size_t removeIndex);
78 
Align4(size_t size)79     static inline size_t Align4(size_t size)
80     {
81         return (size + ALIGN_FOUR) & ~ALIGN_FOUR;
82     }
83 
84     class DataPointer {
85     public:
86         DataPointer(const void *data, size_t size, bool ifOccupy);
87         ~DataPointer();
88 
89         bool operator<(const DataPointer& rValue) const
90         {
91             if (size_ == rValue.size_) {
92                 return memcmp(pointer_, rValue.pointer_, size_) < 0;
93             } else {
94                 return size_ < rValue.size_;
95             }
96         }
GetData()97         const void *GetData() const
98         {
99             return pointer_;
100         }
GetSize()101         size_t GetSize() const
102         {
103             return size_;
104         }
105 
106     private:
107         DataPointer(const DataPointer&);
108         void operator=(const DataPointer&);
109         const void *pointer_;
110         size_t size_;
111         bool toFree_;
112     };
113 
114     class ShaderPointer {
115     public:
116         ShaderPointer();
117         ShaderPointer(const std::shared_ptr<DataPointer>& key, const std::shared_ptr<DataPointer>& value);
118         ShaderPointer(const ShaderPointer& sp);
119         bool operator<(const ShaderPointer& rValue) const
120         {
121             return *keyPointer_ < *rValue.keyPointer_;
122         }
123         const ShaderPointer& operator=(const ShaderPointer& rValue)
124         {
125             keyPointer_ = rValue.keyPointer_;
126             valuePointer_ = rValue.valuePointer_;
127             return *this;
128         }
GetKeyPointer()129         std::shared_ptr<DataPointer> GetKeyPointer() const
130         {
131             return keyPointer_;
132         }
GetValuePointer()133         std::shared_ptr<DataPointer> GetValuePointer() const
134         {
135             return valuePointer_;
136         }
SetValue(const std::shared_ptr<DataPointer> & value)137         void SetValue(const std::shared_ptr<DataPointer>& value)
138         {
139             valuePointer_ = value;
140         }
141 
142     private:
143         std::shared_ptr<DataPointer> keyPointer_;
144         std::shared_ptr<DataPointer> valuePointer_;
145     };
146 
147     struct Header {
148         size_t numShaders_;
149     };
150 
151     struct ShaderData {
152         size_t keySize_;
153         size_t valueSize_;
154         uint8_t data_[];
155     };
156 
157     size_t totalSize_ = 0;
158     std::vector<ShaderPointer> shaderPointers_;
159     size_t numShaders_ = 0;
160 
161     const size_t MAX_MULTIPLE_SIZE = 2;
162     const size_t CLEAN_LEVEL = 2;
163     static const size_t ALIGN_FOUR = 3;
164     static const int ERR_NUMBER = -1;
165     const int RAND_SHIFT = 16;
166     const int RAND_LENGTH = 3;
167 
168     size_t maxKeySize_;
169     size_t maxValueSize_;
170     size_t maxTotalSize_;
171     std::string cacheDir_;
172 };
173 }   // namespace Rosen
174 }   // namespace OHOS
175 #endif // OHOS_CACHE_DATA_H
176