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_SHADER_CACHE_H 17 #define OHOS_SHADER_CACHE_H 18 19 #include <include/gpu/GrContext.h> 20 #include <include/gpu/GrContextOptions.h> 21 #include <mutex> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 #include "cache_data.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 class ShaderCache : public GrContextOptions::PersistentCache { 30 public: 31 static ShaderCache& Instance(); 32 33 virtual void InitShaderCache(const char *identity, const size_t size, bool isUni); InitShaderCache()34 virtual void InitShaderCache() 35 { 36 InitShaderCache(nullptr, 0, false); 37 } 38 39 virtual void SetFilePath(const std::string& filename); 40 41 sk_sp<SkData> load(const SkData &key) override; 42 void store(const SkData &key, const SkData &data) override; 43 44 private: 45 ShaderCache() = default; 46 ShaderCache(const ShaderCache &) = delete; 47 void operator=(const ShaderCache &) = delete; 48 GetCacheData()49 CacheData *GetCacheData() 50 { 51 return cacheData_.get(); 52 } 53 54 void WriteToDisk(); 55 56 bool initialized_ = false; 57 std::unique_ptr<CacheData> cacheData_; 58 std::string filePath_; 59 std::vector<uint8_t> idHash_; 60 mutable std::mutex mutex_; 61 62 bool savePending_ = false; 63 unsigned int saveDelaySeconds_ = 3; 64 65 size_t bufferSize_ = 16 * 1024; 66 bool cacheDirty_ = false; 67 68 static constexpr uint8_t ID_KEY = 0; 69 70 static constexpr size_t MAX_KEY_SIZE = 1024; 71 static constexpr size_t MAX_VALUE_SIZE = MAX_KEY_SIZE * 512; 72 static constexpr size_t MAX_TOTAL_SIZE = MAX_VALUE_SIZE * 4; 73 static constexpr size_t MAX_UNIRENDER_SIZE = MAX_VALUE_SIZE * 10; 74 }; 75 } // namespace Rosen 76 } // namespace OHOS 77 #endif // OHOS_SHADER_CACHE_H 78