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