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 #ifndef USE_ROSEN_DRAWING 19 #if defined(NEW_SKIA) 20 #include <include/gpu/GrDirectContext.h> 21 #else 22 #include <include/gpu/GrContext.h> 23 #endif 24 #include <include/gpu/GrContextOptions.h> 25 #else 26 #include "image/gpu_context.h" 27 #endif 28 #include <mutex> 29 #include <memory> 30 #include <string> 31 #include <vector> 32 #include "cache_data.h" 33 34 namespace OHOS { 35 namespace Rosen { 36 #ifndef USE_ROSEN_DRAWING 37 class ShaderCache : public GrContextOptions::PersistentCache { 38 #else 39 class ShaderCache : public Drawing::GPUContextOptions::PersistentCache { 40 #endif 41 public: 42 static ShaderCache& Instance(); 43 44 virtual void InitShaderCache(const char *identity, const size_t size, bool isUni); InitShaderCache()45 virtual void InitShaderCache() 46 { 47 InitShaderCache(nullptr, 0, false); 48 } 49 50 virtual void SetFilePath(const std::string& filename); 51 52 #ifndef USE_ROSEN_DRAWING 53 sk_sp<SkData> load(const SkData &key) override; 54 void store(const SkData &key, const SkData &data) override; 55 #else 56 std::shared_ptr<Drawing::Data> Load(const Drawing::Data &key) override; 57 void Store(const Drawing::Data &key, const Drawing::Data &data) override; 58 #endif 59 IfInitialized()60 bool IfInitialized() const 61 { 62 return initialized_; 63 } 64 QuerryShaderSize()65 size_t QuerryShaderSize() const 66 { 67 return cacheData_->GetTotalSize(); 68 } 69 QuerryShaderNum()70 size_t QuerryShaderNum() const 71 { 72 return cacheData_->GetShaderNum(); 73 } 74 CleanAllShaders()75 size_t CleanAllShaders() const 76 { 77 cacheData_->Clear(); 78 return 0; 79 } 80 81 private: 82 ShaderCache() = default; 83 ~ShaderCache(); 84 ShaderCache(const ShaderCache &) = delete; 85 void operator=(const ShaderCache &) = delete; 86 87 void WriteToDisk(); 88 89 bool initialized_ = false; 90 std::unique_ptr<CacheData> cacheData_; 91 std::string filePath_; 92 std::vector<uint8_t> idHash_; 93 mutable std::mutex mutex_; 94 95 bool savePending_ = false; 96 unsigned int saveDelaySeconds_ = 3; 97 98 size_t bufferSize_ = 16 * 1024; 99 bool cacheDirty_ = false; 100 101 static constexpr uint8_t ID_KEY = 0; 102 103 static constexpr size_t MAX_KEY_SIZE = 1024; 104 static constexpr size_t MAX_VALUE_SIZE = MAX_KEY_SIZE * 512; 105 static constexpr size_t MAX_TOTAL_SIZE = MAX_VALUE_SIZE * 4; 106 static constexpr size_t MAX_UNIRENDER_SIZE = MAX_VALUE_SIZE * 10; 107 }; 108 } // namespace Rosen 109 } // namespace OHOS 110 #endif // OHOS_SHADER_CACHE_H 111