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 WriteToFile(); 54 55 void ReadFromFile(); 56 57 int DeSerialize(uint8_t const *buffer, const size_t size); 58 Clear()59 void Clear() 60 { 61 shaderPointers_.clear(); 62 totalSize_ = 0; 63 } 64 GetTotalSize()65 size_t GetTotalSize() const 66 { 67 return totalSize_; 68 } 69 GetShaderNum()70 size_t GetShaderNum() const 71 { 72 return shaderPointers_.size(); 73 } 74 75 bool IsValidFile(uint8_t *buffer, size_t bufferSize); 76 77 uint32_t CrcGen(const uint8_t *buffer, size_t bufferSize); 78 79 private: 80 CacheData(const CacheData&); 81 void operator=(const CacheData&); 82 83 unsigned short cleanInit_[3] = {0}; 84 size_t cleanThreshold_ = 0; 85 86 bool IfSizeValidate(const size_t newSize, const size_t addedSize) const; 87 bool IfSkipClean(const size_t addedSize) const; 88 bool IfCleanFinished(); 89 void RandClean(const size_t cleanThreshold); 90 size_t Clean(const size_t removeIndex); 91 Align4(size_t size)92 static inline size_t Align4(size_t size) 93 { 94 return (size + ALIGN_FOUR) & ~ALIGN_FOUR; 95 } 96 97 class DataPointer { 98 public: 99 DataPointer(const void *data, size_t size, bool ifOccupy); 100 ~DataPointer(); 101 102 bool operator<(const DataPointer& rValue) const 103 { 104 if (size_ == rValue.size_) { 105 return memcmp(pointer_, rValue.pointer_, size_) < 0; 106 } else { 107 return size_ < rValue.size_; 108 } 109 } GetData()110 const void *GetData() const 111 { 112 return pointer_; 113 } GetSize()114 size_t GetSize() const 115 { 116 return size_; 117 } 118 119 private: 120 DataPointer(const DataPointer&); 121 void operator=(const DataPointer&); 122 const void *pointer_; 123 size_t size_; 124 bool toFree_; 125 }; 126 127 class ShaderPointer { 128 public: 129 ShaderPointer(); 130 ShaderPointer(const std::shared_ptr<DataPointer>& key, const std::shared_ptr<DataPointer>& value); 131 ShaderPointer(const ShaderPointer& sp); 132 bool operator<(const ShaderPointer& rValue) const 133 { 134 return *keyPointer_ < *rValue.keyPointer_; 135 } 136 const ShaderPointer& operator=(const ShaderPointer& rValue) 137 { 138 keyPointer_ = rValue.keyPointer_; 139 valuePointer_ = rValue.valuePointer_; 140 return *this; 141 } GetKeyPointer()142 std::shared_ptr<DataPointer> GetKeyPointer() const 143 { 144 return keyPointer_; 145 } GetValuePointer()146 std::shared_ptr<DataPointer> GetValuePointer() const 147 { 148 return valuePointer_; 149 } SetValue(const std::shared_ptr<DataPointer> & value)150 void SetValue(const std::shared_ptr<DataPointer>& value) 151 { 152 valuePointer_ = value; 153 } 154 155 private: 156 std::shared_ptr<DataPointer> keyPointer_; 157 std::shared_ptr<DataPointer> valuePointer_; 158 }; 159 160 struct Header { 161 size_t numShaders_; 162 }; 163 164 struct ShaderData { 165 size_t keySize_; 166 size_t valueSize_; 167 uint8_t data_[]; 168 }; 169 170 size_t totalSize_ = 0; 171 std::vector<ShaderPointer> shaderPointers_; 172 size_t numShaders_ = 0; 173 174 const size_t maxMultipleSize_ = 2; 175 const size_t cleanLevel_ = 2; 176 static const size_t ALIGN_FOUR = 3; 177 static const int ERR_NUMBER = -1; 178 static const int TIME_MAX_LEN = 80; 179 const int randShift_ = 16; 180 const int randLength_ = 3; 181 182 size_t maxKeySize_; 183 size_t maxValueSize_; 184 size_t maxTotalSize_; 185 std::string cacheDir_; 186 }; 187 } // namespace Rosen 188 } // namespace OHOS 189 #endif // OHOS_CACHE_DATA_H 190