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